#include <linux/blkdev.h>
#include <linux/backing-dev.h>
#include <linux/vfs.h>
#include <linux/fs_struct.h>
#include <linux/sched/mm.h>
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
#include "sysctl.h"
#include "logfile.h"
#include "index.h"
#include "ntfs.h"
#include "ea.h"
#include "volume.h"
static __le16 *default_upcase;
static unsigned long ntfs_nr_upcase_users;
static struct workqueue_struct *ntfs_wq;
enum {
ON_ERRORS_PANIC = 0x01,
ON_ERRORS_REMOUNT_RO = 0x02,
ON_ERRORS_CONTINUE = 0x04,
};
static const struct constant_table ntfs_param_enums[] = {
{ "panic", ON_ERRORS_PANIC },
{ "remount-ro", ON_ERRORS_REMOUNT_RO },
{ "continue", ON_ERRORS_CONTINUE },
{}
};
enum {
NATIVE_SYMLINK_RAW,
NATIVE_SYMLINK_REL,
};
static const struct constant_table ntfs_native_symlink_enums[] = {
{ "raw", NATIVE_SYMLINK_RAW },
{ "rel", NATIVE_SYMLINK_REL },
{}
};
enum {
SYMLINK_WSL,
SYMLINK_NATIVE,
};
static const struct constant_table ntfs_symlink_enums[] = {
{ "wsl", SYMLINK_WSL },
{ "native", SYMLINK_NATIVE },
{}
};
enum {
Opt_uid,
Opt_gid,
Opt_umask,
Opt_dmask,
Opt_fmask,
Opt_errors,
Opt_nls,
Opt_charset,
Opt_show_sys_files,
Opt_show_meta,
Opt_case_sensitive,
Opt_disable_sparse,
Opt_sparse,
Opt_mft_zone_multiplier,
Opt_preallocated_size,
Opt_sys_immutable,
Opt_nohidden,
Opt_hide_dot_files,
Opt_check_windows_names,
Opt_acl,
Opt_discard,
Opt_nocase,
Opt_native_symlink,
Opt_symlink,
};
static const struct fs_parameter_spec ntfs_parameters[] = {
fsparam_u32("uid", Opt_uid),
fsparam_u32("gid", Opt_gid),
fsparam_u32oct("umask", Opt_umask),
fsparam_u32oct("dmask", Opt_dmask),
fsparam_u32oct("fmask", Opt_fmask),
fsparam_string("nls", Opt_nls),
fsparam_string("iocharset", Opt_charset),
fsparam_enum("errors", Opt_errors, ntfs_param_enums),
fsparam_flag("show_sys_files", Opt_show_sys_files),
fsparam_flag("showmeta", Opt_show_meta),
fsparam_flag("case_sensitive", Opt_case_sensitive),
fsparam_flag("disable_sparse", Opt_disable_sparse),
fsparam_s32("mft_zone_multiplier", Opt_mft_zone_multiplier),
fsparam_u64("preallocated_size", Opt_preallocated_size),
fsparam_flag("sys_immutable", Opt_sys_immutable),
fsparam_flag("nohidden", Opt_nohidden),
fsparam_flag("hide_dot_files", Opt_hide_dot_files),
fsparam_flag("windows_names", Opt_check_windows_names),
fsparam_flag("acl", Opt_acl),
fsparam_flag("discard", Opt_discard),
fsparam_flag("sparse", Opt_sparse),
fsparam_flag("nocase", Opt_nocase),
fsparam_enum("native_symlink", Opt_native_symlink, ntfs_native_symlink_enums),
fsparam_enum("symlink", Opt_symlink, ntfs_symlink_enums),
{}
};
static int ntfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
{
struct ntfs_volume *vol = fc->s_fs_info;
struct fs_parse_result result;
int opt;
opt = fs_parse(fc, ntfs_parameters, param, &result);
if (opt < 0)
return opt;
switch (opt) {
case Opt_uid:
vol->uid = make_kuid(current_user_ns(), result.uint_32);
break;
case Opt_gid:
vol->gid = make_kgid(current_user_ns(), result.uint_32);
break;
case Opt_umask:
vol->fmask = vol->dmask = result.uint_32;
break;
case Opt_dmask:
vol->dmask = result.uint_32;
break;
case Opt_fmask:
vol->fmask = result.uint_32;
break;
case Opt_errors:
vol->on_errors = result.uint_32;
break;
case Opt_nls:
case Opt_charset:
if (vol->nls_map)
unload_nls(vol->nls_map);
vol->nls_map = load_nls(param->string);
if (!vol->nls_map) {
ntfs_error(vol->sb, "Failed to load NLS table '%s'.",
param->string);
return -EINVAL;
}
break;
case Opt_mft_zone_multiplier:
if (vol->mft_zone_multiplier && vol->mft_zone_multiplier !=
result.int_32) {
ntfs_error(vol->sb, "Cannot change mft_zone_multiplier on remount.");
return -EINVAL;
}
if (result.int_32 < 1 || result.int_32 > 4) {
ntfs_error(vol->sb,
"Invalid mft_zone_multiplier. Using default value, i.e. 1.");
vol->mft_zone_multiplier = 1;
} else
vol->mft_zone_multiplier = result.int_32;
break;
case Opt_show_sys_files:
case Opt_show_meta:
if (result.boolean)
NVolSetShowSystemFiles(vol);
else
NVolClearShowSystemFiles(vol);
break;
case Opt_case_sensitive:
if (result.boolean)
NVolSetCaseSensitive(vol);
else
NVolClearCaseSensitive(vol);
break;
case Opt_nocase:
if (result.boolean)
NVolClearCaseSensitive(vol);
else
NVolSetCaseSensitive(vol);
break;
case Opt_preallocated_size:
vol->preallocated_size = (loff_t)result.uint_64;
break;
case Opt_sys_immutable:
if (result.boolean)
NVolSetSysImmutable(vol);
else
NVolClearSysImmutable(vol);
break;
case Opt_nohidden:
if (result.boolean)
NVolClearShowHiddenFiles(vol);
else
NVolSetShowHiddenFiles(vol);
break;
case Opt_hide_dot_files:
if (result.boolean)
NVolSetHideDotFiles(vol);
else
NVolClearHideDotFiles(vol);
break;
case Opt_check_windows_names:
if (result.boolean)
NVolSetCheckWindowsNames(vol);
else
NVolClearCheckWindowsNames(vol);
break;
case Opt_acl:
#ifdef CONFIG_NTFS_FS_POSIX_ACL
if (result.boolean)
fc->sb_flags |= SB_POSIXACL;
else
fc->sb_flags &= ~SB_POSIXACL;
break;
#else
return -EINVAL;
#endif
case Opt_discard:
if (result.boolean)
NVolSetDiscard(vol);
else
NVolClearDiscard(vol);
break;
case Opt_disable_sparse:
if (result.boolean)
NVolSetDisableSparse(vol);
else
NVolClearDisableSparse(vol);
break;
case Opt_native_symlink:
if (result.uint_32 == NATIVE_SYMLINK_REL)
NVolSetNativeSymlinkRel(vol);
else
NVolClearNativeSymlinkRel(vol);
break;
case Opt_symlink:
if (result.uint_32 == SYMLINK_NATIVE)
NVolSetSymlinkNative(vol);
else
NVolClearSymlinkNative(vol);
break;
case Opt_sparse:
break;
default:
return -EINVAL;
}
return 0;
}
static int ntfs_reconfigure(struct fs_context *fc)
{
struct super_block *sb = fc->root->d_sb;
struct ntfs_volume *vol = NTFS_SB(sb);
ntfs_debug("Entering with remount");
sync_filesystem(sb);
if (sb_rdonly(sb) && !(fc->sb_flags & SB_RDONLY)) {
static const char *es = ". Cannot remount read-write.";
if (NVolErrors(vol)) {
ntfs_error(sb, "Volume has errors and is read-only%s",
es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_IS_DIRTY) {
ntfs_error(sb, "Volume is dirty and read-only%s", es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
ntfs_error(sb, "Volume has been modified by chkdsk and is read-only%s", es);
return -EROFS;
}
if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
ntfs_error(sb, "Volume has unsupported flags set (0x%x) and is read-only%s",
le16_to_cpu(vol->vol_flags), es);
return -EROFS;
}
if (vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino)) {
ntfs_error(sb, "Failed to empty journal LogFile%s",
es);
NVolSetErrors(vol);
return -EROFS;
}
} else if (!sb_rdonly(sb) && (fc->sb_flags & SB_RDONLY)) {
if (!NVolErrors(vol)) {
if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
ntfs_warning(sb,
"Failed to clear dirty bit in volume information flags. Run chkdsk.");
}
}
ntfs_debug("Done.");
return 0;
}
const struct option_t on_errors_arr[] = {
{ ON_ERRORS_PANIC, "panic" },
{ ON_ERRORS_REMOUNT_RO, "remount-ro", },
{ ON_ERRORS_CONTINUE, "continue", },
{ 0, NULL }
};
void ntfs_handle_error(struct super_block *sb)
{
struct ntfs_volume *vol = NTFS_SB(sb);
if (sb_rdonly(sb))
return;
if (vol->on_errors == ON_ERRORS_REMOUNT_RO) {
sb->s_flags |= SB_RDONLY;
pr_crit("(device %s): Filesystem has been set read-only\n",
sb->s_id);
} else if (vol->on_errors == ON_ERRORS_PANIC) {
panic("ntfs: (device %s): panic from previous error\n",
sb->s_id);
} else if (vol->on_errors == ON_ERRORS_CONTINUE) {
if (errseq_check(&sb->s_wb_err, vol->wb_err) == -ENODEV) {
NVolSetShutdown(vol);
vol->wb_err = sb->s_wb_err;
}
}
}
static int ntfs_write_volume_flags(struct ntfs_volume *vol, const __le16 flags)
{
struct ntfs_inode *ni = NTFS_I(vol->vol_ino);
struct volume_information *vi;
struct ntfs_attr_search_ctx *ctx;
int err;
ntfs_debug("Entering, old flags = 0x%x, new flags = 0x%x.",
le16_to_cpu(vol->vol_flags), le16_to_cpu(flags));
mutex_lock(&ni->mrec_lock);
if (vol->vol_flags == flags)
goto done;
ctx = ntfs_attr_get_search_ctx(ni, NULL);
if (!ctx) {
err = -ENOMEM;
goto put_unm_err_out;
}
err = ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
ctx);
if (err)
goto put_unm_err_out;
vi = (struct volume_information *)((u8 *)ctx->attr +
le16_to_cpu(ctx->attr->data.resident.value_offset));
vol->vol_flags = vi->flags = flags;
mark_mft_record_dirty(ctx->ntfs_ino);
ntfs_attr_put_search_ctx(ctx);
done:
mutex_unlock(&ni->mrec_lock);
ntfs_debug("Done.");
return 0;
put_unm_err_out:
if (ctx)
ntfs_attr_put_search_ctx(ctx);
mutex_unlock(&ni->mrec_lock);
ntfs_error(vol->sb, "Failed with error code %i.", -err);
return err;
}
int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags)
{
flags &= VOLUME_FLAGS_MASK;
return ntfs_write_volume_flags(vol, vol->vol_flags | flags);
}
int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags)
{
flags &= VOLUME_FLAGS_MASK;
flags = vol->vol_flags & cpu_to_le16(~le16_to_cpu(flags));
return ntfs_write_volume_flags(vol, flags);
}
int ntfs_write_volume_label(struct ntfs_volume *vol, char *label)
{
struct ntfs_inode *vol_ni = NTFS_I(vol->vol_ino);
struct ntfs_attr_search_ctx *ctx;
char *new_label;
__le16 *uname;
int uname_len, ret;
uname_len = ntfs_nlstoucs(vol, label, strlen(label),
&uname, FSLABEL_MAX);
if (uname_len < 0) {
ntfs_error(vol->sb,
"Failed to convert volume label '%s' to Unicode.",
label);
return uname_len;
}
if (uname_len > NTFS_MAX_LABEL_LEN) {
ntfs_error(vol->sb,
"Volume label is too long (max %d characters).",
NTFS_MAX_LABEL_LEN);
kvfree(uname);
return -EINVAL;
}
new_label = kstrdup(label, GFP_KERNEL);
if (!new_label) {
kvfree(uname);
return -ENOMEM;
}
mutex_lock(&vol_ni->mrec_lock);
ctx = ntfs_attr_get_search_ctx(vol_ni, NULL);
if (!ctx) {
ret = -ENOMEM;
goto out;
}
ret = ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0,
ctx);
if (!ret)
ret = ntfs_attr_record_rm(ctx);
else if (ret == -ENOENT)
ret = 0;
ntfs_attr_put_search_ctx(ctx);
if (ret)
goto out;
ret = ntfs_resident_attr_record_add(vol_ni, AT_VOLUME_NAME, AT_UNNAMED, 0,
(u8 *)uname, uname_len * sizeof(__le16), 0);
out:
if (ret >= 0) {
char *old_label;
mutex_lock(&vol->volume_label_lock);
old_label = vol->volume_label;
vol->volume_label = new_label;
mutex_unlock(&vol->volume_label_lock);
kfree(old_label);
mark_inode_dirty_sync(vol->vol_ino);
ret = 0;
}
mutex_unlock(&vol_ni->mrec_lock);
kvfree(uname);
if (ret < 0)
kfree(new_label);
return ret;
}
static bool is_boot_sector_ntfs(const struct super_block *sb,
const struct ntfs_boot_sector *b, const bool silent)
{
if ((void *)b < (void *)&b->checksum && b->checksum && !silent) {
__le32 *u;
u32 i;
for (i = 0, u = (__le32 *)b; u < (__le32 *)(&b->checksum); ++u)
i += le32_to_cpup(u);
if (le32_to_cpu(b->checksum) != i)
ntfs_warning(sb, "Invalid boot sector checksum.");
}
if (b->oem_id != magicNTFS)
goto not_ntfs;
if (le16_to_cpu(b->bpb.bytes_per_sector) < 0x100 ||
le16_to_cpu(b->bpb.bytes_per_sector) > 0x1000)
goto not_ntfs;
if (b->bpb.sectors_per_cluster > 0x80 &&
b->bpb.sectors_per_cluster < 0xf4)
goto not_ntfs;
if (le16_to_cpu(b->bpb.reserved_sectors) ||
le16_to_cpu(b->bpb.root_entries) ||
le16_to_cpu(b->bpb.sectors) ||
le16_to_cpu(b->bpb.sectors_per_fat) ||
le32_to_cpu(b->bpb.large_sectors) || b->bpb.fats)
goto not_ntfs;
if ((u8)b->clusters_per_mft_record < 0xe1 ||
(u8)b->clusters_per_mft_record > 0xf7)
switch (b->clusters_per_mft_record) {
case 1: case 2: case 4: case 8: case 16: case 32: case 64:
break;
default:
goto not_ntfs;
}
if ((u8)b->clusters_per_index_record < 0xe1 ||
(u8)b->clusters_per_index_record > 0xf7)
switch (b->clusters_per_index_record) {
case 1: case 2: case 4: case 8: case 16: case 32: case 64:
break;
default:
goto not_ntfs;
}
if (!silent && b->end_of_sector_marker != cpu_to_le16(0xaa55))
ntfs_warning(sb, "Invalid end of sector marker.");
return true;
not_ntfs:
return false;
}
static char *read_ntfs_boot_sector(struct super_block *sb,
const int silent)
{
char *boot_sector;
boot_sector = kzalloc(PAGE_SIZE, GFP_NOFS);
if (!boot_sector)
return NULL;
if (ntfs_bdev_read(sb->s_bdev, boot_sector, 0, PAGE_SIZE)) {
if (!silent)
ntfs_error(sb, "Unable to read primary boot sector.");
kfree(boot_sector);
return NULL;
}
if (!is_boot_sector_ntfs(sb, (struct ntfs_boot_sector *)boot_sector,
silent)) {
if (!silent)
ntfs_error(sb, "Primary boot sector is invalid.");
kfree(boot_sector);
return NULL;
}
return boot_sector;
}
static bool parse_ntfs_boot_sector(struct ntfs_volume *vol,
const struct ntfs_boot_sector *b)
{
unsigned int sectors_per_cluster, sectors_per_cluster_bits, nr_hidden_sects;
int clusters_per_mft_record, clusters_per_index_record;
s64 ll;
vol->sector_size = le16_to_cpu(b->bpb.bytes_per_sector);
vol->sector_size_bits = ffs(vol->sector_size) - 1;
ntfs_debug("vol->sector_size = %i (0x%x)", vol->sector_size,
vol->sector_size);
ntfs_debug("vol->sector_size_bits = %i (0x%x)", vol->sector_size_bits,
vol->sector_size_bits);
if (vol->sector_size < vol->sb->s_blocksize) {
ntfs_error(vol->sb,
"Sector size (%i) is smaller than the device block size (%lu). This is not supported.",
vol->sector_size, vol->sb->s_blocksize);
return false;
}
if (b->bpb.sectors_per_cluster >= 0xf4)
sectors_per_cluster = 1U << -(s8)b->bpb.sectors_per_cluster;
else
sectors_per_cluster = b->bpb.sectors_per_cluster;
ntfs_debug("sectors_per_cluster = 0x%x", b->bpb.sectors_per_cluster);
sectors_per_cluster_bits = ffs(sectors_per_cluster) - 1;
ntfs_debug("sectors_per_cluster_bits = 0x%x",
sectors_per_cluster_bits);
nr_hidden_sects = le32_to_cpu(b->bpb.hidden_sectors);
ntfs_debug("number of hidden sectors = 0x%x", nr_hidden_sects);
vol->cluster_size = vol->sector_size << sectors_per_cluster_bits;
vol->cluster_size_mask = vol->cluster_size - 1;
vol->cluster_size_bits = ffs(vol->cluster_size) - 1;
ntfs_debug("vol->cluster_size = %i (0x%x)", vol->cluster_size,
vol->cluster_size);
ntfs_debug("vol->cluster_size_mask = 0x%x", vol->cluster_size_mask);
ntfs_debug("vol->cluster_size_bits = %i", vol->cluster_size_bits);
if (vol->cluster_size < vol->sector_size) {
ntfs_error(vol->sb,
"Cluster size (%i) is smaller than the sector size (%i). This is not supported.",
vol->cluster_size, vol->sector_size);
return false;
}
clusters_per_mft_record = b->clusters_per_mft_record;
ntfs_debug("clusters_per_mft_record = %i (0x%x)",
clusters_per_mft_record, clusters_per_mft_record);
if (clusters_per_mft_record > 0)
vol->mft_record_size = vol->cluster_size <<
(ffs(clusters_per_mft_record) - 1);
else
vol->mft_record_size = 1 << -clusters_per_mft_record;
vol->mft_record_size_mask = vol->mft_record_size - 1;
vol->mft_record_size_bits = ffs(vol->mft_record_size) - 1;
ntfs_debug("vol->mft_record_size = %i (0x%x)", vol->mft_record_size,
vol->mft_record_size);
ntfs_debug("vol->mft_record_size_mask = 0x%x",
vol->mft_record_size_mask);
ntfs_debug("vol->mft_record_size_bits = %i (0x%x)",
vol->mft_record_size_bits, vol->mft_record_size_bits);
if (vol->mft_record_size > PAGE_SIZE) {
ntfs_error(vol->sb,
"Mft record size (%i) exceeds the PAGE_SIZE on your system (%lu). This is not supported.",
vol->mft_record_size, PAGE_SIZE);
return false;
}
if (vol->mft_record_size < vol->sector_size) {
ntfs_warning(vol->sb, "Mft record size (%i) is smaller than the sector size (%i).",
vol->mft_record_size, vol->sector_size);
}
clusters_per_index_record = b->clusters_per_index_record;
ntfs_debug("clusters_per_index_record = %i (0x%x)",
clusters_per_index_record, clusters_per_index_record);
if (clusters_per_index_record > 0)
vol->index_record_size = vol->cluster_size <<
(ffs(clusters_per_index_record) - 1);
else
vol->index_record_size = 1 << -clusters_per_index_record;
vol->index_record_size_mask = vol->index_record_size - 1;
vol->index_record_size_bits = ffs(vol->index_record_size) - 1;
ntfs_debug("vol->index_record_size = %i (0x%x)",
vol->index_record_size, vol->index_record_size);
ntfs_debug("vol->index_record_size_mask = 0x%x",
vol->index_record_size_mask);
ntfs_debug("vol->index_record_size_bits = %i (0x%x)",
vol->index_record_size_bits,
vol->index_record_size_bits);
if (vol->index_record_size < vol->sector_size) {
ntfs_error(vol->sb,
"Index record size (%i) is smaller than the sector size (%i). This is not supported.",
vol->index_record_size, vol->sector_size);
return false;
}
ll = le64_to_cpu(b->number_of_sectors) >> sectors_per_cluster_bits;
if ((u64)ll >= 1ULL << 32) {
ntfs_error(vol->sb, "Cannot handle 64-bit clusters.");
return false;
}
vol->nr_clusters = ll;
ntfs_debug("vol->nr_clusters = 0x%llx", vol->nr_clusters);
ll = le64_to_cpu(b->mft_lcn);
if (ll >= vol->nr_clusters) {
ntfs_error(vol->sb, "MFT LCN (%lli, 0x%llx) is beyond end of volume. Weird.",
ll, ll);
return false;
}
vol->mft_lcn = ll;
ntfs_debug("vol->mft_lcn = 0x%llx", vol->mft_lcn);
ll = le64_to_cpu(b->mftmirr_lcn);
if (ll >= vol->nr_clusters) {
ntfs_error(vol->sb, "MFTMirr LCN (%lli, 0x%llx) is beyond end of volume. Weird.",
ll, ll);
return false;
}
vol->mftmirr_lcn = ll;
ntfs_debug("vol->mftmirr_lcn = 0x%llx", vol->mftmirr_lcn);
if (vol->cluster_size <= (4 << vol->mft_record_size_bits))
vol->mftmirr_size = 4;
else
vol->mftmirr_size = vol->cluster_size >>
vol->mft_record_size_bits;
ntfs_debug("vol->mftmirr_size = %i", vol->mftmirr_size);
vol->serial_no = le64_to_cpu(b->volume_serial_number);
ntfs_debug("vol->serial_no = 0x%llx", vol->serial_no);
vol->sparse_compression_unit = 4;
if (vol->cluster_size > 4096) {
switch (vol->cluster_size) {
case 65536:
vol->sparse_compression_unit = 0;
break;
case 32768:
vol->sparse_compression_unit = 1;
break;
case 16384:
vol->sparse_compression_unit = 2;
break;
case 8192:
vol->sparse_compression_unit = 3;
break;
}
}
return true;
}
static void ntfs_setup_allocators(struct ntfs_volume *vol)
{
s64 mft_zone_size, mft_lcn;
ntfs_debug("vol->mft_zone_multiplier = 0x%x",
vol->mft_zone_multiplier);
mft_zone_size = vol->nr_clusters;
switch (vol->mft_zone_multiplier) {
case 4:
mft_zone_size >>= 1;
break;
case 3:
mft_zone_size = (mft_zone_size +
(mft_zone_size >> 1)) >> 2;
break;
case 2:
mft_zone_size >>= 2;
break;
default:
mft_zone_size >>= 3;
break;
}
vol->mft_zone_start = vol->mft_zone_pos = vol->mft_lcn;
ntfs_debug("vol->mft_zone_pos = 0x%llx", vol->mft_zone_pos);
mft_lcn = NTFS_B_TO_CLU(vol, 8192 + 2 * vol->cluster_size - 1);
if (mft_lcn * vol->cluster_size < 16 * 1024)
mft_lcn = (16 * 1024 + vol->cluster_size - 1) >>
vol->cluster_size_bits;
if (vol->mft_zone_start <= mft_lcn)
vol->mft_zone_start = 0;
ntfs_debug("vol->mft_zone_start = 0x%llx", vol->mft_zone_start);
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
while (vol->mft_zone_end >= vol->nr_clusters) {
mft_zone_size >>= 1;
vol->mft_zone_end = vol->mft_lcn + mft_zone_size;
}
ntfs_debug("vol->mft_zone_end = 0x%llx", vol->mft_zone_end);
vol->data1_zone_pos = vol->mft_zone_end;
ntfs_debug("vol->data1_zone_pos = 0x%llx", vol->data1_zone_pos);
vol->data2_zone_pos = 0;
ntfs_debug("vol->data2_zone_pos = 0x%llx", vol->data2_zone_pos);
vol->mft_data_pos = 24;
ntfs_debug("vol->mft_data_pos = 0x%llx", vol->mft_data_pos);
}
static struct lock_class_key mftmirr_runlist_lock_key,
mftmirr_mrec_lock_key;
static bool load_and_init_mft_mirror(struct ntfs_volume *vol)
{
struct inode *tmp_ino;
struct ntfs_inode *tmp_ni;
ntfs_debug("Entering.");
tmp_ino = ntfs_iget(vol->sb, FILE_MFTMirr);
if (IS_ERR(tmp_ino)) {
if (!IS_ERR(tmp_ino))
iput(tmp_ino);
return false;
}
lockdep_set_class(&NTFS_I(tmp_ino)->runlist.lock,
&mftmirr_runlist_lock_key);
lockdep_set_class(&NTFS_I(tmp_ino)->mrec_lock,
&mftmirr_mrec_lock_key);
tmp_ino->i_uid = GLOBAL_ROOT_UID;
tmp_ino->i_gid = GLOBAL_ROOT_GID;
tmp_ino->i_mode = S_IFREG;
tmp_ino->i_op = &ntfs_empty_inode_ops;
tmp_ino->i_fop = &ntfs_empty_file_ops;
tmp_ino->i_mapping->a_ops = &ntfs_aops;
tmp_ni = NTFS_I(tmp_ino);
NInoSetMstProtected(tmp_ni);
NInoSetSparseDisabled(tmp_ni);
tmp_ni->itype.index.block_size = vol->mft_record_size;
tmp_ni->itype.index.block_size_bits = vol->mft_record_size_bits;
vol->mftmirr_ino = tmp_ino;
ntfs_debug("Done.");
return true;
}
static bool check_mft_mirror(struct ntfs_volume *vol)
{
struct super_block *sb = vol->sb;
struct ntfs_inode *mirr_ni;
struct folio *mft_folio = NULL, *mirr_folio = NULL;
u8 *kmft = NULL, *kmirr = NULL;
struct runlist_element *rl, rl2[2];
pgoff_t index;
int mrecs_per_page, i;
ntfs_debug("Entering.");
mrecs_per_page = PAGE_SIZE / vol->mft_record_size;
index = i = 0;
do {
u32 bytes;
if (!(i % mrecs_per_page)) {
if (index) {
kunmap_local(kmirr);
folio_put(mirr_folio);
kunmap_local(kmft);
folio_put(mft_folio);
}
mft_folio = read_mapping_folio(vol->mft_ino->i_mapping,
index, NULL);
if (IS_ERR(mft_folio)) {
ntfs_error(sb, "Failed to read $MFT.");
return false;
}
kmft = kmap_local_folio(mft_folio, 0);
mirr_folio = read_mapping_folio(vol->mftmirr_ino->i_mapping,
index, NULL);
if (IS_ERR(mirr_folio)) {
ntfs_error(sb, "Failed to read $MFTMirr.");
goto mft_unmap_out;
}
kmirr = kmap_local_folio(mirr_folio, 0);
++index;
}
if (((struct mft_record *)kmft)->flags & MFT_RECORD_IN_USE) {
if (ntfs_is_baad_recordp((__le32 *)kmft)) {
ntfs_error(sb,
"Incomplete multi sector transfer detected in mft record %i.",
i);
mm_unmap_out:
kunmap_local(kmirr);
folio_put(mirr_folio);
mft_unmap_out:
kunmap_local(kmft);
folio_put(mft_folio);
return false;
}
}
if (((struct mft_record *)kmirr)->flags & MFT_RECORD_IN_USE) {
if (ntfs_is_baad_recordp((__le32 *)kmirr)) {
ntfs_error(sb,
"Incomplete multi sector transfer detected in mft mirror record %i.",
i);
goto mm_unmap_out;
}
}
bytes = le32_to_cpu(((struct mft_record *)kmft)->bytes_in_use);
if (bytes < sizeof(struct mft_record_old) ||
bytes > vol->mft_record_size ||
ntfs_is_baad_recordp((__le32 *)kmft)) {
bytes = le32_to_cpu(((struct mft_record *)kmirr)->bytes_in_use);
if (bytes < sizeof(struct mft_record_old) ||
bytes > vol->mft_record_size ||
ntfs_is_baad_recordp((__le32 *)kmirr))
bytes = vol->mft_record_size;
}
if (memcmp(kmft, kmirr, bytes)) {
ntfs_error(sb,
"$MFT and $MFTMirr record %i do not match. Run chkdsk.",
i);
goto mm_unmap_out;
}
kmft += vol->mft_record_size;
kmirr += vol->mft_record_size;
} while (++i < vol->mftmirr_size);
kunmap_local(kmirr);
folio_put(mirr_folio);
kunmap_local(kmft);
folio_put(mft_folio);
rl2[0].vcn = 0;
rl2[0].lcn = vol->mftmirr_lcn;
rl2[0].length = NTFS_B_TO_CLU(vol, vol->mftmirr_size * vol->mft_record_size +
vol->cluster_size - 1);
rl2[1].vcn = rl2[0].length;
rl2[1].lcn = LCN_ENOENT;
rl2[1].length = 0;
mirr_ni = NTFS_I(vol->mftmirr_ino);
down_read(&mirr_ni->runlist.lock);
rl = mirr_ni->runlist.rl;
i = 0;
do {
if (rl2[i].vcn != rl[i].vcn || rl2[i].lcn != rl[i].lcn ||
rl2[i].length != rl[i].length) {
ntfs_error(sb, "$MFTMirr location mismatch. Run chkdsk.");
up_read(&mirr_ni->runlist.lock);
return false;
}
} while (rl2[i++].length);
up_read(&mirr_ni->runlist.lock);
ntfs_debug("Done.");
return true;
}
static int load_and_check_logfile(struct ntfs_volume *vol,
struct restart_page_header **rp)
{
struct inode *tmp_ino;
int err = 0;
ntfs_debug("Entering.");
tmp_ino = ntfs_iget(vol->sb, FILE_LogFile);
if (IS_ERR(tmp_ino)) {
if (!IS_ERR(tmp_ino))
iput(tmp_ino);
return -ENOENT;
}
if (!ntfs_check_logfile(tmp_ino, rp))
err = -EINVAL;
NInoSetSparseDisabled(NTFS_I(tmp_ino));
vol->logfile_ino = tmp_ino;
ntfs_debug("Done.");
return err;
}
#define NTFS_HIBERFIL_HEADER_SIZE 4096
static int check_windows_hibernation_status(struct ntfs_volume *vol)
{
static const __le16 hiberfil[13] = { cpu_to_le16('h'),
cpu_to_le16('i'), cpu_to_le16('b'),
cpu_to_le16('e'), cpu_to_le16('r'),
cpu_to_le16('f'), cpu_to_le16('i'),
cpu_to_le16('l'), cpu_to_le16('.'),
cpu_to_le16('s'), cpu_to_le16('y'),
cpu_to_le16('s'), 0 };
u64 mref;
struct inode *vi;
struct folio *folio;
u32 *kaddr, *kend, *start_addr = NULL;
struct ntfs_name *name = NULL;
int ret = 1;
ntfs_debug("Entering.");
inode_lock(vol->root_ino);
mref = ntfs_lookup_inode_by_name(NTFS_I(vol->root_ino), hiberfil, 12,
&name);
inode_unlock(vol->root_ino);
kfree(name);
if (IS_ERR_MREF(mref)) {
ret = MREF_ERR(mref);
if (ret == -ENOENT) {
ntfs_debug("hiberfil.sys not present. Windows is not hibernated on the volume.");
return 0;
}
ntfs_error(vol->sb, "Failed to find inode number for hiberfil.sys.");
return ret;
}
vi = ntfs_iget(vol->sb, MREF(mref));
if (IS_ERR(vi)) {
if (!IS_ERR(vi))
iput(vi);
ntfs_error(vol->sb, "Failed to load hiberfil.sys.");
return IS_ERR(vi) ? PTR_ERR(vi) : -EIO;
}
if (unlikely(i_size_read(vi) < NTFS_HIBERFIL_HEADER_SIZE)) {
ntfs_debug("hiberfil.sys is smaller than 4kiB (0x%llx). Windows is hibernated on the volume. This is not the system volume.",
i_size_read(vi));
goto iput_out;
}
folio = read_mapping_folio(vi->i_mapping, 0, NULL);
if (IS_ERR(folio)) {
ntfs_error(vol->sb, "Failed to read from hiberfil.sys.");
ret = PTR_ERR(folio);
goto iput_out;
}
start_addr = (u32 *)kmap_local_folio(folio, 0);
kaddr = start_addr;
if (*(__le32 *)kaddr == cpu_to_le32(0x72626968)) {
ntfs_debug("Magic \"hibr\" found in hiberfil.sys. Windows is hibernated on the volume. This is the system volume.");
goto unm_iput_out;
}
kend = kaddr + NTFS_HIBERFIL_HEADER_SIZE/sizeof(*kaddr);
do {
if (unlikely(*kaddr)) {
ntfs_debug("hiberfil.sys is larger than 4kiB (0x%llx), does not contain the \"hibr\" magic, and does not have a zero header. Windows is hibernated on the volume. This is not the system volume.",
i_size_read(vi));
goto unm_iput_out;
}
} while (++kaddr < kend);
ntfs_debug("hiberfil.sys contains a zero header. Windows is not hibernated on the volume. This is the system volume.");
ret = 0;
unm_iput_out:
kunmap_local(start_addr);
folio_put(folio);
iput_out:
iput(vi);
return ret;
}
static bool load_and_init_attrdef(struct ntfs_volume *vol)
{
loff_t i_size;
struct super_block *sb = vol->sb;
struct inode *ino;
struct folio *folio;
u8 *addr;
pgoff_t index, max_index;
unsigned int size;
ntfs_debug("Entering.");
ino = ntfs_iget(sb, FILE_AttrDef);
if (IS_ERR(ino)) {
if (!IS_ERR(ino))
iput(ino);
goto failed;
}
NInoSetSparseDisabled(NTFS_I(ino));
i_size = i_size_read(ino);
if (i_size <= 0 || i_size > 0x7fffffff)
goto iput_failed;
vol->attrdef = kvzalloc(i_size, GFP_NOFS);
if (!vol->attrdef)
goto iput_failed;
index = 0;
max_index = i_size >> PAGE_SHIFT;
size = PAGE_SIZE;
while (index < max_index) {
read_partial_attrdef_page:
folio = read_mapping_folio(ino->i_mapping, index, NULL);
if (IS_ERR(folio))
goto free_iput_failed;
addr = kmap_local_folio(folio, 0);
memcpy((u8 *)vol->attrdef + (index++ << PAGE_SHIFT),
addr, size);
kunmap_local(addr);
folio_put(folio);
}
if (size == PAGE_SIZE) {
size = i_size & ~PAGE_MASK;
if (size)
goto read_partial_attrdef_page;
}
vol->attrdef_size = i_size;
ntfs_debug("Read %llu bytes from $AttrDef.", i_size);
iput(ino);
return true;
free_iput_failed:
kvfree(vol->attrdef);
vol->attrdef = NULL;
iput_failed:
iput(ino);
failed:
ntfs_error(sb, "Failed to initialize attribute definition table.");
return false;
}
static bool load_and_init_upcase(struct ntfs_volume *vol)
{
loff_t i_size;
struct super_block *sb = vol->sb;
struct inode *ino;
struct folio *folio;
u8 *addr;
pgoff_t index, max_index;
unsigned int size;
ntfs_debug("Entering.");
ino = ntfs_iget(sb, FILE_UpCase);
if (IS_ERR(ino)) {
if (!IS_ERR(ino))
iput(ino);
goto upcase_failed;
}
i_size = i_size_read(ino);
if (!i_size || i_size & (sizeof(__le16) - 1) ||
i_size > 64ULL * 1024 * sizeof(__le16))
goto iput_upcase_failed;
vol->upcase = kvzalloc(i_size, GFP_NOFS);
if (!vol->upcase)
goto iput_upcase_failed;
index = 0;
max_index = i_size >> PAGE_SHIFT;
size = PAGE_SIZE;
while (index < max_index) {
read_partial_upcase_page:
folio = read_mapping_folio(ino->i_mapping, index, NULL);
if (IS_ERR(folio))
goto iput_upcase_failed;
addr = kmap_local_folio(folio, 0);
memcpy((char *)vol->upcase + (index++ << PAGE_SHIFT),
addr, size);
kunmap_local(addr);
folio_put(folio);
}
if (size == PAGE_SIZE) {
size = i_size & ~PAGE_MASK;
if (size)
goto read_partial_upcase_page;
}
vol->upcase_len = i_size >> sizeof(unsigned char);
ntfs_debug("Read %llu bytes from $UpCase (expected %zu bytes).",
i_size, 64 * 1024 * sizeof(__le16));
iput(ino);
mutex_lock(&ntfs_lock);
if (!default_upcase) {
ntfs_debug("Using volume specified $UpCase since default is not present.");
mutex_unlock(&ntfs_lock);
return true;
}
if (default_upcase_len == vol->upcase_len &&
!memcmp(vol->upcase, default_upcase,
default_upcase_len * sizeof(*default_upcase))) {
kvfree(vol->upcase);
vol->upcase = default_upcase;
ntfs_nr_upcase_users++;
mutex_unlock(&ntfs_lock);
ntfs_debug("Volume specified $UpCase matches default. Using default.");
return true;
}
mutex_unlock(&ntfs_lock);
ntfs_debug("Using volume specified $UpCase since it does not match the default.");
return true;
iput_upcase_failed:
iput(ino);
kvfree(vol->upcase);
vol->upcase = NULL;
upcase_failed:
mutex_lock(&ntfs_lock);
if (default_upcase) {
vol->upcase = default_upcase;
vol->upcase_len = default_upcase_len;
ntfs_nr_upcase_users++;
mutex_unlock(&ntfs_lock);
ntfs_error(sb, "Failed to load $UpCase from the volume. Using default.");
return true;
}
mutex_unlock(&ntfs_lock);
ntfs_error(sb, "Failed to initialize upcase table.");
return false;
}
static struct lock_class_key
lcnbmp_runlist_lock_key, lcnbmp_mrec_lock_key,
mftbmp_runlist_lock_key, mftbmp_mrec_lock_key;
static bool load_system_files(struct ntfs_volume *vol)
{
struct super_block *sb = vol->sb;
struct mft_record *m;
struct volume_information *vi;
struct ntfs_attr_search_ctx *ctx;
struct restart_page_header *rp;
int err;
ntfs_debug("Entering.");
if (!load_and_init_mft_mirror(vol) || !check_mft_mirror(vol)) {
if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) {
static const char *es1 = "Failed to load $MFTMirr";
static const char *es2 = "$MFTMirr does not match $MFT";
static const char *es3 = ". Run ntfsck and/or chkdsk.";
sb->s_flags |= SB_RDONLY;
ntfs_error(sb, "%s. Mounting read-only%s",
!vol->mftmirr_ino ? es1 : es2, es3);
}
NVolSetErrors(vol);
}
vol->mftbmp_ino = ntfs_attr_iget(vol->mft_ino, AT_BITMAP, NULL, 0);
if (IS_ERR(vol->mftbmp_ino)) {
ntfs_error(sb, "Failed to load $MFT/$BITMAP attribute.");
goto iput_mirr_err_out;
}
lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->runlist.lock,
&mftbmp_runlist_lock_key);
lockdep_set_class(&NTFS_I(vol->mftbmp_ino)->mrec_lock,
&mftbmp_mrec_lock_key);
if (!load_and_init_upcase(vol))
goto iput_mftbmp_err_out;
if (!load_and_init_attrdef(vol))
goto iput_upcase_err_out;
vol->lcnbmp_ino = ntfs_iget(sb, FILE_Bitmap);
if (IS_ERR(vol->lcnbmp_ino)) {
if (!IS_ERR(vol->lcnbmp_ino))
iput(vol->lcnbmp_ino);
goto bitmap_failed;
}
lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->runlist.lock,
&lcnbmp_runlist_lock_key);
lockdep_set_class(&NTFS_I(vol->lcnbmp_ino)->mrec_lock,
&lcnbmp_mrec_lock_key);
NInoSetSparseDisabled(NTFS_I(vol->lcnbmp_ino));
if ((vol->nr_clusters + 7) >> 3 > i_size_read(vol->lcnbmp_ino)) {
iput(vol->lcnbmp_ino);
bitmap_failed:
ntfs_error(sb, "Failed to load $Bitmap.");
goto iput_attrdef_err_out;
}
vol->vol_ino = ntfs_iget(sb, FILE_Volume);
if (IS_ERR(vol->vol_ino)) {
if (!IS_ERR(vol->vol_ino))
iput(vol->vol_ino);
volume_failed:
ntfs_error(sb, "Failed to load $Volume.");
goto iput_lcnbmp_err_out;
}
m = map_mft_record(NTFS_I(vol->vol_ino));
if (IS_ERR(m)) {
iput_volume_failed:
iput(vol->vol_ino);
goto volume_failed;
}
ctx = ntfs_attr_get_search_ctx(NTFS_I(vol->vol_ino), m);
if (!ctx) {
ntfs_error(sb, "Failed to get attribute search context.");
goto get_ctx_vol_failed;
}
if (!ntfs_attr_lookup(AT_VOLUME_NAME, NULL, 0, 0, 0, NULL, 0, ctx) &&
!ctx->attr->non_resident &&
!(ctx->attr->flags & (ATTR_IS_SPARSE | ATTR_IS_COMPRESSED)) &&
le32_to_cpu(ctx->attr->data.resident.value_length) > 0) {
err = ntfs_ucstonls(vol, (__le16 *)((u8 *)ctx->attr +
le16_to_cpu(ctx->attr->data.resident.value_offset)),
le32_to_cpu(ctx->attr->data.resident.value_length) / 2,
&vol->volume_label, NTFS_MAX_LABEL_LEN);
if (err < 0)
vol->volume_label = NULL;
}
ntfs_attr_reinit_search_ctx(ctx);
if (ntfs_attr_lookup(AT_VOLUME_INFORMATION, NULL, 0, 0, 0, NULL, 0,
ctx) || ctx->attr->non_resident || ctx->attr->flags) {
ntfs_attr_put_search_ctx(ctx);
get_ctx_vol_failed:
unmap_mft_record(NTFS_I(vol->vol_ino));
goto iput_volume_failed;
}
vi = (struct volume_information *)((char *)ctx->attr +
le16_to_cpu(ctx->attr->data.resident.value_offset));
vol->vol_flags = vi->flags;
vol->major_ver = vi->major_ver;
vol->minor_ver = vi->minor_ver;
ntfs_attr_put_search_ctx(ctx);
unmap_mft_record(NTFS_I(vol->vol_ino));
pr_info("volume version %i.%i, dev %s, cluster size %d\n",
vol->major_ver, vol->minor_ver, sb->s_id, vol->cluster_size);
if (vol->vol_flags & VOLUME_MUST_MOUNT_RO_MASK) {
static const char *es1a = "Volume is dirty";
static const char *es1b = "Volume has been modified by chkdsk";
static const char *es1c = "Volume has unsupported flags set";
static const char *es2a = ". Run chkdsk and mount in Windows.";
static const char *es2b = ". Mount in Windows.";
const char *es1, *es2;
es2 = es2a;
if (vol->vol_flags & VOLUME_IS_DIRTY)
es1 = es1a;
else if (vol->vol_flags & VOLUME_MODIFIED_BY_CHKDSK) {
es1 = es1b;
es2 = es2b;
} else {
es1 = es1c;
ntfs_warning(sb, "Unsupported volume flags 0x%x encountered.",
(unsigned int)le16_to_cpu(vol->vol_flags));
}
if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) {
sb->s_flags |= SB_RDONLY;
ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
}
}
rp = NULL;
err = load_and_check_logfile(vol, &rp);
if (err) {
if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) {
sb->s_flags |= SB_RDONLY;
ntfs_error(sb, "Failed to load LogFile. Mounting read-only.");
}
NVolSetErrors(vol);
}
kvfree(rp);
vol->root_ino = ntfs_iget(sb, FILE_root);
if (IS_ERR(vol->root_ino)) {
if (!IS_ERR(vol->root_ino))
iput(vol->root_ino);
ntfs_error(sb, "Failed to load root directory.");
goto iput_logfile_err_out;
}
err = check_windows_hibernation_status(vol);
if (unlikely(err)) {
static const char *es1a = "Failed to determine if Windows is hibernated";
static const char *es1b = "Windows is hibernated";
static const char *es2 = ". Run chkdsk.";
const char *es1;
es1 = err < 0 ? es1a : es1b;
if (!sb_rdonly(sb) && vol->on_errors == ON_ERRORS_REMOUNT_RO) {
sb->s_flags |= SB_RDONLY;
ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
}
NVolSetErrors(vol);
}
if (!sb_rdonly(sb) &&
vol->logfile_ino && !ntfs_empty_logfile(vol->logfile_ino) &&
vol->on_errors == ON_ERRORS_REMOUNT_RO) {
static const char *es1 = "Failed to empty LogFile";
static const char *es2 = ". Mount in Windows.";
ntfs_error(sb, "%s. Mounting read-only%s", es1, es2);
sb->s_flags |= SB_RDONLY;
NVolSetErrors(vol);
}
if (unlikely(vol->major_ver < 3))
return true;
vol->secure_ino = ntfs_iget(sb, FILE_Secure);
if (IS_ERR(vol->secure_ino)) {
if (!IS_ERR(vol->secure_ino))
iput(vol->secure_ino);
ntfs_error(sb, "Failed to load $Secure.");
goto iput_root_err_out;
}
vol->extend_ino = ntfs_iget(sb, FILE_Extend);
if (IS_ERR(vol->extend_ino) ||
!S_ISDIR(vol->extend_ino->i_mode)) {
if (!IS_ERR(vol->extend_ino))
iput(vol->extend_ino);
ntfs_error(sb, "Failed to load $Extend.");
goto iput_sec_err_out;
}
return true;
iput_sec_err_out:
iput(vol->secure_ino);
iput_root_err_out:
iput(vol->root_ino);
iput_logfile_err_out:
if (vol->logfile_ino)
iput(vol->logfile_ino);
iput(vol->vol_ino);
iput_lcnbmp_err_out:
iput(vol->lcnbmp_ino);
iput_attrdef_err_out:
vol->attrdef_size = 0;
if (vol->attrdef) {
kvfree(vol->attrdef);
vol->attrdef = NULL;
}
iput_upcase_err_out:
vol->upcase_len = 0;
mutex_lock(&ntfs_lock);
if (vol->upcase && vol->upcase == default_upcase) {
ntfs_nr_upcase_users--;
vol->upcase = NULL;
}
mutex_unlock(&ntfs_lock);
if (vol->upcase) {
kvfree(vol->upcase);
vol->upcase = NULL;
}
iput_mftbmp_err_out:
iput(vol->mftbmp_ino);
iput_mirr_err_out:
iput(vol->mftmirr_ino);
return false;
}
static void ntfs_volume_free(struct ntfs_volume *vol)
{
vol->attrdef_size = 0;
if (vol->attrdef) {
kvfree(vol->attrdef);
vol->attrdef = NULL;
}
vol->upcase_len = 0;
mutex_lock(&ntfs_lock);
if (vol->upcase && vol->upcase == default_upcase) {
ntfs_nr_upcase_users--;
vol->upcase = NULL;
}
if (!ntfs_nr_upcase_users) {
kvfree(default_upcase);
default_upcase = NULL;
}
free_compression_buffers();
mutex_unlock(&ntfs_lock);
if (vol->upcase) {
kvfree(vol->upcase);
vol->upcase = NULL;
}
unload_nls(vol->nls_map);
kvfree(vol->lcn_empty_bits_per_page);
kfree(vol->volume_label);
kfree(vol);
}
static void ntfs_put_super(struct super_block *sb)
{
struct ntfs_volume *vol = NTFS_SB(sb);
pr_info("Entering %s, dev %s\n", __func__, sb->s_id);
cancel_work_sync(&vol->precalc_work);
ntfs_commit_inode(vol->vol_ino);
if (vol->major_ver >= 3) {
if (vol->extend_ino)
ntfs_commit_inode(vol->extend_ino);
if (vol->secure_ino)
ntfs_commit_inode(vol->secure_ino);
}
ntfs_commit_inode(vol->root_ino);
ntfs_commit_inode(vol->lcnbmp_ino);
ntfs_commit_inode(vol->mftbmp_ino);
if (vol->logfile_ino)
ntfs_commit_inode(vol->logfile_ino);
if (vol->mftmirr_ino)
ntfs_commit_inode(vol->mftmirr_ino);
ntfs_commit_inode(vol->mft_ino);
if (!sb_rdonly(sb)) {
if (!NVolErrors(vol)) {
if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY))
ntfs_warning(sb,
"Failed to clear dirty bit in volume information flags. Run chkdsk.");
ntfs_commit_inode(vol->vol_ino);
ntfs_commit_inode(vol->root_ino);
if (vol->mftmirr_ino)
ntfs_commit_inode(vol->mftmirr_ino);
ntfs_commit_inode(vol->mft_ino);
} else {
ntfs_warning(sb,
"Volume has errors. Leaving volume marked dirty. Run chkdsk.");
}
}
iput(vol->vol_ino);
vol->vol_ino = NULL;
if (vol->major_ver >= 3) {
if (vol->extend_ino) {
iput(vol->extend_ino);
vol->extend_ino = NULL;
}
if (vol->secure_ino) {
iput(vol->secure_ino);
vol->secure_ino = NULL;
}
}
iput(vol->root_ino);
vol->root_ino = NULL;
iput(vol->lcnbmp_ino);
vol->lcnbmp_ino = NULL;
iput(vol->mftbmp_ino);
vol->mftbmp_ino = NULL;
if (vol->logfile_ino) {
iput(vol->logfile_ino);
vol->logfile_ino = NULL;
}
if (vol->mftmirr_ino) {
ntfs_commit_inode(vol->mftmirr_ino);
ntfs_commit_inode(vol->mft_ino);
iput(vol->mftmirr_ino);
vol->mftmirr_ino = NULL;
}
ntfs_commit_inode(vol->mft_ino);
write_inode_now(vol->mft_ino, 1);
iput(vol->mft_ino);
vol->mft_ino = NULL;
blkdev_issue_flush(sb->s_bdev);
ntfs_volume_free(vol);
}
int ntfs_force_shutdown(struct super_block *sb, u32 flags)
{
struct ntfs_volume *vol = NTFS_SB(sb);
int ret;
if (NVolShutdown(vol))
return 0;
switch (flags) {
case FS_SHUTDOWN_FLAGS_DEFAULT:
case FS_SHUTDOWN_FLAGS_LOGFLUSH:
ret = bdev_freeze(sb->s_bdev);
if (ret)
return ret;
bdev_thaw(sb->s_bdev);
NVolSetShutdown(vol);
break;
case FS_SHUTDOWN_FLAGS_NOLOGFLUSH:
NVolSetShutdown(vol);
break;
default:
return -EINVAL;
}
return 0;
}
static void ntfs_shutdown(struct super_block *sb)
{
ntfs_force_shutdown(sb, FS_SHUTDOWN_FLAGS_NOLOGFLUSH);
}
static int ntfs_sync_fs(struct super_block *sb, int wait)
{
struct ntfs_volume *vol = NTFS_SB(sb);
int err = 0;
if (NVolShutdown(vol))
return -EIO;
if (!wait)
return 0;
if (ntfs_clear_volume_flags(vol, VOLUME_IS_DIRTY)) {
ntfs_warning(sb, "Failed to clear dirty bit in volume information flags. Run chkdsk.");
err = -EIO;
}
sync_inodes_sb(sb);
sync_blockdev(sb->s_bdev);
blkdev_issue_flush(sb->s_bdev);
return err;
}
s64 get_nr_free_clusters(struct ntfs_volume *vol)
{
s64 nr_free = vol->nr_clusters;
u32 nr_used;
struct address_space *mapping = vol->lcnbmp_ino->i_mapping;
struct folio *folio;
pgoff_t index, max_index;
struct file_ra_state ra = { 0 };
ntfs_debug("Entering.");
if (NVolFreeClusterKnown(vol))
return atomic64_read(&vol->free_clusters);
file_ra_state_init(&ra, mapping);
max_index = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >>
PAGE_SHIFT;
ntfs_debug("Reading $Bitmap, max_index = 0x%lx, max_size = 0x%lx.",
max_index, PAGE_SIZE / 4);
for (index = 0; index < max_index; index++) {
unsigned long *kaddr;
folio = ntfs_get_locked_folio(mapping, index, max_index, &ra);
if (IS_ERR(folio)) {
ntfs_debug("Skipping page (index 0x%lx).", index);
nr_free -= PAGE_SIZE * 8;
vol->lcn_empty_bits_per_page[index] = 0;
continue;
}
kaddr = kmap_local_folio(folio, 0);
nr_used = bitmap_weight(kaddr, PAGE_SIZE * BITS_PER_BYTE);
nr_free -= nr_used;
vol->lcn_empty_bits_per_page[index] = PAGE_SIZE * BITS_PER_BYTE - nr_used;
kunmap_local(kaddr);
folio_unlock(folio);
folio_put(folio);
}
ntfs_debug("Finished reading $Bitmap, last index = 0x%lx.", index - 1);
if (vol->nr_clusters & 63)
nr_free += 64 - (vol->nr_clusters & 63);
if (nr_free < 0)
nr_free = 0;
else
atomic64_set(&vol->free_clusters, nr_free);
NVolSetFreeClusterKnown(vol);
wake_up_all(&vol->free_waitq);
ntfs_debug("Exiting.");
return nr_free;
}
s64 ntfs_available_clusters_count(struct ntfs_volume *vol, s64 nr_clusters)
{
s64 free_clusters;
if (!NVolFreeClusterKnown(vol))
wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
free_clusters = atomic64_read(&vol->free_clusters) -
atomic64_read(&vol->dirty_clusters);
if (free_clusters <= 0)
return -ENOSPC;
else if (free_clusters < nr_clusters)
nr_clusters = free_clusters;
return nr_clusters;
}
static unsigned long __get_nr_free_mft_records(struct ntfs_volume *vol,
s64 nr_free, const pgoff_t max_index)
{
struct address_space *mapping = vol->mftbmp_ino->i_mapping;
struct folio *folio;
pgoff_t index;
struct file_ra_state ra = { 0 };
ntfs_debug("Entering.");
file_ra_state_init(&ra, mapping);
ntfs_debug("Reading $MFT/$BITMAP, max_index = 0x%lx, max_size = 0x%lx.",
max_index, PAGE_SIZE / 4);
for (index = 0; index < max_index; index++) {
unsigned long *kaddr;
folio = ntfs_get_locked_folio(mapping, index, max_index, &ra);
if (IS_ERR(folio)) {
ntfs_debug("read_mapping_page() error. Skipping page (index 0x%lx).",
index);
nr_free -= PAGE_SIZE * 8;
continue;
}
kaddr = kmap_local_folio(folio, 0);
nr_free -= bitmap_weight(kaddr,
PAGE_SIZE * BITS_PER_BYTE);
kunmap_local(kaddr);
folio_unlock(folio);
folio_put(folio);
}
ntfs_debug("Finished reading $MFT/$BITMAP, last index = 0x%lx.",
index - 1);
if (nr_free < 0)
nr_free = 0;
else
atomic64_set(&vol->free_mft_records, nr_free);
ntfs_debug("Exiting.");
return nr_free;
}
static int ntfs_statfs(struct dentry *dentry, struct kstatfs *sfs)
{
struct super_block *sb = dentry->d_sb;
s64 size;
struct ntfs_volume *vol = NTFS_SB(sb);
struct ntfs_inode *mft_ni = NTFS_I(vol->mft_ino);
unsigned long flags;
ntfs_debug("Entering.");
sfs->f_type = NTFS_SB_MAGIC;
sfs->f_bsize = vol->cluster_size;
sfs->f_frsize = vol->cluster_size;
sfs->f_blocks = vol->nr_clusters;
if (!NVolFreeClusterKnown(vol))
wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
size = atomic64_read(&vol->free_clusters) -
atomic64_read(&vol->dirty_clusters);
if (size < 0LL)
size = 0LL;
sfs->f_bavail = sfs->f_bfree = size;
read_lock_irqsave(&mft_ni->size_lock, flags);
sfs->f_files = i_size_read(vol->mft_ino) >> vol->mft_record_size_bits;
read_unlock_irqrestore(&mft_ni->size_lock, flags);
sfs->f_ffree = atomic64_read(&vol->free_mft_records);
sfs->f_fsid = u64_to_fsid(vol->serial_no);
sfs->f_namelen = NTFS_MAX_NAME_LEN;
return 0;
}
static int ntfs_write_inode(struct inode *vi, struct writeback_control *wbc)
{
return __ntfs_write_inode(vi, wbc->sync_mode == WB_SYNC_ALL);
}
static const struct super_operations ntfs_sops = {
.alloc_inode = ntfs_alloc_big_inode,
.free_inode = ntfs_free_big_inode,
.drop_inode = ntfs_drop_big_inode,
.write_inode = ntfs_write_inode,
.put_super = ntfs_put_super,
.shutdown = ntfs_shutdown,
.sync_fs = ntfs_sync_fs,
.statfs = ntfs_statfs,
.evict_inode = ntfs_evict_big_inode,
.show_options = ntfs_show_options,
};
static void precalc_free_clusters(struct work_struct *work)
{
struct ntfs_volume *vol = container_of(work, struct ntfs_volume, precalc_work);
s64 nr_free;
nr_free = get_nr_free_clusters(vol);
ntfs_debug("pre-calculate free clusters(%lld) using workqueue",
nr_free);
}
static struct lock_class_key ntfs_mft_inval_lock_key;
static int ntfs_fill_super(struct super_block *sb, struct fs_context *fc)
{
char *boot;
struct inode *tmp_ino;
int blocksize, result;
pgoff_t lcn_bit_pages;
struct ntfs_volume *vol = NTFS_SB(sb);
int silent = fc->sb_flags & SB_SILENT;
vol->sb = sb;
lockdep_off();
ntfs_debug("Entering.");
if (vol->nls_map && !strcmp(vol->nls_map->charset, "utf8"))
vol->nls_utf8 = true;
if (NVolDisableSparse(vol))
vol->preallocated_size = 0;
if (NVolDiscard(vol) && !bdev_max_discard_sectors(sb->s_bdev)) {
ntfs_warning(
sb,
"Discard requested but device does not support discard. Discard disabled.");
NVolClearDiscard(vol);
}
if (bdev_logical_block_size(sb->s_bdev) > PAGE_SIZE) {
if (!silent)
ntfs_error(sb,
"Device has unsupported sector size (%i). The maximum supported sector size on this architecture is %lu bytes.",
bdev_logical_block_size(sb->s_bdev),
PAGE_SIZE);
goto err_out_now;
}
blocksize = sb_min_blocksize(sb, NTFS_BLOCK_SIZE);
if (blocksize < NTFS_BLOCK_SIZE) {
if (!silent)
ntfs_error(sb, "Unable to set device block size.");
goto err_out_now;
}
ntfs_debug("Set device block size to %i bytes (block size bits %i).",
blocksize, sb->s_blocksize_bits);
if (!bdev_nr_bytes(sb->s_bdev)) {
if (!silent)
ntfs_error(sb, "Unable to determine device size.");
goto err_out_now;
}
vol->nr_blocks = bdev_nr_bytes(sb->s_bdev) >>
sb->s_blocksize_bits;
boot = read_ntfs_boot_sector(sb, silent);
if (!boot) {
if (!silent)
ntfs_error(sb, "Not an NTFS volume.");
goto err_out_now;
}
result = parse_ntfs_boot_sector(vol, (struct ntfs_boot_sector *)boot);
kfree(boot);
if (!result) {
if (!silent)
ntfs_error(sb, "Unsupported NTFS filesystem.");
goto err_out_now;
}
if (vol->sector_size > blocksize) {
blocksize = sb_set_blocksize(sb, vol->sector_size);
if (blocksize != vol->sector_size) {
if (!silent)
ntfs_error(sb,
"Unable to set device block size to sector size (%i).",
vol->sector_size);
goto err_out_now;
}
vol->nr_blocks = bdev_nr_bytes(sb->s_bdev) >>
sb->s_blocksize_bits;
ntfs_debug("Changed device block size to %i bytes (block size bits %i) to match volume sector size.",
blocksize, sb->s_blocksize_bits);
}
ntfs_setup_allocators(vol);
sb->s_magic = NTFS_SB_MAGIC;
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_time_gran = 100;
sb->s_xattr = ntfs_xattr_handlers;
sb->s_op = &ntfs_sops;
tmp_ino = new_inode(sb);
if (!tmp_ino) {
if (!silent)
ntfs_error(sb, "Failed to load essential metadata.");
goto err_out_now;
}
tmp_ino->i_ino = FILE_MFT;
insert_inode_hash(tmp_ino);
if (ntfs_read_inode_mount(tmp_ino) < 0) {
if (!silent)
ntfs_error(sb, "Failed to load essential metadata.");
goto iput_tmp_ino_err_out_now;
}
lockdep_set_class(&tmp_ino->i_mapping->invalidate_lock,
&ntfs_mft_inval_lock_key);
mutex_lock(&ntfs_lock);
if (!default_upcase)
default_upcase = generate_default_upcase();
ntfs_nr_upcase_users++;
mutex_unlock(&ntfs_lock);
lcn_bit_pages = (((vol->nr_clusters + 7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT;
vol->lcn_empty_bits_per_page = kvmalloc_array(lcn_bit_pages, sizeof(unsigned int),
GFP_KERNEL);
if (!vol->lcn_empty_bits_per_page) {
ntfs_error(sb,
"Unable to allocate pages for storing LCN empty bit counts\n");
goto unl_upcase_iput_tmp_ino_err_out_now;
}
if (!load_system_files(vol)) {
ntfs_error(sb, "Failed to load system files.");
goto unl_upcase_iput_tmp_ino_err_out_now;
}
ihold(vol->root_ino);
sb->s_root = d_make_root(vol->root_ino);
if (sb->s_root) {
s64 nr_records;
ntfs_debug("Exiting, status successful.");
mutex_lock(&ntfs_lock);
if (!--ntfs_nr_upcase_users && default_upcase) {
kvfree(default_upcase);
default_upcase = NULL;
}
mutex_unlock(&ntfs_lock);
sb->s_export_op = &ntfs_export_ops;
lockdep_on();
nr_records = __get_nr_free_mft_records(vol,
i_size_read(vol->mft_ino) >> vol->mft_record_size_bits,
((((NTFS_I(vol->mft_ino)->initialized_size >>
vol->mft_record_size_bits) +
7) >> 3) + PAGE_SIZE - 1) >> PAGE_SHIFT);
ntfs_debug("Free mft records(%lld)", nr_records);
init_waitqueue_head(&vol->free_waitq);
INIT_WORK(&vol->precalc_work, precalc_free_clusters);
queue_work(ntfs_wq, &vol->precalc_work);
return 0;
}
ntfs_error(sb, "Failed to allocate root directory.");
iput(vol->vol_ino);
vol->vol_ino = NULL;
if (vol->major_ver >= 3) {
if (vol->extend_ino) {
iput(vol->extend_ino);
vol->extend_ino = NULL;
}
if (vol->secure_ino) {
iput(vol->secure_ino);
vol->secure_ino = NULL;
}
}
iput(vol->root_ino);
vol->root_ino = NULL;
iput(vol->lcnbmp_ino);
vol->lcnbmp_ino = NULL;
iput(vol->mftbmp_ino);
vol->mftbmp_ino = NULL;
if (vol->logfile_ino) {
iput(vol->logfile_ino);
vol->logfile_ino = NULL;
}
if (vol->mftmirr_ino) {
iput(vol->mftmirr_ino);
vol->mftmirr_ino = NULL;
}
vol->attrdef_size = 0;
if (vol->attrdef) {
kvfree(vol->attrdef);
vol->attrdef = NULL;
}
vol->upcase_len = 0;
mutex_lock(&ntfs_lock);
if (vol->upcase && vol->upcase == default_upcase) {
ntfs_nr_upcase_users--;
vol->upcase = NULL;
}
mutex_unlock(&ntfs_lock);
if (vol->upcase) {
kvfree(vol->upcase);
vol->upcase = NULL;
}
if (vol->nls_map) {
unload_nls(vol->nls_map);
vol->nls_map = NULL;
}
unl_upcase_iput_tmp_ino_err_out_now:
mutex_lock(&ntfs_lock);
if (!--ntfs_nr_upcase_users && default_upcase) {
kvfree(default_upcase);
default_upcase = NULL;
}
mutex_unlock(&ntfs_lock);
iput_tmp_ino_err_out_now:
iput(tmp_ino);
if (vol->mft_ino && vol->mft_ino != tmp_ino)
iput(vol->mft_ino);
vol->mft_ino = NULL;
err_out_now:
sb->s_fs_info = NULL;
kvfree(vol->lcn_empty_bits_per_page);
kfree(vol->volume_label);
unload_nls(vol->nls_map);
kfree(vol);
ntfs_debug("Failed, returning -EINVAL.");
lockdep_on();
return -EINVAL;
}
struct kmem_cache *ntfs_name_cache;
struct kmem_cache *ntfs_inode_cache;
struct kmem_cache *ntfs_big_inode_cache;
static void ntfs_big_inode_init_once(void *foo)
{
struct ntfs_inode *ni = foo;
inode_init_once(VFS_I(ni));
}
struct kmem_cache *ntfs_attr_ctx_cache;
struct kmem_cache *ntfs_index_ctx_cache;
DEFINE_MUTEX(ntfs_lock);
static int ntfs_get_tree(struct fs_context *fc)
{
return get_tree_bdev(fc, ntfs_fill_super);
}
static void ntfs_free_fs_context(struct fs_context *fc)
{
struct ntfs_volume *vol = fc->s_fs_info;
if (vol)
ntfs_volume_free(vol);
}
static const struct fs_context_operations ntfs_context_ops = {
.parse_param = ntfs_parse_param,
.get_tree = ntfs_get_tree,
.free = ntfs_free_fs_context,
.reconfigure = ntfs_reconfigure,
};
static int ntfs_init_fs_context(struct fs_context *fc)
{
struct ntfs_volume *vol;
vol = kmalloc(sizeof(struct ntfs_volume), GFP_NOFS);
if (!vol)
return -ENOMEM;
*vol = (struct ntfs_volume) {
.uid = INVALID_UID,
.gid = INVALID_GID,
.fmask = 0,
.dmask = 0,
.mft_zone_multiplier = 1,
.on_errors = ON_ERRORS_CONTINUE,
.nls_map = load_nls_default(),
.preallocated_size = NTFS_DEF_PREALLOC_SIZE,
};
NVolSetShowHiddenFiles(vol);
NVolSetCaseSensitive(vol);
init_rwsem(&vol->mftbmp_lock);
init_rwsem(&vol->lcnbmp_lock);
mutex_init(&vol->volume_label_lock);
fc->s_fs_info = vol;
fc->ops = &ntfs_context_ops;
return 0;
}
static struct file_system_type ntfs_fs_type = {
.owner = THIS_MODULE,
.name = "ntfs",
.init_fs_context = ntfs_init_fs_context,
.parameters = ntfs_parameters,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
};
MODULE_ALIAS_FS("ntfs");
static int ntfs_workqueue_init(void)
{
ntfs_wq = alloc_workqueue("ntfs-bg-io", WQ_PERCPU, 0);
if (!ntfs_wq)
return -ENOMEM;
return 0;
}
static void ntfs_workqueue_destroy(void)
{
destroy_workqueue(ntfs_wq);
ntfs_wq = NULL;
}
static const char ntfs_index_ctx_cache_name[] = "ntfs_index_ctx_cache";
static const char ntfs_attr_ctx_cache_name[] = "ntfs_attr_ctx_cache";
static const char ntfs_name_cache_name[] = "ntfs_name_cache";
static const char ntfs_inode_cache_name[] = "ntfs_inode_cache";
static const char ntfs_big_inode_cache_name[] = "ntfs_big_inode_cache";
static int __init init_ntfs_fs(void)
{
int err = 0;
err = ntfs_workqueue_init();
if (err) {
pr_crit("Failed to register workqueue!\n");
return err;
}
ntfs_index_ctx_cache = kmem_cache_create(ntfs_index_ctx_cache_name,
sizeof(struct ntfs_index_context), 0 ,
SLAB_HWCACHE_ALIGN, NULL );
if (!ntfs_index_ctx_cache) {
pr_crit("Failed to create %s!\n", ntfs_index_ctx_cache_name);
goto ictx_err_out;
}
ntfs_attr_ctx_cache = kmem_cache_create(ntfs_attr_ctx_cache_name,
sizeof(struct ntfs_attr_search_ctx), 0 ,
SLAB_HWCACHE_ALIGN, NULL );
if (!ntfs_attr_ctx_cache) {
pr_crit("NTFS: Failed to create %s!\n",
ntfs_attr_ctx_cache_name);
goto actx_err_out;
}
ntfs_name_cache = kmem_cache_create(ntfs_name_cache_name,
(NTFS_MAX_NAME_LEN+2) * sizeof(__le16), 0,
SLAB_HWCACHE_ALIGN, NULL);
if (!ntfs_name_cache) {
pr_crit("Failed to create %s!\n", ntfs_name_cache_name);
goto name_err_out;
}
ntfs_inode_cache = kmem_cache_create(ntfs_inode_cache_name,
sizeof(struct ntfs_inode), 0, SLAB_RECLAIM_ACCOUNT, NULL);
if (!ntfs_inode_cache) {
pr_crit("Failed to create %s!\n", ntfs_inode_cache_name);
goto inode_err_out;
}
ntfs_big_inode_cache = kmem_cache_create(ntfs_big_inode_cache_name,
sizeof(struct big_ntfs_inode), 0, SLAB_HWCACHE_ALIGN |
SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
ntfs_big_inode_init_once);
if (!ntfs_big_inode_cache) {
pr_crit("Failed to create %s!\n", ntfs_big_inode_cache_name);
goto big_inode_err_out;
}
err = ntfs_sysctl(1);
if (err) {
pr_crit("Failed to register NTFS sysctls!\n");
goto sysctl_err_out;
}
err = register_filesystem(&ntfs_fs_type);
if (!err) {
ntfs_debug("NTFS driver registered successfully.");
return 0;
}
pr_crit("Failed to register NTFS filesystem driver!\n");
ntfs_sysctl(0);
sysctl_err_out:
kmem_cache_destroy(ntfs_big_inode_cache);
big_inode_err_out:
kmem_cache_destroy(ntfs_inode_cache);
inode_err_out:
kmem_cache_destroy(ntfs_name_cache);
name_err_out:
kmem_cache_destroy(ntfs_attr_ctx_cache);
actx_err_out:
kmem_cache_destroy(ntfs_index_ctx_cache);
ictx_err_out:
if (!err) {
pr_crit("Aborting NTFS filesystem driver registration...\n");
err = -ENOMEM;
}
return err;
}
static void __exit exit_ntfs_fs(void)
{
ntfs_debug("Unregistering NTFS driver.");
unregister_filesystem(&ntfs_fs_type);
rcu_barrier();
kmem_cache_destroy(ntfs_big_inode_cache);
kmem_cache_destroy(ntfs_inode_cache);
kmem_cache_destroy(ntfs_name_cache);
kmem_cache_destroy(ntfs_attr_ctx_cache);
kmem_cache_destroy(ntfs_index_ctx_cache);
ntfs_workqueue_destroy();
ntfs_sysctl(0);
}
module_init(init_ntfs_fs);
module_exit(exit_ntfs_fs);
MODULE_AUTHOR("Anton Altaparmakov <anton@tuxera.com>");
MODULE_AUTHOR("Namjae Jeon <linkinjeon@kernel.org>");
MODULE_DESCRIPTION("NTFS read-write filesystem driver");
MODULE_LICENSE("GPL");
#ifdef DEBUG
module_param(debug_msgs, uint, 0);
MODULE_PARM_DESC(debug_msgs, "Enable debug messages.");
#endif