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 int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
96 struct kernel_lb_addr *);
97 static void udf_load_fileset(struct super_block *, struct buffer_head *,
98 struct kernel_lb_addr *);
99 static void udf_open_lvid(struct super_block *);
100 static void udf_close_lvid(struct super_block *);
101 static unsigned int udf_count_free(struct super_block *);
102 static int udf_statfs(struct dentry *, struct kstatfs *);
103 static int udf_show_options(struct seq_file *, struct dentry *);
105 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
107 struct logicalVolIntegrityDesc *lvid;
108 unsigned int partnum;
111 if (!UDF_SB(sb)->s_lvid_bh)
113 lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
114 partnum = le32_to_cpu(lvid->numOfPartitions);
115 if ((sb->s_blocksize - sizeof(struct logicalVolIntegrityDescImpUse) -
116 offsetof(struct logicalVolIntegrityDesc, impUse)) /
117 (2 * sizeof(uint32_t)) < partnum) {
118 udf_err(sb, "Logical volume integrity descriptor corrupted "
119 "(numOfPartitions = %u)!\n", partnum);
122 /* The offset is to skip freeSpaceTable and sizeTable arrays */
123 offset = partnum * 2 * sizeof(uint32_t);
124 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
127 /* UDF filesystem type */
128 static struct dentry *udf_mount(struct file_system_type *fs_type,
129 int flags, const char *dev_name, void *data)
131 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
134 static struct file_system_type udf_fstype = {
135 .owner = THIS_MODULE,
138 .kill_sb = kill_block_super,
139 .fs_flags = FS_REQUIRES_DEV,
141 MODULE_ALIAS_FS("udf");
143 static struct kmem_cache *udf_inode_cachep;
145 static struct inode *udf_alloc_inode(struct super_block *sb)
147 struct udf_inode_info *ei;
148 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
153 ei->i_lenExtents = 0;
154 ei->i_next_alloc_block = 0;
155 ei->i_next_alloc_goal = 0;
157 init_rwsem(&ei->i_data_sem);
158 ei->cached_extent.lstart = -1;
159 spin_lock_init(&ei->i_extent_cache_lock);
161 return &ei->vfs_inode;
164 static void udf_i_callback(struct rcu_head *head)
166 struct inode *inode = container_of(head, struct inode, i_rcu);
167 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
170 static void udf_destroy_inode(struct inode *inode)
172 call_rcu(&inode->i_rcu, udf_i_callback);
175 static void init_once(void *foo)
177 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
179 ei->i_ext.i_data = NULL;
180 inode_init_once(&ei->vfs_inode);
183 static int __init init_inodecache(void)
185 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
186 sizeof(struct udf_inode_info),
187 0, (SLAB_RECLAIM_ACCOUNT |
191 if (!udf_inode_cachep)
196 static void destroy_inodecache(void)
199 * Make sure all delayed rcu free inodes are flushed before we
203 kmem_cache_destroy(udf_inode_cachep);
206 /* Superblock operations */
207 static const struct super_operations udf_sb_ops = {
208 .alloc_inode = udf_alloc_inode,
209 .destroy_inode = udf_destroy_inode,
210 .write_inode = udf_write_inode,
211 .evict_inode = udf_evict_inode,
212 .put_super = udf_put_super,
213 .sync_fs = udf_sync_fs,
214 .statfs = udf_statfs,
215 .remount_fs = udf_remount_fs,
216 .show_options = udf_show_options,
221 unsigned int blocksize;
222 unsigned int session;
223 unsigned int lastblock;
226 unsigned short partition;
227 unsigned int fileset;
228 unsigned int rootdir;
235 struct nls_table *nls_map;
238 static int __init init_udf_fs(void)
242 err = init_inodecache();
245 err = register_filesystem(&udf_fstype);
252 destroy_inodecache();
258 static void __exit exit_udf_fs(void)
260 unregister_filesystem(&udf_fstype);
261 destroy_inodecache();
264 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
266 struct udf_sb_info *sbi = UDF_SB(sb);
268 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
269 if (!sbi->s_partmaps) {
270 sbi->s_partitions = 0;
274 sbi->s_partitions = count;
278 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
281 int nr_groups = bitmap->s_nr_groups;
283 for (i = 0; i < nr_groups; i++)
284 if (bitmap->s_block_bitmap[i])
285 brelse(bitmap->s_block_bitmap[i]);
290 static void udf_free_partition(struct udf_part_map *map)
293 struct udf_meta_data *mdata;
295 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
296 iput(map->s_uspace.s_table);
297 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
298 iput(map->s_fspace.s_table);
299 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
300 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
301 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
302 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
303 if (map->s_partition_type == UDF_SPARABLE_MAP15)
304 for (i = 0; i < 4; i++)
305 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
306 else if (map->s_partition_type == UDF_METADATA_MAP25) {
307 mdata = &map->s_type_specific.s_metadata;
308 iput(mdata->s_metadata_fe);
309 mdata->s_metadata_fe = NULL;
311 iput(mdata->s_mirror_fe);
312 mdata->s_mirror_fe = NULL;
314 iput(mdata->s_bitmap_fe);
315 mdata->s_bitmap_fe = NULL;
319 static void udf_sb_free_partitions(struct super_block *sb)
321 struct udf_sb_info *sbi = UDF_SB(sb);
324 if (!sbi->s_partmaps)
326 for (i = 0; i < sbi->s_partitions; i++)
327 udf_free_partition(&sbi->s_partmaps[i]);
328 kfree(sbi->s_partmaps);
329 sbi->s_partmaps = NULL;
332 static int udf_show_options(struct seq_file *seq, struct dentry *root)
334 struct super_block *sb = root->d_sb;
335 struct udf_sb_info *sbi = UDF_SB(sb);
337 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
338 seq_puts(seq, ",nostrict");
339 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
340 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
341 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
342 seq_puts(seq, ",unhide");
343 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
344 seq_puts(seq, ",undelete");
345 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
346 seq_puts(seq, ",noadinicb");
347 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
348 seq_puts(seq, ",shortad");
349 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
350 seq_puts(seq, ",uid=forget");
351 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
352 seq_puts(seq, ",uid=ignore");
353 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
354 seq_puts(seq, ",gid=forget");
355 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
356 seq_puts(seq, ",gid=ignore");
357 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
358 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
359 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
360 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
361 if (sbi->s_umask != 0)
362 seq_printf(seq, ",umask=%ho", sbi->s_umask);
363 if (sbi->s_fmode != UDF_INVALID_MODE)
364 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
365 if (sbi->s_dmode != UDF_INVALID_MODE)
366 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
367 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
368 seq_printf(seq, ",session=%d", sbi->s_session);
369 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
370 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
371 if (sbi->s_anchor != 0)
372 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
374 * volume, partition, fileset and rootdir seem to be ignored
377 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
378 seq_puts(seq, ",utf8");
379 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
380 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
389 * Parse mount options.
392 * The following mount options are supported:
394 * gid= Set the default group.
395 * umask= Set the default umask.
396 * mode= Set the default file permissions.
397 * dmode= Set the default directory permissions.
398 * uid= Set the default user.
399 * bs= Set the block size.
400 * unhide Show otherwise hidden files.
401 * undelete Show deleted files in lists.
402 * adinicb Embed data in the inode (default)
403 * noadinicb Don't embed data in the inode
404 * shortad Use short ad's
405 * longad Use long ad's (default)
406 * nostrict Unset strict conformance
407 * iocharset= Set the NLS character set
409 * The remaining are for debugging and disaster recovery:
411 * novrs Skip volume sequence recognition
413 * The following expect a offset from 0.
415 * session= Set the CDROM session (default= last session)
416 * anchor= Override standard anchor location. (default= 256)
417 * volume= Override the VolumeDesc location. (unused)
418 * partition= Override the PartitionDesc location. (unused)
419 * lastblock= Set the last block of the filesystem/
421 * The following expect a offset from the partition root.
423 * fileset= Override the fileset block location. (unused)
424 * rootdir= Override the root directory location. (unused)
425 * WARNING: overriding the rootdir to a non-directory may
426 * yield highly unpredictable results.
429 * options Pointer to mount options string.
430 * uopts Pointer to mount options variable.
433 * <return> 1 Mount options parsed okay.
434 * <return> 0 Error parsing mount options.
437 * July 1, 1997 - Andrew E. Mileski
438 * Written, tested, and released.
442 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
443 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
444 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
445 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
446 Opt_rootdir, Opt_utf8, Opt_iocharset,
447 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
451 static const match_table_t tokens = {
452 {Opt_novrs, "novrs"},
453 {Opt_nostrict, "nostrict"},
455 {Opt_unhide, "unhide"},
456 {Opt_undelete, "undelete"},
457 {Opt_noadinicb, "noadinicb"},
458 {Opt_adinicb, "adinicb"},
459 {Opt_shortad, "shortad"},
460 {Opt_longad, "longad"},
461 {Opt_uforget, "uid=forget"},
462 {Opt_uignore, "uid=ignore"},
463 {Opt_gforget, "gid=forget"},
464 {Opt_gignore, "gid=ignore"},
467 {Opt_umask, "umask=%o"},
468 {Opt_session, "session=%u"},
469 {Opt_lastblock, "lastblock=%u"},
470 {Opt_anchor, "anchor=%u"},
471 {Opt_volume, "volume=%u"},
472 {Opt_partition, "partition=%u"},
473 {Opt_fileset, "fileset=%u"},
474 {Opt_rootdir, "rootdir=%u"},
476 {Opt_iocharset, "iocharset=%s"},
477 {Opt_fmode, "mode=%o"},
478 {Opt_dmode, "dmode=%o"},
482 static int udf_parse_options(char *options, struct udf_options *uopt,
489 uopt->partition = 0xFFFF;
490 uopt->session = 0xFFFFFFFF;
493 uopt->volume = 0xFFFFFFFF;
494 uopt->rootdir = 0xFFFFFFFF;
495 uopt->fileset = 0xFFFFFFFF;
496 uopt->nls_map = NULL;
501 while ((p = strsep(&options, ",")) != NULL) {
502 substring_t args[MAX_OPT_ARGS];
508 token = match_token(p, tokens, args);
514 if (match_int(&args[0], &option))
517 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
520 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
523 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
526 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
529 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
532 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
535 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
538 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
541 if (match_int(args, &option))
543 uopt->gid = make_kgid(current_user_ns(), option);
544 if (!gid_valid(uopt->gid))
546 uopt->flags |= (1 << UDF_FLAG_GID_SET);
549 if (match_int(args, &option))
551 uopt->uid = make_kuid(current_user_ns(), option);
552 if (!uid_valid(uopt->uid))
554 uopt->flags |= (1 << UDF_FLAG_UID_SET);
557 if (match_octal(args, &option))
559 uopt->umask = option;
562 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
565 if (match_int(args, &option))
567 uopt->session = option;
569 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
572 if (match_int(args, &option))
574 uopt->lastblock = option;
576 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
579 if (match_int(args, &option))
581 uopt->anchor = option;
584 if (match_int(args, &option))
586 uopt->volume = option;
589 if (match_int(args, &option))
591 uopt->partition = option;
594 if (match_int(args, &option))
596 uopt->fileset = option;
599 if (match_int(args, &option))
601 uopt->rootdir = option;
604 uopt->flags |= (1 << UDF_FLAG_UTF8);
606 #ifdef CONFIG_UDF_NLS
608 uopt->nls_map = load_nls(args[0].from);
609 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
613 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
616 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
619 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
622 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
625 if (match_octal(args, &option))
627 uopt->fmode = option & 0777;
630 if (match_octal(args, &option))
632 uopt->dmode = option & 0777;
635 pr_err("bad mount option \"%s\" or missing value\n", p);
642 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
644 struct udf_options uopt;
645 struct udf_sb_info *sbi = UDF_SB(sb);
647 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
651 int write_rev = le16_to_cpu(lvidiu->minUDFWriteRev);
652 if (write_rev > UDF_MAX_WRITE_VERSION && !(*flags & SB_RDONLY))
656 uopt.flags = sbi->s_flags;
657 uopt.uid = sbi->s_uid;
658 uopt.gid = sbi->s_gid;
659 uopt.umask = sbi->s_umask;
660 uopt.fmode = sbi->s_fmode;
661 uopt.dmode = sbi->s_dmode;
663 if (!udf_parse_options(options, &uopt, true))
666 write_lock(&sbi->s_cred_lock);
667 sbi->s_flags = uopt.flags;
668 sbi->s_uid = uopt.uid;
669 sbi->s_gid = uopt.gid;
670 sbi->s_umask = uopt.umask;
671 sbi->s_fmode = uopt.fmode;
672 sbi->s_dmode = uopt.dmode;
673 write_unlock(&sbi->s_cred_lock);
675 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
678 if (*flags & SB_RDONLY)
687 /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
688 /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
689 static loff_t udf_check_vsd(struct super_block *sb)
691 struct volStructDesc *vsd = NULL;
692 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
694 struct buffer_head *bh = NULL;
697 struct udf_sb_info *sbi;
700 if (sb->s_blocksize < sizeof(struct volStructDesc))
701 sectorsize = sizeof(struct volStructDesc);
703 sectorsize = sb->s_blocksize;
705 sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits);
707 udf_debug("Starting at sector %u (%lu byte sectors)\n",
708 (unsigned int)(sector >> sb->s_blocksize_bits),
710 /* Process the sequence (if applicable). The hard limit on the sector
711 * offset is arbitrary, hopefully large enough so that all valid UDF
712 * filesystems will be recognised. There is no mention of an upper
713 * bound to the size of the volume recognition area in the standard.
714 * The limit will prevent the code to read all the sectors of a
715 * specially crafted image (like a bluray disc full of CD001 sectors),
716 * potentially causing minutes or even hours of uninterruptible I/O
717 * activity. This actually happened with uninitialised SSD partitions
718 * (all 0xFF) before the check for the limit and all valid IDs were
720 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
721 sector += sectorsize) {
723 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
727 /* Look for ISO descriptors */
728 vsd = (struct volStructDesc *)(bh->b_data +
729 (sector & (sb->s_blocksize - 1)));
731 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
733 switch (vsd->structType) {
735 udf_debug("ISO9660 Boot Record found\n");
738 udf_debug("ISO9660 Primary Volume Descriptor found\n");
741 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
744 udf_debug("ISO9660 Volume Partition Descriptor found\n");
747 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
750 udf_debug("ISO9660 VRS (%u) found\n",
754 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
757 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
761 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
764 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
767 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
770 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
774 /* invalid id : end of volume recognition area */
785 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
786 VSD_FIRST_SECTOR_OFFSET)
792 static int udf_find_fileset(struct super_block *sb,
793 struct kernel_lb_addr *fileset,
794 struct kernel_lb_addr *root)
796 struct buffer_head *bh = NULL;
799 struct udf_sb_info *sbi;
801 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
802 fileset->partitionReferenceNum != 0xFFFF) {
803 bh = udf_read_ptagged(sb, fileset, 0, &ident);
807 } else if (ident != TAG_IDENT_FSD) {
816 /* Search backwards through the partitions */
817 struct kernel_lb_addr newfileset;
819 /* --> cvg: FIXME - is it reasonable? */
822 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
823 (newfileset.partitionReferenceNum != 0xFFFF &&
824 fileset->logicalBlockNum == 0xFFFFFFFF &&
825 fileset->partitionReferenceNum == 0xFFFF);
826 newfileset.partitionReferenceNum--) {
827 lastblock = sbi->s_partmaps
828 [newfileset.partitionReferenceNum]
830 newfileset.logicalBlockNum = 0;
833 bh = udf_read_ptagged(sb, &newfileset, 0,
836 newfileset.logicalBlockNum++;
843 struct spaceBitmapDesc *sp;
844 sp = (struct spaceBitmapDesc *)
846 newfileset.logicalBlockNum += 1 +
847 ((le32_to_cpu(sp->numOfBytes) +
848 sizeof(struct spaceBitmapDesc)
849 - 1) >> sb->s_blocksize_bits);
854 *fileset = newfileset;
857 newfileset.logicalBlockNum++;
862 } while (newfileset.logicalBlockNum < lastblock &&
863 fileset->logicalBlockNum == 0xFFFFFFFF &&
864 fileset->partitionReferenceNum == 0xFFFF);
868 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
869 fileset->partitionReferenceNum != 0xFFFF) && bh) {
870 udf_debug("Fileset at block=%u, partition=%u\n",
871 fileset->logicalBlockNum,
872 fileset->partitionReferenceNum);
874 sbi->s_partition = fileset->partitionReferenceNum;
875 udf_load_fileset(sb, bh, root);
883 * Load primary Volume Descriptor Sequence
885 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
888 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
890 struct primaryVolDesc *pvoldesc;
892 struct buffer_head *bh;
896 outstr = kmalloc(128, GFP_NOFS);
900 bh = udf_read_tagged(sb, block, block, &ident);
906 if (ident != TAG_IDENT_PVD) {
911 pvoldesc = (struct primaryVolDesc *)bh->b_data;
913 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
914 pvoldesc->recordingDateAndTime)) {
916 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
917 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
918 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
919 ts->minute, le16_to_cpu(ts->typeAndTimezone));
923 ret = udf_dstrCS0toUTF8(outstr, 31, pvoldesc->volIdent, 32);
927 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
928 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
930 ret = udf_dstrCS0toUTF8(outstr, 127, pvoldesc->volSetIdent, 128);
935 udf_debug("volSetIdent[] = '%s'\n", outstr);
945 struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
946 u32 meta_file_loc, u32 partition_ref)
948 struct kernel_lb_addr addr;
949 struct inode *metadata_fe;
951 addr.logicalBlockNum = meta_file_loc;
952 addr.partitionReferenceNum = partition_ref;
954 metadata_fe = udf_iget_special(sb, &addr);
956 if (IS_ERR(metadata_fe)) {
957 udf_warn(sb, "metadata inode efe not found\n");
960 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
961 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
963 return ERR_PTR(-EIO);
969 static int udf_load_metadata_files(struct super_block *sb, int partition,
972 struct udf_sb_info *sbi = UDF_SB(sb);
973 struct udf_part_map *map;
974 struct udf_meta_data *mdata;
975 struct kernel_lb_addr addr;
978 map = &sbi->s_partmaps[partition];
979 mdata = &map->s_type_specific.s_metadata;
980 mdata->s_phys_partition_ref = type1_index;
982 /* metadata address */
983 udf_debug("Metadata file location: block = %u part = %u\n",
984 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
986 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
987 mdata->s_phys_partition_ref);
989 /* mirror file entry */
990 udf_debug("Mirror metadata file location: block = %u part = %u\n",
991 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
993 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
994 mdata->s_phys_partition_ref);
997 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
1000 mdata->s_mirror_fe = fe;
1002 mdata->s_metadata_fe = fe;
1008 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1010 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1011 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1012 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
1014 udf_debug("Bitmap file location: block = %u part = %u\n",
1015 addr.logicalBlockNum, addr.partitionReferenceNum);
1017 fe = udf_iget_special(sb, &addr);
1020 udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
1022 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
1026 mdata->s_bitmap_fe = fe;
1029 udf_debug("udf_load_metadata_files Ok\n");
1033 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1034 struct kernel_lb_addr *root)
1036 struct fileSetDesc *fset;
1038 fset = (struct fileSetDesc *)bh->b_data;
1040 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1042 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1044 udf_debug("Rootdir at block=%u, partition=%u\n",
1045 root->logicalBlockNum, root->partitionReferenceNum);
1048 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1050 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1051 return DIV_ROUND_UP(map->s_partition_len +
1052 (sizeof(struct spaceBitmapDesc) << 3),
1053 sb->s_blocksize * 8);
1056 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1058 struct udf_bitmap *bitmap;
1062 nr_groups = udf_compute_nr_groups(sb, index);
1063 size = sizeof(struct udf_bitmap) +
1064 (sizeof(struct buffer_head *) * nr_groups);
1066 if (size <= PAGE_SIZE)
1067 bitmap = kzalloc(size, GFP_KERNEL);
1069 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
1074 bitmap->s_nr_groups = nr_groups;
1078 static int udf_fill_partdesc_info(struct super_block *sb,
1079 struct partitionDesc *p, int p_index)
1081 struct udf_part_map *map;
1082 struct udf_sb_info *sbi = UDF_SB(sb);
1083 struct partitionHeaderDesc *phd;
1085 map = &sbi->s_partmaps[p_index];
1087 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1088 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1090 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1091 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1092 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1093 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1094 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1095 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1096 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1097 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1099 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
1100 p_index, map->s_partition_type,
1101 map->s_partition_root, map->s_partition_len);
1103 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1104 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1107 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1108 if (phd->unallocSpaceTable.extLength) {
1109 struct kernel_lb_addr loc = {
1110 .logicalBlockNum = le32_to_cpu(
1111 phd->unallocSpaceTable.extPosition),
1112 .partitionReferenceNum = p_index,
1114 struct inode *inode;
1116 inode = udf_iget_special(sb, &loc);
1117 if (IS_ERR(inode)) {
1118 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1120 return PTR_ERR(inode);
1122 map->s_uspace.s_table = inode;
1123 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1124 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
1125 p_index, map->s_uspace.s_table->i_ino);
1128 if (phd->unallocSpaceBitmap.extLength) {
1129 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1132 map->s_uspace.s_bitmap = bitmap;
1133 bitmap->s_extPosition = le32_to_cpu(
1134 phd->unallocSpaceBitmap.extPosition);
1135 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1136 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
1137 p_index, bitmap->s_extPosition);
1140 if (phd->partitionIntegrityTable.extLength)
1141 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1143 if (phd->freedSpaceTable.extLength) {
1144 struct kernel_lb_addr loc = {
1145 .logicalBlockNum = le32_to_cpu(
1146 phd->freedSpaceTable.extPosition),
1147 .partitionReferenceNum = p_index,
1149 struct inode *inode;
1151 inode = udf_iget_special(sb, &loc);
1152 if (IS_ERR(inode)) {
1153 udf_debug("cannot load freedSpaceTable (part %d)\n",
1155 return PTR_ERR(inode);
1157 map->s_fspace.s_table = inode;
1158 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1159 udf_debug("freedSpaceTable (part %d) @ %lu\n",
1160 p_index, map->s_fspace.s_table->i_ino);
1163 if (phd->freedSpaceBitmap.extLength) {
1164 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1167 map->s_fspace.s_bitmap = bitmap;
1168 bitmap->s_extPosition = le32_to_cpu(
1169 phd->freedSpaceBitmap.extPosition);
1170 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1171 udf_debug("freedSpaceBitmap (part %d) @ %u\n",
1172 p_index, bitmap->s_extPosition);
1177 static void udf_find_vat_block(struct super_block *sb, int p_index,
1178 int type1_index, sector_t start_block)
1180 struct udf_sb_info *sbi = UDF_SB(sb);
1181 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1183 struct kernel_lb_addr ino;
1184 struct inode *inode;
1187 * VAT file entry is in the last recorded block. Some broken disks have
1188 * it a few blocks before so try a bit harder...
1190 ino.partitionReferenceNum = type1_index;
1191 for (vat_block = start_block;
1192 vat_block >= map->s_partition_root &&
1193 vat_block >= start_block - 3; vat_block--) {
1194 ino.logicalBlockNum = vat_block - map->s_partition_root;
1195 inode = udf_iget_special(sb, &ino);
1196 if (!IS_ERR(inode)) {
1197 sbi->s_vat_inode = inode;
1203 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1205 struct udf_sb_info *sbi = UDF_SB(sb);
1206 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1207 struct buffer_head *bh = NULL;
1208 struct udf_inode_info *vati;
1210 struct virtualAllocationTable20 *vat20;
1211 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1212 sb->s_blocksize_bits;
1214 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
1215 if (!sbi->s_vat_inode &&
1216 sbi->s_last_block != blocks - 1) {
1217 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1218 (unsigned long)sbi->s_last_block,
1219 (unsigned long)blocks - 1);
1220 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
1222 if (!sbi->s_vat_inode)
1225 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1226 map->s_type_specific.s_virtual.s_start_offset = 0;
1227 map->s_type_specific.s_virtual.s_num_entries =
1228 (sbi->s_vat_inode->i_size - 36) >> 2;
1229 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1230 vati = UDF_I(sbi->s_vat_inode);
1231 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1232 pos = udf_block_map(sbi->s_vat_inode, 0);
1233 bh = sb_bread(sb, pos);
1236 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1238 vat20 = (struct virtualAllocationTable20 *)
1242 map->s_type_specific.s_virtual.s_start_offset =
1243 le16_to_cpu(vat20->lengthHeader);
1244 map->s_type_specific.s_virtual.s_num_entries =
1245 (sbi->s_vat_inode->i_size -
1246 map->s_type_specific.s_virtual.
1247 s_start_offset) >> 2;
1254 * Load partition descriptor block
1256 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1259 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1261 struct buffer_head *bh;
1262 struct partitionDesc *p;
1263 struct udf_part_map *map;
1264 struct udf_sb_info *sbi = UDF_SB(sb);
1266 uint16_t partitionNumber;
1270 bh = udf_read_tagged(sb, block, block, &ident);
1273 if (ident != TAG_IDENT_PD) {
1278 p = (struct partitionDesc *)bh->b_data;
1279 partitionNumber = le16_to_cpu(p->partitionNumber);
1281 /* First scan for TYPE1 and SPARABLE partitions */
1282 for (i = 0; i < sbi->s_partitions; i++) {
1283 map = &sbi->s_partmaps[i];
1284 udf_debug("Searching map: (%u == %u)\n",
1285 map->s_partition_num, partitionNumber);
1286 if (map->s_partition_num == partitionNumber &&
1287 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1288 map->s_partition_type == UDF_SPARABLE_MAP15))
1292 if (i >= sbi->s_partitions) {
1293 udf_debug("Partition (%u) not found in partition map\n",
1299 ret = udf_fill_partdesc_info(sb, p, i);
1304 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1305 * PHYSICAL partitions are already set up
1309 map = NULL; /* supress 'maybe used uninitialized' warning */
1311 for (i = 0; i < sbi->s_partitions; i++) {
1312 map = &sbi->s_partmaps[i];
1314 if (map->s_partition_num == partitionNumber &&
1315 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1316 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1317 map->s_partition_type == UDF_METADATA_MAP25))
1321 if (i >= sbi->s_partitions) {
1326 ret = udf_fill_partdesc_info(sb, p, i);
1330 if (map->s_partition_type == UDF_METADATA_MAP25) {
1331 ret = udf_load_metadata_files(sb, i, type1_idx);
1333 udf_err(sb, "error loading MetaData partition map %d\n",
1339 * If we have a partition with virtual map, we don't handle
1340 * writing to it (we overwrite blocks instead of relocating
1343 if (!sb_rdonly(sb)) {
1347 ret = udf_load_vat(sb, i, type1_idx);
1353 /* In case loading failed, we handle cleanup in udf_fill_super */
1358 static int udf_load_sparable_map(struct super_block *sb,
1359 struct udf_part_map *map,
1360 struct sparablePartitionMap *spm)
1364 struct sparingTable *st;
1365 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1367 struct buffer_head *bh;
1369 map->s_partition_type = UDF_SPARABLE_MAP15;
1370 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1371 if (!is_power_of_2(sdata->s_packet_len)) {
1372 udf_err(sb, "error loading logical volume descriptor: "
1373 "Invalid packet length %u\n",
1374 (unsigned)sdata->s_packet_len);
1377 if (spm->numSparingTables > 4) {
1378 udf_err(sb, "error loading logical volume descriptor: "
1379 "Too many sparing tables (%d)\n",
1380 (int)spm->numSparingTables);
1384 for (i = 0; i < spm->numSparingTables; i++) {
1385 loc = le32_to_cpu(spm->locSparingTable[i]);
1386 bh = udf_read_tagged(sb, loc, loc, &ident);
1390 st = (struct sparingTable *)bh->b_data;
1392 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1393 strlen(UDF_ID_SPARING)) ||
1394 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1400 sdata->s_spar_map[i] = bh;
1402 map->s_partition_func = udf_get_pblock_spar15;
1406 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1407 struct kernel_lb_addr *fileset)
1409 struct logicalVolDesc *lvd;
1412 struct udf_sb_info *sbi = UDF_SB(sb);
1413 struct genericPartitionMap *gpm;
1415 struct buffer_head *bh;
1416 unsigned int table_len;
1419 bh = udf_read_tagged(sb, block, block, &ident);
1422 BUG_ON(ident != TAG_IDENT_LVD);
1423 lvd = (struct logicalVolDesc *)bh->b_data;
1424 table_len = le32_to_cpu(lvd->mapTableLength);
1425 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
1426 udf_err(sb, "error loading logical volume descriptor: "
1427 "Partition table too long (%u > %lu)\n", table_len,
1428 sb->s_blocksize - sizeof(*lvd));
1433 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1437 for (i = 0, offset = 0;
1438 i < sbi->s_partitions && offset < table_len;
1439 i++, offset += gpm->partitionMapLength) {
1440 struct udf_part_map *map = &sbi->s_partmaps[i];
1441 gpm = (struct genericPartitionMap *)
1442 &(lvd->partitionMaps[offset]);
1443 type = gpm->partitionMapType;
1445 struct genericPartitionMap1 *gpm1 =
1446 (struct genericPartitionMap1 *)gpm;
1447 map->s_partition_type = UDF_TYPE1_MAP15;
1448 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1449 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1450 map->s_partition_func = NULL;
1451 } else if (type == 2) {
1452 struct udfPartitionMap2 *upm2 =
1453 (struct udfPartitionMap2 *)gpm;
1454 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1455 strlen(UDF_ID_VIRTUAL))) {
1457 le16_to_cpu(((__le16 *)upm2->partIdent.
1460 map->s_partition_type =
1462 map->s_partition_func =
1463 udf_get_pblock_virt15;
1465 map->s_partition_type =
1467 map->s_partition_func =
1468 udf_get_pblock_virt20;
1470 } else if (!strncmp(upm2->partIdent.ident,
1472 strlen(UDF_ID_SPARABLE))) {
1473 ret = udf_load_sparable_map(sb, map,
1474 (struct sparablePartitionMap *)gpm);
1477 } else if (!strncmp(upm2->partIdent.ident,
1479 strlen(UDF_ID_METADATA))) {
1480 struct udf_meta_data *mdata =
1481 &map->s_type_specific.s_metadata;
1482 struct metadataPartitionMap *mdm =
1483 (struct metadataPartitionMap *)
1484 &(lvd->partitionMaps[offset]);
1485 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
1486 i, type, UDF_ID_METADATA);
1488 map->s_partition_type = UDF_METADATA_MAP25;
1489 map->s_partition_func = udf_get_pblock_meta25;
1491 mdata->s_meta_file_loc =
1492 le32_to_cpu(mdm->metadataFileLoc);
1493 mdata->s_mirror_file_loc =
1494 le32_to_cpu(mdm->metadataMirrorFileLoc);
1495 mdata->s_bitmap_file_loc =
1496 le32_to_cpu(mdm->metadataBitmapFileLoc);
1497 mdata->s_alloc_unit_size =
1498 le32_to_cpu(mdm->allocUnitSize);
1499 mdata->s_align_unit_size =
1500 le16_to_cpu(mdm->alignUnitSize);
1501 if (mdm->flags & 0x01)
1502 mdata->s_flags |= MF_DUPLICATE_MD;
1504 udf_debug("Metadata Ident suffix=0x%x\n",
1505 le16_to_cpu(*(__le16 *)
1506 mdm->partIdent.identSuffix));
1507 udf_debug("Metadata part num=%u\n",
1508 le16_to_cpu(mdm->partitionNum));
1509 udf_debug("Metadata part alloc unit size=%u\n",
1510 le32_to_cpu(mdm->allocUnitSize));
1511 udf_debug("Metadata file loc=%u\n",
1512 le32_to_cpu(mdm->metadataFileLoc));
1513 udf_debug("Mirror file loc=%u\n",
1514 le32_to_cpu(mdm->metadataMirrorFileLoc));
1515 udf_debug("Bitmap file loc=%u\n",
1516 le32_to_cpu(mdm->metadataBitmapFileLoc));
1517 udf_debug("Flags: %d %u\n",
1518 mdata->s_flags, mdm->flags);
1520 udf_debug("Unknown ident: %s\n",
1521 upm2->partIdent.ident);
1524 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1525 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1527 udf_debug("Partition (%d:%u) type %u on volume %u\n",
1528 i, map->s_partition_num, type, map->s_volumeseqnum);
1532 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1534 *fileset = lelb_to_cpu(la->extLocation);
1535 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
1536 fileset->logicalBlockNum,
1537 fileset->partitionReferenceNum);
1539 if (lvd->integritySeqExt.extLength)
1540 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1548 * Find the prevailing Logical Volume Integrity Descriptor.
1550 static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1552 struct buffer_head *bh, *final_bh;
1554 struct udf_sb_info *sbi = UDF_SB(sb);
1555 struct logicalVolIntegrityDesc *lvid;
1556 int indirections = 0;
1558 while (++indirections <= UDF_MAX_LVID_NESTING) {
1560 while (loc.extLength > 0 &&
1561 (bh = udf_read_tagged(sb, loc.extLocation,
1562 loc.extLocation, &ident))) {
1563 if (ident != TAG_IDENT_LVID) {
1571 loc.extLength -= sb->s_blocksize;
1578 brelse(sbi->s_lvid_bh);
1579 sbi->s_lvid_bh = final_bh;
1581 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1582 if (lvid->nextIntegrityExt.extLength == 0)
1585 loc = leea_to_cpu(lvid->nextIntegrityExt);
1588 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1589 UDF_MAX_LVID_NESTING);
1590 brelse(sbi->s_lvid_bh);
1591 sbi->s_lvid_bh = NULL;
1595 * Step for reallocation of table of partition descriptor sequence numbers.
1596 * Must be power of 2.
1598 #define PART_DESC_ALLOC_STEP 32
1600 struct desc_seq_scan_data {
1601 struct udf_vds_record vds[VDS_POS_LENGTH];
1602 unsigned int size_part_descs;
1603 struct udf_vds_record *part_descs_loc;
1606 static struct udf_vds_record *handle_partition_descriptor(
1607 struct buffer_head *bh,
1608 struct desc_seq_scan_data *data)
1610 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1613 partnum = le16_to_cpu(desc->partitionNumber);
1614 if (partnum >= data->size_part_descs) {
1615 struct udf_vds_record *new_loc;
1616 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1618 new_loc = kzalloc(sizeof(*new_loc) * new_size, GFP_KERNEL);
1620 return ERR_PTR(-ENOMEM);
1621 memcpy(new_loc, data->part_descs_loc,
1622 data->size_part_descs * sizeof(*new_loc));
1623 kfree(data->part_descs_loc);
1624 data->part_descs_loc = new_loc;
1625 data->size_part_descs = new_size;
1627 return &(data->part_descs_loc[partnum]);
1631 static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1632 struct buffer_head *bh, struct desc_seq_scan_data *data)
1635 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1636 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
1637 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1638 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
1639 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1640 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
1641 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1642 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1643 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1644 return handle_partition_descriptor(bh, data);
1650 * Process a main/reserve volume descriptor sequence.
1651 * @block First block of first extent of the sequence.
1652 * @lastblock Lastblock of first extent of the sequence.
1653 * @fileset There we store extent containing root fileset
1655 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1658 static noinline int udf_process_sequence(
1659 struct super_block *sb,
1660 sector_t block, sector_t lastblock,
1661 struct kernel_lb_addr *fileset)
1663 struct buffer_head *bh = NULL;
1664 struct udf_vds_record *curr;
1665 struct generic_desc *gd;
1666 struct volDescPtr *vdp;
1671 unsigned int indirections = 0;
1672 struct desc_seq_scan_data data;
1675 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1676 data.size_part_descs = PART_DESC_ALLOC_STEP;
1677 data.part_descs_loc = kzalloc(sizeof(*data.part_descs_loc) *
1678 data.size_part_descs, GFP_KERNEL);
1679 if (!data.part_descs_loc)
1683 * Read the main descriptor sequence and find which descriptors
1686 for (; (!done && block <= lastblock); block++) {
1688 bh = udf_read_tagged(sb, block, block, &ident);
1692 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1693 gd = (struct generic_desc *)bh->b_data;
1694 vdsn = le32_to_cpu(gd->volDescSeqNum);
1696 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1697 if (++indirections > UDF_MAX_TD_NESTING) {
1698 udf_err(sb, "too many Volume Descriptor "
1699 "Pointers (max %u supported)\n",
1700 UDF_MAX_TD_NESTING);
1705 vdp = (struct volDescPtr *)bh->b_data;
1706 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1707 lastblock = le32_to_cpu(
1708 vdp->nextVolDescSeqExt.extLength) >>
1709 sb->s_blocksize_bits;
1710 lastblock += block - 1;
1711 /* For loop is going to increment 'block' again */
1714 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1715 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1716 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1717 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1718 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1719 curr = get_volume_descriptor_record(ident, bh, &data);
1722 return PTR_ERR(curr);
1724 /* Descriptor we don't care about? */
1727 if (vdsn >= curr->volDescSeqNum) {
1728 curr->volDescSeqNum = vdsn;
1729 curr->block = block;
1732 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1739 * Now read interesting descriptors again and process them
1740 * in a suitable order
1742 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1743 udf_err(sb, "Primary Volume Descriptor not found!\n");
1746 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
1750 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
1751 ret = udf_load_logicalvol(sb,
1752 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1758 /* Now handle prevailing Partition Descriptors */
1759 for (i = 0; i < data.size_part_descs; i++) {
1760 if (data.part_descs_loc[i].block) {
1761 ret = udf_load_partdesc(sb,
1762 data.part_descs_loc[i].block);
1772 * Load Volume Descriptor Sequence described by anchor in bh
1774 * Returns <0 on error, 0 on success
1776 static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1777 struct kernel_lb_addr *fileset)
1779 struct anchorVolDescPtr *anchor;
1780 sector_t main_s, main_e, reserve_s, reserve_e;
1783 anchor = (struct anchorVolDescPtr *)bh->b_data;
1785 /* Locate the main sequence */
1786 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1787 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1788 main_e = main_e >> sb->s_blocksize_bits;
1789 main_e += main_s - 1;
1791 /* Locate the reserve sequence */
1792 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1793 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1794 reserve_e = reserve_e >> sb->s_blocksize_bits;
1795 reserve_e += reserve_s - 1;
1797 /* Process the main & reserve sequences */
1798 /* responsible for finding the PartitionDesc(s) */
1799 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1802 udf_sb_free_partitions(sb);
1803 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1805 udf_sb_free_partitions(sb);
1806 /* No sequence was OK, return -EIO */
1814 * Check whether there is an anchor block in the given block and
1815 * load Volume Descriptor Sequence if so.
1817 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1820 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1821 struct kernel_lb_addr *fileset)
1823 struct buffer_head *bh;
1827 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1828 udf_fixed_to_variable(block) >=
1829 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
1832 bh = udf_read_tagged(sb, block, block, &ident);
1835 if (ident != TAG_IDENT_AVDP) {
1839 ret = udf_load_sequence(sb, bh, fileset);
1845 * Search for an anchor volume descriptor pointer.
1847 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1850 static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1851 struct kernel_lb_addr *fileset)
1855 struct udf_sb_info *sbi = UDF_SB(sb);
1859 /* First try user provided anchor */
1860 if (sbi->s_anchor) {
1861 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1866 * according to spec, anchor is in either:
1870 * however, if the disc isn't closed, it could be 512.
1872 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1876 * The trouble is which block is the last one. Drives often misreport
1877 * this so we try various possibilities.
1879 last[last_count++] = *lastblock;
1880 if (*lastblock >= 1)
1881 last[last_count++] = *lastblock - 1;
1882 last[last_count++] = *lastblock + 1;
1883 if (*lastblock >= 2)
1884 last[last_count++] = *lastblock - 2;
1885 if (*lastblock >= 150)
1886 last[last_count++] = *lastblock - 150;
1887 if (*lastblock >= 152)
1888 last[last_count++] = *lastblock - 152;
1890 for (i = 0; i < last_count; i++) {
1891 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
1892 sb->s_blocksize_bits)
1894 ret = udf_check_anchor_block(sb, last[i], fileset);
1895 if (ret != -EAGAIN) {
1897 *lastblock = last[i];
1902 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1903 if (ret != -EAGAIN) {
1905 *lastblock = last[i];
1910 /* Finally try block 512 in case media is open */
1911 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
1915 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1916 * area specified by it. The function expects sbi->s_lastblock to be the last
1917 * block on the media.
1919 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1922 static int udf_find_anchor(struct super_block *sb,
1923 struct kernel_lb_addr *fileset)
1925 struct udf_sb_info *sbi = UDF_SB(sb);
1926 sector_t lastblock = sbi->s_last_block;
1929 ret = udf_scan_anchors(sb, &lastblock, fileset);
1933 /* No anchor found? Try VARCONV conversion of block numbers */
1934 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1935 lastblock = udf_variable_to_fixed(sbi->s_last_block);
1936 /* Firstly, we try to not convert number of the last block */
1937 ret = udf_scan_anchors(sb, &lastblock, fileset);
1941 lastblock = sbi->s_last_block;
1942 /* Secondly, we try with converted number of the last block */
1943 ret = udf_scan_anchors(sb, &lastblock, fileset);
1945 /* VARCONV didn't help. Clear it. */
1946 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1950 sbi->s_last_block = lastblock;
1955 * Check Volume Structure Descriptor, find Anchor block and load Volume
1956 * Descriptor Sequence.
1958 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1959 * block was not found.
1961 static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1962 int silent, struct kernel_lb_addr *fileset)
1964 struct udf_sb_info *sbi = UDF_SB(sb);
1968 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1970 udf_warn(sb, "Bad block size\n");
1973 sbi->s_last_block = uopt->lastblock;
1975 /* Check that it is NSR02 compliant */
1976 nsr_off = udf_check_vsd(sb);
1979 udf_warn(sb, "No VRS found\n");
1983 udf_debug("Failed to read sector at offset %d. "
1984 "Assuming open disc. Skipping validity "
1985 "check\n", VSD_FIRST_SECTOR_OFFSET);
1986 if (!sbi->s_last_block)
1987 sbi->s_last_block = udf_get_last_block(sb);
1989 udf_debug("Validity check skipped because of novrs option\n");
1992 /* Look for anchor block and load Volume Descriptor Sequence */
1993 sbi->s_anchor = uopt->anchor;
1994 ret = udf_find_anchor(sb, fileset);
1996 if (!silent && ret == -EAGAIN)
1997 udf_warn(sb, "No anchor found\n");
2003 static void udf_open_lvid(struct super_block *sb)
2005 struct udf_sb_info *sbi = UDF_SB(sb);
2006 struct buffer_head *bh = sbi->s_lvid_bh;
2007 struct logicalVolIntegrityDesc *lvid;
2008 struct logicalVolIntegrityDescImpUse *lvidiu;
2013 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2014 lvidiu = udf_sb_lvidiu(sb);
2018 mutex_lock(&sbi->s_alloc_mutex);
2019 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2020 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2021 ktime_get_real_ts(&ts);
2022 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
2023 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
2025 lvid->descTag.descCRC = cpu_to_le16(
2026 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
2027 le16_to_cpu(lvid->descTag.descCRCLength)));
2029 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2030 mark_buffer_dirty(bh);
2031 sbi->s_lvid_dirty = 0;
2032 mutex_unlock(&sbi->s_alloc_mutex);
2033 /* Make opening of filesystem visible on the media immediately */
2034 sync_dirty_buffer(bh);
2037 static void udf_close_lvid(struct super_block *sb)
2039 struct udf_sb_info *sbi = UDF_SB(sb);
2040 struct buffer_head *bh = sbi->s_lvid_bh;
2041 struct logicalVolIntegrityDesc *lvid;
2042 struct logicalVolIntegrityDescImpUse *lvidiu;
2047 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2048 lvidiu = udf_sb_lvidiu(sb);
2052 mutex_lock(&sbi->s_alloc_mutex);
2053 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2054 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2055 ktime_get_real_ts(&ts);
2056 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
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 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
2065 lvid->descTag.descCRC = cpu_to_le16(
2066 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
2067 le16_to_cpu(lvid->descTag.descCRCLength)));
2069 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2071 * We set buffer uptodate unconditionally here to avoid spurious
2072 * warnings from mark_buffer_dirty() when previous EIO has marked
2073 * the buffer as !uptodate
2075 set_buffer_uptodate(bh);
2076 mark_buffer_dirty(bh);
2077 sbi->s_lvid_dirty = 0;
2078 mutex_unlock(&sbi->s_alloc_mutex);
2079 /* Make closing of filesystem visible on the media immediately */
2080 sync_dirty_buffer(bh);
2083 u64 lvid_get_unique_id(struct super_block *sb)
2085 struct buffer_head *bh;
2086 struct udf_sb_info *sbi = UDF_SB(sb);
2087 struct logicalVolIntegrityDesc *lvid;
2088 struct logicalVolHeaderDesc *lvhd;
2092 bh = sbi->s_lvid_bh;
2096 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2097 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2099 mutex_lock(&sbi->s_alloc_mutex);
2100 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2101 if (!(++uniqueID & 0xFFFFFFFF))
2103 lvhd->uniqueID = cpu_to_le64(uniqueID);
2104 mutex_unlock(&sbi->s_alloc_mutex);
2105 mark_buffer_dirty(bh);
2110 static int udf_fill_super(struct super_block *sb, void *options, int silent)
2113 struct inode *inode = NULL;
2114 struct udf_options uopt;
2115 struct kernel_lb_addr rootdir, fileset;
2116 struct udf_sb_info *sbi;
2117 bool lvid_open = false;
2119 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
2120 uopt.uid = INVALID_UID;
2121 uopt.gid = INVALID_GID;
2123 uopt.fmode = UDF_INVALID_MODE;
2124 uopt.dmode = UDF_INVALID_MODE;
2126 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2130 sb->s_fs_info = sbi;
2132 mutex_init(&sbi->s_alloc_mutex);
2134 if (!udf_parse_options((char *)options, &uopt, false))
2135 goto parse_options_failure;
2137 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
2138 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
2139 udf_err(sb, "utf8 cannot be combined with iocharset\n");
2140 goto parse_options_failure;
2142 #ifdef CONFIG_UDF_NLS
2143 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
2144 uopt.nls_map = load_nls_default();
2146 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2148 udf_debug("Using default NLS map\n");
2151 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2152 uopt.flags |= (1 << UDF_FLAG_UTF8);
2154 fileset.logicalBlockNum = 0xFFFFFFFF;
2155 fileset.partitionReferenceNum = 0xFFFF;
2157 sbi->s_flags = uopt.flags;
2158 sbi->s_uid = uopt.uid;
2159 sbi->s_gid = uopt.gid;
2160 sbi->s_umask = uopt.umask;
2161 sbi->s_fmode = uopt.fmode;
2162 sbi->s_dmode = uopt.dmode;
2163 sbi->s_nls_map = uopt.nls_map;
2164 rwlock_init(&sbi->s_cred_lock);
2166 if (uopt.session == 0xFFFFFFFF)
2167 sbi->s_session = udf_get_last_session(sb);
2169 sbi->s_session = uopt.session;
2171 udf_debug("Multi-session=%d\n", sbi->s_session);
2173 /* Fill in the rest of the superblock */
2174 sb->s_op = &udf_sb_ops;
2175 sb->s_export_op = &udf_export_ops;
2177 sb->s_magic = UDF_SUPER_MAGIC;
2178 sb->s_time_gran = 1000;
2180 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2181 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2183 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
2184 while (uopt.blocksize <= 4096) {
2185 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2187 if (!silent && ret != -EACCES) {
2188 pr_notice("Scanning with blocksize %u failed\n",
2191 brelse(sbi->s_lvid_bh);
2192 sbi->s_lvid_bh = NULL;
2194 * EACCES is special - we want to propagate to
2195 * upper layers that we cannot handle RW mount.
2202 uopt.blocksize <<= 1;
2206 if (ret == -EAGAIN) {
2207 udf_warn(sb, "No partition found (1)\n");
2213 udf_debug("Lastblock=%u\n", sbi->s_last_block);
2215 if (sbi->s_lvid_bh) {
2216 struct logicalVolIntegrityDescImpUse *lvidiu =
2218 uint16_t minUDFReadRev;
2219 uint16_t minUDFWriteRev;
2225 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2226 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
2227 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
2228 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
2230 UDF_MAX_READ_VERSION);
2233 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
2239 sbi->s_udfrev = minUDFWriteRev;
2241 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2242 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2243 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2244 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2247 if (!sbi->s_partitions) {
2248 udf_warn(sb, "No partition found (2)\n");
2253 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2254 UDF_PART_FLAG_READ_ONLY &&
2260 if (udf_find_fileset(sb, &fileset, &rootdir)) {
2261 udf_warn(sb, "No fileset found\n");
2267 struct timestamp ts;
2268 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
2269 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2270 sbi->s_volume_ident,
2271 le16_to_cpu(ts.year), ts.month, ts.day,
2272 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
2274 if (!sb_rdonly(sb)) {
2279 /* Assign the root inode */
2280 /* assign inodes by physical block number */
2281 /* perhaps it's not extensible enough, but for now ... */
2282 inode = udf_iget(sb, &rootdir);
2283 if (IS_ERR(inode)) {
2284 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
2285 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2286 ret = PTR_ERR(inode);
2290 /* Allocate a dentry for the root inode */
2291 sb->s_root = d_make_root(inode);
2293 udf_err(sb, "Couldn't allocate root dentry\n");
2297 sb->s_maxbytes = MAX_LFS_FILESIZE;
2298 sb->s_max_links = UDF_MAX_LINKS;
2302 iput(sbi->s_vat_inode);
2303 parse_options_failure:
2304 #ifdef CONFIG_UDF_NLS
2305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2306 unload_nls(sbi->s_nls_map);
2310 brelse(sbi->s_lvid_bh);
2311 udf_sb_free_partitions(sb);
2313 sb->s_fs_info = NULL;
2318 void _udf_err(struct super_block *sb, const char *function,
2319 const char *fmt, ...)
2321 struct va_format vaf;
2324 va_start(args, fmt);
2329 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2334 void _udf_warn(struct super_block *sb, const char *function,
2335 const char *fmt, ...)
2337 struct va_format vaf;
2340 va_start(args, fmt);
2345 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2350 static void udf_put_super(struct super_block *sb)
2352 struct udf_sb_info *sbi;
2356 iput(sbi->s_vat_inode);
2357 #ifdef CONFIG_UDF_NLS
2358 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2359 unload_nls(sbi->s_nls_map);
2363 brelse(sbi->s_lvid_bh);
2364 udf_sb_free_partitions(sb);
2365 mutex_destroy(&sbi->s_alloc_mutex);
2366 kfree(sb->s_fs_info);
2367 sb->s_fs_info = NULL;
2370 static int udf_sync_fs(struct super_block *sb, int wait)
2372 struct udf_sb_info *sbi = UDF_SB(sb);
2374 mutex_lock(&sbi->s_alloc_mutex);
2375 if (sbi->s_lvid_dirty) {
2377 * Blockdevice will be synced later so we don't have to submit
2380 mark_buffer_dirty(sbi->s_lvid_bh);
2381 sbi->s_lvid_dirty = 0;
2383 mutex_unlock(&sbi->s_alloc_mutex);
2388 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2390 struct super_block *sb = dentry->d_sb;
2391 struct udf_sb_info *sbi = UDF_SB(sb);
2392 struct logicalVolIntegrityDescImpUse *lvidiu;
2393 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2395 lvidiu = udf_sb_lvidiu(sb);
2396 buf->f_type = UDF_SUPER_MAGIC;
2397 buf->f_bsize = sb->s_blocksize;
2398 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2399 buf->f_bfree = udf_count_free(sb);
2400 buf->f_bavail = buf->f_bfree;
2401 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2402 le32_to_cpu(lvidiu->numDirs)) : 0)
2404 buf->f_ffree = buf->f_bfree;
2405 buf->f_namelen = UDF_NAME_LEN;
2406 buf->f_fsid.val[0] = (u32)id;
2407 buf->f_fsid.val[1] = (u32)(id >> 32);
2412 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2413 struct udf_bitmap *bitmap)
2415 struct buffer_head *bh = NULL;
2416 unsigned int accum = 0;
2418 udf_pblk_t block = 0, newblock;
2419 struct kernel_lb_addr loc;
2423 struct spaceBitmapDesc *bm;
2425 loc.logicalBlockNum = bitmap->s_extPosition;
2426 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2427 bh = udf_read_ptagged(sb, &loc, 0, &ident);
2430 udf_err(sb, "udf_count_free failed\n");
2432 } else if (ident != TAG_IDENT_SBD) {
2434 udf_err(sb, "udf_count_free failed\n");
2438 bm = (struct spaceBitmapDesc *)bh->b_data;
2439 bytes = le32_to_cpu(bm->numOfBytes);
2440 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2441 ptr = (uint8_t *)bh->b_data;
2444 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2445 accum += bitmap_weight((const unsigned long *)(ptr + index),
2450 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2451 bh = udf_tread(sb, newblock);
2453 udf_debug("read failed\n");
2457 ptr = (uint8_t *)bh->b_data;
2465 static unsigned int udf_count_free_table(struct super_block *sb,
2466 struct inode *table)
2468 unsigned int accum = 0;
2470 struct kernel_lb_addr eloc;
2472 struct extent_position epos;
2474 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
2475 epos.block = UDF_I(table)->i_location;
2476 epos.offset = sizeof(struct unallocSpaceEntry);
2479 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2480 accum += (elen >> table->i_sb->s_blocksize_bits);
2483 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
2488 static unsigned int udf_count_free(struct super_block *sb)
2490 unsigned int accum = 0;
2491 struct udf_sb_info *sbi;
2492 struct udf_part_map *map;
2495 if (sbi->s_lvid_bh) {
2496 struct logicalVolIntegrityDesc *lvid =
2497 (struct logicalVolIntegrityDesc *)
2498 sbi->s_lvid_bh->b_data;
2499 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2500 accum = le32_to_cpu(
2501 lvid->freeSpaceTable[sbi->s_partition]);
2502 if (accum == 0xFFFFFFFF)
2510 map = &sbi->s_partmaps[sbi->s_partition];
2511 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2512 accum += udf_count_free_bitmap(sb,
2513 map->s_uspace.s_bitmap);
2515 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2516 accum += udf_count_free_bitmap(sb,
2517 map->s_fspace.s_bitmap);
2522 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2523 accum += udf_count_free_table(sb,
2524 map->s_uspace.s_table);
2526 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2527 accum += udf_count_free_table(sb,
2528 map->s_fspace.s_table);
2534 MODULE_AUTHOR("Ben Fennema");
2535 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2536 MODULE_LICENSE("GPL");
2537 module_init(init_udf_fs)
2538 module_exit(exit_udf_fs)