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;
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 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
612 /* Process the sequence (if applicable) */
613 for (; !nsr02 && !nsr03; sector += sectorsize) {
615 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
619 /* Look for ISO descriptors */
620 vsd = (struct volStructDesc *)(bh->b_data +
621 (sector & (sb->s_blocksize - 1)));
623 if (vsd->stdIdent[0] == 0) {
626 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
629 switch (vsd->structType) {
631 udf_debug("ISO9660 Boot Record found\n");
634 udf_debug("ISO9660 Primary Volume Descriptor "
638 udf_debug("ISO9660 Supplementary Volume "
639 "Descriptor found\n");
642 udf_debug("ISO9660 Volume Partition Descriptor "
646 udf_debug("ISO9660 Volume Descriptor Set "
647 "Terminator found\n");
650 udf_debug("ISO9660 VRS (%u) found\n",
654 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
657 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
661 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
664 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
674 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
681 * Check whether there is an anchor block in the given block
683 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
686 struct buffer_head *bh = NULL;
692 bh = sb_bread(sb, udf_fixed_to_variable(block));
694 bh = sb_bread(sb, block);
699 t = (tag *)bh->b_data;
700 ident = le16_to_cpu(t->tagIdent);
701 location = le32_to_cpu(t->tagLocation);
703 if (ident != TAG_IDENT_AVDP)
705 return location == block;
708 /* Search for an anchor volume descriptor pointer */
709 static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
714 struct udf_sb_info *sbi = UDF_SB(sb);
717 last[1] = last[0] - 1;
718 last[2] = last[0] + 1;
719 last[3] = last[0] - 2;
720 last[4] = last[0] - 150;
721 last[5] = last[0] - 152;
723 /* according to spec, anchor is in either:
727 * however, if the disc isn't closed, it could be 512 */
729 for (i = 0; i < ARRAY_SIZE(last); i++) {
732 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
733 sb->s_blocksize_bits)
736 if (udf_check_anchor_block(sb, last[i], varconv)) {
737 sbi->s_anchor[0] = last[i];
738 sbi->s_anchor[1] = last[i] - 256;
745 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
746 sbi->s_anchor[1] = last[i] - 256;
751 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
752 sbi->s_anchor[0] = sbi->s_session + 256;
755 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
756 sbi->s_anchor[0] = sbi->s_session + 512;
763 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
764 * be the last block on the media.
766 * Return 1 if not found, 0 if ok
769 static void udf_find_anchor(struct super_block *sb)
772 struct buffer_head *bh = NULL;
775 struct udf_sb_info *sbi = UDF_SB(sb);
777 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
781 /* No anchor found? Try VARCONV conversion of block numbers */
782 /* Firstly, we try to not convert number of the last block */
783 lastblock = udf_scan_anchors(sb, 1,
784 udf_variable_to_fixed(sbi->s_last_block));
786 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
790 /* Secondly, we try with converted number of the last block */
791 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
793 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
797 * Check located anchors and the anchor block supplied via
800 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
801 if (!sbi->s_anchor[i])
803 bh = udf_read_tagged(sb, sbi->s_anchor[i],
804 sbi->s_anchor[i], &ident);
806 sbi->s_anchor[i] = 0;
809 if (ident != TAG_IDENT_AVDP)
810 sbi->s_anchor[i] = 0;
814 sbi->s_last_block = lastblock;
817 static int udf_find_fileset(struct super_block *sb,
818 kernel_lb_addr *fileset,
819 kernel_lb_addr *root)
821 struct buffer_head *bh = NULL;
824 struct udf_sb_info *sbi;
826 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
827 fileset->partitionReferenceNum != 0xFFFF) {
828 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
832 } else if (ident != TAG_IDENT_FSD) {
841 /* Search backwards through the partitions */
842 kernel_lb_addr newfileset;
844 /* --> cvg: FIXME - is it reasonable? */
847 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
848 (newfileset.partitionReferenceNum != 0xFFFF &&
849 fileset->logicalBlockNum == 0xFFFFFFFF &&
850 fileset->partitionReferenceNum == 0xFFFF);
851 newfileset.partitionReferenceNum--) {
852 lastblock = sbi->s_partmaps
853 [newfileset.partitionReferenceNum]
855 newfileset.logicalBlockNum = 0;
858 bh = udf_read_ptagged(sb, newfileset, 0,
861 newfileset.logicalBlockNum++;
868 struct spaceBitmapDesc *sp;
869 sp = (struct spaceBitmapDesc *)
871 newfileset.logicalBlockNum += 1 +
872 ((le32_to_cpu(sp->numOfBytes) +
873 sizeof(struct spaceBitmapDesc)
874 - 1) >> sb->s_blocksize_bits);
879 *fileset = newfileset;
882 newfileset.logicalBlockNum++;
887 } while (newfileset.logicalBlockNum < lastblock &&
888 fileset->logicalBlockNum == 0xFFFFFFFF &&
889 fileset->partitionReferenceNum == 0xFFFF);
893 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
894 fileset->partitionReferenceNum != 0xFFFF) && bh) {
895 udf_debug("Fileset at block=%d, partition=%d\n",
896 fileset->logicalBlockNum,
897 fileset->partitionReferenceNum);
899 sbi->s_partition = fileset->partitionReferenceNum;
900 udf_load_fileset(sb, bh, root);
907 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
909 struct primaryVolDesc *pvoldesc;
912 struct buffer_head *bh;
915 bh = udf_read_tagged(sb, block, block, &ident);
918 BUG_ON(ident != TAG_IDENT_PVD);
920 pvoldesc = (struct primaryVolDesc *)bh->b_data;
922 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
923 pvoldesc->recordingDateAndTime)) {
925 timestamp *ts = &pvoldesc->recordingDateAndTime;
926 udf_debug("recording time %04u/%02u/%02u"
928 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
929 ts->minute, le16_to_cpu(ts->typeAndTimezone));
933 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
934 if (udf_CS0toUTF8(&outstr, &instr)) {
935 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
936 outstr.u_len > 31 ? 31 : outstr.u_len);
937 udf_debug("volIdent[] = '%s'\n",
938 UDF_SB(sb)->s_volume_ident);
941 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
942 if (udf_CS0toUTF8(&outstr, &instr))
943 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
949 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
950 kernel_lb_addr *root)
952 struct fileSetDesc *fset;
954 fset = (struct fileSetDesc *)bh->b_data;
956 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
958 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
960 udf_debug("Rootdir at block=%d, partition=%d\n",
961 root->logicalBlockNum, root->partitionReferenceNum);
964 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
966 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
967 return DIV_ROUND_UP(map->s_partition_len +
968 (sizeof(struct spaceBitmapDesc) << 3),
969 sb->s_blocksize * 8);
972 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
974 struct udf_bitmap *bitmap;
978 nr_groups = udf_compute_nr_groups(sb, index);
979 size = sizeof(struct udf_bitmap) +
980 (sizeof(struct buffer_head *) * nr_groups);
982 if (size <= PAGE_SIZE)
983 bitmap = kmalloc(size, GFP_KERNEL);
985 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
987 if (bitmap == NULL) {
988 udf_error(sb, __FUNCTION__,
989 "Unable to allocate space for bitmap "
990 "and %d buffer_head pointers", nr_groups);
994 memset(bitmap, 0x00, size);
995 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
996 bitmap->s_nr_groups = nr_groups;
1000 static int udf_fill_partdesc_info(struct super_block *sb,
1001 struct partitionDesc *p, int p_index)
1003 struct udf_part_map *map;
1004 struct udf_sb_info *sbi = UDF_SB(sb);
1005 struct partitionHeaderDesc *phd;
1007 map = &sbi->s_partmaps[p_index];
1009 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1010 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1012 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1013 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1014 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1015 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1016 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1017 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1018 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1019 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1021 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1022 "block length %d\n", partitionNumber, p_index,
1023 map->s_partition_type, map->s_partition_root,
1024 map->s_partition_len);
1026 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1027 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1030 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1031 if (phd->unallocSpaceTable.extLength) {
1032 kernel_lb_addr loc = {
1033 .logicalBlockNum = le32_to_cpu(
1034 phd->unallocSpaceTable.extPosition),
1035 .partitionReferenceNum = p_index,
1038 map->s_uspace.s_table = udf_iget(sb, loc);
1039 if (!map->s_uspace.s_table) {
1040 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1044 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1045 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1046 p_index, map->s_uspace.s_table->i_ino);
1049 if (phd->unallocSpaceBitmap.extLength) {
1050 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1053 map->s_uspace.s_bitmap = bitmap;
1054 bitmap->s_extLength = le32_to_cpu(
1055 phd->unallocSpaceBitmap.extLength);
1056 bitmap->s_extPosition = le32_to_cpu(
1057 phd->unallocSpaceBitmap.extPosition);
1058 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1059 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1060 bitmap->s_extPosition);
1063 if (phd->partitionIntegrityTable.extLength)
1064 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1066 if (phd->freedSpaceTable.extLength) {
1067 kernel_lb_addr loc = {
1068 .logicalBlockNum = le32_to_cpu(
1069 phd->freedSpaceTable.extPosition),
1070 .partitionReferenceNum = p_index,
1073 map->s_fspace.s_table = udf_iget(sb, loc);
1074 if (!map->s_fspace.s_table) {
1075 udf_debug("cannot load freedSpaceTable (part %d)\n",
1080 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1081 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1082 p_index, map->s_fspace.s_table->i_ino);
1085 if (phd->freedSpaceBitmap.extLength) {
1086 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1089 map->s_fspace.s_bitmap = bitmap;
1090 bitmap->s_extLength = le32_to_cpu(
1091 phd->freedSpaceBitmap.extLength);
1092 bitmap->s_extPosition = le32_to_cpu(
1093 phd->freedSpaceBitmap.extPosition);
1094 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1095 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1096 bitmap->s_extPosition);
1101 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1103 struct udf_sb_info *sbi = UDF_SB(sb);
1104 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1106 struct buffer_head *bh;
1108 /* VAT file entry is in the last recorded block */
1109 ino.partitionReferenceNum = type1_index;
1110 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1111 sbi->s_vat_inode = udf_iget(sb, ino);
1112 if (!sbi->s_vat_inode)
1115 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1116 map->s_type_specific.s_virtual.s_start_offset =
1117 udf_ext0_offset(sbi->s_vat_inode);
1118 map->s_type_specific.s_virtual.s_num_entries =
1119 (sbi->s_vat_inode->i_size - 36) >> 2;
1120 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1122 struct virtualAllocationTable20 *vat20;
1124 pos = udf_block_map(sbi->s_vat_inode, 0);
1125 bh = sb_bread(sb, pos);
1128 vat20 = (struct virtualAllocationTable20 *)bh->b_data +
1129 udf_ext0_offset(sbi->s_vat_inode);
1130 map->s_type_specific.s_virtual.s_start_offset =
1131 le16_to_cpu(vat20->lengthHeader) +
1132 udf_ext0_offset(sbi->s_vat_inode);
1133 map->s_type_specific.s_virtual.s_num_entries =
1134 (sbi->s_vat_inode->i_size -
1135 map->s_type_specific.s_virtual.
1136 s_start_offset) >> 2;
1142 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1144 struct buffer_head *bh;
1145 struct partitionDesc *p;
1146 struct udf_part_map *map;
1147 struct udf_sb_info *sbi = UDF_SB(sb);
1149 uint16_t partitionNumber;
1153 bh = udf_read_tagged(sb, block, block, &ident);
1156 if (ident != TAG_IDENT_PD)
1159 p = (struct partitionDesc *)bh->b_data;
1160 partitionNumber = le16_to_cpu(p->partitionNumber);
1162 /* First scan for TYPE1 and SPARABLE partitions */
1163 for (i = 0; i < sbi->s_partitions; i++) {
1164 map = &sbi->s_partmaps[i];
1165 udf_debug("Searching map: (%d == %d)\n",
1166 map->s_partition_num, partitionNumber);
1167 if (map->s_partition_num == partitionNumber &&
1168 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1169 map->s_partition_type == UDF_SPARABLE_MAP15))
1173 if (i >= sbi->s_partitions) {
1174 udf_debug("Partition (%d) not found in partition map\n",
1179 ret = udf_fill_partdesc_info(sb, p, i);
1182 * Now rescan for VIRTUAL partitions when TYPE1 partitions are
1186 for (i = 0; i < sbi->s_partitions; i++) {
1187 map = &sbi->s_partmaps[i];
1189 if (map->s_partition_num == partitionNumber &&
1190 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1191 map->s_partition_type == UDF_VIRTUAL_MAP20))
1195 if (i >= sbi->s_partitions)
1198 ret = udf_fill_partdesc_info(sb, p, i);
1202 ret = udf_load_vat(sb, i, type1_idx);
1204 /* In case loading failed, we handle cleanup in udf_fill_super */
1209 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1210 kernel_lb_addr *fileset)
1212 struct logicalVolDesc *lvd;
1215 struct udf_sb_info *sbi = UDF_SB(sb);
1216 struct genericPartitionMap *gpm;
1218 struct buffer_head *bh;
1221 bh = udf_read_tagged(sb, block, block, &ident);
1224 BUG_ON(ident != TAG_IDENT_LVD);
1225 lvd = (struct logicalVolDesc *)bh->b_data;
1227 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1233 for (i = 0, offset = 0;
1234 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1235 i++, offset += gpm->partitionMapLength) {
1236 struct udf_part_map *map = &sbi->s_partmaps[i];
1237 gpm = (struct genericPartitionMap *)
1238 &(lvd->partitionMaps[offset]);
1239 type = gpm->partitionMapType;
1241 struct genericPartitionMap1 *gpm1 =
1242 (struct genericPartitionMap1 *)gpm;
1243 map->s_partition_type = UDF_TYPE1_MAP15;
1244 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1245 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1246 map->s_partition_func = NULL;
1247 } else if (type == 2) {
1248 struct udfPartitionMap2 *upm2 =
1249 (struct udfPartitionMap2 *)gpm;
1250 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1251 strlen(UDF_ID_VIRTUAL))) {
1253 le16_to_cpu(((__le16 *)upm2->partIdent.
1255 if (suf == 0x0150) {
1256 map->s_partition_type =
1258 map->s_partition_func =
1259 udf_get_pblock_virt15;
1260 } else if (suf == 0x0200) {
1261 map->s_partition_type =
1263 map->s_partition_func =
1264 udf_get_pblock_virt20;
1266 } else if (!strncmp(upm2->partIdent.ident,
1268 strlen(UDF_ID_SPARABLE))) {
1270 struct sparingTable *st;
1271 struct sparablePartitionMap *spm =
1272 (struct sparablePartitionMap *)gpm;
1274 map->s_partition_type = UDF_SPARABLE_MAP15;
1275 map->s_type_specific.s_sparing.s_packet_len =
1276 le16_to_cpu(spm->packetLength);
1277 for (j = 0; j < spm->numSparingTables; j++) {
1278 struct buffer_head *bh2;
1281 spm->locSparingTable[j]);
1282 bh2 = udf_read_tagged(sb, loc, loc,
1284 map->s_type_specific.s_sparing.
1285 s_spar_map[j] = bh2;
1290 st = (struct sparingTable *)bh2->b_data;
1291 if (ident != 0 || strncmp(
1292 st->sparingIdent.ident,
1294 strlen(UDF_ID_SPARING))) {
1296 map->s_type_specific.s_sparing.
1297 s_spar_map[j] = NULL;
1300 map->s_partition_func = udf_get_pblock_spar15;
1302 udf_debug("Unknown ident: %s\n",
1303 upm2->partIdent.ident);
1306 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1307 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1309 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1310 i, map->s_partition_num, type,
1311 map->s_volumeseqnum);
1315 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1317 *fileset = lelb_to_cpu(la->extLocation);
1318 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1319 "partition=%d\n", fileset->logicalBlockNum,
1320 fileset->partitionReferenceNum);
1322 if (lvd->integritySeqExt.extLength)
1323 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1331 * udf_load_logicalvolint
1334 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1336 struct buffer_head *bh = NULL;
1338 struct udf_sb_info *sbi = UDF_SB(sb);
1339 struct logicalVolIntegrityDesc *lvid;
1341 while (loc.extLength > 0 &&
1342 (bh = udf_read_tagged(sb, loc.extLocation,
1343 loc.extLocation, &ident)) &&
1344 ident == TAG_IDENT_LVID) {
1345 sbi->s_lvid_bh = bh;
1346 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1348 if (lvid->nextIntegrityExt.extLength)
1349 udf_load_logicalvolint(sb,
1350 leea_to_cpu(lvid->nextIntegrityExt));
1352 if (sbi->s_lvid_bh != bh)
1354 loc.extLength -= sb->s_blocksize;
1357 if (sbi->s_lvid_bh != bh)
1362 * udf_process_sequence
1365 * Process a main/reserve volume descriptor sequence.
1368 * sb Pointer to _locked_ superblock.
1369 * block First block of first extent of the sequence.
1370 * lastblock Lastblock of first extent of the sequence.
1373 * July 1, 1997 - Andrew E. Mileski
1374 * Written, tested, and released.
1376 static noinline int udf_process_sequence(struct super_block *sb, long block,
1377 long lastblock, kernel_lb_addr *fileset)
1379 struct buffer_head *bh = NULL;
1380 struct udf_vds_record vds[VDS_POS_LENGTH];
1381 struct udf_vds_record *curr;
1382 struct generic_desc *gd;
1383 struct volDescPtr *vdp;
1387 long next_s = 0, next_e = 0;
1389 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1392 * Read the main descriptor sequence and find which descriptors
1395 for (; (!done && block <= lastblock); block++) {
1397 bh = udf_read_tagged(sb, block, block, &ident);
1399 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1400 "sequence is corrupted or we could not read "
1401 "it.\n", (unsigned long long)block);
1405 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1406 gd = (struct generic_desc *)bh->b_data;
1407 vdsn = le32_to_cpu(gd->volDescSeqNum);
1409 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1410 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1411 if (vdsn >= curr->volDescSeqNum) {
1412 curr->volDescSeqNum = vdsn;
1413 curr->block = block;
1416 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1417 curr = &vds[VDS_POS_VOL_DESC_PTR];
1418 if (vdsn >= curr->volDescSeqNum) {
1419 curr->volDescSeqNum = vdsn;
1420 curr->block = block;
1422 vdp = (struct volDescPtr *)bh->b_data;
1423 next_s = le32_to_cpu(
1424 vdp->nextVolDescSeqExt.extLocation);
1425 next_e = le32_to_cpu(
1426 vdp->nextVolDescSeqExt.extLength);
1427 next_e = next_e >> sb->s_blocksize_bits;
1431 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1432 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1433 if (vdsn >= curr->volDescSeqNum) {
1434 curr->volDescSeqNum = vdsn;
1435 curr->block = block;
1438 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1439 curr = &vds[VDS_POS_PARTITION_DESC];
1441 curr->block = block;
1443 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1444 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1445 if (vdsn >= curr->volDescSeqNum) {
1446 curr->volDescSeqNum = vdsn;
1447 curr->block = block;
1450 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1451 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1452 if (vdsn >= curr->volDescSeqNum) {
1453 curr->volDescSeqNum = vdsn;
1454 curr->block = block;
1457 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1458 vds[VDS_POS_TERMINATING_DESC].block = block;
1462 next_s = next_e = 0;
1470 * Now read interesting descriptors again and process them
1471 * in a suitable order
1473 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1474 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1477 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1480 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1481 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1484 if (vds[VDS_POS_PARTITION_DESC].block) {
1486 * We rescan the whole descriptor sequence to find
1487 * partition descriptor blocks and process them.
1489 for (block = vds[VDS_POS_PARTITION_DESC].block;
1490 block < vds[VDS_POS_TERMINATING_DESC].block;
1492 if (udf_load_partdesc(sb, block))
1502 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1505 struct udf_sb_info *sbi = UDF_SB(sb);
1508 udf_debug("Validity check skipped because of novrs option\n");
1511 /* Check that it is NSR02 compliant */
1512 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1513 block = udf_vrs(sb, silent);
1515 udf_debug("Failed to read byte 32768. Assuming open "
1516 "disc. Skipping validity check\n");
1517 if (block && !sbi->s_last_block)
1518 sbi->s_last_block = udf_get_last_block(sb);
1522 static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
1524 struct anchorVolDescPtr *anchor;
1526 struct buffer_head *bh;
1527 long main_s, main_e, reserve_s, reserve_e;
1529 struct udf_sb_info *sbi;
1535 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1536 if (!sbi->s_anchor[i])
1539 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1544 anchor = (struct anchorVolDescPtr *)bh->b_data;
1546 /* Locate the main sequence */
1547 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1548 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1549 main_e = main_e >> sb->s_blocksize_bits;
1552 /* Locate the reserve sequence */
1553 reserve_s = le32_to_cpu(
1554 anchor->reserveVolDescSeqExt.extLocation);
1555 reserve_e = le32_to_cpu(
1556 anchor->reserveVolDescSeqExt.extLength);
1557 reserve_e = reserve_e >> sb->s_blocksize_bits;
1558 reserve_e += reserve_s;
1562 /* Process the main & reserve sequences */
1563 /* responsible for finding the PartitionDesc(s) */
1564 if (!(udf_process_sequence(sb, main_s, main_e,
1566 udf_process_sequence(sb, reserve_s, reserve_e,
1571 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1572 udf_debug("No Anchor block found\n");
1575 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1580 static void udf_open_lvid(struct super_block *sb)
1582 struct udf_sb_info *sbi = UDF_SB(sb);
1583 struct buffer_head *bh = sbi->s_lvid_bh;
1584 struct logicalVolIntegrityDesc *lvid;
1585 struct logicalVolIntegrityDescImpUse *lvidiu;
1589 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1590 lvidiu = udf_sb_lvidiu(sbi);
1592 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1593 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1594 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1596 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1598 lvid->descTag.descCRC = cpu_to_le16(
1599 udf_crc((char *)lvid + sizeof(tag),
1600 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1602 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1603 mark_buffer_dirty(bh);
1606 static void udf_close_lvid(struct super_block *sb)
1608 struct udf_sb_info *sbi = UDF_SB(sb);
1609 struct buffer_head *bh = sbi->s_lvid_bh;
1610 struct logicalVolIntegrityDesc *lvid;
1611 struct logicalVolIntegrityDescImpUse *lvidiu;
1616 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1618 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1621 lvidiu = udf_sb_lvidiu(sbi);
1622 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1623 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1624 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1625 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1626 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1627 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1628 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1629 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1630 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1631 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1633 lvid->descTag.descCRC = cpu_to_le16(
1634 udf_crc((char *)lvid + sizeof(tag),
1635 le16_to_cpu(lvid->descTag.descCRCLength),
1638 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1639 mark_buffer_dirty(bh);
1642 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1645 int nr_groups = bitmap->s_nr_groups;
1646 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1649 for (i = 0; i < nr_groups; i++)
1650 if (bitmap->s_block_bitmap[i])
1651 brelse(bitmap->s_block_bitmap[i]);
1653 if (size <= PAGE_SIZE)
1659 static void udf_free_partition(struct udf_part_map *map)
1663 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1664 iput(map->s_uspace.s_table);
1665 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1666 iput(map->s_fspace.s_table);
1667 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1668 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1669 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1670 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1671 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1672 for (i = 0; i < 4; i++)
1673 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1676 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1679 struct inode *inode = NULL;
1680 struct udf_options uopt;
1681 kernel_lb_addr rootdir, fileset;
1682 struct udf_sb_info *sbi;
1684 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1689 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1693 sb->s_fs_info = sbi;
1695 mutex_init(&sbi->s_alloc_mutex);
1697 if (!udf_parse_options((char *)options, &uopt, false))
1700 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1701 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1702 udf_error(sb, "udf_read_super",
1703 "utf8 cannot be combined with iocharset\n");
1706 #ifdef CONFIG_UDF_NLS
1707 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1708 uopt.nls_map = load_nls_default();
1710 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1712 udf_debug("Using default NLS map\n");
1715 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1716 uopt.flags |= (1 << UDF_FLAG_UTF8);
1718 fileset.logicalBlockNum = 0xFFFFFFFF;
1719 fileset.partitionReferenceNum = 0xFFFF;
1721 sbi->s_flags = uopt.flags;
1722 sbi->s_uid = uopt.uid;
1723 sbi->s_gid = uopt.gid;
1724 sbi->s_umask = uopt.umask;
1725 sbi->s_nls_map = uopt.nls_map;
1727 /* Set the block size for all transfers */
1728 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1729 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1730 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1734 if (uopt.session == 0xFFFFFFFF)
1735 sbi->s_session = udf_get_last_session(sb);
1737 sbi->s_session = uopt.session;
1739 udf_debug("Multi-session=%d\n", sbi->s_session);
1741 sbi->s_last_block = uopt.lastblock;
1742 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1743 sbi->s_anchor[2] = uopt.anchor;
1745 if (udf_check_valid(sb, uopt.novrs, silent)) {
1746 /* read volume recognition sequences */
1747 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1751 udf_find_anchor(sb);
1753 /* Fill in the rest of the superblock */
1754 sb->s_op = &udf_sb_ops;
1757 sb->s_magic = UDF_SUPER_MAGIC;
1758 sb->s_time_gran = 1000;
1760 if (udf_load_sequence(sb, &fileset)) {
1761 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1765 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1767 if (sbi->s_lvid_bh) {
1768 struct logicalVolIntegrityDescImpUse *lvidiu =
1770 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1771 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1772 /* uint16_t maxUDFWriteRev =
1773 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1775 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1776 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1778 le16_to_cpu(lvidiu->minUDFReadRev),
1779 UDF_MAX_READ_VERSION);
1781 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1782 sb->s_flags |= MS_RDONLY;
1784 sbi->s_udfrev = minUDFWriteRev;
1786 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1787 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1788 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1789 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1792 if (!sbi->s_partitions) {
1793 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1797 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1798 UDF_PART_FLAG_READ_ONLY) {
1799 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1800 "forcing readonly mount\n");
1801 sb->s_flags |= MS_RDONLY;
1804 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1805 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1811 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
1812 udf_info("UDF: Mounting volume '%s', "
1813 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1814 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1815 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
1817 if (!(sb->s_flags & MS_RDONLY))
1820 /* Assign the root inode */
1821 /* assign inodes by physical block number */
1822 /* perhaps it's not extensible enough, but for now ... */
1823 inode = udf_iget(sb, rootdir);
1825 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1827 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1831 /* Allocate a dentry for the root inode */
1832 sb->s_root = d_alloc_root(inode);
1834 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1838 sb->s_maxbytes = MAX_LFS_FILESIZE;
1842 if (sbi->s_vat_inode)
1843 iput(sbi->s_vat_inode);
1844 if (sbi->s_partitions)
1845 for (i = 0; i < sbi->s_partitions; i++)
1846 udf_free_partition(&sbi->s_partmaps[i]);
1847 #ifdef CONFIG_UDF_NLS
1848 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1849 unload_nls(sbi->s_nls_map);
1851 if (!(sb->s_flags & MS_RDONLY))
1853 brelse(sbi->s_lvid_bh);
1855 kfree(sbi->s_partmaps);
1857 sb->s_fs_info = NULL;
1862 static void udf_error(struct super_block *sb, const char *function,
1863 const char *fmt, ...)
1867 if (!(sb->s_flags & MS_RDONLY)) {
1871 va_start(args, fmt);
1872 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1874 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1875 sb->s_id, function, error_buf);
1878 void udf_warning(struct super_block *sb, const char *function,
1879 const char *fmt, ...)
1883 va_start(args, fmt);
1884 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1886 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1887 sb->s_id, function, error_buf);
1890 static void udf_put_super(struct super_block *sb)
1893 struct udf_sb_info *sbi;
1896 if (sbi->s_vat_inode)
1897 iput(sbi->s_vat_inode);
1898 if (sbi->s_partitions)
1899 for (i = 0; i < sbi->s_partitions; i++)
1900 udf_free_partition(&sbi->s_partmaps[i]);
1901 #ifdef CONFIG_UDF_NLS
1902 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1903 unload_nls(sbi->s_nls_map);
1905 if (!(sb->s_flags & MS_RDONLY))
1907 brelse(sbi->s_lvid_bh);
1908 kfree(sbi->s_partmaps);
1909 kfree(sb->s_fs_info);
1910 sb->s_fs_info = NULL;
1913 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1915 struct super_block *sb = dentry->d_sb;
1916 struct udf_sb_info *sbi = UDF_SB(sb);
1917 struct logicalVolIntegrityDescImpUse *lvidiu;
1919 if (sbi->s_lvid_bh != NULL)
1920 lvidiu = udf_sb_lvidiu(sbi);
1924 buf->f_type = UDF_SUPER_MAGIC;
1925 buf->f_bsize = sb->s_blocksize;
1926 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1927 buf->f_bfree = udf_count_free(sb);
1928 buf->f_bavail = buf->f_bfree;
1929 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1930 le32_to_cpu(lvidiu->numDirs)) : 0)
1932 buf->f_ffree = buf->f_bfree;
1933 /* __kernel_fsid_t f_fsid */
1934 buf->f_namelen = UDF_NAME_LEN - 2;
1939 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1940 struct udf_bitmap *bitmap)
1942 struct buffer_head *bh = NULL;
1943 unsigned int accum = 0;
1945 int block = 0, newblock;
1950 struct spaceBitmapDesc *bm;
1954 loc.logicalBlockNum = bitmap->s_extPosition;
1955 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1956 bh = udf_read_ptagged(sb, loc, 0, &ident);
1959 printk(KERN_ERR "udf: udf_count_free failed\n");
1961 } else if (ident != TAG_IDENT_SBD) {
1963 printk(KERN_ERR "udf: udf_count_free failed\n");
1967 bm = (struct spaceBitmapDesc *)bh->b_data;
1968 bytes = le32_to_cpu(bm->numOfBytes);
1969 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1970 ptr = (uint8_t *)bh->b_data;
1973 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
1974 accum += bitmap_weight((const unsigned long *)(ptr + index),
1979 newblock = udf_get_lb_pblock(sb, loc, ++block);
1980 bh = udf_tread(sb, newblock);
1982 udf_debug("read failed\n");
1986 ptr = (uint8_t *)bh->b_data;
1997 static unsigned int udf_count_free_table(struct super_block *sb,
1998 struct inode *table)
2000 unsigned int accum = 0;
2002 kernel_lb_addr eloc;
2004 struct extent_position epos;
2008 epos.block = UDF_I(table)->i_location;
2009 epos.offset = sizeof(struct unallocSpaceEntry);
2012 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2013 accum += (elen >> table->i_sb->s_blocksize_bits);
2022 static unsigned int udf_count_free(struct super_block *sb)
2024 unsigned int accum = 0;
2025 struct udf_sb_info *sbi;
2026 struct udf_part_map *map;
2029 if (sbi->s_lvid_bh) {
2030 struct logicalVolIntegrityDesc *lvid =
2031 (struct logicalVolIntegrityDesc *)
2032 sbi->s_lvid_bh->b_data;
2033 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2034 accum = le32_to_cpu(
2035 lvid->freeSpaceTable[sbi->s_partition]);
2036 if (accum == 0xFFFFFFFF)
2044 map = &sbi->s_partmaps[sbi->s_partition];
2045 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2046 accum += udf_count_free_bitmap(sb,
2047 map->s_uspace.s_bitmap);
2049 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2050 accum += udf_count_free_bitmap(sb,
2051 map->s_fspace.s_bitmap);
2056 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2057 accum += udf_count_free_table(sb,
2058 map->s_uspace.s_table);
2060 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2061 accum += udf_count_free_table(sb,
2062 map->s_fspace.s_table);