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 <asm/byteorder.h>
60 #include <linux/udf_fs.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 *);
104 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
106 struct logicalVolIntegrityDesc *lvid =
107 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
108 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
109 __u32 offset = number_of_partitions * 2 *
110 sizeof(uint32_t)/sizeof(uint8_t);
111 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
114 /* UDF filesystem type */
115 static int udf_get_sb(struct file_system_type *fs_type,
116 int flags, const char *dev_name, void *data,
117 struct vfsmount *mnt)
119 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
122 static struct file_system_type udf_fstype = {
123 .owner = THIS_MODULE,
125 .get_sb = udf_get_sb,
126 .kill_sb = kill_block_super,
127 .fs_flags = FS_REQUIRES_DEV,
130 static struct kmem_cache *udf_inode_cachep;
132 static struct inode *udf_alloc_inode(struct super_block *sb)
134 struct udf_inode_info *ei;
135 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
140 ei->i_lenExtents = 0;
141 ei->i_next_alloc_block = 0;
142 ei->i_next_alloc_goal = 0;
145 return &ei->vfs_inode;
148 static void udf_destroy_inode(struct inode *inode)
150 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
153 static void init_once(struct kmem_cache *cachep, void *foo)
155 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
157 ei->i_ext.i_data = NULL;
158 inode_init_once(&ei->vfs_inode);
161 static int init_inodecache(void)
163 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
164 sizeof(struct udf_inode_info),
165 0, (SLAB_RECLAIM_ACCOUNT |
168 if (!udf_inode_cachep)
173 static void destroy_inodecache(void)
175 kmem_cache_destroy(udf_inode_cachep);
178 /* Superblock operations */
179 static const struct super_operations udf_sb_ops = {
180 .alloc_inode = udf_alloc_inode,
181 .destroy_inode = udf_destroy_inode,
182 .write_inode = udf_write_inode,
183 .delete_inode = udf_delete_inode,
184 .clear_inode = udf_clear_inode,
185 .put_super = udf_put_super,
186 .write_super = udf_write_super,
187 .statfs = udf_statfs,
188 .remount_fs = udf_remount_fs,
189 .show_options = udf_show_options,
194 unsigned int blocksize;
195 unsigned int session;
196 unsigned int lastblock;
199 unsigned short partition;
200 unsigned int fileset;
201 unsigned int rootdir;
206 struct nls_table *nls_map;
209 static int __init init_udf_fs(void)
213 err = init_inodecache();
216 err = register_filesystem(&udf_fstype);
223 destroy_inodecache();
229 static void __exit exit_udf_fs(void)
231 unregister_filesystem(&udf_fstype);
232 destroy_inodecache();
235 module_init(init_udf_fs)
236 module_exit(exit_udf_fs)
238 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
240 struct udf_sb_info *sbi = UDF_SB(sb);
242 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
244 if (!sbi->s_partmaps) {
245 udf_error(sb, __FUNCTION__,
246 "Unable to allocate space for %d partition maps",
248 sbi->s_partitions = 0;
252 sbi->s_partitions = count;
256 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
258 struct super_block *sb = mnt->mnt_sb;
259 struct udf_sb_info *sbi = UDF_SB(sb);
261 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
262 seq_puts(seq, ",nostrict");
263 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
264 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
266 seq_puts(seq, ",unhide");
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
268 seq_puts(seq, ",undelete");
269 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
270 seq_puts(seq, ",noadinicb");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
272 seq_puts(seq, ",shortad");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
274 seq_puts(seq, ",uid=forget");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
276 seq_puts(seq, ",uid=ignore");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
278 seq_puts(seq, ",gid=forget");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
280 seq_puts(seq, ",gid=ignore");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
282 seq_printf(seq, ",uid=%u", sbi->s_uid);
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
284 seq_printf(seq, ",gid=%u", sbi->s_gid);
285 if (sbi->s_umask != 0)
286 seq_printf(seq, ",umask=%o", sbi->s_umask);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
288 seq_printf(seq, ",session=%u", sbi->s_session);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
290 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
292 * s_anchor[2] could be zeroed out in case there is no anchor
293 * in the specified block, but then the "anchor=N" option
294 * originally given by the user wasn't effective, so it's OK
295 * if we don't show it.
297 if (sbi->s_anchor[2] != 0)
298 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
300 * volume, partition, fileset and rootdir seem to be ignored
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
304 seq_puts(seq, ",utf8");
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
306 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
315 * Parse mount options.
318 * The following mount options are supported:
320 * gid= Set the default group.
321 * umask= Set the default umask.
322 * uid= Set the default user.
323 * bs= Set the block size.
324 * unhide Show otherwise hidden files.
325 * undelete Show deleted files in lists.
326 * adinicb Embed data in the inode (default)
327 * noadinicb Don't embed data in the inode
328 * shortad Use short ad's
329 * longad Use long ad's (default)
330 * nostrict Unset strict conformance
331 * iocharset= Set the NLS character set
333 * The remaining are for debugging and disaster recovery:
335 * novrs Skip volume sequence recognition
337 * The following expect a offset from 0.
339 * session= Set the CDROM session (default= last session)
340 * anchor= Override standard anchor location. (default= 256)
341 * volume= Override the VolumeDesc location. (unused)
342 * partition= Override the PartitionDesc location. (unused)
343 * lastblock= Set the last block of the filesystem/
345 * The following expect a offset from the partition root.
347 * fileset= Override the fileset block location. (unused)
348 * rootdir= Override the root directory location. (unused)
349 * WARNING: overriding the rootdir to a non-directory may
350 * yield highly unpredictable results.
353 * options Pointer to mount options string.
354 * uopts Pointer to mount options variable.
357 * <return> 1 Mount options parsed okay.
358 * <return> 0 Error parsing mount options.
361 * July 1, 1997 - Andrew E. Mileski
362 * Written, tested, and released.
366 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
367 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
368 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
369 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
370 Opt_rootdir, Opt_utf8, Opt_iocharset,
371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
374 static match_table_t tokens = {
375 {Opt_novrs, "novrs"},
376 {Opt_nostrict, "nostrict"},
378 {Opt_unhide, "unhide"},
379 {Opt_undelete, "undelete"},
380 {Opt_noadinicb, "noadinicb"},
381 {Opt_adinicb, "adinicb"},
382 {Opt_shortad, "shortad"},
383 {Opt_longad, "longad"},
384 {Opt_uforget, "uid=forget"},
385 {Opt_uignore, "uid=ignore"},
386 {Opt_gforget, "gid=forget"},
387 {Opt_gignore, "gid=ignore"},
390 {Opt_umask, "umask=%o"},
391 {Opt_session, "session=%u"},
392 {Opt_lastblock, "lastblock=%u"},
393 {Opt_anchor, "anchor=%u"},
394 {Opt_volume, "volume=%u"},
395 {Opt_partition, "partition=%u"},
396 {Opt_fileset, "fileset=%u"},
397 {Opt_rootdir, "rootdir=%u"},
399 {Opt_iocharset, "iocharset=%s"},
403 static int udf_parse_options(char *options, struct udf_options *uopt,
410 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
411 uopt->partition = 0xFFFF;
412 uopt->session = 0xFFFFFFFF;
415 uopt->volume = 0xFFFFFFFF;
416 uopt->rootdir = 0xFFFFFFFF;
417 uopt->fileset = 0xFFFFFFFF;
418 uopt->nls_map = NULL;
423 while ((p = strsep(&options, ",")) != NULL) {
424 substring_t args[MAX_OPT_ARGS];
429 token = match_token(p, tokens, args);
434 if (match_int(&args[0], &option))
436 uopt->blocksize = option;
439 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
442 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
445 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
448 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
451 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
454 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
457 if (match_int(args, &option))
460 uopt->flags |= (1 << UDF_FLAG_GID_SET);
463 if (match_int(args, &option))
466 uopt->flags |= (1 << UDF_FLAG_UID_SET);
469 if (match_octal(args, &option))
471 uopt->umask = option;
474 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
477 if (match_int(args, &option))
479 uopt->session = option;
481 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
484 if (match_int(args, &option))
486 uopt->lastblock = option;
488 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
491 if (match_int(args, &option))
493 uopt->anchor = option;
496 if (match_int(args, &option))
498 uopt->volume = option;
501 if (match_int(args, &option))
503 uopt->partition = option;
506 if (match_int(args, &option))
508 uopt->fileset = option;
511 if (match_int(args, &option))
513 uopt->rootdir = option;
516 uopt->flags |= (1 << UDF_FLAG_UTF8);
518 #ifdef CONFIG_UDF_NLS
520 uopt->nls_map = load_nls(args[0].from);
521 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
525 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
528 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
531 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
534 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
537 printk(KERN_ERR "udf: bad mount option \"%s\" "
538 "or missing value\n", p);
545 static void udf_write_super(struct super_block *sb)
549 if (!(sb->s_flags & MS_RDONLY))
556 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
558 struct udf_options uopt;
559 struct udf_sb_info *sbi = UDF_SB(sb);
561 uopt.flags = sbi->s_flags;
562 uopt.uid = sbi->s_uid;
563 uopt.gid = sbi->s_gid;
564 uopt.umask = sbi->s_umask;
566 if (!udf_parse_options(options, &uopt, true))
569 sbi->s_flags = uopt.flags;
570 sbi->s_uid = uopt.uid;
571 sbi->s_gid = uopt.gid;
572 sbi->s_umask = uopt.umask;
574 if (sbi->s_lvid_bh) {
575 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
576 if (write_rev > UDF_MAX_WRITE_VERSION)
580 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
582 if (*flags & MS_RDONLY)
590 static int udf_vrs(struct super_block *sb, int silent)
592 struct volStructDesc *vsd = NULL;
595 struct buffer_head *bh = NULL;
599 struct udf_sb_info *sbi;
601 /* Block size must be a multiple of 512 */
602 if (sb->s_blocksize & 511)
606 if (sb->s_blocksize < sizeof(struct volStructDesc))
607 sectorsize = sizeof(struct volStructDesc);
609 sectorsize = sb->s_blocksize;
611 sector += (sbi->s_session << sb->s_blocksize_bits);
613 udf_debug("Starting at sector %u (%ld byte sectors)\n",
614 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
615 /* Process the sequence (if applicable) */
616 for (; !nsr02 && !nsr03; sector += sectorsize) {
618 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
622 /* Look for ISO descriptors */
623 vsd = (struct volStructDesc *)(bh->b_data +
624 (sector & (sb->s_blocksize - 1)));
626 if (vsd->stdIdent[0] == 0) {
629 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
632 switch (vsd->structType) {
634 udf_debug("ISO9660 Boot Record found\n");
637 udf_debug("ISO9660 Primary Volume Descriptor "
641 udf_debug("ISO9660 Supplementary Volume "
642 "Descriptor found\n");
645 udf_debug("ISO9660 Volume Partition Descriptor "
649 udf_debug("ISO9660 Volume Descriptor Set "
650 "Terminator found\n");
653 udf_debug("ISO9660 VRS (%u) found\n",
657 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
660 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
664 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
667 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
677 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
687 * Find an anchor volume descriptor.
690 * sb Pointer to _locked_ superblock.
691 * lastblock Last block on media.
694 * <return> 1 if not found, 0 if ok
697 * July 1, 1997 - Andrew E. Mileski
698 * Written, tested, and released.
700 static void udf_find_anchor(struct super_block *sb)
703 struct buffer_head *bh = NULL;
707 struct udf_sb_info *sbi;
710 lastblock = sbi->s_last_block;
713 int varlastblock = udf_variable_to_fixed(lastblock);
714 int last[] = { lastblock, lastblock - 2,
715 lastblock - 150, lastblock - 152,
716 varlastblock, varlastblock - 2,
717 varlastblock - 150, varlastblock - 152 };
721 /* Search for an anchor volume descriptor pointer */
723 /* according to spec, anchor is in either:
727 * however, if the disc isn't closed, it could be 512 */
729 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
730 ident = location = 0;
732 bh = sb_bread(sb, last[i]);
734 tag *t = (tag *)bh->b_data;
735 ident = le16_to_cpu(t->tagIdent);
736 location = le32_to_cpu(t->tagLocation);
741 if (ident == TAG_IDENT_AVDP) {
742 if (location == last[i] - sbi->s_session) {
743 lastblock = last[i] - sbi->s_session;
744 sbi->s_anchor[0] = lastblock;
745 sbi->s_anchor[1] = lastblock - 256;
746 } else if (location ==
747 udf_variable_to_fixed(last[i]) -
749 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
751 udf_variable_to_fixed(last[i]) -
753 sbi->s_anchor[0] = lastblock;
754 sbi->s_anchor[1] = lastblock - 256 -
757 udf_debug("Anchor found at block %d, "
758 "location mismatch %d.\n",
761 } else if (ident == TAG_IDENT_FE ||
762 ident == TAG_IDENT_EFE) {
764 sbi->s_anchor[3] = 512;
766 ident = location = 0;
767 if (last[i] >= 256) {
768 bh = sb_bread(sb, last[i] - 256);
770 tag *t = (tag *)bh->b_data;
773 location = le32_to_cpu(
779 if (ident == TAG_IDENT_AVDP &&
780 location == last[i] - 256 -
783 sbi->s_anchor[1] = last[i] - 256;
785 ident = location = 0;
786 if (last[i] >= 312 + sbi->s_session) {
795 location = le32_to_cpu(
801 if (ident == TAG_IDENT_AVDP &&
802 location == udf_variable_to_fixed(last[i]) - 256) {
805 lastblock = udf_variable_to_fixed(last[i]);
806 sbi->s_anchor[1] = lastblock - 256;
814 /* We haven't found the lastblock. check 312 */
815 bh = sb_bread(sb, 312 + sbi->s_session);
817 tag *t = (tag *)bh->b_data;
818 ident = le16_to_cpu(t->tagIdent);
819 location = le32_to_cpu(t->tagLocation);
822 if (ident == TAG_IDENT_AVDP && location == 256)
823 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
827 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
828 if (sbi->s_anchor[i]) {
829 bh = udf_read_tagged(sb, sbi->s_anchor[i],
830 sbi->s_anchor[i], &ident);
832 sbi->s_anchor[i] = 0;
835 if ((ident != TAG_IDENT_AVDP) &&
836 (i || (ident != TAG_IDENT_FE &&
837 ident != TAG_IDENT_EFE)))
838 sbi->s_anchor[i] = 0;
843 sbi->s_last_block = lastblock;
846 static int udf_find_fileset(struct super_block *sb,
847 kernel_lb_addr *fileset,
848 kernel_lb_addr *root)
850 struct buffer_head *bh = NULL;
853 struct udf_sb_info *sbi;
855 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
856 fileset->partitionReferenceNum != 0xFFFF) {
857 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
861 } else if (ident != TAG_IDENT_FSD) {
870 /* Search backwards through the partitions */
871 kernel_lb_addr newfileset;
873 /* --> cvg: FIXME - is it reasonable? */
876 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
877 (newfileset.partitionReferenceNum != 0xFFFF &&
878 fileset->logicalBlockNum == 0xFFFFFFFF &&
879 fileset->partitionReferenceNum == 0xFFFF);
880 newfileset.partitionReferenceNum--) {
881 lastblock = sbi->s_partmaps
882 [newfileset.partitionReferenceNum]
884 newfileset.logicalBlockNum = 0;
887 bh = udf_read_ptagged(sb, newfileset, 0,
890 newfileset.logicalBlockNum++;
897 struct spaceBitmapDesc *sp;
898 sp = (struct spaceBitmapDesc *)
900 newfileset.logicalBlockNum += 1 +
901 ((le32_to_cpu(sp->numOfBytes) +
902 sizeof(struct spaceBitmapDesc)
903 - 1) >> sb->s_blocksize_bits);
908 *fileset = newfileset;
911 newfileset.logicalBlockNum++;
916 } while (newfileset.logicalBlockNum < lastblock &&
917 fileset->logicalBlockNum == 0xFFFFFFFF &&
918 fileset->partitionReferenceNum == 0xFFFF);
922 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
923 fileset->partitionReferenceNum != 0xFFFF) && bh) {
924 udf_debug("Fileset at block=%d, partition=%d\n",
925 fileset->logicalBlockNum,
926 fileset->partitionReferenceNum);
928 sbi->s_partition = fileset->partitionReferenceNum;
929 udf_load_fileset(sb, bh, root);
936 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
938 struct primaryVolDesc *pvoldesc;
944 pvoldesc = (struct primaryVolDesc *)bh->b_data;
946 if (udf_stamp_to_time(&recording, &recording_usec,
947 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
949 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
950 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
952 recording, recording_usec,
953 ts.year, ts.month, ts.day, ts.hour,
954 ts.minute, ts.typeAndTimezone);
955 UDF_SB(sb)->s_record_time.tv_sec = recording;
956 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
959 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
960 if (udf_CS0toUTF8(&outstr, &instr)) {
961 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
962 outstr.u_len > 31 ? 31 : outstr.u_len);
963 udf_debug("volIdent[] = '%s'\n",
964 UDF_SB(sb)->s_volume_ident);
967 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
968 if (udf_CS0toUTF8(&outstr, &instr))
969 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
972 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
973 kernel_lb_addr *root)
975 struct fileSetDesc *fset;
977 fset = (struct fileSetDesc *)bh->b_data;
979 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
981 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
983 udf_debug("Rootdir at block=%d, partition=%d\n",
984 root->logicalBlockNum, root->partitionReferenceNum);
987 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
989 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
990 return (map->s_partition_len +
991 (sizeof(struct spaceBitmapDesc) << 3) +
992 (sb->s_blocksize * 8) - 1) /
993 (sb->s_blocksize * 8);
996 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
998 struct udf_bitmap *bitmap;
1002 nr_groups = udf_compute_nr_groups(sb, index);
1003 size = sizeof(struct udf_bitmap) +
1004 (sizeof(struct buffer_head *) * nr_groups);
1006 if (size <= PAGE_SIZE)
1007 bitmap = kmalloc(size, GFP_KERNEL);
1009 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1011 if (bitmap == NULL) {
1012 udf_error(sb, __FUNCTION__,
1013 "Unable to allocate space for bitmap "
1014 "and %d buffer_head pointers", nr_groups);
1018 memset(bitmap, 0x00, size);
1019 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1020 bitmap->s_nr_groups = nr_groups;
1024 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
1026 struct partitionDesc *p;
1028 struct udf_part_map *map;
1029 struct udf_sb_info *sbi;
1031 p = (struct partitionDesc *)bh->b_data;
1034 for (i = 0; i < sbi->s_partitions; i++) {
1035 map = &sbi->s_partmaps[i];
1036 udf_debug("Searching map: (%d == %d)\n",
1037 map->s_partition_num,
1038 le16_to_cpu(p->partitionNumber));
1039 if (map->s_partition_num ==
1040 le16_to_cpu(p->partitionNumber)) {
1041 map->s_partition_len =
1042 le32_to_cpu(p->partitionLength); /* blocks */
1043 map->s_partition_root =
1044 le32_to_cpu(p->partitionStartingLocation);
1045 if (p->accessType ==
1046 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1047 map->s_partition_flags |=
1048 UDF_PART_FLAG_READ_ONLY;
1049 if (p->accessType ==
1050 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1051 map->s_partition_flags |=
1052 UDF_PART_FLAG_WRITE_ONCE;
1053 if (p->accessType ==
1054 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1055 map->s_partition_flags |=
1056 UDF_PART_FLAG_REWRITABLE;
1057 if (p->accessType ==
1058 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1059 map->s_partition_flags |=
1060 UDF_PART_FLAG_OVERWRITABLE;
1062 if (!strcmp(p->partitionContents.ident,
1063 PD_PARTITION_CONTENTS_NSR02) ||
1064 !strcmp(p->partitionContents.ident,
1065 PD_PARTITION_CONTENTS_NSR03)) {
1066 struct partitionHeaderDesc *phd;
1068 phd = (struct partitionHeaderDesc *)
1069 (p->partitionContentsUse);
1070 if (phd->unallocSpaceTable.extLength) {
1071 kernel_lb_addr loc = {
1072 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1073 .partitionReferenceNum = i,
1076 map->s_uspace.s_table =
1078 if (!map->s_uspace.s_table) {
1079 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
1082 map->s_partition_flags |=
1083 UDF_PART_FLAG_UNALLOC_TABLE;
1084 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1085 i, map->s_uspace.s_table->i_ino);
1087 if (phd->unallocSpaceBitmap.extLength) {
1088 struct udf_bitmap *bitmap =
1089 udf_sb_alloc_bitmap(sb, i);
1090 map->s_uspace.s_bitmap = bitmap;
1091 if (bitmap != NULL) {
1092 bitmap->s_extLength =
1093 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
1094 bitmap->s_extPosition =
1095 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
1096 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1097 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1098 i, bitmap->s_extPosition);
1101 if (phd->partitionIntegrityTable.extLength)
1102 udf_debug("partitionIntegrityTable (part %d)\n", i);
1103 if (phd->freedSpaceTable.extLength) {
1104 kernel_lb_addr loc = {
1105 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1106 .partitionReferenceNum = i,
1109 map->s_fspace.s_table =
1111 if (!map->s_fspace.s_table) {
1112 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
1115 map->s_partition_flags |=
1116 UDF_PART_FLAG_FREED_TABLE;
1117 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1118 i, map->s_fspace.s_table->i_ino);
1120 if (phd->freedSpaceBitmap.extLength) {
1121 struct udf_bitmap *bitmap =
1122 udf_sb_alloc_bitmap(sb, i);
1123 map->s_fspace.s_bitmap = bitmap;
1124 if (bitmap != NULL) {
1125 bitmap->s_extLength =
1126 le32_to_cpu(phd->freedSpaceBitmap.extLength);
1127 bitmap->s_extPosition =
1128 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
1129 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1130 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1131 i, bitmap->s_extPosition);
1138 if (i == sbi->s_partitions)
1139 udf_debug("Partition (%d) not found in partition map\n",
1140 le16_to_cpu(p->partitionNumber));
1142 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1143 "block length %d\n",
1144 le16_to_cpu(p->partitionNumber), i,
1145 map->s_partition_type,
1146 map->s_partition_root,
1147 map->s_partition_len);
1151 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1152 kernel_lb_addr *fileset)
1154 struct logicalVolDesc *lvd;
1157 struct udf_sb_info *sbi = UDF_SB(sb);
1158 struct genericPartitionMap *gpm;
1160 lvd = (struct logicalVolDesc *)bh->b_data;
1162 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1166 for (i = 0, offset = 0;
1167 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1168 i++, offset += gpm->partitionMapLength) {
1169 struct udf_part_map *map = &sbi->s_partmaps[i];
1170 gpm = (struct genericPartitionMap *)
1171 &(lvd->partitionMaps[offset]);
1172 type = gpm->partitionMapType;
1174 struct genericPartitionMap1 *gpm1 =
1175 (struct genericPartitionMap1 *)gpm;
1176 map->s_partition_type = UDF_TYPE1_MAP15;
1177 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1178 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1179 map->s_partition_func = NULL;
1180 } else if (type == 2) {
1181 struct udfPartitionMap2 *upm2 =
1182 (struct udfPartitionMap2 *)gpm;
1183 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1184 strlen(UDF_ID_VIRTUAL))) {
1186 le16_to_cpu(((__le16 *)upm2->partIdent.
1188 if (suf == 0x0150) {
1189 map->s_partition_type =
1191 map->s_partition_func =
1192 udf_get_pblock_virt15;
1193 } else if (suf == 0x0200) {
1194 map->s_partition_type =
1196 map->s_partition_func =
1197 udf_get_pblock_virt20;
1199 } else if (!strncmp(upm2->partIdent.ident,
1201 strlen(UDF_ID_SPARABLE))) {
1204 struct sparingTable *st;
1205 struct sparablePartitionMap *spm =
1206 (struct sparablePartitionMap *)gpm;
1208 map->s_partition_type = UDF_SPARABLE_MAP15;
1209 map->s_type_specific.s_sparing.s_packet_len =
1210 le16_to_cpu(spm->packetLength);
1211 for (j = 0; j < spm->numSparingTables; j++) {
1212 struct buffer_head *bh2;
1215 spm->locSparingTable[j]);
1216 bh2 = udf_read_tagged(sb, loc, loc,
1218 map->s_type_specific.s_sparing.
1219 s_spar_map[j] = bh2;
1222 st = (struct sparingTable *)
1224 if (ident != 0 || strncmp(
1225 st->sparingIdent.ident,
1227 strlen(UDF_ID_SPARING))) {
1229 map->s_type_specific.
1236 map->s_partition_func = udf_get_pblock_spar15;
1238 udf_debug("Unknown ident: %s\n",
1239 upm2->partIdent.ident);
1242 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1243 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1245 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1246 i, map->s_partition_num, type,
1247 map->s_volumeseqnum);
1251 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1253 *fileset = lelb_to_cpu(la->extLocation);
1254 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1255 "partition=%d\n", fileset->logicalBlockNum,
1256 fileset->partitionReferenceNum);
1258 if (lvd->integritySeqExt.extLength)
1259 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1265 * udf_load_logicalvolint
1268 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1270 struct buffer_head *bh = NULL;
1272 struct udf_sb_info *sbi = UDF_SB(sb);
1273 struct logicalVolIntegrityDesc *lvid;
1275 while (loc.extLength > 0 &&
1276 (bh = udf_read_tagged(sb, loc.extLocation,
1277 loc.extLocation, &ident)) &&
1278 ident == TAG_IDENT_LVID) {
1279 sbi->s_lvid_bh = bh;
1280 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1282 if (lvid->nextIntegrityExt.extLength)
1283 udf_load_logicalvolint(sb,
1284 leea_to_cpu(lvid->nextIntegrityExt));
1286 if (sbi->s_lvid_bh != bh)
1288 loc.extLength -= sb->s_blocksize;
1291 if (sbi->s_lvid_bh != bh)
1296 * udf_process_sequence
1299 * Process a main/reserve volume descriptor sequence.
1302 * sb Pointer to _locked_ superblock.
1303 * block First block of first extent of the sequence.
1304 * lastblock Lastblock of first extent of the sequence.
1307 * July 1, 1997 - Andrew E. Mileski
1308 * Written, tested, and released.
1310 static int udf_process_sequence(struct super_block *sb, long block,
1311 long lastblock, kernel_lb_addr *fileset)
1313 struct buffer_head *bh = NULL;
1314 struct udf_vds_record vds[VDS_POS_LENGTH];
1315 struct udf_vds_record *curr;
1316 struct generic_desc *gd;
1317 struct volDescPtr *vdp;
1322 long next_s = 0, next_e = 0;
1324 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1326 /* Read the main descriptor sequence */
1327 for (; (!done && block <= lastblock); block++) {
1329 bh = udf_read_tagged(sb, block, block, &ident);
1333 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1334 gd = (struct generic_desc *)bh->b_data;
1335 vdsn = le32_to_cpu(gd->volDescSeqNum);
1337 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1338 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1339 if (vdsn >= curr->volDescSeqNum) {
1340 curr->volDescSeqNum = vdsn;
1341 curr->block = block;
1344 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1345 curr = &vds[VDS_POS_VOL_DESC_PTR];
1346 if (vdsn >= curr->volDescSeqNum) {
1347 curr->volDescSeqNum = vdsn;
1348 curr->block = block;
1350 vdp = (struct volDescPtr *)bh->b_data;
1351 next_s = le32_to_cpu(
1352 vdp->nextVolDescSeqExt.extLocation);
1353 next_e = le32_to_cpu(
1354 vdp->nextVolDescSeqExt.extLength);
1355 next_e = next_e >> sb->s_blocksize_bits;
1359 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1360 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1361 if (vdsn >= curr->volDescSeqNum) {
1362 curr->volDescSeqNum = vdsn;
1363 curr->block = block;
1366 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1367 curr = &vds[VDS_POS_PARTITION_DESC];
1369 curr->block = block;
1371 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1372 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1373 if (vdsn >= curr->volDescSeqNum) {
1374 curr->volDescSeqNum = vdsn;
1375 curr->block = block;
1378 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1379 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1380 if (vdsn >= curr->volDescSeqNum) {
1381 curr->volDescSeqNum = vdsn;
1382 curr->block = block;
1385 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1386 vds[VDS_POS_TERMINATING_DESC].block = block;
1390 next_s = next_e = 0;
1397 for (i = 0; i < VDS_POS_LENGTH; i++) {
1399 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1402 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1403 udf_load_pvoldesc(sb, bh);
1404 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1405 if (udf_load_logicalvol(sb, bh, fileset)) {
1409 } else if (i == VDS_POS_PARTITION_DESC) {
1410 struct buffer_head *bh2 = NULL;
1411 if (udf_load_partdesc(sb, bh)) {
1415 for (j = vds[i].block + 1;
1416 j < vds[VDS_POS_TERMINATING_DESC].block;
1418 bh2 = udf_read_tagged(sb, j, j, &ident);
1419 gd = (struct generic_desc *)bh2->b_data;
1420 if (ident == TAG_IDENT_PD)
1421 if (udf_load_partdesc(sb,
1440 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1445 udf_debug("Validity check skipped because of novrs option\n");
1448 /* Check that it is NSR02 compliant */
1449 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1451 block = udf_vrs(sb, silent);
1453 struct udf_sb_info *sbi = UDF_SB(sb);
1454 udf_debug("Failed to read byte 32768. Assuming open "
1455 "disc. Skipping validity check\n");
1456 if (!sbi->s_last_block)
1457 sbi->s_last_block = udf_get_last_block(sb);
1464 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1466 struct anchorVolDescPtr *anchor;
1468 struct buffer_head *bh;
1469 long main_s, main_e, reserve_s, reserve_e;
1471 struct udf_sb_info *sbi;
1477 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1478 if (!sbi->s_anchor[i])
1480 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1485 anchor = (struct anchorVolDescPtr *)bh->b_data;
1487 /* Locate the main sequence */
1488 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1489 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1490 main_e = main_e >> sb->s_blocksize_bits;
1493 /* Locate the reserve sequence */
1494 reserve_s = le32_to_cpu(
1495 anchor->reserveVolDescSeqExt.extLocation);
1496 reserve_e = le32_to_cpu(
1497 anchor->reserveVolDescSeqExt.extLength);
1498 reserve_e = reserve_e >> sb->s_blocksize_bits;
1499 reserve_e += reserve_s;
1503 /* Process the main & reserve sequences */
1504 /* responsible for finding the PartitionDesc(s) */
1505 if (!(udf_process_sequence(sb, main_s, main_e,
1507 udf_process_sequence(sb, reserve_s, reserve_e,
1512 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1513 udf_debug("No Anchor block found\n");
1516 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1518 for (i = 0; i < sbi->s_partitions; i++) {
1519 kernel_lb_addr uninitialized_var(ino);
1520 struct udf_part_map *map = &sbi->s_partmaps[i];
1521 switch (map->s_partition_type) {
1522 case UDF_VIRTUAL_MAP15:
1523 case UDF_VIRTUAL_MAP20:
1524 if (!sbi->s_last_block) {
1525 sbi->s_last_block = udf_get_last_block(sb);
1526 udf_find_anchor(sb);
1529 if (!sbi->s_last_block) {
1530 udf_debug("Unable to determine Lastblock (For "
1531 "Virtual Partition)\n");
1535 for (j = 0; j < sbi->s_partitions; j++) {
1536 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1538 map->s_volumeseqnum ==
1539 map2->s_volumeseqnum &&
1540 map->s_partition_num ==
1541 map2->s_partition_num) {
1542 ino.partitionReferenceNum = j;
1543 ino.logicalBlockNum =
1545 map2->s_partition_root;
1550 if (j == sbi->s_partitions)
1553 sbi->s_vat_inode = udf_iget(sb, ino);
1554 if (!sbi->s_vat_inode)
1557 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1558 map->s_type_specific.s_virtual.s_start_offset =
1559 udf_ext0_offset(sbi->s_vat_inode);
1560 map->s_type_specific.s_virtual.s_num_entries =
1561 (sbi->s_vat_inode->i_size - 36) >> 2;
1562 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1564 struct virtualAllocationTable20 *vat20;
1566 pos = udf_block_map(sbi->s_vat_inode, 0);
1567 bh = sb_bread(sb, pos);
1570 vat20 = (struct virtualAllocationTable20 *)
1572 udf_ext0_offset(sbi->s_vat_inode);
1573 map->s_type_specific.s_virtual.s_start_offset =
1574 le16_to_cpu(vat20->lengthHeader) +
1575 udf_ext0_offset(sbi->s_vat_inode);
1576 map->s_type_specific.s_virtual.s_num_entries =
1577 (sbi->s_vat_inode->i_size -
1578 map->s_type_specific.s_virtual.
1579 s_start_offset) >> 2;
1582 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1583 map->s_partition_len =
1584 sbi->s_partmaps[ino.partitionReferenceNum].
1591 static void udf_open_lvid(struct super_block *sb)
1593 struct udf_sb_info *sbi = UDF_SB(sb);
1594 struct buffer_head *bh = sbi->s_lvid_bh;
1596 kernel_timestamp cpu_time;
1597 struct logicalVolIntegrityDesc *lvid =
1598 (struct logicalVolIntegrityDesc *)bh->b_data;
1599 struct logicalVolIntegrityDescImpUse *lvidiu =
1602 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1603 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1604 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1605 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1606 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1608 lvid->descTag.descCRC = cpu_to_le16(
1609 udf_crc((char *)lvid + sizeof(tag),
1610 le16_to_cpu(lvid->descTag.descCRCLength),
1613 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1614 mark_buffer_dirty(bh);
1618 static void udf_close_lvid(struct super_block *sb)
1620 kernel_timestamp cpu_time;
1621 struct udf_sb_info *sbi = UDF_SB(sb);
1622 struct buffer_head *bh = sbi->s_lvid_bh;
1623 struct logicalVolIntegrityDesc *lvid;
1628 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1630 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1631 struct logicalVolIntegrityDescImpUse *lvidiu =
1633 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1634 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1635 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1636 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1637 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1638 lvidiu->maxUDFWriteRev =
1639 cpu_to_le16(UDF_MAX_WRITE_VERSION);
1640 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1641 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1642 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1643 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1644 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1646 lvid->descTag.descCRC = cpu_to_le16(
1647 udf_crc((char *)lvid + sizeof(tag),
1648 le16_to_cpu(lvid->descTag.descCRCLength),
1651 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1652 mark_buffer_dirty(bh);
1656 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1659 int nr_groups = bitmap->s_nr_groups;
1660 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1663 for (i = 0; i < nr_groups; i++)
1664 if (bitmap->s_block_bitmap[i])
1665 brelse(bitmap->s_block_bitmap[i]);
1667 if (size <= PAGE_SIZE)
1673 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1676 struct inode *inode = NULL;
1677 struct udf_options uopt;
1678 kernel_lb_addr rootdir, fileset;
1679 struct udf_sb_info *sbi;
1681 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1686 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1690 sb->s_fs_info = sbi;
1692 mutex_init(&sbi->s_alloc_mutex);
1694 if (!udf_parse_options((char *)options, &uopt, false))
1697 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1698 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1699 udf_error(sb, "udf_read_super",
1700 "utf8 cannot be combined with iocharset\n");
1703 #ifdef CONFIG_UDF_NLS
1704 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1705 uopt.nls_map = load_nls_default();
1707 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1709 udf_debug("Using default NLS map\n");
1712 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1713 uopt.flags |= (1 << UDF_FLAG_UTF8);
1715 fileset.logicalBlockNum = 0xFFFFFFFF;
1716 fileset.partitionReferenceNum = 0xFFFF;
1718 sbi->s_flags = uopt.flags;
1719 sbi->s_uid = uopt.uid;
1720 sbi->s_gid = uopt.gid;
1721 sbi->s_umask = uopt.umask;
1722 sbi->s_nls_map = uopt.nls_map;
1724 /* Set the block size for all transfers */
1725 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1726 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1727 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1731 if (uopt.session == 0xFFFFFFFF)
1732 sbi->s_session = udf_get_last_session(sb);
1734 sbi->s_session = uopt.session;
1736 udf_debug("Multi-session=%d\n", sbi->s_session);
1738 sbi->s_last_block = uopt.lastblock;
1739 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1740 sbi->s_anchor[2] = uopt.anchor;
1741 sbi->s_anchor[3] = 256;
1743 if (udf_check_valid(sb, uopt.novrs, silent)) {
1744 /* read volume recognition sequences */
1745 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1749 udf_find_anchor(sb);
1751 /* Fill in the rest of the superblock */
1752 sb->s_op = &udf_sb_ops;
1755 sb->s_magic = UDF_SUPER_MAGIC;
1756 sb->s_time_gran = 1000;
1758 if (udf_load_partition(sb, &fileset)) {
1759 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1763 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1765 if (sbi->s_lvid_bh) {
1766 struct logicalVolIntegrityDescImpUse *lvidiu =
1768 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1769 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1770 /* uint16_t maxUDFWriteRev =
1771 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1773 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1774 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1776 le16_to_cpu(lvidiu->minUDFReadRev),
1777 UDF_MAX_READ_VERSION);
1779 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1780 sb->s_flags |= MS_RDONLY;
1782 sbi->s_udfrev = minUDFWriteRev;
1784 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1785 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1786 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1787 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1790 if (!sbi->s_partitions) {
1791 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1795 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1796 UDF_PART_FLAG_READ_ONLY) {
1797 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1798 "forcing readonly mount\n");
1799 sb->s_flags |= MS_RDONLY;
1802 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1803 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1808 kernel_timestamp ts;
1809 udf_time_to_stamp(&ts, sbi->s_record_time);
1810 udf_info("UDF: Mounting volume '%s', "
1811 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1812 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1813 ts.hour, ts.minute, ts.typeAndTimezone);
1815 if (!(sb->s_flags & MS_RDONLY))
1818 /* Assign the root inode */
1819 /* assign inodes by physical block number */
1820 /* perhaps it's not extensible enough, but for now ... */
1821 inode = udf_iget(sb, rootdir);
1823 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1825 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1829 /* Allocate a dentry for the root inode */
1830 sb->s_root = d_alloc_root(inode);
1832 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1836 sb->s_maxbytes = MAX_LFS_FILESIZE;
1840 if (sbi->s_vat_inode)
1841 iput(sbi->s_vat_inode);
1842 if (sbi->s_partitions) {
1843 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1844 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1845 iput(map->s_uspace.s_table);
1846 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1847 iput(map->s_fspace.s_table);
1848 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1849 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1850 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1851 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1852 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1853 for (i = 0; i < 4; i++)
1854 brelse(map->s_type_specific.s_sparing.
1857 #ifdef CONFIG_UDF_NLS
1858 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1859 unload_nls(sbi->s_nls_map);
1861 if (!(sb->s_flags & MS_RDONLY))
1863 brelse(sbi->s_lvid_bh);
1865 kfree(sbi->s_partmaps);
1867 sb->s_fs_info = NULL;
1872 void udf_error(struct super_block *sb, const char *function,
1873 const char *fmt, ...)
1877 if (!(sb->s_flags & MS_RDONLY)) {
1881 va_start(args, fmt);
1882 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1884 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1885 sb->s_id, function, error_buf);
1888 void udf_warning(struct super_block *sb, const char *function,
1889 const char *fmt, ...)
1893 va_start(args, fmt);
1894 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1896 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1897 sb->s_id, function, error_buf);
1900 static void udf_put_super(struct super_block *sb)
1903 struct udf_sb_info *sbi;
1906 if (sbi->s_vat_inode)
1907 iput(sbi->s_vat_inode);
1908 if (sbi->s_partitions) {
1909 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1910 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1911 iput(map->s_uspace.s_table);
1912 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1913 iput(map->s_fspace.s_table);
1914 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1915 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1916 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1917 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1918 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1919 for (i = 0; i < 4; i++)
1920 brelse(map->s_type_specific.s_sparing.
1923 #ifdef CONFIG_UDF_NLS
1924 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1925 unload_nls(sbi->s_nls_map);
1927 if (!(sb->s_flags & MS_RDONLY))
1929 brelse(sbi->s_lvid_bh);
1930 kfree(sbi->s_partmaps);
1931 kfree(sb->s_fs_info);
1932 sb->s_fs_info = NULL;
1935 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1937 struct super_block *sb = dentry->d_sb;
1938 struct udf_sb_info *sbi = UDF_SB(sb);
1939 struct logicalVolIntegrityDescImpUse *lvidiu;
1941 if (sbi->s_lvid_bh != NULL)
1942 lvidiu = udf_sb_lvidiu(sbi);
1946 buf->f_type = UDF_SUPER_MAGIC;
1947 buf->f_bsize = sb->s_blocksize;
1948 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1949 buf->f_bfree = udf_count_free(sb);
1950 buf->f_bavail = buf->f_bfree;
1951 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1952 le32_to_cpu(lvidiu->numDirs)) : 0)
1954 buf->f_ffree = buf->f_bfree;
1955 /* __kernel_fsid_t f_fsid */
1956 buf->f_namelen = UDF_NAME_LEN - 2;
1961 static unsigned char udf_bitmap_lookup[16] = {
1962 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1965 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1966 struct udf_bitmap *bitmap)
1968 struct buffer_head *bh = NULL;
1969 unsigned int accum = 0;
1971 int block = 0, newblock;
1977 struct spaceBitmapDesc *bm;
1981 loc.logicalBlockNum = bitmap->s_extPosition;
1982 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1983 bh = udf_read_ptagged(sb, loc, 0, &ident);
1986 printk(KERN_ERR "udf: udf_count_free failed\n");
1988 } else if (ident != TAG_IDENT_SBD) {
1990 printk(KERN_ERR "udf: udf_count_free failed\n");
1994 bm = (struct spaceBitmapDesc *)bh->b_data;
1995 bytes = le32_to_cpu(bm->numOfBytes);
1996 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1997 ptr = (uint8_t *)bh->b_data;
2000 while ((bytes > 0) && (index < sb->s_blocksize)) {
2002 accum += udf_bitmap_lookup[value & 0x0f];
2003 accum += udf_bitmap_lookup[value >> 4];
2009 newblock = udf_get_lb_pblock(sb, loc, ++block);
2010 bh = udf_tread(sb, newblock);
2012 udf_debug("read failed\n");
2016 ptr = (uint8_t *)bh->b_data;
2027 static unsigned int udf_count_free_table(struct super_block *sb,
2028 struct inode *table)
2030 unsigned int accum = 0;
2032 kernel_lb_addr eloc;
2034 struct extent_position epos;
2038 epos.block = UDF_I(table)->i_location;
2039 epos.offset = sizeof(struct unallocSpaceEntry);
2042 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2043 accum += (elen >> table->i_sb->s_blocksize_bits);
2052 static unsigned int udf_count_free(struct super_block *sb)
2054 unsigned int accum = 0;
2055 struct udf_sb_info *sbi;
2056 struct udf_part_map *map;
2059 if (sbi->s_lvid_bh) {
2060 struct logicalVolIntegrityDesc *lvid =
2061 (struct logicalVolIntegrityDesc *)
2062 sbi->s_lvid_bh->b_data;
2063 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2064 accum = le32_to_cpu(
2065 lvid->freeSpaceTable[sbi->s_partition]);
2066 if (accum == 0xFFFFFFFF)
2074 map = &sbi->s_partmaps[sbi->s_partition];
2075 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2076 accum += udf_count_free_bitmap(sb,
2077 map->s_uspace.s_bitmap);
2079 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2080 accum += udf_count_free_bitmap(sb,
2081 map->s_fspace.s_bitmap);
2086 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2087 accum += udf_count_free_table(sb,
2088 map->s_uspace.s_table);
2090 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2091 accum += udf_count_free_table(sb,
2092 map->s_fspace.s_table);