5 * Super block routines for the OSTA-UDF(tm) filesystem.
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * https://www.ecma.ch/
15 * https://www.iso.org/
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
36 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
38 * 12/20/98 find the free space bitmap (if it exists)
43 #include <linux/blkdev.h>
44 #include <linux/slab.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/parser.h>
48 #include <linux/stat.h>
49 #include <linux/cdrom.h>
50 #include <linux/nls.h>
51 #include <linux/vfs.h>
52 #include <linux/vmalloc.h>
53 #include <linux/errno.h>
54 #include <linux/mount.h>
55 #include <linux/seq_file.h>
56 #include <linux/bitmap.h>
57 #include <linux/crc-itu-t.h>
58 #include <linux/log2.h>
59 #include <asm/byteorder.h>
60 #include <linux/iversion.h>
65 #include <linux/init.h>
66 #include <linux/uaccess.h>
69 VDS_POS_PRIMARY_VOL_DESC,
70 VDS_POS_UNALLOC_SPACE_DESC,
71 VDS_POS_LOGICAL_VOL_DESC,
72 VDS_POS_IMP_USE_VOL_DESC,
76 #define VSD_FIRST_SECTOR_OFFSET 32768
77 #define VSD_MAX_SECTOR_OFFSET 0x800000
80 * Maximum number of Terminating Descriptor / Logical Volume Integrity
81 * Descriptor redirections. The chosen numbers are arbitrary - just that we
82 * hopefully don't limit any real use of rewritten inode on write-once media
83 * but avoid looping for too long on corrupted media.
85 #define UDF_MAX_TD_NESTING 64
86 #define UDF_MAX_LVID_NESTING 1000
88 enum { UDF_MAX_LINKS = 0xffff };
90 * We limit filesize to 4TB. This is arbitrary as the on-disk format supports
91 * more but because the file space is described by a linked list of extents,
92 * each of which can have at most 1GB, the creation and handling of extents
93 * gets unusably slow beyond certain point...
95 #define UDF_MAX_FILESIZE (1ULL << 42)
97 /* These are the "meat" - everything else is stuffing */
98 static int udf_fill_super(struct super_block *, void *, int);
99 static void udf_put_super(struct super_block *);
100 static int udf_sync_fs(struct super_block *, int);
101 static int udf_remount_fs(struct super_block *, int *, char *);
102 static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
103 static void udf_open_lvid(struct super_block *);
104 static void udf_close_lvid(struct super_block *);
105 static unsigned int udf_count_free(struct super_block *);
106 static int udf_statfs(struct dentry *, struct kstatfs *);
107 static int udf_show_options(struct seq_file *, struct dentry *);
109 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
111 struct logicalVolIntegrityDesc *lvid;
112 unsigned int partnum;
115 if (!UDF_SB(sb)->s_lvid_bh)
117 lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
118 partnum = le32_to_cpu(lvid->numOfPartitions);
119 /* The offset is to skip freeSpaceTable and sizeTable arrays */
120 offset = partnum * 2 * sizeof(uint32_t);
121 return (struct logicalVolIntegrityDescImpUse *)
122 (((uint8_t *)(lvid + 1)) + offset);
125 /* UDF filesystem type */
126 static struct dentry *udf_mount(struct file_system_type *fs_type,
127 int flags, const char *dev_name, void *data)
129 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
132 static struct file_system_type udf_fstype = {
133 .owner = THIS_MODULE,
136 .kill_sb = kill_block_super,
137 .fs_flags = FS_REQUIRES_DEV,
139 MODULE_ALIAS_FS("udf");
141 static struct kmem_cache *udf_inode_cachep;
143 static struct inode *udf_alloc_inode(struct super_block *sb)
145 struct udf_inode_info *ei;
146 ei = alloc_inode_sb(sb, udf_inode_cachep, GFP_KERNEL);
151 ei->i_lenExtents = 0;
152 ei->i_lenStreams = 0;
153 ei->i_next_alloc_block = 0;
154 ei->i_next_alloc_goal = 0;
158 init_rwsem(&ei->i_data_sem);
159 ei->cached_extent.lstart = -1;
160 spin_lock_init(&ei->i_extent_cache_lock);
161 inode_set_iversion(&ei->vfs_inode, 1);
163 return &ei->vfs_inode;
166 static void udf_free_in_core_inode(struct inode *inode)
168 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
171 static void init_once(void *foo)
173 struct udf_inode_info *ei = foo;
176 inode_init_once(&ei->vfs_inode);
179 static int __init init_inodecache(void)
181 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
182 sizeof(struct udf_inode_info),
183 0, (SLAB_RECLAIM_ACCOUNT |
187 if (!udf_inode_cachep)
192 static void destroy_inodecache(void)
195 * Make sure all delayed rcu free inodes are flushed before we
199 kmem_cache_destroy(udf_inode_cachep);
202 /* Superblock operations */
203 static const struct super_operations udf_sb_ops = {
204 .alloc_inode = udf_alloc_inode,
205 .free_inode = udf_free_in_core_inode,
206 .write_inode = udf_write_inode,
207 .evict_inode = udf_evict_inode,
208 .put_super = udf_put_super,
209 .sync_fs = udf_sync_fs,
210 .statfs = udf_statfs,
211 .remount_fs = udf_remount_fs,
212 .show_options = udf_show_options,
217 unsigned int blocksize;
218 unsigned int session;
219 unsigned int lastblock;
227 struct nls_table *nls_map;
230 static int __init init_udf_fs(void)
234 err = init_inodecache();
237 err = register_filesystem(&udf_fstype);
244 destroy_inodecache();
250 static void __exit exit_udf_fs(void)
252 unregister_filesystem(&udf_fstype);
253 destroy_inodecache();
256 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
258 struct udf_sb_info *sbi = UDF_SB(sb);
260 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
261 if (!sbi->s_partmaps) {
262 sbi->s_partitions = 0;
266 sbi->s_partitions = count;
270 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
273 int nr_groups = bitmap->s_nr_groups;
275 for (i = 0; i < nr_groups; i++)
276 brelse(bitmap->s_block_bitmap[i]);
281 static void udf_free_partition(struct udf_part_map *map)
284 struct udf_meta_data *mdata;
286 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
287 iput(map->s_uspace.s_table);
288 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
289 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
290 if (map->s_partition_type == UDF_SPARABLE_MAP15)
291 for (i = 0; i < 4; i++)
292 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
293 else if (map->s_partition_type == UDF_METADATA_MAP25) {
294 mdata = &map->s_type_specific.s_metadata;
295 iput(mdata->s_metadata_fe);
296 mdata->s_metadata_fe = NULL;
298 iput(mdata->s_mirror_fe);
299 mdata->s_mirror_fe = NULL;
301 iput(mdata->s_bitmap_fe);
302 mdata->s_bitmap_fe = NULL;
306 static void udf_sb_free_partitions(struct super_block *sb)
308 struct udf_sb_info *sbi = UDF_SB(sb);
311 if (!sbi->s_partmaps)
313 for (i = 0; i < sbi->s_partitions; i++)
314 udf_free_partition(&sbi->s_partmaps[i]);
315 kfree(sbi->s_partmaps);
316 sbi->s_partmaps = NULL;
319 static int udf_show_options(struct seq_file *seq, struct dentry *root)
321 struct super_block *sb = root->d_sb;
322 struct udf_sb_info *sbi = UDF_SB(sb);
324 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
325 seq_puts(seq, ",nostrict");
326 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
327 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
328 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
329 seq_puts(seq, ",unhide");
330 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
331 seq_puts(seq, ",undelete");
332 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
333 seq_puts(seq, ",noadinicb");
334 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
335 seq_puts(seq, ",shortad");
336 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
337 seq_puts(seq, ",uid=forget");
338 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
339 seq_puts(seq, ",gid=forget");
340 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
341 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
342 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
343 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
344 if (sbi->s_umask != 0)
345 seq_printf(seq, ",umask=%ho", sbi->s_umask);
346 if (sbi->s_fmode != UDF_INVALID_MODE)
347 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
348 if (sbi->s_dmode != UDF_INVALID_MODE)
349 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
350 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
351 seq_printf(seq, ",session=%d", sbi->s_session);
352 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
353 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
354 if (sbi->s_anchor != 0)
355 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
357 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
359 seq_puts(seq, ",iocharset=utf8");
368 * Parse mount options.
371 * The following mount options are supported:
373 * gid= Set the default group.
374 * umask= Set the default umask.
375 * mode= Set the default file permissions.
376 * dmode= Set the default directory permissions.
377 * uid= Set the default user.
378 * bs= Set the block size.
379 * unhide Show otherwise hidden files.
380 * undelete Show deleted files in lists.
381 * adinicb Embed data in the inode (default)
382 * noadinicb Don't embed data in the inode
383 * shortad Use short ad's
384 * longad Use long ad's (default)
385 * nostrict Unset strict conformance
386 * iocharset= Set the NLS character set
388 * The remaining are for debugging and disaster recovery:
390 * novrs Skip volume sequence recognition
392 * The following expect a offset from 0.
394 * session= Set the CDROM session (default= last session)
395 * anchor= Override standard anchor location. (default= 256)
396 * volume= Override the VolumeDesc location. (unused)
397 * partition= Override the PartitionDesc location. (unused)
398 * lastblock= Set the last block of the filesystem/
400 * The following expect a offset from the partition root.
402 * fileset= Override the fileset block location. (unused)
403 * rootdir= Override the root directory location. (unused)
404 * WARNING: overriding the rootdir to a non-directory may
405 * yield highly unpredictable results.
408 * options Pointer to mount options string.
409 * uopts Pointer to mount options variable.
412 * <return> 1 Mount options parsed okay.
413 * <return> 0 Error parsing mount options.
416 * July 1, 1997 - Andrew E. Mileski
417 * Written, tested, and released.
421 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
422 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
423 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
424 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
425 Opt_rootdir, Opt_utf8, Opt_iocharset,
426 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
430 static const match_table_t tokens = {
431 {Opt_novrs, "novrs"},
432 {Opt_nostrict, "nostrict"},
434 {Opt_unhide, "unhide"},
435 {Opt_undelete, "undelete"},
436 {Opt_noadinicb, "noadinicb"},
437 {Opt_adinicb, "adinicb"},
438 {Opt_shortad, "shortad"},
439 {Opt_longad, "longad"},
440 {Opt_uforget, "uid=forget"},
441 {Opt_uignore, "uid=ignore"},
442 {Opt_gforget, "gid=forget"},
443 {Opt_gignore, "gid=ignore"},
446 {Opt_umask, "umask=%o"},
447 {Opt_session, "session=%u"},
448 {Opt_lastblock, "lastblock=%u"},
449 {Opt_anchor, "anchor=%u"},
450 {Opt_volume, "volume=%u"},
451 {Opt_partition, "partition=%u"},
452 {Opt_fileset, "fileset=%u"},
453 {Opt_rootdir, "rootdir=%u"},
455 {Opt_iocharset, "iocharset=%s"},
456 {Opt_fmode, "mode=%o"},
457 {Opt_dmode, "dmode=%o"},
461 static int udf_parse_options(char *options, struct udf_options *uopt,
469 uopt->session = 0xFFFFFFFF;
476 while ((p = strsep(&options, ",")) != NULL) {
477 substring_t args[MAX_OPT_ARGS];
483 token = match_token(p, tokens, args);
489 if (match_int(&args[0], &option))
492 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
495 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
498 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
501 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
504 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
507 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
510 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
513 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
516 if (match_uint(args, &uv))
518 uopt->gid = make_kgid(current_user_ns(), uv);
519 if (!gid_valid(uopt->gid))
521 uopt->flags |= (1 << UDF_FLAG_GID_SET);
524 if (match_uint(args, &uv))
526 uopt->uid = make_kuid(current_user_ns(), uv);
527 if (!uid_valid(uopt->uid))
529 uopt->flags |= (1 << UDF_FLAG_UID_SET);
532 if (match_octal(args, &option))
534 uopt->umask = option;
537 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
540 if (match_int(args, &option))
542 uopt->session = option;
544 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
547 if (match_int(args, &option))
549 uopt->lastblock = option;
551 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
554 if (match_int(args, &option))
556 uopt->anchor = option;
562 /* Ignored (never implemented properly) */
566 unload_nls(uopt->nls_map);
567 uopt->nls_map = NULL;
572 unload_nls(uopt->nls_map);
573 uopt->nls_map = NULL;
575 /* When nls_map is not loaded then UTF-8 is used */
576 if (!remount && strcmp(args[0].from, "utf8") != 0) {
577 uopt->nls_map = load_nls(args[0].from);
578 if (!uopt->nls_map) {
579 pr_err("iocharset %s not found\n",
586 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
590 /* These options are superseeded by uid=<number> */
593 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
596 if (match_octal(args, &option))
598 uopt->fmode = option & 0777;
601 if (match_octal(args, &option))
603 uopt->dmode = option & 0777;
606 pr_err("bad mount option \"%s\" or missing value\n", p);
613 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
615 struct udf_options uopt;
616 struct udf_sb_info *sbi = UDF_SB(sb);
619 if (!(*flags & SB_RDONLY) && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
624 uopt.flags = sbi->s_flags;
625 uopt.uid = sbi->s_uid;
626 uopt.gid = sbi->s_gid;
627 uopt.umask = sbi->s_umask;
628 uopt.fmode = sbi->s_fmode;
629 uopt.dmode = sbi->s_dmode;
632 if (!udf_parse_options(options, &uopt, true))
635 write_lock(&sbi->s_cred_lock);
636 sbi->s_flags = uopt.flags;
637 sbi->s_uid = uopt.uid;
638 sbi->s_gid = uopt.gid;
639 sbi->s_umask = uopt.umask;
640 sbi->s_fmode = uopt.fmode;
641 sbi->s_dmode = uopt.dmode;
642 write_unlock(&sbi->s_cred_lock);
644 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
647 if (*flags & SB_RDONLY)
657 * Check VSD descriptor. Returns -1 in case we are at the end of volume
658 * recognition area, 0 if the descriptor is valid but non-interesting, 1 if
659 * we found one of NSR descriptors we are looking for.
661 static int identify_vsd(const struct volStructDesc *vsd)
665 if (!memcmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) {
666 switch (vsd->structType) {
668 udf_debug("ISO9660 Boot Record found\n");
671 udf_debug("ISO9660 Primary Volume Descriptor found\n");
674 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
677 udf_debug("ISO9660 Volume Partition Descriptor found\n");
680 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
683 udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
686 } else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN))
688 else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN))
690 else if (!memcmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN))
692 else if (!memcmp(vsd->stdIdent, VSD_STD_ID_BOOT2, VSD_STD_ID_LEN))
694 else if (!memcmp(vsd->stdIdent, VSD_STD_ID_CDW02, VSD_STD_ID_LEN))
697 /* TEA01 or invalid id : end of volume recognition area */
705 * Check Volume Structure Descriptors (ECMA 167 2/9.1)
706 * We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1)
707 * @return 1 if NSR02 or NSR03 found,
708 * -1 if first sector read error, 0 otherwise
710 static int udf_check_vsd(struct super_block *sb)
712 struct volStructDesc *vsd = NULL;
713 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
715 struct buffer_head *bh = NULL;
717 struct udf_sb_info *sbi;
718 loff_t session_offset;
721 if (sb->s_blocksize < sizeof(struct volStructDesc))
722 sectorsize = sizeof(struct volStructDesc);
724 sectorsize = sb->s_blocksize;
726 session_offset = (loff_t)sbi->s_session << sb->s_blocksize_bits;
727 sector += session_offset;
729 udf_debug("Starting at sector %u (%lu byte sectors)\n",
730 (unsigned int)(sector >> sb->s_blocksize_bits),
732 /* Process the sequence (if applicable). The hard limit on the sector
733 * offset is arbitrary, hopefully large enough so that all valid UDF
734 * filesystems will be recognised. There is no mention of an upper
735 * bound to the size of the volume recognition area in the standard.
736 * The limit will prevent the code to read all the sectors of a
737 * specially crafted image (like a bluray disc full of CD001 sectors),
738 * potentially causing minutes or even hours of uninterruptible I/O
739 * activity. This actually happened with uninitialised SSD partitions
740 * (all 0xFF) before the check for the limit and all valid IDs were
742 for (; !nsr && sector < VSD_MAX_SECTOR_OFFSET; sector += sectorsize) {
744 bh = sb_bread(sb, sector >> sb->s_blocksize_bits);
748 vsd = (struct volStructDesc *)(bh->b_data +
749 (sector & (sb->s_blocksize - 1)));
750 nsr = identify_vsd(vsd);
751 /* Found NSR or end? */
757 * Special handling for improperly formatted VRS (e.g., Win10)
758 * where components are separated by 2048 bytes even though
761 if (sb->s_blocksize == 4096) {
762 nsr = identify_vsd(vsd + 1);
763 /* Ignore unknown IDs... */
772 else if (!bh && sector - session_offset == VSD_FIRST_SECTOR_OFFSET)
778 static int udf_verify_domain_identifier(struct super_block *sb,
779 struct regid *ident, char *dname)
781 struct domainIdentSuffix *suffix;
783 if (memcmp(ident->ident, UDF_ID_COMPLIANT, strlen(UDF_ID_COMPLIANT))) {
784 udf_warn(sb, "Not OSTA UDF compliant %s descriptor.\n", dname);
787 if (ident->flags & ENTITYID_FLAGS_DIRTY) {
788 udf_warn(sb, "Possibly not OSTA UDF compliant %s descriptor.\n",
792 suffix = (struct domainIdentSuffix *)ident->identSuffix;
793 if ((suffix->domainFlags & DOMAIN_FLAGS_HARD_WRITE_PROTECT) ||
794 (suffix->domainFlags & DOMAIN_FLAGS_SOFT_WRITE_PROTECT)) {
795 if (!sb_rdonly(sb)) {
796 udf_warn(sb, "Descriptor for %s marked write protected."
797 " Forcing read only mount.\n", dname);
806 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
810 static int udf_load_fileset(struct super_block *sb, struct fileSetDesc *fset,
811 struct kernel_lb_addr *root)
815 ret = udf_verify_domain_identifier(sb, &fset->domainIdent, "file set");
819 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
820 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
822 udf_debug("Rootdir at block=%u, partition=%u\n",
823 root->logicalBlockNum, root->partitionReferenceNum);
827 static int udf_find_fileset(struct super_block *sb,
828 struct kernel_lb_addr *fileset,
829 struct kernel_lb_addr *root)
831 struct buffer_head *bh;
835 if (fileset->logicalBlockNum == 0xFFFFFFFF &&
836 fileset->partitionReferenceNum == 0xFFFF)
839 bh = udf_read_ptagged(sb, fileset, 0, &ident);
842 if (ident != TAG_IDENT_FSD) {
847 udf_debug("Fileset at block=%u, partition=%u\n",
848 fileset->logicalBlockNum, fileset->partitionReferenceNum);
850 UDF_SB(sb)->s_partition = fileset->partitionReferenceNum;
851 ret = udf_load_fileset(sb, (struct fileSetDesc *)bh->b_data, root);
857 * Load primary Volume Descriptor Sequence
859 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
862 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
864 struct primaryVolDesc *pvoldesc;
866 struct buffer_head *bh;
869 struct timestamp *ts;
871 outstr = kmalloc(128, GFP_NOFS);
875 bh = udf_read_tagged(sb, block, block, &ident);
881 if (ident != TAG_IDENT_PVD) {
886 pvoldesc = (struct primaryVolDesc *)bh->b_data;
888 udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
889 pvoldesc->recordingDateAndTime);
890 ts = &pvoldesc->recordingDateAndTime;
891 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
892 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
893 ts->minute, le16_to_cpu(ts->typeAndTimezone));
895 ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
897 strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName");
898 pr_warn("incorrect volume identification, setting to "
901 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
903 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
905 ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
911 udf_debug("volSetIdent[] = '%s'\n", outstr);
921 struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
922 u32 meta_file_loc, u32 partition_ref)
924 struct kernel_lb_addr addr;
925 struct inode *metadata_fe;
927 addr.logicalBlockNum = meta_file_loc;
928 addr.partitionReferenceNum = partition_ref;
930 metadata_fe = udf_iget_special(sb, &addr);
932 if (IS_ERR(metadata_fe)) {
933 udf_warn(sb, "metadata inode efe not found\n");
936 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
937 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
939 return ERR_PTR(-EIO);
945 static int udf_load_metadata_files(struct super_block *sb, int partition,
948 struct udf_sb_info *sbi = UDF_SB(sb);
949 struct udf_part_map *map;
950 struct udf_meta_data *mdata;
951 struct kernel_lb_addr addr;
954 map = &sbi->s_partmaps[partition];
955 mdata = &map->s_type_specific.s_metadata;
956 mdata->s_phys_partition_ref = type1_index;
958 /* metadata address */
959 udf_debug("Metadata file location: block = %u part = %u\n",
960 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
962 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
963 mdata->s_phys_partition_ref);
965 /* mirror file entry */
966 udf_debug("Mirror metadata file location: block = %u part = %u\n",
967 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
969 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
970 mdata->s_phys_partition_ref);
973 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
976 mdata->s_mirror_fe = fe;
978 mdata->s_metadata_fe = fe;
984 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
986 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
987 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
988 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
990 udf_debug("Bitmap file location: block = %u part = %u\n",
991 addr.logicalBlockNum, addr.partitionReferenceNum);
993 fe = udf_iget_special(sb, &addr);
996 udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
998 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
1002 mdata->s_bitmap_fe = fe;
1005 udf_debug("udf_load_metadata_files Ok\n");
1009 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1011 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1012 return DIV_ROUND_UP(map->s_partition_len +
1013 (sizeof(struct spaceBitmapDesc) << 3),
1014 sb->s_blocksize * 8);
1017 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1019 struct udf_bitmap *bitmap;
1020 int nr_groups = udf_compute_nr_groups(sb, index);
1022 bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
1027 bitmap->s_nr_groups = nr_groups;
1031 static int check_partition_desc(struct super_block *sb,
1032 struct partitionDesc *p,
1033 struct udf_part_map *map)
1035 bool umap, utable, fmap, ftable;
1036 struct partitionHeaderDesc *phd;
1038 switch (le32_to_cpu(p->accessType)) {
1039 case PD_ACCESS_TYPE_READ_ONLY:
1040 case PD_ACCESS_TYPE_WRITE_ONCE:
1041 case PD_ACCESS_TYPE_NONE:
1045 /* No Partition Header Descriptor? */
1046 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1047 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1050 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1051 utable = phd->unallocSpaceTable.extLength;
1052 umap = phd->unallocSpaceBitmap.extLength;
1053 ftable = phd->freedSpaceTable.extLength;
1054 fmap = phd->freedSpaceBitmap.extLength;
1056 /* No allocation info? */
1057 if (!utable && !umap && !ftable && !fmap)
1060 /* We don't support blocks that require erasing before overwrite */
1063 /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
1067 if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1068 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1069 map->s_partition_type == UDF_METADATA_MAP25)
1076 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1080 static int udf_fill_partdesc_info(struct super_block *sb,
1081 struct partitionDesc *p, int p_index)
1083 struct udf_part_map *map;
1084 struct udf_sb_info *sbi = UDF_SB(sb);
1085 struct partitionHeaderDesc *phd;
1088 map = &sbi->s_partmaps[p_index];
1090 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1091 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1093 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1094 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1095 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1096 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1097 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1098 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1099 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1100 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1102 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
1103 p_index, map->s_partition_type,
1104 map->s_partition_root, map->s_partition_len);
1106 err = check_partition_desc(sb, p, map);
1111 * Skip loading allocation info it we cannot ever write to the fs.
1112 * This is a correctness thing as we may have decided to force ro mount
1113 * to avoid allocation info we don't support.
1115 if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
1118 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1119 if (phd->unallocSpaceTable.extLength) {
1120 struct kernel_lb_addr loc = {
1121 .logicalBlockNum = le32_to_cpu(
1122 phd->unallocSpaceTable.extPosition),
1123 .partitionReferenceNum = p_index,
1125 struct inode *inode;
1127 inode = udf_iget_special(sb, &loc);
1128 if (IS_ERR(inode)) {
1129 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1131 return PTR_ERR(inode);
1133 map->s_uspace.s_table = inode;
1134 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1135 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
1136 p_index, map->s_uspace.s_table->i_ino);
1139 if (phd->unallocSpaceBitmap.extLength) {
1140 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1143 map->s_uspace.s_bitmap = bitmap;
1144 bitmap->s_extPosition = le32_to_cpu(
1145 phd->unallocSpaceBitmap.extPosition);
1146 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1147 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
1148 p_index, bitmap->s_extPosition);
1154 static void udf_find_vat_block(struct super_block *sb, int p_index,
1155 int type1_index, sector_t start_block)
1157 struct udf_sb_info *sbi = UDF_SB(sb);
1158 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1160 struct kernel_lb_addr ino;
1161 struct inode *inode;
1164 * VAT file entry is in the last recorded block. Some broken disks have
1165 * it a few blocks before so try a bit harder...
1167 ino.partitionReferenceNum = type1_index;
1168 for (vat_block = start_block;
1169 vat_block >= map->s_partition_root &&
1170 vat_block >= start_block - 3; vat_block--) {
1171 ino.logicalBlockNum = vat_block - map->s_partition_root;
1172 inode = udf_iget_special(sb, &ino);
1173 if (!IS_ERR(inode)) {
1174 sbi->s_vat_inode = inode;
1180 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1182 struct udf_sb_info *sbi = UDF_SB(sb);
1183 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1184 struct buffer_head *bh = NULL;
1185 struct udf_inode_info *vati;
1186 struct virtualAllocationTable20 *vat20;
1187 sector_t blocks = sb_bdev_nr_blocks(sb);
1189 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
1190 if (!sbi->s_vat_inode &&
1191 sbi->s_last_block != blocks - 1) {
1192 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1193 (unsigned long)sbi->s_last_block,
1194 (unsigned long)blocks - 1);
1195 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
1197 if (!sbi->s_vat_inode)
1200 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1201 map->s_type_specific.s_virtual.s_start_offset = 0;
1202 map->s_type_specific.s_virtual.s_num_entries =
1203 (sbi->s_vat_inode->i_size - 36) >> 2;
1204 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1205 vati = UDF_I(sbi->s_vat_inode);
1206 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1209 bh = udf_bread(sbi->s_vat_inode, 0, 0, &err);
1212 err = -EFSCORRUPTED;
1215 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1217 vat20 = (struct virtualAllocationTable20 *)
1221 map->s_type_specific.s_virtual.s_start_offset =
1222 le16_to_cpu(vat20->lengthHeader);
1223 map->s_type_specific.s_virtual.s_num_entries =
1224 (sbi->s_vat_inode->i_size -
1225 map->s_type_specific.s_virtual.
1226 s_start_offset) >> 2;
1233 * Load partition descriptor block
1235 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1238 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1240 struct buffer_head *bh;
1241 struct partitionDesc *p;
1242 struct udf_part_map *map;
1243 struct udf_sb_info *sbi = UDF_SB(sb);
1245 uint16_t partitionNumber;
1249 bh = udf_read_tagged(sb, block, block, &ident);
1252 if (ident != TAG_IDENT_PD) {
1257 p = (struct partitionDesc *)bh->b_data;
1258 partitionNumber = le16_to_cpu(p->partitionNumber);
1260 /* First scan for TYPE1 and SPARABLE partitions */
1261 for (i = 0; i < sbi->s_partitions; i++) {
1262 map = &sbi->s_partmaps[i];
1263 udf_debug("Searching map: (%u == %u)\n",
1264 map->s_partition_num, partitionNumber);
1265 if (map->s_partition_num == partitionNumber &&
1266 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1267 map->s_partition_type == UDF_SPARABLE_MAP15))
1271 if (i >= sbi->s_partitions) {
1272 udf_debug("Partition (%u) not found in partition map\n",
1278 ret = udf_fill_partdesc_info(sb, p, i);
1283 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1284 * PHYSICAL partitions are already set up
1287 map = NULL; /* supress 'maybe used uninitialized' warning */
1288 for (i = 0; i < sbi->s_partitions; i++) {
1289 map = &sbi->s_partmaps[i];
1291 if (map->s_partition_num == partitionNumber &&
1292 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1293 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1294 map->s_partition_type == UDF_METADATA_MAP25))
1298 if (i >= sbi->s_partitions) {
1303 ret = udf_fill_partdesc_info(sb, p, i);
1307 if (map->s_partition_type == UDF_METADATA_MAP25) {
1308 ret = udf_load_metadata_files(sb, i, type1_idx);
1310 udf_err(sb, "error loading MetaData partition map %d\n",
1316 * If we have a partition with virtual map, we don't handle
1317 * writing to it (we overwrite blocks instead of relocating
1320 if (!sb_rdonly(sb)) {
1324 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1325 ret = udf_load_vat(sb, i, type1_idx);
1331 /* In case loading failed, we handle cleanup in udf_fill_super */
1336 static int udf_load_sparable_map(struct super_block *sb,
1337 struct udf_part_map *map,
1338 struct sparablePartitionMap *spm)
1342 struct sparingTable *st;
1343 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1345 struct buffer_head *bh;
1347 map->s_partition_type = UDF_SPARABLE_MAP15;
1348 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1349 if (!is_power_of_2(sdata->s_packet_len)) {
1350 udf_err(sb, "error loading logical volume descriptor: "
1351 "Invalid packet length %u\n",
1352 (unsigned)sdata->s_packet_len);
1355 if (spm->numSparingTables > 4) {
1356 udf_err(sb, "error loading logical volume descriptor: "
1357 "Too many sparing tables (%d)\n",
1358 (int)spm->numSparingTables);
1361 if (le32_to_cpu(spm->sizeSparingTable) > sb->s_blocksize) {
1362 udf_err(sb, "error loading logical volume descriptor: "
1363 "Too big sparing table size (%u)\n",
1364 le32_to_cpu(spm->sizeSparingTable));
1368 for (i = 0; i < spm->numSparingTables; i++) {
1369 loc = le32_to_cpu(spm->locSparingTable[i]);
1370 bh = udf_read_tagged(sb, loc, loc, &ident);
1374 st = (struct sparingTable *)bh->b_data;
1376 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1377 strlen(UDF_ID_SPARING)) ||
1378 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1384 sdata->s_spar_map[i] = bh;
1386 map->s_partition_func = udf_get_pblock_spar15;
1390 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1391 struct kernel_lb_addr *fileset)
1393 struct logicalVolDesc *lvd;
1396 struct udf_sb_info *sbi = UDF_SB(sb);
1397 struct genericPartitionMap *gpm;
1399 struct buffer_head *bh;
1400 unsigned int table_len;
1403 bh = udf_read_tagged(sb, block, block, &ident);
1406 BUG_ON(ident != TAG_IDENT_LVD);
1407 lvd = (struct logicalVolDesc *)bh->b_data;
1408 table_len = le32_to_cpu(lvd->mapTableLength);
1409 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
1410 udf_err(sb, "error loading logical volume descriptor: "
1411 "Partition table too long (%u > %lu)\n", table_len,
1412 sb->s_blocksize - sizeof(*lvd));
1417 ret = udf_verify_domain_identifier(sb, &lvd->domainIdent,
1421 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1425 for (i = 0, offset = 0;
1426 i < sbi->s_partitions && offset < table_len;
1427 i++, offset += gpm->partitionMapLength) {
1428 struct udf_part_map *map = &sbi->s_partmaps[i];
1429 gpm = (struct genericPartitionMap *)
1430 &(lvd->partitionMaps[offset]);
1431 type = gpm->partitionMapType;
1433 struct genericPartitionMap1 *gpm1 =
1434 (struct genericPartitionMap1 *)gpm;
1435 map->s_partition_type = UDF_TYPE1_MAP15;
1436 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1437 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1438 map->s_partition_func = NULL;
1439 } else if (type == 2) {
1440 struct udfPartitionMap2 *upm2 =
1441 (struct udfPartitionMap2 *)gpm;
1442 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1443 strlen(UDF_ID_VIRTUAL))) {
1445 le16_to_cpu(((__le16 *)upm2->partIdent.
1448 map->s_partition_type =
1450 map->s_partition_func =
1451 udf_get_pblock_virt15;
1453 map->s_partition_type =
1455 map->s_partition_func =
1456 udf_get_pblock_virt20;
1458 } else if (!strncmp(upm2->partIdent.ident,
1460 strlen(UDF_ID_SPARABLE))) {
1461 ret = udf_load_sparable_map(sb, map,
1462 (struct sparablePartitionMap *)gpm);
1465 } else if (!strncmp(upm2->partIdent.ident,
1467 strlen(UDF_ID_METADATA))) {
1468 struct udf_meta_data *mdata =
1469 &map->s_type_specific.s_metadata;
1470 struct metadataPartitionMap *mdm =
1471 (struct metadataPartitionMap *)
1472 &(lvd->partitionMaps[offset]);
1473 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
1474 i, type, UDF_ID_METADATA);
1476 map->s_partition_type = UDF_METADATA_MAP25;
1477 map->s_partition_func = udf_get_pblock_meta25;
1479 mdata->s_meta_file_loc =
1480 le32_to_cpu(mdm->metadataFileLoc);
1481 mdata->s_mirror_file_loc =
1482 le32_to_cpu(mdm->metadataMirrorFileLoc);
1483 mdata->s_bitmap_file_loc =
1484 le32_to_cpu(mdm->metadataBitmapFileLoc);
1485 mdata->s_alloc_unit_size =
1486 le32_to_cpu(mdm->allocUnitSize);
1487 mdata->s_align_unit_size =
1488 le16_to_cpu(mdm->alignUnitSize);
1489 if (mdm->flags & 0x01)
1490 mdata->s_flags |= MF_DUPLICATE_MD;
1492 udf_debug("Metadata Ident suffix=0x%x\n",
1493 le16_to_cpu(*(__le16 *)
1494 mdm->partIdent.identSuffix));
1495 udf_debug("Metadata part num=%u\n",
1496 le16_to_cpu(mdm->partitionNum));
1497 udf_debug("Metadata part alloc unit size=%u\n",
1498 le32_to_cpu(mdm->allocUnitSize));
1499 udf_debug("Metadata file loc=%u\n",
1500 le32_to_cpu(mdm->metadataFileLoc));
1501 udf_debug("Mirror file loc=%u\n",
1502 le32_to_cpu(mdm->metadataMirrorFileLoc));
1503 udf_debug("Bitmap file loc=%u\n",
1504 le32_to_cpu(mdm->metadataBitmapFileLoc));
1505 udf_debug("Flags: %d %u\n",
1506 mdata->s_flags, mdm->flags);
1508 udf_debug("Unknown ident: %s\n",
1509 upm2->partIdent.ident);
1512 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1513 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1515 udf_debug("Partition (%d:%u) type %u on volume %u\n",
1516 i, map->s_partition_num, type, map->s_volumeseqnum);
1520 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1522 *fileset = lelb_to_cpu(la->extLocation);
1523 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
1524 fileset->logicalBlockNum,
1525 fileset->partitionReferenceNum);
1527 if (lvd->integritySeqExt.extLength)
1528 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1531 if (!sbi->s_lvid_bh) {
1532 /* We can't generate unique IDs without a valid LVID */
1533 if (sb_rdonly(sb)) {
1534 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1536 udf_warn(sb, "Damaged or missing LVID, forcing "
1537 "readonly mount\n");
1547 * Find the prevailing Logical Volume Integrity Descriptor.
1549 static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1551 struct buffer_head *bh, *final_bh;
1553 struct udf_sb_info *sbi = UDF_SB(sb);
1554 struct logicalVolIntegrityDesc *lvid;
1555 int indirections = 0;
1556 u32 parts, impuselen;
1558 while (++indirections <= UDF_MAX_LVID_NESTING) {
1560 while (loc.extLength > 0 &&
1561 (bh = udf_read_tagged(sb, loc.extLocation,
1562 loc.extLocation, &ident))) {
1563 if (ident != TAG_IDENT_LVID) {
1571 loc.extLength -= sb->s_blocksize;
1578 brelse(sbi->s_lvid_bh);
1579 sbi->s_lvid_bh = final_bh;
1581 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1582 if (lvid->nextIntegrityExt.extLength == 0)
1585 loc = leea_to_cpu(lvid->nextIntegrityExt);
1588 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1589 UDF_MAX_LVID_NESTING);
1591 brelse(sbi->s_lvid_bh);
1592 sbi->s_lvid_bh = NULL;
1595 parts = le32_to_cpu(lvid->numOfPartitions);
1596 impuselen = le32_to_cpu(lvid->lengthOfImpUse);
1597 if (parts >= sb->s_blocksize || impuselen >= sb->s_blocksize ||
1598 sizeof(struct logicalVolIntegrityDesc) + impuselen +
1599 2 * parts * sizeof(u32) > sb->s_blocksize) {
1600 udf_warn(sb, "Corrupted LVID (parts=%u, impuselen=%u), "
1601 "ignoring.\n", parts, impuselen);
1607 * Step for reallocation of table of partition descriptor sequence numbers.
1608 * Must be power of 2.
1610 #define PART_DESC_ALLOC_STEP 32
1612 struct part_desc_seq_scan_data {
1613 struct udf_vds_record rec;
1617 struct desc_seq_scan_data {
1618 struct udf_vds_record vds[VDS_POS_LENGTH];
1619 unsigned int size_part_descs;
1620 unsigned int num_part_descs;
1621 struct part_desc_seq_scan_data *part_descs_loc;
1624 static struct udf_vds_record *handle_partition_descriptor(
1625 struct buffer_head *bh,
1626 struct desc_seq_scan_data *data)
1628 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1632 partnum = le16_to_cpu(desc->partitionNumber);
1633 for (i = 0; i < data->num_part_descs; i++)
1634 if (partnum == data->part_descs_loc[i].partnum)
1635 return &(data->part_descs_loc[i].rec);
1636 if (data->num_part_descs >= data->size_part_descs) {
1637 struct part_desc_seq_scan_data *new_loc;
1638 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1640 new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
1642 return ERR_PTR(-ENOMEM);
1643 memcpy(new_loc, data->part_descs_loc,
1644 data->size_part_descs * sizeof(*new_loc));
1645 kfree(data->part_descs_loc);
1646 data->part_descs_loc = new_loc;
1647 data->size_part_descs = new_size;
1649 return &(data->part_descs_loc[data->num_part_descs++].rec);
1653 static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1654 struct buffer_head *bh, struct desc_seq_scan_data *data)
1657 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1658 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
1659 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1660 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
1661 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1662 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
1663 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1664 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1665 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1666 return handle_partition_descriptor(bh, data);
1672 * Process a main/reserve volume descriptor sequence.
1673 * @block First block of first extent of the sequence.
1674 * @lastblock Lastblock of first extent of the sequence.
1675 * @fileset There we store extent containing root fileset
1677 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1680 static noinline int udf_process_sequence(
1681 struct super_block *sb,
1682 sector_t block, sector_t lastblock,
1683 struct kernel_lb_addr *fileset)
1685 struct buffer_head *bh = NULL;
1686 struct udf_vds_record *curr;
1687 struct generic_desc *gd;
1688 struct volDescPtr *vdp;
1693 unsigned int indirections = 0;
1694 struct desc_seq_scan_data data;
1697 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1698 data.size_part_descs = PART_DESC_ALLOC_STEP;
1699 data.num_part_descs = 0;
1700 data.part_descs_loc = kcalloc(data.size_part_descs,
1701 sizeof(*data.part_descs_loc),
1703 if (!data.part_descs_loc)
1707 * Read the main descriptor sequence and find which descriptors
1710 for (; (!done && block <= lastblock); block++) {
1711 bh = udf_read_tagged(sb, block, block, &ident);
1715 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1716 gd = (struct generic_desc *)bh->b_data;
1717 vdsn = le32_to_cpu(gd->volDescSeqNum);
1719 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1720 if (++indirections > UDF_MAX_TD_NESTING) {
1721 udf_err(sb, "too many Volume Descriptor "
1722 "Pointers (max %u supported)\n",
1723 UDF_MAX_TD_NESTING);
1729 vdp = (struct volDescPtr *)bh->b_data;
1730 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1731 lastblock = le32_to_cpu(
1732 vdp->nextVolDescSeqExt.extLength) >>
1733 sb->s_blocksize_bits;
1734 lastblock += block - 1;
1735 /* For loop is going to increment 'block' again */
1738 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1739 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1740 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1741 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1742 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1743 curr = get_volume_descriptor_record(ident, bh, &data);
1746 ret = PTR_ERR(curr);
1749 /* Descriptor we don't care about? */
1752 if (vdsn >= curr->volDescSeqNum) {
1753 curr->volDescSeqNum = vdsn;
1754 curr->block = block;
1757 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1764 * Now read interesting descriptors again and process them
1765 * in a suitable order
1767 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1768 udf_err(sb, "Primary Volume Descriptor not found!\n");
1772 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
1776 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
1777 ret = udf_load_logicalvol(sb,
1778 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1784 /* Now handle prevailing Partition Descriptors */
1785 for (i = 0; i < data.num_part_descs; i++) {
1786 ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
1792 kfree(data.part_descs_loc);
1797 * Load Volume Descriptor Sequence described by anchor in bh
1799 * Returns <0 on error, 0 on success
1801 static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1802 struct kernel_lb_addr *fileset)
1804 struct anchorVolDescPtr *anchor;
1805 sector_t main_s, main_e, reserve_s, reserve_e;
1808 anchor = (struct anchorVolDescPtr *)bh->b_data;
1810 /* Locate the main sequence */
1811 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1812 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1813 main_e = main_e >> sb->s_blocksize_bits;
1814 main_e += main_s - 1;
1816 /* Locate the reserve sequence */
1817 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1818 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1819 reserve_e = reserve_e >> sb->s_blocksize_bits;
1820 reserve_e += reserve_s - 1;
1822 /* Process the main & reserve sequences */
1823 /* responsible for finding the PartitionDesc(s) */
1824 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1827 udf_sb_free_partitions(sb);
1828 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1830 udf_sb_free_partitions(sb);
1831 /* No sequence was OK, return -EIO */
1839 * Check whether there is an anchor block in the given block and
1840 * load Volume Descriptor Sequence if so.
1842 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1845 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1846 struct kernel_lb_addr *fileset)
1848 struct buffer_head *bh;
1852 bh = udf_read_tagged(sb, block, block, &ident);
1855 if (ident != TAG_IDENT_AVDP) {
1859 ret = udf_load_sequence(sb, bh, fileset);
1865 * Search for an anchor volume descriptor pointer.
1867 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1870 static int udf_scan_anchors(struct super_block *sb, udf_pblk_t *lastblock,
1871 struct kernel_lb_addr *fileset)
1875 struct udf_sb_info *sbi = UDF_SB(sb);
1879 /* First try user provided anchor */
1880 if (sbi->s_anchor) {
1881 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1886 * according to spec, anchor is in either:
1890 * however, if the disc isn't closed, it could be 512.
1892 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1896 * The trouble is which block is the last one. Drives often misreport
1897 * this so we try various possibilities.
1899 last[last_count++] = *lastblock;
1900 if (*lastblock >= 1)
1901 last[last_count++] = *lastblock - 1;
1902 last[last_count++] = *lastblock + 1;
1903 if (*lastblock >= 2)
1904 last[last_count++] = *lastblock - 2;
1905 if (*lastblock >= 150)
1906 last[last_count++] = *lastblock - 150;
1907 if (*lastblock >= 152)
1908 last[last_count++] = *lastblock - 152;
1910 for (i = 0; i < last_count; i++) {
1911 if (last[i] >= sb_bdev_nr_blocks(sb))
1913 ret = udf_check_anchor_block(sb, last[i], fileset);
1914 if (ret != -EAGAIN) {
1916 *lastblock = last[i];
1921 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1922 if (ret != -EAGAIN) {
1924 *lastblock = last[i];
1929 /* Finally try block 512 in case media is open */
1930 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
1934 * Check Volume Structure Descriptor, find Anchor block and load Volume
1935 * Descriptor Sequence.
1937 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1938 * block was not found.
1940 static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1941 int silent, struct kernel_lb_addr *fileset)
1943 struct udf_sb_info *sbi = UDF_SB(sb);
1947 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1949 udf_warn(sb, "Bad block size\n");
1952 sbi->s_last_block = uopt->lastblock;
1954 /* Check that it is NSR02 compliant */
1955 nsr = udf_check_vsd(sb);
1958 udf_warn(sb, "No VRS found\n");
1962 udf_debug("Failed to read sector at offset %d. "
1963 "Assuming open disc. Skipping validity "
1964 "check\n", VSD_FIRST_SECTOR_OFFSET);
1965 if (!sbi->s_last_block)
1966 sbi->s_last_block = udf_get_last_block(sb);
1968 udf_debug("Validity check skipped because of novrs option\n");
1971 /* Look for anchor block and load Volume Descriptor Sequence */
1972 sbi->s_anchor = uopt->anchor;
1973 ret = udf_scan_anchors(sb, &sbi->s_last_block, fileset);
1975 if (!silent && ret == -EAGAIN)
1976 udf_warn(sb, "No anchor found\n");
1982 static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
1984 struct timespec64 ts;
1986 ktime_get_real_ts64(&ts);
1987 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
1988 lvid->descTag.descCRC = cpu_to_le16(
1989 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
1990 le16_to_cpu(lvid->descTag.descCRCLength)));
1991 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1994 static void udf_open_lvid(struct super_block *sb)
1996 struct udf_sb_info *sbi = UDF_SB(sb);
1997 struct buffer_head *bh = sbi->s_lvid_bh;
1998 struct logicalVolIntegrityDesc *lvid;
1999 struct logicalVolIntegrityDescImpUse *lvidiu;
2003 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2004 lvidiu = udf_sb_lvidiu(sb);
2008 mutex_lock(&sbi->s_alloc_mutex);
2009 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2010 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2011 if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
2012 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
2014 UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
2016 udf_finalize_lvid(lvid);
2017 mark_buffer_dirty(bh);
2018 sbi->s_lvid_dirty = 0;
2019 mutex_unlock(&sbi->s_alloc_mutex);
2020 /* Make opening of filesystem visible on the media immediately */
2021 sync_dirty_buffer(bh);
2024 static void udf_close_lvid(struct super_block *sb)
2026 struct udf_sb_info *sbi = UDF_SB(sb);
2027 struct buffer_head *bh = sbi->s_lvid_bh;
2028 struct logicalVolIntegrityDesc *lvid;
2029 struct logicalVolIntegrityDescImpUse *lvidiu;
2033 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2034 lvidiu = udf_sb_lvidiu(sb);
2038 mutex_lock(&sbi->s_alloc_mutex);
2039 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2040 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2041 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2042 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2043 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2044 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2045 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2046 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
2047 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
2048 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
2051 * We set buffer uptodate unconditionally here to avoid spurious
2052 * warnings from mark_buffer_dirty() when previous EIO has marked
2053 * the buffer as !uptodate
2055 set_buffer_uptodate(bh);
2056 udf_finalize_lvid(lvid);
2057 mark_buffer_dirty(bh);
2058 sbi->s_lvid_dirty = 0;
2059 mutex_unlock(&sbi->s_alloc_mutex);
2060 /* Make closing of filesystem visible on the media immediately */
2061 sync_dirty_buffer(bh);
2064 u64 lvid_get_unique_id(struct super_block *sb)
2066 struct buffer_head *bh;
2067 struct udf_sb_info *sbi = UDF_SB(sb);
2068 struct logicalVolIntegrityDesc *lvid;
2069 struct logicalVolHeaderDesc *lvhd;
2073 bh = sbi->s_lvid_bh;
2077 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2078 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2080 mutex_lock(&sbi->s_alloc_mutex);
2081 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2082 if (!(++uniqueID & 0xFFFFFFFF))
2084 lvhd->uniqueID = cpu_to_le64(uniqueID);
2085 udf_updated_lvid(sb);
2086 mutex_unlock(&sbi->s_alloc_mutex);
2091 static int udf_fill_super(struct super_block *sb, void *options, int silent)
2094 struct inode *inode = NULL;
2095 struct udf_options uopt;
2096 struct kernel_lb_addr rootdir, fileset;
2097 struct udf_sb_info *sbi;
2098 bool lvid_open = false;
2100 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
2101 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2102 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2103 uopt.gid = make_kgid(current_user_ns(), overflowgid);
2105 uopt.fmode = UDF_INVALID_MODE;
2106 uopt.dmode = UDF_INVALID_MODE;
2107 uopt.nls_map = NULL;
2109 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2113 sb->s_fs_info = sbi;
2115 mutex_init(&sbi->s_alloc_mutex);
2117 if (!udf_parse_options((char *)options, &uopt, false))
2118 goto parse_options_failure;
2120 fileset.logicalBlockNum = 0xFFFFFFFF;
2121 fileset.partitionReferenceNum = 0xFFFF;
2123 sbi->s_flags = uopt.flags;
2124 sbi->s_uid = uopt.uid;
2125 sbi->s_gid = uopt.gid;
2126 sbi->s_umask = uopt.umask;
2127 sbi->s_fmode = uopt.fmode;
2128 sbi->s_dmode = uopt.dmode;
2129 sbi->s_nls_map = uopt.nls_map;
2130 rwlock_init(&sbi->s_cred_lock);
2132 if (uopt.session == 0xFFFFFFFF)
2133 sbi->s_session = udf_get_last_session(sb);
2135 sbi->s_session = uopt.session;
2137 udf_debug("Multi-session=%d\n", sbi->s_session);
2139 /* Fill in the rest of the superblock */
2140 sb->s_op = &udf_sb_ops;
2141 sb->s_export_op = &udf_export_ops;
2143 sb->s_magic = UDF_SUPER_MAGIC;
2144 sb->s_time_gran = 1000;
2146 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2147 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2149 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
2150 while (uopt.blocksize <= 4096) {
2151 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2153 if (!silent && ret != -EACCES) {
2154 pr_notice("Scanning with blocksize %u failed\n",
2157 brelse(sbi->s_lvid_bh);
2158 sbi->s_lvid_bh = NULL;
2160 * EACCES is special - we want to propagate to
2161 * upper layers that we cannot handle RW mount.
2168 uopt.blocksize <<= 1;
2172 if (ret == -EAGAIN) {
2173 udf_warn(sb, "No partition found (1)\n");
2179 udf_debug("Lastblock=%u\n", sbi->s_last_block);
2181 if (sbi->s_lvid_bh) {
2182 struct logicalVolIntegrityDescImpUse *lvidiu =
2184 uint16_t minUDFReadRev;
2185 uint16_t minUDFWriteRev;
2191 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2192 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
2193 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
2194 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
2196 UDF_MAX_READ_VERSION);
2199 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
2200 if (!sb_rdonly(sb)) {
2204 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
2207 sbi->s_udfrev = minUDFWriteRev;
2209 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2210 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2211 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2212 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2215 if (!sbi->s_partitions) {
2216 udf_warn(sb, "No partition found (2)\n");
2221 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2222 UDF_PART_FLAG_READ_ONLY) {
2223 if (!sb_rdonly(sb)) {
2227 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
2230 ret = udf_find_fileset(sb, &fileset, &rootdir);
2232 udf_warn(sb, "No fileset found\n");
2237 struct timestamp ts;
2238 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
2239 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2240 sbi->s_volume_ident,
2241 le16_to_cpu(ts.year), ts.month, ts.day,
2242 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
2244 if (!sb_rdonly(sb)) {
2249 /* Assign the root inode */
2250 /* assign inodes by physical block number */
2251 /* perhaps it's not extensible enough, but for now ... */
2252 inode = udf_iget(sb, &rootdir);
2253 if (IS_ERR(inode)) {
2254 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
2255 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2256 ret = PTR_ERR(inode);
2260 /* Allocate a dentry for the root inode */
2261 sb->s_root = d_make_root(inode);
2263 udf_err(sb, "Couldn't allocate root dentry\n");
2267 sb->s_maxbytes = UDF_MAX_FILESIZE;
2268 sb->s_max_links = UDF_MAX_LINKS;
2272 iput(sbi->s_vat_inode);
2273 parse_options_failure:
2274 unload_nls(uopt.nls_map);
2277 brelse(sbi->s_lvid_bh);
2278 udf_sb_free_partitions(sb);
2280 sb->s_fs_info = NULL;
2285 void _udf_err(struct super_block *sb, const char *function,
2286 const char *fmt, ...)
2288 struct va_format vaf;
2291 va_start(args, fmt);
2296 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2301 void _udf_warn(struct super_block *sb, const char *function,
2302 const char *fmt, ...)
2304 struct va_format vaf;
2307 va_start(args, fmt);
2312 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2317 static void udf_put_super(struct super_block *sb)
2319 struct udf_sb_info *sbi;
2323 iput(sbi->s_vat_inode);
2324 unload_nls(sbi->s_nls_map);
2327 brelse(sbi->s_lvid_bh);
2328 udf_sb_free_partitions(sb);
2329 mutex_destroy(&sbi->s_alloc_mutex);
2330 kfree(sb->s_fs_info);
2331 sb->s_fs_info = NULL;
2334 static int udf_sync_fs(struct super_block *sb, int wait)
2336 struct udf_sb_info *sbi = UDF_SB(sb);
2338 mutex_lock(&sbi->s_alloc_mutex);
2339 if (sbi->s_lvid_dirty) {
2340 struct buffer_head *bh = sbi->s_lvid_bh;
2341 struct logicalVolIntegrityDesc *lvid;
2343 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2344 udf_finalize_lvid(lvid);
2347 * Blockdevice will be synced later so we don't have to submit
2350 mark_buffer_dirty(bh);
2351 sbi->s_lvid_dirty = 0;
2353 mutex_unlock(&sbi->s_alloc_mutex);
2358 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2360 struct super_block *sb = dentry->d_sb;
2361 struct udf_sb_info *sbi = UDF_SB(sb);
2362 struct logicalVolIntegrityDescImpUse *lvidiu;
2363 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2365 lvidiu = udf_sb_lvidiu(sb);
2366 buf->f_type = UDF_SUPER_MAGIC;
2367 buf->f_bsize = sb->s_blocksize;
2368 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2369 buf->f_bfree = udf_count_free(sb);
2370 buf->f_bavail = buf->f_bfree;
2372 * Let's pretend each free block is also a free 'inode' since UDF does
2373 * not have separate preallocated table of inodes.
2375 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2376 le32_to_cpu(lvidiu->numDirs)) : 0)
2378 buf->f_ffree = buf->f_bfree;
2379 buf->f_namelen = UDF_NAME_LEN;
2380 buf->f_fsid = u64_to_fsid(id);
2385 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2386 struct udf_bitmap *bitmap)
2388 struct buffer_head *bh = NULL;
2389 unsigned int accum = 0;
2391 udf_pblk_t block = 0, newblock;
2392 struct kernel_lb_addr loc;
2396 struct spaceBitmapDesc *bm;
2398 loc.logicalBlockNum = bitmap->s_extPosition;
2399 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2400 bh = udf_read_ptagged(sb, &loc, 0, &ident);
2403 udf_err(sb, "udf_count_free failed\n");
2405 } else if (ident != TAG_IDENT_SBD) {
2407 udf_err(sb, "udf_count_free failed\n");
2411 bm = (struct spaceBitmapDesc *)bh->b_data;
2412 bytes = le32_to_cpu(bm->numOfBytes);
2413 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2414 ptr = (uint8_t *)bh->b_data;
2417 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2418 accum += bitmap_weight((const unsigned long *)(ptr + index),
2423 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2424 bh = sb_bread(sb, newblock);
2426 udf_debug("read failed\n");
2430 ptr = (uint8_t *)bh->b_data;
2438 static unsigned int udf_count_free_table(struct super_block *sb,
2439 struct inode *table)
2441 unsigned int accum = 0;
2443 struct kernel_lb_addr eloc;
2444 struct extent_position epos;
2446 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
2447 epos.block = UDF_I(table)->i_location;
2448 epos.offset = sizeof(struct unallocSpaceEntry);
2451 while (udf_next_aext(table, &epos, &eloc, &elen, 1) != -1)
2452 accum += (elen >> table->i_sb->s_blocksize_bits);
2455 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
2460 static unsigned int udf_count_free(struct super_block *sb)
2462 unsigned int accum = 0;
2463 struct udf_sb_info *sbi = UDF_SB(sb);
2464 struct udf_part_map *map;
2465 unsigned int part = sbi->s_partition;
2466 int ptype = sbi->s_partmaps[part].s_partition_type;
2468 if (ptype == UDF_METADATA_MAP25) {
2469 part = sbi->s_partmaps[part].s_type_specific.s_metadata.
2470 s_phys_partition_ref;
2471 } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
2473 * Filesystems with VAT are append-only and we cannot write to
2474 * them. Let's just report 0 here.
2479 if (sbi->s_lvid_bh) {
2480 struct logicalVolIntegrityDesc *lvid =
2481 (struct logicalVolIntegrityDesc *)
2482 sbi->s_lvid_bh->b_data;
2483 if (le32_to_cpu(lvid->numOfPartitions) > part) {
2484 accum = le32_to_cpu(
2485 lvid->freeSpaceTable[part]);
2486 if (accum == 0xFFFFFFFF)
2494 map = &sbi->s_partmaps[part];
2495 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2496 accum += udf_count_free_bitmap(sb,
2497 map->s_uspace.s_bitmap);
2502 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2503 accum += udf_count_free_table(sb,
2504 map->s_uspace.s_table);
2509 MODULE_AUTHOR("Ben Fennema");
2510 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2511 MODULE_LICENSE("GPL");
2512 module_init(init_udf_fs)
2513 module_exit(exit_udf_fs)