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 int udf_load_partition(struct super_block *, kernel_lb_addr *);
88 static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
90 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
91 static void udf_find_anchor(struct super_block *);
92 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
94 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
95 static void udf_load_fileset(struct super_block *, struct buffer_head *,
97 static int udf_load_partdesc(struct super_block *, struct buffer_head *);
98 static void udf_open_lvid(struct super_block *);
99 static void udf_close_lvid(struct super_block *);
100 static unsigned int udf_count_free(struct super_block *);
101 static int udf_statfs(struct dentry *, struct kstatfs *);
102 static int udf_show_options(struct seq_file *, struct vfsmount *);
103 static void udf_error(struct super_block *sb, const char *function,
104 const char *fmt, ...);
106 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
108 struct logicalVolIntegrityDesc *lvid =
109 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
110 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
111 __u32 offset = number_of_partitions * 2 *
112 sizeof(uint32_t)/sizeof(uint8_t);
113 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
116 /* UDF filesystem type */
117 static int udf_get_sb(struct file_system_type *fs_type,
118 int flags, const char *dev_name, void *data,
119 struct vfsmount *mnt)
121 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
124 static struct file_system_type udf_fstype = {
125 .owner = THIS_MODULE,
127 .get_sb = udf_get_sb,
128 .kill_sb = kill_block_super,
129 .fs_flags = FS_REQUIRES_DEV,
132 static struct kmem_cache *udf_inode_cachep;
134 static struct inode *udf_alloc_inode(struct super_block *sb)
136 struct udf_inode_info *ei;
137 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
142 ei->i_lenExtents = 0;
143 ei->i_next_alloc_block = 0;
144 ei->i_next_alloc_goal = 0;
147 return &ei->vfs_inode;
150 static void udf_destroy_inode(struct inode *inode)
152 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
155 static void init_once(struct kmem_cache *cachep, void *foo)
157 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
159 ei->i_ext.i_data = NULL;
160 inode_init_once(&ei->vfs_inode);
163 static int init_inodecache(void)
165 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
166 sizeof(struct udf_inode_info),
167 0, (SLAB_RECLAIM_ACCOUNT |
170 if (!udf_inode_cachep)
175 static void destroy_inodecache(void)
177 kmem_cache_destroy(udf_inode_cachep);
180 /* Superblock operations */
181 static const struct super_operations udf_sb_ops = {
182 .alloc_inode = udf_alloc_inode,
183 .destroy_inode = udf_destroy_inode,
184 .write_inode = udf_write_inode,
185 .delete_inode = udf_delete_inode,
186 .clear_inode = udf_clear_inode,
187 .put_super = udf_put_super,
188 .write_super = udf_write_super,
189 .statfs = udf_statfs,
190 .remount_fs = udf_remount_fs,
191 .show_options = udf_show_options,
196 unsigned int blocksize;
197 unsigned int session;
198 unsigned int lastblock;
201 unsigned short partition;
202 unsigned int fileset;
203 unsigned int rootdir;
208 struct nls_table *nls_map;
211 static int __init init_udf_fs(void)
215 err = init_inodecache();
218 err = register_filesystem(&udf_fstype);
225 destroy_inodecache();
231 static void __exit exit_udf_fs(void)
233 unregister_filesystem(&udf_fstype);
234 destroy_inodecache();
237 module_init(init_udf_fs)
238 module_exit(exit_udf_fs)
240 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
242 struct udf_sb_info *sbi = UDF_SB(sb);
244 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
246 if (!sbi->s_partmaps) {
247 udf_error(sb, __FUNCTION__,
248 "Unable to allocate space for %d partition maps",
250 sbi->s_partitions = 0;
254 sbi->s_partitions = count;
258 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
260 struct super_block *sb = mnt->mnt_sb;
261 struct udf_sb_info *sbi = UDF_SB(sb);
263 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
264 seq_puts(seq, ",nostrict");
265 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
266 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
268 seq_puts(seq, ",unhide");
269 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
270 seq_puts(seq, ",undelete");
271 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
272 seq_puts(seq, ",noadinicb");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
274 seq_puts(seq, ",shortad");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
276 seq_puts(seq, ",uid=forget");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
278 seq_puts(seq, ",uid=ignore");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
280 seq_puts(seq, ",gid=forget");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
282 seq_puts(seq, ",gid=ignore");
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
284 seq_printf(seq, ",uid=%u", sbi->s_uid);
285 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
286 seq_printf(seq, ",gid=%u", sbi->s_gid);
287 if (sbi->s_umask != 0)
288 seq_printf(seq, ",umask=%o", sbi->s_umask);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
290 seq_printf(seq, ",session=%u", sbi->s_session);
291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
292 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
294 * s_anchor[2] could be zeroed out in case there is no anchor
295 * in the specified block, but then the "anchor=N" option
296 * originally given by the user wasn't effective, so it's OK
297 * if we don't show it.
299 if (sbi->s_anchor[2] != 0)
300 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
302 * volume, partition, fileset and rootdir seem to be ignored
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
306 seq_puts(seq, ",utf8");
307 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
308 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
317 * Parse mount options.
320 * The following mount options are supported:
322 * gid= Set the default group.
323 * umask= Set the default umask.
324 * uid= Set the default user.
325 * bs= Set the block size.
326 * unhide Show otherwise hidden files.
327 * undelete Show deleted files in lists.
328 * adinicb Embed data in the inode (default)
329 * noadinicb Don't embed data in the inode
330 * shortad Use short ad's
331 * longad Use long ad's (default)
332 * nostrict Unset strict conformance
333 * iocharset= Set the NLS character set
335 * The remaining are for debugging and disaster recovery:
337 * novrs Skip volume sequence recognition
339 * The following expect a offset from 0.
341 * session= Set the CDROM session (default= last session)
342 * anchor= Override standard anchor location. (default= 256)
343 * volume= Override the VolumeDesc location. (unused)
344 * partition= Override the PartitionDesc location. (unused)
345 * lastblock= Set the last block of the filesystem/
347 * The following expect a offset from the partition root.
349 * fileset= Override the fileset block location. (unused)
350 * rootdir= Override the root directory location. (unused)
351 * WARNING: overriding the rootdir to a non-directory may
352 * yield highly unpredictable results.
355 * options Pointer to mount options string.
356 * uopts Pointer to mount options variable.
359 * <return> 1 Mount options parsed okay.
360 * <return> 0 Error parsing mount options.
363 * July 1, 1997 - Andrew E. Mileski
364 * Written, tested, and released.
368 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
369 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
370 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
371 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
372 Opt_rootdir, Opt_utf8, Opt_iocharset,
373 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
376 static match_table_t tokens = {
377 {Opt_novrs, "novrs"},
378 {Opt_nostrict, "nostrict"},
380 {Opt_unhide, "unhide"},
381 {Opt_undelete, "undelete"},
382 {Opt_noadinicb, "noadinicb"},
383 {Opt_adinicb, "adinicb"},
384 {Opt_shortad, "shortad"},
385 {Opt_longad, "longad"},
386 {Opt_uforget, "uid=forget"},
387 {Opt_uignore, "uid=ignore"},
388 {Opt_gforget, "gid=forget"},
389 {Opt_gignore, "gid=ignore"},
392 {Opt_umask, "umask=%o"},
393 {Opt_session, "session=%u"},
394 {Opt_lastblock, "lastblock=%u"},
395 {Opt_anchor, "anchor=%u"},
396 {Opt_volume, "volume=%u"},
397 {Opt_partition, "partition=%u"},
398 {Opt_fileset, "fileset=%u"},
399 {Opt_rootdir, "rootdir=%u"},
401 {Opt_iocharset, "iocharset=%s"},
405 static int udf_parse_options(char *options, struct udf_options *uopt,
412 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
413 uopt->partition = 0xFFFF;
414 uopt->session = 0xFFFFFFFF;
417 uopt->volume = 0xFFFFFFFF;
418 uopt->rootdir = 0xFFFFFFFF;
419 uopt->fileset = 0xFFFFFFFF;
420 uopt->nls_map = NULL;
425 while ((p = strsep(&options, ",")) != NULL) {
426 substring_t args[MAX_OPT_ARGS];
431 token = match_token(p, tokens, args);
436 if (match_int(&args[0], &option))
438 uopt->blocksize = option;
441 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
444 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
447 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
450 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
453 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
456 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
459 if (match_int(args, &option))
462 uopt->flags |= (1 << UDF_FLAG_GID_SET);
465 if (match_int(args, &option))
468 uopt->flags |= (1 << UDF_FLAG_UID_SET);
471 if (match_octal(args, &option))
473 uopt->umask = option;
476 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
479 if (match_int(args, &option))
481 uopt->session = option;
483 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
486 if (match_int(args, &option))
488 uopt->lastblock = option;
490 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
493 if (match_int(args, &option))
495 uopt->anchor = option;
498 if (match_int(args, &option))
500 uopt->volume = option;
503 if (match_int(args, &option))
505 uopt->partition = option;
508 if (match_int(args, &option))
510 uopt->fileset = option;
513 if (match_int(args, &option))
515 uopt->rootdir = option;
518 uopt->flags |= (1 << UDF_FLAG_UTF8);
520 #ifdef CONFIG_UDF_NLS
522 uopt->nls_map = load_nls(args[0].from);
523 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
527 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
530 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
533 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
536 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
539 printk(KERN_ERR "udf: bad mount option \"%s\" "
540 "or missing value\n", p);
547 static void udf_write_super(struct super_block *sb)
551 if (!(sb->s_flags & MS_RDONLY))
558 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
560 struct udf_options uopt;
561 struct udf_sb_info *sbi = UDF_SB(sb);
563 uopt.flags = sbi->s_flags;
564 uopt.uid = sbi->s_uid;
565 uopt.gid = sbi->s_gid;
566 uopt.umask = sbi->s_umask;
568 if (!udf_parse_options(options, &uopt, true))
571 sbi->s_flags = uopt.flags;
572 sbi->s_uid = uopt.uid;
573 sbi->s_gid = uopt.gid;
574 sbi->s_umask = uopt.umask;
576 if (sbi->s_lvid_bh) {
577 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
578 if (write_rev > UDF_MAX_WRITE_VERSION)
582 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
584 if (*flags & MS_RDONLY)
592 static int udf_vrs(struct super_block *sb, int silent)
594 struct volStructDesc *vsd = NULL;
597 struct buffer_head *bh = NULL;
601 struct udf_sb_info *sbi;
603 /* Block size must be a multiple of 512 */
604 if (sb->s_blocksize & 511)
608 if (sb->s_blocksize < sizeof(struct volStructDesc))
609 sectorsize = sizeof(struct volStructDesc);
611 sectorsize = sb->s_blocksize;
613 sector += (sbi->s_session << sb->s_blocksize_bits);
615 udf_debug("Starting at sector %u (%ld byte sectors)\n",
616 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
617 /* Process the sequence (if applicable) */
618 for (; !nsr02 && !nsr03; sector += sectorsize) {
620 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
624 /* Look for ISO descriptors */
625 vsd = (struct volStructDesc *)(bh->b_data +
626 (sector & (sb->s_blocksize - 1)));
628 if (vsd->stdIdent[0] == 0) {
631 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
634 switch (vsd->structType) {
636 udf_debug("ISO9660 Boot Record found\n");
639 udf_debug("ISO9660 Primary Volume Descriptor "
643 udf_debug("ISO9660 Supplementary Volume "
644 "Descriptor found\n");
647 udf_debug("ISO9660 Volume Partition Descriptor "
651 udf_debug("ISO9660 Volume Descriptor Set "
652 "Terminator found\n");
655 udf_debug("ISO9660 VRS (%u) found\n",
659 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
662 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
666 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
669 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
679 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
689 * Find an anchor volume descriptor.
692 * sb Pointer to _locked_ superblock.
693 * lastblock Last block on media.
696 * <return> 1 if not found, 0 if ok
699 * July 1, 1997 - Andrew E. Mileski
700 * Written, tested, and released.
702 static void udf_find_anchor(struct super_block *sb)
705 struct buffer_head *bh = NULL;
709 struct udf_sb_info *sbi;
712 lastblock = sbi->s_last_block;
715 int varlastblock = udf_variable_to_fixed(lastblock);
716 int last[] = { lastblock, lastblock - 2,
717 lastblock - 150, lastblock - 152,
718 varlastblock, varlastblock - 2,
719 varlastblock - 150, varlastblock - 152 };
723 /* Search for an anchor volume descriptor pointer */
725 /* according to spec, anchor is in either:
729 * however, if the disc isn't closed, it could be 512 */
731 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
732 ident = location = 0;
734 bh = sb_bread(sb, last[i]);
736 tag *t = (tag *)bh->b_data;
737 ident = le16_to_cpu(t->tagIdent);
738 location = le32_to_cpu(t->tagLocation);
743 if (ident == TAG_IDENT_AVDP) {
744 if (location == last[i] - sbi->s_session) {
745 lastblock = last[i] - sbi->s_session;
746 sbi->s_anchor[0] = lastblock;
747 sbi->s_anchor[1] = lastblock - 256;
748 } else if (location ==
749 udf_variable_to_fixed(last[i]) -
751 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
753 udf_variable_to_fixed(last[i]) -
755 sbi->s_anchor[0] = lastblock;
756 sbi->s_anchor[1] = lastblock - 256 -
759 udf_debug("Anchor found at block %d, "
760 "location mismatch %d.\n",
763 } else if (ident == TAG_IDENT_FE ||
764 ident == TAG_IDENT_EFE) {
766 sbi->s_anchor[3] = 512;
768 ident = location = 0;
769 if (last[i] >= 256) {
770 bh = sb_bread(sb, last[i] - 256);
772 tag *t = (tag *)bh->b_data;
775 location = le32_to_cpu(
781 if (ident == TAG_IDENT_AVDP &&
782 location == last[i] - 256 -
785 sbi->s_anchor[1] = last[i] - 256;
787 ident = location = 0;
788 if (last[i] >= 312 + sbi->s_session) {
797 location = le32_to_cpu(
803 if (ident == TAG_IDENT_AVDP &&
804 location == udf_variable_to_fixed(last[i]) - 256) {
807 lastblock = udf_variable_to_fixed(last[i]);
808 sbi->s_anchor[1] = lastblock - 256;
816 /* We haven't found the lastblock. check 312 */
817 bh = sb_bread(sb, 312 + sbi->s_session);
819 tag *t = (tag *)bh->b_data;
820 ident = le16_to_cpu(t->tagIdent);
821 location = le32_to_cpu(t->tagLocation);
824 if (ident == TAG_IDENT_AVDP && location == 256)
825 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
829 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
830 if (sbi->s_anchor[i]) {
831 bh = udf_read_tagged(sb, sbi->s_anchor[i],
832 sbi->s_anchor[i], &ident);
834 sbi->s_anchor[i] = 0;
837 if ((ident != TAG_IDENT_AVDP) &&
838 (i || (ident != TAG_IDENT_FE &&
839 ident != TAG_IDENT_EFE)))
840 sbi->s_anchor[i] = 0;
845 sbi->s_last_block = lastblock;
848 static int udf_find_fileset(struct super_block *sb,
849 kernel_lb_addr *fileset,
850 kernel_lb_addr *root)
852 struct buffer_head *bh = NULL;
855 struct udf_sb_info *sbi;
857 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
858 fileset->partitionReferenceNum != 0xFFFF) {
859 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
863 } else if (ident != TAG_IDENT_FSD) {
872 /* Search backwards through the partitions */
873 kernel_lb_addr newfileset;
875 /* --> cvg: FIXME - is it reasonable? */
878 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
879 (newfileset.partitionReferenceNum != 0xFFFF &&
880 fileset->logicalBlockNum == 0xFFFFFFFF &&
881 fileset->partitionReferenceNum == 0xFFFF);
882 newfileset.partitionReferenceNum--) {
883 lastblock = sbi->s_partmaps
884 [newfileset.partitionReferenceNum]
886 newfileset.logicalBlockNum = 0;
889 bh = udf_read_ptagged(sb, newfileset, 0,
892 newfileset.logicalBlockNum++;
899 struct spaceBitmapDesc *sp;
900 sp = (struct spaceBitmapDesc *)
902 newfileset.logicalBlockNum += 1 +
903 ((le32_to_cpu(sp->numOfBytes) +
904 sizeof(struct spaceBitmapDesc)
905 - 1) >> sb->s_blocksize_bits);
910 *fileset = newfileset;
913 newfileset.logicalBlockNum++;
918 } while (newfileset.logicalBlockNum < lastblock &&
919 fileset->logicalBlockNum == 0xFFFFFFFF &&
920 fileset->partitionReferenceNum == 0xFFFF);
924 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
925 fileset->partitionReferenceNum != 0xFFFF) && bh) {
926 udf_debug("Fileset at block=%d, partition=%d\n",
927 fileset->logicalBlockNum,
928 fileset->partitionReferenceNum);
930 sbi->s_partition = fileset->partitionReferenceNum;
931 udf_load_fileset(sb, bh, root);
938 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
940 struct primaryVolDesc *pvoldesc;
946 pvoldesc = (struct primaryVolDesc *)bh->b_data;
948 if (udf_stamp_to_time(&recording, &recording_usec,
949 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
951 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
952 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
954 recording, recording_usec,
955 ts.year, ts.month, ts.day, ts.hour,
956 ts.minute, ts.typeAndTimezone);
957 UDF_SB(sb)->s_record_time.tv_sec = recording;
958 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
961 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
962 if (udf_CS0toUTF8(&outstr, &instr)) {
963 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
964 outstr.u_len > 31 ? 31 : outstr.u_len);
965 udf_debug("volIdent[] = '%s'\n",
966 UDF_SB(sb)->s_volume_ident);
969 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
970 if (udf_CS0toUTF8(&outstr, &instr))
971 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
974 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
975 kernel_lb_addr *root)
977 struct fileSetDesc *fset;
979 fset = (struct fileSetDesc *)bh->b_data;
981 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
983 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
985 udf_debug("Rootdir at block=%d, partition=%d\n",
986 root->logicalBlockNum, root->partitionReferenceNum);
989 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
991 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
992 return DIV_ROUND_UP(map->s_partition_len +
993 (sizeof(struct spaceBitmapDesc) << 3),
994 sb->s_blocksize * 8);
997 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
999 struct udf_bitmap *bitmap;
1003 nr_groups = udf_compute_nr_groups(sb, index);
1004 size = sizeof(struct udf_bitmap) +
1005 (sizeof(struct buffer_head *) * nr_groups);
1007 if (size <= PAGE_SIZE)
1008 bitmap = kmalloc(size, GFP_KERNEL);
1010 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1012 if (bitmap == NULL) {
1013 udf_error(sb, __FUNCTION__,
1014 "Unable to allocate space for bitmap "
1015 "and %d buffer_head pointers", nr_groups);
1019 memset(bitmap, 0x00, size);
1020 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1021 bitmap->s_nr_groups = nr_groups;
1025 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
1027 struct partitionDesc *p;
1029 struct udf_part_map *map;
1030 struct udf_sb_info *sbi;
1032 p = (struct partitionDesc *)bh->b_data;
1035 for (i = 0; i < sbi->s_partitions; i++) {
1036 map = &sbi->s_partmaps[i];
1037 udf_debug("Searching map: (%d == %d)\n",
1038 map->s_partition_num,
1039 le16_to_cpu(p->partitionNumber));
1040 if (map->s_partition_num ==
1041 le16_to_cpu(p->partitionNumber)) {
1042 map->s_partition_len =
1043 le32_to_cpu(p->partitionLength); /* blocks */
1044 map->s_partition_root =
1045 le32_to_cpu(p->partitionStartingLocation);
1046 if (p->accessType ==
1047 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1048 map->s_partition_flags |=
1049 UDF_PART_FLAG_READ_ONLY;
1050 if (p->accessType ==
1051 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1052 map->s_partition_flags |=
1053 UDF_PART_FLAG_WRITE_ONCE;
1054 if (p->accessType ==
1055 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1056 map->s_partition_flags |=
1057 UDF_PART_FLAG_REWRITABLE;
1058 if (p->accessType ==
1059 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1060 map->s_partition_flags |=
1061 UDF_PART_FLAG_OVERWRITABLE;
1063 if (!strcmp(p->partitionContents.ident,
1064 PD_PARTITION_CONTENTS_NSR02) ||
1065 !strcmp(p->partitionContents.ident,
1066 PD_PARTITION_CONTENTS_NSR03)) {
1067 struct partitionHeaderDesc *phd;
1069 phd = (struct partitionHeaderDesc *)
1070 (p->partitionContentsUse);
1071 if (phd->unallocSpaceTable.extLength) {
1072 kernel_lb_addr loc = {
1073 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1074 .partitionReferenceNum = i,
1077 map->s_uspace.s_table =
1079 if (!map->s_uspace.s_table) {
1080 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
1083 map->s_partition_flags |=
1084 UDF_PART_FLAG_UNALLOC_TABLE;
1085 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1086 i, map->s_uspace.s_table->i_ino);
1088 if (phd->unallocSpaceBitmap.extLength) {
1089 struct udf_bitmap *bitmap =
1090 udf_sb_alloc_bitmap(sb, i);
1091 map->s_uspace.s_bitmap = bitmap;
1092 if (bitmap != NULL) {
1093 bitmap->s_extLength =
1094 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
1095 bitmap->s_extPosition =
1096 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
1097 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1098 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1099 i, bitmap->s_extPosition);
1102 if (phd->partitionIntegrityTable.extLength)
1103 udf_debug("partitionIntegrityTable (part %d)\n", i);
1104 if (phd->freedSpaceTable.extLength) {
1105 kernel_lb_addr loc = {
1106 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1107 .partitionReferenceNum = i,
1110 map->s_fspace.s_table =
1112 if (!map->s_fspace.s_table) {
1113 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
1116 map->s_partition_flags |=
1117 UDF_PART_FLAG_FREED_TABLE;
1118 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1119 i, map->s_fspace.s_table->i_ino);
1121 if (phd->freedSpaceBitmap.extLength) {
1122 struct udf_bitmap *bitmap =
1123 udf_sb_alloc_bitmap(sb, i);
1124 map->s_fspace.s_bitmap = bitmap;
1125 if (bitmap != NULL) {
1126 bitmap->s_extLength =
1127 le32_to_cpu(phd->freedSpaceBitmap.extLength);
1128 bitmap->s_extPosition =
1129 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
1130 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1131 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1132 i, bitmap->s_extPosition);
1139 if (i == sbi->s_partitions)
1140 udf_debug("Partition (%d) not found in partition map\n",
1141 le16_to_cpu(p->partitionNumber));
1143 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1144 "block length %d\n",
1145 le16_to_cpu(p->partitionNumber), i,
1146 map->s_partition_type,
1147 map->s_partition_root,
1148 map->s_partition_len);
1152 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1153 kernel_lb_addr *fileset)
1155 struct logicalVolDesc *lvd;
1158 struct udf_sb_info *sbi = UDF_SB(sb);
1159 struct genericPartitionMap *gpm;
1161 lvd = (struct logicalVolDesc *)bh->b_data;
1163 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1167 for (i = 0, offset = 0;
1168 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1169 i++, offset += gpm->partitionMapLength) {
1170 struct udf_part_map *map = &sbi->s_partmaps[i];
1171 gpm = (struct genericPartitionMap *)
1172 &(lvd->partitionMaps[offset]);
1173 type = gpm->partitionMapType;
1175 struct genericPartitionMap1 *gpm1 =
1176 (struct genericPartitionMap1 *)gpm;
1177 map->s_partition_type = UDF_TYPE1_MAP15;
1178 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1179 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1180 map->s_partition_func = NULL;
1181 } else if (type == 2) {
1182 struct udfPartitionMap2 *upm2 =
1183 (struct udfPartitionMap2 *)gpm;
1184 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1185 strlen(UDF_ID_VIRTUAL))) {
1187 le16_to_cpu(((__le16 *)upm2->partIdent.
1189 if (suf == 0x0150) {
1190 map->s_partition_type =
1192 map->s_partition_func =
1193 udf_get_pblock_virt15;
1194 } else if (suf == 0x0200) {
1195 map->s_partition_type =
1197 map->s_partition_func =
1198 udf_get_pblock_virt20;
1200 } else if (!strncmp(upm2->partIdent.ident,
1202 strlen(UDF_ID_SPARABLE))) {
1205 struct sparingTable *st;
1206 struct sparablePartitionMap *spm =
1207 (struct sparablePartitionMap *)gpm;
1209 map->s_partition_type = UDF_SPARABLE_MAP15;
1210 map->s_type_specific.s_sparing.s_packet_len =
1211 le16_to_cpu(spm->packetLength);
1212 for (j = 0; j < spm->numSparingTables; j++) {
1213 struct buffer_head *bh2;
1216 spm->locSparingTable[j]);
1217 bh2 = udf_read_tagged(sb, loc, loc,
1219 map->s_type_specific.s_sparing.
1220 s_spar_map[j] = bh2;
1223 st = (struct sparingTable *)
1225 if (ident != 0 || strncmp(
1226 st->sparingIdent.ident,
1228 strlen(UDF_ID_SPARING))) {
1230 map->s_type_specific.
1237 map->s_partition_func = udf_get_pblock_spar15;
1239 udf_debug("Unknown ident: %s\n",
1240 upm2->partIdent.ident);
1243 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1244 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1246 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1247 i, map->s_partition_num, type,
1248 map->s_volumeseqnum);
1252 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1254 *fileset = lelb_to_cpu(la->extLocation);
1255 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1256 "partition=%d\n", fileset->logicalBlockNum,
1257 fileset->partitionReferenceNum);
1259 if (lvd->integritySeqExt.extLength)
1260 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1266 * udf_load_logicalvolint
1269 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1271 struct buffer_head *bh = NULL;
1273 struct udf_sb_info *sbi = UDF_SB(sb);
1274 struct logicalVolIntegrityDesc *lvid;
1276 while (loc.extLength > 0 &&
1277 (bh = udf_read_tagged(sb, loc.extLocation,
1278 loc.extLocation, &ident)) &&
1279 ident == TAG_IDENT_LVID) {
1280 sbi->s_lvid_bh = bh;
1281 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1283 if (lvid->nextIntegrityExt.extLength)
1284 udf_load_logicalvolint(sb,
1285 leea_to_cpu(lvid->nextIntegrityExt));
1287 if (sbi->s_lvid_bh != bh)
1289 loc.extLength -= sb->s_blocksize;
1292 if (sbi->s_lvid_bh != bh)
1297 * udf_process_sequence
1300 * Process a main/reserve volume descriptor sequence.
1303 * sb Pointer to _locked_ superblock.
1304 * block First block of first extent of the sequence.
1305 * lastblock Lastblock of first extent of the sequence.
1308 * July 1, 1997 - Andrew E. Mileski
1309 * Written, tested, and released.
1311 static int udf_process_sequence(struct super_block *sb, long block,
1312 long lastblock, kernel_lb_addr *fileset)
1314 struct buffer_head *bh = NULL;
1315 struct udf_vds_record vds[VDS_POS_LENGTH];
1316 struct udf_vds_record *curr;
1317 struct generic_desc *gd;
1318 struct volDescPtr *vdp;
1323 long next_s = 0, next_e = 0;
1325 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1327 /* Read the main descriptor sequence */
1328 for (; (!done && block <= lastblock); block++) {
1330 bh = udf_read_tagged(sb, block, block, &ident);
1334 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1335 gd = (struct generic_desc *)bh->b_data;
1336 vdsn = le32_to_cpu(gd->volDescSeqNum);
1338 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1339 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1340 if (vdsn >= curr->volDescSeqNum) {
1341 curr->volDescSeqNum = vdsn;
1342 curr->block = block;
1345 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1346 curr = &vds[VDS_POS_VOL_DESC_PTR];
1347 if (vdsn >= curr->volDescSeqNum) {
1348 curr->volDescSeqNum = vdsn;
1349 curr->block = block;
1351 vdp = (struct volDescPtr *)bh->b_data;
1352 next_s = le32_to_cpu(
1353 vdp->nextVolDescSeqExt.extLocation);
1354 next_e = le32_to_cpu(
1355 vdp->nextVolDescSeqExt.extLength);
1356 next_e = next_e >> sb->s_blocksize_bits;
1360 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1361 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1362 if (vdsn >= curr->volDescSeqNum) {
1363 curr->volDescSeqNum = vdsn;
1364 curr->block = block;
1367 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1368 curr = &vds[VDS_POS_PARTITION_DESC];
1370 curr->block = block;
1372 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1373 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1374 if (vdsn >= curr->volDescSeqNum) {
1375 curr->volDescSeqNum = vdsn;
1376 curr->block = block;
1379 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1380 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1381 if (vdsn >= curr->volDescSeqNum) {
1382 curr->volDescSeqNum = vdsn;
1383 curr->block = block;
1386 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1387 vds[VDS_POS_TERMINATING_DESC].block = block;
1391 next_s = next_e = 0;
1398 for (i = 0; i < VDS_POS_LENGTH; i++) {
1400 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1403 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1404 udf_load_pvoldesc(sb, bh);
1405 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1406 if (udf_load_logicalvol(sb, bh, fileset)) {
1410 } else if (i == VDS_POS_PARTITION_DESC) {
1411 struct buffer_head *bh2 = NULL;
1412 if (udf_load_partdesc(sb, bh)) {
1416 for (j = vds[i].block + 1;
1417 j < vds[VDS_POS_TERMINATING_DESC].block;
1419 bh2 = udf_read_tagged(sb, j, j, &ident);
1420 gd = (struct generic_desc *)bh2->b_data;
1421 if (ident == TAG_IDENT_PD)
1422 if (udf_load_partdesc(sb,
1441 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1446 udf_debug("Validity check skipped because of novrs option\n");
1449 /* Check that it is NSR02 compliant */
1450 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1452 block = udf_vrs(sb, silent);
1454 struct udf_sb_info *sbi = UDF_SB(sb);
1455 udf_debug("Failed to read byte 32768. Assuming open "
1456 "disc. Skipping validity check\n");
1457 if (!sbi->s_last_block)
1458 sbi->s_last_block = udf_get_last_block(sb);
1465 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1467 struct anchorVolDescPtr *anchor;
1469 struct buffer_head *bh;
1470 long main_s, main_e, reserve_s, reserve_e;
1472 struct udf_sb_info *sbi;
1478 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1479 if (!sbi->s_anchor[i])
1481 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1486 anchor = (struct anchorVolDescPtr *)bh->b_data;
1488 /* Locate the main sequence */
1489 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1490 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1491 main_e = main_e >> sb->s_blocksize_bits;
1494 /* Locate the reserve sequence */
1495 reserve_s = le32_to_cpu(
1496 anchor->reserveVolDescSeqExt.extLocation);
1497 reserve_e = le32_to_cpu(
1498 anchor->reserveVolDescSeqExt.extLength);
1499 reserve_e = reserve_e >> sb->s_blocksize_bits;
1500 reserve_e += reserve_s;
1504 /* Process the main & reserve sequences */
1505 /* responsible for finding the PartitionDesc(s) */
1506 if (!(udf_process_sequence(sb, main_s, main_e,
1508 udf_process_sequence(sb, reserve_s, reserve_e,
1513 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1514 udf_debug("No Anchor block found\n");
1517 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1519 for (i = 0; i < sbi->s_partitions; i++) {
1520 kernel_lb_addr uninitialized_var(ino);
1521 struct udf_part_map *map = &sbi->s_partmaps[i];
1522 switch (map->s_partition_type) {
1523 case UDF_VIRTUAL_MAP15:
1524 case UDF_VIRTUAL_MAP20:
1525 if (!sbi->s_last_block) {
1526 sbi->s_last_block = udf_get_last_block(sb);
1527 udf_find_anchor(sb);
1530 if (!sbi->s_last_block) {
1531 udf_debug("Unable to determine Lastblock (For "
1532 "Virtual Partition)\n");
1536 for (j = 0; j < sbi->s_partitions; j++) {
1537 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1539 map->s_volumeseqnum ==
1540 map2->s_volumeseqnum &&
1541 map->s_partition_num ==
1542 map2->s_partition_num) {
1543 ino.partitionReferenceNum = j;
1544 ino.logicalBlockNum =
1546 map2->s_partition_root;
1551 if (j == sbi->s_partitions)
1554 sbi->s_vat_inode = udf_iget(sb, ino);
1555 if (!sbi->s_vat_inode)
1558 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1559 map->s_type_specific.s_virtual.s_start_offset =
1560 udf_ext0_offset(sbi->s_vat_inode);
1561 map->s_type_specific.s_virtual.s_num_entries =
1562 (sbi->s_vat_inode->i_size - 36) >> 2;
1563 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1565 struct virtualAllocationTable20 *vat20;
1567 pos = udf_block_map(sbi->s_vat_inode, 0);
1568 bh = sb_bread(sb, pos);
1571 vat20 = (struct virtualAllocationTable20 *)
1573 udf_ext0_offset(sbi->s_vat_inode);
1574 map->s_type_specific.s_virtual.s_start_offset =
1575 le16_to_cpu(vat20->lengthHeader) +
1576 udf_ext0_offset(sbi->s_vat_inode);
1577 map->s_type_specific.s_virtual.s_num_entries =
1578 (sbi->s_vat_inode->i_size -
1579 map->s_type_specific.s_virtual.
1580 s_start_offset) >> 2;
1583 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1584 map->s_partition_len =
1585 sbi->s_partmaps[ino.partitionReferenceNum].
1592 static void udf_open_lvid(struct super_block *sb)
1594 struct udf_sb_info *sbi = UDF_SB(sb);
1595 struct buffer_head *bh = sbi->s_lvid_bh;
1597 kernel_timestamp cpu_time;
1598 struct logicalVolIntegrityDesc *lvid =
1599 (struct logicalVolIntegrityDesc *)bh->b_data;
1600 struct logicalVolIntegrityDescImpUse *lvidiu =
1603 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1604 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1605 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1606 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1607 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1609 lvid->descTag.descCRC = cpu_to_le16(
1610 udf_crc((char *)lvid + sizeof(tag),
1611 le16_to_cpu(lvid->descTag.descCRCLength),
1614 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1615 mark_buffer_dirty(bh);
1619 static void udf_close_lvid(struct super_block *sb)
1621 kernel_timestamp cpu_time;
1622 struct udf_sb_info *sbi = UDF_SB(sb);
1623 struct buffer_head *bh = sbi->s_lvid_bh;
1624 struct logicalVolIntegrityDesc *lvid;
1629 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1631 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1632 struct logicalVolIntegrityDescImpUse *lvidiu =
1634 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1635 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1636 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1637 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1638 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1639 lvidiu->maxUDFWriteRev =
1640 cpu_to_le16(UDF_MAX_WRITE_VERSION);
1641 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1642 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1643 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1644 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1645 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1647 lvid->descTag.descCRC = cpu_to_le16(
1648 udf_crc((char *)lvid + sizeof(tag),
1649 le16_to_cpu(lvid->descTag.descCRCLength),
1652 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1653 mark_buffer_dirty(bh);
1657 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1660 int nr_groups = bitmap->s_nr_groups;
1661 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1664 for (i = 0; i < nr_groups; i++)
1665 if (bitmap->s_block_bitmap[i])
1666 brelse(bitmap->s_block_bitmap[i]);
1668 if (size <= PAGE_SIZE)
1674 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1677 struct inode *inode = NULL;
1678 struct udf_options uopt;
1679 kernel_lb_addr rootdir, fileset;
1680 struct udf_sb_info *sbi;
1682 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1687 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1691 sb->s_fs_info = sbi;
1693 mutex_init(&sbi->s_alloc_mutex);
1695 if (!udf_parse_options((char *)options, &uopt, false))
1698 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1699 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1700 udf_error(sb, "udf_read_super",
1701 "utf8 cannot be combined with iocharset\n");
1704 #ifdef CONFIG_UDF_NLS
1705 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1706 uopt.nls_map = load_nls_default();
1708 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1710 udf_debug("Using default NLS map\n");
1713 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1714 uopt.flags |= (1 << UDF_FLAG_UTF8);
1716 fileset.logicalBlockNum = 0xFFFFFFFF;
1717 fileset.partitionReferenceNum = 0xFFFF;
1719 sbi->s_flags = uopt.flags;
1720 sbi->s_uid = uopt.uid;
1721 sbi->s_gid = uopt.gid;
1722 sbi->s_umask = uopt.umask;
1723 sbi->s_nls_map = uopt.nls_map;
1725 /* Set the block size for all transfers */
1726 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1727 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1728 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1732 if (uopt.session == 0xFFFFFFFF)
1733 sbi->s_session = udf_get_last_session(sb);
1735 sbi->s_session = uopt.session;
1737 udf_debug("Multi-session=%d\n", sbi->s_session);
1739 sbi->s_last_block = uopt.lastblock;
1740 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1741 sbi->s_anchor[2] = uopt.anchor;
1742 sbi->s_anchor[3] = 256;
1744 if (udf_check_valid(sb, uopt.novrs, silent)) {
1745 /* read volume recognition sequences */
1746 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1750 udf_find_anchor(sb);
1752 /* Fill in the rest of the superblock */
1753 sb->s_op = &udf_sb_ops;
1756 sb->s_magic = UDF_SUPER_MAGIC;
1757 sb->s_time_gran = 1000;
1759 if (udf_load_partition(sb, &fileset)) {
1760 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1764 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1766 if (sbi->s_lvid_bh) {
1767 struct logicalVolIntegrityDescImpUse *lvidiu =
1769 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1770 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1771 /* uint16_t maxUDFWriteRev =
1772 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1774 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1775 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1777 le16_to_cpu(lvidiu->minUDFReadRev),
1778 UDF_MAX_READ_VERSION);
1780 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1781 sb->s_flags |= MS_RDONLY;
1783 sbi->s_udfrev = minUDFWriteRev;
1785 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1786 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1787 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1788 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1791 if (!sbi->s_partitions) {
1792 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1796 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1797 UDF_PART_FLAG_READ_ONLY) {
1798 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1799 "forcing readonly mount\n");
1800 sb->s_flags |= MS_RDONLY;
1803 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1804 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1809 kernel_timestamp ts;
1810 udf_time_to_stamp(&ts, sbi->s_record_time);
1811 udf_info("UDF: Mounting volume '%s', "
1812 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1813 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1814 ts.hour, ts.minute, ts.typeAndTimezone);
1816 if (!(sb->s_flags & MS_RDONLY))
1819 /* Assign the root inode */
1820 /* assign inodes by physical block number */
1821 /* perhaps it's not extensible enough, but for now ... */
1822 inode = udf_iget(sb, rootdir);
1824 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1826 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1830 /* Allocate a dentry for the root inode */
1831 sb->s_root = d_alloc_root(inode);
1833 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1837 sb->s_maxbytes = MAX_LFS_FILESIZE;
1841 if (sbi->s_vat_inode)
1842 iput(sbi->s_vat_inode);
1843 if (sbi->s_partitions) {
1844 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1845 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1846 iput(map->s_uspace.s_table);
1847 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1848 iput(map->s_fspace.s_table);
1849 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1850 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1851 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1852 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1853 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1854 for (i = 0; i < 4; i++)
1855 brelse(map->s_type_specific.s_sparing.
1858 #ifdef CONFIG_UDF_NLS
1859 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1860 unload_nls(sbi->s_nls_map);
1862 if (!(sb->s_flags & MS_RDONLY))
1864 brelse(sbi->s_lvid_bh);
1866 kfree(sbi->s_partmaps);
1868 sb->s_fs_info = NULL;
1873 static void udf_error(struct super_block *sb, const char *function,
1874 const char *fmt, ...)
1878 if (!(sb->s_flags & MS_RDONLY)) {
1882 va_start(args, fmt);
1883 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1885 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1886 sb->s_id, function, error_buf);
1889 void udf_warning(struct super_block *sb, const char *function,
1890 const char *fmt, ...)
1894 va_start(args, fmt);
1895 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1897 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1898 sb->s_id, function, error_buf);
1901 static void udf_put_super(struct super_block *sb)
1904 struct udf_sb_info *sbi;
1907 if (sbi->s_vat_inode)
1908 iput(sbi->s_vat_inode);
1909 if (sbi->s_partitions) {
1910 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1911 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1912 iput(map->s_uspace.s_table);
1913 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1914 iput(map->s_fspace.s_table);
1915 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1916 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1917 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1918 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1919 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1920 for (i = 0; i < 4; i++)
1921 brelse(map->s_type_specific.s_sparing.
1924 #ifdef CONFIG_UDF_NLS
1925 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1926 unload_nls(sbi->s_nls_map);
1928 if (!(sb->s_flags & MS_RDONLY))
1930 brelse(sbi->s_lvid_bh);
1931 kfree(sbi->s_partmaps);
1932 kfree(sb->s_fs_info);
1933 sb->s_fs_info = NULL;
1936 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1938 struct super_block *sb = dentry->d_sb;
1939 struct udf_sb_info *sbi = UDF_SB(sb);
1940 struct logicalVolIntegrityDescImpUse *lvidiu;
1942 if (sbi->s_lvid_bh != NULL)
1943 lvidiu = udf_sb_lvidiu(sbi);
1947 buf->f_type = UDF_SUPER_MAGIC;
1948 buf->f_bsize = sb->s_blocksize;
1949 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1950 buf->f_bfree = udf_count_free(sb);
1951 buf->f_bavail = buf->f_bfree;
1952 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1953 le32_to_cpu(lvidiu->numDirs)) : 0)
1955 buf->f_ffree = buf->f_bfree;
1956 /* __kernel_fsid_t f_fsid */
1957 buf->f_namelen = UDF_NAME_LEN - 2;
1962 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1963 struct udf_bitmap *bitmap)
1965 struct buffer_head *bh = NULL;
1966 unsigned int accum = 0;
1968 int block = 0, newblock;
1973 struct spaceBitmapDesc *bm;
1977 loc.logicalBlockNum = bitmap->s_extPosition;
1978 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1979 bh = udf_read_ptagged(sb, loc, 0, &ident);
1982 printk(KERN_ERR "udf: udf_count_free failed\n");
1984 } else if (ident != TAG_IDENT_SBD) {
1986 printk(KERN_ERR "udf: udf_count_free failed\n");
1990 bm = (struct spaceBitmapDesc *)bh->b_data;
1991 bytes = le32_to_cpu(bm->numOfBytes);
1992 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1993 ptr = (uint8_t *)bh->b_data;
1996 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
1997 accum += bitmap_weight((const unsigned long *)(ptr + index),
2002 newblock = udf_get_lb_pblock(sb, loc, ++block);
2003 bh = udf_tread(sb, newblock);
2005 udf_debug("read failed\n");
2009 ptr = (uint8_t *)bh->b_data;
2020 static unsigned int udf_count_free_table(struct super_block *sb,
2021 struct inode *table)
2023 unsigned int accum = 0;
2025 kernel_lb_addr eloc;
2027 struct extent_position epos;
2031 epos.block = UDF_I(table)->i_location;
2032 epos.offset = sizeof(struct unallocSpaceEntry);
2035 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2036 accum += (elen >> table->i_sb->s_blocksize_bits);
2045 static unsigned int udf_count_free(struct super_block *sb)
2047 unsigned int accum = 0;
2048 struct udf_sb_info *sbi;
2049 struct udf_part_map *map;
2052 if (sbi->s_lvid_bh) {
2053 struct logicalVolIntegrityDesc *lvid =
2054 (struct logicalVolIntegrityDesc *)
2055 sbi->s_lvid_bh->b_data;
2056 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2057 accum = le32_to_cpu(
2058 lvid->freeSpaceTable[sbi->s_partition]);
2059 if (accum == 0xFFFFFFFF)
2067 map = &sbi->s_partmaps[sbi->s_partition];
2068 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2069 accum += udf_count_free_bitmap(sb,
2070 map->s_uspace.s_bitmap);
2072 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2073 accum += udf_count_free_bitmap(sb,
2074 map->s_fspace.s_bitmap);
2079 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2080 accum += udf_count_free_table(sb,
2081 map->s_uspace.s_table);
2083 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2084 accum += udf_count_free_table(sb,
2085 map->s_fspace.s_table);