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 <asm/byteorder.h>
58 #include <linux/udf_fs.h>
62 #include <linux/init.h>
63 #include <asm/uaccess.h>
65 #define VDS_POS_PRIMARY_VOL_DESC 0
66 #define VDS_POS_UNALLOC_SPACE_DESC 1
67 #define VDS_POS_LOGICAL_VOL_DESC 2
68 #define VDS_POS_PARTITION_DESC 3
69 #define VDS_POS_IMP_USE_VOL_DESC 4
70 #define VDS_POS_VOL_DESC_PTR 5
71 #define VDS_POS_TERMINATING_DESC 6
72 #define VDS_POS_LENGTH 7
74 static char error_buf[1024];
76 /* These are the "meat" - everything else is stuffing */
77 static int udf_fill_super(struct super_block *, void *, int);
78 static void udf_put_super(struct super_block *);
79 static void udf_write_super(struct super_block *);
80 static int udf_remount_fs(struct super_block *, int *, char *);
81 static int udf_check_valid(struct super_block *, int, int);
82 static int udf_vrs(struct super_block *sb, int silent);
83 static int udf_load_partition(struct super_block *, kernel_lb_addr *);
84 static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
86 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
87 static void udf_find_anchor(struct super_block *);
88 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
90 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
91 static void udf_load_fileset(struct super_block *, struct buffer_head *,
93 static int udf_load_partdesc(struct super_block *, struct buffer_head *);
94 static void udf_open_lvid(struct super_block *);
95 static void udf_close_lvid(struct super_block *);
96 static unsigned int udf_count_free(struct super_block *);
97 static int udf_statfs(struct dentry *, struct kstatfs *);
99 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
101 struct logicalVolIntegrityDesc *lvid =
102 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
103 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
104 __u32 offset = number_of_partitions * 2 *
105 sizeof(uint32_t)/sizeof(uint8_t);
106 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
109 /* UDF filesystem type */
110 static int udf_get_sb(struct file_system_type *fs_type,
111 int flags, const char *dev_name, void *data,
112 struct vfsmount *mnt)
114 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
117 static struct file_system_type udf_fstype = {
118 .owner = THIS_MODULE,
120 .get_sb = udf_get_sb,
121 .kill_sb = kill_block_super,
122 .fs_flags = FS_REQUIRES_DEV,
125 static struct kmem_cache *udf_inode_cachep;
127 static struct inode *udf_alloc_inode(struct super_block *sb)
129 struct udf_inode_info *ei;
130 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
135 ei->i_lenExtents = 0;
136 ei->i_next_alloc_block = 0;
137 ei->i_next_alloc_goal = 0;
140 return &ei->vfs_inode;
143 static void udf_destroy_inode(struct inode *inode)
145 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
148 static void init_once(struct kmem_cache *cachep, void *foo)
150 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
152 ei->i_ext.i_data = NULL;
153 inode_init_once(&ei->vfs_inode);
156 static int init_inodecache(void)
158 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
159 sizeof(struct udf_inode_info),
160 0, (SLAB_RECLAIM_ACCOUNT |
163 if (!udf_inode_cachep)
168 static void destroy_inodecache(void)
170 kmem_cache_destroy(udf_inode_cachep);
173 /* Superblock operations */
174 static const struct super_operations udf_sb_ops = {
175 .alloc_inode = udf_alloc_inode,
176 .destroy_inode = udf_destroy_inode,
177 .write_inode = udf_write_inode,
178 .delete_inode = udf_delete_inode,
179 .clear_inode = udf_clear_inode,
180 .put_super = udf_put_super,
181 .write_super = udf_write_super,
182 .statfs = udf_statfs,
183 .remount_fs = udf_remount_fs,
188 unsigned int blocksize;
189 unsigned int session;
190 unsigned int lastblock;
193 unsigned short partition;
194 unsigned int fileset;
195 unsigned int rootdir;
200 struct nls_table *nls_map;
203 static int __init init_udf_fs(void)
207 err = init_inodecache();
210 err = register_filesystem(&udf_fstype);
217 destroy_inodecache();
223 static void __exit exit_udf_fs(void)
225 unregister_filesystem(&udf_fstype);
226 destroy_inodecache();
229 module_init(init_udf_fs)
230 module_exit(exit_udf_fs)
232 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
234 struct udf_sb_info *sbi = UDF_SB(sb);
236 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
238 if (!sbi->s_partmaps) {
239 udf_error(sb, __FUNCTION__,
240 "Unable to allocate space for %d partition maps",
242 sbi->s_partitions = 0;
246 sbi->s_partitions = count;
254 * Parse mount options.
257 * The following mount options are supported:
259 * gid= Set the default group.
260 * umask= Set the default umask.
261 * uid= Set the default user.
262 * bs= Set the block size.
263 * unhide Show otherwise hidden files.
264 * undelete Show deleted files in lists.
265 * adinicb Embed data in the inode (default)
266 * noadinicb Don't embed data in the inode
267 * shortad Use short ad's
268 * longad Use long ad's (default)
269 * nostrict Unset strict conformance
270 * iocharset= Set the NLS character set
272 * The remaining are for debugging and disaster recovery:
274 * novrs Skip volume sequence recognition
276 * The following expect a offset from 0.
278 * session= Set the CDROM session (default= last session)
279 * anchor= Override standard anchor location. (default= 256)
280 * volume= Override the VolumeDesc location. (unused)
281 * partition= Override the PartitionDesc location. (unused)
282 * lastblock= Set the last block of the filesystem/
284 * The following expect a offset from the partition root.
286 * fileset= Override the fileset block location. (unused)
287 * rootdir= Override the root directory location. (unused)
288 * WARNING: overriding the rootdir to a non-directory may
289 * yield highly unpredictable results.
292 * options Pointer to mount options string.
293 * uopts Pointer to mount options variable.
296 * <return> 1 Mount options parsed okay.
297 * <return> 0 Error parsing mount options.
300 * July 1, 1997 - Andrew E. Mileski
301 * Written, tested, and released.
305 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
306 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
307 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
308 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
309 Opt_rootdir, Opt_utf8, Opt_iocharset,
310 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
313 static match_table_t tokens = {
314 {Opt_novrs, "novrs"},
315 {Opt_nostrict, "nostrict"},
317 {Opt_unhide, "unhide"},
318 {Opt_undelete, "undelete"},
319 {Opt_noadinicb, "noadinicb"},
320 {Opt_adinicb, "adinicb"},
321 {Opt_shortad, "shortad"},
322 {Opt_longad, "longad"},
323 {Opt_uforget, "uid=forget"},
324 {Opt_uignore, "uid=ignore"},
325 {Opt_gforget, "gid=forget"},
326 {Opt_gignore, "gid=ignore"},
329 {Opt_umask, "umask=%o"},
330 {Opt_session, "session=%u"},
331 {Opt_lastblock, "lastblock=%u"},
332 {Opt_anchor, "anchor=%u"},
333 {Opt_volume, "volume=%u"},
334 {Opt_partition, "partition=%u"},
335 {Opt_fileset, "fileset=%u"},
336 {Opt_rootdir, "rootdir=%u"},
338 {Opt_iocharset, "iocharset=%s"},
342 static int udf_parse_options(char *options, struct udf_options *uopt)
348 uopt->blocksize = 2048;
349 uopt->partition = 0xFFFF;
350 uopt->session = 0xFFFFFFFF;
353 uopt->volume = 0xFFFFFFFF;
354 uopt->rootdir = 0xFFFFFFFF;
355 uopt->fileset = 0xFFFFFFFF;
356 uopt->nls_map = NULL;
361 while ((p = strsep(&options, ",")) != NULL) {
362 substring_t args[MAX_OPT_ARGS];
367 token = match_token(p, tokens, args);
372 if (match_int(&args[0], &option))
374 uopt->blocksize = option;
377 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
380 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
383 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
386 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
389 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
392 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
395 if (match_int(args, &option))
398 uopt->flags |= (1 << UDF_FLAG_GID_SET);
401 if (match_int(args, &option))
404 uopt->flags |= (1 << UDF_FLAG_UID_SET);
407 if (match_octal(args, &option))
409 uopt->umask = option;
412 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
415 if (match_int(args, &option))
417 uopt->session = option;
420 if (match_int(args, &option))
422 uopt->lastblock = option;
425 if (match_int(args, &option))
427 uopt->anchor = option;
430 if (match_int(args, &option))
432 uopt->volume = option;
435 if (match_int(args, &option))
437 uopt->partition = option;
440 if (match_int(args, &option))
442 uopt->fileset = option;
445 if (match_int(args, &option))
447 uopt->rootdir = option;
450 uopt->flags |= (1 << UDF_FLAG_UTF8);
452 #ifdef CONFIG_UDF_NLS
454 uopt->nls_map = load_nls(args[0].from);
455 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
459 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
462 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
465 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
468 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
471 printk(KERN_ERR "udf: bad mount option \"%s\" "
472 "or missing value\n", p);
479 static void udf_write_super(struct super_block *sb)
483 if (!(sb->s_flags & MS_RDONLY))
490 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
492 struct udf_options uopt;
493 struct udf_sb_info *sbi = UDF_SB(sb);
495 uopt.flags = sbi->s_flags;
496 uopt.uid = sbi->s_uid;
497 uopt.gid = sbi->s_gid;
498 uopt.umask = sbi->s_umask;
500 if (!udf_parse_options(options, &uopt))
503 sbi->s_flags = uopt.flags;
504 sbi->s_uid = uopt.uid;
505 sbi->s_gid = uopt.gid;
506 sbi->s_umask = uopt.umask;
508 if (sbi->s_lvid_bh) {
509 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
510 if (write_rev > UDF_MAX_WRITE_VERSION)
514 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
516 if (*flags & MS_RDONLY)
528 * Set the block size to be used in all transfers.
531 * To allow room for a DMA transfer, it is best to guess big when unsure.
532 * This routine picks 2048 bytes as the blocksize when guessing. This
533 * should be adequate until devices with larger block sizes become common.
535 * Note that the Linux kernel can currently only deal with blocksizes of
536 * 512, 1024, 2048, 4096, and 8192 bytes.
539 * sb Pointer to _locked_ superblock.
542 * sb->s_blocksize Blocksize.
543 * sb->s_blocksize_bits log2 of blocksize.
544 * <return> 0 Blocksize is valid.
545 * <return> 1 Blocksize is invalid.
548 * July 1, 1997 - Andrew E. Mileski
549 * Written, tested, and released.
551 static int udf_set_blocksize(struct super_block *sb, int bsize)
553 if (!sb_min_blocksize(sb, bsize)) {
554 udf_debug("Bad block size (%d)\n", bsize);
555 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
559 return sb->s_blocksize;
562 static int udf_vrs(struct super_block *sb, int silent)
564 struct volStructDesc *vsd = NULL;
567 struct buffer_head *bh = NULL;
571 struct udf_sb_info *sbi;
573 /* Block size must be a multiple of 512 */
574 if (sb->s_blocksize & 511)
578 if (sb->s_blocksize < sizeof(struct volStructDesc))
579 sectorsize = sizeof(struct volStructDesc);
581 sectorsize = sb->s_blocksize;
583 sector += (sbi->s_session << sb->s_blocksize_bits);
585 udf_debug("Starting at sector %u (%ld byte sectors)\n",
586 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
587 /* Process the sequence (if applicable) */
588 for (; !nsr02 && !nsr03; sector += sectorsize) {
590 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
594 /* Look for ISO descriptors */
595 vsd = (struct volStructDesc *)(bh->b_data +
596 (sector & (sb->s_blocksize - 1)));
598 if (vsd->stdIdent[0] == 0) {
601 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
604 switch (vsd->structType) {
606 udf_debug("ISO9660 Boot Record found\n");
609 udf_debug("ISO9660 Primary Volume Descriptor "
613 udf_debug("ISO9660 Supplementary Volume "
614 "Descriptor found\n");
617 udf_debug("ISO9660 Volume Partition Descriptor "
621 udf_debug("ISO9660 Volume Descriptor Set "
622 "Terminator found\n");
625 udf_debug("ISO9660 VRS (%u) found\n",
629 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
632 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
636 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
639 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
649 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
659 * Find an anchor volume descriptor.
662 * sb Pointer to _locked_ superblock.
663 * lastblock Last block on media.
666 * <return> 1 if not found, 0 if ok
669 * July 1, 1997 - Andrew E. Mileski
670 * Written, tested, and released.
672 static void udf_find_anchor(struct super_block *sb)
675 struct buffer_head *bh = NULL;
679 struct udf_sb_info *sbi;
682 lastblock = sbi->s_last_block;
685 int varlastblock = udf_variable_to_fixed(lastblock);
686 int last[] = { lastblock, lastblock - 2,
687 lastblock - 150, lastblock - 152,
688 varlastblock, varlastblock - 2,
689 varlastblock - 150, varlastblock - 152 };
693 /* Search for an anchor volume descriptor pointer */
695 /* according to spec, anchor is in either:
699 * however, if the disc isn't closed, it could be 512 */
701 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
702 ident = location = 0;
704 bh = sb_bread(sb, last[i]);
706 tag *t = (tag *)bh->b_data;
707 ident = le16_to_cpu(t->tagIdent);
708 location = le32_to_cpu(t->tagLocation);
713 if (ident == TAG_IDENT_AVDP) {
714 if (location == last[i] - sbi->s_session) {
715 lastblock = last[i] - sbi->s_session;
716 sbi->s_anchor[0] = lastblock;
717 sbi->s_anchor[1] = lastblock - 256;
718 } else if (location ==
719 udf_variable_to_fixed(last[i]) -
721 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
723 udf_variable_to_fixed(last[i]) -
725 sbi->s_anchor[0] = lastblock;
726 sbi->s_anchor[1] = lastblock - 256 -
729 udf_debug("Anchor found at block %d, "
730 "location mismatch %d.\n",
733 } else if (ident == TAG_IDENT_FE ||
734 ident == TAG_IDENT_EFE) {
736 sbi->s_anchor[3] = 512;
738 ident = location = 0;
739 if (last[i] >= 256) {
740 bh = sb_bread(sb, last[i] - 256);
742 tag *t = (tag *)bh->b_data;
745 location = le32_to_cpu(
751 if (ident == TAG_IDENT_AVDP &&
752 location == last[i] - 256 -
755 sbi->s_anchor[1] = last[i] - 256;
757 ident = location = 0;
758 if (last[i] >= 312 + sbi->s_session) {
767 location = le32_to_cpu(
773 if (ident == TAG_IDENT_AVDP &&
774 location == udf_variable_to_fixed(last[i]) - 256) {
777 lastblock = udf_variable_to_fixed(last[i]);
778 sbi->s_anchor[1] = lastblock - 256;
786 /* We haven't found the lastblock. check 312 */
787 bh = sb_bread(sb, 312 + sbi->s_session);
789 tag *t = (tag *)bh->b_data;
790 ident = le16_to_cpu(t->tagIdent);
791 location = le32_to_cpu(t->tagLocation);
794 if (ident == TAG_IDENT_AVDP && location == 256)
795 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
799 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
800 if (sbi->s_anchor[i]) {
801 bh = udf_read_tagged(sb, sbi->s_anchor[i],
802 sbi->s_anchor[i], &ident);
804 sbi->s_anchor[i] = 0;
807 if ((ident != TAG_IDENT_AVDP) &&
808 (i || (ident != TAG_IDENT_FE &&
809 ident != TAG_IDENT_EFE)))
810 sbi->s_anchor[i] = 0;
815 sbi->s_last_block = lastblock;
818 static int udf_find_fileset(struct super_block *sb,
819 kernel_lb_addr *fileset,
820 kernel_lb_addr *root)
822 struct buffer_head *bh = NULL;
825 struct udf_sb_info *sbi;
827 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
828 fileset->partitionReferenceNum != 0xFFFF) {
829 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
833 } else if (ident != TAG_IDENT_FSD) {
842 /* Search backwards through the partitions */
843 kernel_lb_addr newfileset;
845 /* --> cvg: FIXME - is it reasonable? */
848 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
849 (newfileset.partitionReferenceNum != 0xFFFF &&
850 fileset->logicalBlockNum == 0xFFFFFFFF &&
851 fileset->partitionReferenceNum == 0xFFFF);
852 newfileset.partitionReferenceNum--) {
853 lastblock = sbi->s_partmaps
854 [newfileset.partitionReferenceNum]
856 newfileset.logicalBlockNum = 0;
859 bh = udf_read_ptagged(sb, newfileset, 0,
862 newfileset.logicalBlockNum++;
869 struct spaceBitmapDesc *sp;
870 sp = (struct spaceBitmapDesc *)
872 newfileset.logicalBlockNum += 1 +
873 ((le32_to_cpu(sp->numOfBytes) +
874 sizeof(struct spaceBitmapDesc)
875 - 1) >> sb->s_blocksize_bits);
880 *fileset = newfileset;
883 newfileset.logicalBlockNum++;
888 } while (newfileset.logicalBlockNum < lastblock &&
889 fileset->logicalBlockNum == 0xFFFFFFFF &&
890 fileset->partitionReferenceNum == 0xFFFF);
894 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
895 fileset->partitionReferenceNum != 0xFFFF) && bh) {
896 udf_debug("Fileset at block=%d, partition=%d\n",
897 fileset->logicalBlockNum,
898 fileset->partitionReferenceNum);
900 sbi->s_partition = fileset->partitionReferenceNum;
901 udf_load_fileset(sb, bh, root);
908 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
910 struct primaryVolDesc *pvoldesc;
916 pvoldesc = (struct primaryVolDesc *)bh->b_data;
918 if (udf_stamp_to_time(&recording, &recording_usec,
919 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
921 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
922 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
924 recording, recording_usec,
925 ts.year, ts.month, ts.day, ts.hour,
926 ts.minute, ts.typeAndTimezone);
927 UDF_SB(sb)->s_record_time.tv_sec = recording;
928 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
931 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
932 if (udf_CS0toUTF8(&outstr, &instr)) {
933 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
934 outstr.u_len > 31 ? 31 : outstr.u_len);
935 udf_debug("volIdent[] = '%s'\n",
936 UDF_SB(sb)->s_volume_ident);
939 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
940 if (udf_CS0toUTF8(&outstr, &instr))
941 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
944 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
945 kernel_lb_addr *root)
947 struct fileSetDesc *fset;
949 fset = (struct fileSetDesc *)bh->b_data;
951 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
953 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
955 udf_debug("Rootdir at block=%d, partition=%d\n",
956 root->logicalBlockNum, root->partitionReferenceNum);
959 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
961 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
962 return (map->s_partition_len +
963 (sizeof(struct spaceBitmapDesc) << 3) +
964 (sb->s_blocksize * 8) - 1) /
965 (sb->s_blocksize * 8);
968 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
970 struct udf_bitmap *bitmap;
974 nr_groups = udf_compute_nr_groups(sb, index);
975 size = sizeof(struct udf_bitmap) +
976 (sizeof(struct buffer_head *) * nr_groups);
978 if (size <= PAGE_SIZE)
979 bitmap = kmalloc(size, GFP_KERNEL);
981 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
983 if (bitmap == NULL) {
984 udf_error(sb, __FUNCTION__,
985 "Unable to allocate space for bitmap "
986 "and %d buffer_head pointers", nr_groups);
990 memset(bitmap, 0x00, size);
991 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
992 bitmap->s_nr_groups = nr_groups;
996 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
998 struct partitionDesc *p;
1000 struct udf_part_map *map;
1001 struct udf_sb_info *sbi;
1003 p = (struct partitionDesc *)bh->b_data;
1006 for (i = 0; i < sbi->s_partitions; i++) {
1007 map = &sbi->s_partmaps[i];
1008 udf_debug("Searching map: (%d == %d)\n",
1009 map->s_partition_num,
1010 le16_to_cpu(p->partitionNumber));
1011 if (map->s_partition_num ==
1012 le16_to_cpu(p->partitionNumber)) {
1013 map->s_partition_len =
1014 le32_to_cpu(p->partitionLength); /* blocks */
1015 map->s_partition_root =
1016 le32_to_cpu(p->partitionStartingLocation);
1017 if (p->accessType ==
1018 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1019 map->s_partition_flags |=
1020 UDF_PART_FLAG_READ_ONLY;
1021 if (p->accessType ==
1022 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1023 map->s_partition_flags |=
1024 UDF_PART_FLAG_WRITE_ONCE;
1025 if (p->accessType ==
1026 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1027 map->s_partition_flags |=
1028 UDF_PART_FLAG_REWRITABLE;
1029 if (p->accessType ==
1030 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1031 map->s_partition_flags |=
1032 UDF_PART_FLAG_OVERWRITABLE;
1034 if (!strcmp(p->partitionContents.ident,
1035 PD_PARTITION_CONTENTS_NSR02) ||
1036 !strcmp(p->partitionContents.ident,
1037 PD_PARTITION_CONTENTS_NSR03)) {
1038 struct partitionHeaderDesc *phd;
1040 phd = (struct partitionHeaderDesc *)
1041 (p->partitionContentsUse);
1042 if (phd->unallocSpaceTable.extLength) {
1043 kernel_lb_addr loc = {
1044 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1045 .partitionReferenceNum = i,
1048 map->s_uspace.s_table =
1050 if (!map->s_uspace.s_table) {
1051 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
1054 map->s_partition_flags |=
1055 UDF_PART_FLAG_UNALLOC_TABLE;
1056 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1057 i, map->s_uspace.s_table->i_ino);
1059 if (phd->unallocSpaceBitmap.extLength) {
1060 struct udf_bitmap *bitmap =
1061 udf_sb_alloc_bitmap(sb, i);
1062 map->s_uspace.s_bitmap = bitmap;
1063 if (bitmap != NULL) {
1064 bitmap->s_extLength =
1065 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
1066 bitmap->s_extPosition =
1067 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
1068 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1069 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1070 i, bitmap->s_extPosition);
1073 if (phd->partitionIntegrityTable.extLength)
1074 udf_debug("partitionIntegrityTable (part %d)\n", i);
1075 if (phd->freedSpaceTable.extLength) {
1076 kernel_lb_addr loc = {
1077 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1078 .partitionReferenceNum = i,
1081 map->s_fspace.s_table =
1083 if (!map->s_fspace.s_table) {
1084 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
1087 map->s_partition_flags |=
1088 UDF_PART_FLAG_FREED_TABLE;
1089 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1090 i, map->s_fspace.s_table->i_ino);
1092 if (phd->freedSpaceBitmap.extLength) {
1093 struct udf_bitmap *bitmap =
1094 udf_sb_alloc_bitmap(sb, i);
1095 map->s_fspace.s_bitmap = bitmap;
1096 if (bitmap != NULL) {
1097 bitmap->s_extLength =
1098 le32_to_cpu(phd->freedSpaceBitmap.extLength);
1099 bitmap->s_extPosition =
1100 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
1101 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1102 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1103 i, bitmap->s_extPosition);
1110 if (i == sbi->s_partitions)
1111 udf_debug("Partition (%d) not found in partition map\n",
1112 le16_to_cpu(p->partitionNumber));
1114 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1115 "block length %d\n",
1116 le16_to_cpu(p->partitionNumber), i,
1117 map->s_partition_type,
1118 map->s_partition_root,
1119 map->s_partition_len);
1123 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1124 kernel_lb_addr *fileset)
1126 struct logicalVolDesc *lvd;
1129 struct udf_sb_info *sbi = UDF_SB(sb);
1130 struct genericPartitionMap *gpm;
1132 lvd = (struct logicalVolDesc *)bh->b_data;
1134 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1138 for (i = 0, offset = 0;
1139 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1140 i++, offset += gpm->partitionMapLength) {
1141 struct udf_part_map *map = &sbi->s_partmaps[i];
1142 gpm = (struct genericPartitionMap *)
1143 &(lvd->partitionMaps[offset]);
1144 type = gpm->partitionMapType;
1146 struct genericPartitionMap1 *gpm1 =
1147 (struct genericPartitionMap1 *)gpm;
1148 map->s_partition_type = UDF_TYPE1_MAP15;
1149 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1150 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1151 map->s_partition_func = NULL;
1152 } else if (type == 2) {
1153 struct udfPartitionMap2 *upm2 =
1154 (struct udfPartitionMap2 *)gpm;
1155 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1156 strlen(UDF_ID_VIRTUAL))) {
1158 le16_to_cpu(((__le16 *)upm2->partIdent.
1160 if (suf == 0x0150) {
1161 map->s_partition_type =
1163 map->s_partition_func =
1164 udf_get_pblock_virt15;
1165 } else if (suf == 0x0200) {
1166 map->s_partition_type =
1168 map->s_partition_func =
1169 udf_get_pblock_virt20;
1171 } else if (!strncmp(upm2->partIdent.ident,
1173 strlen(UDF_ID_SPARABLE))) {
1176 struct sparingTable *st;
1177 struct sparablePartitionMap *spm =
1178 (struct sparablePartitionMap *)gpm;
1180 map->s_partition_type = UDF_SPARABLE_MAP15;
1181 map->s_type_specific.s_sparing.s_packet_len =
1182 le16_to_cpu(spm->packetLength);
1183 for (j = 0; j < spm->numSparingTables; j++) {
1184 struct buffer_head *bh2;
1187 spm->locSparingTable[j]);
1188 bh2 = udf_read_tagged(sb, loc, loc,
1190 map->s_type_specific.s_sparing.
1191 s_spar_map[j] = bh2;
1194 st = (struct sparingTable *)
1196 if (ident != 0 || strncmp(
1197 st->sparingIdent.ident,
1199 strlen(UDF_ID_SPARING))) {
1201 map->s_type_specific.
1208 map->s_partition_func = udf_get_pblock_spar15;
1210 udf_debug("Unknown ident: %s\n",
1211 upm2->partIdent.ident);
1214 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1215 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1217 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1218 i, map->s_partition_num, type,
1219 map->s_volumeseqnum);
1223 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1225 *fileset = lelb_to_cpu(la->extLocation);
1226 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1227 "partition=%d\n", fileset->logicalBlockNum,
1228 fileset->partitionReferenceNum);
1230 if (lvd->integritySeqExt.extLength)
1231 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1237 * udf_load_logicalvolint
1240 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1242 struct buffer_head *bh = NULL;
1244 struct udf_sb_info *sbi = UDF_SB(sb);
1245 struct logicalVolIntegrityDesc *lvid;
1247 while (loc.extLength > 0 &&
1248 (bh = udf_read_tagged(sb, loc.extLocation,
1249 loc.extLocation, &ident)) &&
1250 ident == TAG_IDENT_LVID) {
1251 sbi->s_lvid_bh = bh;
1252 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1254 if (lvid->nextIntegrityExt.extLength)
1255 udf_load_logicalvolint(sb,
1256 leea_to_cpu(lvid->nextIntegrityExt));
1258 if (sbi->s_lvid_bh != bh)
1260 loc.extLength -= sb->s_blocksize;
1263 if (sbi->s_lvid_bh != bh)
1268 * udf_process_sequence
1271 * Process a main/reserve volume descriptor sequence.
1274 * sb Pointer to _locked_ superblock.
1275 * block First block of first extent of the sequence.
1276 * lastblock Lastblock of first extent of the sequence.
1279 * July 1, 1997 - Andrew E. Mileski
1280 * Written, tested, and released.
1282 static int udf_process_sequence(struct super_block *sb, long block,
1283 long lastblock, kernel_lb_addr *fileset)
1285 struct buffer_head *bh = NULL;
1286 struct udf_vds_record vds[VDS_POS_LENGTH];
1287 struct udf_vds_record *curr;
1288 struct generic_desc *gd;
1289 struct volDescPtr *vdp;
1294 long next_s = 0, next_e = 0;
1296 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1298 /* Read the main descriptor sequence */
1299 for (; (!done && block <= lastblock); block++) {
1301 bh = udf_read_tagged(sb, block, block, &ident);
1305 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1306 gd = (struct generic_desc *)bh->b_data;
1307 vdsn = le32_to_cpu(gd->volDescSeqNum);
1309 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1310 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1311 if (vdsn >= curr->volDescSeqNum) {
1312 curr->volDescSeqNum = vdsn;
1313 curr->block = block;
1316 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1317 curr = &vds[VDS_POS_VOL_DESC_PTR];
1318 if (vdsn >= curr->volDescSeqNum) {
1319 curr->volDescSeqNum = vdsn;
1320 curr->block = block;
1322 vdp = (struct volDescPtr *)bh->b_data;
1323 next_s = le32_to_cpu(
1324 vdp->nextVolDescSeqExt.extLocation);
1325 next_e = le32_to_cpu(
1326 vdp->nextVolDescSeqExt.extLength);
1327 next_e = next_e >> sb->s_blocksize_bits;
1331 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1332 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1333 if (vdsn >= curr->volDescSeqNum) {
1334 curr->volDescSeqNum = vdsn;
1335 curr->block = block;
1338 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1339 curr = &vds[VDS_POS_PARTITION_DESC];
1341 curr->block = block;
1343 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1344 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1345 if (vdsn >= curr->volDescSeqNum) {
1346 curr->volDescSeqNum = vdsn;
1347 curr->block = block;
1350 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1351 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1352 if (vdsn >= curr->volDescSeqNum) {
1353 curr->volDescSeqNum = vdsn;
1354 curr->block = block;
1357 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1358 vds[VDS_POS_TERMINATING_DESC].block = block;
1362 next_s = next_e = 0;
1369 for (i = 0; i < VDS_POS_LENGTH; i++) {
1371 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1374 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1375 udf_load_pvoldesc(sb, bh);
1376 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1377 if (udf_load_logicalvol(sb, bh, fileset)) {
1381 } else if (i == VDS_POS_PARTITION_DESC) {
1382 struct buffer_head *bh2 = NULL;
1383 if (udf_load_partdesc(sb, bh)) {
1387 for (j = vds[i].block + 1;
1388 j < vds[VDS_POS_TERMINATING_DESC].block;
1390 bh2 = udf_read_tagged(sb, j, j, &ident);
1391 gd = (struct generic_desc *)bh2->b_data;
1392 if (ident == TAG_IDENT_PD)
1393 if (udf_load_partdesc(sb,
1412 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1417 udf_debug("Validity check skipped because of novrs option\n");
1420 /* Check that it is NSR02 compliant */
1421 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1423 block = udf_vrs(sb, silent);
1425 struct udf_sb_info *sbi = UDF_SB(sb);
1426 udf_debug("Failed to read byte 32768. Assuming open "
1427 "disc. Skipping validity check\n");
1428 if (!sbi->s_last_block)
1429 sbi->s_last_block = udf_get_last_block(sb);
1436 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1438 struct anchorVolDescPtr *anchor;
1440 struct buffer_head *bh;
1441 long main_s, main_e, reserve_s, reserve_e;
1443 struct udf_sb_info *sbi;
1449 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1450 if (!sbi->s_anchor[i])
1452 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1457 anchor = (struct anchorVolDescPtr *)bh->b_data;
1459 /* Locate the main sequence */
1460 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1461 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1462 main_e = main_e >> sb->s_blocksize_bits;
1465 /* Locate the reserve sequence */
1466 reserve_s = le32_to_cpu(
1467 anchor->reserveVolDescSeqExt.extLocation);
1468 reserve_e = le32_to_cpu(
1469 anchor->reserveVolDescSeqExt.extLength);
1470 reserve_e = reserve_e >> sb->s_blocksize_bits;
1471 reserve_e += reserve_s;
1475 /* Process the main & reserve sequences */
1476 /* responsible for finding the PartitionDesc(s) */
1477 if (!(udf_process_sequence(sb, main_s, main_e,
1479 udf_process_sequence(sb, reserve_s, reserve_e,
1484 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1485 udf_debug("No Anchor block found\n");
1488 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1490 for (i = 0; i < sbi->s_partitions; i++) {
1491 kernel_lb_addr uninitialized_var(ino);
1492 struct udf_part_map *map = &sbi->s_partmaps[i];
1493 switch (map->s_partition_type) {
1494 case UDF_VIRTUAL_MAP15:
1495 case UDF_VIRTUAL_MAP20:
1496 if (!sbi->s_last_block) {
1497 sbi->s_last_block = udf_get_last_block(sb);
1498 udf_find_anchor(sb);
1501 if (!sbi->s_last_block) {
1502 udf_debug("Unable to determine Lastblock (For "
1503 "Virtual Partition)\n");
1507 for (j = 0; j < sbi->s_partitions; j++) {
1508 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1510 map->s_volumeseqnum ==
1511 map2->s_volumeseqnum &&
1512 map->s_partition_num ==
1513 map2->s_partition_num) {
1514 ino.partitionReferenceNum = j;
1515 ino.logicalBlockNum =
1517 map2->s_partition_root;
1522 if (j == sbi->s_partitions)
1525 sbi->s_vat_inode = udf_iget(sb, ino);
1526 if (!sbi->s_vat_inode)
1529 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1530 map->s_type_specific.s_virtual.s_start_offset =
1531 udf_ext0_offset(sbi->s_vat_inode);
1532 map->s_type_specific.s_virtual.s_num_entries =
1533 (sbi->s_vat_inode->i_size - 36) >> 2;
1534 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1536 struct virtualAllocationTable20 *vat20;
1538 pos = udf_block_map(sbi->s_vat_inode, 0);
1539 bh = sb_bread(sb, pos);
1542 vat20 = (struct virtualAllocationTable20 *)
1544 udf_ext0_offset(sbi->s_vat_inode);
1545 map->s_type_specific.s_virtual.s_start_offset =
1546 le16_to_cpu(vat20->lengthHeader) +
1547 udf_ext0_offset(sbi->s_vat_inode);
1548 map->s_type_specific.s_virtual.s_num_entries =
1549 (sbi->s_vat_inode->i_size -
1550 map->s_type_specific.s_virtual.
1551 s_start_offset) >> 2;
1554 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1555 map->s_partition_len =
1556 sbi->s_partmaps[ino.partitionReferenceNum].
1563 static void udf_open_lvid(struct super_block *sb)
1565 struct udf_sb_info *sbi = UDF_SB(sb);
1566 struct buffer_head *bh = sbi->s_lvid_bh;
1568 kernel_timestamp cpu_time;
1569 struct logicalVolIntegrityDesc *lvid =
1570 (struct logicalVolIntegrityDesc *)bh->b_data;
1571 struct logicalVolIntegrityDescImpUse *lvidiu =
1574 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1575 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1576 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1577 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1578 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1580 lvid->descTag.descCRC = cpu_to_le16(
1581 udf_crc((char *)lvid + sizeof(tag),
1582 le16_to_cpu(lvid->descTag.descCRCLength),
1585 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1586 mark_buffer_dirty(bh);
1590 static void udf_close_lvid(struct super_block *sb)
1592 kernel_timestamp cpu_time;
1593 struct udf_sb_info *sbi = UDF_SB(sb);
1594 struct buffer_head *bh = sbi->s_lvid_bh;
1595 struct logicalVolIntegrityDesc *lvid;
1600 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1602 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1603 struct logicalVolIntegrityDescImpUse *lvidiu =
1605 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1606 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1607 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1608 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1609 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1610 lvidiu->maxUDFWriteRev =
1611 cpu_to_le16(UDF_MAX_WRITE_VERSION);
1612 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1613 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1614 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1615 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1616 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1618 lvid->descTag.descCRC = cpu_to_le16(
1619 udf_crc((char *)lvid + sizeof(tag),
1620 le16_to_cpu(lvid->descTag.descCRCLength),
1623 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1624 mark_buffer_dirty(bh);
1628 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1631 int nr_groups = bitmap->s_nr_groups;
1632 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1635 for (i = 0; i < nr_groups; i++)
1636 if (bitmap->s_block_bitmap[i])
1637 brelse(bitmap->s_block_bitmap[i]);
1639 if (size <= PAGE_SIZE)
1649 * Complete the specified super block.
1652 * sb Pointer to superblock to complete - never NULL.
1653 * sb->s_dev Device to read suberblock from.
1654 * options Pointer to mount options.
1655 * silent Silent flag.
1658 * July 1, 1997 - Andrew E. Mileski
1659 * Written, tested, and released.
1661 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1664 struct inode *inode = NULL;
1665 struct udf_options uopt;
1666 kernel_lb_addr rootdir, fileset;
1667 struct udf_sb_info *sbi;
1669 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1674 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1678 sb->s_fs_info = sbi;
1680 mutex_init(&sbi->s_alloc_mutex);
1682 if (!udf_parse_options((char *)options, &uopt))
1685 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1686 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1687 udf_error(sb, "udf_read_super",
1688 "utf8 cannot be combined with iocharset\n");
1691 #ifdef CONFIG_UDF_NLS
1692 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1693 uopt.nls_map = load_nls_default();
1695 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1697 udf_debug("Using default NLS map\n");
1700 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1701 uopt.flags |= (1 << UDF_FLAG_UTF8);
1703 fileset.logicalBlockNum = 0xFFFFFFFF;
1704 fileset.partitionReferenceNum = 0xFFFF;
1706 sbi->s_flags = uopt.flags;
1707 sbi->s_uid = uopt.uid;
1708 sbi->s_gid = uopt.gid;
1709 sbi->s_umask = uopt.umask;
1710 sbi->s_nls_map = uopt.nls_map;
1712 /* Set the block size for all transfers */
1713 if (!udf_set_blocksize(sb, uopt.blocksize))
1716 if (uopt.session == 0xFFFFFFFF)
1717 sbi->s_session = udf_get_last_session(sb);
1719 sbi->s_session = uopt.session;
1721 udf_debug("Multi-session=%d\n", sbi->s_session);
1723 sbi->s_last_block = uopt.lastblock;
1724 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1725 sbi->s_anchor[2] = uopt.anchor;
1726 sbi->s_anchor[3] = 256;
1728 if (udf_check_valid(sb, uopt.novrs, silent)) {
1729 /* read volume recognition sequences */
1730 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1734 udf_find_anchor(sb);
1736 /* Fill in the rest of the superblock */
1737 sb->s_op = &udf_sb_ops;
1740 sb->s_magic = UDF_SUPER_MAGIC;
1741 sb->s_time_gran = 1000;
1743 if (udf_load_partition(sb, &fileset)) {
1744 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1748 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1750 if (sbi->s_lvid_bh) {
1751 struct logicalVolIntegrityDescImpUse *lvidiu =
1753 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1754 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1755 /* uint16_t maxUDFWriteRev =
1756 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1758 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1759 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1761 le16_to_cpu(lvidiu->minUDFReadRev),
1762 UDF_MAX_READ_VERSION);
1764 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1765 sb->s_flags |= MS_RDONLY;
1767 sbi->s_udfrev = minUDFWriteRev;
1769 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1770 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1771 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1772 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1775 if (!sbi->s_partitions) {
1776 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1780 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1781 UDF_PART_FLAG_READ_ONLY) {
1782 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1783 "forcing readonly mount\n");
1784 sb->s_flags |= MS_RDONLY;
1787 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1788 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1793 kernel_timestamp ts;
1794 udf_time_to_stamp(&ts, sbi->s_record_time);
1795 udf_info("UDF: Mounting volume '%s', "
1796 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1797 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1798 ts.hour, ts.minute, ts.typeAndTimezone);
1800 if (!(sb->s_flags & MS_RDONLY))
1803 /* Assign the root inode */
1804 /* assign inodes by physical block number */
1805 /* perhaps it's not extensible enough, but for now ... */
1806 inode = udf_iget(sb, rootdir);
1808 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1810 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1814 /* Allocate a dentry for the root inode */
1815 sb->s_root = d_alloc_root(inode);
1817 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1821 sb->s_maxbytes = MAX_LFS_FILESIZE;
1825 if (sbi->s_vat_inode)
1826 iput(sbi->s_vat_inode);
1827 if (sbi->s_partitions) {
1828 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1829 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1830 iput(map->s_uspace.s_table);
1831 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1832 iput(map->s_fspace.s_table);
1833 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1834 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1835 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1836 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1837 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1838 for (i = 0; i < 4; i++)
1839 brelse(map->s_type_specific.s_sparing.
1842 #ifdef CONFIG_UDF_NLS
1843 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1844 unload_nls(sbi->s_nls_map);
1846 if (!(sb->s_flags & MS_RDONLY))
1848 brelse(sbi->s_lvid_bh);
1850 kfree(sbi->s_partmaps);
1852 sb->s_fs_info = NULL;
1857 void udf_error(struct super_block *sb, const char *function,
1858 const char *fmt, ...)
1862 if (!(sb->s_flags & MS_RDONLY)) {
1866 va_start(args, fmt);
1867 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1869 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1870 sb->s_id, function, error_buf);
1873 void udf_warning(struct super_block *sb, const char *function,
1874 const char *fmt, ...)
1878 va_start(args, fmt);
1879 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1881 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1882 sb->s_id, function, error_buf);
1889 * Prepare for destruction of the superblock.
1892 * Called before the filesystem is unmounted.
1895 * July 1, 1997 - Andrew E. Mileski
1896 * Written, tested, and released.
1898 static void udf_put_super(struct super_block *sb)
1901 struct udf_sb_info *sbi;
1904 if (sbi->s_vat_inode)
1905 iput(sbi->s_vat_inode);
1906 if (sbi->s_partitions) {
1907 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1908 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1909 iput(map->s_uspace.s_table);
1910 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1911 iput(map->s_fspace.s_table);
1912 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1913 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1914 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1915 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1916 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1917 for (i = 0; i < 4; i++)
1918 brelse(map->s_type_specific.s_sparing.
1921 #ifdef CONFIG_UDF_NLS
1922 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1923 unload_nls(sbi->s_nls_map);
1925 if (!(sb->s_flags & MS_RDONLY))
1927 brelse(sbi->s_lvid_bh);
1928 kfree(sbi->s_partmaps);
1929 kfree(sb->s_fs_info);
1930 sb->s_fs_info = NULL;
1937 * Return info about the filesystem.
1940 * Called by sys_statfs()
1943 * July 1, 1997 - Andrew E. Mileski
1944 * Written, tested, and released.
1946 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1948 struct super_block *sb = dentry->d_sb;
1949 struct udf_sb_info *sbi = UDF_SB(sb);
1950 struct logicalVolIntegrityDescImpUse *lvidiu;
1952 if (sbi->s_lvid_bh != NULL)
1953 lvidiu = udf_sb_lvidiu(sbi);
1957 buf->f_type = UDF_SUPER_MAGIC;
1958 buf->f_bsize = sb->s_blocksize;
1959 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1960 buf->f_bfree = udf_count_free(sb);
1961 buf->f_bavail = buf->f_bfree;
1962 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1963 le32_to_cpu(lvidiu->numDirs)) : 0)
1965 buf->f_ffree = buf->f_bfree;
1966 /* __kernel_fsid_t f_fsid */
1967 buf->f_namelen = UDF_NAME_LEN - 2;
1972 static unsigned char udf_bitmap_lookup[16] = {
1973 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1976 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1977 struct udf_bitmap *bitmap)
1979 struct buffer_head *bh = NULL;
1980 unsigned int accum = 0;
1982 int block = 0, newblock;
1988 struct spaceBitmapDesc *bm;
1992 loc.logicalBlockNum = bitmap->s_extPosition;
1993 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1994 bh = udf_read_ptagged(sb, loc, 0, &ident);
1997 printk(KERN_ERR "udf: udf_count_free failed\n");
1999 } else if (ident != TAG_IDENT_SBD) {
2001 printk(KERN_ERR "udf: udf_count_free failed\n");
2005 bm = (struct spaceBitmapDesc *)bh->b_data;
2006 bytes = le32_to_cpu(bm->numOfBytes);
2007 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2008 ptr = (uint8_t *)bh->b_data;
2011 while ((bytes > 0) && (index < sb->s_blocksize)) {
2013 accum += udf_bitmap_lookup[value & 0x0f];
2014 accum += udf_bitmap_lookup[value >> 4];
2020 newblock = udf_get_lb_pblock(sb, loc, ++block);
2021 bh = udf_tread(sb, newblock);
2023 udf_debug("read failed\n");
2027 ptr = (uint8_t *)bh->b_data;
2038 static unsigned int udf_count_free_table(struct super_block *sb,
2039 struct inode *table)
2041 unsigned int accum = 0;
2043 kernel_lb_addr eloc;
2045 struct extent_position epos;
2049 epos.block = UDF_I(table)->i_location;
2050 epos.offset = sizeof(struct unallocSpaceEntry);
2053 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2054 accum += (elen >> table->i_sb->s_blocksize_bits);
2063 static unsigned int udf_count_free(struct super_block *sb)
2065 unsigned int accum = 0;
2066 struct udf_sb_info *sbi;
2067 struct udf_part_map *map;
2070 if (sbi->s_lvid_bh) {
2071 struct logicalVolIntegrityDesc *lvid =
2072 (struct logicalVolIntegrityDesc *)
2073 sbi->s_lvid_bh->b_data;
2074 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2075 accum = le32_to_cpu(
2076 lvid->freeSpaceTable[sbi->s_partition]);
2077 if (accum == 0xFFFFFFFF)
2085 map = &sbi->s_partmaps[sbi->s_partition];
2086 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2087 accum += udf_count_free_bitmap(sb,
2088 map->s_uspace.s_bitmap);
2090 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2091 accum += udf_count_free_bitmap(sb,
2092 map->s_fspace.s_bitmap);
2097 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2098 accum += udf_count_free_table(sb,
2099 map->s_uspace.s_table);
2101 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2102 accum += udf_count_free_table(sb,
2103 map->s_fspace.s_table);