2019-05-19 12:08:55 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2006-08-31 10:50:04 +00:00
|
|
|
#include <linux/compat.h>
|
2012-10-05 00:14:57 +00:00
|
|
|
#include <linux/uaccess.h>
|
2017-12-11 11:35:09 +00:00
|
|
|
#include <linux/iversion.h>
|
2008-11-06 20:53:46 +00:00
|
|
|
#include "fat.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-04-30 14:08:18 +00:00
|
|
|
/*
|
|
|
|
* 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))
|
|
|
|
|
2012-07-30 21:42:16 +00:00
|
|
|
static inline unsigned char fat_tolower(unsigned char c)
|
|
|
|
{
|
|
|
|
return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00: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);
|
|
|
|
}
|
|
|
|
|
2005-09-06 22:17:12 +00: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 */
|
2019-01-03 23:28:00 +00:00
|
|
|
if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO))
|
2005-09-06 22:17:12 +00:00
|
|
|
return;
|
|
|
|
|
2006-01-08 09:02:09 +00:00
|
|
|
bh = sb_find_get_block(sb, phys);
|
|
|
|
if (bh == NULL || !buffer_uptodate(bh)) {
|
2005-09-06 22:17:12 +00:00
|
|
|
for (sec = 0; sec < sbi->sec_per_clus; sec++)
|
|
|
|
sb_breadahead(sb, phys + sec);
|
|
|
|
}
|
|
|
|
brelse(bh);
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00: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;
|
2006-01-08 09:02:11 +00:00
|
|
|
unsigned long mapped_blocks;
|
|
|
|
int err, offset;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
next:
|
2019-09-25 23:47:24 +00:00
|
|
|
brelse(*bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
*bh = NULL;
|
|
|
|
iblock = *pos >> sb->s_blocksize_bits;
|
2016-01-20 22:59:46 +00:00
|
|
|
err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0, false);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err || !phys)
|
|
|
|
return -1; /* beyond EOF or error */
|
|
|
|
|
2005-09-06 22:17:12 +00:00
|
|
|
fat_dir_readahead(dir, iblock, phys);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*bh = sb_bread(sb, phys);
|
|
|
|
if (*bh == NULL) {
|
2012-05-31 23:26:14 +00:00
|
|
|
fat_msg_ratelimit(sb, KERN_ERR,
|
|
|
|
"Directory bread(block %llu) failed", (llu)phys);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 &&
|
2012-10-05 00:14:57 +00:00
|
|
|
(*de - (struct msdos_dir_entry *)(*bh)->b_data) <
|
|
|
|
MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
|
2005-04-16 22:20:36 +00:00
|
|
|
*pos += sizeof(struct msdos_dir_entry);
|
|
|
|
(*de)++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fat__get_entry(dir, pos, bh, de);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-21 23:13:35 +00:00
|
|
|
* Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
|
2005-04-16 22:20:36 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2011-04-12 12:08:38 +00:00
|
|
|
static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
|
|
|
|
const wchar_t *uni, int len, struct nls_table *nls)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-04-12 12:08:38 +00:00
|
|
|
int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
|
2008-07-25 08:46:43 +00:00
|
|
|
const wchar_t *ip;
|
|
|
|
wchar_t ec;
|
2010-03-15 20:48:08 +00:00
|
|
|
unsigned char *op;
|
2005-04-16 22:20:36 +00:00
|
|
|
int charlen;
|
|
|
|
|
|
|
|
ip = uni;
|
|
|
|
op = ascii;
|
|
|
|
|
2008-04-28 09:16:29 +00:00
|
|
|
while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
ec = *ip++;
|
2012-10-05 00:14:57 +00:00
|
|
|
charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE);
|
|
|
|
if (charlen > 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
op += charlen;
|
2008-04-28 09:16:29 +00:00
|
|
|
len -= charlen;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
if (uni_xlate == 1) {
|
2010-03-15 20:48:08 +00:00
|
|
|
*op++ = ':';
|
2011-11-01 00:12:57 +00:00
|
|
|
op = hex_byte_pack(op, ec >> 8);
|
|
|
|
op = hex_byte_pack(op, ec);
|
2008-04-28 09:16:29 +00:00
|
|
|
len -= 5;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
*op++ = '?';
|
2008-04-28 09:16:29 +00:00
|
|
|
len--;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-28 09:16:29 +00:00
|
|
|
|
|
|
|
if (unlikely(*ip)) {
|
2012-10-05 00:14:57 +00:00
|
|
|
fat_msg(sb, KERN_WARNING,
|
|
|
|
"filename was truncated while converting.");
|
2008-04-28 09:16:29 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*op = 0;
|
2012-10-05 00:14:57 +00:00
|
|
|
return op - ascii;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 12:08:38 +00:00
|
|
|
static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
|
2008-07-25 08:46:43 +00:00
|
|
|
unsigned char *buf, int size)
|
|
|
|
{
|
2011-04-12 12:08:38 +00:00
|
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
2008-07-25 08:46:43 +00:00
|
|
|
if (sbi->options.utf8)
|
2009-04-30 14:08:18 +00:00
|
|
|
return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
|
|
|
|
UTF16_HOST_ENDIAN, buf, size);
|
2008-07-25 08:46:43 +00:00
|
|
|
else
|
2011-04-12 12:08:38 +00:00
|
|
|
return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
|
2008-07-25 08:46:43 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
2012-10-05 00:14:57 +00:00
|
|
|
fat_short2lower_uni(struct nls_table *t, unsigned char *c,
|
|
|
|
int clen, wchar_t *uni)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2012-10-05 00:14:57 +00:00
|
|
|
charlen = t->char2uni(&nc, 1, uni);
|
|
|
|
if (charlen < 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
*uni = 0x003f; /* a question mark */
|
|
|
|
charlen = 1;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
*uni = wc;
|
|
|
|
|
|
|
|
return charlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-07-25 08:46:43 +00: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);
|
|
|
|
}
|
|
|
|
|
2005-10-30 23:03:50 +00: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) {
|
2008-04-28 09:16:29 +00:00
|
|
|
*unicode = __getname();
|
2005-10-30 23:03:50 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-30 21:42:16 +00: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));
|
2018-10-30 22:06:46 +00:00
|
|
|
/* For an explanation of the special treatment of 0x05 in
|
|
|
|
* filenames, see msdos_format_name in namei_msdos.c
|
|
|
|
*/
|
2012-07-30 21:42:16 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2012-12-20 23:05:46 +00:00
|
|
|
* Return values: negative -> error/not found, 0 -> found.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
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;
|
2008-04-28 09:16:29 +00:00
|
|
|
unsigned char nr_slots;
|
2005-04-16 22:20:36 +00:00
|
|
|
wchar_t *unicode = NULL;
|
2008-07-25 08:46:44 +00:00
|
|
|
unsigned char bufname[FAT_MAX_SHORT_SIZE];
|
2005-04-16 22:20:36 +00:00
|
|
|
loff_t cpos = 0;
|
2012-07-30 21:42:16 +00:00
|
|
|
int err, len;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err = -ENOENT;
|
2008-07-25 08:46:43 +00:00
|
|
|
while (1) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto end_of_dir;
|
2005-04-16 22:20:36 +00:00
|
|
|
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) {
|
2005-10-30 23:03:50 +00:00
|
|
|
int status = fat_parse_long(inode, &cpos, &bh, &de,
|
|
|
|
&unicode, &nr_slots);
|
2008-11-06 20:53:48 +00:00
|
|
|
if (status < 0) {
|
|
|
|
err = status;
|
|
|
|
goto end_of_dir;
|
|
|
|
} else if (status == PARSE_INVALID)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
2005-10-30 23:03:50 +00:00
|
|
|
else if (status == PARSE_NOT_LONGNAME)
|
|
|
|
goto parse_record;
|
|
|
|
else if (status == PARSE_EOF)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto end_of_dir;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 21:42:16 +00:00
|
|
|
/* 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)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
/* Compare shortname */
|
|
|
|
if (fat_name_match(sbi, name, name_len, bufname, len))
|
|
|
|
goto found;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (nr_slots) {
|
2008-07-25 08:46:44 +00:00
|
|
|
void *longname = unicode + FAT_MAX_UNI_CHARS;
|
|
|
|
int size = PATH_MAX - FAT_MAX_UNI_SIZE;
|
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
/* Compare longname */
|
2011-04-12 12:08:38 +00:00
|
|
|
len = fat_uni_to_x8(sb, unicode, longname, size);
|
2008-07-25 08:46:44 +00:00
|
|
|
if (fat_name_match(sbi, name, name_len, longname, len))
|
2008-07-25 08:46:43 +00:00
|
|
|
goto found;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
found:
|
2005-04-16 22:20:36 +00:00
|
|
|
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;
|
2008-07-25 08:46:43 +00:00
|
|
|
end_of_dir:
|
2005-04-16 22:20:36 +00:00
|
|
|
if (unicode)
|
2008-04-28 09:16:29 +00:00
|
|
|
__putname(unicode);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_search_long);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
struct fat_ioctl_filldir_callback {
|
2013-05-22 22:37:16 +00:00
|
|
|
struct dir_context ctx;
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
void __user *dirent;
|
2005-04-16 22:20:36 +00:00
|
|
|
int result;
|
|
|
|
/* for dir ioctl */
|
|
|
|
const char *longname;
|
|
|
|
int long_len;
|
|
|
|
const char *shortname;
|
|
|
|
int short_len;
|
|
|
|
};
|
|
|
|
|
2013-05-22 22:37:16 +00:00
|
|
|
static int __fat_readdir(struct inode *inode, struct file *file,
|
|
|
|
struct dir_context *ctx, int short_only,
|
|
|
|
struct fat_ioctl_filldir_callback *both)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
|
|
struct buffer_head *bh;
|
|
|
|
struct msdos_dir_entry *de;
|
2008-07-25 08:46:43 +00:00
|
|
|
unsigned char nr_slots;
|
2005-04-16 22:20:36 +00:00
|
|
|
wchar_t *unicode = NULL;
|
2012-07-30 21:42:16 +00:00
|
|
|
unsigned char bufname[FAT_MAX_SHORT_SIZE];
|
2005-04-16 22:20:36 +00:00
|
|
|
int isvfat = sbi->options.isvfat;
|
2008-07-25 08:46:44 +00:00
|
|
|
const char *fill_name = NULL;
|
2013-05-22 22:37:16 +00:00
|
|
|
int fake_offset = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
loff_t cpos;
|
2012-07-30 21:42:16 +00:00
|
|
|
int short_len = 0, fill_len = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2012-10-06 10:40:03 +00:00
|
|
|
mutex_lock(&sbi->s_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-05-22 22:37:16 +00:00
|
|
|
cpos = ctx->pos;
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Fake . and .. for the root directory. */
|
|
|
|
if (inode->i_ino == MSDOS_ROOT_INO) {
|
2013-05-22 22:37:16 +00:00
|
|
|
if (!dir_emit_dots(file, ctx))
|
|
|
|
goto out;
|
|
|
|
if (ctx->pos == 2) {
|
|
|
|
fake_offset = 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
cpos = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-07-25 08:46:43 +00:00
|
|
|
if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
|
2005-04-16 22:20:36 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bh = NULL;
|
2008-07-25 08:46:43 +00:00
|
|
|
get_new:
|
2005-04-16 22:20:36 +00:00
|
|
|
if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto end_of_dir;
|
2005-10-30 23:03:50 +00:00
|
|
|
parse_record:
|
2008-07-25 08:46:43 +00:00
|
|
|
nr_slots = 0;
|
2008-07-25 08:46:44 +00:00
|
|
|
/*
|
|
|
|
* Check for long filename entry, but if short_only, we don't
|
|
|
|
* need to parse long filename.
|
|
|
|
*/
|
|
|
|
if (isvfat && !short_only) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (de->name[0] == DELETED_FLAG)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (de->attr != ATTR_EXT && IS_FREE(de->name))
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
|
|
|
if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isvfat && de->attr == ATTR_EXT) {
|
2005-10-30 23:03:50 +00:00
|
|
|
int status = fat_parse_long(inode, &cpos, &bh, &de,
|
2008-07-25 08:46:43 +00:00
|
|
|
&unicode, &nr_slots);
|
2005-10-30 23:03:50 +00:00
|
|
|
if (status < 0) {
|
2015-11-20 23:57:15 +00:00
|
|
|
bh = NULL;
|
2005-10-30 23:03:50 +00:00
|
|
|
ret = status;
|
2015-11-20 23:57:15 +00:00
|
|
|
goto end_of_dir;
|
2005-10-30 23:03:50 +00:00
|
|
|
} else if (status == PARSE_INVALID)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-10-30 23:03:50 +00:00
|
|
|
else if (status == PARSE_NOT_LONGNAME)
|
|
|
|
goto parse_record;
|
|
|
|
else if (status == PARSE_EOF)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto end_of_dir;
|
2008-07-25 08:46:44 +00:00
|
|
|
|
|
|
|
if (nr_slots) {
|
|
|
|
void *longname = unicode + FAT_MAX_UNI_CHARS;
|
|
|
|
int size = PATH_MAX - FAT_MAX_UNI_SIZE;
|
2011-04-12 12:08:38 +00:00
|
|
|
int len = fat_uni_to_x8(sb, unicode, longname, size);
|
2008-07-25 08:46:44 +00:00
|
|
|
|
|
|
|
fill_name = longname;
|
|
|
|
fill_len = len;
|
|
|
|
/* !both && !short_only, so we don't need shortname. */
|
|
|
|
if (!both)
|
|
|
|
goto start_filldir;
|
2013-05-22 22:37:16 +00:00
|
|
|
|
|
|
|
short_len = fat_parse_short(sb, de, bufname,
|
|
|
|
sbi->options.dotsOK);
|
|
|
|
if (short_len == 0)
|
|
|
|
goto record_end;
|
|
|
|
/* hack for fat_ioctl_filldir() */
|
|
|
|
both->longname = fill_name;
|
|
|
|
both->long_len = fill_len;
|
|
|
|
both->shortname = bufname;
|
|
|
|
both->short_len = short_len;
|
|
|
|
fill_name = NULL;
|
|
|
|
fill_len = 0;
|
|
|
|
goto start_filldir;
|
2008-07-25 08:46:44 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 21:42:16 +00:00
|
|
|
short_len = fat_parse_short(sb, de, bufname, sbi->options.dotsOK);
|
|
|
|
if (short_len == 0)
|
2008-07-25 08:46:43 +00:00
|
|
|
goto record_end;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-05-22 22:37:16 +00:00
|
|
|
fill_name = bufname;
|
|
|
|
fill_len = short_len;
|
2008-07-25 08:46:44 +00:00
|
|
|
|
|
|
|
start_filldir:
|
2015-11-20 23:57:15 +00:00
|
|
|
ctx->pos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
|
|
|
|
if (fake_offset && ctx->pos < 2)
|
|
|
|
ctx->pos = 2;
|
2013-05-22 22:37:16 +00:00
|
|
|
|
|
|
|
if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) {
|
|
|
|
if (!dir_emit_dot(file, ctx))
|
|
|
|
goto fill_failed;
|
|
|
|
} else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
|
|
|
|
if (!dir_emit_dotdot(file, ctx))
|
|
|
|
goto fill_failed;
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2013-05-22 22:37:16 +00:00
|
|
|
unsigned long inum;
|
2005-04-16 22:20:36 +00:00
|
|
|
loff_t i_pos = fat_make_i_pos(sb, bh, de);
|
|
|
|
struct inode *tmp = fat_iget(sb, i_pos);
|
|
|
|
if (tmp) {
|
|
|
|
inum = tmp->i_ino;
|
|
|
|
iput(tmp);
|
|
|
|
} else
|
|
|
|
inum = iunique(sb, MSDOS_ROOT_INO);
|
2013-05-22 22:37:16 +00:00
|
|
|
if (!dir_emit(ctx, fill_name, fill_len, inum,
|
|
|
|
(de->attr & ATTR_DIR) ? DT_DIR : DT_REG))
|
|
|
|
goto fill_failed;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
record_end:
|
2013-05-22 22:37:16 +00:00
|
|
|
fake_offset = 0;
|
|
|
|
ctx->pos = cpos;
|
2008-07-25 08:46:43 +00:00
|
|
|
goto get_new;
|
2015-11-20 23:57:15 +00:00
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
end_of_dir:
|
2015-11-20 23:57:15 +00:00
|
|
|
if (fake_offset && cpos < 2)
|
|
|
|
ctx->pos = 2;
|
|
|
|
else
|
|
|
|
ctx->pos = cpos;
|
2008-07-25 08:46:43 +00:00
|
|
|
fill_failed:
|
2005-09-06 22:17:12 +00:00
|
|
|
brelse(bh);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (unicode)
|
2008-04-28 09:16:29 +00:00
|
|
|
__putname(unicode);
|
2005-04-16 22:20:36 +00:00
|
|
|
out:
|
2012-10-06 10:40:03 +00:00
|
|
|
mutex_unlock(&sbi->s_lock);
|
2015-11-20 23:57:15 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-22 22:37:16 +00:00
|
|
|
static int fat_readdir(struct file *file, struct dir_context *ctx)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-05-22 22:37:16 +00:00
|
|
|
return __fat_readdir(file_inode(file), file, ctx, 0, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
#define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
|
2014-10-30 16:37:34 +00:00
|
|
|
static int func(struct dir_context *ctx, const char *name, int name_len, \
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
loff_t offset, u64 ino, unsigned int d_type) \
|
|
|
|
{ \
|
2014-10-30 16:37:34 +00:00
|
|
|
struct fat_ioctl_filldir_callback *buf = \
|
|
|
|
container_of(ctx, struct fat_ioctl_filldir_callback, ctx); \
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
struct dirent_type __user *d1 = buf->dirent; \
|
|
|
|
struct dirent_type __user *d2 = d1 + 1; \
|
|
|
|
\
|
|
|
|
if (buf->result) \
|
|
|
|
return -EINVAL; \
|
|
|
|
buf->result++; \
|
|
|
|
\
|
|
|
|
if (name != NULL) { \
|
|
|
|
/* dirent has only short name */ \
|
|
|
|
if (name_len >= sizeof(d1->d_name)) \
|
|
|
|
name_len = sizeof(d1->d_name) - 1; \
|
|
|
|
\
|
|
|
|
if (put_user(0, d2->d_name) || \
|
|
|
|
put_user(0, &d2->d_reclen) || \
|
|
|
|
copy_to_user(d1->d_name, name, name_len) || \
|
|
|
|
put_user(0, d1->d_name + name_len) || \
|
|
|
|
put_user(name_len, &d1->d_reclen)) \
|
|
|
|
goto efault; \
|
|
|
|
} else { \
|
|
|
|
/* dirent has short and long name */ \
|
|
|
|
const char *longname = buf->longname; \
|
|
|
|
int long_len = buf->long_len; \
|
|
|
|
const char *shortname = buf->shortname; \
|
|
|
|
int short_len = buf->short_len; \
|
|
|
|
\
|
|
|
|
if (long_len >= sizeof(d1->d_name)) \
|
|
|
|
long_len = sizeof(d1->d_name) - 1; \
|
|
|
|
if (short_len >= sizeof(d1->d_name)) \
|
|
|
|
short_len = sizeof(d1->d_name) - 1; \
|
|
|
|
\
|
|
|
|
if (copy_to_user(d2->d_name, longname, long_len) || \
|
|
|
|
put_user(0, d2->d_name + long_len) || \
|
|
|
|
put_user(long_len, &d2->d_reclen) || \
|
|
|
|
put_user(ino, &d2->d_ino) || \
|
|
|
|
put_user(offset, &d2->d_off) || \
|
|
|
|
copy_to_user(d1->d_name, shortname, short_len) || \
|
|
|
|
put_user(0, d1->d_name + short_len) || \
|
|
|
|
put_user(short_len, &d1->d_reclen)) \
|
|
|
|
goto efault; \
|
|
|
|
} \
|
|
|
|
return 0; \
|
|
|
|
efault: \
|
|
|
|
buf->result = -EFAULT; \
|
|
|
|
return -EFAULT; \
|
|
|
|
}
|
|
|
|
|
2008-07-25 08:46:43 +00:00
|
|
|
FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
|
2013-05-22 22:37:16 +00:00
|
|
|
static int fat_ioctl_readdir(struct inode *inode, struct file *file,
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
void __user *dirent, filldir_t filldir,
|
|
|
|
int short_only, int both)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-05-22 22:37:16 +00:00
|
|
|
struct fat_ioctl_filldir_callback buf = {
|
|
|
|
.ctx.actor = filldir,
|
|
|
|
.dirent = dirent
|
|
|
|
};
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
buf.dirent = dirent;
|
|
|
|
buf.result = 0;
|
2016-05-01 03:26:25 +00:00
|
|
|
inode_lock_shared(inode);
|
2013-05-22 22:37:16 +00:00
|
|
|
buf.ctx.pos = file->f_pos;
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
ret = -ENOENT;
|
|
|
|
if (!IS_DEADDIR(inode)) {
|
2013-05-22 22:37:16 +00:00
|
|
|
ret = __fat_readdir(inode, file, &buf.ctx,
|
|
|
|
short_only, both ? &buf : NULL);
|
|
|
|
file->f_pos = buf.ctx.pos;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2016-05-01 03:26:25 +00:00
|
|
|
inode_unlock_shared(inode);
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
ret = buf.result;
|
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2010-05-16 23:13:47 +00:00
|
|
|
static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-01-23 22:07:38 +00:00
|
|
|
struct inode *inode = file_inode(filp);
|
2008-07-25 08:46:43 +00:00
|
|
|
struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
int short_only, both;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
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:
|
2010-05-16 23:13:47 +00:00
|
|
|
return fat_generic_ioctl(filp, cmd, arg);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
Remove 'type' argument from access_ok() function
Nobody has actually used the type (VERIFY_READ vs VERIFY_WRITE) argument
of the user address range verification function since we got rid of the
old racy i386-only code to walk page tables by hand.
It existed because the original 80386 would not honor the write protect
bit when in kernel mode, so you had to do COW by hand before doing any
user access. But we haven't supported that in a long time, and these
days the 'type' argument is a purely historical artifact.
A discussion about extending 'user_access_begin()' to do the range
checking resulted this patch, because there is no way we're going to
move the old VERIFY_xyz interface to that model. And it's best done at
the end of the merge window when I've done most of my merges, so let's
just get this done once and for all.
This patch was mostly done with a sed-script, with manual fix-ups for
the cases that weren't of the trivial 'access_ok(VERIFY_xyz' form.
There were a couple of notable cases:
- csky still had the old "verify_area()" name as an alias.
- the iter_iov code had magical hardcoded knowledge of the actual
values of VERIFY_{READ,WRITE} (not that they mattered, since nothing
really used it)
- microblaze used the type argument for a debug printout
but other than those oddities this should be a total no-op patch.
I tried to fix up all architectures, did fairly extensive grepping for
access_ok() uses, and the changes are trivial, but I may have missed
something. Any missed conversion should be trivially fixable, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 02:57:57 +00:00
|
|
|
if (!access_ok(d1, sizeof(struct __fat_dirent[2])))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
|
|
|
|
short_only, both);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-08-31 10:50:04 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
#define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
|
|
|
|
#define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
|
|
|
|
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
|
2006-08-31 10:50:04 +00:00
|
|
|
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
|
2006-08-31 10:50:04 +00:00
|
|
|
unsigned long arg)
|
|
|
|
{
|
2013-01-23 22:07:38 +00:00
|
|
|
struct inode *inode = file_inode(filp);
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
struct compat_dirent __user *d1 = compat_ptr(arg);
|
|
|
|
int short_only, both;
|
2006-08-31 10:50:04 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case VFAT_IOCTL_READDIR_SHORT32:
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
short_only = 1;
|
|
|
|
both = 0;
|
|
|
|
break;
|
|
|
|
case VFAT_IOCTL_READDIR_BOTH32:
|
|
|
|
short_only = 0;
|
|
|
|
both = 1;
|
2006-08-31 10:50:04 +00:00
|
|
|
break;
|
|
|
|
default:
|
2010-05-16 23:13:47 +00:00
|
|
|
return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
|
2006-08-31 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
Remove 'type' argument from access_ok() function
Nobody has actually used the type (VERIFY_READ vs VERIFY_WRITE) argument
of the user address range verification function since we got rid of the
old racy i386-only code to walk page tables by hand.
It existed because the original 80386 would not honor the write protect
bit when in kernel mode, so you had to do COW by hand before doing any
user access. But we haven't supported that in a long time, and these
days the 'type' argument is a purely historical artifact.
A discussion about extending 'user_access_begin()' to do the range
checking resulted this patch, because there is no way we're going to
move the old VERIFY_xyz interface to that model. And it's best done at
the end of the merge window when I've done most of my merges, so let's
just get this done once and for all.
This patch was mostly done with a sed-script, with manual fix-ups for
the cases that weren't of the trivial 'access_ok(VERIFY_xyz' form.
There were a couple of notable cases:
- csky still had the old "verify_area()" name as an alias.
- the iter_iov code had magical hardcoded knowledge of the actual
values of VERIFY_{READ,WRITE} (not that they mattered, since nothing
really used it)
- microblaze used the type argument for a debug printout
but other than those oddities this should be a total no-op patch.
I tried to fix up all architectures, did fairly extensive grepping for
access_ok() uses, and the changes are trivial, but I may have missed
something. Any missed conversion should be trivially fixable, though.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2019-01-04 02:57:57 +00:00
|
|
|
if (!access_ok(d1, sizeof(struct compat_dirent[2])))
|
fat: fix VFAT compat ioctls on 64-bit systems
If you compile and run the below test case in an msdos or vfat directory on
an x86-64 system with -m32 you'll get garbage in the kernel_dirent struct
followed by a SIGSEGV.
The patch fixes this.
Reported and initial fix by Bart Oldeman
#include <sys/types.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
struct kernel_dirent {
long d_ino;
long d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
#define VFAT_IOCTL_READDIR_BOTH _IOR('r', 1, struct kernel_dirent [2])
#define VFAT_IOCTL_READDIR_SHORT _IOR('r', 2, struct kernel_dirent [2])
int main(void)
{
int fd = open(".", O_RDONLY);
struct kernel_dirent de[2];
while (1) {
int i = ioctl(fd, VFAT_IOCTL_READDIR_BOTH, (long)de);
if (i == -1) break;
if (de[0].d_reclen == 0) break;
printf("SFN: reclen=%2d off=%d ino=%d, %-12s",
de[0].d_reclen, de[0].d_off, de[0].d_ino, de[0].d_name);
if (de[1].d_reclen)
printf("\tLFN: reclen=%2d off=%d ino=%d, %s",
de[1].d_reclen, de[1].d_off, de[1].d_ino, de[1].d_name);
printf("\n");
}
return 0;
}
Signed-off-by: Bart Oldeman <bartoldeman@users.sourceforge.net>
Signed-off-by: OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2007-05-08 07:31:28 +00:00
|
|
|
return -EFAULT;
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
|
|
|
|
short_only, both);
|
2006-08-31 10:50:04 +00:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_COMPAT */
|
|
|
|
|
2006-03-28 09:56:42 +00:00
|
|
|
const struct file_operations fat_dir_operations = {
|
2008-11-06 20:53:47 +00:00
|
|
|
.llseek = generic_file_llseek,
|
2005-04-16 22:20:36 +00:00
|
|
|
.read = generic_read_dir,
|
2016-05-01 03:26:25 +00:00
|
|
|
.iterate_shared = fat_readdir,
|
2010-05-16 23:13:47 +00:00
|
|
|
.unlocked_ioctl = fat_dir_ioctl,
|
2006-08-31 10:50:04 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
.compat_ioctl = fat_compat_dir_ioctl,
|
|
|
|
#endif
|
2009-06-07 17:44:36 +00:00
|
|
|
.fsync = fat_file_fsync,
|
2005-04-16 22:20:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-05 00:14:45 +00:00
|
|
|
* 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.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
|
2012-10-05 00:14:45 +00:00
|
|
|
struct msdos_dir_entry **de)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2012-10-05 00:14:45 +00:00
|
|
|
loff_t offset = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-10-05 00:14:45 +00:00
|
|
|
*de = NULL;
|
2005-04-16 22:20:36 +00:00
|
|
|
while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
|
2012-10-05 00:14:45 +00:00
|
|
|
if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME))
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
bh = NULL;
|
|
|
|
cpos = 0;
|
|
|
|
while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
|
|
|
|
if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
|
|
|
|
strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
|
|
|
|
result = -ENOTEMPTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
brelse(bh);
|
|
|
|
return result;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_dir_empty);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
bh = NULL;
|
|
|
|
cpos = 0;
|
|
|
|
while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
|
|
|
|
if (de->attr & ATTR_DIR)
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
brelse(bh);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, const unsigned char *name,
|
|
|
|
struct fat_slot_info *sinfo)
|
|
|
|
{
|
|
|
|
struct super_block *sb = dir->i_sb;
|
|
|
|
|
|
|
|
sinfo->slot_off = 0;
|
|
|
|
sinfo->bh = NULL;
|
|
|
|
while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
|
|
|
|
&sinfo->de) >= 0) {
|
|
|
|
if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
|
|
|
|
sinfo->slot_off -= sizeof(*sinfo->de);
|
|
|
|
sinfo->nr_slots = 1;
|
|
|
|
sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_scan);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-29 23:21:14 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
sinfo->slot_off = 0;
|
|
|
|
sinfo->bh = NULL;
|
|
|
|
while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
|
|
|
|
&sinfo->de) >= 0) {
|
|
|
|
if (fat_get_start(MSDOS_SB(sb), sinfo->de) == i_logstart) {
|
|
|
|
sinfo->slot_off -= sizeof(*sinfo->de);
|
|
|
|
sinfo->nr_slots = 1;
|
|
|
|
sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
|
|
|
|
{
|
|
|
|
struct super_block *sb = dir->i_sb;
|
|
|
|
struct buffer_head *bh;
|
|
|
|
struct msdos_dir_entry *de, *endp;
|
|
|
|
int err = 0, orig_slots;
|
|
|
|
|
|
|
|
while (nr_slots) {
|
|
|
|
bh = NULL;
|
|
|
|
if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
|
|
|
|
err = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
orig_slots = nr_slots;
|
|
|
|
endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
|
|
|
|
while (nr_slots && de < endp) {
|
|
|
|
de->name[0] = DELETED_FLAG;
|
|
|
|
de++;
|
|
|
|
nr_slots--;
|
|
|
|
}
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bh, dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (IS_DIRSYNC(dir))
|
|
|
|
err = sync_dirty_buffer(bh);
|
|
|
|
brelse(bh);
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* pos is *next* de's position, so this does `- sizeof(de)' */
|
|
|
|
pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
|
|
|
|
{
|
2011-04-12 12:08:38 +00:00
|
|
|
struct super_block *sb = dir->i_sb;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct msdos_dir_entry *de;
|
|
|
|
struct buffer_head *bh;
|
|
|
|
int err = 0, nr_slots;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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--;
|
|
|
|
}
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bh, dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (IS_DIRSYNC(dir))
|
|
|
|
err = sync_dirty_buffer(bh);
|
|
|
|
brelse(bh);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2017-12-11 11:35:09 +00:00
|
|
|
inode_inc_iversion(dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
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) {
|
2011-04-12 12:08:38 +00:00
|
|
|
fat_msg(sb, KERN_WARNING,
|
|
|
|
"Couldn't remove the long name slots");
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 22:06:57 +00:00
|
|
|
fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (IS_DIRSYNC(dir))
|
|
|
|
(void)fat_sync_inode(dir);
|
|
|
|
else
|
|
|
|
mark_inode_dirty(dir);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_remove_entries);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int 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;
|
|
|
|
}
|
2019-09-23 22:32:53 +00:00
|
|
|
/* Avoid race with userspace read via bdev */
|
|
|
|
lock_buffer(bhs[n]);
|
2005-04-16 22:20:36 +00:00
|
|
|
memset(bhs[n]->b_data, 0, sb->s_blocksize);
|
|
|
|
set_buffer_uptodate(bhs[n]);
|
2019-09-23 22:32:53 +00:00
|
|
|
unlock_buffer(bhs[n]);
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bhs[n], dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-22 04:59:48 +00:00
|
|
|
int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct super_block *sb = dir->i_sb;
|
|
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
|
|
struct buffer_head *bhs[MAX_BUF_PER_PAGE];
|
|
|
|
struct msdos_dir_entry *de;
|
|
|
|
sector_t blknr;
|
|
|
|
__le16 date, time;
|
2008-11-06 20:53:47 +00:00
|
|
|
u8 time_cs;
|
2005-04-16 22:20:36 +00:00
|
|
|
int err, cluster;
|
|
|
|
|
|
|
|
err = fat_alloc_clusters(dir, &cluster, 1);
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
blknr = fat_clus_to_blknr(sbi, cluster);
|
|
|
|
bhs[0] = sb_getblk(sb, blknr);
|
|
|
|
if (!bhs[0]) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2008-11-06 20:53:47 +00:00
|
|
|
fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
de = (struct msdos_dir_entry *)bhs[0]->b_data;
|
2019-09-23 22:32:53 +00:00
|
|
|
/* Avoid race with userspace read via bdev */
|
|
|
|
lock_buffer(bhs[0]);
|
2005-04-16 22:20:36 +00:00
|
|
|
/* filling the new directory slots ("." and ".." entries) */
|
|
|
|
memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
|
|
|
|
memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
|
|
|
|
de->attr = de[1].attr = ATTR_DIR;
|
|
|
|
de[0].lcase = de[1].lcase = 0;
|
|
|
|
de[0].time = de[1].time = time;
|
|
|
|
de[0].date = de[1].date = date;
|
|
|
|
if (sbi->options.isvfat) {
|
|
|
|
/* extra timestamps */
|
|
|
|
de[0].ctime = de[1].ctime = time;
|
2008-11-06 20:53:47 +00:00
|
|
|
de[0].ctime_cs = de[1].ctime_cs = time_cs;
|
2005-04-16 22:20:36 +00:00
|
|
|
de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
|
|
|
|
} else {
|
|
|
|
de[0].ctime = de[1].ctime = 0;
|
2008-11-06 20:53:47 +00:00
|
|
|
de[0].ctime_cs = de[1].ctime_cs = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
|
|
|
|
}
|
2012-10-05 00:14:41 +00:00
|
|
|
fat_set_start(&de[0], cluster);
|
|
|
|
fat_set_start(&de[1], MSDOS_I(dir)->i_logstart);
|
2005-04-16 22:20:36 +00:00
|
|
|
de[0].size = de[1].size = 0;
|
|
|
|
memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
|
|
|
|
set_buffer_uptodate(bhs[0]);
|
2019-09-23 22:32:53 +00:00
|
|
|
unlock_buffer(bhs[0]);
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bhs[0], dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
|
|
|
|
if (err)
|
|
|
|
goto error_free;
|
|
|
|
|
|
|
|
return cluster;
|
|
|
|
|
|
|
|
error_free:
|
|
|
|
fat_free_clusters(dir, cluster);
|
|
|
|
error:
|
|
|
|
return err;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
static int 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;
|
|
|
|
unsigned long 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);
|
2019-09-23 22:32:53 +00:00
|
|
|
/* Avoid race with userspace read via bdev */
|
|
|
|
lock_buffer(bhs[n]);
|
2005-04-16 22:20:36 +00:00
|
|
|
memcpy(bhs[n]->b_data, slots, copy);
|
|
|
|
set_buffer_uptodate(bhs[n]);
|
2019-09-23 22:32:53 +00:00
|
|
|
unlock_buffer(bhs[n]);
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bhs[n], dir);
|
2019-09-23 22:32:53 +00:00
|
|
|
slots += copy;
|
|
|
|
size -= copy;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!size)
|
|
|
|
break;
|
|
|
|
n++;
|
|
|
|
blknr++;
|
|
|
|
}
|
|
|
|
} while (++i < *nr_cluster);
|
|
|
|
|
|
|
|
memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
|
|
|
|
offset = copy - sizeof(struct msdos_dir_entry);
|
|
|
|
get_bh(bhs[n]);
|
|
|
|
*bh = bhs[n];
|
|
|
|
*de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
|
|
|
|
*i_pos = fat_make_i_pos(sb, *bh, *de);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
|
|
|
|
struct fat_slot_info *sinfo)
|
|
|
|
{
|
|
|
|
struct super_block *sb = dir->i_sb;
|
|
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
|
|
struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
|
2011-08-17 10:10:06 +00:00
|
|
|
struct msdos_dir_entry *uninitialized_var(de);
|
2005-04-16 22:20:36 +00:00
|
|
|
int err, free_slots, i, nr_bhs;
|
|
|
|
loff_t pos, i_pos;
|
|
|
|
|
|
|
|
sinfo->nr_slots = nr_slots;
|
|
|
|
|
2012-12-20 23:05:46 +00:00
|
|
|
/* First stage: search free directory entries */
|
2005-04-16 22:20:36 +00:00
|
|
|
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) {
|
2019-01-03 23:28:00 +00:00
|
|
|
if (!is_fat32(sbi))
|
2005-04-16 22:20:36 +00:00
|
|
|
goto error;
|
|
|
|
} else if (MSDOS_I(dir)->i_start == 0) {
|
2011-04-12 12:08:38 +00:00
|
|
|
fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
|
2005-04-16 22:20:36 +00:00
|
|
|
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);
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bhs[i], dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
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);
|
2009-06-07 17:44:36 +00:00
|
|
|
mark_buffer_dirty_inode(bhs[i], dir);
|
2005-04-16 22:20:36 +00:00
|
|
|
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)) {
|
2009-06-03 17:34:22 +00:00
|
|
|
fat_fs_error(sb, "Odd directory size");
|
2005-04-16 22:20:36 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
error_remove:
|
|
|
|
brelse(bh);
|
|
|
|
if (free_slots)
|
|
|
|
__fat_remove_entries(dir, pos, free_slots);
|
|
|
|
return err;
|
|
|
|
}
|
2006-01-08 09:02:10 +00:00
|
|
|
EXPORT_SYMBOL_GPL(fat_add_entries);
|