44 #include <linux/slab.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
48 #include <linux/stat.h>
49 #include <linux/cdrom.h>
54 #include <linux/errno.h>
57 #include <linux/bitmap.h>
60 #include <asm/byteorder.h>
66 #include <asm/uaccess.h>
68 #define VDS_POS_PRIMARY_VOL_DESC 0
69 #define VDS_POS_UNALLOC_SPACE_DESC 1
70 #define VDS_POS_LOGICAL_VOL_DESC 2
71 #define VDS_POS_PARTITION_DESC 3
72 #define VDS_POS_IMP_USE_VOL_DESC 4
73 #define VDS_POS_VOL_DESC_PTR 5
74 #define VDS_POS_TERMINATING_DESC 6
75 #define VDS_POS_LENGTH 7
77 #define UDF_DEFAULT_BLOCKSIZE 2048
82 static int udf_fill_super(
struct super_block *,
void *,
int);
85 static int udf_remount_fs(
struct super_block *,
int *,
char *);
89 static void udf_load_fileset(
struct super_block *,
struct buffer_head *,
93 static unsigned int udf_count_free(
struct super_block *);
109 int flags,
const char *dev_name,
void *
data)
111 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
147 static void udf_destroy_inode(
struct inode *inode)
152 static void init_once(
void *
foo)
160 static int init_inodecache(
void)
167 if (!udf_inode_cachep)
172 static void destroy_inodecache(
void)
184 .alloc_inode = udf_alloc_inode,
185 .destroy_inode = udf_destroy_inode,
188 .put_super = udf_put_super,
189 .sync_fs = udf_sync_fs,
190 .statfs = udf_statfs,
191 .remount_fs = udf_remount_fs,
192 .show_options = udf_show_options,
214 static int __init init_udf_fs(
void)
218 err = init_inodecache();
228 destroy_inodecache();
234 static void __exit exit_udf_fs(
void)
237 destroy_inodecache();
250 udf_err(sb,
"Unable to allocate space for %d partition maps\n",
267 for (i = 0; i < nr_groups; i++)
291 for (i = 0; i < 4; i++)
306 static void udf_sb_free_partitions(
struct super_block *sb)
317 static int udf_show_options(
struct seq_file *seq,
struct dentry *root)
478 uopt->
volume = 0xFFFFFFFF;
525 if (!gid_valid(uopt->
gid))
533 if (!uid_valid(uopt->
uid))
587 #ifdef CONFIG_UDF_NLS
608 uopt->
fmode = option & 0777;
613 uopt->
dmode = option & 0777;
616 pr_err(
"bad mount option \"%s\" or missing value\n", p);
623 static int udf_remount_fs(
struct super_block *sb,
int *
flags,
char *options)
636 if (!udf_parse_options(options, &uopt,
true))
657 if (*flags & MS_RDONLY)
668 static loff_t udf_check_vsd(
struct super_block *sb)
673 struct buffer_head *bh =
NULL;
686 udf_debug(
"Starting at sector %u (%ld byte sectors)\n",
690 for (; !nsr02 && !nsr03; sector +=
sectorsize) {
707 udf_debug(
"ISO9660 Boot Record found\n");
710 udf_debug(
"ISO9660 Primary Volume Descriptor found\n");
713 udf_debug(
"ISO9660 Supplementary Volume Descriptor found\n");
716 udf_debug(
"ISO9660 Volume Partition Descriptor found\n");
719 udf_debug(
"ISO9660 Volume Descriptor Set Terminator found\n");
752 static int udf_find_fileset(
struct super_block *sb,
756 struct buffer_head *bh =
NULL;
814 *fileset = newfileset;
830 udf_debug(
"Fileset at block=%d, partition=%d\n",
835 udf_load_fileset(sb, bh, root);
846 struct buffer_head *bh;
870 udf_debug(
"recording time %04u/%02u/%02u %02u:%02u (%x)\n",
881 UDF_SB(sb)->s_volume_ident);
901 struct inode *metadata_fe;
908 if (metadata_fe ==
NULL)
909 udf_warn(sb,
"metadata inode efe not found\n");
911 udf_warn(sb,
"metadata inode efe does not have short allocation descriptors!\n");
927 mdata = &map->s_type_specific.s_metadata;
930 udf_debug(
"Metadata file location: block = %d part = %d\n",
938 udf_debug(
"Mirror metadata file location: block = %d part = %d\n",
945 udf_err(sb,
"Both metadata and mirror metadata inode efe can not found\n");
957 addr.partitionReferenceNum = map->s_partition_num;
959 udf_debug(
"Bitmap file location: block = %d part = %d\n",
960 addr.logicalBlockNum,
addr.partitionReferenceNum);
966 udf_warn(sb,
"bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
968 udf_err(sb,
"bitmap inode efe not found and attempted read-write mount\n");
974 udf_debug(
"udf_load_metadata_files Ok\n");
982 static void udf_load_fileset(
struct super_block *sb,
struct buffer_head *bh,
993 udf_debug(
"Rootdir at block=%d, partition=%d\n",
999 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1028 static int udf_fill_partdesc_info(
struct super_block *sb,
1049 udf_debug(
"Partition (%d type %x) starts at physical %d, block length %d\n",
1062 .partitionReferenceNum = p_index,
1067 udf_debug(
"cannot load unallocSpaceTable (part %d)\n",
1072 udf_debug(
"unallocSpaceTable (part %d) @ %ld\n",
1077 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1086 udf_debug(
"unallocSpaceBitmap (part %d) @ %d\n",
1091 udf_debug(
"partitionIntegrityTable (part %d)\n", p_index);
1097 .partitionReferenceNum = p_index,
1102 udf_debug(
"cannot load freedSpaceTable (part %d)\n",
1108 udf_debug(
"freedSpaceTable (part %d) @ %ld\n",
1113 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1122 udf_debug(
"freedSpaceBitmap (part %d) @ %d\n",
1128 static void udf_find_vat_block(
struct super_block *sb,
int p_index,
1140 ino.partitionReferenceNum = type1_index;
1141 for (vat_block = start_block;
1143 vat_block >= start_block - 3 &&
1150 static int udf_load_vat(
struct super_block *sb,
int p_index,
int type1_index)
1154 struct buffer_head *bh =
NULL;
1160 udf_find_vat_block(sb, p_index, type1_index, sbi->
s_last_block);
1163 pr_notice(
"Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1165 (
unsigned long)blocks - 1);
1166 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
1179 bh = sb_bread(sb, pos);
1193 s_start_offset) >> 2;
1201 struct buffer_head *bh;
1222 udf_debug(
"Searching map: (%d == %d)\n",
1231 udf_debug(
"Partition (%d) not found in partition map\n",
1236 ret = udf_fill_partdesc_info(sb, p, i);
1256 ret = udf_fill_partdesc_info(sb, p, i);
1261 ret = udf_load_metadata_files(sb, i);
1263 udf_err(sb,
"error loading MetaData partition map %d\n",
1268 ret = udf_load_vat(sb, i, type1_idx);
1277 pr_notice(
"Filesystem marked read-only because writing to pseudooverwrite partition is not implemented\n");
1285 static int udf_load_sparable_map(
struct super_block *sb,
1294 struct buffer_head *bh;
1299 udf_err(sb,
"error loading logical volume descriptor: "
1300 "Invalid packet length %u\n",
1305 udf_err(sb,
"error loading logical volume descriptor: "
1306 "Too many sparing tables (%d)\n",
1342 struct buffer_head *bh;
1343 unsigned int table_len;
1353 udf_err(sb,
"error loading logical volume descriptor: "
1354 "Partition table too long (%u > %lu)\n", table_len,
1364 for (i = 0, offset = 0;
1378 }
else if (type == 2) {
1400 if (udf_load_sparable_map(sb, map,
1413 udf_debug(
"Parsing Logical vol part %d type %d id=%s\n",
1429 if (mdm->
flags & 0x01)
1432 udf_debug(
"Metadata Ident suffix=0x%x\n",
1437 udf_debug(
"Metadata part alloc unit size=%d\n",
1455 udf_debug(
"Partition (%d:%d) type %d on volume %d\n",
1463 udf_debug(
"FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1481 struct buffer_head *bh =
NULL;
1494 udf_load_logicalvolint(sb,
1524 struct buffer_head *bh =
NULL;
1532 long next_s = 0, next_e = 0;
1540 for (; (!done && block <= lastblock); block++) {
1545 "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
1546 (
unsigned long long)block);
1607 next_s = next_e = 0;
1619 udf_err(sb,
"Primary Volume Descriptor not found!\n");
1637 if (udf_load_partdesc(sb, block))
1644 static int udf_load_sequence(
struct super_block *sb,
struct buffer_head *bh,
1648 long main_s, main_e, reserve_s, reserve_e;
1662 reserve_e += reserve_s;
1666 if (!udf_process_sequence(sb, main_s, main_e, fileset))
1668 udf_sb_free_partitions(sb);
1669 if (!udf_process_sequence(sb, reserve_s, reserve_e, fileset))
1671 udf_sb_free_partitions(sb);
1682 struct buffer_head *bh;
1698 ret = udf_load_sequence(sb, bh, fileset);
1714 if (udf_check_anchor_block(sb, sbi->
s_anchor, fileset))
1724 if (udf_check_anchor_block(sb, sbi->
s_session + 256, fileset))
1730 last[last_count++] = lastblock;
1732 last[last_count++] = lastblock - 1;
1733 last[last_count++] = lastblock + 1;
1735 last[last_count++] = lastblock - 2;
1736 if (lastblock >= 150)
1737 last[last_count++] = lastblock - 150;
1738 if (lastblock >= 152)
1739 last[last_count++] = lastblock - 152;
1741 for (i = 0; i < last_count; i++) {
1742 if (last[i] >= sb->
s_bdev->bd_inode->i_size >>
1745 if (udf_check_anchor_block(sb, last[i], fileset))
1749 if (udf_check_anchor_block(sb, last[i] - 256, fileset))
1754 if (udf_check_anchor_block(sb, sbi->
s_session + 512, fileset))
1767 static int udf_find_anchor(
struct super_block *sb,
1773 lastblock = udf_scan_anchors(sb, sbi->
s_last_block, fileset);
1780 lastblock = udf_scan_anchors(sb,
1787 lastblock = udf_scan_anchors(sb, sbi->
s_last_block, fileset);
1816 nsr_off = udf_check_vsd(sb);
1823 udf_debug(
"Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
1827 udf_debug(
"Validity check skipped because of novrs option\n");
1832 if (!udf_find_anchor(sb, fileset)) {
1843 struct buffer_head *bh = sbi->
s_lvid_bh;
1870 static void udf_close_lvid(
struct super_block *sb)
1873 struct buffer_head *bh = sbi->
s_lvid_bh;
1904 set_buffer_uptodate(bh);
1912 struct buffer_head *bh;
1928 if (!(++uniqueID & 0xFFFFFFFF))
1937 static int udf_fill_super(
struct super_block *sb,
void *options,
int silent)
1940 struct inode *inode =
NULL;
1960 if (!udf_parse_options((
char *)options, &uopt,
false))
1965 udf_err(sb,
"utf8 cannot be combined with iocharset\n");
1968 #ifdef CONFIG_UDF_NLS
1992 if (uopt.
session == 0xFFFFFFFF)
2000 sb->
s_op = &udf_sb_ops;
2007 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2010 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2013 pr_notice(
"Rescanning with blocksize %d\n",
2018 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2022 udf_warn(sb,
"No partition found (1)\n");
2037 udf_err(sb,
"minUDFReadRev=%x (max is %x)\n",
2053 udf_warn(sb,
"No partition found (2)\n");
2059 pr_notice(
"Partition marked readonly; forcing readonly mount\n");
2063 if (udf_find_fileset(sb, &fileset, &rootdir)) {
2064 udf_warn(sb,
"No fileset found\n");
2071 udf_info(
"Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2076 if (!(sb->
s_flags & MS_RDONLY))
2084 udf_err(sb,
"Error in udf_iget, block=%d, partition=%d\n",
2085 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2092 udf_err(sb,
"Couldn't allocate root dentry\n");
2102 #ifdef CONFIG_UDF_NLS
2106 if (!(sb->
s_flags & MS_RDONLY))
2109 udf_sb_free_partitions(sb);
2117 const char *
fmt, ...)
2127 pr_err(
"error (device %s): %s: %pV", sb->
s_id,
function, &vaf);
2133 const char *
fmt, ...)
2143 pr_warn(
"warning (device %s): %s: %pV", sb->
s_id,
function, &vaf);
2156 #ifdef CONFIG_UDF_NLS
2160 if (!(sb->
s_flags & MS_RDONLY))
2163 udf_sb_free_partitions(sb);
2191 u64 id = huge_encode_dev(sb->
s_bdev->bd_dev);
2201 buf->
f_bfree = udf_count_free(sb);
2214 static unsigned int udf_count_free_bitmap(
struct super_block *sb,
2217 struct buffer_head *bh =
NULL;
2218 unsigned int accum = 0;
2220 int block = 0, newblock;
2228 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2232 udf_err(sb,
"udf_count_free failed\n");
2236 udf_err(sb,
"udf_count_free failed\n");
2247 accum += bitmap_weight((
const unsigned long *)(ptr + index),
2252 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2267 static unsigned int udf_count_free_table(
struct super_block *sb,
2268 struct inode *
table)
2270 unsigned int accum = 0;
2277 epos.block = UDF_I(table)->i_location;
2281 while ((etype =
udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2282 accum += (elen >> table->
i_sb->s_blocksize_bits);
2290 static unsigned int udf_count_free(
struct super_block *sb)
2292 unsigned int accum = 0;
2304 if (accum == 0xFFFFFFFF)
2314 accum += udf_count_free_bitmap(sb,
2318 accum += udf_count_free_bitmap(sb,
2325 accum += udf_count_free_table(sb,
2329 accum += udf_count_free_table(sb,