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_free_in_core_inode(struct inode *inode)
166 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
169 static void init_once(void *foo)
171 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
173 ei->i_ext.i_data = NULL;
174 inode_init_once(&ei->vfs_inode);
177 static int __init init_inodecache(void)
179 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
180 sizeof(struct udf_inode_info),
181 0, (SLAB_RECLAIM_ACCOUNT |
185 if (!udf_inode_cachep)
190 static void destroy_inodecache(void)
193 * Make sure all delayed rcu free inodes are flushed before we
197 kmem_cache_destroy(udf_inode_cachep);
200 /* Superblock operations */
201 static const struct super_operations udf_sb_ops = {
202 .alloc_inode = udf_alloc_inode,
203 .free_inode = udf_free_in_core_inode,
204 .write_inode = udf_write_inode,
205 .evict_inode = udf_evict_inode,
206 .put_super = udf_put_super,
207 .sync_fs = udf_sync_fs,
208 .statfs = udf_statfs,
209 .remount_fs = udf_remount_fs,
210 .show_options = udf_show_options,
215 unsigned int blocksize;
216 unsigned int session;
217 unsigned int lastblock;
225 struct nls_table *nls_map;
228 static int __init init_udf_fs(void)
232 err = init_inodecache();
235 err = register_filesystem(&udf_fstype);
242 destroy_inodecache();
248 static void __exit exit_udf_fs(void)
250 unregister_filesystem(&udf_fstype);
251 destroy_inodecache();
254 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
256 struct udf_sb_info *sbi = UDF_SB(sb);
258 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
259 if (!sbi->s_partmaps) {
260 sbi->s_partitions = 0;
264 sbi->s_partitions = count;
268 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
271 int nr_groups = bitmap->s_nr_groups;
273 for (i = 0; i < nr_groups; i++)
274 if (bitmap->s_block_bitmap[i])
275 brelse(bitmap->s_block_bitmap[i]);
280 static void udf_free_partition(struct udf_part_map *map)
283 struct udf_meta_data *mdata;
285 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
286 iput(map->s_uspace.s_table);
287 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
288 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
289 if (map->s_partition_type == UDF_SPARABLE_MAP15)
290 for (i = 0; i < 4; i++)
291 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
292 else if (map->s_partition_type == UDF_METADATA_MAP25) {
293 mdata = &map->s_type_specific.s_metadata;
294 iput(mdata->s_metadata_fe);
295 mdata->s_metadata_fe = NULL;
297 iput(mdata->s_mirror_fe);
298 mdata->s_mirror_fe = NULL;
300 iput(mdata->s_bitmap_fe);
301 mdata->s_bitmap_fe = NULL;
305 static void udf_sb_free_partitions(struct super_block *sb)
307 struct udf_sb_info *sbi = UDF_SB(sb);
310 if (!sbi->s_partmaps)
312 for (i = 0; i < sbi->s_partitions; i++)
313 udf_free_partition(&sbi->s_partmaps[i]);
314 kfree(sbi->s_partmaps);
315 sbi->s_partmaps = NULL;
318 static int udf_show_options(struct seq_file *seq, struct dentry *root)
320 struct super_block *sb = root->d_sb;
321 struct udf_sb_info *sbi = UDF_SB(sb);
323 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
324 seq_puts(seq, ",nostrict");
325 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
326 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
327 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
328 seq_puts(seq, ",unhide");
329 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
330 seq_puts(seq, ",undelete");
331 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
332 seq_puts(seq, ",noadinicb");
333 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
334 seq_puts(seq, ",shortad");
335 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
336 seq_puts(seq, ",uid=forget");
337 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
338 seq_puts(seq, ",gid=forget");
339 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
340 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
341 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
342 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
343 if (sbi->s_umask != 0)
344 seq_printf(seq, ",umask=%ho", sbi->s_umask);
345 if (sbi->s_fmode != UDF_INVALID_MODE)
346 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
347 if (sbi->s_dmode != UDF_INVALID_MODE)
348 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
349 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
350 seq_printf(seq, ",session=%d", sbi->s_session);
351 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
352 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
353 if (sbi->s_anchor != 0)
354 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
355 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
356 seq_puts(seq, ",utf8");
357 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
358 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
367 * Parse mount options.
370 * The following mount options are supported:
372 * gid= Set the default group.
373 * umask= Set the default umask.
374 * mode= Set the default file permissions.
375 * dmode= Set the default directory permissions.
376 * uid= Set the default user.
377 * bs= Set the block size.
378 * unhide Show otherwise hidden files.
379 * undelete Show deleted files in lists.
380 * adinicb Embed data in the inode (default)
381 * noadinicb Don't embed data in the inode
382 * shortad Use short ad's
383 * longad Use long ad's (default)
384 * nostrict Unset strict conformance
385 * iocharset= Set the NLS character set
387 * The remaining are for debugging and disaster recovery:
389 * novrs Skip volume sequence recognition
391 * The following expect a offset from 0.
393 * session= Set the CDROM session (default= last session)
394 * anchor= Override standard anchor location. (default= 256)
395 * volume= Override the VolumeDesc location. (unused)
396 * partition= Override the PartitionDesc location. (unused)
397 * lastblock= Set the last block of the filesystem/
399 * The following expect a offset from the partition root.
401 * fileset= Override the fileset block location. (unused)
402 * rootdir= Override the root directory location. (unused)
403 * WARNING: overriding the rootdir to a non-directory may
404 * yield highly unpredictable results.
407 * options Pointer to mount options string.
408 * uopts Pointer to mount options variable.
411 * <return> 1 Mount options parsed okay.
412 * <return> 0 Error parsing mount options.
415 * July 1, 1997 - Andrew E. Mileski
416 * Written, tested, and released.
420 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
421 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
422 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
423 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
424 Opt_rootdir, Opt_utf8, Opt_iocharset,
425 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
429 static const match_table_t tokens = {
430 {Opt_novrs, "novrs"},
431 {Opt_nostrict, "nostrict"},
433 {Opt_unhide, "unhide"},
434 {Opt_undelete, "undelete"},
435 {Opt_noadinicb, "noadinicb"},
436 {Opt_adinicb, "adinicb"},
437 {Opt_shortad, "shortad"},
438 {Opt_longad, "longad"},
439 {Opt_uforget, "uid=forget"},
440 {Opt_uignore, "uid=ignore"},
441 {Opt_gforget, "gid=forget"},
442 {Opt_gignore, "gid=ignore"},
445 {Opt_umask, "umask=%o"},
446 {Opt_session, "session=%u"},
447 {Opt_lastblock, "lastblock=%u"},
448 {Opt_anchor, "anchor=%u"},
449 {Opt_volume, "volume=%u"},
450 {Opt_partition, "partition=%u"},
451 {Opt_fileset, "fileset=%u"},
452 {Opt_rootdir, "rootdir=%u"},
454 {Opt_iocharset, "iocharset=%s"},
455 {Opt_fmode, "mode=%o"},
456 {Opt_dmode, "dmode=%o"},
460 static int udf_parse_options(char *options, struct udf_options *uopt,
467 uopt->session = 0xFFFFFFFF;
474 while ((p = strsep(&options, ",")) != NULL) {
475 substring_t args[MAX_OPT_ARGS];
481 token = match_token(p, tokens, args);
487 if (match_int(&args[0], &option))
490 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
493 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
496 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
499 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
502 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
505 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
508 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
511 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
514 if (match_int(args, &option))
516 uopt->gid = make_kgid(current_user_ns(), option);
517 if (!gid_valid(uopt->gid))
519 uopt->flags |= (1 << UDF_FLAG_GID_SET);
522 if (match_int(args, &option))
524 uopt->uid = make_kuid(current_user_ns(), option);
525 if (!uid_valid(uopt->uid))
527 uopt->flags |= (1 << UDF_FLAG_UID_SET);
530 if (match_octal(args, &option))
532 uopt->umask = option;
535 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
538 if (match_int(args, &option))
540 uopt->session = option;
542 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
545 if (match_int(args, &option))
547 uopt->lastblock = option;
549 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
552 if (match_int(args, &option))
554 uopt->anchor = option;
560 /* Ignored (never implemented properly) */
563 uopt->flags |= (1 << UDF_FLAG_UTF8);
568 unload_nls(uopt->nls_map);
570 * load_nls() failure is handled later in
571 * udf_fill_super() after all options are
574 uopt->nls_map = load_nls(args[0].from);
575 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
579 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
583 /* These options are superseeded by uid=<number> */
586 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
589 if (match_octal(args, &option))
591 uopt->fmode = option & 0777;
594 if (match_octal(args, &option))
596 uopt->dmode = option & 0777;
599 pr_err("bad mount option \"%s\" or missing value\n", p);
606 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
608 struct udf_options uopt;
609 struct udf_sb_info *sbi = UDF_SB(sb);
612 if (!(*flags & SB_RDONLY) && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
617 uopt.flags = sbi->s_flags;
618 uopt.uid = sbi->s_uid;
619 uopt.gid = sbi->s_gid;
620 uopt.umask = sbi->s_umask;
621 uopt.fmode = sbi->s_fmode;
622 uopt.dmode = sbi->s_dmode;
625 if (!udf_parse_options(options, &uopt, true))
628 write_lock(&sbi->s_cred_lock);
629 sbi->s_flags = uopt.flags;
630 sbi->s_uid = uopt.uid;
631 sbi->s_gid = uopt.gid;
632 sbi->s_umask = uopt.umask;
633 sbi->s_fmode = uopt.fmode;
634 sbi->s_dmode = uopt.dmode;
635 write_unlock(&sbi->s_cred_lock);
637 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
640 if (*flags & SB_RDONLY)
649 /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
650 /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
651 static loff_t udf_check_vsd(struct super_block *sb)
653 struct volStructDesc *vsd = NULL;
654 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
656 struct buffer_head *bh = NULL;
659 struct udf_sb_info *sbi;
662 if (sb->s_blocksize < sizeof(struct volStructDesc))
663 sectorsize = sizeof(struct volStructDesc);
665 sectorsize = sb->s_blocksize;
667 sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits);
669 udf_debug("Starting at sector %u (%lu byte sectors)\n",
670 (unsigned int)(sector >> sb->s_blocksize_bits),
672 /* Process the sequence (if applicable). The hard limit on the sector
673 * offset is arbitrary, hopefully large enough so that all valid UDF
674 * filesystems will be recognised. There is no mention of an upper
675 * bound to the size of the volume recognition area in the standard.
676 * The limit will prevent the code to read all the sectors of a
677 * specially crafted image (like a bluray disc full of CD001 sectors),
678 * potentially causing minutes or even hours of uninterruptible I/O
679 * activity. This actually happened with uninitialised SSD partitions
680 * (all 0xFF) before the check for the limit and all valid IDs were
682 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
683 sector += sectorsize) {
685 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
689 /* Look for ISO descriptors */
690 vsd = (struct volStructDesc *)(bh->b_data +
691 (sector & (sb->s_blocksize - 1)));
693 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
695 switch (vsd->structType) {
697 udf_debug("ISO9660 Boot Record found\n");
700 udf_debug("ISO9660 Primary Volume Descriptor found\n");
703 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
706 udf_debug("ISO9660 Volume Partition Descriptor found\n");
709 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
712 udf_debug("ISO9660 VRS (%u) found\n",
716 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
719 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
723 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
726 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
729 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
732 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
736 /* invalid id : end of volume recognition area */
747 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
748 VSD_FIRST_SECTOR_OFFSET)
754 static int udf_find_fileset(struct super_block *sb,
755 struct kernel_lb_addr *fileset,
756 struct kernel_lb_addr *root)
758 struct buffer_head *bh = NULL;
761 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
762 fileset->partitionReferenceNum != 0xFFFF) {
763 bh = udf_read_ptagged(sb, fileset, 0, &ident);
767 } else if (ident != TAG_IDENT_FSD) {
772 udf_debug("Fileset at block=%u, partition=%u\n",
773 fileset->logicalBlockNum,
774 fileset->partitionReferenceNum);
776 UDF_SB(sb)->s_partition = fileset->partitionReferenceNum;
777 udf_load_fileset(sb, bh, root);
785 * Load primary Volume Descriptor Sequence
787 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
790 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
792 struct primaryVolDesc *pvoldesc;
794 struct buffer_head *bh;
798 struct timestamp *ts;
801 outstr = kmalloc(128, GFP_NOFS);
805 bh = udf_read_tagged(sb, block, block, &ident);
811 if (ident != TAG_IDENT_PVD) {
816 pvoldesc = (struct primaryVolDesc *)bh->b_data;
818 udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
819 pvoldesc->recordingDateAndTime);
821 ts = &pvoldesc->recordingDateAndTime;
822 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
823 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
824 ts->minute, le16_to_cpu(ts->typeAndTimezone));
828 ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
830 strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName");
831 pr_warn("incorrect volume identification, setting to "
834 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
836 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
838 ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
844 udf_debug("volSetIdent[] = '%s'\n", outstr);
854 struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
855 u32 meta_file_loc, u32 partition_ref)
857 struct kernel_lb_addr addr;
858 struct inode *metadata_fe;
860 addr.logicalBlockNum = meta_file_loc;
861 addr.partitionReferenceNum = partition_ref;
863 metadata_fe = udf_iget_special(sb, &addr);
865 if (IS_ERR(metadata_fe)) {
866 udf_warn(sb, "metadata inode efe not found\n");
869 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
870 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
872 return ERR_PTR(-EIO);
878 static int udf_load_metadata_files(struct super_block *sb, int partition,
881 struct udf_sb_info *sbi = UDF_SB(sb);
882 struct udf_part_map *map;
883 struct udf_meta_data *mdata;
884 struct kernel_lb_addr addr;
887 map = &sbi->s_partmaps[partition];
888 mdata = &map->s_type_specific.s_metadata;
889 mdata->s_phys_partition_ref = type1_index;
891 /* metadata address */
892 udf_debug("Metadata file location: block = %u part = %u\n",
893 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
895 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
896 mdata->s_phys_partition_ref);
898 /* mirror file entry */
899 udf_debug("Mirror metadata file location: block = %u part = %u\n",
900 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
902 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
903 mdata->s_phys_partition_ref);
906 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
909 mdata->s_mirror_fe = fe;
911 mdata->s_metadata_fe = fe;
917 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
919 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
920 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
921 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
923 udf_debug("Bitmap file location: block = %u part = %u\n",
924 addr.logicalBlockNum, addr.partitionReferenceNum);
926 fe = udf_iget_special(sb, &addr);
929 udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
931 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
935 mdata->s_bitmap_fe = fe;
938 udf_debug("udf_load_metadata_files Ok\n");
942 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
943 struct kernel_lb_addr *root)
945 struct fileSetDesc *fset;
947 fset = (struct fileSetDesc *)bh->b_data;
949 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
951 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
953 udf_debug("Rootdir at block=%u, partition=%u\n",
954 root->logicalBlockNum, root->partitionReferenceNum);
957 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
959 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
960 return DIV_ROUND_UP(map->s_partition_len +
961 (sizeof(struct spaceBitmapDesc) << 3),
962 sb->s_blocksize * 8);
965 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
967 struct udf_bitmap *bitmap;
971 nr_groups = udf_compute_nr_groups(sb, index);
972 size = sizeof(struct udf_bitmap) +
973 (sizeof(struct buffer_head *) * nr_groups);
975 if (size <= PAGE_SIZE)
976 bitmap = kzalloc(size, GFP_KERNEL);
978 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
983 bitmap->s_nr_groups = nr_groups;
987 static int check_partition_desc(struct super_block *sb,
988 struct partitionDesc *p,
989 struct udf_part_map *map)
991 bool umap, utable, fmap, ftable;
992 struct partitionHeaderDesc *phd;
994 switch (le32_to_cpu(p->accessType)) {
995 case PD_ACCESS_TYPE_READ_ONLY:
996 case PD_ACCESS_TYPE_WRITE_ONCE:
997 case PD_ACCESS_TYPE_REWRITABLE:
998 case PD_ACCESS_TYPE_NONE:
1002 /* No Partition Header Descriptor? */
1003 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1004 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1007 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1008 utable = phd->unallocSpaceTable.extLength;
1009 umap = phd->unallocSpaceBitmap.extLength;
1010 ftable = phd->freedSpaceTable.extLength;
1011 fmap = phd->freedSpaceBitmap.extLength;
1013 /* No allocation info? */
1014 if (!utable && !umap && !ftable && !fmap)
1017 /* We don't support blocks that require erasing before overwrite */
1020 /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
1024 if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1025 map->s_partition_type == UDF_VIRTUAL_MAP20)
1032 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1036 static int udf_fill_partdesc_info(struct super_block *sb,
1037 struct partitionDesc *p, int p_index)
1039 struct udf_part_map *map;
1040 struct udf_sb_info *sbi = UDF_SB(sb);
1041 struct partitionHeaderDesc *phd;
1044 map = &sbi->s_partmaps[p_index];
1046 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1047 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1049 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1050 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1051 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1052 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1053 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1054 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1055 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1056 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1058 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
1059 p_index, map->s_partition_type,
1060 map->s_partition_root, map->s_partition_len);
1062 err = check_partition_desc(sb, p, map);
1067 * Skip loading allocation info it we cannot ever write to the fs.
1068 * This is a correctness thing as we may have decided to force ro mount
1069 * to avoid allocation info we don't support.
1071 if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
1074 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1075 if (phd->unallocSpaceTable.extLength) {
1076 struct kernel_lb_addr loc = {
1077 .logicalBlockNum = le32_to_cpu(
1078 phd->unallocSpaceTable.extPosition),
1079 .partitionReferenceNum = p_index,
1081 struct inode *inode;
1083 inode = udf_iget_special(sb, &loc);
1084 if (IS_ERR(inode)) {
1085 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1087 return PTR_ERR(inode);
1089 map->s_uspace.s_table = inode;
1090 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1091 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
1092 p_index, map->s_uspace.s_table->i_ino);
1095 if (phd->unallocSpaceBitmap.extLength) {
1096 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1099 map->s_uspace.s_bitmap = bitmap;
1100 bitmap->s_extPosition = le32_to_cpu(
1101 phd->unallocSpaceBitmap.extPosition);
1102 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1103 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
1104 p_index, bitmap->s_extPosition);
1110 static void udf_find_vat_block(struct super_block *sb, int p_index,
1111 int type1_index, sector_t start_block)
1113 struct udf_sb_info *sbi = UDF_SB(sb);
1114 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1116 struct kernel_lb_addr ino;
1117 struct inode *inode;
1120 * VAT file entry is in the last recorded block. Some broken disks have
1121 * it a few blocks before so try a bit harder...
1123 ino.partitionReferenceNum = type1_index;
1124 for (vat_block = start_block;
1125 vat_block >= map->s_partition_root &&
1126 vat_block >= start_block - 3; vat_block--) {
1127 ino.logicalBlockNum = vat_block - map->s_partition_root;
1128 inode = udf_iget_special(sb, &ino);
1129 if (!IS_ERR(inode)) {
1130 sbi->s_vat_inode = inode;
1136 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1138 struct udf_sb_info *sbi = UDF_SB(sb);
1139 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1140 struct buffer_head *bh = NULL;
1141 struct udf_inode_info *vati;
1143 struct virtualAllocationTable20 *vat20;
1144 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1145 sb->s_blocksize_bits;
1147 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
1148 if (!sbi->s_vat_inode &&
1149 sbi->s_last_block != blocks - 1) {
1150 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1151 (unsigned long)sbi->s_last_block,
1152 (unsigned long)blocks - 1);
1153 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
1155 if (!sbi->s_vat_inode)
1158 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1159 map->s_type_specific.s_virtual.s_start_offset = 0;
1160 map->s_type_specific.s_virtual.s_num_entries =
1161 (sbi->s_vat_inode->i_size - 36) >> 2;
1162 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1163 vati = UDF_I(sbi->s_vat_inode);
1164 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1165 pos = udf_block_map(sbi->s_vat_inode, 0);
1166 bh = sb_bread(sb, pos);
1169 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1171 vat20 = (struct virtualAllocationTable20 *)
1175 map->s_type_specific.s_virtual.s_start_offset =
1176 le16_to_cpu(vat20->lengthHeader);
1177 map->s_type_specific.s_virtual.s_num_entries =
1178 (sbi->s_vat_inode->i_size -
1179 map->s_type_specific.s_virtual.
1180 s_start_offset) >> 2;
1187 * Load partition descriptor block
1189 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1192 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1194 struct buffer_head *bh;
1195 struct partitionDesc *p;
1196 struct udf_part_map *map;
1197 struct udf_sb_info *sbi = UDF_SB(sb);
1199 uint16_t partitionNumber;
1203 bh = udf_read_tagged(sb, block, block, &ident);
1206 if (ident != TAG_IDENT_PD) {
1211 p = (struct partitionDesc *)bh->b_data;
1212 partitionNumber = le16_to_cpu(p->partitionNumber);
1214 /* First scan for TYPE1 and SPARABLE partitions */
1215 for (i = 0; i < sbi->s_partitions; i++) {
1216 map = &sbi->s_partmaps[i];
1217 udf_debug("Searching map: (%u == %u)\n",
1218 map->s_partition_num, partitionNumber);
1219 if (map->s_partition_num == partitionNumber &&
1220 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1221 map->s_partition_type == UDF_SPARABLE_MAP15))
1225 if (i >= sbi->s_partitions) {
1226 udf_debug("Partition (%u) not found in partition map\n",
1232 ret = udf_fill_partdesc_info(sb, p, i);
1237 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1238 * PHYSICAL partitions are already set up
1242 map = NULL; /* supress 'maybe used uninitialized' warning */
1244 for (i = 0; i < sbi->s_partitions; i++) {
1245 map = &sbi->s_partmaps[i];
1247 if (map->s_partition_num == partitionNumber &&
1248 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1249 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1250 map->s_partition_type == UDF_METADATA_MAP25))
1254 if (i >= sbi->s_partitions) {
1259 ret = udf_fill_partdesc_info(sb, p, i);
1263 if (map->s_partition_type == UDF_METADATA_MAP25) {
1264 ret = udf_load_metadata_files(sb, i, type1_idx);
1266 udf_err(sb, "error loading MetaData partition map %d\n",
1272 * If we have a partition with virtual map, we don't handle
1273 * writing to it (we overwrite blocks instead of relocating
1276 if (!sb_rdonly(sb)) {
1280 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1281 ret = udf_load_vat(sb, i, type1_idx);
1287 /* In case loading failed, we handle cleanup in udf_fill_super */
1292 static int udf_load_sparable_map(struct super_block *sb,
1293 struct udf_part_map *map,
1294 struct sparablePartitionMap *spm)
1298 struct sparingTable *st;
1299 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1301 struct buffer_head *bh;
1303 map->s_partition_type = UDF_SPARABLE_MAP15;
1304 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1305 if (!is_power_of_2(sdata->s_packet_len)) {
1306 udf_err(sb, "error loading logical volume descriptor: "
1307 "Invalid packet length %u\n",
1308 (unsigned)sdata->s_packet_len);
1311 if (spm->numSparingTables > 4) {
1312 udf_err(sb, "error loading logical volume descriptor: "
1313 "Too many sparing tables (%d)\n",
1314 (int)spm->numSparingTables);
1318 for (i = 0; i < spm->numSparingTables; i++) {
1319 loc = le32_to_cpu(spm->locSparingTable[i]);
1320 bh = udf_read_tagged(sb, loc, loc, &ident);
1324 st = (struct sparingTable *)bh->b_data;
1326 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1327 strlen(UDF_ID_SPARING)) ||
1328 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1334 sdata->s_spar_map[i] = bh;
1336 map->s_partition_func = udf_get_pblock_spar15;
1340 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1341 struct kernel_lb_addr *fileset)
1343 struct logicalVolDesc *lvd;
1346 struct udf_sb_info *sbi = UDF_SB(sb);
1347 struct genericPartitionMap *gpm;
1349 struct buffer_head *bh;
1350 unsigned int table_len;
1353 bh = udf_read_tagged(sb, block, block, &ident);
1356 BUG_ON(ident != TAG_IDENT_LVD);
1357 lvd = (struct logicalVolDesc *)bh->b_data;
1358 table_len = le32_to_cpu(lvd->mapTableLength);
1359 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
1360 udf_err(sb, "error loading logical volume descriptor: "
1361 "Partition table too long (%u > %lu)\n", table_len,
1362 sb->s_blocksize - sizeof(*lvd));
1367 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1371 for (i = 0, offset = 0;
1372 i < sbi->s_partitions && offset < table_len;
1373 i++, offset += gpm->partitionMapLength) {
1374 struct udf_part_map *map = &sbi->s_partmaps[i];
1375 gpm = (struct genericPartitionMap *)
1376 &(lvd->partitionMaps[offset]);
1377 type = gpm->partitionMapType;
1379 struct genericPartitionMap1 *gpm1 =
1380 (struct genericPartitionMap1 *)gpm;
1381 map->s_partition_type = UDF_TYPE1_MAP15;
1382 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1383 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1384 map->s_partition_func = NULL;
1385 } else if (type == 2) {
1386 struct udfPartitionMap2 *upm2 =
1387 (struct udfPartitionMap2 *)gpm;
1388 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1389 strlen(UDF_ID_VIRTUAL))) {
1391 le16_to_cpu(((__le16 *)upm2->partIdent.
1394 map->s_partition_type =
1396 map->s_partition_func =
1397 udf_get_pblock_virt15;
1399 map->s_partition_type =
1401 map->s_partition_func =
1402 udf_get_pblock_virt20;
1404 } else if (!strncmp(upm2->partIdent.ident,
1406 strlen(UDF_ID_SPARABLE))) {
1407 ret = udf_load_sparable_map(sb, map,
1408 (struct sparablePartitionMap *)gpm);
1411 } else if (!strncmp(upm2->partIdent.ident,
1413 strlen(UDF_ID_METADATA))) {
1414 struct udf_meta_data *mdata =
1415 &map->s_type_specific.s_metadata;
1416 struct metadataPartitionMap *mdm =
1417 (struct metadataPartitionMap *)
1418 &(lvd->partitionMaps[offset]);
1419 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
1420 i, type, UDF_ID_METADATA);
1422 map->s_partition_type = UDF_METADATA_MAP25;
1423 map->s_partition_func = udf_get_pblock_meta25;
1425 mdata->s_meta_file_loc =
1426 le32_to_cpu(mdm->metadataFileLoc);
1427 mdata->s_mirror_file_loc =
1428 le32_to_cpu(mdm->metadataMirrorFileLoc);
1429 mdata->s_bitmap_file_loc =
1430 le32_to_cpu(mdm->metadataBitmapFileLoc);
1431 mdata->s_alloc_unit_size =
1432 le32_to_cpu(mdm->allocUnitSize);
1433 mdata->s_align_unit_size =
1434 le16_to_cpu(mdm->alignUnitSize);
1435 if (mdm->flags & 0x01)
1436 mdata->s_flags |= MF_DUPLICATE_MD;
1438 udf_debug("Metadata Ident suffix=0x%x\n",
1439 le16_to_cpu(*(__le16 *)
1440 mdm->partIdent.identSuffix));
1441 udf_debug("Metadata part num=%u\n",
1442 le16_to_cpu(mdm->partitionNum));
1443 udf_debug("Metadata part alloc unit size=%u\n",
1444 le32_to_cpu(mdm->allocUnitSize));
1445 udf_debug("Metadata file loc=%u\n",
1446 le32_to_cpu(mdm->metadataFileLoc));
1447 udf_debug("Mirror file loc=%u\n",
1448 le32_to_cpu(mdm->metadataMirrorFileLoc));
1449 udf_debug("Bitmap file loc=%u\n",
1450 le32_to_cpu(mdm->metadataBitmapFileLoc));
1451 udf_debug("Flags: %d %u\n",
1452 mdata->s_flags, mdm->flags);
1454 udf_debug("Unknown ident: %s\n",
1455 upm2->partIdent.ident);
1458 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1459 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1461 udf_debug("Partition (%d:%u) type %u on volume %u\n",
1462 i, map->s_partition_num, type, map->s_volumeseqnum);
1466 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1468 *fileset = lelb_to_cpu(la->extLocation);
1469 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
1470 fileset->logicalBlockNum,
1471 fileset->partitionReferenceNum);
1473 if (lvd->integritySeqExt.extLength)
1474 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1477 if (!sbi->s_lvid_bh) {
1478 /* We can't generate unique IDs without a valid LVID */
1479 if (sb_rdonly(sb)) {
1480 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1482 udf_warn(sb, "Damaged or missing LVID, forcing "
1483 "readonly mount\n");
1493 * Find the prevailing Logical Volume Integrity Descriptor.
1495 static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1497 struct buffer_head *bh, *final_bh;
1499 struct udf_sb_info *sbi = UDF_SB(sb);
1500 struct logicalVolIntegrityDesc *lvid;
1501 int indirections = 0;
1503 while (++indirections <= UDF_MAX_LVID_NESTING) {
1505 while (loc.extLength > 0 &&
1506 (bh = udf_read_tagged(sb, loc.extLocation,
1507 loc.extLocation, &ident))) {
1508 if (ident != TAG_IDENT_LVID) {
1516 loc.extLength -= sb->s_blocksize;
1523 brelse(sbi->s_lvid_bh);
1524 sbi->s_lvid_bh = final_bh;
1526 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1527 if (lvid->nextIntegrityExt.extLength == 0)
1530 loc = leea_to_cpu(lvid->nextIntegrityExt);
1533 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1534 UDF_MAX_LVID_NESTING);
1535 brelse(sbi->s_lvid_bh);
1536 sbi->s_lvid_bh = NULL;
1540 * Step for reallocation of table of partition descriptor sequence numbers.
1541 * Must be power of 2.
1543 #define PART_DESC_ALLOC_STEP 32
1545 struct part_desc_seq_scan_data {
1546 struct udf_vds_record rec;
1550 struct desc_seq_scan_data {
1551 struct udf_vds_record vds[VDS_POS_LENGTH];
1552 unsigned int size_part_descs;
1553 unsigned int num_part_descs;
1554 struct part_desc_seq_scan_data *part_descs_loc;
1557 static struct udf_vds_record *handle_partition_descriptor(
1558 struct buffer_head *bh,
1559 struct desc_seq_scan_data *data)
1561 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1565 partnum = le16_to_cpu(desc->partitionNumber);
1566 for (i = 0; i < data->num_part_descs; i++)
1567 if (partnum == data->part_descs_loc[i].partnum)
1568 return &(data->part_descs_loc[i].rec);
1569 if (data->num_part_descs >= data->size_part_descs) {
1570 struct part_desc_seq_scan_data *new_loc;
1571 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1573 new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
1575 return ERR_PTR(-ENOMEM);
1576 memcpy(new_loc, data->part_descs_loc,
1577 data->size_part_descs * sizeof(*new_loc));
1578 kfree(data->part_descs_loc);
1579 data->part_descs_loc = new_loc;
1580 data->size_part_descs = new_size;
1582 return &(data->part_descs_loc[data->num_part_descs++].rec);
1586 static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1587 struct buffer_head *bh, struct desc_seq_scan_data *data)
1590 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1591 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
1592 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1593 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
1594 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1595 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
1596 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1597 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1598 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1599 return handle_partition_descriptor(bh, data);
1605 * Process a main/reserve volume descriptor sequence.
1606 * @block First block of first extent of the sequence.
1607 * @lastblock Lastblock of first extent of the sequence.
1608 * @fileset There we store extent containing root fileset
1610 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1613 static noinline int udf_process_sequence(
1614 struct super_block *sb,
1615 sector_t block, sector_t lastblock,
1616 struct kernel_lb_addr *fileset)
1618 struct buffer_head *bh = NULL;
1619 struct udf_vds_record *curr;
1620 struct generic_desc *gd;
1621 struct volDescPtr *vdp;
1626 unsigned int indirections = 0;
1627 struct desc_seq_scan_data data;
1630 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1631 data.size_part_descs = PART_DESC_ALLOC_STEP;
1632 data.num_part_descs = 0;
1633 data.part_descs_loc = kcalloc(data.size_part_descs,
1634 sizeof(*data.part_descs_loc),
1636 if (!data.part_descs_loc)
1640 * Read the main descriptor sequence and find which descriptors
1643 for (; (!done && block <= lastblock); block++) {
1644 bh = udf_read_tagged(sb, block, block, &ident);
1648 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1649 gd = (struct generic_desc *)bh->b_data;
1650 vdsn = le32_to_cpu(gd->volDescSeqNum);
1652 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1653 if (++indirections > UDF_MAX_TD_NESTING) {
1654 udf_err(sb, "too many Volume Descriptor "
1655 "Pointers (max %u supported)\n",
1656 UDF_MAX_TD_NESTING);
1661 vdp = (struct volDescPtr *)bh->b_data;
1662 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1663 lastblock = le32_to_cpu(
1664 vdp->nextVolDescSeqExt.extLength) >>
1665 sb->s_blocksize_bits;
1666 lastblock += block - 1;
1667 /* For loop is going to increment 'block' again */
1670 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1671 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1672 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1673 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1674 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1675 curr = get_volume_descriptor_record(ident, bh, &data);
1678 return PTR_ERR(curr);
1680 /* Descriptor we don't care about? */
1683 if (vdsn >= curr->volDescSeqNum) {
1684 curr->volDescSeqNum = vdsn;
1685 curr->block = block;
1688 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1695 * Now read interesting descriptors again and process them
1696 * in a suitable order
1698 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1699 udf_err(sb, "Primary Volume Descriptor not found!\n");
1702 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
1706 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
1707 ret = udf_load_logicalvol(sb,
1708 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1714 /* Now handle prevailing Partition Descriptors */
1715 for (i = 0; i < data.num_part_descs; i++) {
1716 ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
1725 * Load Volume Descriptor Sequence described by anchor in bh
1727 * Returns <0 on error, 0 on success
1729 static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1730 struct kernel_lb_addr *fileset)
1732 struct anchorVolDescPtr *anchor;
1733 sector_t main_s, main_e, reserve_s, reserve_e;
1736 anchor = (struct anchorVolDescPtr *)bh->b_data;
1738 /* Locate the main sequence */
1739 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1740 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1741 main_e = main_e >> sb->s_blocksize_bits;
1742 main_e += main_s - 1;
1744 /* Locate the reserve sequence */
1745 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1746 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1747 reserve_e = reserve_e >> sb->s_blocksize_bits;
1748 reserve_e += reserve_s - 1;
1750 /* Process the main & reserve sequences */
1751 /* responsible for finding the PartitionDesc(s) */
1752 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1755 udf_sb_free_partitions(sb);
1756 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1758 udf_sb_free_partitions(sb);
1759 /* No sequence was OK, return -EIO */
1767 * Check whether there is an anchor block in the given block and
1768 * load Volume Descriptor Sequence if so.
1770 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1773 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1774 struct kernel_lb_addr *fileset)
1776 struct buffer_head *bh;
1780 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1781 udf_fixed_to_variable(block) >=
1782 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
1785 bh = udf_read_tagged(sb, block, block, &ident);
1788 if (ident != TAG_IDENT_AVDP) {
1792 ret = udf_load_sequence(sb, bh, fileset);
1798 * Search for an anchor volume descriptor pointer.
1800 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1803 static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1804 struct kernel_lb_addr *fileset)
1808 struct udf_sb_info *sbi = UDF_SB(sb);
1812 /* First try user provided anchor */
1813 if (sbi->s_anchor) {
1814 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1819 * according to spec, anchor is in either:
1823 * however, if the disc isn't closed, it could be 512.
1825 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1829 * The trouble is which block is the last one. Drives often misreport
1830 * this so we try various possibilities.
1832 last[last_count++] = *lastblock;
1833 if (*lastblock >= 1)
1834 last[last_count++] = *lastblock - 1;
1835 last[last_count++] = *lastblock + 1;
1836 if (*lastblock >= 2)
1837 last[last_count++] = *lastblock - 2;
1838 if (*lastblock >= 150)
1839 last[last_count++] = *lastblock - 150;
1840 if (*lastblock >= 152)
1841 last[last_count++] = *lastblock - 152;
1843 for (i = 0; i < last_count; i++) {
1844 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
1845 sb->s_blocksize_bits)
1847 ret = udf_check_anchor_block(sb, last[i], fileset);
1848 if (ret != -EAGAIN) {
1850 *lastblock = last[i];
1855 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1856 if (ret != -EAGAIN) {
1858 *lastblock = last[i];
1863 /* Finally try block 512 in case media is open */
1864 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
1868 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1869 * area specified by it. The function expects sbi->s_lastblock to be the last
1870 * block on the media.
1872 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1875 static int udf_find_anchor(struct super_block *sb,
1876 struct kernel_lb_addr *fileset)
1878 struct udf_sb_info *sbi = UDF_SB(sb);
1879 sector_t lastblock = sbi->s_last_block;
1882 ret = udf_scan_anchors(sb, &lastblock, fileset);
1886 /* No anchor found? Try VARCONV conversion of block numbers */
1887 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1888 lastblock = udf_variable_to_fixed(sbi->s_last_block);
1889 /* Firstly, we try to not convert number of the last block */
1890 ret = udf_scan_anchors(sb, &lastblock, fileset);
1894 lastblock = sbi->s_last_block;
1895 /* Secondly, we try with converted number of the last block */
1896 ret = udf_scan_anchors(sb, &lastblock, fileset);
1898 /* VARCONV didn't help. Clear it. */
1899 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1903 sbi->s_last_block = lastblock;
1908 * Check Volume Structure Descriptor, find Anchor block and load Volume
1909 * Descriptor Sequence.
1911 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1912 * block was not found.
1914 static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1915 int silent, struct kernel_lb_addr *fileset)
1917 struct udf_sb_info *sbi = UDF_SB(sb);
1921 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1923 udf_warn(sb, "Bad block size\n");
1926 sbi->s_last_block = uopt->lastblock;
1928 /* Check that it is NSR02 compliant */
1929 nsr_off = udf_check_vsd(sb);
1932 udf_warn(sb, "No VRS found\n");
1936 udf_debug("Failed to read sector at offset %d. "
1937 "Assuming open disc. Skipping validity "
1938 "check\n", VSD_FIRST_SECTOR_OFFSET);
1939 if (!sbi->s_last_block)
1940 sbi->s_last_block = udf_get_last_block(sb);
1942 udf_debug("Validity check skipped because of novrs option\n");
1945 /* Look for anchor block and load Volume Descriptor Sequence */
1946 sbi->s_anchor = uopt->anchor;
1947 ret = udf_find_anchor(sb, fileset);
1949 if (!silent && ret == -EAGAIN)
1950 udf_warn(sb, "No anchor found\n");
1956 static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
1958 struct timespec64 ts;
1960 ktime_get_real_ts64(&ts);
1961 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
1962 lvid->descTag.descCRC = cpu_to_le16(
1963 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
1964 le16_to_cpu(lvid->descTag.descCRCLength)));
1965 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1968 static void udf_open_lvid(struct super_block *sb)
1970 struct udf_sb_info *sbi = UDF_SB(sb);
1971 struct buffer_head *bh = sbi->s_lvid_bh;
1972 struct logicalVolIntegrityDesc *lvid;
1973 struct logicalVolIntegrityDescImpUse *lvidiu;
1977 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1978 lvidiu = udf_sb_lvidiu(sb);
1982 mutex_lock(&sbi->s_alloc_mutex);
1983 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1984 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1985 if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
1986 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
1988 UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
1990 udf_finalize_lvid(lvid);
1991 mark_buffer_dirty(bh);
1992 sbi->s_lvid_dirty = 0;
1993 mutex_unlock(&sbi->s_alloc_mutex);
1994 /* Make opening of filesystem visible on the media immediately */
1995 sync_dirty_buffer(bh);
1998 static void udf_close_lvid(struct super_block *sb)
2000 struct udf_sb_info *sbi = UDF_SB(sb);
2001 struct buffer_head *bh = sbi->s_lvid_bh;
2002 struct logicalVolIntegrityDesc *lvid;
2003 struct logicalVolIntegrityDescImpUse *lvidiu;
2007 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2008 lvidiu = udf_sb_lvidiu(sb);
2012 mutex_lock(&sbi->s_alloc_mutex);
2013 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2014 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
2015 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2016 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2017 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2018 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2019 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2020 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
2021 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
2022 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
2025 * We set buffer uptodate unconditionally here to avoid spurious
2026 * warnings from mark_buffer_dirty() when previous EIO has marked
2027 * the buffer as !uptodate
2029 set_buffer_uptodate(bh);
2030 udf_finalize_lvid(lvid);
2031 mark_buffer_dirty(bh);
2032 sbi->s_lvid_dirty = 0;
2033 mutex_unlock(&sbi->s_alloc_mutex);
2034 /* Make closing of filesystem visible on the media immediately */
2035 sync_dirty_buffer(bh);
2038 u64 lvid_get_unique_id(struct super_block *sb)
2040 struct buffer_head *bh;
2041 struct udf_sb_info *sbi = UDF_SB(sb);
2042 struct logicalVolIntegrityDesc *lvid;
2043 struct logicalVolHeaderDesc *lvhd;
2047 bh = sbi->s_lvid_bh;
2051 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2052 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2054 mutex_lock(&sbi->s_alloc_mutex);
2055 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2056 if (!(++uniqueID & 0xFFFFFFFF))
2058 lvhd->uniqueID = cpu_to_le64(uniqueID);
2059 udf_updated_lvid(sb);
2060 mutex_unlock(&sbi->s_alloc_mutex);
2065 static int udf_fill_super(struct super_block *sb, void *options, int silent)
2068 struct inode *inode = NULL;
2069 struct udf_options uopt;
2070 struct kernel_lb_addr rootdir, fileset;
2071 struct udf_sb_info *sbi;
2072 bool lvid_open = false;
2074 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
2075 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2076 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2077 uopt.gid = make_kgid(current_user_ns(), overflowgid);
2079 uopt.fmode = UDF_INVALID_MODE;
2080 uopt.dmode = UDF_INVALID_MODE;
2081 uopt.nls_map = NULL;
2083 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2087 sb->s_fs_info = sbi;
2089 mutex_init(&sbi->s_alloc_mutex);
2091 if (!udf_parse_options((char *)options, &uopt, false))
2092 goto parse_options_failure;
2094 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
2095 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
2096 udf_err(sb, "utf8 cannot be combined with iocharset\n");
2097 goto parse_options_failure;
2099 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
2100 uopt.nls_map = load_nls_default();
2102 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2104 udf_debug("Using default NLS map\n");
2106 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2107 uopt.flags |= (1 << UDF_FLAG_UTF8);
2109 fileset.logicalBlockNum = 0xFFFFFFFF;
2110 fileset.partitionReferenceNum = 0xFFFF;
2112 sbi->s_flags = uopt.flags;
2113 sbi->s_uid = uopt.uid;
2114 sbi->s_gid = uopt.gid;
2115 sbi->s_umask = uopt.umask;
2116 sbi->s_fmode = uopt.fmode;
2117 sbi->s_dmode = uopt.dmode;
2118 sbi->s_nls_map = uopt.nls_map;
2119 rwlock_init(&sbi->s_cred_lock);
2121 if (uopt.session == 0xFFFFFFFF)
2122 sbi->s_session = udf_get_last_session(sb);
2124 sbi->s_session = uopt.session;
2126 udf_debug("Multi-session=%d\n", sbi->s_session);
2128 /* Fill in the rest of the superblock */
2129 sb->s_op = &udf_sb_ops;
2130 sb->s_export_op = &udf_export_ops;
2132 sb->s_magic = UDF_SUPER_MAGIC;
2133 sb->s_time_gran = 1000;
2135 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2136 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2138 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
2139 while (uopt.blocksize <= 4096) {
2140 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2142 if (!silent && ret != -EACCES) {
2143 pr_notice("Scanning with blocksize %u failed\n",
2146 brelse(sbi->s_lvid_bh);
2147 sbi->s_lvid_bh = NULL;
2149 * EACCES is special - we want to propagate to
2150 * upper layers that we cannot handle RW mount.
2157 uopt.blocksize <<= 1;
2161 if (ret == -EAGAIN) {
2162 udf_warn(sb, "No partition found (1)\n");
2168 udf_debug("Lastblock=%u\n", sbi->s_last_block);
2170 if (sbi->s_lvid_bh) {
2171 struct logicalVolIntegrityDescImpUse *lvidiu =
2173 uint16_t minUDFReadRev;
2174 uint16_t minUDFWriteRev;
2180 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2181 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
2182 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
2183 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
2185 UDF_MAX_READ_VERSION);
2188 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
2189 if (!sb_rdonly(sb)) {
2193 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
2196 sbi->s_udfrev = minUDFWriteRev;
2198 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2199 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2200 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2201 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2204 if (!sbi->s_partitions) {
2205 udf_warn(sb, "No partition found (2)\n");
2210 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2211 UDF_PART_FLAG_READ_ONLY) {
2212 if (!sb_rdonly(sb)) {
2216 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
2219 if (udf_find_fileset(sb, &fileset, &rootdir)) {
2220 udf_warn(sb, "No fileset found\n");
2226 struct timestamp ts;
2227 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
2228 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2229 sbi->s_volume_ident,
2230 le16_to_cpu(ts.year), ts.month, ts.day,
2231 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
2233 if (!sb_rdonly(sb)) {
2238 /* Assign the root inode */
2239 /* assign inodes by physical block number */
2240 /* perhaps it's not extensible enough, but for now ... */
2241 inode = udf_iget(sb, &rootdir);
2242 if (IS_ERR(inode)) {
2243 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
2244 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2245 ret = PTR_ERR(inode);
2249 /* Allocate a dentry for the root inode */
2250 sb->s_root = d_make_root(inode);
2252 udf_err(sb, "Couldn't allocate root dentry\n");
2256 sb->s_maxbytes = MAX_LFS_FILESIZE;
2257 sb->s_max_links = UDF_MAX_LINKS;
2261 iput(sbi->s_vat_inode);
2262 parse_options_failure:
2264 unload_nls(uopt.nls_map);
2267 brelse(sbi->s_lvid_bh);
2268 udf_sb_free_partitions(sb);
2270 sb->s_fs_info = NULL;
2275 void _udf_err(struct super_block *sb, const char *function,
2276 const char *fmt, ...)
2278 struct va_format vaf;
2281 va_start(args, fmt);
2286 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2291 void _udf_warn(struct super_block *sb, const char *function,
2292 const char *fmt, ...)
2294 struct va_format vaf;
2297 va_start(args, fmt);
2302 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2307 static void udf_put_super(struct super_block *sb)
2309 struct udf_sb_info *sbi;
2313 iput(sbi->s_vat_inode);
2314 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2315 unload_nls(sbi->s_nls_map);
2318 brelse(sbi->s_lvid_bh);
2319 udf_sb_free_partitions(sb);
2320 mutex_destroy(&sbi->s_alloc_mutex);
2321 kfree(sb->s_fs_info);
2322 sb->s_fs_info = NULL;
2325 static int udf_sync_fs(struct super_block *sb, int wait)
2327 struct udf_sb_info *sbi = UDF_SB(sb);
2329 mutex_lock(&sbi->s_alloc_mutex);
2330 if (sbi->s_lvid_dirty) {
2331 struct buffer_head *bh = sbi->s_lvid_bh;
2332 struct logicalVolIntegrityDesc *lvid;
2334 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2335 udf_finalize_lvid(lvid);
2338 * Blockdevice will be synced later so we don't have to submit
2341 mark_buffer_dirty(bh);
2342 sbi->s_lvid_dirty = 0;
2344 mutex_unlock(&sbi->s_alloc_mutex);
2349 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2351 struct super_block *sb = dentry->d_sb;
2352 struct udf_sb_info *sbi = UDF_SB(sb);
2353 struct logicalVolIntegrityDescImpUse *lvidiu;
2354 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
2356 lvidiu = udf_sb_lvidiu(sb);
2357 buf->f_type = UDF_SUPER_MAGIC;
2358 buf->f_bsize = sb->s_blocksize;
2359 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2360 buf->f_bfree = udf_count_free(sb);
2361 buf->f_bavail = buf->f_bfree;
2362 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2363 le32_to_cpu(lvidiu->numDirs)) : 0)
2365 buf->f_ffree = buf->f_bfree;
2366 buf->f_namelen = UDF_NAME_LEN;
2367 buf->f_fsid.val[0] = (u32)id;
2368 buf->f_fsid.val[1] = (u32)(id >> 32);
2373 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2374 struct udf_bitmap *bitmap)
2376 struct buffer_head *bh = NULL;
2377 unsigned int accum = 0;
2379 udf_pblk_t block = 0, newblock;
2380 struct kernel_lb_addr loc;
2384 struct spaceBitmapDesc *bm;
2386 loc.logicalBlockNum = bitmap->s_extPosition;
2387 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2388 bh = udf_read_ptagged(sb, &loc, 0, &ident);
2391 udf_err(sb, "udf_count_free failed\n");
2393 } else if (ident != TAG_IDENT_SBD) {
2395 udf_err(sb, "udf_count_free failed\n");
2399 bm = (struct spaceBitmapDesc *)bh->b_data;
2400 bytes = le32_to_cpu(bm->numOfBytes);
2401 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2402 ptr = (uint8_t *)bh->b_data;
2405 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2406 accum += bitmap_weight((const unsigned long *)(ptr + index),
2411 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2412 bh = udf_tread(sb, newblock);
2414 udf_debug("read failed\n");
2418 ptr = (uint8_t *)bh->b_data;
2426 static unsigned int udf_count_free_table(struct super_block *sb,
2427 struct inode *table)
2429 unsigned int accum = 0;
2431 struct kernel_lb_addr eloc;
2433 struct extent_position epos;
2435 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
2436 epos.block = UDF_I(table)->i_location;
2437 epos.offset = sizeof(struct unallocSpaceEntry);
2440 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2441 accum += (elen >> table->i_sb->s_blocksize_bits);
2444 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
2449 static unsigned int udf_count_free(struct super_block *sb)
2451 unsigned int accum = 0;
2452 struct udf_sb_info *sbi;
2453 struct udf_part_map *map;
2456 if (sbi->s_lvid_bh) {
2457 struct logicalVolIntegrityDesc *lvid =
2458 (struct logicalVolIntegrityDesc *)
2459 sbi->s_lvid_bh->b_data;
2460 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2461 accum = le32_to_cpu(
2462 lvid->freeSpaceTable[sbi->s_partition]);
2463 if (accum == 0xFFFFFFFF)
2471 map = &sbi->s_partmaps[sbi->s_partition];
2472 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2473 accum += udf_count_free_bitmap(sb,
2474 map->s_uspace.s_bitmap);
2479 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2480 accum += udf_count_free_table(sb,
2481 map->s_uspace.s_table);
2486 MODULE_AUTHOR("Ben Fennema");
2487 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2488 MODULE_LICENSE("GPL");
2489 module_init(init_udf_fs)
2490 module_exit(exit_udf_fs)