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/
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>
64 #include <linux/init.h>
65 #include <linux/uaccess.h>
68 VDS_POS_PRIMARY_VOL_DESC
,
69 VDS_POS_UNALLOC_SPACE_DESC
,
70 VDS_POS_LOGICAL_VOL_DESC
,
71 VDS_POS_IMP_USE_VOL_DESC
,
75 #define VSD_FIRST_SECTOR_OFFSET 32768
76 #define VSD_MAX_SECTOR_OFFSET 0x800000
79 * Maximum number of Terminating Descriptor / Logical Volume Integrity
80 * Descriptor redirections. The chosen numbers are arbitrary - just that we
81 * hopefully don't limit any real use of rewritten inode on write-once media
82 * but avoid looping for too long on corrupted media.
84 #define UDF_MAX_TD_NESTING 64
85 #define UDF_MAX_LVID_NESTING 1000
87 enum { UDF_MAX_LINKS
= 0xffff };
89 /* These are the "meat" - everything else is stuffing */
90 static int udf_fill_super(struct super_block
*, void *, int);
91 static void udf_put_super(struct super_block
*);
92 static int udf_sync_fs(struct super_block
*, int);
93 static int udf_remount_fs(struct super_block
*, int *, char *);
94 static void udf_load_logicalvolint(struct super_block
*, struct kernel_extent_ad
);
95 static void udf_open_lvid(struct super_block
*);
96 static void udf_close_lvid(struct super_block
*);
97 static unsigned int udf_count_free(struct super_block
*);
98 static int udf_statfs(struct dentry
*, struct kstatfs
*);
99 static int udf_show_options(struct seq_file
*, struct dentry
*);
101 struct logicalVolIntegrityDescImpUse
*udf_sb_lvidiu(struct super_block
*sb
)
103 struct logicalVolIntegrityDesc
*lvid
;
104 unsigned int partnum
;
107 if (!UDF_SB(sb
)->s_lvid_bh
)
109 lvid
= (struct logicalVolIntegrityDesc
*)UDF_SB(sb
)->s_lvid_bh
->b_data
;
110 partnum
= le32_to_cpu(lvid
->numOfPartitions
);
111 if ((sb
->s_blocksize
- sizeof(struct logicalVolIntegrityDescImpUse
) -
112 offsetof(struct logicalVolIntegrityDesc
, impUse
)) /
113 (2 * sizeof(uint32_t)) < partnum
) {
114 udf_err(sb
, "Logical volume integrity descriptor corrupted "
115 "(numOfPartitions = %u)!\n", partnum
);
118 /* The offset is to skip freeSpaceTable and sizeTable arrays */
119 offset
= partnum
* 2 * sizeof(uint32_t);
120 return (struct logicalVolIntegrityDescImpUse
*)&(lvid
->impUse
[offset
]);
123 /* UDF filesystem type */
124 static struct dentry
*udf_mount(struct file_system_type
*fs_type
,
125 int flags
, const char *dev_name
, void *data
)
127 return mount_bdev(fs_type
, flags
, dev_name
, data
, udf_fill_super
);
130 static struct file_system_type udf_fstype
= {
131 .owner
= THIS_MODULE
,
134 .kill_sb
= kill_block_super
,
135 .fs_flags
= FS_REQUIRES_DEV
,
137 MODULE_ALIAS_FS("udf");
139 static struct kmem_cache
*udf_inode_cachep
;
141 static struct inode
*udf_alloc_inode(struct super_block
*sb
)
143 struct udf_inode_info
*ei
;
144 ei
= kmem_cache_alloc(udf_inode_cachep
, GFP_KERNEL
);
149 ei
->i_lenExtents
= 0;
150 ei
->i_lenStreams
= 0;
151 ei
->i_next_alloc_block
= 0;
152 ei
->i_next_alloc_goal
= 0;
155 init_rwsem(&ei
->i_data_sem
);
156 ei
->cached_extent
.lstart
= -1;
157 spin_lock_init(&ei
->i_extent_cache_lock
);
159 return &ei
->vfs_inode
;
162 static void udf_free_in_core_inode(struct inode
*inode
)
164 kmem_cache_free(udf_inode_cachep
, UDF_I(inode
));
167 static void init_once(void *foo
)
169 struct udf_inode_info
*ei
= (struct udf_inode_info
*)foo
;
171 ei
->i_ext
.i_data
= NULL
;
172 inode_init_once(&ei
->vfs_inode
);
175 static int __init
init_inodecache(void)
177 udf_inode_cachep
= kmem_cache_create("udf_inode_cache",
178 sizeof(struct udf_inode_info
),
179 0, (SLAB_RECLAIM_ACCOUNT
|
183 if (!udf_inode_cachep
)
188 static void destroy_inodecache(void)
191 * Make sure all delayed rcu free inodes are flushed before we
195 kmem_cache_destroy(udf_inode_cachep
);
198 /* Superblock operations */
199 static const struct super_operations udf_sb_ops
= {
200 .alloc_inode
= udf_alloc_inode
,
201 .free_inode
= udf_free_in_core_inode
,
202 .write_inode
= udf_write_inode
,
203 .evict_inode
= udf_evict_inode
,
204 .put_super
= udf_put_super
,
205 .sync_fs
= udf_sync_fs
,
206 .statfs
= udf_statfs
,
207 .remount_fs
= udf_remount_fs
,
208 .show_options
= udf_show_options
,
213 unsigned int blocksize
;
214 unsigned int session
;
215 unsigned int lastblock
;
223 struct nls_table
*nls_map
;
226 static int __init
init_udf_fs(void)
230 err
= init_inodecache();
233 err
= register_filesystem(&udf_fstype
);
240 destroy_inodecache();
246 static void __exit
exit_udf_fs(void)
248 unregister_filesystem(&udf_fstype
);
249 destroy_inodecache();
252 static int udf_sb_alloc_partition_maps(struct super_block
*sb
, u32 count
)
254 struct udf_sb_info
*sbi
= UDF_SB(sb
);
256 sbi
->s_partmaps
= kcalloc(count
, sizeof(*sbi
->s_partmaps
), GFP_KERNEL
);
257 if (!sbi
->s_partmaps
) {
258 sbi
->s_partitions
= 0;
262 sbi
->s_partitions
= count
;
266 static void udf_sb_free_bitmap(struct udf_bitmap
*bitmap
)
269 int nr_groups
= bitmap
->s_nr_groups
;
271 for (i
= 0; i
< nr_groups
; i
++)
272 brelse(bitmap
->s_block_bitmap
[i
]);
277 static void udf_free_partition(struct udf_part_map
*map
)
280 struct udf_meta_data
*mdata
;
282 if (map
->s_partition_flags
& UDF_PART_FLAG_UNALLOC_TABLE
)
283 iput(map
->s_uspace
.s_table
);
284 if (map
->s_partition_flags
& UDF_PART_FLAG_UNALLOC_BITMAP
)
285 udf_sb_free_bitmap(map
->s_uspace
.s_bitmap
);
286 if (map
->s_partition_type
== UDF_SPARABLE_MAP15
)
287 for (i
= 0; i
< 4; i
++)
288 brelse(map
->s_type_specific
.s_sparing
.s_spar_map
[i
]);
289 else if (map
->s_partition_type
== UDF_METADATA_MAP25
) {
290 mdata
= &map
->s_type_specific
.s_metadata
;
291 iput(mdata
->s_metadata_fe
);
292 mdata
->s_metadata_fe
= NULL
;
294 iput(mdata
->s_mirror_fe
);
295 mdata
->s_mirror_fe
= NULL
;
297 iput(mdata
->s_bitmap_fe
);
298 mdata
->s_bitmap_fe
= NULL
;
302 static void udf_sb_free_partitions(struct super_block
*sb
)
304 struct udf_sb_info
*sbi
= UDF_SB(sb
);
307 if (!sbi
->s_partmaps
)
309 for (i
= 0; i
< sbi
->s_partitions
; i
++)
310 udf_free_partition(&sbi
->s_partmaps
[i
]);
311 kfree(sbi
->s_partmaps
);
312 sbi
->s_partmaps
= NULL
;
315 static int udf_show_options(struct seq_file
*seq
, struct dentry
*root
)
317 struct super_block
*sb
= root
->d_sb
;
318 struct udf_sb_info
*sbi
= UDF_SB(sb
);
320 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
))
321 seq_puts(seq
, ",nostrict");
322 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_BLOCKSIZE_SET
))
323 seq_printf(seq
, ",bs=%lu", sb
->s_blocksize
);
324 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UNHIDE
))
325 seq_puts(seq
, ",unhide");
326 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UNDELETE
))
327 seq_puts(seq
, ",undelete");
328 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_USE_AD_IN_ICB
))
329 seq_puts(seq
, ",noadinicb");
330 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_USE_SHORT_AD
))
331 seq_puts(seq
, ",shortad");
332 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UID_FORGET
))
333 seq_puts(seq
, ",uid=forget");
334 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_GID_FORGET
))
335 seq_puts(seq
, ",gid=forget");
336 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UID_SET
))
337 seq_printf(seq
, ",uid=%u", from_kuid(&init_user_ns
, sbi
->s_uid
));
338 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_GID_SET
))
339 seq_printf(seq
, ",gid=%u", from_kgid(&init_user_ns
, sbi
->s_gid
));
340 if (sbi
->s_umask
!= 0)
341 seq_printf(seq
, ",umask=%ho", sbi
->s_umask
);
342 if (sbi
->s_fmode
!= UDF_INVALID_MODE
)
343 seq_printf(seq
, ",mode=%ho", sbi
->s_fmode
);
344 if (sbi
->s_dmode
!= UDF_INVALID_MODE
)
345 seq_printf(seq
, ",dmode=%ho", sbi
->s_dmode
);
346 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_SESSION_SET
))
347 seq_printf(seq
, ",session=%d", sbi
->s_session
);
348 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_LASTBLOCK_SET
))
349 seq_printf(seq
, ",lastblock=%u", sbi
->s_last_block
);
350 if (sbi
->s_anchor
!= 0)
351 seq_printf(seq
, ",anchor=%u", sbi
->s_anchor
);
352 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_UTF8
))
353 seq_puts(seq
, ",utf8");
354 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
) && sbi
->s_nls_map
)
355 seq_printf(seq
, ",iocharset=%s", sbi
->s_nls_map
->charset
);
364 * Parse mount options.
367 * The following mount options are supported:
369 * gid= Set the default group.
370 * umask= Set the default umask.
371 * mode= Set the default file permissions.
372 * dmode= Set the default directory permissions.
373 * uid= Set the default user.
374 * bs= Set the block size.
375 * unhide Show otherwise hidden files.
376 * undelete Show deleted files in lists.
377 * adinicb Embed data in the inode (default)
378 * noadinicb Don't embed data in the inode
379 * shortad Use short ad's
380 * longad Use long ad's (default)
381 * nostrict Unset strict conformance
382 * iocharset= Set the NLS character set
384 * The remaining are for debugging and disaster recovery:
386 * novrs Skip volume sequence recognition
388 * The following expect a offset from 0.
390 * session= Set the CDROM session (default= last session)
391 * anchor= Override standard anchor location. (default= 256)
392 * volume= Override the VolumeDesc location. (unused)
393 * partition= Override the PartitionDesc location. (unused)
394 * lastblock= Set the last block of the filesystem/
396 * The following expect a offset from the partition root.
398 * fileset= Override the fileset block location. (unused)
399 * rootdir= Override the root directory location. (unused)
400 * WARNING: overriding the rootdir to a non-directory may
401 * yield highly unpredictable results.
404 * options Pointer to mount options string.
405 * uopts Pointer to mount options variable.
408 * <return> 1 Mount options parsed okay.
409 * <return> 0 Error parsing mount options.
412 * July 1, 1997 - Andrew E. Mileski
413 * Written, tested, and released.
417 Opt_novrs
, Opt_nostrict
, Opt_bs
, Opt_unhide
, Opt_undelete
,
418 Opt_noadinicb
, Opt_adinicb
, Opt_shortad
, Opt_longad
,
419 Opt_gid
, Opt_uid
, Opt_umask
, Opt_session
, Opt_lastblock
,
420 Opt_anchor
, Opt_volume
, Opt_partition
, Opt_fileset
,
421 Opt_rootdir
, Opt_utf8
, Opt_iocharset
,
422 Opt_err
, Opt_uforget
, Opt_uignore
, Opt_gforget
, Opt_gignore
,
426 static const match_table_t tokens
= {
427 {Opt_novrs
, "novrs"},
428 {Opt_nostrict
, "nostrict"},
430 {Opt_unhide
, "unhide"},
431 {Opt_undelete
, "undelete"},
432 {Opt_noadinicb
, "noadinicb"},
433 {Opt_adinicb
, "adinicb"},
434 {Opt_shortad
, "shortad"},
435 {Opt_longad
, "longad"},
436 {Opt_uforget
, "uid=forget"},
437 {Opt_uignore
, "uid=ignore"},
438 {Opt_gforget
, "gid=forget"},
439 {Opt_gignore
, "gid=ignore"},
442 {Opt_umask
, "umask=%o"},
443 {Opt_session
, "session=%u"},
444 {Opt_lastblock
, "lastblock=%u"},
445 {Opt_anchor
, "anchor=%u"},
446 {Opt_volume
, "volume=%u"},
447 {Opt_partition
, "partition=%u"},
448 {Opt_fileset
, "fileset=%u"},
449 {Opt_rootdir
, "rootdir=%u"},
451 {Opt_iocharset
, "iocharset=%s"},
452 {Opt_fmode
, "mode=%o"},
453 {Opt_dmode
, "dmode=%o"},
457 static int udf_parse_options(char *options
, struct udf_options
*uopt
,
464 uopt
->session
= 0xFFFFFFFF;
471 while ((p
= strsep(&options
, ",")) != NULL
) {
472 substring_t args
[MAX_OPT_ARGS
];
478 token
= match_token(p
, tokens
, args
);
484 if (match_int(&args
[0], &option
))
487 if (n
!= 512 && n
!= 1024 && n
!= 2048 && n
!= 4096)
490 uopt
->flags
|= (1 << UDF_FLAG_BLOCKSIZE_SET
);
493 uopt
->flags
|= (1 << UDF_FLAG_UNHIDE
);
496 uopt
->flags
|= (1 << UDF_FLAG_UNDELETE
);
499 uopt
->flags
&= ~(1 << UDF_FLAG_USE_AD_IN_ICB
);
502 uopt
->flags
|= (1 << UDF_FLAG_USE_AD_IN_ICB
);
505 uopt
->flags
|= (1 << UDF_FLAG_USE_SHORT_AD
);
508 uopt
->flags
&= ~(1 << UDF_FLAG_USE_SHORT_AD
);
511 if (match_int(args
, &option
))
513 uopt
->gid
= make_kgid(current_user_ns(), option
);
514 if (!gid_valid(uopt
->gid
))
516 uopt
->flags
|= (1 << UDF_FLAG_GID_SET
);
519 if (match_int(args
, &option
))
521 uopt
->uid
= make_kuid(current_user_ns(), option
);
522 if (!uid_valid(uopt
->uid
))
524 uopt
->flags
|= (1 << UDF_FLAG_UID_SET
);
527 if (match_octal(args
, &option
))
529 uopt
->umask
= option
;
532 uopt
->flags
&= ~(1 << UDF_FLAG_STRICT
);
535 if (match_int(args
, &option
))
537 uopt
->session
= option
;
539 uopt
->flags
|= (1 << UDF_FLAG_SESSION_SET
);
542 if (match_int(args
, &option
))
544 uopt
->lastblock
= option
;
546 uopt
->flags
|= (1 << UDF_FLAG_LASTBLOCK_SET
);
549 if (match_int(args
, &option
))
551 uopt
->anchor
= option
;
557 /* Ignored (never implemented properly) */
560 uopt
->flags
|= (1 << UDF_FLAG_UTF8
);
565 unload_nls(uopt
->nls_map
);
567 * load_nls() failure is handled later in
568 * udf_fill_super() after all options are
571 uopt
->nls_map
= load_nls(args
[0].from
);
572 uopt
->flags
|= (1 << UDF_FLAG_NLS_MAP
);
576 uopt
->flags
|= (1 << UDF_FLAG_UID_FORGET
);
580 /* These options are superseeded by uid=<number> */
583 uopt
->flags
|= (1 << UDF_FLAG_GID_FORGET
);
586 if (match_octal(args
, &option
))
588 uopt
->fmode
= option
& 0777;
591 if (match_octal(args
, &option
))
593 uopt
->dmode
= option
& 0777;
596 pr_err("bad mount option \"%s\" or missing value\n", p
);
603 static int udf_remount_fs(struct super_block
*sb
, int *flags
, char *options
)
605 struct udf_options uopt
;
606 struct udf_sb_info
*sbi
= UDF_SB(sb
);
609 if (!(*flags
& SB_RDONLY
) && UDF_QUERY_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
))
614 uopt
.flags
= sbi
->s_flags
;
615 uopt
.uid
= sbi
->s_uid
;
616 uopt
.gid
= sbi
->s_gid
;
617 uopt
.umask
= sbi
->s_umask
;
618 uopt
.fmode
= sbi
->s_fmode
;
619 uopt
.dmode
= sbi
->s_dmode
;
622 if (!udf_parse_options(options
, &uopt
, true))
625 write_lock(&sbi
->s_cred_lock
);
626 sbi
->s_flags
= uopt
.flags
;
627 sbi
->s_uid
= uopt
.uid
;
628 sbi
->s_gid
= uopt
.gid
;
629 sbi
->s_umask
= uopt
.umask
;
630 sbi
->s_fmode
= uopt
.fmode
;
631 sbi
->s_dmode
= uopt
.dmode
;
632 write_unlock(&sbi
->s_cred_lock
);
634 if ((bool)(*flags
& SB_RDONLY
) == sb_rdonly(sb
))
637 if (*flags
& SB_RDONLY
)
647 * Check VSD descriptor. Returns -1 in case we are at the end of volume
648 * recognition area, 0 if the descriptor is valid but non-interesting, 1 if
649 * we found one of NSR descriptors we are looking for.
651 static int identify_vsd(const struct volStructDesc
*vsd
)
655 if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_CD001
, VSD_STD_ID_LEN
)) {
656 switch (vsd
->structType
) {
658 udf_debug("ISO9660 Boot Record found\n");
661 udf_debug("ISO9660 Primary Volume Descriptor found\n");
664 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
667 udf_debug("ISO9660 Volume Partition Descriptor found\n");
670 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
673 udf_debug("ISO9660 VRS (%u) found\n", vsd
->structType
);
676 } else if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_BEA01
, VSD_STD_ID_LEN
))
678 else if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_NSR02
, VSD_STD_ID_LEN
))
680 else if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_NSR03
, VSD_STD_ID_LEN
))
682 else if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_BOOT2
, VSD_STD_ID_LEN
))
684 else if (!memcmp(vsd
->stdIdent
, VSD_STD_ID_CDW02
, VSD_STD_ID_LEN
))
687 /* TEA01 or invalid id : end of volume recognition area */
695 * Check Volume Structure Descriptors (ECMA 167 2/9.1)
696 * We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1)
697 * @return 1 if NSR02 or NSR03 found,
698 * -1 if first sector read error, 0 otherwise
700 static int udf_check_vsd(struct super_block
*sb
)
702 struct volStructDesc
*vsd
= NULL
;
703 loff_t sector
= VSD_FIRST_SECTOR_OFFSET
;
705 struct buffer_head
*bh
= NULL
;
707 struct udf_sb_info
*sbi
;
710 if (sb
->s_blocksize
< sizeof(struct volStructDesc
))
711 sectorsize
= sizeof(struct volStructDesc
);
713 sectorsize
= sb
->s_blocksize
;
715 sector
+= (((loff_t
)sbi
->s_session
) << sb
->s_blocksize_bits
);
717 udf_debug("Starting at sector %u (%lu byte sectors)\n",
718 (unsigned int)(sector
>> sb
->s_blocksize_bits
),
720 /* Process the sequence (if applicable). The hard limit on the sector
721 * offset is arbitrary, hopefully large enough so that all valid UDF
722 * filesystems will be recognised. There is no mention of an upper
723 * bound to the size of the volume recognition area in the standard.
724 * The limit will prevent the code to read all the sectors of a
725 * specially crafted image (like a bluray disc full of CD001 sectors),
726 * potentially causing minutes or even hours of uninterruptible I/O
727 * activity. This actually happened with uninitialised SSD partitions
728 * (all 0xFF) before the check for the limit and all valid IDs were
730 for (; !nsr
&& sector
< VSD_MAX_SECTOR_OFFSET
; sector
+= sectorsize
) {
732 bh
= udf_tread(sb
, sector
>> sb
->s_blocksize_bits
);
736 vsd
= (struct volStructDesc
*)(bh
->b_data
+
737 (sector
& (sb
->s_blocksize
- 1)));
738 nsr
= identify_vsd(vsd
);
739 /* Found NSR or end? */
745 * Special handling for improperly formatted VRS (e.g., Win10)
746 * where components are separated by 2048 bytes even though
749 if (sb
->s_blocksize
== 4096) {
750 nsr
= identify_vsd(vsd
+ 1);
751 /* Ignore unknown IDs... */
760 else if (!bh
&& sector
- (sbi
->s_session
<< sb
->s_blocksize_bits
) ==
761 VSD_FIRST_SECTOR_OFFSET
)
767 static int udf_verify_domain_identifier(struct super_block
*sb
,
768 struct regid
*ident
, char *dname
)
770 struct domainEntityIDSuffix
*suffix
;
772 if (memcmp(ident
->ident
, UDF_ID_COMPLIANT
, strlen(UDF_ID_COMPLIANT
))) {
773 udf_warn(sb
, "Not OSTA UDF compliant %s descriptor.\n", dname
);
776 if (ident
->flags
& (1 << ENTITYID_FLAGS_DIRTY
)) {
777 udf_warn(sb
, "Possibly not OSTA UDF compliant %s descriptor.\n",
781 suffix
= (struct domainEntityIDSuffix
*)ident
->identSuffix
;
782 if (suffix
->flags
& (1 << ENTITYIDSUFFIX_FLAGS_HARDWRITEPROTECT
) ||
783 suffix
->flags
& (1 << ENTITYIDSUFFIX_FLAGS_SOFTWRITEPROTECT
)) {
784 if (!sb_rdonly(sb
)) {
785 udf_warn(sb
, "Descriptor for %s marked write protected."
786 " Forcing read only mount.\n", dname
);
795 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
799 static int udf_load_fileset(struct super_block
*sb
, struct fileSetDesc
*fset
,
800 struct kernel_lb_addr
*root
)
804 ret
= udf_verify_domain_identifier(sb
, &fset
->domainIdent
, "file set");
808 *root
= lelb_to_cpu(fset
->rootDirectoryICB
.extLocation
);
809 UDF_SB(sb
)->s_serial_number
= le16_to_cpu(fset
->descTag
.tagSerialNum
);
811 udf_debug("Rootdir at block=%u, partition=%u\n",
812 root
->logicalBlockNum
, root
->partitionReferenceNum
);
816 static int udf_find_fileset(struct super_block
*sb
,
817 struct kernel_lb_addr
*fileset
,
818 struct kernel_lb_addr
*root
)
820 struct buffer_head
*bh
= NULL
;
824 if (fileset
->logicalBlockNum
== 0xFFFFFFFF &&
825 fileset
->partitionReferenceNum
== 0xFFFF)
828 bh
= udf_read_ptagged(sb
, fileset
, 0, &ident
);
831 if (ident
!= TAG_IDENT_FSD
) {
836 udf_debug("Fileset at block=%u, partition=%u\n",
837 fileset
->logicalBlockNum
, fileset
->partitionReferenceNum
);
839 UDF_SB(sb
)->s_partition
= fileset
->partitionReferenceNum
;
840 ret
= udf_load_fileset(sb
, (struct fileSetDesc
*)bh
->b_data
, root
);
846 * Load primary Volume Descriptor Sequence
848 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
851 static int udf_load_pvoldesc(struct super_block
*sb
, sector_t block
)
853 struct primaryVolDesc
*pvoldesc
;
855 struct buffer_head
*bh
;
858 struct timestamp
*ts
;
860 outstr
= kmalloc(128, GFP_NOFS
);
864 bh
= udf_read_tagged(sb
, block
, block
, &ident
);
870 if (ident
!= TAG_IDENT_PVD
) {
875 pvoldesc
= (struct primaryVolDesc
*)bh
->b_data
;
877 udf_disk_stamp_to_time(&UDF_SB(sb
)->s_record_time
,
878 pvoldesc
->recordingDateAndTime
);
879 ts
= &pvoldesc
->recordingDateAndTime
;
880 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
881 le16_to_cpu(ts
->year
), ts
->month
, ts
->day
, ts
->hour
,
882 ts
->minute
, le16_to_cpu(ts
->typeAndTimezone
));
884 ret
= udf_dstrCS0toChar(sb
, outstr
, 31, pvoldesc
->volIdent
, 32);
886 strcpy(UDF_SB(sb
)->s_volume_ident
, "InvalidName");
887 pr_warn("incorrect volume identification, setting to "
890 strncpy(UDF_SB(sb
)->s_volume_ident
, outstr
, ret
);
892 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb
)->s_volume_ident
);
894 ret
= udf_dstrCS0toChar(sb
, outstr
, 127, pvoldesc
->volSetIdent
, 128);
900 udf_debug("volSetIdent[] = '%s'\n", outstr
);
910 struct inode
*udf_find_metadata_inode_efe(struct super_block
*sb
,
911 u32 meta_file_loc
, u32 partition_ref
)
913 struct kernel_lb_addr addr
;
914 struct inode
*metadata_fe
;
916 addr
.logicalBlockNum
= meta_file_loc
;
917 addr
.partitionReferenceNum
= partition_ref
;
919 metadata_fe
= udf_iget_special(sb
, &addr
);
921 if (IS_ERR(metadata_fe
)) {
922 udf_warn(sb
, "metadata inode efe not found\n");
925 if (UDF_I(metadata_fe
)->i_alloc_type
!= ICBTAG_FLAG_AD_SHORT
) {
926 udf_warn(sb
, "metadata inode efe does not have short allocation descriptors!\n");
928 return ERR_PTR(-EIO
);
934 static int udf_load_metadata_files(struct super_block
*sb
, int partition
,
937 struct udf_sb_info
*sbi
= UDF_SB(sb
);
938 struct udf_part_map
*map
;
939 struct udf_meta_data
*mdata
;
940 struct kernel_lb_addr addr
;
943 map
= &sbi
->s_partmaps
[partition
];
944 mdata
= &map
->s_type_specific
.s_metadata
;
945 mdata
->s_phys_partition_ref
= type1_index
;
947 /* metadata address */
948 udf_debug("Metadata file location: block = %u part = %u\n",
949 mdata
->s_meta_file_loc
, mdata
->s_phys_partition_ref
);
951 fe
= udf_find_metadata_inode_efe(sb
, mdata
->s_meta_file_loc
,
952 mdata
->s_phys_partition_ref
);
954 /* mirror file entry */
955 udf_debug("Mirror metadata file location: block = %u part = %u\n",
956 mdata
->s_mirror_file_loc
, mdata
->s_phys_partition_ref
);
958 fe
= udf_find_metadata_inode_efe(sb
, mdata
->s_mirror_file_loc
,
959 mdata
->s_phys_partition_ref
);
962 udf_err(sb
, "Both metadata and mirror metadata inode efe can not found\n");
965 mdata
->s_mirror_fe
= fe
;
967 mdata
->s_metadata_fe
= fe
;
973 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
975 if (mdata
->s_bitmap_file_loc
!= 0xFFFFFFFF) {
976 addr
.logicalBlockNum
= mdata
->s_bitmap_file_loc
;
977 addr
.partitionReferenceNum
= mdata
->s_phys_partition_ref
;
979 udf_debug("Bitmap file location: block = %u part = %u\n",
980 addr
.logicalBlockNum
, addr
.partitionReferenceNum
);
982 fe
= udf_iget_special(sb
, &addr
);
985 udf_warn(sb
, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
987 udf_err(sb
, "bitmap inode efe not found and attempted read-write mount\n");
991 mdata
->s_bitmap_fe
= fe
;
994 udf_debug("udf_load_metadata_files Ok\n");
998 int udf_compute_nr_groups(struct super_block
*sb
, u32 partition
)
1000 struct udf_part_map
*map
= &UDF_SB(sb
)->s_partmaps
[partition
];
1001 return DIV_ROUND_UP(map
->s_partition_len
+
1002 (sizeof(struct spaceBitmapDesc
) << 3),
1003 sb
->s_blocksize
* 8);
1006 static struct udf_bitmap
*udf_sb_alloc_bitmap(struct super_block
*sb
, u32 index
)
1008 struct udf_bitmap
*bitmap
;
1012 nr_groups
= udf_compute_nr_groups(sb
, index
);
1013 size
= sizeof(struct udf_bitmap
) +
1014 (sizeof(struct buffer_head
*) * nr_groups
);
1016 if (size
<= PAGE_SIZE
)
1017 bitmap
= kzalloc(size
, GFP_KERNEL
);
1019 bitmap
= vzalloc(size
); /* TODO: get rid of vzalloc */
1024 bitmap
->s_nr_groups
= nr_groups
;
1028 static int check_partition_desc(struct super_block
*sb
,
1029 struct partitionDesc
*p
,
1030 struct udf_part_map
*map
)
1032 bool umap
, utable
, fmap
, ftable
;
1033 struct partitionHeaderDesc
*phd
;
1035 switch (le32_to_cpu(p
->accessType
)) {
1036 case PD_ACCESS_TYPE_READ_ONLY
:
1037 case PD_ACCESS_TYPE_WRITE_ONCE
:
1038 case PD_ACCESS_TYPE_NONE
:
1042 /* No Partition Header Descriptor? */
1043 if (strcmp(p
->partitionContents
.ident
, PD_PARTITION_CONTENTS_NSR02
) &&
1044 strcmp(p
->partitionContents
.ident
, PD_PARTITION_CONTENTS_NSR03
))
1047 phd
= (struct partitionHeaderDesc
*)p
->partitionContentsUse
;
1048 utable
= phd
->unallocSpaceTable
.extLength
;
1049 umap
= phd
->unallocSpaceBitmap
.extLength
;
1050 ftable
= phd
->freedSpaceTable
.extLength
;
1051 fmap
= phd
->freedSpaceBitmap
.extLength
;
1053 /* No allocation info? */
1054 if (!utable
&& !umap
&& !ftable
&& !fmap
)
1057 /* We don't support blocks that require erasing before overwrite */
1060 /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
1064 if (map
->s_partition_type
== UDF_VIRTUAL_MAP15
||
1065 map
->s_partition_type
== UDF_VIRTUAL_MAP20
)
1072 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
1076 static int udf_fill_partdesc_info(struct super_block
*sb
,
1077 struct partitionDesc
*p
, int p_index
)
1079 struct udf_part_map
*map
;
1080 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1081 struct partitionHeaderDesc
*phd
;
1084 map
= &sbi
->s_partmaps
[p_index
];
1086 map
->s_partition_len
= le32_to_cpu(p
->partitionLength
); /* blocks */
1087 map
->s_partition_root
= le32_to_cpu(p
->partitionStartingLocation
);
1089 if (p
->accessType
== cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY
))
1090 map
->s_partition_flags
|= UDF_PART_FLAG_READ_ONLY
;
1091 if (p
->accessType
== cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE
))
1092 map
->s_partition_flags
|= UDF_PART_FLAG_WRITE_ONCE
;
1093 if (p
->accessType
== cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE
))
1094 map
->s_partition_flags
|= UDF_PART_FLAG_REWRITABLE
;
1095 if (p
->accessType
== cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE
))
1096 map
->s_partition_flags
|= UDF_PART_FLAG_OVERWRITABLE
;
1098 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
1099 p_index
, map
->s_partition_type
,
1100 map
->s_partition_root
, map
->s_partition_len
);
1102 err
= check_partition_desc(sb
, p
, map
);
1107 * Skip loading allocation info it we cannot ever write to the fs.
1108 * This is a correctness thing as we may have decided to force ro mount
1109 * to avoid allocation info we don't support.
1111 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
))
1114 phd
= (struct partitionHeaderDesc
*)p
->partitionContentsUse
;
1115 if (phd
->unallocSpaceTable
.extLength
) {
1116 struct kernel_lb_addr loc
= {
1117 .logicalBlockNum
= le32_to_cpu(
1118 phd
->unallocSpaceTable
.extPosition
),
1119 .partitionReferenceNum
= p_index
,
1121 struct inode
*inode
;
1123 inode
= udf_iget_special(sb
, &loc
);
1124 if (IS_ERR(inode
)) {
1125 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1127 return PTR_ERR(inode
);
1129 map
->s_uspace
.s_table
= inode
;
1130 map
->s_partition_flags
|= UDF_PART_FLAG_UNALLOC_TABLE
;
1131 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
1132 p_index
, map
->s_uspace
.s_table
->i_ino
);
1135 if (phd
->unallocSpaceBitmap
.extLength
) {
1136 struct udf_bitmap
*bitmap
= udf_sb_alloc_bitmap(sb
, p_index
);
1139 map
->s_uspace
.s_bitmap
= bitmap
;
1140 bitmap
->s_extPosition
= le32_to_cpu(
1141 phd
->unallocSpaceBitmap
.extPosition
);
1142 map
->s_partition_flags
|= UDF_PART_FLAG_UNALLOC_BITMAP
;
1143 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
1144 p_index
, bitmap
->s_extPosition
);
1150 static void udf_find_vat_block(struct super_block
*sb
, int p_index
,
1151 int type1_index
, sector_t start_block
)
1153 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1154 struct udf_part_map
*map
= &sbi
->s_partmaps
[p_index
];
1156 struct kernel_lb_addr ino
;
1157 struct inode
*inode
;
1160 * VAT file entry is in the last recorded block. Some broken disks have
1161 * it a few blocks before so try a bit harder...
1163 ino
.partitionReferenceNum
= type1_index
;
1164 for (vat_block
= start_block
;
1165 vat_block
>= map
->s_partition_root
&&
1166 vat_block
>= start_block
- 3; vat_block
--) {
1167 ino
.logicalBlockNum
= vat_block
- map
->s_partition_root
;
1168 inode
= udf_iget_special(sb
, &ino
);
1169 if (!IS_ERR(inode
)) {
1170 sbi
->s_vat_inode
= inode
;
1176 static int udf_load_vat(struct super_block
*sb
, int p_index
, int type1_index
)
1178 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1179 struct udf_part_map
*map
= &sbi
->s_partmaps
[p_index
];
1180 struct buffer_head
*bh
= NULL
;
1181 struct udf_inode_info
*vati
;
1183 struct virtualAllocationTable20
*vat20
;
1184 sector_t blocks
= i_size_read(sb
->s_bdev
->bd_inode
) >>
1185 sb
->s_blocksize_bits
;
1187 udf_find_vat_block(sb
, p_index
, type1_index
, sbi
->s_last_block
);
1188 if (!sbi
->s_vat_inode
&&
1189 sbi
->s_last_block
!= blocks
- 1) {
1190 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1191 (unsigned long)sbi
->s_last_block
,
1192 (unsigned long)blocks
- 1);
1193 udf_find_vat_block(sb
, p_index
, type1_index
, blocks
- 1);
1195 if (!sbi
->s_vat_inode
)
1198 if (map
->s_partition_type
== UDF_VIRTUAL_MAP15
) {
1199 map
->s_type_specific
.s_virtual
.s_start_offset
= 0;
1200 map
->s_type_specific
.s_virtual
.s_num_entries
=
1201 (sbi
->s_vat_inode
->i_size
- 36) >> 2;
1202 } else if (map
->s_partition_type
== UDF_VIRTUAL_MAP20
) {
1203 vati
= UDF_I(sbi
->s_vat_inode
);
1204 if (vati
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
1205 pos
= udf_block_map(sbi
->s_vat_inode
, 0);
1206 bh
= sb_bread(sb
, pos
);
1209 vat20
= (struct virtualAllocationTable20
*)bh
->b_data
;
1211 vat20
= (struct virtualAllocationTable20
*)
1215 map
->s_type_specific
.s_virtual
.s_start_offset
=
1216 le16_to_cpu(vat20
->lengthHeader
);
1217 map
->s_type_specific
.s_virtual
.s_num_entries
=
1218 (sbi
->s_vat_inode
->i_size
-
1219 map
->s_type_specific
.s_virtual
.
1220 s_start_offset
) >> 2;
1227 * Load partition descriptor block
1229 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1232 static int udf_load_partdesc(struct super_block
*sb
, sector_t block
)
1234 struct buffer_head
*bh
;
1235 struct partitionDesc
*p
;
1236 struct udf_part_map
*map
;
1237 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1239 uint16_t partitionNumber
;
1243 bh
= udf_read_tagged(sb
, block
, block
, &ident
);
1246 if (ident
!= TAG_IDENT_PD
) {
1251 p
= (struct partitionDesc
*)bh
->b_data
;
1252 partitionNumber
= le16_to_cpu(p
->partitionNumber
);
1254 /* First scan for TYPE1 and SPARABLE partitions */
1255 for (i
= 0; i
< sbi
->s_partitions
; i
++) {
1256 map
= &sbi
->s_partmaps
[i
];
1257 udf_debug("Searching map: (%u == %u)\n",
1258 map
->s_partition_num
, partitionNumber
);
1259 if (map
->s_partition_num
== partitionNumber
&&
1260 (map
->s_partition_type
== UDF_TYPE1_MAP15
||
1261 map
->s_partition_type
== UDF_SPARABLE_MAP15
))
1265 if (i
>= sbi
->s_partitions
) {
1266 udf_debug("Partition (%u) not found in partition map\n",
1272 ret
= udf_fill_partdesc_info(sb
, p
, i
);
1277 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1278 * PHYSICAL partitions are already set up
1281 map
= NULL
; /* supress 'maybe used uninitialized' warning */
1282 for (i
= 0; i
< sbi
->s_partitions
; i
++) {
1283 map
= &sbi
->s_partmaps
[i
];
1285 if (map
->s_partition_num
== partitionNumber
&&
1286 (map
->s_partition_type
== UDF_VIRTUAL_MAP15
||
1287 map
->s_partition_type
== UDF_VIRTUAL_MAP20
||
1288 map
->s_partition_type
== UDF_METADATA_MAP25
))
1292 if (i
>= sbi
->s_partitions
) {
1297 ret
= udf_fill_partdesc_info(sb
, p
, i
);
1301 if (map
->s_partition_type
== UDF_METADATA_MAP25
) {
1302 ret
= udf_load_metadata_files(sb
, i
, type1_idx
);
1304 udf_err(sb
, "error loading MetaData partition map %d\n",
1310 * If we have a partition with virtual map, we don't handle
1311 * writing to it (we overwrite blocks instead of relocating
1314 if (!sb_rdonly(sb
)) {
1318 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
1319 ret
= udf_load_vat(sb
, i
, type1_idx
);
1325 /* In case loading failed, we handle cleanup in udf_fill_super */
1330 static int udf_load_sparable_map(struct super_block
*sb
,
1331 struct udf_part_map
*map
,
1332 struct sparablePartitionMap
*spm
)
1336 struct sparingTable
*st
;
1337 struct udf_sparing_data
*sdata
= &map
->s_type_specific
.s_sparing
;
1339 struct buffer_head
*bh
;
1341 map
->s_partition_type
= UDF_SPARABLE_MAP15
;
1342 sdata
->s_packet_len
= le16_to_cpu(spm
->packetLength
);
1343 if (!is_power_of_2(sdata
->s_packet_len
)) {
1344 udf_err(sb
, "error loading logical volume descriptor: "
1345 "Invalid packet length %u\n",
1346 (unsigned)sdata
->s_packet_len
);
1349 if (spm
->numSparingTables
> 4) {
1350 udf_err(sb
, "error loading logical volume descriptor: "
1351 "Too many sparing tables (%d)\n",
1352 (int)spm
->numSparingTables
);
1356 for (i
= 0; i
< spm
->numSparingTables
; i
++) {
1357 loc
= le32_to_cpu(spm
->locSparingTable
[i
]);
1358 bh
= udf_read_tagged(sb
, loc
, loc
, &ident
);
1362 st
= (struct sparingTable
*)bh
->b_data
;
1364 strncmp(st
->sparingIdent
.ident
, UDF_ID_SPARING
,
1365 strlen(UDF_ID_SPARING
)) ||
1366 sizeof(*st
) + le16_to_cpu(st
->reallocationTableLen
) >
1372 sdata
->s_spar_map
[i
] = bh
;
1374 map
->s_partition_func
= udf_get_pblock_spar15
;
1378 static int udf_load_logicalvol(struct super_block
*sb
, sector_t block
,
1379 struct kernel_lb_addr
*fileset
)
1381 struct logicalVolDesc
*lvd
;
1384 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1385 struct genericPartitionMap
*gpm
;
1387 struct buffer_head
*bh
;
1388 unsigned int table_len
;
1391 bh
= udf_read_tagged(sb
, block
, block
, &ident
);
1394 BUG_ON(ident
!= TAG_IDENT_LVD
);
1395 lvd
= (struct logicalVolDesc
*)bh
->b_data
;
1396 table_len
= le32_to_cpu(lvd
->mapTableLength
);
1397 if (table_len
> sb
->s_blocksize
- sizeof(*lvd
)) {
1398 udf_err(sb
, "error loading logical volume descriptor: "
1399 "Partition table too long (%u > %lu)\n", table_len
,
1400 sb
->s_blocksize
- sizeof(*lvd
));
1405 ret
= udf_verify_domain_identifier(sb
, &lvd
->domainIdent
,
1409 ret
= udf_sb_alloc_partition_maps(sb
, le32_to_cpu(lvd
->numPartitionMaps
));
1413 for (i
= 0, offset
= 0;
1414 i
< sbi
->s_partitions
&& offset
< table_len
;
1415 i
++, offset
+= gpm
->partitionMapLength
) {
1416 struct udf_part_map
*map
= &sbi
->s_partmaps
[i
];
1417 gpm
= (struct genericPartitionMap
*)
1418 &(lvd
->partitionMaps
[offset
]);
1419 type
= gpm
->partitionMapType
;
1421 struct genericPartitionMap1
*gpm1
=
1422 (struct genericPartitionMap1
*)gpm
;
1423 map
->s_partition_type
= UDF_TYPE1_MAP15
;
1424 map
->s_volumeseqnum
= le16_to_cpu(gpm1
->volSeqNum
);
1425 map
->s_partition_num
= le16_to_cpu(gpm1
->partitionNum
);
1426 map
->s_partition_func
= NULL
;
1427 } else if (type
== 2) {
1428 struct udfPartitionMap2
*upm2
=
1429 (struct udfPartitionMap2
*)gpm
;
1430 if (!strncmp(upm2
->partIdent
.ident
, UDF_ID_VIRTUAL
,
1431 strlen(UDF_ID_VIRTUAL
))) {
1433 le16_to_cpu(((__le16
*)upm2
->partIdent
.
1436 map
->s_partition_type
=
1438 map
->s_partition_func
=
1439 udf_get_pblock_virt15
;
1441 map
->s_partition_type
=
1443 map
->s_partition_func
=
1444 udf_get_pblock_virt20
;
1446 } else if (!strncmp(upm2
->partIdent
.ident
,
1448 strlen(UDF_ID_SPARABLE
))) {
1449 ret
= udf_load_sparable_map(sb
, map
,
1450 (struct sparablePartitionMap
*)gpm
);
1453 } else if (!strncmp(upm2
->partIdent
.ident
,
1455 strlen(UDF_ID_METADATA
))) {
1456 struct udf_meta_data
*mdata
=
1457 &map
->s_type_specific
.s_metadata
;
1458 struct metadataPartitionMap
*mdm
=
1459 (struct metadataPartitionMap
*)
1460 &(lvd
->partitionMaps
[offset
]);
1461 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
1462 i
, type
, UDF_ID_METADATA
);
1464 map
->s_partition_type
= UDF_METADATA_MAP25
;
1465 map
->s_partition_func
= udf_get_pblock_meta25
;
1467 mdata
->s_meta_file_loc
=
1468 le32_to_cpu(mdm
->metadataFileLoc
);
1469 mdata
->s_mirror_file_loc
=
1470 le32_to_cpu(mdm
->metadataMirrorFileLoc
);
1471 mdata
->s_bitmap_file_loc
=
1472 le32_to_cpu(mdm
->metadataBitmapFileLoc
);
1473 mdata
->s_alloc_unit_size
=
1474 le32_to_cpu(mdm
->allocUnitSize
);
1475 mdata
->s_align_unit_size
=
1476 le16_to_cpu(mdm
->alignUnitSize
);
1477 if (mdm
->flags
& 0x01)
1478 mdata
->s_flags
|= MF_DUPLICATE_MD
;
1480 udf_debug("Metadata Ident suffix=0x%x\n",
1481 le16_to_cpu(*(__le16
*)
1482 mdm
->partIdent
.identSuffix
));
1483 udf_debug("Metadata part num=%u\n",
1484 le16_to_cpu(mdm
->partitionNum
));
1485 udf_debug("Metadata part alloc unit size=%u\n",
1486 le32_to_cpu(mdm
->allocUnitSize
));
1487 udf_debug("Metadata file loc=%u\n",
1488 le32_to_cpu(mdm
->metadataFileLoc
));
1489 udf_debug("Mirror file loc=%u\n",
1490 le32_to_cpu(mdm
->metadataMirrorFileLoc
));
1491 udf_debug("Bitmap file loc=%u\n",
1492 le32_to_cpu(mdm
->metadataBitmapFileLoc
));
1493 udf_debug("Flags: %d %u\n",
1494 mdata
->s_flags
, mdm
->flags
);
1496 udf_debug("Unknown ident: %s\n",
1497 upm2
->partIdent
.ident
);
1500 map
->s_volumeseqnum
= le16_to_cpu(upm2
->volSeqNum
);
1501 map
->s_partition_num
= le16_to_cpu(upm2
->partitionNum
);
1503 udf_debug("Partition (%d:%u) type %u on volume %u\n",
1504 i
, map
->s_partition_num
, type
, map
->s_volumeseqnum
);
1508 struct long_ad
*la
= (struct long_ad
*)&(lvd
->logicalVolContentsUse
[0]);
1510 *fileset
= lelb_to_cpu(la
->extLocation
);
1511 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
1512 fileset
->logicalBlockNum
,
1513 fileset
->partitionReferenceNum
);
1515 if (lvd
->integritySeqExt
.extLength
)
1516 udf_load_logicalvolint(sb
, leea_to_cpu(lvd
->integritySeqExt
));
1519 if (!sbi
->s_lvid_bh
) {
1520 /* We can't generate unique IDs without a valid LVID */
1521 if (sb_rdonly(sb
)) {
1522 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
1524 udf_warn(sb
, "Damaged or missing LVID, forcing "
1525 "readonly mount\n");
1535 * Find the prevailing Logical Volume Integrity Descriptor.
1537 static void udf_load_logicalvolint(struct super_block
*sb
, struct kernel_extent_ad loc
)
1539 struct buffer_head
*bh
, *final_bh
;
1541 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1542 struct logicalVolIntegrityDesc
*lvid
;
1543 int indirections
= 0;
1545 while (++indirections
<= UDF_MAX_LVID_NESTING
) {
1547 while (loc
.extLength
> 0 &&
1548 (bh
= udf_read_tagged(sb
, loc
.extLocation
,
1549 loc
.extLocation
, &ident
))) {
1550 if (ident
!= TAG_IDENT_LVID
) {
1558 loc
.extLength
-= sb
->s_blocksize
;
1565 brelse(sbi
->s_lvid_bh
);
1566 sbi
->s_lvid_bh
= final_bh
;
1568 lvid
= (struct logicalVolIntegrityDesc
*)final_bh
->b_data
;
1569 if (lvid
->nextIntegrityExt
.extLength
== 0)
1572 loc
= leea_to_cpu(lvid
->nextIntegrityExt
);
1575 udf_warn(sb
, "Too many LVID indirections (max %u), ignoring.\n",
1576 UDF_MAX_LVID_NESTING
);
1577 brelse(sbi
->s_lvid_bh
);
1578 sbi
->s_lvid_bh
= NULL
;
1582 * Step for reallocation of table of partition descriptor sequence numbers.
1583 * Must be power of 2.
1585 #define PART_DESC_ALLOC_STEP 32
1587 struct part_desc_seq_scan_data
{
1588 struct udf_vds_record rec
;
1592 struct desc_seq_scan_data
{
1593 struct udf_vds_record vds
[VDS_POS_LENGTH
];
1594 unsigned int size_part_descs
;
1595 unsigned int num_part_descs
;
1596 struct part_desc_seq_scan_data
*part_descs_loc
;
1599 static struct udf_vds_record
*handle_partition_descriptor(
1600 struct buffer_head
*bh
,
1601 struct desc_seq_scan_data
*data
)
1603 struct partitionDesc
*desc
= (struct partitionDesc
*)bh
->b_data
;
1607 partnum
= le16_to_cpu(desc
->partitionNumber
);
1608 for (i
= 0; i
< data
->num_part_descs
; i
++)
1609 if (partnum
== data
->part_descs_loc
[i
].partnum
)
1610 return &(data
->part_descs_loc
[i
].rec
);
1611 if (data
->num_part_descs
>= data
->size_part_descs
) {
1612 struct part_desc_seq_scan_data
*new_loc
;
1613 unsigned int new_size
= ALIGN(partnum
, PART_DESC_ALLOC_STEP
);
1615 new_loc
= kcalloc(new_size
, sizeof(*new_loc
), GFP_KERNEL
);
1617 return ERR_PTR(-ENOMEM
);
1618 memcpy(new_loc
, data
->part_descs_loc
,
1619 data
->size_part_descs
* sizeof(*new_loc
));
1620 kfree(data
->part_descs_loc
);
1621 data
->part_descs_loc
= new_loc
;
1622 data
->size_part_descs
= new_size
;
1624 return &(data
->part_descs_loc
[data
->num_part_descs
++].rec
);
1628 static struct udf_vds_record
*get_volume_descriptor_record(uint16_t ident
,
1629 struct buffer_head
*bh
, struct desc_seq_scan_data
*data
)
1632 case TAG_IDENT_PVD
: /* ISO 13346 3/10.1 */
1633 return &(data
->vds
[VDS_POS_PRIMARY_VOL_DESC
]);
1634 case TAG_IDENT_IUVD
: /* ISO 13346 3/10.4 */
1635 return &(data
->vds
[VDS_POS_IMP_USE_VOL_DESC
]);
1636 case TAG_IDENT_LVD
: /* ISO 13346 3/10.6 */
1637 return &(data
->vds
[VDS_POS_LOGICAL_VOL_DESC
]);
1638 case TAG_IDENT_USD
: /* ISO 13346 3/10.8 */
1639 return &(data
->vds
[VDS_POS_UNALLOC_SPACE_DESC
]);
1640 case TAG_IDENT_PD
: /* ISO 13346 3/10.5 */
1641 return handle_partition_descriptor(bh
, data
);
1647 * Process a main/reserve volume descriptor sequence.
1648 * @block First block of first extent of the sequence.
1649 * @lastblock Lastblock of first extent of the sequence.
1650 * @fileset There we store extent containing root fileset
1652 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1655 static noinline
int udf_process_sequence(
1656 struct super_block
*sb
,
1657 sector_t block
, sector_t lastblock
,
1658 struct kernel_lb_addr
*fileset
)
1660 struct buffer_head
*bh
= NULL
;
1661 struct udf_vds_record
*curr
;
1662 struct generic_desc
*gd
;
1663 struct volDescPtr
*vdp
;
1668 unsigned int indirections
= 0;
1669 struct desc_seq_scan_data data
;
1672 memset(data
.vds
, 0, sizeof(struct udf_vds_record
) * VDS_POS_LENGTH
);
1673 data
.size_part_descs
= PART_DESC_ALLOC_STEP
;
1674 data
.num_part_descs
= 0;
1675 data
.part_descs_loc
= kcalloc(data
.size_part_descs
,
1676 sizeof(*data
.part_descs_loc
),
1678 if (!data
.part_descs_loc
)
1682 * Read the main descriptor sequence and find which descriptors
1685 for (; (!done
&& block
<= lastblock
); block
++) {
1686 bh
= udf_read_tagged(sb
, block
, block
, &ident
);
1690 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1691 gd
= (struct generic_desc
*)bh
->b_data
;
1692 vdsn
= le32_to_cpu(gd
->volDescSeqNum
);
1694 case TAG_IDENT_VDP
: /* ISO 13346 3/10.3 */
1695 if (++indirections
> UDF_MAX_TD_NESTING
) {
1696 udf_err(sb
, "too many Volume Descriptor "
1697 "Pointers (max %u supported)\n",
1698 UDF_MAX_TD_NESTING
);
1703 vdp
= (struct volDescPtr
*)bh
->b_data
;
1704 block
= le32_to_cpu(vdp
->nextVolDescSeqExt
.extLocation
);
1705 lastblock
= le32_to_cpu(
1706 vdp
->nextVolDescSeqExt
.extLength
) >>
1707 sb
->s_blocksize_bits
;
1708 lastblock
+= block
- 1;
1709 /* For loop is going to increment 'block' again */
1712 case TAG_IDENT_PVD
: /* ISO 13346 3/10.1 */
1713 case TAG_IDENT_IUVD
: /* ISO 13346 3/10.4 */
1714 case TAG_IDENT_LVD
: /* ISO 13346 3/10.6 */
1715 case TAG_IDENT_USD
: /* ISO 13346 3/10.8 */
1716 case TAG_IDENT_PD
: /* ISO 13346 3/10.5 */
1717 curr
= get_volume_descriptor_record(ident
, bh
, &data
);
1720 return PTR_ERR(curr
);
1722 /* Descriptor we don't care about? */
1725 if (vdsn
>= curr
->volDescSeqNum
) {
1726 curr
->volDescSeqNum
= vdsn
;
1727 curr
->block
= block
;
1730 case TAG_IDENT_TD
: /* ISO 13346 3/10.9 */
1737 * Now read interesting descriptors again and process them
1738 * in a suitable order
1740 if (!data
.vds
[VDS_POS_PRIMARY_VOL_DESC
].block
) {
1741 udf_err(sb
, "Primary Volume Descriptor not found!\n");
1744 ret
= udf_load_pvoldesc(sb
, data
.vds
[VDS_POS_PRIMARY_VOL_DESC
].block
);
1748 if (data
.vds
[VDS_POS_LOGICAL_VOL_DESC
].block
) {
1749 ret
= udf_load_logicalvol(sb
,
1750 data
.vds
[VDS_POS_LOGICAL_VOL_DESC
].block
,
1756 /* Now handle prevailing Partition Descriptors */
1757 for (i
= 0; i
< data
.num_part_descs
; i
++) {
1758 ret
= udf_load_partdesc(sb
, data
.part_descs_loc
[i
].rec
.block
);
1767 * Load Volume Descriptor Sequence described by anchor in bh
1769 * Returns <0 on error, 0 on success
1771 static int udf_load_sequence(struct super_block
*sb
, struct buffer_head
*bh
,
1772 struct kernel_lb_addr
*fileset
)
1774 struct anchorVolDescPtr
*anchor
;
1775 sector_t main_s
, main_e
, reserve_s
, reserve_e
;
1778 anchor
= (struct anchorVolDescPtr
*)bh
->b_data
;
1780 /* Locate the main sequence */
1781 main_s
= le32_to_cpu(anchor
->mainVolDescSeqExt
.extLocation
);
1782 main_e
= le32_to_cpu(anchor
->mainVolDescSeqExt
.extLength
);
1783 main_e
= main_e
>> sb
->s_blocksize_bits
;
1784 main_e
+= main_s
- 1;
1786 /* Locate the reserve sequence */
1787 reserve_s
= le32_to_cpu(anchor
->reserveVolDescSeqExt
.extLocation
);
1788 reserve_e
= le32_to_cpu(anchor
->reserveVolDescSeqExt
.extLength
);
1789 reserve_e
= reserve_e
>> sb
->s_blocksize_bits
;
1790 reserve_e
+= reserve_s
- 1;
1792 /* Process the main & reserve sequences */
1793 /* responsible for finding the PartitionDesc(s) */
1794 ret
= udf_process_sequence(sb
, main_s
, main_e
, fileset
);
1797 udf_sb_free_partitions(sb
);
1798 ret
= udf_process_sequence(sb
, reserve_s
, reserve_e
, fileset
);
1800 udf_sb_free_partitions(sb
);
1801 /* No sequence was OK, return -EIO */
1809 * Check whether there is an anchor block in the given block and
1810 * load Volume Descriptor Sequence if so.
1812 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1815 static int udf_check_anchor_block(struct super_block
*sb
, sector_t block
,
1816 struct kernel_lb_addr
*fileset
)
1818 struct buffer_head
*bh
;
1822 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_VARCONV
) &&
1823 udf_fixed_to_variable(block
) >=
1824 i_size_read(sb
->s_bdev
->bd_inode
) >> sb
->s_blocksize_bits
)
1827 bh
= udf_read_tagged(sb
, block
, block
, &ident
);
1830 if (ident
!= TAG_IDENT_AVDP
) {
1834 ret
= udf_load_sequence(sb
, bh
, fileset
);
1840 * Search for an anchor volume descriptor pointer.
1842 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1845 static int udf_scan_anchors(struct super_block
*sb
, sector_t
*lastblock
,
1846 struct kernel_lb_addr
*fileset
)
1850 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1854 /* First try user provided anchor */
1855 if (sbi
->s_anchor
) {
1856 ret
= udf_check_anchor_block(sb
, sbi
->s_anchor
, fileset
);
1861 * according to spec, anchor is in either:
1865 * however, if the disc isn't closed, it could be 512.
1867 ret
= udf_check_anchor_block(sb
, sbi
->s_session
+ 256, fileset
);
1871 * The trouble is which block is the last one. Drives often misreport
1872 * this so we try various possibilities.
1874 last
[last_count
++] = *lastblock
;
1875 if (*lastblock
>= 1)
1876 last
[last_count
++] = *lastblock
- 1;
1877 last
[last_count
++] = *lastblock
+ 1;
1878 if (*lastblock
>= 2)
1879 last
[last_count
++] = *lastblock
- 2;
1880 if (*lastblock
>= 150)
1881 last
[last_count
++] = *lastblock
- 150;
1882 if (*lastblock
>= 152)
1883 last
[last_count
++] = *lastblock
- 152;
1885 for (i
= 0; i
< last_count
; i
++) {
1886 if (last
[i
] >= i_size_read(sb
->s_bdev
->bd_inode
) >>
1887 sb
->s_blocksize_bits
)
1889 ret
= udf_check_anchor_block(sb
, last
[i
], fileset
);
1890 if (ret
!= -EAGAIN
) {
1892 *lastblock
= last
[i
];
1897 ret
= udf_check_anchor_block(sb
, last
[i
] - 256, fileset
);
1898 if (ret
!= -EAGAIN
) {
1900 *lastblock
= last
[i
];
1905 /* Finally try block 512 in case media is open */
1906 return udf_check_anchor_block(sb
, sbi
->s_session
+ 512, fileset
);
1910 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1911 * area specified by it. The function expects sbi->s_lastblock to be the last
1912 * block on the media.
1914 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1917 static int udf_find_anchor(struct super_block
*sb
,
1918 struct kernel_lb_addr
*fileset
)
1920 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1921 sector_t lastblock
= sbi
->s_last_block
;
1924 ret
= udf_scan_anchors(sb
, &lastblock
, fileset
);
1928 /* No anchor found? Try VARCONV conversion of block numbers */
1929 UDF_SET_FLAG(sb
, UDF_FLAG_VARCONV
);
1930 lastblock
= udf_variable_to_fixed(sbi
->s_last_block
);
1931 /* Firstly, we try to not convert number of the last block */
1932 ret
= udf_scan_anchors(sb
, &lastblock
, fileset
);
1936 lastblock
= sbi
->s_last_block
;
1937 /* Secondly, we try with converted number of the last block */
1938 ret
= udf_scan_anchors(sb
, &lastblock
, fileset
);
1940 /* VARCONV didn't help. Clear it. */
1941 UDF_CLEAR_FLAG(sb
, UDF_FLAG_VARCONV
);
1945 sbi
->s_last_block
= lastblock
;
1950 * Check Volume Structure Descriptor, find Anchor block and load Volume
1951 * Descriptor Sequence.
1953 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1954 * block was not found.
1956 static int udf_load_vrs(struct super_block
*sb
, struct udf_options
*uopt
,
1957 int silent
, struct kernel_lb_addr
*fileset
)
1959 struct udf_sb_info
*sbi
= UDF_SB(sb
);
1963 if (!sb_set_blocksize(sb
, uopt
->blocksize
)) {
1965 udf_warn(sb
, "Bad block size\n");
1968 sbi
->s_last_block
= uopt
->lastblock
;
1970 /* Check that it is NSR02 compliant */
1971 nsr
= udf_check_vsd(sb
);
1974 udf_warn(sb
, "No VRS found\n");
1978 udf_debug("Failed to read sector at offset %d. "
1979 "Assuming open disc. Skipping validity "
1980 "check\n", VSD_FIRST_SECTOR_OFFSET
);
1981 if (!sbi
->s_last_block
)
1982 sbi
->s_last_block
= udf_get_last_block(sb
);
1984 udf_debug("Validity check skipped because of novrs option\n");
1987 /* Look for anchor block and load Volume Descriptor Sequence */
1988 sbi
->s_anchor
= uopt
->anchor
;
1989 ret
= udf_find_anchor(sb
, fileset
);
1991 if (!silent
&& ret
== -EAGAIN
)
1992 udf_warn(sb
, "No anchor found\n");
1998 static void udf_finalize_lvid(struct logicalVolIntegrityDesc
*lvid
)
2000 struct timespec64 ts
;
2002 ktime_get_real_ts64(&ts
);
2003 udf_time_to_disk_stamp(&lvid
->recordingDateAndTime
, ts
);
2004 lvid
->descTag
.descCRC
= cpu_to_le16(
2005 crc_itu_t(0, (char *)lvid
+ sizeof(struct tag
),
2006 le16_to_cpu(lvid
->descTag
.descCRCLength
)));
2007 lvid
->descTag
.tagChecksum
= udf_tag_checksum(&lvid
->descTag
);
2010 static void udf_open_lvid(struct super_block
*sb
)
2012 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2013 struct buffer_head
*bh
= sbi
->s_lvid_bh
;
2014 struct logicalVolIntegrityDesc
*lvid
;
2015 struct logicalVolIntegrityDescImpUse
*lvidiu
;
2019 lvid
= (struct logicalVolIntegrityDesc
*)bh
->b_data
;
2020 lvidiu
= udf_sb_lvidiu(sb
);
2024 mutex_lock(&sbi
->s_alloc_mutex
);
2025 lvidiu
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
2026 lvidiu
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
2027 if (le32_to_cpu(lvid
->integrityType
) == LVID_INTEGRITY_TYPE_CLOSE
)
2028 lvid
->integrityType
= cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN
);
2030 UDF_SET_FLAG(sb
, UDF_FLAG_INCONSISTENT
);
2032 udf_finalize_lvid(lvid
);
2033 mark_buffer_dirty(bh
);
2034 sbi
->s_lvid_dirty
= 0;
2035 mutex_unlock(&sbi
->s_alloc_mutex
);
2036 /* Make opening of filesystem visible on the media immediately */
2037 sync_dirty_buffer(bh
);
2040 static void udf_close_lvid(struct super_block
*sb
)
2042 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2043 struct buffer_head
*bh
= sbi
->s_lvid_bh
;
2044 struct logicalVolIntegrityDesc
*lvid
;
2045 struct logicalVolIntegrityDescImpUse
*lvidiu
;
2049 lvid
= (struct logicalVolIntegrityDesc
*)bh
->b_data
;
2050 lvidiu
= udf_sb_lvidiu(sb
);
2054 mutex_lock(&sbi
->s_alloc_mutex
);
2055 lvidiu
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
2056 lvidiu
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
2057 if (UDF_MAX_WRITE_VERSION
> le16_to_cpu(lvidiu
->maxUDFWriteRev
))
2058 lvidiu
->maxUDFWriteRev
= cpu_to_le16(UDF_MAX_WRITE_VERSION
);
2059 if (sbi
->s_udfrev
> le16_to_cpu(lvidiu
->minUDFReadRev
))
2060 lvidiu
->minUDFReadRev
= cpu_to_le16(sbi
->s_udfrev
);
2061 if (sbi
->s_udfrev
> le16_to_cpu(lvidiu
->minUDFWriteRev
))
2062 lvidiu
->minUDFWriteRev
= cpu_to_le16(sbi
->s_udfrev
);
2063 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_INCONSISTENT
))
2064 lvid
->integrityType
= cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE
);
2067 * We set buffer uptodate unconditionally here to avoid spurious
2068 * warnings from mark_buffer_dirty() when previous EIO has marked
2069 * the buffer as !uptodate
2071 set_buffer_uptodate(bh
);
2072 udf_finalize_lvid(lvid
);
2073 mark_buffer_dirty(bh
);
2074 sbi
->s_lvid_dirty
= 0;
2075 mutex_unlock(&sbi
->s_alloc_mutex
);
2076 /* Make closing of filesystem visible on the media immediately */
2077 sync_dirty_buffer(bh
);
2080 u64
lvid_get_unique_id(struct super_block
*sb
)
2082 struct buffer_head
*bh
;
2083 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2084 struct logicalVolIntegrityDesc
*lvid
;
2085 struct logicalVolHeaderDesc
*lvhd
;
2089 bh
= sbi
->s_lvid_bh
;
2093 lvid
= (struct logicalVolIntegrityDesc
*)bh
->b_data
;
2094 lvhd
= (struct logicalVolHeaderDesc
*)lvid
->logicalVolContentsUse
;
2096 mutex_lock(&sbi
->s_alloc_mutex
);
2097 ret
= uniqueID
= le64_to_cpu(lvhd
->uniqueID
);
2098 if (!(++uniqueID
& 0xFFFFFFFF))
2100 lvhd
->uniqueID
= cpu_to_le64(uniqueID
);
2101 udf_updated_lvid(sb
);
2102 mutex_unlock(&sbi
->s_alloc_mutex
);
2107 static int udf_fill_super(struct super_block
*sb
, void *options
, int silent
)
2110 struct inode
*inode
= NULL
;
2111 struct udf_options uopt
;
2112 struct kernel_lb_addr rootdir
, fileset
;
2113 struct udf_sb_info
*sbi
;
2114 bool lvid_open
= false;
2116 uopt
.flags
= (1 << UDF_FLAG_USE_AD_IN_ICB
) | (1 << UDF_FLAG_STRICT
);
2117 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2118 uopt
.uid
= make_kuid(current_user_ns(), overflowuid
);
2119 uopt
.gid
= make_kgid(current_user_ns(), overflowgid
);
2121 uopt
.fmode
= UDF_INVALID_MODE
;
2122 uopt
.dmode
= UDF_INVALID_MODE
;
2123 uopt
.nls_map
= NULL
;
2125 sbi
= kzalloc(sizeof(*sbi
), GFP_KERNEL
);
2129 sb
->s_fs_info
= sbi
;
2131 mutex_init(&sbi
->s_alloc_mutex
);
2133 if (!udf_parse_options((char *)options
, &uopt
, false))
2134 goto parse_options_failure
;
2136 if (uopt
.flags
& (1 << UDF_FLAG_UTF8
) &&
2137 uopt
.flags
& (1 << UDF_FLAG_NLS_MAP
)) {
2138 udf_err(sb
, "utf8 cannot be combined with iocharset\n");
2139 goto parse_options_failure
;
2141 if ((uopt
.flags
& (1 << UDF_FLAG_NLS_MAP
)) && !uopt
.nls_map
) {
2142 uopt
.nls_map
= load_nls_default();
2144 uopt
.flags
&= ~(1 << UDF_FLAG_NLS_MAP
);
2146 udf_debug("Using default NLS map\n");
2148 if (!(uopt
.flags
& (1 << UDF_FLAG_NLS_MAP
)))
2149 uopt
.flags
|= (1 << UDF_FLAG_UTF8
);
2151 fileset
.logicalBlockNum
= 0xFFFFFFFF;
2152 fileset
.partitionReferenceNum
= 0xFFFF;
2154 sbi
->s_flags
= uopt
.flags
;
2155 sbi
->s_uid
= uopt
.uid
;
2156 sbi
->s_gid
= uopt
.gid
;
2157 sbi
->s_umask
= uopt
.umask
;
2158 sbi
->s_fmode
= uopt
.fmode
;
2159 sbi
->s_dmode
= uopt
.dmode
;
2160 sbi
->s_nls_map
= uopt
.nls_map
;
2161 rwlock_init(&sbi
->s_cred_lock
);
2163 if (uopt
.session
== 0xFFFFFFFF)
2164 sbi
->s_session
= udf_get_last_session(sb
);
2166 sbi
->s_session
= uopt
.session
;
2168 udf_debug("Multi-session=%d\n", sbi
->s_session
);
2170 /* Fill in the rest of the superblock */
2171 sb
->s_op
= &udf_sb_ops
;
2172 sb
->s_export_op
= &udf_export_ops
;
2174 sb
->s_magic
= UDF_SUPER_MAGIC
;
2175 sb
->s_time_gran
= 1000;
2177 if (uopt
.flags
& (1 << UDF_FLAG_BLOCKSIZE_SET
)) {
2178 ret
= udf_load_vrs(sb
, &uopt
, silent
, &fileset
);
2180 uopt
.blocksize
= bdev_logical_block_size(sb
->s_bdev
);
2181 while (uopt
.blocksize
<= 4096) {
2182 ret
= udf_load_vrs(sb
, &uopt
, silent
, &fileset
);
2184 if (!silent
&& ret
!= -EACCES
) {
2185 pr_notice("Scanning with blocksize %u failed\n",
2188 brelse(sbi
->s_lvid_bh
);
2189 sbi
->s_lvid_bh
= NULL
;
2191 * EACCES is special - we want to propagate to
2192 * upper layers that we cannot handle RW mount.
2199 uopt
.blocksize
<<= 1;
2203 if (ret
== -EAGAIN
) {
2204 udf_warn(sb
, "No partition found (1)\n");
2210 udf_debug("Lastblock=%u\n", sbi
->s_last_block
);
2212 if (sbi
->s_lvid_bh
) {
2213 struct logicalVolIntegrityDescImpUse
*lvidiu
=
2215 uint16_t minUDFReadRev
;
2216 uint16_t minUDFWriteRev
;
2222 minUDFReadRev
= le16_to_cpu(lvidiu
->minUDFReadRev
);
2223 minUDFWriteRev
= le16_to_cpu(lvidiu
->minUDFWriteRev
);
2224 if (minUDFReadRev
> UDF_MAX_READ_VERSION
) {
2225 udf_err(sb
, "minUDFReadRev=%x (max is %x)\n",
2227 UDF_MAX_READ_VERSION
);
2230 } else if (minUDFWriteRev
> UDF_MAX_WRITE_VERSION
) {
2231 if (!sb_rdonly(sb
)) {
2235 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
2238 sbi
->s_udfrev
= minUDFWriteRev
;
2240 if (minUDFReadRev
>= UDF_VERS_USE_EXTENDED_FE
)
2241 UDF_SET_FLAG(sb
, UDF_FLAG_USE_EXTENDED_FE
);
2242 if (minUDFReadRev
>= UDF_VERS_USE_STREAMS
)
2243 UDF_SET_FLAG(sb
, UDF_FLAG_USE_STREAMS
);
2246 if (!sbi
->s_partitions
) {
2247 udf_warn(sb
, "No partition found (2)\n");
2252 if (sbi
->s_partmaps
[sbi
->s_partition
].s_partition_flags
&
2253 UDF_PART_FLAG_READ_ONLY
) {
2254 if (!sb_rdonly(sb
)) {
2258 UDF_SET_FLAG(sb
, UDF_FLAG_RW_INCOMPAT
);
2261 ret
= udf_find_fileset(sb
, &fileset
, &rootdir
);
2263 udf_warn(sb
, "No fileset found\n");
2268 struct timestamp ts
;
2269 udf_time_to_disk_stamp(&ts
, sbi
->s_record_time
);
2270 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2271 sbi
->s_volume_ident
,
2272 le16_to_cpu(ts
.year
), ts
.month
, ts
.day
,
2273 ts
.hour
, ts
.minute
, le16_to_cpu(ts
.typeAndTimezone
));
2275 if (!sb_rdonly(sb
)) {
2280 /* Assign the root inode */
2281 /* assign inodes by physical block number */
2282 /* perhaps it's not extensible enough, but for now ... */
2283 inode
= udf_iget(sb
, &rootdir
);
2284 if (IS_ERR(inode
)) {
2285 udf_err(sb
, "Error in udf_iget, block=%u, partition=%u\n",
2286 rootdir
.logicalBlockNum
, rootdir
.partitionReferenceNum
);
2287 ret
= PTR_ERR(inode
);
2291 /* Allocate a dentry for the root inode */
2292 sb
->s_root
= d_make_root(inode
);
2294 udf_err(sb
, "Couldn't allocate root dentry\n");
2298 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
2299 sb
->s_max_links
= UDF_MAX_LINKS
;
2303 iput(sbi
->s_vat_inode
);
2304 parse_options_failure
:
2306 unload_nls(uopt
.nls_map
);
2309 brelse(sbi
->s_lvid_bh
);
2310 udf_sb_free_partitions(sb
);
2312 sb
->s_fs_info
= NULL
;
2317 void _udf_err(struct super_block
*sb
, const char *function
,
2318 const char *fmt
, ...)
2320 struct va_format vaf
;
2323 va_start(args
, fmt
);
2328 pr_err("error (device %s): %s: %pV", sb
->s_id
, function
, &vaf
);
2333 void _udf_warn(struct super_block
*sb
, const char *function
,
2334 const char *fmt
, ...)
2336 struct va_format vaf
;
2339 va_start(args
, fmt
);
2344 pr_warn("warning (device %s): %s: %pV", sb
->s_id
, function
, &vaf
);
2349 static void udf_put_super(struct super_block
*sb
)
2351 struct udf_sb_info
*sbi
;
2355 iput(sbi
->s_vat_inode
);
2356 if (UDF_QUERY_FLAG(sb
, UDF_FLAG_NLS_MAP
))
2357 unload_nls(sbi
->s_nls_map
);
2360 brelse(sbi
->s_lvid_bh
);
2361 udf_sb_free_partitions(sb
);
2362 mutex_destroy(&sbi
->s_alloc_mutex
);
2363 kfree(sb
->s_fs_info
);
2364 sb
->s_fs_info
= NULL
;
2367 static int udf_sync_fs(struct super_block
*sb
, int wait
)
2369 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2371 mutex_lock(&sbi
->s_alloc_mutex
);
2372 if (sbi
->s_lvid_dirty
) {
2373 struct buffer_head
*bh
= sbi
->s_lvid_bh
;
2374 struct logicalVolIntegrityDesc
*lvid
;
2376 lvid
= (struct logicalVolIntegrityDesc
*)bh
->b_data
;
2377 udf_finalize_lvid(lvid
);
2380 * Blockdevice will be synced later so we don't have to submit
2383 mark_buffer_dirty(bh
);
2384 sbi
->s_lvid_dirty
= 0;
2386 mutex_unlock(&sbi
->s_alloc_mutex
);
2391 static int udf_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2393 struct super_block
*sb
= dentry
->d_sb
;
2394 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2395 struct logicalVolIntegrityDescImpUse
*lvidiu
;
2396 u64 id
= huge_encode_dev(sb
->s_bdev
->bd_dev
);
2398 lvidiu
= udf_sb_lvidiu(sb
);
2399 buf
->f_type
= UDF_SUPER_MAGIC
;
2400 buf
->f_bsize
= sb
->s_blocksize
;
2401 buf
->f_blocks
= sbi
->s_partmaps
[sbi
->s_partition
].s_partition_len
;
2402 buf
->f_bfree
= udf_count_free(sb
);
2403 buf
->f_bavail
= buf
->f_bfree
;
2404 buf
->f_files
= (lvidiu
!= NULL
? (le32_to_cpu(lvidiu
->numFiles
) +
2405 le32_to_cpu(lvidiu
->numDirs
)) : 0)
2407 buf
->f_ffree
= buf
->f_bfree
;
2408 buf
->f_namelen
= UDF_NAME_LEN
;
2409 buf
->f_fsid
.val
[0] = (u32
)id
;
2410 buf
->f_fsid
.val
[1] = (u32
)(id
>> 32);
2415 static unsigned int udf_count_free_bitmap(struct super_block
*sb
,
2416 struct udf_bitmap
*bitmap
)
2418 struct buffer_head
*bh
= NULL
;
2419 unsigned int accum
= 0;
2421 udf_pblk_t block
= 0, newblock
;
2422 struct kernel_lb_addr loc
;
2426 struct spaceBitmapDesc
*bm
;
2428 loc
.logicalBlockNum
= bitmap
->s_extPosition
;
2429 loc
.partitionReferenceNum
= UDF_SB(sb
)->s_partition
;
2430 bh
= udf_read_ptagged(sb
, &loc
, 0, &ident
);
2433 udf_err(sb
, "udf_count_free failed\n");
2435 } else if (ident
!= TAG_IDENT_SBD
) {
2437 udf_err(sb
, "udf_count_free failed\n");
2441 bm
= (struct spaceBitmapDesc
*)bh
->b_data
;
2442 bytes
= le32_to_cpu(bm
->numOfBytes
);
2443 index
= sizeof(struct spaceBitmapDesc
); /* offset in first block only */
2444 ptr
= (uint8_t *)bh
->b_data
;
2447 u32 cur_bytes
= min_t(u32
, bytes
, sb
->s_blocksize
- index
);
2448 accum
+= bitmap_weight((const unsigned long *)(ptr
+ index
),
2453 newblock
= udf_get_lb_pblock(sb
, &loc
, ++block
);
2454 bh
= udf_tread(sb
, newblock
);
2456 udf_debug("read failed\n");
2460 ptr
= (uint8_t *)bh
->b_data
;
2468 static unsigned int udf_count_free_table(struct super_block
*sb
,
2469 struct inode
*table
)
2471 unsigned int accum
= 0;
2473 struct kernel_lb_addr eloc
;
2475 struct extent_position epos
;
2477 mutex_lock(&UDF_SB(sb
)->s_alloc_mutex
);
2478 epos
.block
= UDF_I(table
)->i_location
;
2479 epos
.offset
= sizeof(struct unallocSpaceEntry
);
2482 while ((etype
= udf_next_aext(table
, &epos
, &eloc
, &elen
, 1)) != -1)
2483 accum
+= (elen
>> table
->i_sb
->s_blocksize_bits
);
2486 mutex_unlock(&UDF_SB(sb
)->s_alloc_mutex
);
2491 static unsigned int udf_count_free(struct super_block
*sb
)
2493 unsigned int accum
= 0;
2494 struct udf_sb_info
*sbi
= UDF_SB(sb
);
2495 struct udf_part_map
*map
;
2496 unsigned int part
= sbi
->s_partition
;
2497 int ptype
= sbi
->s_partmaps
[part
].s_partition_type
;
2499 if (ptype
== UDF_METADATA_MAP25
) {
2500 part
= sbi
->s_partmaps
[part
].s_type_specific
.s_metadata
.
2501 s_phys_partition_ref
;
2502 } else if (ptype
== UDF_VIRTUAL_MAP15
|| ptype
== UDF_VIRTUAL_MAP20
) {
2504 * Filesystems with VAT are append-only and we cannot write to
2505 * them. Let's just report 0 here.
2510 if (sbi
->s_lvid_bh
) {
2511 struct logicalVolIntegrityDesc
*lvid
=
2512 (struct logicalVolIntegrityDesc
*)
2513 sbi
->s_lvid_bh
->b_data
;
2514 if (le32_to_cpu(lvid
->numOfPartitions
) > part
) {
2515 accum
= le32_to_cpu(
2516 lvid
->freeSpaceTable
[part
]);
2517 if (accum
== 0xFFFFFFFF)
2525 map
= &sbi
->s_partmaps
[part
];
2526 if (map
->s_partition_flags
& UDF_PART_FLAG_UNALLOC_BITMAP
) {
2527 accum
+= udf_count_free_bitmap(sb
,
2528 map
->s_uspace
.s_bitmap
);
2533 if (map
->s_partition_flags
& UDF_PART_FLAG_UNALLOC_TABLE
) {
2534 accum
+= udf_count_free_table(sb
,
2535 map
->s_uspace
.s_table
);
2540 MODULE_AUTHOR("Ben Fennema");
2541 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2542 MODULE_LICENSE("GPL");
2543 module_init(init_udf_fs
)
2544 module_exit(exit_udf_fs
)