Changes in 4.9.318 USB: new quirk for Dell Gen 2 devices ptrace/xtensa: Replace PT_SINGLESTEP with TIF_SINGLESTEP ptrace: Reimplement PTRACE_KILL by always sending SIGKILL btrfs: add "0x" prefix for unsupported optional features drm/virtio: fix NULL pointer dereference in virtio_gpu_conn_get_modes mwifiex: add mutex lock for call in mwifiex_dfs_chan_sw_work_queue b43legacy: Fix assigning negative value to unsigned variable b43: Fix assigning negative value to unsigned variable ipw2x00: Fix potential NULL dereference in libipw_xmit() ACPICA: Avoid cache flush inside virtual machines ALSA: jack: Access input_dev under mutex drm/amd/pm: fix double free in si_parse_power_table() ath9k: fix QCA9561 PA bias level media: cx25821: Fix the warning when removing the module scsi: megaraid: Fix error check return value of register_chrdev() drm/amd/pm: fix the compile warning ipv6: Don't send rs packets to the interface of ARPHRD_TUNNEL ASoC: dapm: Don't fold register value changes into notifications dma-debug: change allocation mode from GFP_NOWAIT to GFP_ATIOMIC ipmi:ssif: Check for NULL msg when handling events and messages openrisc: start CPU timer early in boot nvme-pci: fix a NULL pointer dereference in nvme_alloc_admin_tags ASoC: rt5645: Fix errorenous cleanup order media: exynos4-is: Fix compile warning rxrpc: Return an error to sendmsg if call failed eth: tg3: silence the GCC 12 array-bounds warning fs: jfs: fix possible NULL pointer dereference in dbFree() ARM: OMAP1: clock: Fix UART rate reporting algorithm fat: add ratelimit to fat*_ent_bread() ARM: versatile: Add missing of_node_put in dcscb_init ARM: dts: exynos: add atmel,24c128 fallback to Samsung EEPROM ARM: hisi: Add missing of_node_put after of_find_compatible_node PCI: Avoid pci_dev_lock() AB/BA deadlock with sriov_numvfs_store() powerpc/xics: fix refcount leak in icp_opal_init() macintosh/via-pmu: Fix build failure when CONFIG_INPUT is disabled drm: fix EDID struct for old ARM OABI format ASoC: mediatek: Fix error handling in mt8173_max98090_dev_probe x86/delay: Fix the wrong asm constraint in delay_loop() drm/mediatek: Fix mtk_cec_mask() spi: spi-ti-qspi: Fix return value handling of wait_for_completion_timeout NFC: NULL out the dev->rfkill to prevent UAF HID: hid-led: fix maximum brightness for Dream Cheeky spi: img-spfi: Fix pm_runtime_get_sync() error checking ath9k_htc: fix potential out of bounds access with invalid rxstatus->rs_keyix inotify: show inotify mask flags in proc fdinfo x86/pm: Fix false positive kmemleak report in msr_build_context() drm/msm/dsi: fix error checks and return values for DSI xmit functions drm/msm/hdmi: check return value after calling platform_get_resource_byname() drm/rockchip: vop: fix possible null-ptr-deref in vop_bind() x86/mm: Cleanup the control_va_addr_alignment() __setup handler drm/msm: return an error pointer in msm_gem_prime_get_sg_table() media: uvcvideo: Fix missing check to determine if element is found in list ASoC: mxs-saif: Fix refcount leak in mxs_saif_probe regulator: pfuze100: Fix refcount leak in pfuze_parse_regulators_dt media: exynos4-is: Change clk_disable to clk_disable_unprepare media: pvrusb2: fix array-index-out-of-bounds in pvr2_i2c_core_init Bluetooth: fix dangling sco_conn and use-after-free in sco_sock_timeout m68k: math-emu: Fix dependencies of math emulation support sctp: read sk->sk_bound_dev_if once in sctp_rcv() ASoC: wm2000: fix missing clk_disable_unprepare() on error in wm2000_anc_transition() rxrpc: Fix listen() setting the bar too high for the prealloc rings rxrpc: Don't try to resend the request if we're receiving the reply soc: qcom: smp2p: Fix missing of_node_put() in smp2p_parse_ipc soc: qcom: smsm: Fix missing of_node_put() in smsm_parse_ipc mfd: ipaq-micro: Fix error check return value of platform_get_irq() scsi: fcoe: Fix Wstringop-overflow warnings in fcoe_wwn_from_mac() drivers/base/node.c: fix compaction sysfs file leak powerpc/8xx: export 'cpm_setbrg' for modules powerpc/idle: Fix return value of __setup() handler powerpc/4xx/cpm: Fix return value of __setup() handler tty: fix deadlock caused by calling printk() under tty_port->lock Input: sparcspkr - fix refcount leak in bbc_beep_probe video: fbdev: clcdfb: Fix refcount leak in clcdfb_of_vram_setup iommu/amd: Increase timeout waiting for GA log enablement wifi: mac80211: fix use-after-free in chanctx code iwlwifi: mvm: fix assert 1F04 upon reconfig fs-writeback: writeback_sb_inodes:Recalculate 'wrote' according skipped pages ext4: fix use-after-free in ext4_rename_dir_prepare ext4: fix bug_on in ext4_writepages ext4: verify dir block before splitting it dlm: fix plock invalid read dlm: fix missing lkb refcount handling ocfs2: dlmfs: fix error handling of user_dlm_destroy_lock scsi: dc395x: Fix a missing check on list iterator scsi: ufs: qcom: Add a readl() to make sure ref_clk gets enabled drm/amdgpu/cs: make commands with 0 chunks illegal behaviour. drm/bridge: analogix_dp: Grab runtime PM reference for DP-AUX md: fix an incorrect NULL check in does_sb_need_changing md: fix an incorrect NULL check in md_reload_sb RDMA/hfi1: Fix potential integer multiplication overflow errors irqchip/armada-370-xp: Do not touch Performance Counter Overflow on A375, A38x, A39x irqchip: irq-xtensa-mx: fix initial IRQ affinity mac80211: upgrade passive scan to active scan on DFS channels after beacon rx um: chan_user: Fix winch_tramp() return value um: Fix out-of-bounds read in LDT setup iommu/msm: Fix an incorrect NULL check on list iterator nodemask.h: fix compilation error with GCC12 hugetlb: fix huge_pmd_unshare address update rtl818x: Prevent using not initialized queues ASoC: rt5514: Fix event generation for "DSP Voice Wake Up" control carl9170: tx: fix an incorrect use of list iterator gma500: fix an incorrect NULL check on list iterator docs/conf.py: Cope with removal of language=None in Sphinx 5.0.0 dt-bindings: gpio: altera: correct interrupt-cells RDMA/rxe: Generate a completion for unsupported/invalid opcode MIPS: IP27: Remove incorrect `cpu_has_fpu' override netfilter: nf_tables: disallow non-stateful expression in sets earlier pcmcia: db1xxx_ss: restrict to MIPS_DB1XXX boards staging: greybus: codecs: fix type confusion of list iterator variable usb: usbip: fix a refcount leak in stub_probe() usb: usbip: add missing device lock on tweak configuration cmd USB: storage: karma: fix rio_karma_init return pwm: lp3943: Fix duty calculation in case period was clamped rpmsg: qcom_smd: Fix irq_of_parse_and_map() return value rtc: mt6397: check return value after calling platform_get_resource() serial: meson: acquire port->lock in startup() serial: digicolor-usart: Don't allow CS5-6 serial: txx9: Don't allow CS5-6 serial: sh-sci: Don't allow CS5-6 serial: st-asc: Sanitize CSIZE and correct PARENB for CS7 firmware: dmi-sysfs: Fix memory leak in dmi_sysfs_register_handle clocksource/drivers/oxnas-rps: Fix irq_of_parse_and_map() return value net: ethernet: mtk_eth_soc: out of bounds read in mtk_hwlro_get_fdir_entry() modpost: fix removing numeric suffixes jffs2: fix memory leak in jffs2_do_fill_super tcp: tcp_rtx_synack() can be called from process context tracing: Avoid adding tracer option before update_tracer_options i2c: cadence: Increase timeout per message if necessary m68knommu: set ZERO_PAGE() to the allocated zeroed page m68knommu: fix undefined reference to `_init_sp' video: fbdev: pxa3xx-gcu: release the resources correctly in pxa3xx_gcu_probe/remove() net: fix nla_strcmp to handle more then one trailing null character ata: pata_octeon_cf: Fix refcount leak in octeon_cf_probe net/mlx4_en: Fix wrong return value on ioctl EEPROM query failure SUNRPC: Fix the calculation of xdr->end in xdr_get_next_encode_buffer() net: xfrm: unexport __init-annotated xfrm4_protocol_init() net: altera: Fix refcount leak in altera_tse_mdio_create iio: dummy: iio_simple_dummy: check the return value of kstrdup() lkdtm/usercopy: Expand size of "out of frame" object tty: synclink_gt: Fix null-pointer-dereference in slgt_clean() tty: Fix a possible resource leak in icom_probe drivers: staging: rtl8192e: Fix deadlock in rtllib_beacons_stop() USB: host: isp116x: check return value after calling platform_get_resource() drivers: tty: serial: Fix deadlock in sa1100_set_termios() drivers: usb: host: Fix deadlock in oxu_bus_suspend() USB: hcd-pci: Fully suspend across freeze/thaw cycle usb: dwc2: gadget: don't reset gadget's driver->bus misc: rtsx: set NULL intfdata when probe fails clocksource/drivers/sp804: Avoid error on multiple instances staging: rtl8712: fix uninit-value in r871xu_drv_init() serial: msm_serial: disable interrupts in __msm_console_write() md: protect md_unregister_thread from reentrancy Revert "net: af_key: add check for pfkey_broadcast in function pfkey_process" drm/radeon: fix a possible null pointer dereference modpost: fix undefined behavior of is_arm_mapping_symbol() nodemask: Fix return values to be unsigned vringh: Fix loop descriptors check in the indirect cases ALSA: hda/conexant - Fix loopback issue with CX20632 cifs: return errors during session setup during reconnects ata: libata-transport: fix {dma|pio|xfer}_mode sysfs files nfc: st21nfca: fix incorrect validating logic in EVT_TRANSACTION nfc: st21nfca: fix memory leaks in EVT_TRANSACTION handling Input: bcm5974 - set missing URB_NO_TRANSFER_DMA_MAP urb flag powerpc/32: Fix overread/overwrite of thread_struct via ptrace mtd: cfi_cmdset_0002: Move and rename chip_check/chip_ready/chip_good_for_write mtd: cfi_cmdset_0002: Use chip_ready() for write on S29GL064N PCI: qcom: Fix unbalanced PHY init on probe errors tcp: fix tcp_mtup_probe_success vs wrong snd_cwnd Linux 4.9.318 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: I2028f3dcd422886bf997ec7e1d2ae86515f1bfb9
698 lines
17 KiB
C
698 lines
17 KiB
C
/*
|
|
* Copyright (C) 2004, OGAWA Hirofumi
|
|
* Released under GPL v2.
|
|
*/
|
|
|
|
#include <linux/blkdev.h>
|
|
#include "fat.h"
|
|
|
|
struct fatent_operations {
|
|
void (*ent_blocknr)(struct super_block *, int, int *, sector_t *);
|
|
void (*ent_set_ptr)(struct fat_entry *, int);
|
|
int (*ent_bread)(struct super_block *, struct fat_entry *,
|
|
int, sector_t);
|
|
int (*ent_get)(struct fat_entry *);
|
|
void (*ent_put)(struct fat_entry *, int);
|
|
int (*ent_next)(struct fat_entry *);
|
|
};
|
|
|
|
static DEFINE_SPINLOCK(fat12_entry_lock);
|
|
|
|
static void fat12_ent_blocknr(struct super_block *sb, int entry,
|
|
int *offset, sector_t *blocknr)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
int bytes = entry + (entry >> 1);
|
|
WARN_ON(!fat_valid_entry(sbi, entry));
|
|
*offset = bytes & (sb->s_blocksize - 1);
|
|
*blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
|
|
}
|
|
|
|
static void fat_ent_blocknr(struct super_block *sb, int entry,
|
|
int *offset, sector_t *blocknr)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
int bytes = (entry << sbi->fatent_shift);
|
|
WARN_ON(!fat_valid_entry(sbi, entry));
|
|
*offset = bytes & (sb->s_blocksize - 1);
|
|
*blocknr = sbi->fat_start + (bytes >> sb->s_blocksize_bits);
|
|
}
|
|
|
|
static void fat12_ent_set_ptr(struct fat_entry *fatent, int offset)
|
|
{
|
|
struct buffer_head **bhs = fatent->bhs;
|
|
if (fatent->nr_bhs == 1) {
|
|
WARN_ON(offset >= (bhs[0]->b_size - 1));
|
|
fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
|
|
fatent->u.ent12_p[1] = bhs[0]->b_data + (offset + 1);
|
|
} else {
|
|
WARN_ON(offset != (bhs[0]->b_size - 1));
|
|
fatent->u.ent12_p[0] = bhs[0]->b_data + offset;
|
|
fatent->u.ent12_p[1] = bhs[1]->b_data;
|
|
}
|
|
}
|
|
|
|
static void fat16_ent_set_ptr(struct fat_entry *fatent, int offset)
|
|
{
|
|
WARN_ON(offset & (2 - 1));
|
|
fatent->u.ent16_p = (__le16 *)(fatent->bhs[0]->b_data + offset);
|
|
}
|
|
|
|
static void fat32_ent_set_ptr(struct fat_entry *fatent, int offset)
|
|
{
|
|
WARN_ON(offset & (4 - 1));
|
|
fatent->u.ent32_p = (__le32 *)(fatent->bhs[0]->b_data + offset);
|
|
}
|
|
|
|
static int fat12_ent_bread(struct super_block *sb, struct fat_entry *fatent,
|
|
int offset, sector_t blocknr)
|
|
{
|
|
struct buffer_head **bhs = fatent->bhs;
|
|
|
|
WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
|
|
fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
|
|
|
|
bhs[0] = sb_bread(sb, blocknr);
|
|
if (!bhs[0])
|
|
goto err;
|
|
|
|
if ((offset + 1) < sb->s_blocksize)
|
|
fatent->nr_bhs = 1;
|
|
else {
|
|
/* This entry is block boundary, it needs the next block */
|
|
blocknr++;
|
|
bhs[1] = sb_bread(sb, blocknr);
|
|
if (!bhs[1])
|
|
goto err_brelse;
|
|
fatent->nr_bhs = 2;
|
|
}
|
|
fat12_ent_set_ptr(fatent, offset);
|
|
return 0;
|
|
|
|
err_brelse:
|
|
brelse(bhs[0]);
|
|
err:
|
|
fat_msg_ratelimit(sb, KERN_ERR, "FAT read failed (blocknr %llu)",
|
|
(llu)blocknr);
|
|
return -EIO;
|
|
}
|
|
|
|
static int fat_ent_bread(struct super_block *sb, struct fat_entry *fatent,
|
|
int offset, sector_t blocknr)
|
|
{
|
|
const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
|
|
|
|
WARN_ON(blocknr < MSDOS_SB(sb)->fat_start);
|
|
fatent->fat_inode = MSDOS_SB(sb)->fat_inode;
|
|
fatent->bhs[0] = sb_bread(sb, blocknr);
|
|
if (!fatent->bhs[0]) {
|
|
fat_msg_ratelimit(sb, KERN_ERR, "FAT read failed (blocknr %llu)",
|
|
(llu)blocknr);
|
|
return -EIO;
|
|
}
|
|
fatent->nr_bhs = 1;
|
|
ops->ent_set_ptr(fatent, offset);
|
|
return 0;
|
|
}
|
|
|
|
static int fat12_ent_get(struct fat_entry *fatent)
|
|
{
|
|
u8 **ent12_p = fatent->u.ent12_p;
|
|
int next;
|
|
|
|
spin_lock(&fat12_entry_lock);
|
|
if (fatent->entry & 1)
|
|
next = (*ent12_p[0] >> 4) | (*ent12_p[1] << 4);
|
|
else
|
|
next = (*ent12_p[1] << 8) | *ent12_p[0];
|
|
spin_unlock(&fat12_entry_lock);
|
|
|
|
next &= 0x0fff;
|
|
if (next >= BAD_FAT12)
|
|
next = FAT_ENT_EOF;
|
|
return next;
|
|
}
|
|
|
|
static int fat16_ent_get(struct fat_entry *fatent)
|
|
{
|
|
int next = le16_to_cpu(*fatent->u.ent16_p);
|
|
WARN_ON((unsigned long)fatent->u.ent16_p & (2 - 1));
|
|
if (next >= BAD_FAT16)
|
|
next = FAT_ENT_EOF;
|
|
return next;
|
|
}
|
|
|
|
static int fat32_ent_get(struct fat_entry *fatent)
|
|
{
|
|
int next = le32_to_cpu(*fatent->u.ent32_p) & 0x0fffffff;
|
|
WARN_ON((unsigned long)fatent->u.ent32_p & (4 - 1));
|
|
if (next >= BAD_FAT32)
|
|
next = FAT_ENT_EOF;
|
|
return next;
|
|
}
|
|
|
|
static void fat12_ent_put(struct fat_entry *fatent, int new)
|
|
{
|
|
u8 **ent12_p = fatent->u.ent12_p;
|
|
|
|
if (new == FAT_ENT_EOF)
|
|
new = EOF_FAT12;
|
|
|
|
spin_lock(&fat12_entry_lock);
|
|
if (fatent->entry & 1) {
|
|
*ent12_p[0] = (new << 4) | (*ent12_p[0] & 0x0f);
|
|
*ent12_p[1] = new >> 4;
|
|
} else {
|
|
*ent12_p[0] = new & 0xff;
|
|
*ent12_p[1] = (*ent12_p[1] & 0xf0) | (new >> 8);
|
|
}
|
|
spin_unlock(&fat12_entry_lock);
|
|
|
|
mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
|
|
if (fatent->nr_bhs == 2)
|
|
mark_buffer_dirty_inode(fatent->bhs[1], fatent->fat_inode);
|
|
}
|
|
|
|
static void fat16_ent_put(struct fat_entry *fatent, int new)
|
|
{
|
|
if (new == FAT_ENT_EOF)
|
|
new = EOF_FAT16;
|
|
|
|
*fatent->u.ent16_p = cpu_to_le16(new);
|
|
mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
|
|
}
|
|
|
|
static void fat32_ent_put(struct fat_entry *fatent, int new)
|
|
{
|
|
WARN_ON(new & 0xf0000000);
|
|
new |= le32_to_cpu(*fatent->u.ent32_p) & ~0x0fffffff;
|
|
*fatent->u.ent32_p = cpu_to_le32(new);
|
|
mark_buffer_dirty_inode(fatent->bhs[0], fatent->fat_inode);
|
|
}
|
|
|
|
static int fat12_ent_next(struct fat_entry *fatent)
|
|
{
|
|
u8 **ent12_p = fatent->u.ent12_p;
|
|
struct buffer_head **bhs = fatent->bhs;
|
|
u8 *nextp = ent12_p[1] + 1 + (fatent->entry & 1);
|
|
|
|
fatent->entry++;
|
|
if (fatent->nr_bhs == 1) {
|
|
WARN_ON(ent12_p[0] > (u8 *)(bhs[0]->b_data +
|
|
(bhs[0]->b_size - 2)));
|
|
WARN_ON(ent12_p[1] > (u8 *)(bhs[0]->b_data +
|
|
(bhs[0]->b_size - 1)));
|
|
if (nextp < (u8 *)(bhs[0]->b_data + (bhs[0]->b_size - 1))) {
|
|
ent12_p[0] = nextp - 1;
|
|
ent12_p[1] = nextp;
|
|
return 1;
|
|
}
|
|
} else {
|
|
WARN_ON(ent12_p[0] != (u8 *)(bhs[0]->b_data +
|
|
(bhs[0]->b_size - 1)));
|
|
WARN_ON(ent12_p[1] != (u8 *)bhs[1]->b_data);
|
|
ent12_p[0] = nextp - 1;
|
|
ent12_p[1] = nextp;
|
|
brelse(bhs[0]);
|
|
bhs[0] = bhs[1];
|
|
fatent->nr_bhs = 1;
|
|
return 1;
|
|
}
|
|
ent12_p[0] = NULL;
|
|
ent12_p[1] = NULL;
|
|
return 0;
|
|
}
|
|
|
|
static int fat16_ent_next(struct fat_entry *fatent)
|
|
{
|
|
const struct buffer_head *bh = fatent->bhs[0];
|
|
fatent->entry++;
|
|
if (fatent->u.ent16_p < (__le16 *)(bh->b_data + (bh->b_size - 2))) {
|
|
fatent->u.ent16_p++;
|
|
return 1;
|
|
}
|
|
fatent->u.ent16_p = NULL;
|
|
return 0;
|
|
}
|
|
|
|
static int fat32_ent_next(struct fat_entry *fatent)
|
|
{
|
|
const struct buffer_head *bh = fatent->bhs[0];
|
|
fatent->entry++;
|
|
if (fatent->u.ent32_p < (__le32 *)(bh->b_data + (bh->b_size - 4))) {
|
|
fatent->u.ent32_p++;
|
|
return 1;
|
|
}
|
|
fatent->u.ent32_p = NULL;
|
|
return 0;
|
|
}
|
|
|
|
static const struct fatent_operations fat12_ops = {
|
|
.ent_blocknr = fat12_ent_blocknr,
|
|
.ent_set_ptr = fat12_ent_set_ptr,
|
|
.ent_bread = fat12_ent_bread,
|
|
.ent_get = fat12_ent_get,
|
|
.ent_put = fat12_ent_put,
|
|
.ent_next = fat12_ent_next,
|
|
};
|
|
|
|
static const struct fatent_operations fat16_ops = {
|
|
.ent_blocknr = fat_ent_blocknr,
|
|
.ent_set_ptr = fat16_ent_set_ptr,
|
|
.ent_bread = fat_ent_bread,
|
|
.ent_get = fat16_ent_get,
|
|
.ent_put = fat16_ent_put,
|
|
.ent_next = fat16_ent_next,
|
|
};
|
|
|
|
static const struct fatent_operations fat32_ops = {
|
|
.ent_blocknr = fat_ent_blocknr,
|
|
.ent_set_ptr = fat32_ent_set_ptr,
|
|
.ent_bread = fat_ent_bread,
|
|
.ent_get = fat32_ent_get,
|
|
.ent_put = fat32_ent_put,
|
|
.ent_next = fat32_ent_next,
|
|
};
|
|
|
|
static inline void lock_fat(struct msdos_sb_info *sbi)
|
|
{
|
|
mutex_lock(&sbi->fat_lock);
|
|
}
|
|
|
|
static inline void unlock_fat(struct msdos_sb_info *sbi)
|
|
{
|
|
mutex_unlock(&sbi->fat_lock);
|
|
}
|
|
|
|
void fat_ent_access_init(struct super_block *sb)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
|
|
mutex_init(&sbi->fat_lock);
|
|
|
|
switch (sbi->fat_bits) {
|
|
case 32:
|
|
sbi->fatent_shift = 2;
|
|
sbi->fatent_ops = &fat32_ops;
|
|
break;
|
|
case 16:
|
|
sbi->fatent_shift = 1;
|
|
sbi->fatent_ops = &fat16_ops;
|
|
break;
|
|
case 12:
|
|
sbi->fatent_shift = -1;
|
|
sbi->fatent_ops = &fat12_ops;
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void mark_fsinfo_dirty(struct super_block *sb)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
|
|
if (sb->s_flags & MS_RDONLY || sbi->fat_bits != 32)
|
|
return;
|
|
|
|
__mark_inode_dirty(sbi->fsinfo_inode, I_DIRTY_SYNC);
|
|
}
|
|
|
|
static inline int fat_ent_update_ptr(struct super_block *sb,
|
|
struct fat_entry *fatent,
|
|
int offset, sector_t blocknr)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
const struct fatent_operations *ops = sbi->fatent_ops;
|
|
struct buffer_head **bhs = fatent->bhs;
|
|
|
|
/* Is this fatent's blocks including this entry? */
|
|
if (!fatent->nr_bhs || bhs[0]->b_blocknr != blocknr)
|
|
return 0;
|
|
if (sbi->fat_bits == 12) {
|
|
if ((offset + 1) < sb->s_blocksize) {
|
|
/* This entry is on bhs[0]. */
|
|
if (fatent->nr_bhs == 2) {
|
|
brelse(bhs[1]);
|
|
fatent->nr_bhs = 1;
|
|
}
|
|
} else {
|
|
/* This entry needs the next block. */
|
|
if (fatent->nr_bhs != 2)
|
|
return 0;
|
|
if (bhs[1]->b_blocknr != (blocknr + 1))
|
|
return 0;
|
|
}
|
|
}
|
|
ops->ent_set_ptr(fatent, offset);
|
|
return 1;
|
|
}
|
|
|
|
int fat_ent_read(struct inode *inode, struct fat_entry *fatent, int entry)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb);
|
|
const struct fatent_operations *ops = sbi->fatent_ops;
|
|
int err, offset;
|
|
sector_t blocknr;
|
|
|
|
if (!fat_valid_entry(sbi, entry)) {
|
|
fatent_brelse(fatent);
|
|
fat_fs_error(sb, "invalid access to FAT (entry 0x%08x)", entry);
|
|
return -EIO;
|
|
}
|
|
|
|
fatent_set_entry(fatent, entry);
|
|
ops->ent_blocknr(sb, entry, &offset, &blocknr);
|
|
|
|
if (!fat_ent_update_ptr(sb, fatent, offset, blocknr)) {
|
|
fatent_brelse(fatent);
|
|
err = ops->ent_bread(sb, fatent, offset, blocknr);
|
|
if (err)
|
|
return err;
|
|
}
|
|
return ops->ent_get(fatent);
|
|
}
|
|
|
|
/* FIXME: We can write the blocks as more big chunk. */
|
|
static int fat_mirror_bhs(struct super_block *sb, struct buffer_head **bhs,
|
|
int nr_bhs)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
struct buffer_head *c_bh;
|
|
int err, n, copy;
|
|
|
|
err = 0;
|
|
for (copy = 1; copy < sbi->fats; copy++) {
|
|
sector_t backup_fat = sbi->fat_length * copy;
|
|
|
|
for (n = 0; n < nr_bhs; n++) {
|
|
c_bh = sb_getblk(sb, backup_fat + bhs[n]->b_blocknr);
|
|
if (!c_bh) {
|
|
err = -ENOMEM;
|
|
goto error;
|
|
}
|
|
/* Avoid race with userspace read via bdev */
|
|
lock_buffer(c_bh);
|
|
memcpy(c_bh->b_data, bhs[n]->b_data, sb->s_blocksize);
|
|
set_buffer_uptodate(c_bh);
|
|
unlock_buffer(c_bh);
|
|
mark_buffer_dirty_inode(c_bh, sbi->fat_inode);
|
|
if (sb->s_flags & MS_SYNCHRONOUS)
|
|
err = sync_dirty_buffer(c_bh);
|
|
brelse(c_bh);
|
|
if (err)
|
|
goto error;
|
|
}
|
|
}
|
|
error:
|
|
return err;
|
|
}
|
|
|
|
int fat_ent_write(struct inode *inode, struct fat_entry *fatent,
|
|
int new, int wait)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
|
|
int err;
|
|
|
|
ops->ent_put(fatent, new);
|
|
if (wait) {
|
|
err = fat_sync_bhs(fatent->bhs, fatent->nr_bhs);
|
|
if (err)
|
|
return err;
|
|
}
|
|
return fat_mirror_bhs(sb, fatent->bhs, fatent->nr_bhs);
|
|
}
|
|
|
|
static inline int fat_ent_next(struct msdos_sb_info *sbi,
|
|
struct fat_entry *fatent)
|
|
{
|
|
if (sbi->fatent_ops->ent_next(fatent)) {
|
|
if (fatent->entry < sbi->max_cluster)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static inline int fat_ent_read_block(struct super_block *sb,
|
|
struct fat_entry *fatent)
|
|
{
|
|
const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
|
|
sector_t blocknr;
|
|
int offset;
|
|
|
|
fatent_brelse(fatent);
|
|
ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
|
|
return ops->ent_bread(sb, fatent, offset, blocknr);
|
|
}
|
|
|
|
static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
|
|
struct fat_entry *fatent)
|
|
{
|
|
int n, i;
|
|
|
|
for (n = 0; n < fatent->nr_bhs; n++) {
|
|
for (i = 0; i < *nr_bhs; i++) {
|
|
if (fatent->bhs[n] == bhs[i])
|
|
break;
|
|
}
|
|
if (i == *nr_bhs) {
|
|
get_bh(fatent->bhs[n]);
|
|
bhs[i] = fatent->bhs[n];
|
|
(*nr_bhs)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
const struct fatent_operations *ops = sbi->fatent_ops;
|
|
struct fat_entry fatent, prev_ent;
|
|
struct buffer_head *bhs[MAX_BUF_PER_PAGE];
|
|
int i, count, err, nr_bhs, idx_clus;
|
|
|
|
BUG_ON(nr_cluster > (MAX_BUF_PER_PAGE / 2)); /* fixed limit */
|
|
|
|
lock_fat(sbi);
|
|
if (sbi->free_clusters != -1 && sbi->free_clus_valid &&
|
|
sbi->free_clusters < nr_cluster) {
|
|
unlock_fat(sbi);
|
|
return -ENOSPC;
|
|
}
|
|
|
|
err = nr_bhs = idx_clus = 0;
|
|
count = FAT_START_ENT;
|
|
fatent_init(&prev_ent);
|
|
fatent_init(&fatent);
|
|
fatent_set_entry(&fatent, sbi->prev_free + 1);
|
|
while (count < sbi->max_cluster) {
|
|
if (fatent.entry >= sbi->max_cluster)
|
|
fatent.entry = FAT_START_ENT;
|
|
fatent_set_entry(&fatent, fatent.entry);
|
|
err = fat_ent_read_block(sb, &fatent);
|
|
if (err)
|
|
goto out;
|
|
|
|
/* Find the free entries in a block */
|
|
do {
|
|
if (ops->ent_get(&fatent) == FAT_ENT_FREE) {
|
|
int entry = fatent.entry;
|
|
|
|
/* make the cluster chain */
|
|
ops->ent_put(&fatent, FAT_ENT_EOF);
|
|
if (prev_ent.nr_bhs)
|
|
ops->ent_put(&prev_ent, entry);
|
|
|
|
fat_collect_bhs(bhs, &nr_bhs, &fatent);
|
|
|
|
sbi->prev_free = entry;
|
|
if (sbi->free_clusters != -1)
|
|
sbi->free_clusters--;
|
|
|
|
cluster[idx_clus] = entry;
|
|
idx_clus++;
|
|
if (idx_clus == nr_cluster)
|
|
goto out;
|
|
|
|
/*
|
|
* fat_collect_bhs() gets ref-count of bhs,
|
|
* so we can still use the prev_ent.
|
|
*/
|
|
prev_ent = fatent;
|
|
}
|
|
count++;
|
|
if (count == sbi->max_cluster)
|
|
break;
|
|
} while (fat_ent_next(sbi, &fatent));
|
|
}
|
|
|
|
/* Couldn't allocate the free entries */
|
|
sbi->free_clusters = 0;
|
|
sbi->free_clus_valid = 1;
|
|
err = -ENOSPC;
|
|
|
|
out:
|
|
unlock_fat(sbi);
|
|
mark_fsinfo_dirty(sb);
|
|
fatent_brelse(&fatent);
|
|
if (!err) {
|
|
if (inode_needs_sync(inode))
|
|
err = fat_sync_bhs(bhs, nr_bhs);
|
|
if (!err)
|
|
err = fat_mirror_bhs(sb, bhs, nr_bhs);
|
|
}
|
|
for (i = 0; i < nr_bhs; i++)
|
|
brelse(bhs[i]);
|
|
|
|
if (err && idx_clus)
|
|
fat_free_clusters(inode, cluster[0]);
|
|
|
|
return err;
|
|
}
|
|
|
|
int fat_free_clusters(struct inode *inode, int cluster)
|
|
{
|
|
struct super_block *sb = inode->i_sb;
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
const struct fatent_operations *ops = sbi->fatent_ops;
|
|
struct fat_entry fatent;
|
|
struct buffer_head *bhs[MAX_BUF_PER_PAGE];
|
|
int i, err, nr_bhs;
|
|
int first_cl = cluster, dirty_fsinfo = 0;
|
|
|
|
nr_bhs = 0;
|
|
fatent_init(&fatent);
|
|
lock_fat(sbi);
|
|
do {
|
|
cluster = fat_ent_read(inode, &fatent, cluster);
|
|
if (cluster < 0) {
|
|
err = cluster;
|
|
goto error;
|
|
} else if (cluster == FAT_ENT_FREE) {
|
|
fat_fs_error(sb, "%s: deleting FAT entry beyond EOF",
|
|
__func__);
|
|
err = -EIO;
|
|
goto error;
|
|
}
|
|
|
|
if (sbi->options.discard) {
|
|
/*
|
|
* Issue discard for the sectors we no longer
|
|
* care about, batching contiguous clusters
|
|
* into one request
|
|
*/
|
|
if (cluster != fatent.entry + 1) {
|
|
int nr_clus = fatent.entry - first_cl + 1;
|
|
|
|
sb_issue_discard(sb,
|
|
fat_clus_to_blknr(sbi, first_cl),
|
|
nr_clus * sbi->sec_per_clus,
|
|
GFP_NOFS, 0);
|
|
|
|
first_cl = cluster;
|
|
}
|
|
}
|
|
|
|
ops->ent_put(&fatent, FAT_ENT_FREE);
|
|
if (sbi->free_clusters != -1) {
|
|
sbi->free_clusters++;
|
|
dirty_fsinfo = 1;
|
|
}
|
|
|
|
if (nr_bhs + fatent.nr_bhs > MAX_BUF_PER_PAGE) {
|
|
if (sb->s_flags & MS_SYNCHRONOUS) {
|
|
err = fat_sync_bhs(bhs, nr_bhs);
|
|
if (err)
|
|
goto error;
|
|
}
|
|
err = fat_mirror_bhs(sb, bhs, nr_bhs);
|
|
if (err)
|
|
goto error;
|
|
for (i = 0; i < nr_bhs; i++)
|
|
brelse(bhs[i]);
|
|
nr_bhs = 0;
|
|
}
|
|
fat_collect_bhs(bhs, &nr_bhs, &fatent);
|
|
} while (cluster != FAT_ENT_EOF);
|
|
|
|
if (sb->s_flags & MS_SYNCHRONOUS) {
|
|
err = fat_sync_bhs(bhs, nr_bhs);
|
|
if (err)
|
|
goto error;
|
|
}
|
|
err = fat_mirror_bhs(sb, bhs, nr_bhs);
|
|
error:
|
|
fatent_brelse(&fatent);
|
|
for (i = 0; i < nr_bhs; i++)
|
|
brelse(bhs[i]);
|
|
unlock_fat(sbi);
|
|
if (dirty_fsinfo)
|
|
mark_fsinfo_dirty(sb);
|
|
|
|
return err;
|
|
}
|
|
EXPORT_SYMBOL_GPL(fat_free_clusters);
|
|
|
|
/* 128kb is the whole sectors for FAT12 and FAT16 */
|
|
#define FAT_READA_SIZE (128 * 1024)
|
|
|
|
static void fat_ent_reada(struct super_block *sb, struct fat_entry *fatent,
|
|
unsigned long reada_blocks)
|
|
{
|
|
const struct fatent_operations *ops = MSDOS_SB(sb)->fatent_ops;
|
|
sector_t blocknr;
|
|
int i, offset;
|
|
|
|
ops->ent_blocknr(sb, fatent->entry, &offset, &blocknr);
|
|
|
|
for (i = 0; i < reada_blocks; i++)
|
|
sb_breadahead(sb, blocknr + i);
|
|
}
|
|
|
|
int fat_count_free_clusters(struct super_block *sb)
|
|
{
|
|
struct msdos_sb_info *sbi = MSDOS_SB(sb);
|
|
const struct fatent_operations *ops = sbi->fatent_ops;
|
|
struct fat_entry fatent;
|
|
unsigned long reada_blocks, reada_mask, cur_block;
|
|
int err = 0, free;
|
|
|
|
lock_fat(sbi);
|
|
if (sbi->free_clusters != -1 && sbi->free_clus_valid)
|
|
goto out;
|
|
|
|
reada_blocks = FAT_READA_SIZE >> sb->s_blocksize_bits;
|
|
reada_mask = reada_blocks - 1;
|
|
cur_block = 0;
|
|
|
|
free = 0;
|
|
fatent_init(&fatent);
|
|
fatent_set_entry(&fatent, FAT_START_ENT);
|
|
while (fatent.entry < sbi->max_cluster) {
|
|
/* readahead of fat blocks */
|
|
if ((cur_block & reada_mask) == 0) {
|
|
unsigned long rest = sbi->fat_length - cur_block;
|
|
fat_ent_reada(sb, &fatent, min(reada_blocks, rest));
|
|
}
|
|
cur_block++;
|
|
|
|
err = fat_ent_read_block(sb, &fatent);
|
|
if (err)
|
|
goto out;
|
|
|
|
do {
|
|
if (ops->ent_get(&fatent) == FAT_ENT_FREE)
|
|
free++;
|
|
} while (fat_ent_next(sbi, &fatent));
|
|
cond_resched();
|
|
}
|
|
sbi->free_clusters = free;
|
|
sbi->free_clus_valid = 1;
|
|
mark_fsinfo_dirty(sb);
|
|
fatent_brelse(&fatent);
|
|
out:
|
|
unlock_fat(sbi);
|
|
return err;
|
|
}
|