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 <asm/byteorder.h>
57 #include <linux/udf_fs.h>
61 #include <linux/init.h>
62 #include <asm/uaccess.h>
64 #define VDS_POS_PRIMARY_VOL_DESC 0
65 #define VDS_POS_UNALLOC_SPACE_DESC 1
66 #define VDS_POS_LOGICAL_VOL_DESC 2
67 #define VDS_POS_PARTITION_DESC 3
68 #define VDS_POS_IMP_USE_VOL_DESC 4
69 #define VDS_POS_VOL_DESC_PTR 5
70 #define VDS_POS_TERMINATING_DESC 6
71 #define VDS_POS_LENGTH 7
73 static char error_buf[1024];
75 /* These are the "meat" - everything else is stuffing */
76 static int udf_fill_super(struct super_block *, void *, int);
77 static void udf_put_super(struct super_block *);
78 static void udf_write_super(struct super_block *);
79 static int udf_remount_fs(struct super_block *, int *, char *);
80 static int udf_check_valid(struct super_block *, int, int);
81 static int udf_vrs(struct super_block *sb, int silent);
82 static int udf_load_partition(struct super_block *, kernel_lb_addr *);
83 static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
85 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
86 static void udf_find_anchor(struct super_block *);
87 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
89 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
90 static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 static int udf_load_partdesc(struct super_block *, struct buffer_head *);
93 static void udf_open_lvid(struct super_block *);
94 static void udf_close_lvid(struct super_block *);
95 static unsigned int udf_count_free(struct super_block *);
96 static int udf_statfs(struct dentry *, struct kstatfs *);
98 /* UDF filesystem type */
99 static int udf_get_sb(struct file_system_type *fs_type,
100 int flags, const char *dev_name, void *data,
101 struct vfsmount *mnt)
103 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
106 static struct file_system_type udf_fstype = {
107 .owner = THIS_MODULE,
109 .get_sb = udf_get_sb,
110 .kill_sb = kill_block_super,
111 .fs_flags = FS_REQUIRES_DEV,
114 static struct kmem_cache *udf_inode_cachep;
116 static struct inode *udf_alloc_inode(struct super_block *sb)
118 struct udf_inode_info *ei;
119 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
124 ei->i_lenExtents = 0;
125 ei->i_next_alloc_block = 0;
126 ei->i_next_alloc_goal = 0;
129 return &ei->vfs_inode;
132 static void udf_destroy_inode(struct inode *inode)
134 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
137 static void init_once(struct kmem_cache *cachep, void *foo)
139 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
141 ei->i_ext.i_data = NULL;
142 inode_init_once(&ei->vfs_inode);
145 static int init_inodecache(void)
147 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
148 sizeof(struct udf_inode_info),
149 0, (SLAB_RECLAIM_ACCOUNT |
152 if (!udf_inode_cachep)
157 static void destroy_inodecache(void)
159 kmem_cache_destroy(udf_inode_cachep);
162 /* Superblock operations */
163 static const struct super_operations udf_sb_ops = {
164 .alloc_inode = udf_alloc_inode,
165 .destroy_inode = udf_destroy_inode,
166 .write_inode = udf_write_inode,
167 .delete_inode = udf_delete_inode,
168 .clear_inode = udf_clear_inode,
169 .put_super = udf_put_super,
170 .write_super = udf_write_super,
171 .statfs = udf_statfs,
172 .remount_fs = udf_remount_fs,
177 unsigned int blocksize;
178 unsigned int session;
179 unsigned int lastblock;
182 unsigned short partition;
183 unsigned int fileset;
184 unsigned int rootdir;
189 struct nls_table *nls_map;
192 static int __init init_udf_fs(void)
196 err = init_inodecache();
199 err = register_filesystem(&udf_fstype);
206 destroy_inodecache();
212 static void __exit exit_udf_fs(void)
214 unregister_filesystem(&udf_fstype);
215 destroy_inodecache();
218 module_init(init_udf_fs)
219 module_exit(exit_udf_fs)
225 * Parse mount options.
228 * The following mount options are supported:
230 * gid= Set the default group.
231 * umask= Set the default umask.
232 * uid= Set the default user.
233 * bs= Set the block size.
234 * unhide Show otherwise hidden files.
235 * undelete Show deleted files in lists.
236 * adinicb Embed data in the inode (default)
237 * noadinicb Don't embed data in the inode
238 * shortad Use short ad's
239 * longad Use long ad's (default)
240 * nostrict Unset strict conformance
241 * iocharset= Set the NLS character set
243 * The remaining are for debugging and disaster recovery:
245 * novrs Skip volume sequence recognition
247 * The following expect a offset from 0.
249 * session= Set the CDROM session (default= last session)
250 * anchor= Override standard anchor location. (default= 256)
251 * volume= Override the VolumeDesc location. (unused)
252 * partition= Override the PartitionDesc location. (unused)
253 * lastblock= Set the last block of the filesystem/
255 * The following expect a offset from the partition root.
257 * fileset= Override the fileset block location. (unused)
258 * rootdir= Override the root directory location. (unused)
259 * WARNING: overriding the rootdir to a non-directory may
260 * yield highly unpredictable results.
263 * options Pointer to mount options string.
264 * uopts Pointer to mount options variable.
267 * <return> 1 Mount options parsed okay.
268 * <return> 0 Error parsing mount options.
271 * July 1, 1997 - Andrew E. Mileski
272 * Written, tested, and released.
276 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
277 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
278 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
279 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
280 Opt_rootdir, Opt_utf8, Opt_iocharset,
281 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
284 static match_table_t tokens = {
285 {Opt_novrs, "novrs"},
286 {Opt_nostrict, "nostrict"},
288 {Opt_unhide, "unhide"},
289 {Opt_undelete, "undelete"},
290 {Opt_noadinicb, "noadinicb"},
291 {Opt_adinicb, "adinicb"},
292 {Opt_shortad, "shortad"},
293 {Opt_longad, "longad"},
294 {Opt_uforget, "uid=forget"},
295 {Opt_uignore, "uid=ignore"},
296 {Opt_gforget, "gid=forget"},
297 {Opt_gignore, "gid=ignore"},
300 {Opt_umask, "umask=%o"},
301 {Opt_session, "session=%u"},
302 {Opt_lastblock, "lastblock=%u"},
303 {Opt_anchor, "anchor=%u"},
304 {Opt_volume, "volume=%u"},
305 {Opt_partition, "partition=%u"},
306 {Opt_fileset, "fileset=%u"},
307 {Opt_rootdir, "rootdir=%u"},
309 {Opt_iocharset, "iocharset=%s"},
313 static int udf_parse_options(char *options, struct udf_options *uopt)
319 uopt->blocksize = 2048;
320 uopt->partition = 0xFFFF;
321 uopt->session = 0xFFFFFFFF;
324 uopt->volume = 0xFFFFFFFF;
325 uopt->rootdir = 0xFFFFFFFF;
326 uopt->fileset = 0xFFFFFFFF;
327 uopt->nls_map = NULL;
332 while ((p = strsep(&options, ",")) != NULL) {
333 substring_t args[MAX_OPT_ARGS];
338 token = match_token(p, tokens, args);
343 if (match_int(&args[0], &option))
345 uopt->blocksize = option;
348 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
351 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
354 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
357 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
360 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
363 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
366 if (match_int(args, &option))
369 uopt->flags |= (1 << UDF_FLAG_GID_SET);
372 if (match_int(args, &option))
375 uopt->flags |= (1 << UDF_FLAG_UID_SET);
378 if (match_octal(args, &option))
380 uopt->umask = option;
383 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
386 if (match_int(args, &option))
388 uopt->session = option;
391 if (match_int(args, &option))
393 uopt->lastblock = option;
396 if (match_int(args, &option))
398 uopt->anchor = option;
401 if (match_int(args, &option))
403 uopt->volume = option;
406 if (match_int(args, &option))
408 uopt->partition = option;
411 if (match_int(args, &option))
413 uopt->fileset = option;
416 if (match_int(args, &option))
418 uopt->rootdir = option;
421 uopt->flags |= (1 << UDF_FLAG_UTF8);
423 #ifdef CONFIG_UDF_NLS
425 uopt->nls_map = load_nls(args[0].from);
426 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
430 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
433 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
436 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
439 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
442 printk(KERN_ERR "udf: bad mount option \"%s\" "
443 "or missing value\n", p);
450 void udf_write_super(struct super_block *sb)
454 if (!(sb->s_flags & MS_RDONLY))
461 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
463 struct udf_options uopt;
465 uopt.flags = UDF_SB(sb)->s_flags;
466 uopt.uid = UDF_SB(sb)->s_uid;
467 uopt.gid = UDF_SB(sb)->s_gid;
468 uopt.umask = UDF_SB(sb)->s_umask;
470 if (!udf_parse_options(options, &uopt))
473 UDF_SB(sb)->s_flags = uopt.flags;
474 UDF_SB(sb)->s_uid = uopt.uid;
475 UDF_SB(sb)->s_gid = uopt.gid;
476 UDF_SB(sb)->s_umask = uopt.umask;
478 if (UDF_SB_LVIDBH(sb)) {
479 int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
480 if (write_rev > UDF_MAX_WRITE_VERSION)
484 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
486 if (*flags & MS_RDONLY)
498 * Set the block size to be used in all transfers.
501 * To allow room for a DMA transfer, it is best to guess big when unsure.
502 * This routine picks 2048 bytes as the blocksize when guessing. This
503 * should be adequate until devices with larger block sizes become common.
505 * Note that the Linux kernel can currently only deal with blocksizes of
506 * 512, 1024, 2048, 4096, and 8192 bytes.
509 * sb Pointer to _locked_ superblock.
512 * sb->s_blocksize Blocksize.
513 * sb->s_blocksize_bits log2 of blocksize.
514 * <return> 0 Blocksize is valid.
515 * <return> 1 Blocksize is invalid.
518 * July 1, 1997 - Andrew E. Mileski
519 * Written, tested, and released.
521 static int udf_set_blocksize(struct super_block *sb, int bsize)
523 if (!sb_min_blocksize(sb, bsize)) {
524 udf_debug("Bad block size (%d)\n", bsize);
525 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
529 return sb->s_blocksize;
532 static int udf_vrs(struct super_block *sb, int silent)
534 struct volStructDesc *vsd = NULL;
537 struct buffer_head *bh = NULL;
542 /* Block size must be a multiple of 512 */
543 if (sb->s_blocksize & 511)
546 if (sb->s_blocksize < sizeof(struct volStructDesc))
547 sectorsize = sizeof(struct volStructDesc);
549 sectorsize = sb->s_blocksize;
551 sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
553 udf_debug("Starting at sector %u (%ld byte sectors)\n",
554 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
555 /* Process the sequence (if applicable) */
556 for (; !nsr02 && !nsr03; sector += sectorsize) {
558 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
562 /* Look for ISO descriptors */
563 vsd = (struct volStructDesc *)(bh->b_data +
564 (sector & (sb->s_blocksize - 1)));
566 if (vsd->stdIdent[0] == 0) {
569 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
572 switch (vsd->structType) {
574 udf_debug("ISO9660 Boot Record found\n");
577 udf_debug("ISO9660 Primary Volume Descriptor "
581 udf_debug("ISO9660 Supplementary Volume "
582 "Descriptor found\n");
585 udf_debug("ISO9660 Volume Partition Descriptor "
589 udf_debug("ISO9660 Volume Descriptor Set "
590 "Terminator found\n");
593 udf_debug("ISO9660 VRS (%u) found\n",
597 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
600 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
604 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
607 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
617 else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
627 * Find an anchor volume descriptor.
630 * sb Pointer to _locked_ superblock.
631 * lastblock Last block on media.
634 * <return> 1 if not found, 0 if ok
637 * July 1, 1997 - Andrew E. Mileski
638 * Written, tested, and released.
640 static void udf_find_anchor(struct super_block *sb)
642 int lastblock = UDF_SB_LASTBLOCK(sb);
643 struct buffer_head *bh = NULL;
649 int varlastblock = udf_variable_to_fixed(lastblock);
650 int last[] = { lastblock, lastblock - 2,
651 lastblock - 150, lastblock - 152,
652 varlastblock, varlastblock - 2,
653 varlastblock - 150, varlastblock - 152 };
657 /* Search for an anchor volume descriptor pointer */
659 /* according to spec, anchor is in either:
663 * however, if the disc isn't closed, it could be 512 */
665 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
666 ident = location = 0;
668 bh = sb_bread(sb, last[i]);
670 tag *t = (tag *)bh->b_data;
671 ident = le16_to_cpu(t->tagIdent);
672 location = le32_to_cpu(t->tagLocation);
677 if (ident == TAG_IDENT_AVDP) {
678 if (location == last[i] - UDF_SB_SESSION(sb)) {
679 lastblock = last[i] - UDF_SB_SESSION(sb);
680 UDF_SB_ANCHOR(sb)[0] = lastblock;
681 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
682 } else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb)) {
683 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
684 lastblock = udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb);
685 UDF_SB_ANCHOR(sb)[0] = lastblock;
686 UDF_SB_ANCHOR(sb)[1] = lastblock - 256 - UDF_SB_SESSION(sb);
688 udf_debug("Anchor found at block %d, location mismatch %d.\n",
691 } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) {
693 UDF_SB_ANCHOR(sb)[3] = 512;
695 ident = location = 0;
696 if (last[i] >= 256) {
697 bh = sb_bread(sb, last[i] - 256);
699 tag *t = (tag *)bh->b_data;
700 ident = le16_to_cpu(t->tagIdent);
701 location = le32_to_cpu(t->tagLocation);
706 if (ident == TAG_IDENT_AVDP &&
707 location == last[i] - 256 - UDF_SB_SESSION(sb)) {
709 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
711 ident = location = 0;
712 if (last[i] >= 312 + UDF_SB_SESSION(sb)) {
713 bh = sb_bread(sb, last[i] - 312 - UDF_SB_SESSION(sb));
715 tag *t = (tag *)bh->b_data;
716 ident = le16_to_cpu(t->tagIdent);
717 location = le32_to_cpu(t->tagLocation);
722 if (ident == TAG_IDENT_AVDP &&
723 location == udf_variable_to_fixed(last[i]) - 256) {
724 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
725 lastblock = udf_variable_to_fixed(last[i]);
726 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
734 /* We haven't found the lastblock. check 312 */
735 bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb));
737 tag *t = (tag *)bh->b_data;
738 ident = le16_to_cpu(t->tagIdent);
739 location = le32_to_cpu(t->tagLocation);
742 if (ident == TAG_IDENT_AVDP && location == 256)
743 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
747 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
748 if (UDF_SB_ANCHOR(sb)[i]) {
749 bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i],
750 UDF_SB_ANCHOR(sb)[i], &ident);
752 UDF_SB_ANCHOR(sb)[i] = 0;
755 if ((ident != TAG_IDENT_AVDP) &&
756 (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE)))
757 UDF_SB_ANCHOR(sb)[i] = 0;
762 UDF_SB_LASTBLOCK(sb) = lastblock;
765 static int udf_find_fileset(struct super_block *sb,
766 kernel_lb_addr *fileset,
767 kernel_lb_addr *root)
769 struct buffer_head *bh = NULL;
773 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
774 fileset->partitionReferenceNum != 0xFFFF) {
775 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
779 } else if (ident != TAG_IDENT_FSD) {
787 /* Search backwards through the partitions */
788 kernel_lb_addr newfileset;
790 /* --> cvg: FIXME - is it reasonable? */
793 for (newfileset.partitionReferenceNum = UDF_SB_NUMPARTS(sb) - 1;
794 (newfileset.partitionReferenceNum != 0xFFFF &&
795 fileset->logicalBlockNum == 0xFFFFFFFF &&
796 fileset->partitionReferenceNum == 0xFFFF);
797 newfileset.partitionReferenceNum--) {
798 lastblock = UDF_SB_PARTLEN(sb,
799 newfileset.partitionReferenceNum);
800 newfileset.logicalBlockNum = 0;
803 bh = udf_read_ptagged(sb, newfileset, 0,
806 newfileset.logicalBlockNum++;
813 struct spaceBitmapDesc *sp;
814 sp = (struct spaceBitmapDesc *)bh->b_data;
815 newfileset.logicalBlockNum += 1 +
816 ((le32_to_cpu(sp->numOfBytes) +
817 sizeof(struct spaceBitmapDesc) - 1)
818 >> sb->s_blocksize_bits);
823 *fileset = newfileset;
826 newfileset.logicalBlockNum++;
831 } while (newfileset.logicalBlockNum < lastblock &&
832 fileset->logicalBlockNum == 0xFFFFFFFF &&
833 fileset->partitionReferenceNum == 0xFFFF);
837 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
838 fileset->partitionReferenceNum != 0xFFFF) && bh) {
839 udf_debug("Fileset at block=%d, partition=%d\n",
840 fileset->logicalBlockNum,
841 fileset->partitionReferenceNum);
843 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
844 udf_load_fileset(sb, bh, root);
851 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
853 struct primaryVolDesc *pvoldesc;
859 pvoldesc = (struct primaryVolDesc *)bh->b_data;
861 if (udf_stamp_to_time(&recording, &recording_usec,
862 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
864 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
865 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
867 recording, recording_usec,
868 ts.year, ts.month, ts.day, ts.hour,
869 ts.minute, ts.typeAndTimezone);
870 UDF_SB_RECORDTIME(sb).tv_sec = recording;
871 UDF_SB_RECORDTIME(sb).tv_nsec = recording_usec * 1000;
874 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
875 if (udf_CS0toUTF8(&outstr, &instr)) {
876 strncpy(UDF_SB_VOLIDENT(sb), outstr.u_name,
877 outstr.u_len > 31 ? 31 : outstr.u_len);
878 udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
882 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
883 if (udf_CS0toUTF8(&outstr, &instr))
884 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
888 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
889 kernel_lb_addr *root)
891 struct fileSetDesc *fset;
893 fset = (struct fileSetDesc *)bh->b_data;
895 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
897 UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
899 udf_debug("Rootdir at block=%d, partition=%d\n",
900 root->logicalBlockNum, root->partitionReferenceNum);
903 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
905 struct partitionDesc *p;
908 p = (struct partitionDesc *)bh->b_data;
910 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
911 udf_debug("Searching map: (%d == %d)\n",
912 UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
913 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber)) {
914 UDF_SB_PARTLEN(sb, i) = le32_to_cpu(p->partitionLength); /* blocks */
915 UDF_SB_PARTROOT(sb, i) = le32_to_cpu(p->partitionStartingLocation);
916 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
917 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_READ_ONLY;
918 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
919 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_WRITE_ONCE;
920 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
921 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_REWRITABLE;
922 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
923 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_OVERWRITABLE;
925 if (!strcmp(p->partitionContents.ident,
926 PD_PARTITION_CONTENTS_NSR02) ||
927 !strcmp(p->partitionContents.ident,
928 PD_PARTITION_CONTENTS_NSR03)) {
929 struct partitionHeaderDesc *phd;
931 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
932 if (phd->unallocSpaceTable.extLength) {
933 kernel_lb_addr loc = {
934 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
935 .partitionReferenceNum = i,
938 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table =
940 if (!UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table) {
941 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
944 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_UNALLOC_TABLE;
945 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
946 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table->i_ino);
948 if (phd->unallocSpaceBitmap.extLength) {
949 UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
950 if (UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap != NULL) {
951 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extLength =
952 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
953 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition =
954 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
955 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_UNALLOC_BITMAP;
956 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
957 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition);
960 if (phd->partitionIntegrityTable.extLength)
961 udf_debug("partitionIntegrityTable (part %d)\n", i);
962 if (phd->freedSpaceTable.extLength) {
963 kernel_lb_addr loc = {
964 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
965 .partitionReferenceNum = i,
968 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table =
970 if (!UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table) {
971 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
974 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_FREED_TABLE;
975 udf_debug("freedSpaceTable (part %d) @ %ld\n",
976 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table->i_ino);
978 if (phd->freedSpaceBitmap.extLength) {
979 UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
980 if (UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap != NULL) {
981 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extLength =
982 le32_to_cpu(phd->freedSpaceBitmap.extLength);
983 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition =
984 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
985 UDF_SB_PARTFLAGS(sb, i) |= UDF_PART_FLAG_FREED_BITMAP;
986 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
987 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition);
994 if (i == UDF_SB_NUMPARTS(sb)) {
995 udf_debug("Partition (%d) not found in partition map\n",
996 le16_to_cpu(p->partitionNumber));
998 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1000 le16_to_cpu(p->partitionNumber), i,
1001 UDF_SB_PARTTYPE(sb, i), UDF_SB_PARTROOT(sb, i),
1002 UDF_SB_PARTLEN(sb, i));
1007 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1008 kernel_lb_addr *fileset)
1010 struct logicalVolDesc *lvd;
1014 lvd = (struct logicalVolDesc *)bh->b_data;
1016 UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
1018 for (i = 0, offset = 0;
1019 i < UDF_SB_NUMPARTS(sb) && offset < le32_to_cpu(lvd->mapTableLength);
1020 i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) {
1021 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
1023 struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
1024 UDF_SB_PARTTYPE(sb, i) = UDF_TYPE1_MAP15;
1025 UDF_SB_PARTVSN(sb, i) = le16_to_cpu(gpm1->volSeqNum);
1026 UDF_SB_PARTNUM(sb, i) = le16_to_cpu(gpm1->partitionNum);
1027 UDF_SB_PARTFUNC(sb, i) = NULL;
1028 } else if (type == 2) {
1029 struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
1030 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) {
1031 if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) {
1032 UDF_SB_PARTTYPE(sb, i) = UDF_VIRTUAL_MAP15;
1033 UDF_SB_PARTFUNC(sb, i) = udf_get_pblock_virt15;
1034 } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) {
1035 UDF_SB_PARTTYPE(sb, i) = UDF_VIRTUAL_MAP20;
1036 UDF_SB_PARTFUNC(sb, i) = udf_get_pblock_virt20;
1038 } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) {
1041 struct sparingTable *st;
1042 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
1044 UDF_SB_PARTTYPE(sb, i) = UDF_SPARABLE_MAP15;
1045 UDF_SB_TYPESPAR(sb, i).s_packet_len = le16_to_cpu(spm->packetLength);
1046 for (j = 0; j < spm->numSparingTables; j++) {
1047 loc = le32_to_cpu(spm->locSparingTable[j]);
1048 UDF_SB_TYPESPAR(sb, i).s_spar_map[j] =
1049 udf_read_tagged(sb, loc, loc, &ident);
1050 if (UDF_SB_TYPESPAR(sb, i).s_spar_map[j] != NULL) {
1051 st = (struct sparingTable *)UDF_SB_TYPESPAR(sb, i).s_spar_map[j]->b_data;
1053 strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) {
1054 brelse(UDF_SB_TYPESPAR(sb, i).s_spar_map[j]);
1055 UDF_SB_TYPESPAR(sb, i).s_spar_map[j] = NULL;
1059 UDF_SB_PARTFUNC(sb, i) = udf_get_pblock_spar15;
1061 udf_debug("Unknown ident: %s\n",
1062 upm2->partIdent.ident);
1065 UDF_SB_PARTVSN(sb, i) = le16_to_cpu(upm2->volSeqNum);
1066 UDF_SB_PARTNUM(sb, i) = le16_to_cpu(upm2->partitionNum);
1068 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1069 i, UDF_SB_PARTNUM(sb, i), type,
1070 UDF_SB_PARTVSN(sb, i));
1074 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1076 *fileset = lelb_to_cpu(la->extLocation);
1077 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1078 "partition=%d\n", fileset->logicalBlockNum,
1079 fileset->partitionReferenceNum);
1081 if (lvd->integritySeqExt.extLength)
1082 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1088 * udf_load_logicalvolint
1091 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1093 struct buffer_head *bh = NULL;
1096 while (loc.extLength > 0 &&
1097 (bh = udf_read_tagged(sb, loc.extLocation,
1098 loc.extLocation, &ident)) &&
1099 ident == TAG_IDENT_LVID) {
1100 UDF_SB_LVIDBH(sb) = bh;
1102 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
1103 udf_load_logicalvolint(sb,
1104 leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
1106 if (UDF_SB_LVIDBH(sb) != bh)
1108 loc.extLength -= sb->s_blocksize;
1111 if (UDF_SB_LVIDBH(sb) != bh)
1116 * udf_process_sequence
1119 * Process a main/reserve volume descriptor sequence.
1122 * sb Pointer to _locked_ superblock.
1123 * block First block of first extent of the sequence.
1124 * lastblock Lastblock of first extent of the sequence.
1127 * July 1, 1997 - Andrew E. Mileski
1128 * Written, tested, and released.
1130 static int udf_process_sequence(struct super_block *sb, long block,
1131 long lastblock, kernel_lb_addr *fileset)
1133 struct buffer_head *bh = NULL;
1134 struct udf_vds_record vds[VDS_POS_LENGTH];
1135 struct generic_desc *gd;
1136 struct volDescPtr *vdp;
1141 long next_s = 0, next_e = 0;
1143 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1145 /* Read the main descriptor sequence */
1146 for (; (!done && block <= lastblock); block++) {
1148 bh = udf_read_tagged(sb, block, block, &ident);
1152 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1153 gd = (struct generic_desc *)bh->b_data;
1154 vdsn = le32_to_cpu(gd->volDescSeqNum);
1156 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1157 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
1158 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
1159 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1162 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1163 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1164 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1165 vds[VDS_POS_VOL_DESC_PTR].block = block;
1167 vdp = (struct volDescPtr *)bh->b_data;
1168 next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1169 next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
1170 next_e = next_e >> sb->s_blocksize_bits;
1174 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1175 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
1176 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
1177 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1180 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1181 if (!vds[VDS_POS_PARTITION_DESC].block)
1182 vds[VDS_POS_PARTITION_DESC].block = block;
1184 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1185 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
1186 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
1187 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1190 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1191 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1192 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
1193 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1196 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1197 vds[VDS_POS_TERMINATING_DESC].block = block;
1201 next_s = next_e = 0;
1209 for (i = 0; i < VDS_POS_LENGTH; i++) {
1211 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1214 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1215 udf_load_pvoldesc(sb, bh);
1216 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1217 udf_load_logicalvol(sb, bh, fileset);
1218 } else if (i == VDS_POS_PARTITION_DESC) {
1219 struct buffer_head *bh2 = NULL;
1220 if (udf_load_partdesc(sb, bh)) {
1224 for (j = vds[i].block + 1;
1225 j < vds[VDS_POS_TERMINATING_DESC].block;
1227 bh2 = udf_read_tagged(sb, j, j, &ident);
1228 gd = (struct generic_desc *)bh2->b_data;
1229 if (ident == TAG_IDENT_PD)
1230 if (udf_load_partdesc(sb,
1249 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1254 udf_debug("Validity check skipped because of novrs option\n");
1257 /* Check that it is NSR02 compliant */
1258 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1260 block = udf_vrs(sb, silent);
1262 udf_debug("Failed to read byte 32768. Assuming open "
1263 "disc. Skipping validity check\n");
1264 if (!UDF_SB_LASTBLOCK(sb))
1265 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1272 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1274 struct anchorVolDescPtr *anchor;
1276 struct buffer_head *bh;
1277 long main_s, main_e, reserve_s, reserve_e;
1283 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
1284 if (UDF_SB_ANCHOR(sb)[i] &&
1285 (bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i],
1286 UDF_SB_ANCHOR(sb)[i], &ident))) {
1287 anchor = (struct anchorVolDescPtr *)bh->b_data;
1289 /* Locate the main sequence */
1290 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1291 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1292 main_e = main_e >> sb->s_blocksize_bits;
1295 /* Locate the reserve sequence */
1296 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1297 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1298 reserve_e = reserve_e >> sb->s_blocksize_bits;
1299 reserve_e += reserve_s;
1303 /* Process the main & reserve sequences */
1304 /* responsible for finding the PartitionDesc(s) */
1305 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1306 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1311 if (i == ARRAY_SIZE(UDF_SB_ANCHOR(sb))) {
1312 udf_debug("No Anchor block found\n");
1315 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1317 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
1318 kernel_lb_addr uninitialized_var(ino);
1319 switch (UDF_SB_PARTTYPE(sb, i)) {
1320 case UDF_VIRTUAL_MAP15:
1321 case UDF_VIRTUAL_MAP20:
1322 if (!UDF_SB_LASTBLOCK(sb)) {
1323 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1324 udf_find_anchor(sb);
1327 if (!UDF_SB_LASTBLOCK(sb)) {
1328 udf_debug("Unable to determine Lastblock (For "
1329 "Virtual Partition)\n");
1333 for (j = 0; j < UDF_SB_NUMPARTS(sb); j++) {
1335 UDF_SB_PARTVSN(sb, i) == UDF_SB_PARTVSN(sb, j) &&
1336 UDF_SB_PARTNUM(sb, i) == UDF_SB_PARTNUM(sb, j)) {
1337 ino.partitionReferenceNum = j;
1338 ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) - UDF_SB_PARTROOT(sb, j);
1343 if (j == UDF_SB_NUMPARTS(sb))
1346 UDF_SB_VAT(sb) = udf_iget(sb, ino);
1347 if (!UDF_SB_VAT(sb))
1350 if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP15) {
1351 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1352 udf_ext0_offset(UDF_SB_VAT(sb));
1353 UDF_SB_TYPEVIRT(sb, i).s_num_entries =
1354 (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1355 } else if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP20) {
1356 struct buffer_head *bh = NULL;
1359 pos = udf_block_map(UDF_SB_VAT(sb), 0);
1360 bh = sb_bread(sb, pos);
1363 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1364 le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data +
1365 udf_ext0_offset(UDF_SB_VAT(sb)))->lengthHeader) +
1366 udf_ext0_offset(UDF_SB_VAT(sb));
1367 UDF_SB_TYPEVIRT(sb, i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
1368 UDF_SB_TYPEVIRT(sb, i).s_start_offset) >> 2;
1371 UDF_SB_PARTROOT(sb, i) = udf_get_pblock(sb, 0, i, 0);
1372 UDF_SB_PARTLEN(sb, i) = UDF_SB_PARTLEN(sb, ino.partitionReferenceNum);
1378 static void udf_open_lvid(struct super_block *sb)
1380 if (UDF_SB_LVIDBH(sb)) {
1382 kernel_timestamp cpu_time;
1384 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1385 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1386 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1387 UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1388 UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1390 UDF_SB_LVID(sb)->descTag.descCRC = cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1391 le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1393 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1394 for (i = 0; i < 16; i++)
1396 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1397 ((uint8_t *) &(UDF_SB_LVID(sb)->descTag))[i];
1399 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1403 static void udf_close_lvid(struct super_block *sb)
1405 kernel_timestamp cpu_time;
1408 if (UDF_SB_LVIDBH(sb) &&
1409 UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1410 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1411 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1412 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1413 UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1414 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1415 UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1416 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1417 UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1418 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1419 UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1420 UDF_SB_LVID(sb)->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1422 UDF_SB_LVID(sb)->descTag.descCRC =
1423 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1424 le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1426 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1427 for (i = 0; i < 16; i++)
1429 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1430 ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
1432 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1440 * Complete the specified super block.
1443 * sb Pointer to superblock to complete - never NULL.
1444 * sb->s_dev Device to read suberblock from.
1445 * options Pointer to mount options.
1446 * silent Silent flag.
1449 * July 1, 1997 - Andrew E. Mileski
1450 * Written, tested, and released.
1452 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1455 struct inode *inode = NULL;
1456 struct udf_options uopt;
1457 kernel_lb_addr rootdir, fileset;
1458 struct udf_sb_info *sbi;
1460 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1465 sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1469 sb->s_fs_info = sbi;
1470 memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1472 mutex_init(&sbi->s_alloc_mutex);
1474 if (!udf_parse_options((char *)options, &uopt))
1477 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1478 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1479 udf_error(sb, "udf_read_super",
1480 "utf8 cannot be combined with iocharset\n");
1483 #ifdef CONFIG_UDF_NLS
1484 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1485 uopt.nls_map = load_nls_default();
1487 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1489 udf_debug("Using default NLS map\n");
1492 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1493 uopt.flags |= (1 << UDF_FLAG_UTF8);
1495 fileset.logicalBlockNum = 0xFFFFFFFF;
1496 fileset.partitionReferenceNum = 0xFFFF;
1498 UDF_SB(sb)->s_flags = uopt.flags;
1499 UDF_SB(sb)->s_uid = uopt.uid;
1500 UDF_SB(sb)->s_gid = uopt.gid;
1501 UDF_SB(sb)->s_umask = uopt.umask;
1502 UDF_SB(sb)->s_nls_map = uopt.nls_map;
1504 /* Set the block size for all transfers */
1505 if (!udf_set_blocksize(sb, uopt.blocksize))
1508 if (uopt.session == 0xFFFFFFFF)
1509 UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1511 UDF_SB_SESSION(sb) = uopt.session;
1513 udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1515 UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1516 UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0;
1517 UDF_SB_ANCHOR(sb)[2] = uopt.anchor;
1518 UDF_SB_ANCHOR(sb)[3] = 256;
1520 if (udf_check_valid(sb, uopt.novrs, silent)) {
1521 /* read volume recognition sequences */
1522 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1526 udf_find_anchor(sb);
1528 /* Fill in the rest of the superblock */
1529 sb->s_op = &udf_sb_ops;
1532 sb->s_magic = UDF_SUPER_MAGIC;
1533 sb->s_time_gran = 1000;
1535 if (udf_load_partition(sb, &fileset)) {
1536 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1540 udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1542 if (UDF_SB_LVIDBH(sb)) {
1543 uint16_t minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1544 uint16_t minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
1545 /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1547 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1548 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x (max is %x)\n",
1549 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev),
1550 UDF_MAX_READ_VERSION);
1552 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
1553 sb->s_flags |= MS_RDONLY;
1556 UDF_SB_UDFREV(sb) = minUDFWriteRev;
1558 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1559 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1560 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1561 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1564 if (!UDF_SB_NUMPARTS(sb)) {
1565 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1569 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_READ_ONLY) {
1570 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; forcing readonly mount\n");
1571 sb->s_flags |= MS_RDONLY;
1574 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1575 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1580 kernel_timestamp ts;
1581 udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb));
1582 udf_info("UDF %s (%s) Mounting volume '%s', "
1583 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1584 UDFFS_VERSION, UDFFS_DATE,
1585 UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute,
1586 ts.typeAndTimezone);
1588 if (!(sb->s_flags & MS_RDONLY))
1591 /* Assign the root inode */
1592 /* assign inodes by physical block number */
1593 /* perhaps it's not extensible enough, but for now ... */
1594 inode = udf_iget(sb, rootdir);
1596 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
1597 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1601 /* Allocate a dentry for the root inode */
1602 sb->s_root = d_alloc_root(inode);
1604 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1608 sb->s_maxbytes = MAX_LFS_FILESIZE;
1613 iput(UDF_SB_VAT(sb));
1614 if (UDF_SB_NUMPARTS(sb)) {
1615 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1616 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1617 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1618 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1619 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1620 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_uspace);
1621 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1622 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_fspace);
1623 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) {
1624 for (i = 0; i < 4; i++)
1625 brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
1628 #ifdef CONFIG_UDF_NLS
1629 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1630 unload_nls(UDF_SB(sb)->s_nls_map);
1632 if (!(sb->s_flags & MS_RDONLY))
1634 brelse(UDF_SB_LVIDBH(sb));
1637 sb->s_fs_info = NULL;
1642 void udf_error(struct super_block *sb, const char *function,
1643 const char *fmt, ...)
1647 if (!(sb->s_flags & MS_RDONLY)) {
1651 va_start(args, fmt);
1652 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1654 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1655 sb->s_id, function, error_buf);
1658 void udf_warning(struct super_block *sb, const char *function,
1659 const char *fmt, ...)
1663 va_start(args, fmt);
1664 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1666 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1667 sb->s_id, function, error_buf);
1674 * Prepare for destruction of the superblock.
1677 * Called before the filesystem is unmounted.
1680 * July 1, 1997 - Andrew E. Mileski
1681 * Written, tested, and released.
1683 static void udf_put_super(struct super_block *sb)
1688 iput(UDF_SB_VAT(sb));
1689 if (UDF_SB_NUMPARTS(sb)) {
1690 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1691 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1692 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1693 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1694 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1695 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_uspace);
1696 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1697 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_fspace);
1698 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) {
1699 for (i = 0; i < 4; i++)
1700 brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
1703 #ifdef CONFIG_UDF_NLS
1704 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1705 unload_nls(UDF_SB(sb)->s_nls_map);
1707 if (!(sb->s_flags & MS_RDONLY))
1709 brelse(UDF_SB_LVIDBH(sb));
1711 kfree(sb->s_fs_info);
1712 sb->s_fs_info = NULL;
1719 * Return info about the filesystem.
1722 * Called by sys_statfs()
1725 * July 1, 1997 - Andrew E. Mileski
1726 * Written, tested, and released.
1728 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1730 struct super_block *sb = dentry->d_sb;
1732 buf->f_type = UDF_SUPER_MAGIC;
1733 buf->f_bsize = sb->s_blocksize;
1734 buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1735 buf->f_bfree = udf_count_free(sb);
1736 buf->f_bavail = buf->f_bfree;
1737 buf->f_files = (UDF_SB_LVIDBH(sb) ?
1738 (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
1739 le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree;
1740 buf->f_ffree = buf->f_bfree;
1741 /* __kernel_fsid_t f_fsid */
1742 buf->f_namelen = UDF_NAME_LEN - 2;
1747 static unsigned char udf_bitmap_lookup[16] = {
1748 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1751 static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
1753 struct buffer_head *bh = NULL;
1754 unsigned int accum = 0;
1756 int block = 0, newblock;
1762 struct spaceBitmapDesc *bm;
1766 loc.logicalBlockNum = bitmap->s_extPosition;
1767 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1768 bh = udf_read_ptagged(sb, loc, 0, &ident);
1771 printk(KERN_ERR "udf: udf_count_free failed\n");
1773 } else if (ident != TAG_IDENT_SBD) {
1775 printk(KERN_ERR "udf: udf_count_free failed\n");
1779 bm = (struct spaceBitmapDesc *)bh->b_data;
1780 bytes = le32_to_cpu(bm->numOfBytes);
1781 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1782 ptr = (uint8_t *)bh->b_data;
1785 while ((bytes > 0) && (index < sb->s_blocksize)) {
1787 accum += udf_bitmap_lookup[value & 0x0f];
1788 accum += udf_bitmap_lookup[value >> 4];
1794 newblock = udf_get_lb_pblock(sb, loc, ++block);
1795 bh = udf_tread(sb, newblock);
1797 udf_debug("read failed\n");
1801 ptr = (uint8_t *)bh->b_data;
1812 static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table)
1814 unsigned int accum = 0;
1816 kernel_lb_addr eloc;
1818 struct extent_position epos;
1822 epos.block = UDF_I_LOCATION(table);
1823 epos.offset = sizeof(struct unallocSpaceEntry);
1826 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
1827 accum += (elen >> table->i_sb->s_blocksize_bits);
1836 static unsigned int udf_count_free(struct super_block *sb)
1838 unsigned int accum = 0;
1840 if (UDF_SB_LVIDBH(sb)) {
1841 if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb)) {
1842 accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
1843 if (accum == 0xFFFFFFFF)
1851 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP) {
1852 accum += udf_count_free_bitmap(sb,
1853 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
1855 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP) {
1856 accum += udf_count_free_bitmap(sb,
1857 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
1862 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE) {
1863 accum += udf_count_free_table(sb,
1864 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1866 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE) {
1867 accum += udf_count_free_table(sb,
1868 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);