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>
67 #define VDS_POS_PRIMARY_VOL_DESC 0
68 #define VDS_POS_UNALLOC_SPACE_DESC 1
69 #define VDS_POS_LOGICAL_VOL_DESC 2
70 #define VDS_POS_PARTITION_DESC 3
71 #define VDS_POS_IMP_USE_VOL_DESC 4
72 #define VDS_POS_VOL_DESC_PTR 5
73 #define VDS_POS_TERMINATING_DESC 6
74 #define VDS_POS_LENGTH 7
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 /* These are the "meat" - everything else is stuffing */
91 static int udf_fill_super(struct super_block *, void *, int);
92 static void udf_put_super(struct super_block *);
93 static int udf_sync_fs(struct super_block *, int);
94 static int udf_remount_fs(struct super_block *, int *, char *);
95 static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
96 static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
97 struct kernel_lb_addr *);
98 static void udf_load_fileset(struct super_block *, struct buffer_head *,
99 struct kernel_lb_addr *);
100 static void udf_open_lvid(struct super_block *);
101 static void udf_close_lvid(struct super_block *);
102 static unsigned int udf_count_free(struct super_block *);
103 static int udf_statfs(struct dentry *, struct kstatfs *);
104 static int udf_show_options(struct seq_file *, struct dentry *);
106 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
108 struct logicalVolIntegrityDesc *lvid;
109 unsigned int partnum;
112 if (!UDF_SB(sb)->s_lvid_bh)
114 lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
115 partnum = le32_to_cpu(lvid->numOfPartitions);
116 /* The offset is to skip freeSpaceTable and sizeTable arrays */
117 offset = partnum * 2 * sizeof(uint32_t);
118 return (struct logicalVolIntegrityDescImpUse *)
119 (((uint8_t *)(lvid + 1)) + offset);
122 /* UDF filesystem type */
123 static struct dentry *udf_mount(struct file_system_type *fs_type,
124 int flags, const char *dev_name, void *data)
126 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
129 static struct file_system_type udf_fstype = {
130 .owner = THIS_MODULE,
133 .kill_sb = kill_block_super,
134 .fs_flags = FS_REQUIRES_DEV,
136 MODULE_ALIAS_FS("udf");
138 static struct kmem_cache *udf_inode_cachep;
140 static struct inode *udf_alloc_inode(struct super_block *sb)
142 struct udf_inode_info *ei;
143 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
148 ei->i_lenExtents = 0;
149 ei->i_next_alloc_block = 0;
150 ei->i_next_alloc_goal = 0;
152 init_rwsem(&ei->i_data_sem);
153 ei->cached_extent.lstart = -1;
154 spin_lock_init(&ei->i_extent_cache_lock);
156 return &ei->vfs_inode;
159 static void udf_i_callback(struct rcu_head *head)
161 struct inode *inode = container_of(head, struct inode, i_rcu);
162 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
165 static void udf_destroy_inode(struct inode *inode)
167 call_rcu(&inode->i_rcu, udf_i_callback);
170 static void init_once(void *foo)
172 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
174 ei->i_ext.i_data = NULL;
175 inode_init_once(&ei->vfs_inode);
178 static int __init init_inodecache(void)
180 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
181 sizeof(struct udf_inode_info),
182 0, (SLAB_RECLAIM_ACCOUNT |
186 if (!udf_inode_cachep)
191 static void destroy_inodecache(void)
194 * Make sure all delayed rcu free inodes are flushed before we
198 kmem_cache_destroy(udf_inode_cachep);
201 /* Superblock operations */
202 static const struct super_operations udf_sb_ops = {
203 .alloc_inode = udf_alloc_inode,
204 .destroy_inode = udf_destroy_inode,
205 .write_inode = udf_write_inode,
206 .evict_inode = udf_evict_inode,
207 .put_super = udf_put_super,
208 .sync_fs = udf_sync_fs,
209 .statfs = udf_statfs,
210 .remount_fs = udf_remount_fs,
211 .show_options = udf_show_options,
216 unsigned int blocksize;
217 unsigned int session;
218 unsigned int lastblock;
221 unsigned short partition;
222 unsigned int fileset;
223 unsigned int rootdir;
230 struct nls_table *nls_map;
233 static int __init init_udf_fs(void)
237 err = init_inodecache();
240 err = register_filesystem(&udf_fstype);
247 destroy_inodecache();
253 static void __exit exit_udf_fs(void)
255 unregister_filesystem(&udf_fstype);
256 destroy_inodecache();
259 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
261 struct udf_sb_info *sbi = UDF_SB(sb);
263 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
264 if (!sbi->s_partmaps) {
265 sbi->s_partitions = 0;
269 sbi->s_partitions = count;
273 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
276 int nr_groups = bitmap->s_nr_groups;
278 for (i = 0; i < nr_groups; i++)
279 if (bitmap->s_block_bitmap[i])
280 brelse(bitmap->s_block_bitmap[i]);
285 static void udf_free_partition(struct udf_part_map *map)
288 struct udf_meta_data *mdata;
290 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
291 iput(map->s_uspace.s_table);
292 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
293 iput(map->s_fspace.s_table);
294 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
295 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
296 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
297 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
298 if (map->s_partition_type == UDF_SPARABLE_MAP15)
299 for (i = 0; i < 4; i++)
300 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
301 else if (map->s_partition_type == UDF_METADATA_MAP25) {
302 mdata = &map->s_type_specific.s_metadata;
303 iput(mdata->s_metadata_fe);
304 mdata->s_metadata_fe = NULL;
306 iput(mdata->s_mirror_fe);
307 mdata->s_mirror_fe = NULL;
309 iput(mdata->s_bitmap_fe);
310 mdata->s_bitmap_fe = NULL;
314 static void udf_sb_free_partitions(struct super_block *sb)
316 struct udf_sb_info *sbi = UDF_SB(sb);
319 if (!sbi->s_partmaps)
321 for (i = 0; i < sbi->s_partitions; i++)
322 udf_free_partition(&sbi->s_partmaps[i]);
323 kfree(sbi->s_partmaps);
324 sbi->s_partmaps = NULL;
327 static int udf_show_options(struct seq_file *seq, struct dentry *root)
329 struct super_block *sb = root->d_sb;
330 struct udf_sb_info *sbi = UDF_SB(sb);
332 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
333 seq_puts(seq, ",nostrict");
334 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
335 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
336 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
337 seq_puts(seq, ",unhide");
338 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
339 seq_puts(seq, ",undelete");
340 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
341 seq_puts(seq, ",noadinicb");
342 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
343 seq_puts(seq, ",shortad");
344 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
345 seq_puts(seq, ",uid=forget");
346 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
347 seq_puts(seq, ",uid=ignore");
348 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
349 seq_puts(seq, ",gid=forget");
350 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
351 seq_puts(seq, ",gid=ignore");
352 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
353 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
354 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
355 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
356 if (sbi->s_umask != 0)
357 seq_printf(seq, ",umask=%ho", sbi->s_umask);
358 if (sbi->s_fmode != UDF_INVALID_MODE)
359 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
360 if (sbi->s_dmode != UDF_INVALID_MODE)
361 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
362 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
363 seq_printf(seq, ",session=%u", sbi->s_session);
364 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
365 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
366 if (sbi->s_anchor != 0)
367 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
369 * volume, partition, fileset and rootdir seem to be ignored
372 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
373 seq_puts(seq, ",utf8");
374 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
375 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
384 * Parse mount options.
387 * The following mount options are supported:
389 * gid= Set the default group.
390 * umask= Set the default umask.
391 * mode= Set the default file permissions.
392 * dmode= Set the default directory permissions.
393 * uid= Set the default user.
394 * bs= Set the block size.
395 * unhide Show otherwise hidden files.
396 * undelete Show deleted files in lists.
397 * adinicb Embed data in the inode (default)
398 * noadinicb Don't embed data in the inode
399 * shortad Use short ad's
400 * longad Use long ad's (default)
401 * nostrict Unset strict conformance
402 * iocharset= Set the NLS character set
404 * The remaining are for debugging and disaster recovery:
406 * novrs Skip volume sequence recognition
408 * The following expect a offset from 0.
410 * session= Set the CDROM session (default= last session)
411 * anchor= Override standard anchor location. (default= 256)
412 * volume= Override the VolumeDesc location. (unused)
413 * partition= Override the PartitionDesc location. (unused)
414 * lastblock= Set the last block of the filesystem/
416 * The following expect a offset from the partition root.
418 * fileset= Override the fileset block location. (unused)
419 * rootdir= Override the root directory location. (unused)
420 * WARNING: overriding the rootdir to a non-directory may
421 * yield highly unpredictable results.
424 * options Pointer to mount options string.
425 * uopts Pointer to mount options variable.
428 * <return> 1 Mount options parsed okay.
429 * <return> 0 Error parsing mount options.
432 * July 1, 1997 - Andrew E. Mileski
433 * Written, tested, and released.
437 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
438 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
439 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
440 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
441 Opt_rootdir, Opt_utf8, Opt_iocharset,
442 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
446 static const match_table_t tokens = {
447 {Opt_novrs, "novrs"},
448 {Opt_nostrict, "nostrict"},
450 {Opt_unhide, "unhide"},
451 {Opt_undelete, "undelete"},
452 {Opt_noadinicb, "noadinicb"},
453 {Opt_adinicb, "adinicb"},
454 {Opt_shortad, "shortad"},
455 {Opt_longad, "longad"},
456 {Opt_uforget, "uid=forget"},
457 {Opt_uignore, "uid=ignore"},
458 {Opt_gforget, "gid=forget"},
459 {Opt_gignore, "gid=ignore"},
462 {Opt_umask, "umask=%o"},
463 {Opt_session, "session=%u"},
464 {Opt_lastblock, "lastblock=%u"},
465 {Opt_anchor, "anchor=%u"},
466 {Opt_volume, "volume=%u"},
467 {Opt_partition, "partition=%u"},
468 {Opt_fileset, "fileset=%u"},
469 {Opt_rootdir, "rootdir=%u"},
471 {Opt_iocharset, "iocharset=%s"},
472 {Opt_fmode, "mode=%o"},
473 {Opt_dmode, "dmode=%o"},
477 static int udf_parse_options(char *options, struct udf_options *uopt,
484 uopt->partition = 0xFFFF;
485 uopt->session = 0xFFFFFFFF;
488 uopt->volume = 0xFFFFFFFF;
489 uopt->rootdir = 0xFFFFFFFF;
490 uopt->fileset = 0xFFFFFFFF;
491 uopt->nls_map = NULL;
496 while ((p = strsep(&options, ",")) != NULL) {
497 substring_t args[MAX_OPT_ARGS];
503 token = match_token(p, tokens, args);
509 if (match_int(&args[0], &option))
512 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
515 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
518 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
521 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
524 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
527 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
530 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
533 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
536 if (match_int(args, &option))
538 uopt->gid = make_kgid(current_user_ns(), option);
539 if (!gid_valid(uopt->gid))
541 uopt->flags |= (1 << UDF_FLAG_GID_SET);
544 if (match_int(args, &option))
546 uopt->uid = make_kuid(current_user_ns(), option);
547 if (!uid_valid(uopt->uid))
549 uopt->flags |= (1 << UDF_FLAG_UID_SET);
552 if (match_octal(args, &option))
554 uopt->umask = option;
557 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
560 if (match_int(args, &option))
562 uopt->session = option;
564 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
567 if (match_int(args, &option))
569 uopt->lastblock = option;
571 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
574 if (match_int(args, &option))
576 uopt->anchor = option;
579 if (match_int(args, &option))
581 uopt->volume = option;
584 if (match_int(args, &option))
586 uopt->partition = option;
589 if (match_int(args, &option))
591 uopt->fileset = option;
594 if (match_int(args, &option))
596 uopt->rootdir = option;
599 uopt->flags |= (1 << UDF_FLAG_UTF8);
601 #ifdef CONFIG_UDF_NLS
603 uopt->nls_map = load_nls(args[0].from);
604 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
608 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
611 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
614 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
617 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
620 if (match_octal(args, &option))
622 uopt->fmode = option & 0777;
625 if (match_octal(args, &option))
627 uopt->dmode = option & 0777;
630 pr_err("bad mount option \"%s\" or missing value\n", p);
637 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
639 struct udf_options uopt;
640 struct udf_sb_info *sbi = UDF_SB(sb);
642 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
646 int write_rev = le16_to_cpu(lvidiu->minUDFWriteRev);
647 if (write_rev > UDF_MAX_WRITE_VERSION && !(*flags & MS_RDONLY))
651 uopt.flags = sbi->s_flags;
652 uopt.uid = sbi->s_uid;
653 uopt.gid = sbi->s_gid;
654 uopt.umask = sbi->s_umask;
655 uopt.fmode = sbi->s_fmode;
656 uopt.dmode = sbi->s_dmode;
658 if (!udf_parse_options(options, &uopt, true))
661 write_lock(&sbi->s_cred_lock);
662 sbi->s_flags = uopt.flags;
663 sbi->s_uid = uopt.uid;
664 sbi->s_gid = uopt.gid;
665 sbi->s_umask = uopt.umask;
666 sbi->s_fmode = uopt.fmode;
667 sbi->s_dmode = uopt.dmode;
668 write_unlock(&sbi->s_cred_lock);
670 if ((bool)(*flags & MS_RDONLY) == sb_rdonly(sb))
673 if (*flags & MS_RDONLY)
682 /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
683 /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
684 static loff_t udf_check_vsd(struct super_block *sb)
686 struct volStructDesc *vsd = NULL;
687 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
689 struct buffer_head *bh = NULL;
692 struct udf_sb_info *sbi;
695 if (sb->s_blocksize < sizeof(struct volStructDesc))
696 sectorsize = sizeof(struct volStructDesc);
698 sectorsize = sb->s_blocksize;
700 sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits);
702 udf_debug("Starting at sector %u (%ld byte sectors)\n",
703 (unsigned int)(sector >> sb->s_blocksize_bits),
705 /* Process the sequence (if applicable). The hard limit on the sector
706 * offset is arbitrary, hopefully large enough so that all valid UDF
707 * filesystems will be recognised. There is no mention of an upper
708 * bound to the size of the volume recognition area in the standard.
709 * The limit will prevent the code to read all the sectors of a
710 * specially crafted image (like a bluray disc full of CD001 sectors),
711 * potentially causing minutes or even hours of uninterruptible I/O
712 * activity. This actually happened with uninitialised SSD partitions
713 * (all 0xFF) before the check for the limit and all valid IDs were
715 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
716 sector += sectorsize) {
718 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
722 /* Look for ISO descriptors */
723 vsd = (struct volStructDesc *)(bh->b_data +
724 (sector & (sb->s_blocksize - 1)));
726 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
728 switch (vsd->structType) {
730 udf_debug("ISO9660 Boot Record found\n");
733 udf_debug("ISO9660 Primary Volume Descriptor found\n");
736 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
739 udf_debug("ISO9660 Volume Partition Descriptor found\n");
742 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
745 udf_debug("ISO9660 VRS (%u) found\n",
749 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
752 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
756 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
759 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
762 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
765 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
769 /* invalid id : end of volume recognition area */
780 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
781 VSD_FIRST_SECTOR_OFFSET)
787 static int udf_find_fileset(struct super_block *sb,
788 struct kernel_lb_addr *fileset,
789 struct kernel_lb_addr *root)
791 struct buffer_head *bh = NULL;
794 struct udf_sb_info *sbi;
796 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
797 fileset->partitionReferenceNum != 0xFFFF) {
798 bh = udf_read_ptagged(sb, fileset, 0, &ident);
802 } else if (ident != TAG_IDENT_FSD) {
811 /* Search backwards through the partitions */
812 struct kernel_lb_addr newfileset;
814 /* --> cvg: FIXME - is it reasonable? */
817 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
818 (newfileset.partitionReferenceNum != 0xFFFF &&
819 fileset->logicalBlockNum == 0xFFFFFFFF &&
820 fileset->partitionReferenceNum == 0xFFFF);
821 newfileset.partitionReferenceNum--) {
822 lastblock = sbi->s_partmaps
823 [newfileset.partitionReferenceNum]
825 newfileset.logicalBlockNum = 0;
828 bh = udf_read_ptagged(sb, &newfileset, 0,
831 newfileset.logicalBlockNum++;
838 struct spaceBitmapDesc *sp;
839 sp = (struct spaceBitmapDesc *)
841 newfileset.logicalBlockNum += 1 +
842 ((le32_to_cpu(sp->numOfBytes) +
843 sizeof(struct spaceBitmapDesc)
844 - 1) >> sb->s_blocksize_bits);
849 *fileset = newfileset;
852 newfileset.logicalBlockNum++;
857 } while (newfileset.logicalBlockNum < lastblock &&
858 fileset->logicalBlockNum == 0xFFFFFFFF &&
859 fileset->partitionReferenceNum == 0xFFFF);
863 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
864 fileset->partitionReferenceNum != 0xFFFF) && bh) {
865 udf_debug("Fileset at block=%d, partition=%d\n",
866 fileset->logicalBlockNum,
867 fileset->partitionReferenceNum);
869 sbi->s_partition = fileset->partitionReferenceNum;
870 udf_load_fileset(sb, bh, root);
878 * Load primary Volume Descriptor Sequence
880 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
883 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
885 struct primaryVolDesc *pvoldesc;
887 struct buffer_head *bh;
891 outstr = kmalloc(128, GFP_NOFS);
895 bh = udf_read_tagged(sb, block, block, &ident);
901 if (ident != TAG_IDENT_PVD) {
906 pvoldesc = (struct primaryVolDesc *)bh->b_data;
908 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
909 pvoldesc->recordingDateAndTime)) {
911 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
912 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
913 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
914 ts->minute, le16_to_cpu(ts->typeAndTimezone));
918 ret = udf_dstrCS0toUTF8(outstr, 31, pvoldesc->volIdent, 32);
920 strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName");
921 pr_warn("incorrect volume identification, setting to "
924 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
926 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
928 ret = udf_dstrCS0toUTF8(outstr, 127, pvoldesc->volSetIdent, 128);
934 udf_debug("volSetIdent[] = '%s'\n", outstr);
944 struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
945 u32 meta_file_loc, u32 partition_ref)
947 struct kernel_lb_addr addr;
948 struct inode *metadata_fe;
950 addr.logicalBlockNum = meta_file_loc;
951 addr.partitionReferenceNum = partition_ref;
953 metadata_fe = udf_iget_special(sb, &addr);
955 if (IS_ERR(metadata_fe)) {
956 udf_warn(sb, "metadata inode efe not found\n");
959 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
960 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
962 return ERR_PTR(-EIO);
968 static int udf_load_metadata_files(struct super_block *sb, int partition,
971 struct udf_sb_info *sbi = UDF_SB(sb);
972 struct udf_part_map *map;
973 struct udf_meta_data *mdata;
974 struct kernel_lb_addr addr;
977 map = &sbi->s_partmaps[partition];
978 mdata = &map->s_type_specific.s_metadata;
979 mdata->s_phys_partition_ref = type1_index;
981 /* metadata address */
982 udf_debug("Metadata file location: block = %d part = %d\n",
983 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
985 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
986 mdata->s_phys_partition_ref);
988 /* mirror file entry */
989 udf_debug("Mirror metadata file location: block = %d part = %d\n",
990 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
992 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
993 mdata->s_phys_partition_ref);
996 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
999 mdata->s_mirror_fe = fe;
1001 mdata->s_metadata_fe = fe;
1007 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1009 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1010 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1011 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
1013 udf_debug("Bitmap file location: block = %d part = %d\n",
1014 addr.logicalBlockNum, addr.partitionReferenceNum);
1016 fe = udf_iget_special(sb, &addr);
1019 udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
1021 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
1025 mdata->s_bitmap_fe = fe;
1028 udf_debug("udf_load_metadata_files Ok\n");
1032 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1033 struct kernel_lb_addr *root)
1035 struct fileSetDesc *fset;
1037 fset = (struct fileSetDesc *)bh->b_data;
1039 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1041 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1043 udf_debug("Rootdir at block=%d, partition=%d\n",
1044 root->logicalBlockNum, root->partitionReferenceNum);
1047 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1049 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1050 return DIV_ROUND_UP(map->s_partition_len +
1051 (sizeof(struct spaceBitmapDesc) << 3),
1052 sb->s_blocksize * 8);
1055 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1057 struct udf_bitmap *bitmap;
1061 nr_groups = udf_compute_nr_groups(sb, index);
1062 size = sizeof(struct udf_bitmap) +
1063 (sizeof(struct buffer_head *) * nr_groups);
1065 if (size <= PAGE_SIZE)
1066 bitmap = kzalloc(size, GFP_KERNEL);
1068 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
1073 bitmap->s_nr_groups = nr_groups;
1077 static int udf_fill_partdesc_info(struct super_block *sb,
1078 struct partitionDesc *p, int p_index)
1080 struct udf_part_map *map;
1081 struct udf_sb_info *sbi = UDF_SB(sb);
1082 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 %d, block length %d\n",
1099 p_index, map->s_partition_type,
1100 map->s_partition_root, map->s_partition_len);
1102 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1103 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1106 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1107 if (phd->unallocSpaceTable.extLength) {
1108 struct kernel_lb_addr loc = {
1109 .logicalBlockNum = le32_to_cpu(
1110 phd->unallocSpaceTable.extPosition),
1111 .partitionReferenceNum = p_index,
1113 struct inode *inode;
1115 inode = udf_iget_special(sb, &loc);
1116 if (IS_ERR(inode)) {
1117 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1119 return PTR_ERR(inode);
1121 map->s_uspace.s_table = inode;
1122 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1123 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1124 p_index, map->s_uspace.s_table->i_ino);
1127 if (phd->unallocSpaceBitmap.extLength) {
1128 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1131 map->s_uspace.s_bitmap = bitmap;
1132 bitmap->s_extPosition = le32_to_cpu(
1133 phd->unallocSpaceBitmap.extPosition);
1134 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1135 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1136 p_index, bitmap->s_extPosition);
1139 if (phd->partitionIntegrityTable.extLength)
1140 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1142 if (phd->freedSpaceTable.extLength) {
1143 struct kernel_lb_addr loc = {
1144 .logicalBlockNum = le32_to_cpu(
1145 phd->freedSpaceTable.extPosition),
1146 .partitionReferenceNum = p_index,
1148 struct inode *inode;
1150 inode = udf_iget_special(sb, &loc);
1151 if (IS_ERR(inode)) {
1152 udf_debug("cannot load freedSpaceTable (part %d)\n",
1154 return PTR_ERR(inode);
1156 map->s_fspace.s_table = inode;
1157 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1158 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1159 p_index, map->s_fspace.s_table->i_ino);
1162 if (phd->freedSpaceBitmap.extLength) {
1163 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1166 map->s_fspace.s_bitmap = bitmap;
1167 bitmap->s_extPosition = le32_to_cpu(
1168 phd->freedSpaceBitmap.extPosition);
1169 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1170 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1171 p_index, bitmap->s_extPosition);
1176 static void udf_find_vat_block(struct super_block *sb, int p_index,
1177 int type1_index, sector_t start_block)
1179 struct udf_sb_info *sbi = UDF_SB(sb);
1180 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1182 struct kernel_lb_addr ino;
1183 struct inode *inode;
1186 * VAT file entry is in the last recorded block. Some broken disks have
1187 * it a few blocks before so try a bit harder...
1189 ino.partitionReferenceNum = type1_index;
1190 for (vat_block = start_block;
1191 vat_block >= map->s_partition_root &&
1192 vat_block >= start_block - 3; vat_block--) {
1193 ino.logicalBlockNum = vat_block - map->s_partition_root;
1194 inode = udf_iget_special(sb, &ino);
1195 if (!IS_ERR(inode)) {
1196 sbi->s_vat_inode = inode;
1202 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1204 struct udf_sb_info *sbi = UDF_SB(sb);
1205 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1206 struct buffer_head *bh = NULL;
1207 struct udf_inode_info *vati;
1209 struct virtualAllocationTable20 *vat20;
1210 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1211 sb->s_blocksize_bits;
1213 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
1214 if (!sbi->s_vat_inode &&
1215 sbi->s_last_block != blocks - 1) {
1216 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1217 (unsigned long)sbi->s_last_block,
1218 (unsigned long)blocks - 1);
1219 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
1221 if (!sbi->s_vat_inode)
1224 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1225 map->s_type_specific.s_virtual.s_start_offset = 0;
1226 map->s_type_specific.s_virtual.s_num_entries =
1227 (sbi->s_vat_inode->i_size - 36) >> 2;
1228 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1229 vati = UDF_I(sbi->s_vat_inode);
1230 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1231 pos = udf_block_map(sbi->s_vat_inode, 0);
1232 bh = sb_bread(sb, pos);
1235 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1237 vat20 = (struct virtualAllocationTable20 *)
1241 map->s_type_specific.s_virtual.s_start_offset =
1242 le16_to_cpu(vat20->lengthHeader);
1243 map->s_type_specific.s_virtual.s_num_entries =
1244 (sbi->s_vat_inode->i_size -
1245 map->s_type_specific.s_virtual.
1246 s_start_offset) >> 2;
1253 * Load partition descriptor block
1255 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1258 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1260 struct buffer_head *bh;
1261 struct partitionDesc *p;
1262 struct udf_part_map *map;
1263 struct udf_sb_info *sbi = UDF_SB(sb);
1265 uint16_t partitionNumber;
1269 bh = udf_read_tagged(sb, block, block, &ident);
1272 if (ident != TAG_IDENT_PD) {
1277 p = (struct partitionDesc *)bh->b_data;
1278 partitionNumber = le16_to_cpu(p->partitionNumber);
1280 /* First scan for TYPE1 and SPARABLE partitions */
1281 for (i = 0; i < sbi->s_partitions; i++) {
1282 map = &sbi->s_partmaps[i];
1283 udf_debug("Searching map: (%d == %d)\n",
1284 map->s_partition_num, partitionNumber);
1285 if (map->s_partition_num == partitionNumber &&
1286 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1287 map->s_partition_type == UDF_SPARABLE_MAP15))
1291 if (i >= sbi->s_partitions) {
1292 udf_debug("Partition (%d) not found in partition map\n",
1298 ret = udf_fill_partdesc_info(sb, p, i);
1303 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1304 * PHYSICAL partitions are already set up
1308 map = NULL; /* supress 'maybe used uninitialized' warning */
1310 for (i = 0; i < sbi->s_partitions; i++) {
1311 map = &sbi->s_partmaps[i];
1313 if (map->s_partition_num == partitionNumber &&
1314 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1315 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1316 map->s_partition_type == UDF_METADATA_MAP25))
1320 if (i >= sbi->s_partitions) {
1325 ret = udf_fill_partdesc_info(sb, p, i);
1329 if (map->s_partition_type == UDF_METADATA_MAP25) {
1330 ret = udf_load_metadata_files(sb, i, type1_idx);
1332 udf_err(sb, "error loading MetaData partition map %d\n",
1338 * If we have a partition with virtual map, we don't handle
1339 * writing to it (we overwrite blocks instead of relocating
1342 if (!sb_rdonly(sb)) {
1346 ret = udf_load_vat(sb, i, type1_idx);
1352 /* In case loading failed, we handle cleanup in udf_fill_super */
1357 static int udf_load_sparable_map(struct super_block *sb,
1358 struct udf_part_map *map,
1359 struct sparablePartitionMap *spm)
1363 struct sparingTable *st;
1364 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1366 struct buffer_head *bh;
1368 map->s_partition_type = UDF_SPARABLE_MAP15;
1369 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1370 if (!is_power_of_2(sdata->s_packet_len)) {
1371 udf_err(sb, "error loading logical volume descriptor: "
1372 "Invalid packet length %u\n",
1373 (unsigned)sdata->s_packet_len);
1376 if (spm->numSparingTables > 4) {
1377 udf_err(sb, "error loading logical volume descriptor: "
1378 "Too many sparing tables (%d)\n",
1379 (int)spm->numSparingTables);
1382 if (le32_to_cpu(spm->sizeSparingTable) > sb->s_blocksize) {
1383 udf_err(sb, "error loading logical volume descriptor: "
1384 "Too big sparing table size (%u)\n",
1385 le32_to_cpu(spm->sizeSparingTable));
1389 for (i = 0; i < spm->numSparingTables; i++) {
1390 loc = le32_to_cpu(spm->locSparingTable[i]);
1391 bh = udf_read_tagged(sb, loc, loc, &ident);
1395 st = (struct sparingTable *)bh->b_data;
1397 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1398 strlen(UDF_ID_SPARING)) ||
1399 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1405 sdata->s_spar_map[i] = bh;
1407 map->s_partition_func = udf_get_pblock_spar15;
1411 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1412 struct kernel_lb_addr *fileset)
1414 struct logicalVolDesc *lvd;
1417 struct udf_sb_info *sbi = UDF_SB(sb);
1418 struct genericPartitionMap *gpm;
1420 struct buffer_head *bh;
1421 unsigned int table_len;
1424 bh = udf_read_tagged(sb, block, block, &ident);
1427 BUG_ON(ident != TAG_IDENT_LVD);
1428 lvd = (struct logicalVolDesc *)bh->b_data;
1429 table_len = le32_to_cpu(lvd->mapTableLength);
1430 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
1431 udf_err(sb, "error loading logical volume descriptor: "
1432 "Partition table too long (%u > %lu)\n", table_len,
1433 sb->s_blocksize - sizeof(*lvd));
1438 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1442 for (i = 0, offset = 0;
1443 i < sbi->s_partitions && offset < table_len;
1444 i++, offset += gpm->partitionMapLength) {
1445 struct udf_part_map *map = &sbi->s_partmaps[i];
1446 gpm = (struct genericPartitionMap *)
1447 &(lvd->partitionMaps[offset]);
1448 type = gpm->partitionMapType;
1450 struct genericPartitionMap1 *gpm1 =
1451 (struct genericPartitionMap1 *)gpm;
1452 map->s_partition_type = UDF_TYPE1_MAP15;
1453 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1454 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1455 map->s_partition_func = NULL;
1456 } else if (type == 2) {
1457 struct udfPartitionMap2 *upm2 =
1458 (struct udfPartitionMap2 *)gpm;
1459 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1460 strlen(UDF_ID_VIRTUAL))) {
1462 le16_to_cpu(((__le16 *)upm2->partIdent.
1465 map->s_partition_type =
1467 map->s_partition_func =
1468 udf_get_pblock_virt15;
1470 map->s_partition_type =
1472 map->s_partition_func =
1473 udf_get_pblock_virt20;
1475 } else if (!strncmp(upm2->partIdent.ident,
1477 strlen(UDF_ID_SPARABLE))) {
1478 ret = udf_load_sparable_map(sb, map,
1479 (struct sparablePartitionMap *)gpm);
1482 } else if (!strncmp(upm2->partIdent.ident,
1484 strlen(UDF_ID_METADATA))) {
1485 struct udf_meta_data *mdata =
1486 &map->s_type_specific.s_metadata;
1487 struct metadataPartitionMap *mdm =
1488 (struct metadataPartitionMap *)
1489 &(lvd->partitionMaps[offset]);
1490 udf_debug("Parsing Logical vol part %d type %d id=%s\n",
1491 i, type, UDF_ID_METADATA);
1493 map->s_partition_type = UDF_METADATA_MAP25;
1494 map->s_partition_func = udf_get_pblock_meta25;
1496 mdata->s_meta_file_loc =
1497 le32_to_cpu(mdm->metadataFileLoc);
1498 mdata->s_mirror_file_loc =
1499 le32_to_cpu(mdm->metadataMirrorFileLoc);
1500 mdata->s_bitmap_file_loc =
1501 le32_to_cpu(mdm->metadataBitmapFileLoc);
1502 mdata->s_alloc_unit_size =
1503 le32_to_cpu(mdm->allocUnitSize);
1504 mdata->s_align_unit_size =
1505 le16_to_cpu(mdm->alignUnitSize);
1506 if (mdm->flags & 0x01)
1507 mdata->s_flags |= MF_DUPLICATE_MD;
1509 udf_debug("Metadata Ident suffix=0x%x\n",
1510 le16_to_cpu(*(__le16 *)
1511 mdm->partIdent.identSuffix));
1512 udf_debug("Metadata part num=%d\n",
1513 le16_to_cpu(mdm->partitionNum));
1514 udf_debug("Metadata part alloc unit size=%d\n",
1515 le32_to_cpu(mdm->allocUnitSize));
1516 udf_debug("Metadata file loc=%d\n",
1517 le32_to_cpu(mdm->metadataFileLoc));
1518 udf_debug("Mirror file loc=%d\n",
1519 le32_to_cpu(mdm->metadataMirrorFileLoc));
1520 udf_debug("Bitmap file loc=%d\n",
1521 le32_to_cpu(mdm->metadataBitmapFileLoc));
1522 udf_debug("Flags: %d %d\n",
1523 mdata->s_flags, mdm->flags);
1525 udf_debug("Unknown ident: %s\n",
1526 upm2->partIdent.ident);
1529 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1530 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1532 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1533 i, map->s_partition_num, type, map->s_volumeseqnum);
1537 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1539 *fileset = lelb_to_cpu(la->extLocation);
1540 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1541 fileset->logicalBlockNum,
1542 fileset->partitionReferenceNum);
1544 if (lvd->integritySeqExt.extLength)
1545 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1553 * Find the prevailing Logical Volume Integrity Descriptor.
1555 static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1557 struct buffer_head *bh, *final_bh;
1559 struct udf_sb_info *sbi = UDF_SB(sb);
1560 struct logicalVolIntegrityDesc *lvid;
1561 int indirections = 0;
1562 u32 parts, impuselen;
1564 while (++indirections <= UDF_MAX_LVID_NESTING) {
1566 while (loc.extLength > 0 &&
1567 (bh = udf_read_tagged(sb, loc.extLocation,
1568 loc.extLocation, &ident))) {
1569 if (ident != TAG_IDENT_LVID) {
1577 loc.extLength -= sb->s_blocksize;
1584 brelse(sbi->s_lvid_bh);
1585 sbi->s_lvid_bh = final_bh;
1587 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1588 if (lvid->nextIntegrityExt.extLength == 0)
1591 loc = leea_to_cpu(lvid->nextIntegrityExt);
1594 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1595 UDF_MAX_LVID_NESTING);
1597 brelse(sbi->s_lvid_bh);
1598 sbi->s_lvid_bh = NULL;
1601 parts = le32_to_cpu(lvid->numOfPartitions);
1602 impuselen = le32_to_cpu(lvid->lengthOfImpUse);
1603 if (parts >= sb->s_blocksize || impuselen >= sb->s_blocksize ||
1604 sizeof(struct logicalVolIntegrityDesc) + impuselen +
1605 2 * parts * sizeof(u32) > sb->s_blocksize) {
1606 udf_warn(sb, "Corrupted LVID (parts=%u, impuselen=%u), "
1607 "ignoring.\n", parts, impuselen);
1614 * Process a main/reserve volume descriptor sequence.
1615 * @block First block of first extent of the sequence.
1616 * @lastblock Lastblock of first extent of the sequence.
1617 * @fileset There we store extent containing root fileset
1619 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1622 static noinline int udf_process_sequence(
1623 struct super_block *sb,
1624 sector_t block, sector_t lastblock,
1625 struct kernel_lb_addr *fileset)
1627 struct buffer_head *bh = NULL;
1628 struct udf_vds_record vds[VDS_POS_LENGTH];
1629 struct udf_vds_record *curr;
1630 struct generic_desc *gd;
1631 struct volDescPtr *vdp;
1635 long next_s = 0, next_e = 0;
1637 unsigned int indirections = 0;
1639 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1642 * Read the main descriptor sequence and find which descriptors
1645 for (; (!done && block <= lastblock); block++) {
1647 bh = udf_read_tagged(sb, block, block, &ident);
1650 "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
1651 (unsigned long long)block);
1655 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1656 gd = (struct generic_desc *)bh->b_data;
1657 vdsn = le32_to_cpu(gd->volDescSeqNum);
1659 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1660 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1661 if (vdsn >= curr->volDescSeqNum) {
1662 curr->volDescSeqNum = vdsn;
1663 curr->block = block;
1666 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1667 curr = &vds[VDS_POS_VOL_DESC_PTR];
1668 if (vdsn >= curr->volDescSeqNum) {
1669 curr->volDescSeqNum = vdsn;
1670 curr->block = block;
1672 vdp = (struct volDescPtr *)bh->b_data;
1673 next_s = le32_to_cpu(
1674 vdp->nextVolDescSeqExt.extLocation);
1675 next_e = le32_to_cpu(
1676 vdp->nextVolDescSeqExt.extLength);
1677 next_e = next_e >> sb->s_blocksize_bits;
1681 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1682 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1683 if (vdsn >= curr->volDescSeqNum) {
1684 curr->volDescSeqNum = vdsn;
1685 curr->block = block;
1688 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1689 curr = &vds[VDS_POS_PARTITION_DESC];
1691 curr->block = block;
1693 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1694 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1695 if (vdsn >= curr->volDescSeqNum) {
1696 curr->volDescSeqNum = vdsn;
1697 curr->block = block;
1700 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1701 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1702 if (vdsn >= curr->volDescSeqNum) {
1703 curr->volDescSeqNum = vdsn;
1704 curr->block = block;
1707 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1708 if (++indirections > UDF_MAX_TD_NESTING) {
1709 udf_err(sb, "too many TDs (max %u supported)\n", UDF_MAX_TD_NESTING);
1714 vds[VDS_POS_TERMINATING_DESC].block = block;
1718 next_s = next_e = 0;
1726 * Now read interesting descriptors again and process them
1727 * in a suitable order
1729 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1730 udf_err(sb, "Primary Volume Descriptor not found!\n");
1733 ret = udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block);
1737 if (vds[VDS_POS_LOGICAL_VOL_DESC].block) {
1738 ret = udf_load_logicalvol(sb,
1739 vds[VDS_POS_LOGICAL_VOL_DESC].block,
1745 if (vds[VDS_POS_PARTITION_DESC].block) {
1747 * We rescan the whole descriptor sequence to find
1748 * partition descriptor blocks and process them.
1750 for (block = vds[VDS_POS_PARTITION_DESC].block;
1751 block < vds[VDS_POS_TERMINATING_DESC].block;
1753 ret = udf_load_partdesc(sb, block);
1763 * Load Volume Descriptor Sequence described by anchor in bh
1765 * Returns <0 on error, 0 on success
1767 static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1768 struct kernel_lb_addr *fileset)
1770 struct anchorVolDescPtr *anchor;
1771 sector_t main_s, main_e, reserve_s, reserve_e;
1774 anchor = (struct anchorVolDescPtr *)bh->b_data;
1776 /* Locate the main sequence */
1777 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1778 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1779 main_e = main_e >> sb->s_blocksize_bits;
1782 /* Locate the reserve sequence */
1783 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1784 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1785 reserve_e = reserve_e >> sb->s_blocksize_bits;
1786 reserve_e += reserve_s;
1788 /* Process the main & reserve sequences */
1789 /* responsible for finding the PartitionDesc(s) */
1790 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1793 udf_sb_free_partitions(sb);
1794 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1796 udf_sb_free_partitions(sb);
1797 /* No sequence was OK, return -EIO */
1805 * Check whether there is an anchor block in the given block and
1806 * load Volume Descriptor Sequence if so.
1808 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1811 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1812 struct kernel_lb_addr *fileset)
1814 struct buffer_head *bh;
1818 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1819 udf_fixed_to_variable(block) >=
1820 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
1823 bh = udf_read_tagged(sb, block, block, &ident);
1826 if (ident != TAG_IDENT_AVDP) {
1830 ret = udf_load_sequence(sb, bh, fileset);
1836 * Search for an anchor volume descriptor pointer.
1838 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1841 static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1842 struct kernel_lb_addr *fileset)
1846 struct udf_sb_info *sbi = UDF_SB(sb);
1850 /* First try user provided anchor */
1851 if (sbi->s_anchor) {
1852 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1857 * according to spec, anchor is in either:
1861 * however, if the disc isn't closed, it could be 512.
1863 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1867 * The trouble is which block is the last one. Drives often misreport
1868 * this so we try various possibilities.
1870 last[last_count++] = *lastblock;
1871 if (*lastblock >= 1)
1872 last[last_count++] = *lastblock - 1;
1873 last[last_count++] = *lastblock + 1;
1874 if (*lastblock >= 2)
1875 last[last_count++] = *lastblock - 2;
1876 if (*lastblock >= 150)
1877 last[last_count++] = *lastblock - 150;
1878 if (*lastblock >= 152)
1879 last[last_count++] = *lastblock - 152;
1881 for (i = 0; i < last_count; i++) {
1882 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
1883 sb->s_blocksize_bits)
1885 ret = udf_check_anchor_block(sb, last[i], fileset);
1886 if (ret != -EAGAIN) {
1888 *lastblock = last[i];
1893 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1894 if (ret != -EAGAIN) {
1896 *lastblock = last[i];
1901 /* Finally try block 512 in case media is open */
1902 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
1906 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1907 * area specified by it. The function expects sbi->s_lastblock to be the last
1908 * block on the media.
1910 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1913 static int udf_find_anchor(struct super_block *sb,
1914 struct kernel_lb_addr *fileset)
1916 struct udf_sb_info *sbi = UDF_SB(sb);
1917 sector_t lastblock = sbi->s_last_block;
1920 ret = udf_scan_anchors(sb, &lastblock, fileset);
1924 /* No anchor found? Try VARCONV conversion of block numbers */
1925 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1926 lastblock = udf_variable_to_fixed(sbi->s_last_block);
1927 /* Firstly, we try to not convert number of the last block */
1928 ret = udf_scan_anchors(sb, &lastblock, fileset);
1932 lastblock = sbi->s_last_block;
1933 /* Secondly, we try with converted number of the last block */
1934 ret = udf_scan_anchors(sb, &lastblock, fileset);
1936 /* VARCONV didn't help. Clear it. */
1937 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1941 sbi->s_last_block = lastblock;
1946 * Check Volume Structure Descriptor, find Anchor block and load Volume
1947 * Descriptor Sequence.
1949 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1950 * block was not found.
1952 static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1953 int silent, struct kernel_lb_addr *fileset)
1955 struct udf_sb_info *sbi = UDF_SB(sb);
1959 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1961 udf_warn(sb, "Bad block size\n");
1964 sbi->s_last_block = uopt->lastblock;
1966 /* Check that it is NSR02 compliant */
1967 nsr_off = udf_check_vsd(sb);
1970 udf_warn(sb, "No VRS found\n");
1974 udf_debug("Failed to read sector at offset %d. "
1975 "Assuming open disc. Skipping validity "
1976 "check\n", VSD_FIRST_SECTOR_OFFSET);
1977 if (!sbi->s_last_block)
1978 sbi->s_last_block = udf_get_last_block(sb);
1980 udf_debug("Validity check skipped because of novrs option\n");
1983 /* Look for anchor block and load Volume Descriptor Sequence */
1984 sbi->s_anchor = uopt->anchor;
1985 ret = udf_find_anchor(sb, fileset);
1987 if (!silent && ret == -EAGAIN)
1988 udf_warn(sb, "No anchor found\n");
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;
2004 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2005 lvidiu = udf_sb_lvidiu(sb);
2009 mutex_lock(&sbi->s_alloc_mutex);
2010 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2011 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2012 ktime_get_real_ts(&ts);
2013 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
2014 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
2016 lvid->descTag.descCRC = cpu_to_le16(
2017 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
2018 le16_to_cpu(lvid->descTag.descCRCLength)));
2020 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2021 mark_buffer_dirty(bh);
2022 sbi->s_lvid_dirty = 0;
2023 mutex_unlock(&sbi->s_alloc_mutex);
2024 /* Make opening of filesystem visible on the media immediately */
2025 sync_dirty_buffer(bh);
2028 static void udf_close_lvid(struct super_block *sb)
2030 struct udf_sb_info *sbi = UDF_SB(sb);
2031 struct buffer_head *bh = sbi->s_lvid_bh;
2032 struct logicalVolIntegrityDesc *lvid;
2033 struct logicalVolIntegrityDescImpUse *lvidiu;
2038 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2039 lvidiu = udf_sb_lvidiu(sb);
2043 mutex_lock(&sbi->s_alloc_mutex);
2044 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2045 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2046 ktime_get_real_ts(&ts);
2047 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
2048 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2049 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2050 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2051 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2052 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2053 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
2054 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
2056 lvid->descTag.descCRC = cpu_to_le16(
2057 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
2058 le16_to_cpu(lvid->descTag.descCRCLength)));
2060 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2062 * We set buffer uptodate unconditionally here to avoid spurious
2063 * warnings from mark_buffer_dirty() when previous EIO has marked
2064 * the buffer as !uptodate
2066 set_buffer_uptodate(bh);
2067 mark_buffer_dirty(bh);
2068 sbi->s_lvid_dirty = 0;
2069 mutex_unlock(&sbi->s_alloc_mutex);
2070 /* Make closing of filesystem visible on the media immediately */
2071 sync_dirty_buffer(bh);
2074 u64 lvid_get_unique_id(struct super_block *sb)
2076 struct buffer_head *bh;
2077 struct udf_sb_info *sbi = UDF_SB(sb);
2078 struct logicalVolIntegrityDesc *lvid;
2079 struct logicalVolHeaderDesc *lvhd;
2083 bh = sbi->s_lvid_bh;
2087 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2088 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2090 mutex_lock(&sbi->s_alloc_mutex);
2091 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2092 if (!(++uniqueID & 0xFFFFFFFF))
2094 lvhd->uniqueID = cpu_to_le64(uniqueID);
2095 mutex_unlock(&sbi->s_alloc_mutex);
2096 mark_buffer_dirty(bh);
2101 static int udf_fill_super(struct super_block *sb, void *options, int silent)
2104 struct inode *inode = NULL;
2105 struct udf_options uopt;
2106 struct kernel_lb_addr rootdir, fileset;
2107 struct udf_sb_info *sbi;
2108 bool lvid_open = false;
2110 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
2111 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2112 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2113 uopt.gid = make_kgid(current_user_ns(), overflowgid);
2115 uopt.fmode = UDF_INVALID_MODE;
2116 uopt.dmode = UDF_INVALID_MODE;
2118 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2122 sb->s_fs_info = sbi;
2124 mutex_init(&sbi->s_alloc_mutex);
2126 if (!udf_parse_options((char *)options, &uopt, false))
2127 goto parse_options_failure;
2129 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
2130 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
2131 udf_err(sb, "utf8 cannot be combined with iocharset\n");
2132 goto parse_options_failure;
2134 #ifdef CONFIG_UDF_NLS
2135 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
2136 uopt.nls_map = load_nls_default();
2138 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2140 udf_debug("Using default NLS map\n");
2143 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2144 uopt.flags |= (1 << UDF_FLAG_UTF8);
2146 fileset.logicalBlockNum = 0xFFFFFFFF;
2147 fileset.partitionReferenceNum = 0xFFFF;
2149 sbi->s_flags = uopt.flags;
2150 sbi->s_uid = uopt.uid;
2151 sbi->s_gid = uopt.gid;
2152 sbi->s_umask = uopt.umask;
2153 sbi->s_fmode = uopt.fmode;
2154 sbi->s_dmode = uopt.dmode;
2155 sbi->s_nls_map = uopt.nls_map;
2156 rwlock_init(&sbi->s_cred_lock);
2158 if (uopt.session == 0xFFFFFFFF)
2159 sbi->s_session = udf_get_last_session(sb);
2161 sbi->s_session = uopt.session;
2163 udf_debug("Multi-session=%d\n", sbi->s_session);
2165 /* Fill in the rest of the superblock */
2166 sb->s_op = &udf_sb_ops;
2167 sb->s_export_op = &udf_export_ops;
2169 sb->s_magic = UDF_SUPER_MAGIC;
2170 sb->s_time_gran = 1000;
2172 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2173 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2175 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
2176 while (uopt.blocksize <= 4096) {
2177 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2179 if (!silent && ret != -EACCES) {
2180 pr_notice("Scanning with blocksize %d failed\n",
2183 brelse(sbi->s_lvid_bh);
2184 sbi->s_lvid_bh = NULL;
2186 * EACCES is special - we want to propagate to
2187 * upper layers that we cannot handle RW mount.
2194 uopt.blocksize <<= 1;
2198 if (ret == -EAGAIN) {
2199 udf_warn(sb, "No partition found (1)\n");
2205 udf_debug("Lastblock=%d\n", sbi->s_last_block);
2207 if (sbi->s_lvid_bh) {
2208 struct logicalVolIntegrityDescImpUse *lvidiu =
2210 uint16_t minUDFReadRev;
2211 uint16_t minUDFWriteRev;
2217 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2218 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
2219 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
2220 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
2222 UDF_MAX_READ_VERSION);
2225 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
2231 sbi->s_udfrev = minUDFWriteRev;
2233 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2234 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2235 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2236 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2239 if (!sbi->s_partitions) {
2240 udf_warn(sb, "No partition found (2)\n");
2245 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2246 UDF_PART_FLAG_READ_ONLY &&
2252 if (udf_find_fileset(sb, &fileset, &rootdir)) {
2253 udf_warn(sb, "No fileset found\n");
2259 struct timestamp ts;
2260 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
2261 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2262 sbi->s_volume_ident,
2263 le16_to_cpu(ts.year), ts.month, ts.day,
2264 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
2266 if (!sb_rdonly(sb)) {
2271 /* Assign the root inode */
2272 /* assign inodes by physical block number */
2273 /* perhaps it's not extensible enough, but for now ... */
2274 inode = udf_iget(sb, &rootdir);
2275 if (IS_ERR(inode)) {
2276 udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
2277 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2278 ret = PTR_ERR(inode);
2282 /* Allocate a dentry for the root inode */
2283 sb->s_root = d_make_root(inode);
2285 udf_err(sb, "Couldn't allocate root dentry\n");
2289 sb->s_maxbytes = MAX_LFS_FILESIZE;
2290 sb->s_max_links = UDF_MAX_LINKS;
2294 iput(sbi->s_vat_inode);
2295 parse_options_failure:
2296 #ifdef CONFIG_UDF_NLS
2297 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2298 unload_nls(sbi->s_nls_map);
2302 brelse(sbi->s_lvid_bh);
2303 udf_sb_free_partitions(sb);
2305 sb->s_fs_info = NULL;
2310 void _udf_err(struct super_block *sb, const char *function,
2311 const char *fmt, ...)
2313 struct va_format vaf;
2316 va_start(args, fmt);
2321 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2326 void _udf_warn(struct super_block *sb, const char *function,
2327 const char *fmt, ...)
2329 struct va_format vaf;
2332 va_start(args, fmt);
2337 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2342 static void udf_put_super(struct super_block *sb)
2344 struct udf_sb_info *sbi;
2348 iput(sbi->s_vat_inode);
2349 #ifdef CONFIG_UDF_NLS
2350 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2351 unload_nls(sbi->s_nls_map);
2355 brelse(sbi->s_lvid_bh);
2356 udf_sb_free_partitions(sb);
2357 mutex_destroy(&sbi->s_alloc_mutex);
2358 kfree(sb->s_fs_info);
2359 sb->s_fs_info = NULL;
2362 static int udf_sync_fs(struct super_block *sb, int wait)
2364 struct udf_sb_info *sbi = UDF_SB(sb);
2366 mutex_lock(&sbi->s_alloc_mutex);
2367 if (sbi->s_lvid_dirty) {
2369 * Blockdevice will be synced later so we don't have to submit
2372 mark_buffer_dirty(sbi->s_lvid_bh);
2373 sbi->s_lvid_dirty = 0;
2375 mutex_unlock(&sbi->s_alloc_mutex);
2380 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2382 struct super_block *sb = dentry->d_sb;
2383 struct udf_sb_info *sbi = UDF_SB(sb);
2384 struct logicalVolIntegrityDescImpUse *lvidiu;
2385 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2387 lvidiu = udf_sb_lvidiu(sb);
2388 buf->f_type = UDF_SUPER_MAGIC;
2389 buf->f_bsize = sb->s_blocksize;
2390 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2391 buf->f_bfree = udf_count_free(sb);
2392 buf->f_bavail = buf->f_bfree;
2393 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2394 le32_to_cpu(lvidiu->numDirs)) : 0)
2396 buf->f_ffree = buf->f_bfree;
2397 buf->f_namelen = UDF_NAME_LEN;
2398 buf->f_fsid.val[0] = (u32)id;
2399 buf->f_fsid.val[1] = (u32)(id >> 32);
2404 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2405 struct udf_bitmap *bitmap)
2407 struct buffer_head *bh = NULL;
2408 unsigned int accum = 0;
2410 int block = 0, newblock;
2411 struct kernel_lb_addr loc;
2415 struct spaceBitmapDesc *bm;
2417 loc.logicalBlockNum = bitmap->s_extPosition;
2418 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2419 bh = udf_read_ptagged(sb, &loc, 0, &ident);
2422 udf_err(sb, "udf_count_free failed\n");
2424 } else if (ident != TAG_IDENT_SBD) {
2426 udf_err(sb, "udf_count_free failed\n");
2430 bm = (struct spaceBitmapDesc *)bh->b_data;
2431 bytes = le32_to_cpu(bm->numOfBytes);
2432 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2433 ptr = (uint8_t *)bh->b_data;
2436 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2437 accum += bitmap_weight((const unsigned long *)(ptr + index),
2442 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2443 bh = udf_tread(sb, newblock);
2445 udf_debug("read failed\n");
2449 ptr = (uint8_t *)bh->b_data;
2457 static unsigned int udf_count_free_table(struct super_block *sb,
2458 struct inode *table)
2460 unsigned int accum = 0;
2462 struct kernel_lb_addr eloc;
2464 struct extent_position epos;
2466 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
2467 epos.block = UDF_I(table)->i_location;
2468 epos.offset = sizeof(struct unallocSpaceEntry);
2471 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2472 accum += (elen >> table->i_sb->s_blocksize_bits);
2475 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
2480 static unsigned int udf_count_free(struct super_block *sb)
2482 unsigned int accum = 0;
2483 struct udf_sb_info *sbi = UDF_SB(sb);
2484 struct udf_part_map *map;
2485 unsigned int part = sbi->s_partition;
2486 int ptype = sbi->s_partmaps[part].s_partition_type;
2488 if (ptype == UDF_METADATA_MAP25) {
2489 part = sbi->s_partmaps[part].s_type_specific.s_metadata.
2490 s_phys_partition_ref;
2491 } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
2493 * Filesystems with VAT are append-only and we cannot write to
2494 * them. Let's just report 0 here.
2499 if (sbi->s_lvid_bh) {
2500 struct logicalVolIntegrityDesc *lvid =
2501 (struct logicalVolIntegrityDesc *)
2502 sbi->s_lvid_bh->b_data;
2503 if (le32_to_cpu(lvid->numOfPartitions) > part) {
2504 accum = le32_to_cpu(
2505 lvid->freeSpaceTable[part]);
2506 if (accum == 0xFFFFFFFF)
2514 map = &sbi->s_partmaps[part];
2515 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2516 accum += udf_count_free_bitmap(sb,
2517 map->s_uspace.s_bitmap);
2519 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2520 accum += udf_count_free_bitmap(sb,
2521 map->s_fspace.s_bitmap);
2526 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2527 accum += udf_count_free_table(sb,
2528 map->s_uspace.s_table);
2530 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2531 accum += udf_count_free_table(sb,
2532 map->s_fspace.s_table);
2538 MODULE_AUTHOR("Ben Fennema");
2539 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2540 MODULE_LICENSE("GPL");
2541 module_init(init_udf_fs)
2542 module_exit(exit_udf_fs)