* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / udf / super.c
blobc6ff2c6e2285fbfe8c298c84a71bf9bccefdb4f1
1 /*
2 * super.c
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
7 * DESCRIPTION
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/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
17 * CONTACTS
18 * E-mail regarding any portion of the Linux UDF file system should be
19 * directed to the development team mailing list (run by majordomo):
20 * linux_udf@hootie.lvld.hp.com
22 * COPYRIGHT
23 * This file is distributed under the terms of the GNU General Public
24 * License (GPL). Copies of the GPL can be obtained from:
25 * ftp://prep.ai.mit.edu/pub/gnu/GPL
26 * Each contributing author retains all rights to their own work.
28 * (C) 1998 Dave Boynton
29 * (C) 1998-1999 Ben Fennema
31 * HISTORY
33 * 09/24/98 dgb changed to allow compiling outside of kernel, and
34 * added some debugging.
35 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
36 * 10/16/98 attempting some multi-session support
37 * 10/17/98 added freespace count for "df"
38 * 11/11/98 gr added novrs option
39 * 11/26/98 dgb added fileset,anchor mount options
40 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced vol descs
41 * rewrote option handling based on isofs
42 * 12/20/98 find the free space bitmap (if it exists)
45 #ifndef LINUX_VERSION_CODE
46 #include <linux/version.h>
47 #endif
49 #include "udfdecl.h"
51 #include <linux/blkdev.h>
52 #include <linux/malloc.h>
53 #include <linux/kernel.h>
54 #include <linux/locks.h>
55 #include <linux/module.h>
56 #include <linux/stat.h>
57 #include <linux/cdrom.h>
58 #include <linux/nls.h>
59 #include <asm/byteorder.h>
61 #include <linux/udf_fs.h>
62 #include "udf_sb.h"
63 #include "udf_i.h"
65 #include <linux/init.h>
66 #include <asm/uaccess.h>
68 static char error_buf[1024];
70 /* These are the "meat" - everything else is stuffing */
71 static struct super_block *udf_read_super(struct super_block *, void *, int);
72 static void udf_put_super(struct super_block *);
73 static int udf_remount_fs(struct super_block *, int *, char *);
74 static int udf_check_valid(struct super_block *, int, int);
75 static int udf_vrs(struct super_block *sb, int silent);
76 static int udf_load_partition(struct super_block *, lb_addr *);
77 static int udf_load_logicalvol(struct super_block *, struct buffer_head *, lb_addr *);
78 static void udf_load_logicalvolint(struct super_block *, extent_ad);
79 static int udf_find_anchor(struct super_block *, int, int);
80 static int udf_find_fileset(struct super_block *, lb_addr *, lb_addr *);
81 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
82 static void udf_load_fileset(struct super_block *, struct buffer_head *, lb_addr *);
83 static void udf_load_partdesc(struct super_block *, struct buffer_head *);
84 static void udf_open_lvid(struct super_block *);
85 static void udf_close_lvid(struct super_block *);
86 static unsigned int udf_count_free(struct super_block *);
88 /* version specific functions */
89 static int udf_statfs(struct super_block *, struct statfs *, int);
91 /* UDF filesystem type */
92 static struct file_system_type udf_fstype = {
93 "udf", /* name */
94 FS_REQUIRES_DEV, /* fs_flags */
95 udf_read_super, /* read_super */
96 NULL /* next */
99 /* Superblock operations */
100 static struct super_operations udf_sb_ops =
102 udf_read_inode, /* read_inode */
103 #ifdef CONFIG_UDF_RW
104 udf_write_inode, /* write_inode */
105 #else
106 NULL, /* write_inode */
107 #endif
108 udf_put_inode, /* put_inode */
109 #ifdef CONFIG_UDF_RW
110 udf_delete_inode, /* delete_inode */
111 #else
112 NULL, /* delete_inode */
113 #endif
114 NULL, /* notify_change */
115 udf_put_super, /* put_super */
116 NULL, /* write_super */
117 udf_statfs, /* statfs */
118 udf_remount_fs, /* remount_fs */
119 NULL, /* clear_inode */
120 NULL, /* umount_begin */
123 struct udf_options
125 unsigned char novrs;
126 unsigned char utf8;
127 unsigned int blocksize;
128 unsigned int session;
129 unsigned int lastblock;
130 unsigned int anchor;
131 unsigned int volume;
132 unsigned short partition;
133 unsigned int fileset;
134 unsigned int rootdir;
135 unsigned int flags;
136 mode_t umask;
137 gid_t gid;
138 uid_t uid;
139 char *iocharset;
142 #if defined(MODULE)
145 * cleanup_module
147 * PURPOSE
148 * Unregister the UDF filesystem type.
150 * DESCRIPTION
151 * Clean-up before the module is unloaded.
152 * This routine only applies when compiled as a module.
154 * HISTORY
155 * July 1, 1997 - Andrew E. Mileski
156 * Written, tested, and released.
159 cleanup_module(void)
161 printk(KERN_NOTICE "udf: unregistering filesystem\n");
162 return unregister_filesystem(&udf_fstype);
166 * init_module / init_udf_fs
168 * PURPOSE
169 * Register the UDF filesystem type.
171 * HISTORY
172 * July 1, 1997 - Andrew E. Mileski
173 * Written, tested, and released.
175 int init_module(void)
176 #else /* if !defined(MODULE) */
177 int __init init_udf_fs(void)
178 #endif
180 printk(KERN_NOTICE "udf: registering filesystem\n");
182 struct super_block sb;
183 int size;
185 size = sizeof(struct super_block) +
186 (long)&sb.u - (long)&sb;
187 if ( size < sizeof(struct udf_sb_info) )
189 printk(KERN_ERR "udf: Danger! Kernel was compiled without enough room for udf_sb_info\n");
190 printk(KERN_ERR "udf: Kernel has room for %u bytes, udf needs %u\n",
191 size, sizeof(struct udf_sb_info));
192 return 0;
195 return register_filesystem(&udf_fstype);
199 * udf_parse_options
201 * PURPOSE
202 * Parse mount options.
204 * DESCRIPTION
205 * The following mount options are supported:
207 * gid= Set the default group.
208 * umask= Set the default umask.
209 * uid= Set the default user.
210 * unhide Show otherwise hidden files.
211 * undelete Show deleted files in lists.
212 * strict Set strict conformance (unused)
213 * utf8 (unused)
214 * iocharset (unused)
216 * The remaining are for debugging and disaster recovery:
218 * bs= Set the block size. (may not work unless 2048)
219 * novrs Skip volume sequence recognition
221 * The following expect a offset from 0.
223 * session= Set the CDROM session (default= last session)
224 * anchor= Override standard anchor location. (default= 256)
225 * volume= Override the VolumeDesc location. (unused)
226 * partition= Override the PartitionDesc location. (unused)
227 * lastblock= Set the last block of the filesystem/
229 * The following expect a offset from the partition root.
231 * fileset= Override the fileset block location. (unused)
232 * rootdir= Override the root directory location. (unused)
233 * WARNING: overriding the rootdir to a non-directory may
234 * yield highly unpredictable results.
236 * PRE-CONDITIONS
237 * options Pointer to mount options string.
238 * uopts Pointer to mount options variable.
240 * POST-CONDITIONS
241 * <return> 0 Mount options parsed okay.
242 * <return> -1 Error parsing mount options.
244 * HISTORY
245 * July 1, 1997 - Andrew E. Mileski
246 * Written, tested, and released.
249 static int
250 udf_parse_options(char *options, struct udf_options *uopt)
252 char *opt, *val;
254 uopt->novrs = 0;
255 uopt->blocksize = 2048;
256 uopt->partition = 0xFFFF;
257 uopt->session = 0xFFFFFFFF;
258 uopt->lastblock = 0xFFFFFFFF;
259 uopt->anchor = 0xFFFFFFFF;
260 uopt->volume = 0xFFFFFFFF;
261 uopt->rootdir = 0xFFFFFFFF;
262 uopt->fileset = 0xFFFFFFFF;
263 uopt->iocharset = NULL;
265 if (!options)
266 return 1;
268 for (opt = strtok(options, ","); opt; opt = strtok(NULL, ","))
270 /* Make "opt=val" into two strings */
271 val = strchr(opt, '=');
272 if (val)
273 *(val++) = 0;
274 if (!strcmp(opt, "novrs") && !val)
275 uopt->novrs = 1;
276 else if (!strcmp(opt, "utf8") && !val)
277 uopt->utf8 = 1;
278 else if (!strcmp(opt, "bs") && val)
279 uopt->blocksize = simple_strtoul(val, NULL, 0);
280 else if (!strcmp(opt, "unhide") && !val)
281 uopt->flags |= UDF_FLAG_UNHIDE;
282 else if (!strcmp(opt, "undelete") && !val)
283 uopt->flags |= UDF_FLAG_UNDELETE;
284 else if (!strcmp(opt, "gid") && val)
285 uopt->gid = simple_strtoul(val, NULL, 0);
286 else if (!strcmp(opt, "umask") && val)
287 uopt->umask = simple_strtoul(val, NULL, 0);
288 else if (!strcmp(opt, "strict") && !val)
289 uopt->flags |= UDF_FLAG_STRICT;
290 else if (!strcmp(opt, "uid") && val)
291 uopt->uid = simple_strtoul(val, NULL, 0);
292 else if (!strcmp(opt, "session") && val)
293 uopt->session = simple_strtoul(val, NULL, 0);
294 else if (!strcmp(opt, "lastblock") && val)
295 uopt->lastblock = simple_strtoul(val, NULL, 0);
296 else if (!strcmp(opt, "anchor") && val)
297 uopt->anchor = simple_strtoul(val, NULL, 0);
298 else if (!strcmp(opt, "volume") && val)
299 uopt->volume = simple_strtoul(val, NULL, 0);
300 else if (!strcmp(opt, "partition") && val)
301 uopt->partition = simple_strtoul(val, NULL, 0);
302 else if (!strcmp(opt, "fileset") && val)
303 uopt->fileset = simple_strtoul(val, NULL, 0);
304 else if (!strcmp(opt, "rootdir") && val)
305 uopt->rootdir = simple_strtoul(val, NULL, 0);
306 else if (!strcmp(opt, "iocharset") && val)
308 uopt->iocharset = val;
309 while (*val && *val != ',')
310 val ++;
311 if (val == uopt->iocharset)
312 return 0;
313 *val = 0;
315 else if (val)
317 printk(KERN_ERR "udf: bad mount option \"%s=%s\"\n",
318 opt, val);
319 return 0;
321 else
323 printk(KERN_ERR "udf: bad mount option \"%s\"\n",
324 opt);
325 return 0;
328 return 1;
331 static int
332 udf_remount_fs(struct super_block *sb, int *flags, char *options)
334 struct udf_options uopt;
336 uopt.flags = UDF_SB(sb)->s_flags ;
337 uopt.uid = UDF_SB(sb)->s_uid ;
338 uopt.gid = UDF_SB(sb)->s_gid ;
339 uopt.umask = UDF_SB(sb)->s_umask ;
340 uopt.utf8 = UDF_SB(sb)->s_utf8 ;
342 if ( !udf_parse_options(options, &uopt) )
343 return -EINVAL;
345 UDF_SB(sb)->s_flags = uopt.flags;
346 UDF_SB(sb)->s_uid = uopt.uid;
347 UDF_SB(sb)->s_gid = uopt.gid;
348 UDF_SB(sb)->s_umask = uopt.umask;
349 UDF_SB(sb)->s_utf8 = uopt.utf8;
351 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
352 return 0;
353 if (*flags & MS_RDONLY)
354 udf_close_lvid(sb);
355 else
356 udf_open_lvid(sb);
358 return 0;
362 * udf_set_blocksize
364 * PURPOSE
365 * Set the block size to be used in all transfers.
367 * DESCRIPTION
368 * To allow room for a DMA transfer, it is best to guess big when unsure.
369 * This routine picks 2048 bytes as the blocksize when guessing. This
370 * should be adequate until devices with larger block sizes become common.
372 * Note that the Linux kernel can currently only deal with blocksizes of
373 * 512, 1024, 2048, 4096, and 8192 bytes.
375 * PRE-CONDITIONS
376 * sb Pointer to _locked_ superblock.
378 * POST-CONDITIONS
379 * sb->s_blocksize Blocksize.
380 * sb->s_blocksize_bits log2 of blocksize.
381 * <return> 0 Blocksize is valid.
382 * <return> 1 Blocksize is invalid.
384 * HISTORY
385 * July 1, 1997 - Andrew E. Mileski
386 * Written, tested, and released.
388 static int
389 udf_set_blocksize(struct super_block *sb, int bsize)
391 /* Use specified block size if specified */
392 sb->s_blocksize = get_hardblocksize(sb->s_dev);
393 sb->s_blocksize = sb->s_blocksize ? sb->s_blocksize : 2048;
394 if (bsize > sb->s_blocksize)
395 sb->s_blocksize = bsize;
397 /* Block size must be an even multiple of 512 */
398 switch (sb->s_blocksize) {
399 case 512: sb->s_blocksize_bits = 9; break;
400 case 1024: sb->s_blocksize_bits = 10; break;
401 case 2048: sb->s_blocksize_bits = 11; break;
402 case 4096: sb->s_blocksize_bits = 12; break;
403 case 8192: sb->s_blocksize_bits = 13; break;
404 default:
406 udf_debug("Bad block size (%ld)\n", sb->s_blocksize);
407 printk(KERN_ERR "udf: bad block size (%ld)\n", sb->s_blocksize);
408 return 0;
412 /* Set the block size */
413 set_blocksize(sb->s_dev, sb->s_blocksize);
414 return sb->s_blocksize;
417 static int
418 udf_vrs(struct super_block *sb, int silent)
420 struct VolStructDesc *vsd = NULL;
421 int sector = 32768;
422 struct buffer_head *bh = NULL;
423 int iso9660=0;
424 int nsr02=0;
425 int nsr03=0;
427 /* Block size must be a multiple of 512 */
428 if (sb->s_blocksize & 511)
429 return sector;
431 sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
433 udf_debug("Starting at sector %u (%ld byte sectors)\n",
434 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
435 /* Process the sequence (if applicable) */
436 for (;!nsr02 && !nsr03; sector += 2048)
438 /* Read a block */
439 bh = udf_tread(sb, sector >> sb->s_blocksize_bits, 2048);
440 if (!bh)
441 break;
443 /* Look for ISO descriptors */
444 vsd = (struct VolStructDesc *)(bh->b_data +
445 (sector & (sb->s_blocksize - 1)));
447 if (vsd->stdIdent[0] == 0)
449 udf_release_data(bh);
450 break;
452 else if (!strncmp(vsd->stdIdent, STD_ID_CD001, STD_ID_LEN))
454 iso9660 = sector;
455 switch (vsd->structType)
457 case 0:
458 udf_debug("ISO9660 Boot Record found\n");
459 break;
460 case 1:
461 udf_debug("ISO9660 Primary Volume Descriptor found\n");
462 break;
463 case 2:
464 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
465 break;
466 case 3:
467 udf_debug("ISO9660 Volume Partition Descriptor found\n");
468 break;
469 case 255:
470 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
471 break;
472 default:
473 udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
474 break;
477 else if (!strncmp(vsd->stdIdent, STD_ID_BEA01, STD_ID_LEN))
480 else if (!strncmp(vsd->stdIdent, STD_ID_TEA01, STD_ID_LEN))
482 udf_release_data(bh);
483 break;
485 else if (!strncmp(vsd->stdIdent, STD_ID_NSR02, STD_ID_LEN))
487 nsr02 = sector;
489 else if (!strncmp(vsd->stdIdent, STD_ID_NSR03, STD_ID_LEN))
491 nsr03 = sector;
493 udf_release_data(bh);
496 if (nsr03)
497 return nsr03;
498 else if (nsr02)
499 return nsr02;
500 else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
501 return -1;
502 else
503 return 0;
507 * udf_find_anchor
509 * PURPOSE
510 * Find an anchor volume descriptor.
512 * PRE-CONDITIONS
513 * sb Pointer to _locked_ superblock.
514 * lastblock Last block on media.
516 * POST-CONDITIONS
517 * <return> 1 if not found, 0 if ok
519 * HISTORY
520 * July 1, 1997 - Andrew E. Mileski
521 * Written, tested, and released.
523 static int
524 udf_find_anchor(struct super_block *sb, int useranchor, int lastblock)
526 int varlastblock = udf_variable_to_fixed(lastblock);
527 int last[] = { lastblock, lastblock - 2,
528 lastblock - 150, lastblock - 152,
529 varlastblock, varlastblock - 2,
530 varlastblock - 150, varlastblock - 152 };
531 struct buffer_head *bh = NULL;
532 Uint16 ident;
533 Uint32 location;
534 int i;
536 UDF_SB_ANCHOR(sb)[0] = 0;
537 UDF_SB_ANCHOR(sb)[1] = 0;
538 UDF_SB_ANCHOR(sb)[2] = 0;
539 UDF_SB_ANCHOR(sb)[3] = 256 + UDF_SB_SESSION(sb);
541 lastblock = 0;
543 /* Search for an anchor volume descriptor pointer */
545 /* according to spec, anchor is in either:
546 * block 256
547 * lastblock-256
548 * lastblock
549 * however, if the disc isn't closed, it could be 512 */
551 for (i=0; (!lastblock && i<sizeof(last)/sizeof(int)); i++)
553 if (!(bh = bread(sb->s_dev, last[i], sb->s_blocksize)))
555 ident = location = 0;
557 else
559 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
560 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
561 udf_release_data(bh);
564 if (ident == TID_ANCHOR_VOL_DESC_PTR)
566 if (location == last[i] - UDF_SB_SESSION(sb))
568 lastblock = UDF_SB_ANCHOR(sb)[0] = last[i];
569 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
571 else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb))
573 UDF_SB(sb)->s_flags |= UDF_FLAG_VARCONV;
574 lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]);
575 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
577 else
578 udf_debug("Anchor found at block %d, location mismatch %d.\n",
579 last[i], location);
581 else if (ident == TID_FILE_ENTRY || ident == TID_EXTENDED_FILE_ENTRY)
583 lastblock = last[i];
584 UDF_SB_ANCHOR(sb)[2] = 512 + UDF_SB_SESSION(sb);
586 else
588 if (!(bh = bread(sb->s_dev, last[i] - 256, sb->s_blocksize)))
590 ident = location = 0;
592 else
594 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
595 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
596 udf_release_data(bh);
599 if (ident == TID_ANCHOR_VOL_DESC_PTR &&
600 location == last[i] - 256 - UDF_SB_SESSION(sb))
602 lastblock = last[i];
603 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
605 else
607 if (!(bh = bread(sb->s_dev, last[i] - 312 - UDF_SB_SESSION(sb),
608 sb->s_blocksize)))
610 ident = location = 0;
612 else
614 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
615 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
616 udf_release_data(bh);
619 if (ident == TID_ANCHOR_VOL_DESC_PTR &&
620 location == udf_variable_to_fixed(last[i]) - 256)
622 UDF_SB(sb)->s_flags |= UDF_FLAG_VARCONV;
623 lastblock = udf_variable_to_fixed(last[i]);
624 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
630 if (!lastblock)
632 /* We havn't found the lastblock. check 312 */
633 if ((bh = bread(sb->s_dev, 312 + UDF_SB_SESSION(sb), sb->s_blocksize)))
635 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
636 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
637 udf_release_data(bh);
639 if (ident == TID_ANCHOR_VOL_DESC_PTR && location == 256)
640 UDF_SB(sb)->s_flags |= UDF_FLAG_VARCONV;
644 for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
646 if (UDF_SB_ANCHOR(sb)[i])
648 if (!(bh = udf_read_tagged(sb,
649 UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
651 UDF_SB_ANCHOR(sb)[i] = 0;
653 else
655 udf_release_data(bh);
656 if ((ident != TID_ANCHOR_VOL_DESC_PTR) && (i ||
657 (ident != TID_FILE_ENTRY && ident != TID_EXTENDED_FILE_ENTRY)))
659 UDF_SB_ANCHOR(sb)[i] = 0;
663 else if (useranchor != 0xFFFFFFFF)
665 UDF_SB_ANCHOR(sb)[i] = useranchor;
666 useranchor = 0xFFFFFFFF;
667 i --;
671 return lastblock;
674 static int
675 udf_find_fileset(struct super_block *sb, lb_addr *fileset, lb_addr *root)
677 struct buffer_head *bh = NULL;
678 long lastblock;
679 Uint16 ident;
681 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
682 fileset->partitionReferenceNum != 0xFFFF)
684 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
686 if (!bh)
687 return 1;
688 else if (ident != TID_FILE_SET_DESC)
690 udf_release_data(bh);
691 return 1;
696 if (!bh) /* Search backwards through the partitions */
698 lb_addr newfileset;
700 return 1;
702 for (newfileset.partitionReferenceNum=UDF_SB_NUMPARTS(sb)-1;
703 (newfileset.partitionReferenceNum != 0xFFFF &&
704 fileset->logicalBlockNum == 0xFFFFFFFF &&
705 fileset->partitionReferenceNum == 0xFFFF);
706 newfileset.partitionReferenceNum--)
708 lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum);
709 newfileset.logicalBlockNum = 0;
713 bh = udf_read_ptagged(sb, newfileset, 0, &ident);
714 if (!bh)
716 newfileset.logicalBlockNum ++;
717 continue;
720 switch (ident)
722 case TID_SPACE_BITMAP_DESC:
724 struct SpaceBitmapDesc *sp;
725 sp = (struct SpaceBitmapDesc *)bh->b_data;
726 newfileset.logicalBlockNum += 1 +
727 ((le32_to_cpu(sp->numOfBytes) + sizeof(struct SpaceBitmapDesc) - 1)
728 >> sb->s_blocksize_bits);
729 udf_release_data(bh);
730 break;
732 case TID_FILE_SET_DESC:
734 *fileset = newfileset;
735 break;
737 default:
739 newfileset.logicalBlockNum ++;
740 udf_release_data(bh);
741 bh = NULL;
742 break;
746 while (newfileset.logicalBlockNum < lastblock &&
747 fileset->logicalBlockNum == 0xFFFFFFFF &&
748 fileset->partitionReferenceNum == 0xFFFF);
752 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
753 fileset->partitionReferenceNum != 0xFFFF) && bh)
755 udf_debug("Fileset at block=%d, partition=%d\n",
756 fileset->logicalBlockNum, fileset->partitionReferenceNum);
758 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
759 udf_load_fileset(sb, bh, root);
760 udf_release_data(bh);
761 return 0;
763 return 1;
766 static void
767 udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
769 struct PrimaryVolDesc *pvoldesc;
770 time_t recording;
771 struct ustr instr;
772 struct ustr outstr;
774 pvoldesc = (struct PrimaryVolDesc *)bh->b_data;
776 if ( udf_stamp_to_time(&recording, lets_to_cpu(pvoldesc->recordingDateAndTime)) )
778 timestamp ts;
779 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
780 udf_debug("recording time %ld, %u/%u/%u %u:%u (%x)\n",
781 recording, ts.year, ts.month, ts.day, ts.hour, ts.minute,
782 ts.typeAndTimezone);
783 UDF_SB_RECORDTIME(sb) = recording;
786 if ( !udf_build_ustr(&instr, pvoldesc->volIdent, 32) )
788 if (!udf_CS0toUTF8(&outstr, &instr))
790 udf_debug("volIdent[] = '%s'\n", outstr.u_name);
791 strncpy( UDF_SB_VOLIDENT(sb), outstr.u_name, outstr.u_len);
795 if ( !udf_build_ustr(&instr, pvoldesc->volSetIdent, 128) )
797 if (!udf_CS0toUTF8(&outstr, &instr))
798 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
802 static void
803 udf_load_fileset(struct super_block *sb, struct buffer_head *bh, lb_addr *root)
805 struct FileSetDesc *fset;
807 fset = (struct FileSetDesc *)bh->b_data;
809 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
811 UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
813 udf_debug("Rootdir at block=%d, partition=%d\n",
814 root->logicalBlockNum, root->partitionReferenceNum);
817 static void
818 udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
820 struct PartitionDesc *p;
821 int i;
823 p=(struct PartitionDesc *)bh->b_data;
825 for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
827 udf_debug("Searching map: (%d == %d)\n",
828 UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
829 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber))
831 UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */
832 UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation) + UDF_SB_SESSION(sb);
833 UDF_SB_PARTMAPS(sb)[i].s_uspace_bitmap = 0xFFFFFFFF;
835 if (!strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR02) ||
836 !strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR03))
838 struct PartitionHeaderDesc *phd;
840 phd = (struct PartitionHeaderDesc *)(p->partitionContentsUse);
841 if (phd->unallocatedSpaceTable.extLength)
842 udf_debug("unallocatedSpaceTable (part %d)\n", i);
843 if (phd->unallocatedSpaceBitmap.extLength)
845 UDF_SB_PARTMAPS(sb)[i].s_uspace_bitmap =
846 le32_to_cpu(phd->unallocatedSpaceBitmap.extPosition);
847 udf_debug("unallocatedSpaceBitmap (part %d) @ %d\n",
848 i, UDF_SB_PARTMAPS(sb)[i].s_uspace_bitmap);
850 if (phd->partitionIntegrityTable.extLength)
851 udf_debug("partitionIntegrityTable (part %d)\n", i);
852 if (phd->freedSpaceTable.extLength)
853 udf_debug("freedSpaceTable (part %d)\n", i);
854 if (phd->freedSpaceBitmap.extLength)
855 udf_debug("freedSpaceBitmap (part %d\n", i);
857 break;
860 if (i == UDF_SB_NUMPARTS(sb))
862 udf_debug("Partition (%d) not found in partition map\n", le16_to_cpu(p->partitionNumber));
864 else
866 udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
867 le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i),
868 UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i));
872 static int
873 udf_load_logicalvol(struct super_block *sb, struct buffer_head * bh, lb_addr *fileset)
875 struct LogicalVolDesc *lvd;
876 int i, offset;
877 Uint8 type;
879 lvd = (struct LogicalVolDesc *)bh->b_data;
881 UDF_SB_NUMPARTS(sb) = le32_to_cpu(lvd->numPartitionMaps);
882 UDF_SB_ALLOC_PARTMAPS(sb, UDF_SB_NUMPARTS(sb));
884 for (i=0,offset=0;
885 i<UDF_SB_NUMPARTS(sb) && offset<le32_to_cpu(lvd->mapTableLength);
886 i++,offset+=((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength)
888 type = ((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
889 udf_debug("Partition (%d) type %d\n", i, type);
890 if (type == 1)
892 struct GenericPartitionMap1 *gpm1 = (struct GenericPartitionMap1 *)&(lvd->partitionMaps[offset]);
893 UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15;
894 UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum);
895 UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum);
897 else if (type == 2)
899 struct UdfPartitionMap2 *upm2 = (struct UdfPartitionMap2 *)&(lvd->partitionMaps[offset]);
900 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL)))
902 if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0150)
903 UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15;
904 else if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0200)
905 UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20;
907 else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE)))
909 struct SparablePartitionMap *spm = (struct SparablePartitionMap *)&(lvd->partitionMaps[offset]);
910 UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15;
911 UDF_SB_TYPESPAR(sb,i).s_spar_plen = le16_to_cpu(spm->packetLength);
912 UDF_SB_TYPESPAR(sb,i).s_spar_loc = le32_to_cpu(spm->locSparingTable[0]);
914 else
916 udf_debug("Unknown ident: %s\n", upm2->partIdent.ident);
917 continue;
919 UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum);
920 UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum);
924 if (fileset)
926 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
928 *fileset = lelb_to_cpu(la->extLocation);
929 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
930 fileset->logicalBlockNum,
931 fileset->partitionReferenceNum);
933 if (lvd->integritySeqExt.extLength)
934 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
935 return 0;
939 * udf_load_logicalvolint
942 static void
943 udf_load_logicalvolint(struct super_block *sb, extent_ad loc)
945 struct buffer_head *bh = NULL;
946 Uint16 ident;
948 while ((bh = udf_read_tagged(sb, loc.extLocation, loc.extLocation, &ident)) &&
949 ident == TID_LOGICAL_VOL_INTEGRITY_DESC && loc.extLength > 0)
951 UDF_SB_LVIDBH(sb) = bh;
953 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
954 udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
956 if (UDF_SB_LVIDBH(sb) != bh)
957 udf_release_data(bh);
958 loc.extLength -= sb->s_blocksize;
959 loc.extLocation ++;
961 if (UDF_SB_LVIDBH(sb) != bh)
962 udf_release_data(bh);
966 * udf_process_sequence
968 * PURPOSE
969 * Process a main/reserve volume descriptor sequence.
971 * PRE-CONDITIONS
972 * sb Pointer to _locked_ superblock.
973 * block First block of first extent of the sequence.
974 * lastblock Lastblock of first extent of the sequence.
976 * HISTORY
977 * July 1, 1997 - Andrew E. Mileski
978 * Written, tested, and released.
980 static int
981 udf_process_sequence(struct super_block *sb, long block, long lastblock, lb_addr *fileset)
983 struct buffer_head *bh = NULL;
984 struct udf_vds_record vds[VDS_POS_LENGTH];
985 struct GenericDesc *gd;
986 int done=0;
987 int i,j;
988 Uint32 vdsn;
989 Uint16 ident;
991 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
993 /* Read the main descriptor sequence */
994 for (;(!done && block <= lastblock); block++)
997 bh = udf_read_tagged(sb, block, block, &ident);
998 if (!bh)
999 break;
1001 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1002 gd = (struct GenericDesc *)bh->b_data;
1003 vdsn = le32_to_cpu(gd->volDescSeqNum);
1004 switch (ident)
1006 case TID_PRIMARY_VOL_DESC: /* ISO 13346 3/10.1 */
1007 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum)
1009 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
1010 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1012 break;
1013 case TID_VOL_DESC_PTR: /* ISO 13346 3/10.3 */
1014 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum)
1016 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1017 vds[VDS_POS_VOL_DESC_PTR].block = block;
1019 break;
1020 case TID_IMP_USE_VOL_DESC: /* ISO 13346 3/10.4 */
1021 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum)
1023 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
1024 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1026 break;
1027 case TID_PARTITION_DESC: /* ISO 13346 3/10.5 */
1028 if (!vds[VDS_POS_PARTITION_DESC].block)
1029 vds[VDS_POS_PARTITION_DESC].block = block;
1030 break;
1031 case TID_LOGICAL_VOL_DESC: /* ISO 13346 3/10.6 */
1032 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum)
1034 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
1035 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1037 break;
1038 case TID_UNALLOC_SPACE_DESC: /* ISO 13346 3/10.8 */
1039 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum)
1041 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
1042 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1044 break;
1045 case TID_TERMINATING_DESC: /* ISO 13346 3/10.9 */
1046 vds[VDS_POS_TERMINATING_DESC].block = block;
1047 done = 1;
1048 break;
1050 udf_release_data(bh);
1052 for (i=0; i<VDS_POS_LENGTH; i++)
1054 if (vds[i].block)
1056 bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident);
1058 if (i == VDS_POS_PRIMARY_VOL_DESC)
1059 udf_load_pvoldesc(sb, bh);
1060 else if (i == VDS_POS_LOGICAL_VOL_DESC)
1061 udf_load_logicalvol(sb, bh, fileset);
1062 else if (i == VDS_POS_PARTITION_DESC)
1064 struct buffer_head *bh2 = NULL;
1065 udf_load_partdesc(sb, bh);
1066 for (j=vds[i].block+1; j<vds[VDS_POS_TERMINATING_DESC].block; j++)
1068 bh2 = udf_read_tagged(sb, j, j, &ident);
1069 gd = (struct GenericDesc *)bh2->b_data;
1070 if (ident == TID_PARTITION_DESC)
1071 udf_load_partdesc(sb, bh2);
1072 udf_release_data(bh2);
1075 udf_release_data(bh);
1079 return 0;
1083 * udf_check_valid()
1085 static int
1086 udf_check_valid(struct super_block *sb, int novrs, int silent)
1088 long block;
1090 if (novrs)
1092 udf_debug("Validity check skipped because of novrs option\n");
1093 return 0;
1095 /* Check that it is NSR02 compliant */
1096 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1097 else if ((block = udf_vrs(sb, silent)) == -1)
1099 udf_debug("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
1100 return 0;
1102 else
1103 return !block;
1106 static int
1107 udf_load_partition(struct super_block *sb, lb_addr *fileset)
1109 struct AnchorVolDescPtr *anchor;
1110 Uint16 ident;
1111 struct buffer_head *bh;
1112 long main_s, main_e, reserve_s, reserve_e;
1113 int i;
1115 if (!sb)
1116 return 1;
1118 for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
1120 if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
1121 UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i] - UDF_SB_SESSION(sb), &ident)))
1123 anchor = (struct AnchorVolDescPtr *)bh->b_data;
1125 /* Locate the main sequence */
1126 main_s = le32_to_cpu( anchor->mainVolDescSeqExt.extLocation );
1127 main_e = le32_to_cpu( anchor->mainVolDescSeqExt.extLength );
1128 main_e = main_e >> sb->s_blocksize_bits;
1129 main_e += main_s;
1131 /* Locate the reserve sequence */
1132 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1133 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1134 reserve_e = reserve_e >> sb->s_blocksize_bits;
1135 reserve_e += reserve_s;
1137 udf_release_data(bh);
1139 /* Process the main & reserve sequences */
1140 /* responsible for finding the PartitionDesc(s) */
1141 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1142 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1144 break;
1149 if (i == sizeof(UDF_SB_ANCHOR(sb))/sizeof(int))
1151 udf_debug("No Anchor block found\n");
1152 return 1;
1154 else
1155 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1157 for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
1159 switch UDF_SB_PARTTYPE(sb, i)
1161 case UDF_VIRTUAL_MAP15:
1162 case UDF_VIRTUAL_MAP20:
1164 lb_addr ino;
1166 if (!UDF_SB_LASTBLOCK(sb))
1168 udf_debug("Unable to determine Lastblock (For Virtual Partition)\n");
1169 return 1;
1172 if (i == 0)
1173 ino.partitionReferenceNum = i+1;
1174 else
1175 ino.partitionReferenceNum = i-1;
1177 ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) - UDF_SB_PARTROOT(sb,ino.partitionReferenceNum);
1179 if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1180 return 1;
1182 if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP15)
1184 UDF_SB_TYPEVIRT(sb,i).s_start_offset = UDF_I_EXT0OFFS(UDF_SB_VAT(sb));
1185 UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size - 36) / sizeof(Uint32);
1187 else if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP20)
1189 struct buffer_head *bh = NULL;
1190 Uint32 pos;
1192 pos = udf_block_map(UDF_SB_VAT(sb), 0);
1193 bh = bread(sb->s_dev, pos, sb->s_blocksize);
1194 UDF_SB_TYPEVIRT(sb,i).s_start_offset =
1195 le16_to_cpu(((struct VirtualAllocationTable20 *)bh->b_data + UDF_I_EXT0OFFS(UDF_SB_VAT(sb)))->lengthHeader) +
1196 UDF_I_EXT0OFFS(UDF_SB_VAT(sb));
1197 UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
1198 UDF_SB_TYPEVIRT(sb,i).s_start_offset) / sizeof(Uint32);
1199 udf_release_data(bh);
1201 UDF_SB_PARTROOT(sb,i) = udf_get_pblock(sb, 0, i, 0);
1202 UDF_SB_PARTLEN(sb,i) = UDF_SB_PARTLEN(sb,ino.partitionReferenceNum);
1203 UDF_SB_PARTMAPS(sb)[i].s_uspace_bitmap = 0xFFFFFFFF;
1207 return 0;
1210 static void udf_open_lvid(struct super_block *sb)
1212 #ifdef CONFIG_UDF_RW
1213 if (UDF_SB_LVIDBH(sb))
1215 int i;
1216 timestamp cpu_time;
1218 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1219 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1220 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1221 UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1222 UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_OPEN;
1224 UDF_SB_LVID(sb)->descTag.descCRC =
1225 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1226 le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1228 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1229 for (i=0; i<16; i++)
1230 if (i != 4)
1231 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1232 ((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1234 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
1236 #endif
1239 static void udf_close_lvid(struct super_block *sb)
1241 #ifdef CONFIG_UDF_RW
1242 if (UDF_SB_LVIDBH(sb) &&
1243 UDF_SB_LVID(sb)->integrityType == INTEGRITY_TYPE_OPEN)
1245 int i;
1246 timestamp cpu_time;
1248 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1249 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1250 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1251 UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1253 UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_CLOSE;
1255 UDF_SB_LVID(sb)->descTag.descCRC =
1256 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1257 le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1259 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1260 for (i=0; i<16; i++)
1261 if (i != 4)
1262 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1263 ((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1265 mark_buffer_dirty(UDF_SB_LVIDBH(sb), 1);
1267 #endif
1271 * udf_read_super
1273 * PURPOSE
1274 * Complete the specified super block.
1276 * PRE-CONDITIONS
1277 * sb Pointer to superblock to complete - never NULL.
1278 * sb->s_dev Device to read suberblock from.
1279 * options Pointer to mount options.
1280 * silent Silent flag.
1282 * HISTORY
1283 * July 1, 1997 - Andrew E. Mileski
1284 * Written, tested, and released.
1286 static struct super_block *
1287 udf_read_super(struct super_block *sb, void *options, int silent)
1289 struct inode *inode=NULL;
1290 struct udf_options uopt;
1291 lb_addr rootdir, fileset;
1292 int i;
1294 uopt.flags = 0;
1295 uopt.uid = 0;
1296 uopt.gid = 0;
1297 uopt.umask = 0;
1298 uopt.utf8 = 0;
1300 /* Lock the module in memory (if applicable) */
1301 MOD_INC_USE_COUNT;
1303 lock_super(sb);
1305 UDF_SB_PARTMAPS(sb) = NULL;
1306 UDF_SB_LVIDBH(sb) = NULL;
1307 UDF_SB_VAT(sb) = NULL;
1309 if (!udf_parse_options((char *)options, &uopt))
1310 goto error_out;
1312 memset(UDF_SB_ANCHOR(sb), 0x00, sizeof(UDF_SB_ANCHOR(sb)));
1313 fileset.logicalBlockNum = 0xFFFFFFFF;
1314 fileset.partitionReferenceNum = 0xFFFF;
1315 UDF_SB_RECORDTIME(sb)=0;
1316 UDF_SB_VOLIDENT(sb)[0]=0;
1318 UDF_SB(sb)->s_flags = uopt.flags;
1319 UDF_SB(sb)->s_uid = uopt.uid;
1320 UDF_SB(sb)->s_gid = uopt.gid;
1321 UDF_SB(sb)->s_umask = uopt.umask;
1322 UDF_SB(sb)->s_utf8 = uopt.utf8;
1324 /* Set the block size for all transfers */
1325 if (!udf_set_blocksize(sb, uopt.blocksize))
1326 goto error_out;
1328 if ( uopt.session == 0xFFFFFFFF )
1329 UDF_SB_SESSION(sb) = udf_get_last_session(sb->s_dev);
1330 else
1331 UDF_SB_SESSION(sb) = uopt.session;
1333 udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1335 if ( uopt.lastblock == 0xFFFFFFFF )
1336 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb->s_dev, &(UDF_SB(sb)->s_flags));
1337 else
1338 UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1340 UDF_SB_LASTBLOCK(sb) = udf_find_anchor(sb, uopt.anchor, UDF_SB_LASTBLOCK(sb));
1342 udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1344 if (udf_check_valid(sb, uopt.novrs, silent)) /* read volume recognition sequences */
1346 udf_debug("No VRS found\n");
1347 goto error_out;
1350 UDF_SB_CHARSET(sb) = NULL;
1352 #ifdef CONFIG_NLS
1353 if (uopt.utf8 == 0)
1355 char *p = uopt.iocharset ? uopt.iocharset : "iso8859-1";
1356 UDF_SB_CHARSET(sb) = load_nls(p);
1357 if (!UDF_SB_CHARSET(sb))
1358 if (uopt.iocharset)
1359 goto error_out;
1360 UDF_SB_CHARSET(sb) = load_nls_default();
1362 #endif
1364 /* Fill in the rest of the superblock */
1365 sb->s_op = &udf_sb_ops;
1366 sb->s_time = 0;
1367 sb->dq_op = NULL;
1368 sb->s_dirt = 0;
1369 sb->s_magic = UDF_SUPER_MAGIC;
1371 for (i=0; i<UDF_MAX_BLOCK_LOADED; i++)
1373 UDF_SB_BLOCK_BITMAP_NUMBER(sb,i) = 0;
1374 UDF_SB_BLOCK_BITMAP(sb,i) = NULL;
1376 UDF_SB_LOADED_BLOCK_BITMAPS(sb) = 0;
1378 if (udf_load_partition(sb, &fileset))
1380 udf_debug("No partition found (1)\n");
1381 goto error_out;
1384 if ( !UDF_SB_NUMPARTS(sb) )
1386 udf_debug("No partition found (2)\n");
1387 goto error_out;
1390 if ( udf_find_fileset(sb, &fileset, &rootdir) )
1392 udf_debug("No fileset found\n");
1393 goto error_out;
1396 if (!silent)
1398 timestamp ts;
1399 udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb), 0);
1400 udf_info("Mounting volume '%s', timestamp %u/%02u/%u %02u:%02u\n",
1401 UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute);
1403 if (!(sb->s_flags & MS_RDONLY))
1404 udf_open_lvid(sb);
1405 unlock_super(sb);
1407 /* Assign the root inode */
1408 /* assign inodes by physical block number */
1409 /* perhaps it's not extensible enough, but for now ... */
1410 inode = udf_iget(sb, rootdir);
1411 if (!inode)
1413 udf_debug("Error in udf_iget, block=%d, partition=%d\n",
1414 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1415 goto error_out;
1418 /* Allocate a dentry for the root inode */
1419 sb->s_root = d_alloc_root(inode);
1420 if (!sb->s_root)
1422 iput(inode);
1423 udf_debug("Couldn't allocate root dentry\n");
1424 goto error_out;
1427 return sb;
1429 error_out:
1430 sb->s_dev = NODEV;
1431 if (UDF_SB_VAT(sb))
1432 iput(UDF_SB_VAT(sb));
1433 if (!(sb->s_flags & MS_RDONLY))
1434 udf_close_lvid(sb);
1435 udf_release_data(UDF_SB_LVIDBH(sb));
1436 UDF_SB_FREE(sb);
1437 unlock_super(sb);
1438 MOD_DEC_USE_COUNT;
1439 return NULL;
1442 void udf_error(struct super_block *sb, const char *function,
1443 const char *fmt, ...)
1445 va_list args;
1447 if (!(sb->s_flags & MS_RDONLY))
1449 /* mark sb error */
1450 sb->s_dirt = 1;
1452 va_start(args, fmt);
1453 vsprintf(error_buf, fmt, args);
1454 va_end(args);
1455 printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1456 bdevname(sb->s_dev), function, error_buf);
1459 void udf_warning(struct super_block *sb, const char *function,
1460 const char *fmt, ...)
1462 va_list args;
1464 va_start (args, fmt);
1465 vsprintf(error_buf, fmt, args);
1466 va_end(args);
1467 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1468 bdevname(sb->s_dev), function, error_buf);
1472 * udf_put_super
1474 * PURPOSE
1475 * Prepare for destruction of the superblock.
1477 * DESCRIPTION
1478 * Called before the filesystem is unmounted.
1480 * HISTORY
1481 * July 1, 1997 - Andrew E. Mileski
1482 * Written, tested, and released.
1484 static void
1485 udf_put_super(struct super_block *sb)
1487 int i;
1489 if (UDF_SB_VAT(sb))
1490 iput(UDF_SB_VAT(sb));
1491 if (!(sb->s_flags & MS_RDONLY))
1492 udf_close_lvid(sb);
1493 udf_release_data(UDF_SB_LVIDBH(sb));
1494 for (i=0; i<UDF_MAX_BLOCK_LOADED; i++)
1495 udf_release_data(UDF_SB_BLOCK_BITMAP(sb, i));
1496 UDF_SB_FREE(sb);
1498 MOD_DEC_USE_COUNT;
1502 * udf_stat_fs
1504 * PURPOSE
1505 * Return info about the filesystem.
1507 * DESCRIPTION
1508 * Called by sys_statfs()
1510 * HISTORY
1511 * July 1, 1997 - Andrew E. Mileski
1512 * Written, tested, and released.
1514 static int
1515 udf_statfs(struct super_block *sb, struct statfs *buf, int bufsize)
1517 int size;
1518 struct statfs tmp;
1519 int rc;
1521 size = (bufsize < sizeof(tmp)) ? bufsize: sizeof(tmp);
1523 memset(&tmp, 0, sizeof(tmp));
1524 tmp.f_type = UDF_SUPER_MAGIC;
1525 tmp.f_bsize = sb->s_blocksize;
1526 tmp.f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1527 tmp.f_bfree = udf_count_free(sb);
1528 tmp.f_bavail = tmp.f_bfree;
1529 tmp.f_files = (UDF_SB_LVIDBH(sb) ?
1530 (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
1531 le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + tmp.f_bfree;
1532 tmp.f_ffree = tmp.f_bfree;
1533 /* __kernel_fsid_t f_fsid */
1534 tmp.f_namelen = UDF_NAME_LEN;
1536 rc= copy_to_user(buf, &tmp, size) ? -EFAULT: 0;
1537 return rc;
1540 static unsigned char udf_bitmap_lookup[16] = {
1541 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1544 static unsigned int
1545 udf_count_free(struct super_block *sb)
1547 struct buffer_head *bh = NULL;
1548 unsigned int accum=0;
1549 int index;
1550 int block=0, newblock;
1551 lb_addr loc;
1552 Uint32 bytes;
1553 Uint8 value;
1554 Uint8 * ptr;
1555 Uint16 ident;
1557 if (UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace_bitmap == 0xFFFFFFFF)
1559 if (UDF_SB_LVIDBH(sb))
1561 if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb))
1562 accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
1564 if (accum == 0xFFFFFFFF)
1565 accum = 0;
1567 return accum;
1569 else
1570 return 0;
1572 else
1574 struct SpaceBitmapDesc *bm;
1575 loc.logicalBlockNum = UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace_bitmap;
1576 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1577 bh = udf_read_ptagged(sb, loc, 0, &ident);
1579 if (!bh)
1581 printk(KERN_ERR "udf: udf_count_free failed\n");
1582 return 0;
1584 else if (ident != TID_SPACE_BITMAP_DESC)
1586 udf_release_data(bh);
1587 printk(KERN_ERR "udf: udf_count_free failed\n");
1588 return 0;
1591 bm = (struct SpaceBitmapDesc *)bh->b_data;
1592 bytes = bm->numOfBytes;
1593 index = sizeof(struct SpaceBitmapDesc); /* offset in first block only */
1594 ptr = (Uint8 *)bh->b_data;
1596 while ( bytes > 0 )
1598 while ((bytes > 0) && (index < sb->s_blocksize))
1600 value = ptr[index];
1601 accum += udf_bitmap_lookup[ value & 0x0f ];
1602 accum += udf_bitmap_lookup[ value >> 4 ];
1603 index++;
1604 bytes--;
1606 if ( bytes )
1608 udf_release_data(bh);
1609 newblock = udf_get_lb_pblock(sb, loc, ++block);
1610 bh = udf_tread(sb, newblock, sb->s_blocksize);
1611 if (!bh)
1613 udf_debug("read failed\n");
1614 return accum;
1616 index = 0;
1617 ptr = (Uint8 *)bh->b_data;
1620 udf_release_data(bh);
1621 return accum;