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/smp_lock.h>
52 #include <linux/buffer_head.h>
53 #include <linux/vfs.h>
54 #include <linux/vmalloc.h>
55 #include <linux/errno.h>
56 #include <linux/mount.h>
57 #include <linux/seq_file.h>
58 #include <linux/bitmap.h>
59 #include <linux/crc-itu-t.h>
60 #include <asm/byteorder.h>
65 #include <linux/init.h>
66 #include <asm/uaccess.h>
68 #define VDS_POS_PRIMARY_VOL_DESC 0
69 #define VDS_POS_UNALLOC_SPACE_DESC 1
70 #define VDS_POS_LOGICAL_VOL_DESC 2
71 #define VDS_POS_PARTITION_DESC 3
72 #define VDS_POS_IMP_USE_VOL_DESC 4
73 #define VDS_POS_VOL_DESC_PTR 5
74 #define VDS_POS_TERMINATING_DESC 6
75 #define VDS_POS_LENGTH 7
77 #define UDF_DEFAULT_BLOCKSIZE 2048
79 static char error_buf[1024];
81 /* These are the "meat" - everything else is stuffing */
82 static int udf_fill_super(struct super_block *, void *, int);
83 static void udf_put_super(struct super_block *);
84 static void udf_write_super(struct super_block *);
85 static int udf_remount_fs(struct super_block *, int *, char *);
86 static int udf_check_valid(struct super_block *, int, int);
87 static int udf_vrs(struct super_block *sb, int silent);
88 static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
89 static void udf_find_anchor(struct super_block *);
90 static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
91 struct kernel_lb_addr *);
92 static void udf_load_fileset(struct super_block *, struct buffer_head *,
93 struct kernel_lb_addr *);
94 static void udf_open_lvid(struct super_block *);
95 static void udf_close_lvid(struct super_block *);
96 static unsigned int udf_count_free(struct super_block *);
97 static int udf_statfs(struct dentry *, struct kstatfs *);
98 static int udf_show_options(struct seq_file *, struct vfsmount *);
99 static void udf_error(struct super_block *sb, const char *function,
100 const char *fmt, ...);
102 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
104 struct logicalVolIntegrityDesc *lvid =
105 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
106 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
107 __u32 offset = number_of_partitions * 2 *
108 sizeof(uint32_t)/sizeof(uint8_t);
109 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
112 /* UDF filesystem type */
113 static int udf_get_sb(struct file_system_type *fs_type,
114 int flags, const char *dev_name, void *data,
115 struct vfsmount *mnt)
117 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
120 static struct file_system_type udf_fstype = {
121 .owner = THIS_MODULE,
123 .get_sb = udf_get_sb,
124 .kill_sb = kill_block_super,
125 .fs_flags = FS_REQUIRES_DEV,
128 static struct kmem_cache *udf_inode_cachep;
130 static struct inode *udf_alloc_inode(struct super_block *sb)
132 struct udf_inode_info *ei;
133 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
138 ei->i_lenExtents = 0;
139 ei->i_next_alloc_block = 0;
140 ei->i_next_alloc_goal = 0;
143 return &ei->vfs_inode;
146 static void udf_destroy_inode(struct inode *inode)
148 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
151 static void init_once(void *foo)
153 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
155 ei->i_ext.i_data = NULL;
156 inode_init_once(&ei->vfs_inode);
159 static int init_inodecache(void)
161 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
162 sizeof(struct udf_inode_info),
163 0, (SLAB_RECLAIM_ACCOUNT |
166 if (!udf_inode_cachep)
171 static void destroy_inodecache(void)
173 kmem_cache_destroy(udf_inode_cachep);
176 /* Superblock operations */
177 static const struct super_operations udf_sb_ops = {
178 .alloc_inode = udf_alloc_inode,
179 .destroy_inode = udf_destroy_inode,
180 .write_inode = udf_write_inode,
181 .delete_inode = udf_delete_inode,
182 .clear_inode = udf_clear_inode,
183 .put_super = udf_put_super,
184 .write_super = udf_write_super,
185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
187 .show_options = udf_show_options,
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
204 struct nls_table *nls_map;
207 static int __init init_udf_fs(void)
211 err = init_inodecache();
214 err = register_filesystem(&udf_fstype);
221 destroy_inodecache();
227 static void __exit exit_udf_fs(void)
229 unregister_filesystem(&udf_fstype);
230 destroy_inodecache();
233 module_init(init_udf_fs)
234 module_exit(exit_udf_fs)
236 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
238 struct udf_sb_info *sbi = UDF_SB(sb);
240 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
242 if (!sbi->s_partmaps) {
243 udf_error(sb, __func__,
244 "Unable to allocate space for %d partition maps",
246 sbi->s_partitions = 0;
250 sbi->s_partitions = count;
254 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
256 struct super_block *sb = mnt->mnt_sb;
257 struct udf_sb_info *sbi = UDF_SB(sb);
259 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
260 seq_puts(seq, ",nostrict");
261 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
262 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
263 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
264 seq_puts(seq, ",unhide");
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
266 seq_puts(seq, ",undelete");
267 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
268 seq_puts(seq, ",noadinicb");
269 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
270 seq_puts(seq, ",shortad");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
272 seq_puts(seq, ",uid=forget");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
274 seq_puts(seq, ",uid=ignore");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
276 seq_puts(seq, ",gid=forget");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
278 seq_puts(seq, ",gid=ignore");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
280 seq_printf(seq, ",uid=%u", sbi->s_uid);
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
282 seq_printf(seq, ",gid=%u", sbi->s_gid);
283 if (sbi->s_umask != 0)
284 seq_printf(seq, ",umask=%o", sbi->s_umask);
285 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
286 seq_printf(seq, ",session=%u", sbi->s_session);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
288 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
290 * s_anchor[2] could be zeroed out in case there is no anchor
291 * in the specified block, but then the "anchor=N" option
292 * originally given by the user wasn't effective, so it's OK
293 * if we don't show it.
295 if (sbi->s_anchor[2] != 0)
296 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
298 * volume, partition, fileset and rootdir seem to be ignored
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
302 seq_puts(seq, ",utf8");
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
304 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
313 * Parse mount options.
316 * The following mount options are supported:
318 * gid= Set the default group.
319 * umask= Set the default umask.
320 * uid= Set the default user.
321 * bs= Set the block size.
322 * unhide Show otherwise hidden files.
323 * undelete Show deleted files in lists.
324 * adinicb Embed data in the inode (default)
325 * noadinicb Don't embed data in the inode
326 * shortad Use short ad's
327 * longad Use long ad's (default)
328 * nostrict Unset strict conformance
329 * iocharset= Set the NLS character set
331 * The remaining are for debugging and disaster recovery:
333 * novrs Skip volume sequence recognition
335 * The following expect a offset from 0.
337 * session= Set the CDROM session (default= last session)
338 * anchor= Override standard anchor location. (default= 256)
339 * volume= Override the VolumeDesc location. (unused)
340 * partition= Override the PartitionDesc location. (unused)
341 * lastblock= Set the last block of the filesystem/
343 * The following expect a offset from the partition root.
345 * fileset= Override the fileset block location. (unused)
346 * rootdir= Override the root directory location. (unused)
347 * WARNING: overriding the rootdir to a non-directory may
348 * yield highly unpredictable results.
351 * options Pointer to mount options string.
352 * uopts Pointer to mount options variable.
355 * <return> 1 Mount options parsed okay.
356 * <return> 0 Error parsing mount options.
359 * July 1, 1997 - Andrew E. Mileski
360 * Written, tested, and released.
364 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
365 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
366 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
367 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
368 Opt_rootdir, Opt_utf8, Opt_iocharset,
369 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
372 static const match_table_t tokens = {
373 {Opt_novrs, "novrs"},
374 {Opt_nostrict, "nostrict"},
376 {Opt_unhide, "unhide"},
377 {Opt_undelete, "undelete"},
378 {Opt_noadinicb, "noadinicb"},
379 {Opt_adinicb, "adinicb"},
380 {Opt_shortad, "shortad"},
381 {Opt_longad, "longad"},
382 {Opt_uforget, "uid=forget"},
383 {Opt_uignore, "uid=ignore"},
384 {Opt_gforget, "gid=forget"},
385 {Opt_gignore, "gid=ignore"},
388 {Opt_umask, "umask=%o"},
389 {Opt_session, "session=%u"},
390 {Opt_lastblock, "lastblock=%u"},
391 {Opt_anchor, "anchor=%u"},
392 {Opt_volume, "volume=%u"},
393 {Opt_partition, "partition=%u"},
394 {Opt_fileset, "fileset=%u"},
395 {Opt_rootdir, "rootdir=%u"},
397 {Opt_iocharset, "iocharset=%s"},
401 static int udf_parse_options(char *options, struct udf_options *uopt,
408 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
409 uopt->partition = 0xFFFF;
410 uopt->session = 0xFFFFFFFF;
413 uopt->volume = 0xFFFFFFFF;
414 uopt->rootdir = 0xFFFFFFFF;
415 uopt->fileset = 0xFFFFFFFF;
416 uopt->nls_map = NULL;
421 while ((p = strsep(&options, ",")) != NULL) {
422 substring_t args[MAX_OPT_ARGS];
427 token = match_token(p, tokens, args);
432 if (match_int(&args[0], &option))
434 uopt->blocksize = option;
437 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
440 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
443 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
446 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
449 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
452 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
455 if (match_int(args, &option))
458 uopt->flags |= (1 << UDF_FLAG_GID_SET);
461 if (match_int(args, &option))
464 uopt->flags |= (1 << UDF_FLAG_UID_SET);
467 if (match_octal(args, &option))
469 uopt->umask = option;
472 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
475 if (match_int(args, &option))
477 uopt->session = option;
479 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
482 if (match_int(args, &option))
484 uopt->lastblock = option;
486 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
489 if (match_int(args, &option))
491 uopt->anchor = option;
494 if (match_int(args, &option))
496 uopt->volume = option;
499 if (match_int(args, &option))
501 uopt->partition = option;
504 if (match_int(args, &option))
506 uopt->fileset = option;
509 if (match_int(args, &option))
511 uopt->rootdir = option;
514 uopt->flags |= (1 << UDF_FLAG_UTF8);
516 #ifdef CONFIG_UDF_NLS
518 uopt->nls_map = load_nls(args[0].from);
519 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
523 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
526 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
529 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
532 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
535 printk(KERN_ERR "udf: bad mount option \"%s\" "
536 "or missing value\n", p);
543 static void udf_write_super(struct super_block *sb)
547 if (!(sb->s_flags & MS_RDONLY))
554 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
556 struct udf_options uopt;
557 struct udf_sb_info *sbi = UDF_SB(sb);
559 uopt.flags = sbi->s_flags;
560 uopt.uid = sbi->s_uid;
561 uopt.gid = sbi->s_gid;
562 uopt.umask = sbi->s_umask;
564 if (!udf_parse_options(options, &uopt, true))
567 sbi->s_flags = uopt.flags;
568 sbi->s_uid = uopt.uid;
569 sbi->s_gid = uopt.gid;
570 sbi->s_umask = uopt.umask;
572 if (sbi->s_lvid_bh) {
573 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
574 if (write_rev > UDF_MAX_WRITE_VERSION)
578 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
580 if (*flags & MS_RDONLY)
588 static int udf_vrs(struct super_block *sb, int silent)
590 struct volStructDesc *vsd = NULL;
591 loff_t sector = 32768;
593 struct buffer_head *bh = NULL;
597 struct udf_sb_info *sbi;
599 /* Block size must be a multiple of 512 */
600 if (sb->s_blocksize & 511)
604 if (sb->s_blocksize < sizeof(struct volStructDesc))
605 sectorsize = sizeof(struct volStructDesc);
607 sectorsize = sb->s_blocksize;
609 sector += (sbi->s_session << sb->s_blocksize_bits);
611 udf_debug("Starting at sector %u (%ld byte sectors)\n",
612 (unsigned int)(sector >> sb->s_blocksize_bits),
614 /* Process the sequence (if applicable) */
615 for (; !nsr02 && !nsr03; sector += sectorsize) {
617 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
621 /* Look for ISO descriptors */
622 vsd = (struct volStructDesc *)(bh->b_data +
623 (sector & (sb->s_blocksize - 1)));
625 if (vsd->stdIdent[0] == 0) {
628 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
631 switch (vsd->structType) {
633 udf_debug("ISO9660 Boot Record found\n");
636 udf_debug("ISO9660 Primary Volume Descriptor "
640 udf_debug("ISO9660 Supplementary Volume "
641 "Descriptor found\n");
644 udf_debug("ISO9660 Volume Partition Descriptor "
648 udf_debug("ISO9660 Volume Descriptor Set "
649 "Terminator found\n");
652 udf_debug("ISO9660 VRS (%u) found\n",
656 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
659 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
663 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
666 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
676 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
683 * Check whether there is an anchor block in the given block
685 static int udf_check_anchor_block(struct super_block *sb, sector_t block)
687 struct buffer_head *bh;
690 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
691 udf_fixed_to_variable(block) >=
692 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
695 bh = udf_read_tagged(sb, block, block, &ident);
700 return ident == TAG_IDENT_AVDP;
703 /* Search for an anchor volume descriptor pointer */
704 static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
708 struct udf_sb_info *sbi = UDF_SB(sb);
711 last[1] = last[0] - 1;
712 last[2] = last[0] + 1;
713 last[3] = last[0] - 2;
714 last[4] = last[0] - 150;
715 last[5] = last[0] - 152;
717 /* according to spec, anchor is in either:
721 * however, if the disc isn't closed, it could be 512 */
723 for (i = 0; i < ARRAY_SIZE(last); i++) {
726 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
727 sb->s_blocksize_bits)
730 if (udf_check_anchor_block(sb, last[i])) {
731 sbi->s_anchor[0] = last[i];
732 sbi->s_anchor[1] = last[i] - 256;
739 if (udf_check_anchor_block(sb, last[i] - 256)) {
740 sbi->s_anchor[1] = last[i] - 256;
745 if (udf_check_anchor_block(sb, sbi->s_session + 256)) {
746 sbi->s_anchor[0] = sbi->s_session + 256;
749 if (udf_check_anchor_block(sb, sbi->s_session + 512)) {
750 sbi->s_anchor[0] = sbi->s_session + 512;
757 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
758 * be the last block on the media.
760 * Return 1 if not found, 0 if ok
763 static void udf_find_anchor(struct super_block *sb)
766 struct buffer_head *bh = NULL;
769 struct udf_sb_info *sbi = UDF_SB(sb);
771 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
775 /* No anchor found? Try VARCONV conversion of block numbers */
776 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
777 /* Firstly, we try to not convert number of the last block */
778 lastblock = udf_scan_anchors(sb,
779 udf_variable_to_fixed(sbi->s_last_block));
783 /* Secondly, we try with converted number of the last block */
784 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
786 /* VARCONV didn't help. Clear it. */
787 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
792 * Check located anchors and the anchor block supplied via
795 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
796 if (!sbi->s_anchor[i])
798 bh = udf_read_tagged(sb, sbi->s_anchor[i],
799 sbi->s_anchor[i], &ident);
801 sbi->s_anchor[i] = 0;
804 if (ident != TAG_IDENT_AVDP)
805 sbi->s_anchor[i] = 0;
809 sbi->s_last_block = lastblock;
812 static int udf_find_fileset(struct super_block *sb,
813 struct kernel_lb_addr *fileset,
814 struct kernel_lb_addr *root)
816 struct buffer_head *bh = NULL;
819 struct udf_sb_info *sbi;
821 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
822 fileset->partitionReferenceNum != 0xFFFF) {
823 bh = udf_read_ptagged(sb, fileset, 0, &ident);
827 } else if (ident != TAG_IDENT_FSD) {
836 /* Search backwards through the partitions */
837 struct kernel_lb_addr newfileset;
839 /* --> cvg: FIXME - is it reasonable? */
842 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
843 (newfileset.partitionReferenceNum != 0xFFFF &&
844 fileset->logicalBlockNum == 0xFFFFFFFF &&
845 fileset->partitionReferenceNum == 0xFFFF);
846 newfileset.partitionReferenceNum--) {
847 lastblock = sbi->s_partmaps
848 [newfileset.partitionReferenceNum]
850 newfileset.logicalBlockNum = 0;
853 bh = udf_read_ptagged(sb, &newfileset, 0,
856 newfileset.logicalBlockNum++;
863 struct spaceBitmapDesc *sp;
864 sp = (struct spaceBitmapDesc *)
866 newfileset.logicalBlockNum += 1 +
867 ((le32_to_cpu(sp->numOfBytes) +
868 sizeof(struct spaceBitmapDesc)
869 - 1) >> sb->s_blocksize_bits);
874 *fileset = newfileset;
877 newfileset.logicalBlockNum++;
882 } while (newfileset.logicalBlockNum < lastblock &&
883 fileset->logicalBlockNum == 0xFFFFFFFF &&
884 fileset->partitionReferenceNum == 0xFFFF);
888 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
889 fileset->partitionReferenceNum != 0xFFFF) && bh) {
890 udf_debug("Fileset at block=%d, partition=%d\n",
891 fileset->logicalBlockNum,
892 fileset->partitionReferenceNum);
894 sbi->s_partition = fileset->partitionReferenceNum;
895 udf_load_fileset(sb, bh, root);
902 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
904 struct primaryVolDesc *pvoldesc;
905 struct ustr *instr, *outstr;
906 struct buffer_head *bh;
910 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
914 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
918 bh = udf_read_tagged(sb, block, block, &ident);
922 BUG_ON(ident != TAG_IDENT_PVD);
924 pvoldesc = (struct primaryVolDesc *)bh->b_data;
926 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
927 pvoldesc->recordingDateAndTime)) {
929 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
930 udf_debug("recording time %04u/%02u/%02u"
932 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
933 ts->minute, le16_to_cpu(ts->typeAndTimezone));
937 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
938 if (udf_CS0toUTF8(outstr, instr)) {
939 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
940 outstr->u_len > 31 ? 31 : outstr->u_len);
941 udf_debug("volIdent[] = '%s'\n",
942 UDF_SB(sb)->s_volume_ident);
945 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
946 if (udf_CS0toUTF8(outstr, instr))
947 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
958 static int udf_load_metadata_files(struct super_block *sb, int partition)
960 struct udf_sb_info *sbi = UDF_SB(sb);
961 struct udf_part_map *map;
962 struct udf_meta_data *mdata;
963 struct kernel_lb_addr addr;
966 map = &sbi->s_partmaps[partition];
967 mdata = &map->s_type_specific.s_metadata;
969 /* metadata address */
970 addr.logicalBlockNum = mdata->s_meta_file_loc;
971 addr.partitionReferenceNum = map->s_partition_num;
973 udf_debug("Metadata file location: block = %d part = %d\n",
974 addr.logicalBlockNum, addr.partitionReferenceNum);
976 mdata->s_metadata_fe = udf_iget(sb, &addr);
978 if (mdata->s_metadata_fe == NULL) {
979 udf_warning(sb, __func__, "metadata inode efe not found, "
980 "will try mirror inode.");
982 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
983 ICBTAG_FLAG_AD_SHORT) {
984 udf_warning(sb, __func__, "metadata inode efe does not have "
985 "short allocation descriptors!");
987 iput(mdata->s_metadata_fe);
988 mdata->s_metadata_fe = NULL;
991 /* mirror file entry */
992 addr.logicalBlockNum = mdata->s_mirror_file_loc;
993 addr.partitionReferenceNum = map->s_partition_num;
995 udf_debug("Mirror metadata file location: block = %d part = %d\n",
996 addr.logicalBlockNum, addr.partitionReferenceNum);
998 mdata->s_mirror_fe = udf_iget(sb, &addr);
1000 if (mdata->s_mirror_fe == NULL) {
1002 udf_error(sb, __func__, "mirror inode efe not found "
1003 "and metadata inode is missing too, exiting...");
1006 udf_warning(sb, __func__, "mirror inode efe not found,"
1007 " but metadata inode is OK");
1008 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1009 ICBTAG_FLAG_AD_SHORT) {
1010 udf_warning(sb, __func__, "mirror inode efe does not have "
1011 "short allocation descriptors!");
1012 iput(mdata->s_mirror_fe);
1013 mdata->s_mirror_fe = NULL;
1021 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1023 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1024 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1025 addr.partitionReferenceNum = map->s_partition_num;
1027 udf_debug("Bitmap file location: block = %d part = %d\n",
1028 addr.logicalBlockNum, addr.partitionReferenceNum);
1030 mdata->s_bitmap_fe = udf_iget(sb, &addr);
1032 if (mdata->s_bitmap_fe == NULL) {
1033 if (sb->s_flags & MS_RDONLY)
1034 udf_warning(sb, __func__, "bitmap inode efe "
1035 "not found but it's ok since the disc"
1036 " is mounted read-only");
1038 udf_error(sb, __func__, "bitmap inode efe not "
1039 "found and attempted read-write mount");
1045 udf_debug("udf_load_metadata_files Ok\n");
1053 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1054 struct kernel_lb_addr *root)
1056 struct fileSetDesc *fset;
1058 fset = (struct fileSetDesc *)bh->b_data;
1060 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1062 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1064 udf_debug("Rootdir at block=%d, partition=%d\n",
1065 root->logicalBlockNum, root->partitionReferenceNum);
1068 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1070 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1071 return DIV_ROUND_UP(map->s_partition_len +
1072 (sizeof(struct spaceBitmapDesc) << 3),
1073 sb->s_blocksize * 8);
1076 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1078 struct udf_bitmap *bitmap;
1082 nr_groups = udf_compute_nr_groups(sb, index);
1083 size = sizeof(struct udf_bitmap) +
1084 (sizeof(struct buffer_head *) * nr_groups);
1086 if (size <= PAGE_SIZE)
1087 bitmap = kmalloc(size, GFP_KERNEL);
1089 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1091 if (bitmap == NULL) {
1092 udf_error(sb, __func__,
1093 "Unable to allocate space for bitmap "
1094 "and %d buffer_head pointers", nr_groups);
1098 memset(bitmap, 0x00, size);
1099 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1100 bitmap->s_nr_groups = nr_groups;
1104 static int udf_fill_partdesc_info(struct super_block *sb,
1105 struct partitionDesc *p, int p_index)
1107 struct udf_part_map *map;
1108 struct udf_sb_info *sbi = UDF_SB(sb);
1109 struct partitionHeaderDesc *phd;
1111 map = &sbi->s_partmaps[p_index];
1113 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1114 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1116 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1117 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1118 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1119 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1120 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1121 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1122 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1123 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1125 udf_debug("Partition (%d type %x) starts at physical %d, "
1126 "block length %d\n", p_index,
1127 map->s_partition_type, map->s_partition_root,
1128 map->s_partition_len);
1130 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1131 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1134 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1135 if (phd->unallocSpaceTable.extLength) {
1136 struct kernel_lb_addr loc = {
1137 .logicalBlockNum = le32_to_cpu(
1138 phd->unallocSpaceTable.extPosition),
1139 .partitionReferenceNum = p_index,
1142 map->s_uspace.s_table = udf_iget(sb, &loc);
1143 if (!map->s_uspace.s_table) {
1144 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1148 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1149 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1150 p_index, map->s_uspace.s_table->i_ino);
1153 if (phd->unallocSpaceBitmap.extLength) {
1154 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1157 map->s_uspace.s_bitmap = bitmap;
1158 bitmap->s_extLength = le32_to_cpu(
1159 phd->unallocSpaceBitmap.extLength);
1160 bitmap->s_extPosition = le32_to_cpu(
1161 phd->unallocSpaceBitmap.extPosition);
1162 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1163 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1164 bitmap->s_extPosition);
1167 if (phd->partitionIntegrityTable.extLength)
1168 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1170 if (phd->freedSpaceTable.extLength) {
1171 struct kernel_lb_addr loc = {
1172 .logicalBlockNum = le32_to_cpu(
1173 phd->freedSpaceTable.extPosition),
1174 .partitionReferenceNum = p_index,
1177 map->s_fspace.s_table = udf_iget(sb, &loc);
1178 if (!map->s_fspace.s_table) {
1179 udf_debug("cannot load freedSpaceTable (part %d)\n",
1184 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1185 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1186 p_index, map->s_fspace.s_table->i_ino);
1189 if (phd->freedSpaceBitmap.extLength) {
1190 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1193 map->s_fspace.s_bitmap = bitmap;
1194 bitmap->s_extLength = le32_to_cpu(
1195 phd->freedSpaceBitmap.extLength);
1196 bitmap->s_extPosition = le32_to_cpu(
1197 phd->freedSpaceBitmap.extPosition);
1198 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1199 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1200 bitmap->s_extPosition);
1205 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1207 struct udf_sb_info *sbi = UDF_SB(sb);
1208 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1209 struct kernel_lb_addr ino;
1210 struct buffer_head *bh = NULL;
1211 struct udf_inode_info *vati;
1213 struct virtualAllocationTable20 *vat20;
1215 /* VAT file entry is in the last recorded block */
1216 ino.partitionReferenceNum = type1_index;
1217 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1218 sbi->s_vat_inode = udf_iget(sb, &ino);
1219 if (!sbi->s_vat_inode)
1222 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1223 map->s_type_specific.s_virtual.s_start_offset = 0;
1224 map->s_type_specific.s_virtual.s_num_entries =
1225 (sbi->s_vat_inode->i_size - 36) >> 2;
1226 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1227 vati = UDF_I(sbi->s_vat_inode);
1228 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1229 pos = udf_block_map(sbi->s_vat_inode, 0);
1230 bh = sb_bread(sb, pos);
1233 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1235 vat20 = (struct virtualAllocationTable20 *)
1239 map->s_type_specific.s_virtual.s_start_offset =
1240 le16_to_cpu(vat20->lengthHeader);
1241 map->s_type_specific.s_virtual.s_num_entries =
1242 (sbi->s_vat_inode->i_size -
1243 map->s_type_specific.s_virtual.
1244 s_start_offset) >> 2;
1250 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1252 struct buffer_head *bh;
1253 struct partitionDesc *p;
1254 struct udf_part_map *map;
1255 struct udf_sb_info *sbi = UDF_SB(sb);
1257 uint16_t partitionNumber;
1261 bh = udf_read_tagged(sb, block, block, &ident);
1264 if (ident != TAG_IDENT_PD)
1267 p = (struct partitionDesc *)bh->b_data;
1268 partitionNumber = le16_to_cpu(p->partitionNumber);
1270 /* First scan for TYPE1, SPARABLE and METADATA partitions */
1271 for (i = 0; i < sbi->s_partitions; i++) {
1272 map = &sbi->s_partmaps[i];
1273 udf_debug("Searching map: (%d == %d)\n",
1274 map->s_partition_num, partitionNumber);
1275 if (map->s_partition_num == partitionNumber &&
1276 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1277 map->s_partition_type == UDF_SPARABLE_MAP15))
1281 if (i >= sbi->s_partitions) {
1282 udf_debug("Partition (%d) not found in partition map\n",
1287 ret = udf_fill_partdesc_info(sb, p, i);
1290 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1291 * PHYSICAL partitions are already set up
1294 for (i = 0; i < sbi->s_partitions; i++) {
1295 map = &sbi->s_partmaps[i];
1297 if (map->s_partition_num == partitionNumber &&
1298 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1299 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1300 map->s_partition_type == UDF_METADATA_MAP25))
1304 if (i >= sbi->s_partitions)
1307 ret = udf_fill_partdesc_info(sb, p, i);
1311 if (map->s_partition_type == UDF_METADATA_MAP25) {
1312 ret = udf_load_metadata_files(sb, i);
1314 printk(KERN_ERR "UDF-fs: error loading MetaData "
1315 "partition map %d\n", i);
1319 ret = udf_load_vat(sb, i, type1_idx);
1323 * Mark filesystem read-only if we have a partition with
1324 * virtual map since we don't handle writing to it (we
1325 * overwrite blocks instead of relocating them).
1327 sb->s_flags |= MS_RDONLY;
1328 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1329 "because writing to pseudooverwrite partition is "
1330 "not implemented.\n");
1333 /* In case loading failed, we handle cleanup in udf_fill_super */
1338 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1339 struct kernel_lb_addr *fileset)
1341 struct logicalVolDesc *lvd;
1344 struct udf_sb_info *sbi = UDF_SB(sb);
1345 struct genericPartitionMap *gpm;
1347 struct buffer_head *bh;
1350 bh = udf_read_tagged(sb, block, block, &ident);
1353 BUG_ON(ident != TAG_IDENT_LVD);
1354 lvd = (struct logicalVolDesc *)bh->b_data;
1356 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1362 for (i = 0, offset = 0;
1363 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1364 i++, offset += gpm->partitionMapLength) {
1365 struct udf_part_map *map = &sbi->s_partmaps[i];
1366 gpm = (struct genericPartitionMap *)
1367 &(lvd->partitionMaps[offset]);
1368 type = gpm->partitionMapType;
1370 struct genericPartitionMap1 *gpm1 =
1371 (struct genericPartitionMap1 *)gpm;
1372 map->s_partition_type = UDF_TYPE1_MAP15;
1373 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1374 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1375 map->s_partition_func = NULL;
1376 } else if (type == 2) {
1377 struct udfPartitionMap2 *upm2 =
1378 (struct udfPartitionMap2 *)gpm;
1379 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1380 strlen(UDF_ID_VIRTUAL))) {
1382 le16_to_cpu(((__le16 *)upm2->partIdent.
1385 map->s_partition_type =
1387 map->s_partition_func =
1388 udf_get_pblock_virt15;
1390 map->s_partition_type =
1392 map->s_partition_func =
1393 udf_get_pblock_virt20;
1395 } else if (!strncmp(upm2->partIdent.ident,
1397 strlen(UDF_ID_SPARABLE))) {
1399 struct sparingTable *st;
1400 struct sparablePartitionMap *spm =
1401 (struct sparablePartitionMap *)gpm;
1403 map->s_partition_type = UDF_SPARABLE_MAP15;
1404 map->s_type_specific.s_sparing.s_packet_len =
1405 le16_to_cpu(spm->packetLength);
1406 for (j = 0; j < spm->numSparingTables; j++) {
1407 struct buffer_head *bh2;
1410 spm->locSparingTable[j]);
1411 bh2 = udf_read_tagged(sb, loc, loc,
1413 map->s_type_specific.s_sparing.
1414 s_spar_map[j] = bh2;
1419 st = (struct sparingTable *)bh2->b_data;
1420 if (ident != 0 || strncmp(
1421 st->sparingIdent.ident,
1423 strlen(UDF_ID_SPARING))) {
1425 map->s_type_specific.s_sparing.
1426 s_spar_map[j] = NULL;
1429 map->s_partition_func = udf_get_pblock_spar15;
1430 } else if (!strncmp(upm2->partIdent.ident,
1432 strlen(UDF_ID_METADATA))) {
1433 struct udf_meta_data *mdata =
1434 &map->s_type_specific.s_metadata;
1435 struct metadataPartitionMap *mdm =
1436 (struct metadataPartitionMap *)
1437 &(lvd->partitionMaps[offset]);
1438 udf_debug("Parsing Logical vol part %d "
1439 "type %d id=%s\n", i, type,
1442 map->s_partition_type = UDF_METADATA_MAP25;
1443 map->s_partition_func = udf_get_pblock_meta25;
1445 mdata->s_meta_file_loc =
1446 le32_to_cpu(mdm->metadataFileLoc);
1447 mdata->s_mirror_file_loc =
1448 le32_to_cpu(mdm->metadataMirrorFileLoc);
1449 mdata->s_bitmap_file_loc =
1450 le32_to_cpu(mdm->metadataBitmapFileLoc);
1451 mdata->s_alloc_unit_size =
1452 le32_to_cpu(mdm->allocUnitSize);
1453 mdata->s_align_unit_size =
1454 le16_to_cpu(mdm->alignUnitSize);
1455 mdata->s_dup_md_flag =
1458 udf_debug("Metadata Ident suffix=0x%x\n",
1461 mdm->partIdent.identSuffix)[0])));
1462 udf_debug("Metadata part num=%d\n",
1463 le16_to_cpu(mdm->partitionNum));
1464 udf_debug("Metadata part alloc unit size=%d\n",
1465 le32_to_cpu(mdm->allocUnitSize));
1466 udf_debug("Metadata file loc=%d\n",
1467 le32_to_cpu(mdm->metadataFileLoc));
1468 udf_debug("Mirror file loc=%d\n",
1469 le32_to_cpu(mdm->metadataMirrorFileLoc));
1470 udf_debug("Bitmap file loc=%d\n",
1471 le32_to_cpu(mdm->metadataBitmapFileLoc));
1472 udf_debug("Duplicate Flag: %d %d\n",
1473 mdata->s_dup_md_flag, mdm->flags);
1475 udf_debug("Unknown ident: %s\n",
1476 upm2->partIdent.ident);
1479 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1480 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1482 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1483 i, map->s_partition_num, type,
1484 map->s_volumeseqnum);
1488 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1490 *fileset = lelb_to_cpu(la->extLocation);
1491 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1492 "partition=%d\n", fileset->logicalBlockNum,
1493 fileset->partitionReferenceNum);
1495 if (lvd->integritySeqExt.extLength)
1496 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1504 * udf_load_logicalvolint
1507 static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1509 struct buffer_head *bh = NULL;
1511 struct udf_sb_info *sbi = UDF_SB(sb);
1512 struct logicalVolIntegrityDesc *lvid;
1514 while (loc.extLength > 0 &&
1515 (bh = udf_read_tagged(sb, loc.extLocation,
1516 loc.extLocation, &ident)) &&
1517 ident == TAG_IDENT_LVID) {
1518 sbi->s_lvid_bh = bh;
1519 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1521 if (lvid->nextIntegrityExt.extLength)
1522 udf_load_logicalvolint(sb,
1523 leea_to_cpu(lvid->nextIntegrityExt));
1525 if (sbi->s_lvid_bh != bh)
1527 loc.extLength -= sb->s_blocksize;
1530 if (sbi->s_lvid_bh != bh)
1535 * udf_process_sequence
1538 * Process a main/reserve volume descriptor sequence.
1541 * sb Pointer to _locked_ superblock.
1542 * block First block of first extent of the sequence.
1543 * lastblock Lastblock of first extent of the sequence.
1546 * July 1, 1997 - Andrew E. Mileski
1547 * Written, tested, and released.
1549 static noinline int udf_process_sequence(struct super_block *sb, long block,
1550 long lastblock, struct kernel_lb_addr *fileset)
1552 struct buffer_head *bh = NULL;
1553 struct udf_vds_record vds[VDS_POS_LENGTH];
1554 struct udf_vds_record *curr;
1555 struct generic_desc *gd;
1556 struct volDescPtr *vdp;
1560 long next_s = 0, next_e = 0;
1562 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1565 * Read the main descriptor sequence and find which descriptors
1568 for (; (!done && block <= lastblock); block++) {
1570 bh = udf_read_tagged(sb, block, block, &ident);
1572 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1573 "sequence is corrupted or we could not read "
1574 "it.\n", (unsigned long long)block);
1578 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1579 gd = (struct generic_desc *)bh->b_data;
1580 vdsn = le32_to_cpu(gd->volDescSeqNum);
1582 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1583 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1584 if (vdsn >= curr->volDescSeqNum) {
1585 curr->volDescSeqNum = vdsn;
1586 curr->block = block;
1589 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1590 curr = &vds[VDS_POS_VOL_DESC_PTR];
1591 if (vdsn >= curr->volDescSeqNum) {
1592 curr->volDescSeqNum = vdsn;
1593 curr->block = block;
1595 vdp = (struct volDescPtr *)bh->b_data;
1596 next_s = le32_to_cpu(
1597 vdp->nextVolDescSeqExt.extLocation);
1598 next_e = le32_to_cpu(
1599 vdp->nextVolDescSeqExt.extLength);
1600 next_e = next_e >> sb->s_blocksize_bits;
1604 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1605 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1606 if (vdsn >= curr->volDescSeqNum) {
1607 curr->volDescSeqNum = vdsn;
1608 curr->block = block;
1611 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1612 curr = &vds[VDS_POS_PARTITION_DESC];
1614 curr->block = block;
1616 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1617 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1618 if (vdsn >= curr->volDescSeqNum) {
1619 curr->volDescSeqNum = vdsn;
1620 curr->block = block;
1623 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1624 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1625 if (vdsn >= curr->volDescSeqNum) {
1626 curr->volDescSeqNum = vdsn;
1627 curr->block = block;
1630 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1631 vds[VDS_POS_TERMINATING_DESC].block = block;
1635 next_s = next_e = 0;
1643 * Now read interesting descriptors again and process them
1644 * in a suitable order
1646 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1647 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1650 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1653 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1654 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1657 if (vds[VDS_POS_PARTITION_DESC].block) {
1659 * We rescan the whole descriptor sequence to find
1660 * partition descriptor blocks and process them.
1662 for (block = vds[VDS_POS_PARTITION_DESC].block;
1663 block < vds[VDS_POS_TERMINATING_DESC].block;
1665 if (udf_load_partdesc(sb, block))
1675 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1678 struct udf_sb_info *sbi = UDF_SB(sb);
1681 udf_debug("Validity check skipped because of novrs option\n");
1684 /* Check that it is NSR02 compliant */
1685 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1686 block = udf_vrs(sb, silent);
1688 udf_debug("Failed to read byte 32768. Assuming open "
1689 "disc. Skipping validity check\n");
1690 if (block && !sbi->s_last_block)
1691 sbi->s_last_block = udf_get_last_block(sb);
1695 static int udf_load_sequence(struct super_block *sb, struct kernel_lb_addr *fileset)
1697 struct anchorVolDescPtr *anchor;
1699 struct buffer_head *bh;
1700 long main_s, main_e, reserve_s, reserve_e;
1702 struct udf_sb_info *sbi;
1708 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1709 if (!sbi->s_anchor[i])
1712 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1717 anchor = (struct anchorVolDescPtr *)bh->b_data;
1719 /* Locate the main sequence */
1720 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1721 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1722 main_e = main_e >> sb->s_blocksize_bits;
1725 /* Locate the reserve sequence */
1726 reserve_s = le32_to_cpu(
1727 anchor->reserveVolDescSeqExt.extLocation);
1728 reserve_e = le32_to_cpu(
1729 anchor->reserveVolDescSeqExt.extLength);
1730 reserve_e = reserve_e >> sb->s_blocksize_bits;
1731 reserve_e += reserve_s;
1735 /* Process the main & reserve sequences */
1736 /* responsible for finding the PartitionDesc(s) */
1737 if (!(udf_process_sequence(sb, main_s, main_e,
1739 udf_process_sequence(sb, reserve_s, reserve_e,
1744 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1745 udf_debug("No Anchor block found\n");
1748 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1753 static void udf_open_lvid(struct super_block *sb)
1755 struct udf_sb_info *sbi = UDF_SB(sb);
1756 struct buffer_head *bh = sbi->s_lvid_bh;
1757 struct logicalVolIntegrityDesc *lvid;
1758 struct logicalVolIntegrityDescImpUse *lvidiu;
1762 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1763 lvidiu = udf_sb_lvidiu(sbi);
1765 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1766 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1767 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1769 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1771 lvid->descTag.descCRC = cpu_to_le16(
1772 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
1773 le16_to_cpu(lvid->descTag.descCRCLength)));
1775 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1776 mark_buffer_dirty(bh);
1779 static void udf_close_lvid(struct super_block *sb)
1781 struct udf_sb_info *sbi = UDF_SB(sb);
1782 struct buffer_head *bh = sbi->s_lvid_bh;
1783 struct logicalVolIntegrityDesc *lvid;
1784 struct logicalVolIntegrityDescImpUse *lvidiu;
1789 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1791 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1794 lvidiu = udf_sb_lvidiu(sbi);
1795 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1796 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1797 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1798 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1799 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1800 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1801 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1802 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1803 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1804 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1806 lvid->descTag.descCRC = cpu_to_le16(
1807 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
1808 le16_to_cpu(lvid->descTag.descCRCLength)));
1810 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1811 mark_buffer_dirty(bh);
1814 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1817 int nr_groups = bitmap->s_nr_groups;
1818 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1821 for (i = 0; i < nr_groups; i++)
1822 if (bitmap->s_block_bitmap[i])
1823 brelse(bitmap->s_block_bitmap[i]);
1825 if (size <= PAGE_SIZE)
1831 static void udf_free_partition(struct udf_part_map *map)
1834 struct udf_meta_data *mdata;
1836 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1837 iput(map->s_uspace.s_table);
1838 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1839 iput(map->s_fspace.s_table);
1840 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1841 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1842 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1843 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1844 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1845 for (i = 0; i < 4; i++)
1846 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1847 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1848 mdata = &map->s_type_specific.s_metadata;
1849 iput(mdata->s_metadata_fe);
1850 mdata->s_metadata_fe = NULL;
1852 iput(mdata->s_mirror_fe);
1853 mdata->s_mirror_fe = NULL;
1855 iput(mdata->s_bitmap_fe);
1856 mdata->s_bitmap_fe = NULL;
1860 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1863 struct inode *inode = NULL;
1864 struct udf_options uopt;
1865 struct kernel_lb_addr rootdir, fileset;
1866 struct udf_sb_info *sbi;
1868 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1873 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1877 sb->s_fs_info = sbi;
1879 mutex_init(&sbi->s_alloc_mutex);
1881 if (!udf_parse_options((char *)options, &uopt, false))
1884 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1885 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1886 udf_error(sb, "udf_read_super",
1887 "utf8 cannot be combined with iocharset\n");
1890 #ifdef CONFIG_UDF_NLS
1891 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1892 uopt.nls_map = load_nls_default();
1894 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1896 udf_debug("Using default NLS map\n");
1899 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1900 uopt.flags |= (1 << UDF_FLAG_UTF8);
1902 fileset.logicalBlockNum = 0xFFFFFFFF;
1903 fileset.partitionReferenceNum = 0xFFFF;
1905 sbi->s_flags = uopt.flags;
1906 sbi->s_uid = uopt.uid;
1907 sbi->s_gid = uopt.gid;
1908 sbi->s_umask = uopt.umask;
1909 sbi->s_nls_map = uopt.nls_map;
1911 /* Set the block size for all transfers */
1912 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1913 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1914 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1918 if (uopt.session == 0xFFFFFFFF)
1919 sbi->s_session = udf_get_last_session(sb);
1921 sbi->s_session = uopt.session;
1923 udf_debug("Multi-session=%d\n", sbi->s_session);
1925 sbi->s_last_block = uopt.lastblock;
1926 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1927 sbi->s_anchor[2] = uopt.anchor;
1929 if (udf_check_valid(sb, uopt.novrs, silent)) {
1930 /* read volume recognition sequences */
1931 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1935 udf_find_anchor(sb);
1937 /* Fill in the rest of the superblock */
1938 sb->s_op = &udf_sb_ops;
1939 sb->s_export_op = &udf_export_ops;
1942 sb->s_magic = UDF_SUPER_MAGIC;
1943 sb->s_time_gran = 1000;
1945 if (udf_load_sequence(sb, &fileset)) {
1946 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1950 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1952 if (sbi->s_lvid_bh) {
1953 struct logicalVolIntegrityDescImpUse *lvidiu =
1955 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1956 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1957 /* uint16_t maxUDFWriteRev =
1958 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1960 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1961 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1963 le16_to_cpu(lvidiu->minUDFReadRev),
1964 UDF_MAX_READ_VERSION);
1966 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1967 sb->s_flags |= MS_RDONLY;
1969 sbi->s_udfrev = minUDFWriteRev;
1971 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1972 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1973 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1974 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1977 if (!sbi->s_partitions) {
1978 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1982 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1983 UDF_PART_FLAG_READ_ONLY) {
1984 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1985 "forcing readonly mount\n");
1986 sb->s_flags |= MS_RDONLY;
1989 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1990 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1995 struct timestamp ts;
1996 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
1997 udf_info("UDF: Mounting volume '%s', "
1998 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1999 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2000 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
2002 if (!(sb->s_flags & MS_RDONLY))
2005 /* Assign the root inode */
2006 /* assign inodes by physical block number */
2007 /* perhaps it's not extensible enough, but for now ... */
2008 inode = udf_iget(sb, &rootdir);
2010 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2012 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2016 /* Allocate a dentry for the root inode */
2017 sb->s_root = d_alloc_root(inode);
2019 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
2023 sb->s_maxbytes = MAX_LFS_FILESIZE;
2027 if (sbi->s_vat_inode)
2028 iput(sbi->s_vat_inode);
2029 if (sbi->s_partitions)
2030 for (i = 0; i < sbi->s_partitions; i++)
2031 udf_free_partition(&sbi->s_partmaps[i]);
2032 #ifdef CONFIG_UDF_NLS
2033 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2034 unload_nls(sbi->s_nls_map);
2036 if (!(sb->s_flags & MS_RDONLY))
2038 brelse(sbi->s_lvid_bh);
2040 kfree(sbi->s_partmaps);
2042 sb->s_fs_info = NULL;
2047 static void udf_error(struct super_block *sb, const char *function,
2048 const char *fmt, ...)
2052 if (!(sb->s_flags & MS_RDONLY)) {
2056 va_start(args, fmt);
2057 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2059 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
2060 sb->s_id, function, error_buf);
2063 void udf_warning(struct super_block *sb, const char *function,
2064 const char *fmt, ...)
2068 va_start(args, fmt);
2069 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2071 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
2072 sb->s_id, function, error_buf);
2075 static void udf_put_super(struct super_block *sb)
2078 struct udf_sb_info *sbi;
2081 if (sbi->s_vat_inode)
2082 iput(sbi->s_vat_inode);
2083 if (sbi->s_partitions)
2084 for (i = 0; i < sbi->s_partitions; i++)
2085 udf_free_partition(&sbi->s_partmaps[i]);
2086 #ifdef CONFIG_UDF_NLS
2087 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2088 unload_nls(sbi->s_nls_map);
2090 if (!(sb->s_flags & MS_RDONLY))
2092 brelse(sbi->s_lvid_bh);
2093 kfree(sbi->s_partmaps);
2094 kfree(sb->s_fs_info);
2095 sb->s_fs_info = NULL;
2098 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2100 struct super_block *sb = dentry->d_sb;
2101 struct udf_sb_info *sbi = UDF_SB(sb);
2102 struct logicalVolIntegrityDescImpUse *lvidiu;
2104 if (sbi->s_lvid_bh != NULL)
2105 lvidiu = udf_sb_lvidiu(sbi);
2109 buf->f_type = UDF_SUPER_MAGIC;
2110 buf->f_bsize = sb->s_blocksize;
2111 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2112 buf->f_bfree = udf_count_free(sb);
2113 buf->f_bavail = buf->f_bfree;
2114 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2115 le32_to_cpu(lvidiu->numDirs)) : 0)
2117 buf->f_ffree = buf->f_bfree;
2118 /* __kernel_fsid_t f_fsid */
2119 buf->f_namelen = UDF_NAME_LEN - 2;
2124 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2125 struct udf_bitmap *bitmap)
2127 struct buffer_head *bh = NULL;
2128 unsigned int accum = 0;
2130 int block = 0, newblock;
2131 struct kernel_lb_addr loc;
2135 struct spaceBitmapDesc *bm;
2139 loc.logicalBlockNum = bitmap->s_extPosition;
2140 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2141 bh = udf_read_ptagged(sb, &loc, 0, &ident);
2144 printk(KERN_ERR "udf: udf_count_free failed\n");
2146 } else if (ident != TAG_IDENT_SBD) {
2148 printk(KERN_ERR "udf: udf_count_free failed\n");
2152 bm = (struct spaceBitmapDesc *)bh->b_data;
2153 bytes = le32_to_cpu(bm->numOfBytes);
2154 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2155 ptr = (uint8_t *)bh->b_data;
2158 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2159 accum += bitmap_weight((const unsigned long *)(ptr + index),
2164 newblock = udf_get_lb_pblock(sb, &loc, ++block);
2165 bh = udf_tread(sb, newblock);
2167 udf_debug("read failed\n");
2171 ptr = (uint8_t *)bh->b_data;
2182 static unsigned int udf_count_free_table(struct super_block *sb,
2183 struct inode *table)
2185 unsigned int accum = 0;
2187 struct kernel_lb_addr eloc;
2189 struct extent_position epos;
2193 epos.block = UDF_I(table)->i_location;
2194 epos.offset = sizeof(struct unallocSpaceEntry);
2197 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2198 accum += (elen >> table->i_sb->s_blocksize_bits);
2207 static unsigned int udf_count_free(struct super_block *sb)
2209 unsigned int accum = 0;
2210 struct udf_sb_info *sbi;
2211 struct udf_part_map *map;
2214 if (sbi->s_lvid_bh) {
2215 struct logicalVolIntegrityDesc *lvid =
2216 (struct logicalVolIntegrityDesc *)
2217 sbi->s_lvid_bh->b_data;
2218 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2219 accum = le32_to_cpu(
2220 lvid->freeSpaceTable[sbi->s_partition]);
2221 if (accum == 0xFFFFFFFF)
2229 map = &sbi->s_partmaps[sbi->s_partition];
2230 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2231 accum += udf_count_free_bitmap(sb,
2232 map->s_uspace.s_bitmap);
2234 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2235 accum += udf_count_free_bitmap(sb,
2236 map->s_fspace.s_bitmap);
2241 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2242 accum += udf_count_free_table(sb,
2243 map->s_uspace.s_table);
2245 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2246 accum += udf_count_free_table(sb,
2247 map->s_fspace.s_table);