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 <asm/byteorder.h>
64 #include <linux/init.h>
65 #include <asm/uaccess.h>
67 #define VDS_POS_PRIMARY_VOL_DESC 0
68 #define VDS_POS_UNALLOC_SPACE_DESC 1
69 #define VDS_POS_LOGICAL_VOL_DESC 2
70 #define VDS_POS_PARTITION_DESC 3
71 #define VDS_POS_IMP_USE_VOL_DESC 4
72 #define VDS_POS_VOL_DESC_PTR 5
73 #define VDS_POS_TERMINATING_DESC 6
74 #define VDS_POS_LENGTH 7
76 #define UDF_DEFAULT_BLOCKSIZE 2048
78 static char error_buf[1024];
80 /* These are the "meat" - everything else is stuffing */
81 static int udf_fill_super(struct super_block *, void *, int);
82 static void udf_put_super(struct super_block *);
83 static void udf_write_super(struct super_block *);
84 static int udf_remount_fs(struct super_block *, int *, char *);
85 static int udf_check_valid(struct super_block *, int, int);
86 static int udf_vrs(struct super_block *sb, int silent);
87 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
88 static void udf_find_anchor(struct super_block *);
89 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
91 static void udf_load_fileset(struct super_block *, struct buffer_head *,
93 static void udf_open_lvid(struct super_block *);
94 static void udf_close_lvid(struct super_block *);
95 static unsigned int udf_count_free(struct super_block *);
96 static int udf_statfs(struct dentry *, struct kstatfs *);
97 static int udf_show_options(struct seq_file *, struct vfsmount *);
98 static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
101 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
111 /* UDF filesystem type */
112 static int udf_get_sb(struct file_system_type *fs_type,
113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
119 static struct file_system_type udf_fstype = {
120 .owner = THIS_MODULE,
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
127 static struct kmem_cache *udf_inode_cachep;
129 static struct inode *udf_alloc_inode(struct super_block *sb)
131 struct udf_inode_info *ei;
132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
142 return &ei->vfs_inode;
145 static void udf_destroy_inode(struct inode *inode)
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
150 static void init_once(struct kmem_cache *cachep, void *foo)
152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
158 static int init_inodecache(void)
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
162 0, (SLAB_RECLAIM_ACCOUNT |
165 if (!udf_inode_cachep)
170 static void destroy_inodecache(void)
172 kmem_cache_destroy(udf_inode_cachep);
175 /* Superblock operations */
176 static const struct super_operations udf_sb_ops = {
177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
186 .show_options = udf_show_options,
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
203 struct nls_table *nls_map;
206 static int __init init_udf_fs(void)
210 err = init_inodecache();
213 err = register_filesystem(&udf_fstype);
220 destroy_inodecache();
226 static void __exit exit_udf_fs(void)
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
232 module_init(init_udf_fs)
233 module_exit(exit_udf_fs)
235 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
237 struct udf_sb_info *sbi = UDF_SB(sb);
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
241 if (!sbi->s_partmaps) {
242 udf_error(sb, __FUNCTION__,
243 "Unable to allocate space for %d partition maps",
245 sbi->s_partitions = 0;
249 sbi->s_partitions = count;
253 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
260 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
285 seq_printf(seq, ",session=%u", sbi->s_session);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
287 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
289 * s_anchor[2] could be zeroed out in case there is no anchor
290 * in the specified block, but then the "anchor=N" option
291 * originally given by the user wasn't effective, so it's OK
292 * if we don't show it.
294 if (sbi->s_anchor[2] != 0)
295 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
297 * volume, partition, fileset and rootdir seem to be ignored
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
301 seq_puts(seq, ",utf8");
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
303 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
312 * Parse mount options.
315 * The following mount options are supported:
317 * gid= Set the default group.
318 * umask= Set the default umask.
319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
330 * The remaining are for debugging and disaster recovery:
332 * novrs Skip volume sequence recognition
334 * The following expect a offset from 0.
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
342 * The following expect a offset from the partition root.
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
371 static match_table_t tokens = {
372 {Opt_novrs, "novrs"},
373 {Opt_nostrict, "nostrict"},
375 {Opt_unhide, "unhide"},
376 {Opt_undelete, "undelete"},
377 {Opt_noadinicb, "noadinicb"},
378 {Opt_adinicb, "adinicb"},
379 {Opt_shortad, "shortad"},
380 {Opt_longad, "longad"},
381 {Opt_uforget, "uid=forget"},
382 {Opt_uignore, "uid=ignore"},
383 {Opt_gforget, "gid=forget"},
384 {Opt_gignore, "gid=ignore"},
387 {Opt_umask, "umask=%o"},
388 {Opt_session, "session=%u"},
389 {Opt_lastblock, "lastblock=%u"},
390 {Opt_anchor, "anchor=%u"},
391 {Opt_volume, "volume=%u"},
392 {Opt_partition, "partition=%u"},
393 {Opt_fileset, "fileset=%u"},
394 {Opt_rootdir, "rootdir=%u"},
396 {Opt_iocharset, "iocharset=%s"},
400 static int udf_parse_options(char *options, struct udf_options *uopt,
407 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
408 uopt->partition = 0xFFFF;
409 uopt->session = 0xFFFFFFFF;
412 uopt->volume = 0xFFFFFFFF;
413 uopt->rootdir = 0xFFFFFFFF;
414 uopt->fileset = 0xFFFFFFFF;
415 uopt->nls_map = NULL;
420 while ((p = strsep(&options, ",")) != NULL) {
421 substring_t args[MAX_OPT_ARGS];
426 token = match_token(p, tokens, args);
431 if (match_int(&args[0], &option))
433 uopt->blocksize = option;
436 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
439 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
442 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
445 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
448 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
451 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
454 if (match_int(args, &option))
457 uopt->flags |= (1 << UDF_FLAG_GID_SET);
460 if (match_int(args, &option))
463 uopt->flags |= (1 << UDF_FLAG_UID_SET);
466 if (match_octal(args, &option))
468 uopt->umask = option;
471 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
474 if (match_int(args, &option))
476 uopt->session = option;
478 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
481 if (match_int(args, &option))
483 uopt->lastblock = option;
485 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
488 if (match_int(args, &option))
490 uopt->anchor = option;
493 if (match_int(args, &option))
495 uopt->volume = option;
498 if (match_int(args, &option))
500 uopt->partition = option;
503 if (match_int(args, &option))
505 uopt->fileset = option;
508 if (match_int(args, &option))
510 uopt->rootdir = option;
513 uopt->flags |= (1 << UDF_FLAG_UTF8);
515 #ifdef CONFIG_UDF_NLS
517 uopt->nls_map = load_nls(args[0].from);
518 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
522 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
525 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
528 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
531 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
534 printk(KERN_ERR "udf: bad mount option \"%s\" "
535 "or missing value\n", p);
542 static void udf_write_super(struct super_block *sb)
546 if (!(sb->s_flags & MS_RDONLY))
553 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
555 struct udf_options uopt;
556 struct udf_sb_info *sbi = UDF_SB(sb);
558 uopt.flags = sbi->s_flags;
559 uopt.uid = sbi->s_uid;
560 uopt.gid = sbi->s_gid;
561 uopt.umask = sbi->s_umask;
563 if (!udf_parse_options(options, &uopt, true))
566 sbi->s_flags = uopt.flags;
567 sbi->s_uid = uopt.uid;
568 sbi->s_gid = uopt.gid;
569 sbi->s_umask = uopt.umask;
571 if (sbi->s_lvid_bh) {
572 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
573 if (write_rev > UDF_MAX_WRITE_VERSION)
577 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
579 if (*flags & MS_RDONLY)
587 static int udf_vrs(struct super_block *sb, int silent)
589 struct volStructDesc *vsd = NULL;
590 loff_t sector = 32768;
592 struct buffer_head *bh = NULL;
596 struct udf_sb_info *sbi;
598 /* Block size must be a multiple of 512 */
599 if (sb->s_blocksize & 511)
603 if (sb->s_blocksize < sizeof(struct volStructDesc))
604 sectorsize = sizeof(struct volStructDesc);
606 sectorsize = sb->s_blocksize;
608 sector += (sbi->s_session << sb->s_blocksize_bits);
610 udf_debug("Starting at sector %u (%ld byte sectors)\n",
611 (unsigned int)(sector >> sb->s_blocksize_bits),
613 /* Process the sequence (if applicable) */
614 for (; !nsr02 && !nsr03; sector += sectorsize) {
616 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
620 /* Look for ISO descriptors */
621 vsd = (struct volStructDesc *)(bh->b_data +
622 (sector & (sb->s_blocksize - 1)));
624 if (vsd->stdIdent[0] == 0) {
627 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
630 switch (vsd->structType) {
632 udf_debug("ISO9660 Boot Record found\n");
635 udf_debug("ISO9660 Primary Volume Descriptor "
639 udf_debug("ISO9660 Supplementary Volume "
640 "Descriptor found\n");
643 udf_debug("ISO9660 Volume Partition Descriptor "
647 udf_debug("ISO9660 Volume Descriptor Set "
648 "Terminator found\n");
651 udf_debug("ISO9660 VRS (%u) found\n",
655 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
658 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
662 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
665 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
675 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
682 * Check whether there is an anchor block in the given block
684 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
687 struct buffer_head *bh = NULL;
693 if (udf_fixed_to_variable(block) >=
694 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
696 bh = sb_bread(sb, udf_fixed_to_variable(block));
699 bh = sb_bread(sb, block);
704 t = (tag *)bh->b_data;
705 ident = le16_to_cpu(t->tagIdent);
706 location = le32_to_cpu(t->tagLocation);
708 if (ident != TAG_IDENT_AVDP)
710 return location == block;
713 /* Search for an anchor volume descriptor pointer */
714 static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
719 struct udf_sb_info *sbi = UDF_SB(sb);
722 last[1] = last[0] - 1;
723 last[2] = last[0] + 1;
724 last[3] = last[0] - 2;
725 last[4] = last[0] - 150;
726 last[5] = last[0] - 152;
728 /* according to spec, anchor is in either:
732 * however, if the disc isn't closed, it could be 512 */
734 for (i = 0; i < ARRAY_SIZE(last); i++) {
737 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
738 sb->s_blocksize_bits)
741 if (udf_check_anchor_block(sb, last[i], varconv)) {
742 sbi->s_anchor[0] = last[i];
743 sbi->s_anchor[1] = last[i] - 256;
750 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
751 sbi->s_anchor[1] = last[i] - 256;
756 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
757 sbi->s_anchor[0] = sbi->s_session + 256;
760 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
761 sbi->s_anchor[0] = sbi->s_session + 512;
768 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
769 * be the last block on the media.
771 * Return 1 if not found, 0 if ok
774 static void udf_find_anchor(struct super_block *sb)
777 struct buffer_head *bh = NULL;
780 struct udf_sb_info *sbi = UDF_SB(sb);
782 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
786 /* No anchor found? Try VARCONV conversion of block numbers */
787 /* Firstly, we try to not convert number of the last block */
788 lastblock = udf_scan_anchors(sb, 1,
789 udf_variable_to_fixed(sbi->s_last_block));
791 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
795 /* Secondly, we try with converted number of the last block */
796 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
798 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
802 * Check located anchors and the anchor block supplied via
805 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
806 if (!sbi->s_anchor[i])
808 bh = udf_read_tagged(sb, sbi->s_anchor[i],
809 sbi->s_anchor[i], &ident);
811 sbi->s_anchor[i] = 0;
814 if (ident != TAG_IDENT_AVDP)
815 sbi->s_anchor[i] = 0;
819 sbi->s_last_block = lastblock;
822 static int udf_find_fileset(struct super_block *sb,
823 kernel_lb_addr *fileset,
824 kernel_lb_addr *root)
826 struct buffer_head *bh = NULL;
829 struct udf_sb_info *sbi;
831 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
832 fileset->partitionReferenceNum != 0xFFFF) {
833 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
837 } else if (ident != TAG_IDENT_FSD) {
846 /* Search backwards through the partitions */
847 kernel_lb_addr newfileset;
849 /* --> cvg: FIXME - is it reasonable? */
852 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
853 (newfileset.partitionReferenceNum != 0xFFFF &&
854 fileset->logicalBlockNum == 0xFFFFFFFF &&
855 fileset->partitionReferenceNum == 0xFFFF);
856 newfileset.partitionReferenceNum--) {
857 lastblock = sbi->s_partmaps
858 [newfileset.partitionReferenceNum]
860 newfileset.logicalBlockNum = 0;
863 bh = udf_read_ptagged(sb, newfileset, 0,
866 newfileset.logicalBlockNum++;
873 struct spaceBitmapDesc *sp;
874 sp = (struct spaceBitmapDesc *)
876 newfileset.logicalBlockNum += 1 +
877 ((le32_to_cpu(sp->numOfBytes) +
878 sizeof(struct spaceBitmapDesc)
879 - 1) >> sb->s_blocksize_bits);
884 *fileset = newfileset;
887 newfileset.logicalBlockNum++;
892 } while (newfileset.logicalBlockNum < lastblock &&
893 fileset->logicalBlockNum == 0xFFFFFFFF &&
894 fileset->partitionReferenceNum == 0xFFFF);
898 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
899 fileset->partitionReferenceNum != 0xFFFF) && bh) {
900 udf_debug("Fileset at block=%d, partition=%d\n",
901 fileset->logicalBlockNum,
902 fileset->partitionReferenceNum);
904 sbi->s_partition = fileset->partitionReferenceNum;
905 udf_load_fileset(sb, bh, root);
912 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
914 struct primaryVolDesc *pvoldesc;
917 struct buffer_head *bh;
920 bh = udf_read_tagged(sb, block, block, &ident);
923 BUG_ON(ident != TAG_IDENT_PVD);
925 pvoldesc = (struct primaryVolDesc *)bh->b_data;
927 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
928 pvoldesc->recordingDateAndTime)) {
930 timestamp *ts = &pvoldesc->recordingDateAndTime;
931 udf_debug("recording time %04u/%02u/%02u"
933 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
934 ts->minute, le16_to_cpu(ts->typeAndTimezone));
938 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
939 if (udf_CS0toUTF8(&outstr, &instr)) {
940 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
941 outstr.u_len > 31 ? 31 : outstr.u_len);
942 udf_debug("volIdent[] = '%s'\n",
943 UDF_SB(sb)->s_volume_ident);
946 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
947 if (udf_CS0toUTF8(&outstr, &instr))
948 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
954 static int udf_load_metadata_files(struct super_block *sb, int partition)
956 struct udf_sb_info *sbi = UDF_SB(sb);
957 struct udf_part_map *map;
958 struct udf_meta_data *mdata;
962 map = &sbi->s_partmaps[partition];
963 mdata = &map->s_type_specific.s_metadata;
965 /* metadata address */
966 addr.logicalBlockNum = mdata->s_meta_file_loc;
967 addr.partitionReferenceNum = map->s_partition_num;
969 udf_debug("Metadata file location: block = %d part = %d\n",
970 addr.logicalBlockNum, addr.partitionReferenceNum);
972 mdata->s_metadata_fe = udf_iget(sb, addr);
974 if (mdata->s_metadata_fe == NULL) {
975 udf_warning(sb, __func__, "metadata inode efe not found, "
976 "will try mirror inode.");
978 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
979 ICBTAG_FLAG_AD_SHORT) {
980 udf_warning(sb, __func__, "metadata inode efe does not have "
981 "short allocation descriptors!");
983 iput(mdata->s_metadata_fe);
984 mdata->s_metadata_fe = NULL;
987 /* mirror file entry */
988 addr.logicalBlockNum = mdata->s_mirror_file_loc;
989 addr.partitionReferenceNum = map->s_partition_num;
991 udf_debug("Mirror metadata file location: block = %d part = %d\n",
992 addr.logicalBlockNum, addr.partitionReferenceNum);
994 mdata->s_mirror_fe = udf_iget(sb, addr);
996 if (mdata->s_mirror_fe == NULL) {
998 udf_error(sb, __func__, "mirror inode efe not found "
999 "and metadata inode is missing too, exiting...");
1002 udf_warning(sb, __func__, "mirror inode efe not found,"
1003 " but metadata inode is OK");
1004 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1005 ICBTAG_FLAG_AD_SHORT) {
1006 udf_warning(sb, __func__, "mirror inode efe does not have "
1007 "short allocation descriptors!");
1008 iput(mdata->s_mirror_fe);
1009 mdata->s_mirror_fe = NULL;
1017 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1019 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1020 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1021 addr.partitionReferenceNum = map->s_partition_num;
1023 udf_debug("Bitmap file location: block = %d part = %d\n",
1024 addr.logicalBlockNum, addr.partitionReferenceNum);
1026 mdata->s_bitmap_fe = udf_iget(sb, addr);
1028 if (mdata->s_bitmap_fe == NULL) {
1029 if (sb->s_flags & MS_RDONLY)
1030 udf_warning(sb, __func__, "bitmap inode efe "
1031 "not found but it's ok since the disc"
1032 " is mounted read-only");
1034 udf_error(sb, __func__, "bitmap inode efe not "
1035 "found and attempted read-write mount");
1041 udf_debug("udf_load_metadata_files Ok\n");
1049 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1050 kernel_lb_addr *root)
1052 struct fileSetDesc *fset;
1054 fset = (struct fileSetDesc *)bh->b_data;
1056 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1058 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1060 udf_debug("Rootdir at block=%d, partition=%d\n",
1061 root->logicalBlockNum, root->partitionReferenceNum);
1064 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1066 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1067 return DIV_ROUND_UP(map->s_partition_len +
1068 (sizeof(struct spaceBitmapDesc) << 3),
1069 sb->s_blocksize * 8);
1072 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1074 struct udf_bitmap *bitmap;
1078 nr_groups = udf_compute_nr_groups(sb, index);
1079 size = sizeof(struct udf_bitmap) +
1080 (sizeof(struct buffer_head *) * nr_groups);
1082 if (size <= PAGE_SIZE)
1083 bitmap = kmalloc(size, GFP_KERNEL);
1085 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1087 if (bitmap == NULL) {
1088 udf_error(sb, __FUNCTION__,
1089 "Unable to allocate space for bitmap "
1090 "and %d buffer_head pointers", nr_groups);
1094 memset(bitmap, 0x00, size);
1095 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1096 bitmap->s_nr_groups = nr_groups;
1100 static int udf_fill_partdesc_info(struct super_block *sb,
1101 struct partitionDesc *p, int p_index)
1103 struct udf_part_map *map;
1104 struct udf_sb_info *sbi = UDF_SB(sb);
1105 struct partitionHeaderDesc *phd;
1107 map = &sbi->s_partmaps[p_index];
1109 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1110 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1112 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1113 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1114 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1115 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1116 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1117 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1118 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1119 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1121 udf_debug("Partition (%d type %x) starts at physical %d, "
1122 "block length %d\n", p_index,
1123 map->s_partition_type, map->s_partition_root,
1124 map->s_partition_len);
1126 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1127 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1130 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1131 if (phd->unallocSpaceTable.extLength) {
1132 kernel_lb_addr loc = {
1133 .logicalBlockNum = le32_to_cpu(
1134 phd->unallocSpaceTable.extPosition),
1135 .partitionReferenceNum = p_index,
1138 map->s_uspace.s_table = udf_iget(sb, loc);
1139 if (!map->s_uspace.s_table) {
1140 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1144 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1145 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1146 p_index, map->s_uspace.s_table->i_ino);
1149 if (phd->unallocSpaceBitmap.extLength) {
1150 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1153 map->s_uspace.s_bitmap = bitmap;
1154 bitmap->s_extLength = le32_to_cpu(
1155 phd->unallocSpaceBitmap.extLength);
1156 bitmap->s_extPosition = le32_to_cpu(
1157 phd->unallocSpaceBitmap.extPosition);
1158 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1159 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1160 bitmap->s_extPosition);
1163 if (phd->partitionIntegrityTable.extLength)
1164 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1166 if (phd->freedSpaceTable.extLength) {
1167 kernel_lb_addr loc = {
1168 .logicalBlockNum = le32_to_cpu(
1169 phd->freedSpaceTable.extPosition),
1170 .partitionReferenceNum = p_index,
1173 map->s_fspace.s_table = udf_iget(sb, loc);
1174 if (!map->s_fspace.s_table) {
1175 udf_debug("cannot load freedSpaceTable (part %d)\n",
1180 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1181 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1182 p_index, map->s_fspace.s_table->i_ino);
1185 if (phd->freedSpaceBitmap.extLength) {
1186 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1189 map->s_fspace.s_bitmap = bitmap;
1190 bitmap->s_extLength = le32_to_cpu(
1191 phd->freedSpaceBitmap.extLength);
1192 bitmap->s_extPosition = le32_to_cpu(
1193 phd->freedSpaceBitmap.extPosition);
1194 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1195 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1196 bitmap->s_extPosition);
1201 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1203 struct udf_sb_info *sbi = UDF_SB(sb);
1204 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1206 struct buffer_head *bh = NULL;
1207 struct udf_inode_info *vati;
1209 struct virtualAllocationTable20 *vat20;
1211 /* VAT file entry is in the last recorded block */
1212 ino.partitionReferenceNum = type1_index;
1213 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1214 sbi->s_vat_inode = udf_iget(sb, ino);
1215 if (!sbi->s_vat_inode)
1218 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1219 map->s_type_specific.s_virtual.s_start_offset = 0;
1220 map->s_type_specific.s_virtual.s_num_entries =
1221 (sbi->s_vat_inode->i_size - 36) >> 2;
1222 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1223 vati = UDF_I(sbi->s_vat_inode);
1224 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1225 pos = udf_block_map(sbi->s_vat_inode, 0);
1226 bh = sb_bread(sb, pos);
1229 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1231 vat20 = (struct virtualAllocationTable20 *)
1235 map->s_type_specific.s_virtual.s_start_offset =
1236 le16_to_cpu(vat20->lengthHeader);
1237 map->s_type_specific.s_virtual.s_num_entries =
1238 (sbi->s_vat_inode->i_size -
1239 map->s_type_specific.s_virtual.
1240 s_start_offset) >> 2;
1246 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1248 struct buffer_head *bh;
1249 struct partitionDesc *p;
1250 struct udf_part_map *map;
1251 struct udf_sb_info *sbi = UDF_SB(sb);
1253 uint16_t partitionNumber;
1257 bh = udf_read_tagged(sb, block, block, &ident);
1260 if (ident != TAG_IDENT_PD)
1263 p = (struct partitionDesc *)bh->b_data;
1264 partitionNumber = le16_to_cpu(p->partitionNumber);
1266 /* First scan for TYPE1, SPARABLE and METADATA partitions */
1267 for (i = 0; i < sbi->s_partitions; i++) {
1268 map = &sbi->s_partmaps[i];
1269 udf_debug("Searching map: (%d == %d)\n",
1270 map->s_partition_num, partitionNumber);
1271 if (map->s_partition_num == partitionNumber &&
1272 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1273 map->s_partition_type == UDF_SPARABLE_MAP15))
1277 if (i >= sbi->s_partitions) {
1278 udf_debug("Partition (%d) not found in partition map\n",
1283 ret = udf_fill_partdesc_info(sb, p, i);
1286 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1287 * PHYSICAL partitions are already set up
1290 for (i = 0; i < sbi->s_partitions; i++) {
1291 map = &sbi->s_partmaps[i];
1293 if (map->s_partition_num == partitionNumber &&
1294 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1295 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1296 map->s_partition_type == UDF_METADATA_MAP25))
1300 if (i >= sbi->s_partitions)
1303 ret = udf_fill_partdesc_info(sb, p, i);
1307 if (map->s_partition_type == UDF_METADATA_MAP25) {
1308 ret = udf_load_metadata_files(sb, i);
1310 printk(KERN_ERR "UDF-fs: error loading MetaData "
1311 "partition map %d\n", i);
1315 ret = udf_load_vat(sb, i, type1_idx);
1319 * Mark filesystem read-only if we have a partition with
1320 * virtual map since we don't handle writing to it (we
1321 * overwrite blocks instead of relocating them).
1323 sb->s_flags |= MS_RDONLY;
1324 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1325 "because writing to pseudooverwrite partition is "
1326 "not implemented.\n");
1329 /* In case loading failed, we handle cleanup in udf_fill_super */
1334 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1335 kernel_lb_addr *fileset)
1337 struct logicalVolDesc *lvd;
1340 struct udf_sb_info *sbi = UDF_SB(sb);
1341 struct genericPartitionMap *gpm;
1343 struct buffer_head *bh;
1346 bh = udf_read_tagged(sb, block, block, &ident);
1349 BUG_ON(ident != TAG_IDENT_LVD);
1350 lvd = (struct logicalVolDesc *)bh->b_data;
1352 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1358 for (i = 0, offset = 0;
1359 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1360 i++, offset += gpm->partitionMapLength) {
1361 struct udf_part_map *map = &sbi->s_partmaps[i];
1362 gpm = (struct genericPartitionMap *)
1363 &(lvd->partitionMaps[offset]);
1364 type = gpm->partitionMapType;
1366 struct genericPartitionMap1 *gpm1 =
1367 (struct genericPartitionMap1 *)gpm;
1368 map->s_partition_type = UDF_TYPE1_MAP15;
1369 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1370 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1371 map->s_partition_func = NULL;
1372 } else if (type == 2) {
1373 struct udfPartitionMap2 *upm2 =
1374 (struct udfPartitionMap2 *)gpm;
1375 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1376 strlen(UDF_ID_VIRTUAL))) {
1378 le16_to_cpu(((__le16 *)upm2->partIdent.
1381 map->s_partition_type =
1383 map->s_partition_func =
1384 udf_get_pblock_virt15;
1386 map->s_partition_type =
1388 map->s_partition_func =
1389 udf_get_pblock_virt20;
1391 } else if (!strncmp(upm2->partIdent.ident,
1393 strlen(UDF_ID_SPARABLE))) {
1395 struct sparingTable *st;
1396 struct sparablePartitionMap *spm =
1397 (struct sparablePartitionMap *)gpm;
1399 map->s_partition_type = UDF_SPARABLE_MAP15;
1400 map->s_type_specific.s_sparing.s_packet_len =
1401 le16_to_cpu(spm->packetLength);
1402 for (j = 0; j < spm->numSparingTables; j++) {
1403 struct buffer_head *bh2;
1406 spm->locSparingTable[j]);
1407 bh2 = udf_read_tagged(sb, loc, loc,
1409 map->s_type_specific.s_sparing.
1410 s_spar_map[j] = bh2;
1415 st = (struct sparingTable *)bh2->b_data;
1416 if (ident != 0 || strncmp(
1417 st->sparingIdent.ident,
1419 strlen(UDF_ID_SPARING))) {
1421 map->s_type_specific.s_sparing.
1422 s_spar_map[j] = NULL;
1425 map->s_partition_func = udf_get_pblock_spar15;
1426 } else if (!strncmp(upm2->partIdent.ident,
1428 strlen(UDF_ID_METADATA))) {
1429 struct udf_meta_data *mdata =
1430 &map->s_type_specific.s_metadata;
1431 struct metadataPartitionMap *mdm =
1432 (struct metadataPartitionMap *)
1433 &(lvd->partitionMaps[offset]);
1434 udf_debug("Parsing Logical vol part %d "
1435 "type %d id=%s\n", i, type,
1438 map->s_partition_type = UDF_METADATA_MAP25;
1439 map->s_partition_func = udf_get_pblock_meta25;
1441 mdata->s_meta_file_loc =
1442 le32_to_cpu(mdm->metadataFileLoc);
1443 mdata->s_mirror_file_loc =
1444 le32_to_cpu(mdm->metadataMirrorFileLoc);
1445 mdata->s_bitmap_file_loc =
1446 le32_to_cpu(mdm->metadataBitmapFileLoc);
1447 mdata->s_alloc_unit_size =
1448 le32_to_cpu(mdm->allocUnitSize);
1449 mdata->s_align_unit_size =
1450 le16_to_cpu(mdm->alignUnitSize);
1451 mdata->s_dup_md_flag =
1454 udf_debug("Metadata Ident suffix=0x%x\n",
1457 mdm->partIdent.identSuffix)[0])));
1458 udf_debug("Metadata part num=%d\n",
1459 le16_to_cpu(mdm->partitionNum));
1460 udf_debug("Metadata part alloc unit size=%d\n",
1461 le32_to_cpu(mdm->allocUnitSize));
1462 udf_debug("Metadata file loc=%d\n",
1463 le32_to_cpu(mdm->metadataFileLoc));
1464 udf_debug("Mirror file loc=%d\n",
1465 le32_to_cpu(mdm->metadataMirrorFileLoc));
1466 udf_debug("Bitmap file loc=%d\n",
1467 le32_to_cpu(mdm->metadataBitmapFileLoc));
1468 udf_debug("Duplicate Flag: %d %d\n",
1469 mdata->s_dup_md_flag, mdm->flags);
1471 udf_debug("Unknown ident: %s\n",
1472 upm2->partIdent.ident);
1475 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1476 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1478 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1479 i, map->s_partition_num, type,
1480 map->s_volumeseqnum);
1484 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1486 *fileset = lelb_to_cpu(la->extLocation);
1487 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1488 "partition=%d\n", fileset->logicalBlockNum,
1489 fileset->partitionReferenceNum);
1491 if (lvd->integritySeqExt.extLength)
1492 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1500 * udf_load_logicalvolint
1503 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1505 struct buffer_head *bh = NULL;
1507 struct udf_sb_info *sbi = UDF_SB(sb);
1508 struct logicalVolIntegrityDesc *lvid;
1510 while (loc.extLength > 0 &&
1511 (bh = udf_read_tagged(sb, loc.extLocation,
1512 loc.extLocation, &ident)) &&
1513 ident == TAG_IDENT_LVID) {
1514 sbi->s_lvid_bh = bh;
1515 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1517 if (lvid->nextIntegrityExt.extLength)
1518 udf_load_logicalvolint(sb,
1519 leea_to_cpu(lvid->nextIntegrityExt));
1521 if (sbi->s_lvid_bh != bh)
1523 loc.extLength -= sb->s_blocksize;
1526 if (sbi->s_lvid_bh != bh)
1531 * udf_process_sequence
1534 * Process a main/reserve volume descriptor sequence.
1537 * sb Pointer to _locked_ superblock.
1538 * block First block of first extent of the sequence.
1539 * lastblock Lastblock of first extent of the sequence.
1542 * July 1, 1997 - Andrew E. Mileski
1543 * Written, tested, and released.
1545 static noinline int udf_process_sequence(struct super_block *sb, long block,
1546 long lastblock, kernel_lb_addr *fileset)
1548 struct buffer_head *bh = NULL;
1549 struct udf_vds_record vds[VDS_POS_LENGTH];
1550 struct udf_vds_record *curr;
1551 struct generic_desc *gd;
1552 struct volDescPtr *vdp;
1556 long next_s = 0, next_e = 0;
1558 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1561 * Read the main descriptor sequence and find which descriptors
1564 for (; (!done && block <= lastblock); block++) {
1566 bh = udf_read_tagged(sb, block, block, &ident);
1568 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1569 "sequence is corrupted or we could not read "
1570 "it.\n", (unsigned long long)block);
1574 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1575 gd = (struct generic_desc *)bh->b_data;
1576 vdsn = le32_to_cpu(gd->volDescSeqNum);
1578 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1579 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1580 if (vdsn >= curr->volDescSeqNum) {
1581 curr->volDescSeqNum = vdsn;
1582 curr->block = block;
1585 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1586 curr = &vds[VDS_POS_VOL_DESC_PTR];
1587 if (vdsn >= curr->volDescSeqNum) {
1588 curr->volDescSeqNum = vdsn;
1589 curr->block = block;
1591 vdp = (struct volDescPtr *)bh->b_data;
1592 next_s = le32_to_cpu(
1593 vdp->nextVolDescSeqExt.extLocation);
1594 next_e = le32_to_cpu(
1595 vdp->nextVolDescSeqExt.extLength);
1596 next_e = next_e >> sb->s_blocksize_bits;
1600 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1601 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1602 if (vdsn >= curr->volDescSeqNum) {
1603 curr->volDescSeqNum = vdsn;
1604 curr->block = block;
1607 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1608 curr = &vds[VDS_POS_PARTITION_DESC];
1610 curr->block = block;
1612 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1613 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1614 if (vdsn >= curr->volDescSeqNum) {
1615 curr->volDescSeqNum = vdsn;
1616 curr->block = block;
1619 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1620 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1621 if (vdsn >= curr->volDescSeqNum) {
1622 curr->volDescSeqNum = vdsn;
1623 curr->block = block;
1626 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1627 vds[VDS_POS_TERMINATING_DESC].block = block;
1631 next_s = next_e = 0;
1639 * Now read interesting descriptors again and process them
1640 * in a suitable order
1642 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1643 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1646 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1649 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1650 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1653 if (vds[VDS_POS_PARTITION_DESC].block) {
1655 * We rescan the whole descriptor sequence to find
1656 * partition descriptor blocks and process them.
1658 for (block = vds[VDS_POS_PARTITION_DESC].block;
1659 block < vds[VDS_POS_TERMINATING_DESC].block;
1661 if (udf_load_partdesc(sb, block))
1671 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1674 struct udf_sb_info *sbi = UDF_SB(sb);
1677 udf_debug("Validity check skipped because of novrs option\n");
1680 /* Check that it is NSR02 compliant */
1681 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1682 block = udf_vrs(sb, silent);
1684 udf_debug("Failed to read byte 32768. Assuming open "
1685 "disc. Skipping validity check\n");
1686 if (block && !sbi->s_last_block)
1687 sbi->s_last_block = udf_get_last_block(sb);
1691 static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
1693 struct anchorVolDescPtr *anchor;
1695 struct buffer_head *bh;
1696 long main_s, main_e, reserve_s, reserve_e;
1698 struct udf_sb_info *sbi;
1704 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1705 if (!sbi->s_anchor[i])
1708 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1713 anchor = (struct anchorVolDescPtr *)bh->b_data;
1715 /* Locate the main sequence */
1716 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1717 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1718 main_e = main_e >> sb->s_blocksize_bits;
1721 /* Locate the reserve sequence */
1722 reserve_s = le32_to_cpu(
1723 anchor->reserveVolDescSeqExt.extLocation);
1724 reserve_e = le32_to_cpu(
1725 anchor->reserveVolDescSeqExt.extLength);
1726 reserve_e = reserve_e >> sb->s_blocksize_bits;
1727 reserve_e += reserve_s;
1731 /* Process the main & reserve sequences */
1732 /* responsible for finding the PartitionDesc(s) */
1733 if (!(udf_process_sequence(sb, main_s, main_e,
1735 udf_process_sequence(sb, reserve_s, reserve_e,
1740 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1741 udf_debug("No Anchor block found\n");
1744 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1749 static void udf_open_lvid(struct super_block *sb)
1751 struct udf_sb_info *sbi = UDF_SB(sb);
1752 struct buffer_head *bh = sbi->s_lvid_bh;
1753 struct logicalVolIntegrityDesc *lvid;
1754 struct logicalVolIntegrityDescImpUse *lvidiu;
1758 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1759 lvidiu = udf_sb_lvidiu(sbi);
1761 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1762 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1763 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1765 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1767 lvid->descTag.descCRC = cpu_to_le16(
1768 udf_crc((char *)lvid + sizeof(tag),
1769 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1771 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1772 mark_buffer_dirty(bh);
1775 static void udf_close_lvid(struct super_block *sb)
1777 struct udf_sb_info *sbi = UDF_SB(sb);
1778 struct buffer_head *bh = sbi->s_lvid_bh;
1779 struct logicalVolIntegrityDesc *lvid;
1780 struct logicalVolIntegrityDescImpUse *lvidiu;
1785 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1787 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1790 lvidiu = udf_sb_lvidiu(sbi);
1791 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1792 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1793 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1794 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1795 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1796 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1797 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1798 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1799 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1800 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1802 lvid->descTag.descCRC = cpu_to_le16(
1803 udf_crc((char *)lvid + sizeof(tag),
1804 le16_to_cpu(lvid->descTag.descCRCLength),
1807 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1808 mark_buffer_dirty(bh);
1811 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1814 int nr_groups = bitmap->s_nr_groups;
1815 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1818 for (i = 0; i < nr_groups; i++)
1819 if (bitmap->s_block_bitmap[i])
1820 brelse(bitmap->s_block_bitmap[i]);
1822 if (size <= PAGE_SIZE)
1828 static void udf_free_partition(struct udf_part_map *map)
1831 struct udf_meta_data *mdata;
1833 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1834 iput(map->s_uspace.s_table);
1835 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1836 iput(map->s_fspace.s_table);
1837 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1838 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1839 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1840 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1841 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1842 for (i = 0; i < 4; i++)
1843 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1844 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1845 mdata = &map->s_type_specific.s_metadata;
1846 iput(mdata->s_metadata_fe);
1847 mdata->s_metadata_fe = NULL;
1849 iput(mdata->s_mirror_fe);
1850 mdata->s_mirror_fe = NULL;
1852 iput(mdata->s_bitmap_fe);
1853 mdata->s_bitmap_fe = NULL;
1857 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1860 struct inode *inode = NULL;
1861 struct udf_options uopt;
1862 kernel_lb_addr rootdir, fileset;
1863 struct udf_sb_info *sbi;
1865 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1870 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1874 sb->s_fs_info = sbi;
1876 mutex_init(&sbi->s_alloc_mutex);
1878 if (!udf_parse_options((char *)options, &uopt, false))
1881 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1882 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1883 udf_error(sb, "udf_read_super",
1884 "utf8 cannot be combined with iocharset\n");
1887 #ifdef CONFIG_UDF_NLS
1888 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1889 uopt.nls_map = load_nls_default();
1891 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1893 udf_debug("Using default NLS map\n");
1896 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1897 uopt.flags |= (1 << UDF_FLAG_UTF8);
1899 fileset.logicalBlockNum = 0xFFFFFFFF;
1900 fileset.partitionReferenceNum = 0xFFFF;
1902 sbi->s_flags = uopt.flags;
1903 sbi->s_uid = uopt.uid;
1904 sbi->s_gid = uopt.gid;
1905 sbi->s_umask = uopt.umask;
1906 sbi->s_nls_map = uopt.nls_map;
1908 /* Set the block size for all transfers */
1909 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1910 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1911 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1915 if (uopt.session == 0xFFFFFFFF)
1916 sbi->s_session = udf_get_last_session(sb);
1918 sbi->s_session = uopt.session;
1920 udf_debug("Multi-session=%d\n", sbi->s_session);
1922 sbi->s_last_block = uopt.lastblock;
1923 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1924 sbi->s_anchor[2] = uopt.anchor;
1926 if (udf_check_valid(sb, uopt.novrs, silent)) {
1927 /* read volume recognition sequences */
1928 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1932 udf_find_anchor(sb);
1934 /* Fill in the rest of the superblock */
1935 sb->s_op = &udf_sb_ops;
1938 sb->s_magic = UDF_SUPER_MAGIC;
1939 sb->s_time_gran = 1000;
1941 if (udf_load_sequence(sb, &fileset)) {
1942 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1946 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1948 if (sbi->s_lvid_bh) {
1949 struct logicalVolIntegrityDescImpUse *lvidiu =
1951 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1952 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1953 /* uint16_t maxUDFWriteRev =
1954 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1956 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1957 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1959 le16_to_cpu(lvidiu->minUDFReadRev),
1960 UDF_MAX_READ_VERSION);
1962 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1963 sb->s_flags |= MS_RDONLY;
1965 sbi->s_udfrev = minUDFWriteRev;
1967 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1968 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1969 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1970 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1973 if (!sbi->s_partitions) {
1974 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1978 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1979 UDF_PART_FLAG_READ_ONLY) {
1980 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1981 "forcing readonly mount\n");
1982 sb->s_flags |= MS_RDONLY;
1985 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1986 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1992 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
1993 udf_info("UDF: Mounting volume '%s', "
1994 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1995 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1996 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
1998 if (!(sb->s_flags & MS_RDONLY))
2001 /* Assign the root inode */
2002 /* assign inodes by physical block number */
2003 /* perhaps it's not extensible enough, but for now ... */
2004 inode = udf_iget(sb, rootdir);
2006 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2008 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
2012 /* Allocate a dentry for the root inode */
2013 sb->s_root = d_alloc_root(inode);
2015 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
2019 sb->s_maxbytes = MAX_LFS_FILESIZE;
2023 if (sbi->s_vat_inode)
2024 iput(sbi->s_vat_inode);
2025 if (sbi->s_partitions)
2026 for (i = 0; i < sbi->s_partitions; i++)
2027 udf_free_partition(&sbi->s_partmaps[i]);
2028 #ifdef CONFIG_UDF_NLS
2029 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2030 unload_nls(sbi->s_nls_map);
2032 if (!(sb->s_flags & MS_RDONLY))
2034 brelse(sbi->s_lvid_bh);
2036 kfree(sbi->s_partmaps);
2038 sb->s_fs_info = NULL;
2043 static void udf_error(struct super_block *sb, const char *function,
2044 const char *fmt, ...)
2048 if (!(sb->s_flags & MS_RDONLY)) {
2052 va_start(args, fmt);
2053 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2055 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
2056 sb->s_id, function, error_buf);
2059 void udf_warning(struct super_block *sb, const char *function,
2060 const char *fmt, ...)
2064 va_start(args, fmt);
2065 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
2067 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
2068 sb->s_id, function, error_buf);
2071 static void udf_put_super(struct super_block *sb)
2074 struct udf_sb_info *sbi;
2077 if (sbi->s_vat_inode)
2078 iput(sbi->s_vat_inode);
2079 if (sbi->s_partitions)
2080 for (i = 0; i < sbi->s_partitions; i++)
2081 udf_free_partition(&sbi->s_partmaps[i]);
2082 #ifdef CONFIG_UDF_NLS
2083 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
2084 unload_nls(sbi->s_nls_map);
2086 if (!(sb->s_flags & MS_RDONLY))
2088 brelse(sbi->s_lvid_bh);
2089 kfree(sbi->s_partmaps);
2090 kfree(sb->s_fs_info);
2091 sb->s_fs_info = NULL;
2094 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2096 struct super_block *sb = dentry->d_sb;
2097 struct udf_sb_info *sbi = UDF_SB(sb);
2098 struct logicalVolIntegrityDescImpUse *lvidiu;
2100 if (sbi->s_lvid_bh != NULL)
2101 lvidiu = udf_sb_lvidiu(sbi);
2105 buf->f_type = UDF_SUPER_MAGIC;
2106 buf->f_bsize = sb->s_blocksize;
2107 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2108 buf->f_bfree = udf_count_free(sb);
2109 buf->f_bavail = buf->f_bfree;
2110 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2111 le32_to_cpu(lvidiu->numDirs)) : 0)
2113 buf->f_ffree = buf->f_bfree;
2114 /* __kernel_fsid_t f_fsid */
2115 buf->f_namelen = UDF_NAME_LEN - 2;
2120 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2121 struct udf_bitmap *bitmap)
2123 struct buffer_head *bh = NULL;
2124 unsigned int accum = 0;
2126 int block = 0, newblock;
2131 struct spaceBitmapDesc *bm;
2135 loc.logicalBlockNum = bitmap->s_extPosition;
2136 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2137 bh = udf_read_ptagged(sb, loc, 0, &ident);
2140 printk(KERN_ERR "udf: udf_count_free failed\n");
2142 } else if (ident != TAG_IDENT_SBD) {
2144 printk(KERN_ERR "udf: udf_count_free failed\n");
2148 bm = (struct spaceBitmapDesc *)bh->b_data;
2149 bytes = le32_to_cpu(bm->numOfBytes);
2150 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2151 ptr = (uint8_t *)bh->b_data;
2154 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2155 accum += bitmap_weight((const unsigned long *)(ptr + index),
2160 newblock = udf_get_lb_pblock(sb, loc, ++block);
2161 bh = udf_tread(sb, newblock);
2163 udf_debug("read failed\n");
2167 ptr = (uint8_t *)bh->b_data;
2178 static unsigned int udf_count_free_table(struct super_block *sb,
2179 struct inode *table)
2181 unsigned int accum = 0;
2183 kernel_lb_addr eloc;
2185 struct extent_position epos;
2189 epos.block = UDF_I(table)->i_location;
2190 epos.offset = sizeof(struct unallocSpaceEntry);
2193 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2194 accum += (elen >> table->i_sb->s_blocksize_bits);
2203 static unsigned int udf_count_free(struct super_block *sb)
2205 unsigned int accum = 0;
2206 struct udf_sb_info *sbi;
2207 struct udf_part_map *map;
2210 if (sbi->s_lvid_bh) {
2211 struct logicalVolIntegrityDesc *lvid =
2212 (struct logicalVolIntegrityDesc *)
2213 sbi->s_lvid_bh->b_data;
2214 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2215 accum = le32_to_cpu(
2216 lvid->freeSpaceTable[sbi->s_partition]);
2217 if (accum == 0xFFFFFFFF)
2225 map = &sbi->s_partmaps[sbi->s_partition];
2226 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2227 accum += udf_count_free_bitmap(sb,
2228 map->s_uspace.s_bitmap);
2230 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2231 accum += udf_count_free_bitmap(sb,
2232 map->s_fspace.s_bitmap);
2237 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2238 accum += udf_count_free_table(sb,
2239 map->s_uspace.s_table);
2241 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2242 accum += udf_count_free_table(sb,
2243 map->s_fspace.s_table);