File: /usr/src/linux/fs/udf/super.c

1     /*
2      * super.c
3      *
4      * PURPOSE
5      *  Super block routines for the OSTA-UDF(tm) filesystem.
6      *
7      * DESCRIPTION
8      *  OSTA-UDF(tm) = Optical Storage Technology Association
9      *  Universal Disk Format.
10      *
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/
16      *
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@hpesjro.fc.hp.com
21      *
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.
27      *
28      *  (C) 1998 Dave Boynton
29      *  (C) 1998-2000 Ben Fennema
30      *  (C) 2000 Stelias Computing Inc
31      *
32      * HISTORY
33      *
34      *  09/24/98 dgb  changed to allow compiling outside of kernel, and
35      *                added some debugging.
36      *  10/01/98 dgb  updated to allow (some) possibility of compiling w/2.0.34
37      *  10/16/98      attempting some multi-session support
38      *  10/17/98      added freespace count for "df"
39      *  11/11/98 gr   added novrs option
40      *  11/26/98 dgb  added fileset,anchor mount options
41      *  12/06/98 blf  really hosed things royally. vat/sparing support. sequenced vol descs
42      *                rewrote option handling based on isofs
43      *  12/20/98      find the free space bitmap (if it exists)
44      */
45     
46     #include "udfdecl.h"    
47     
48     #include <linux/config.h>
49     #include <linux/version.h>
50     #include <linux/blkdev.h>
51     #include <linux/slab.h>
52     #include <linux/kernel.h>
53     #include <linux/locks.h>
54     #include <linux/module.h>
55     #include <linux/stat.h>
56     #include <linux/cdrom.h>
57     #include <linux/nls.h>
58     #include <asm/byteorder.h>
59     
60     #include <linux/udf_fs.h>
61     #include "udf_sb.h"
62     #include "udf_i.h"
63     
64     #include <linux/init.h>
65     #include <asm/uaccess.h>
66     
67     #define VDS_POS_PRIMARY_VOL_DESC	0
68     #define VDS_POS_UNALLOC_SPACE_DESC	1
69     #define VDS_POS_LOGICAL_VOL_DESC	2
70     #define VDS_POS_PARTITION_DESC		3
71     #define VDS_POS_IMP_USE_VOL_DESC	4
72     #define VDS_POS_VOL_DESC_PTR		5
73     #define VDS_POS_TERMINATING_DESC	6
74     #define VDS_POS_LENGTH			7
75     
76     static char error_buf[1024];
77     
78     /* These are the "meat" - everything else is stuffing */
79     static struct super_block *udf_read_super(struct super_block *, void *, int);
80     static void udf_put_super(struct super_block *);
81     static void udf_write_super(struct super_block *);
82     static int udf_remount_fs(struct super_block *, int *, char *);
83     static int udf_check_valid(struct super_block *, int, int);
84     static int udf_vrs(struct super_block *sb, int silent);
85     static int udf_load_partition(struct super_block *, lb_addr *);
86     static int udf_load_logicalvol(struct super_block *, struct buffer_head *, lb_addr *);
87     static void udf_load_logicalvolint(struct super_block *, extent_ad);
88     static int udf_find_anchor(struct super_block *, int, int);
89     static int udf_find_fileset(struct super_block *, lb_addr *, 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 *, lb_addr *);
92     static void 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 super_block *, struct statfs *);
97     
98     /* UDF filesystem type */
99     static DECLARE_FSTYPE_DEV(udf_fstype, "udf", udf_read_super);
100     
101     /* Superblock operations */
102     static struct super_operations udf_sb_ops = {
103     	read_inode:		udf_read_inode,
104     	write_inode:		udf_write_inode,
105     	put_inode:		udf_put_inode,
106     	delete_inode:		udf_delete_inode,
107     	put_super:		udf_put_super,
108     	write_super:		udf_write_super,
109     	statfs:			udf_statfs,
110     	remount_fs:		udf_remount_fs,
111     };
112     
113     struct udf_options
114     {
115     	unsigned char novrs;
116     	unsigned int blocksize;
117     	unsigned int session;
118     	unsigned int lastblock;
119     	unsigned int anchor;
120     	unsigned int volume;
121     	unsigned short partition;
122     	unsigned int fileset;
123     	unsigned int rootdir;
124     	unsigned int flags;
125     	mode_t umask;
126     	gid_t gid;
127     	uid_t uid;
128     	struct nls_table *nls_map;
129     };
130     
131     static int __init init_udf_fs(void)
132     {
133     	printk(KERN_NOTICE "udf: registering filesystem\n");
134     	return register_filesystem(&udf_fstype);
135     }
136     
137     static void __exit exit_udf_fs(void)
138     {
139     	printk(KERN_NOTICE "udf: unregistering filesystem\n");
140     	unregister_filesystem(&udf_fstype);
141     }
142     
143     EXPORT_NO_SYMBOLS;
144     
145     module_init(init_udf_fs)
146     module_exit(exit_udf_fs)
147     
148     /*
149      * udf_parse_options
150      *
151      * PURPOSE
152      *	Parse mount options.
153      *
154      * DESCRIPTION
155      *	The following mount options are supported:
156      *
157      *	gid=		Set the default group.
158      *	umask=		Set the default umask.
159      *	uid=		Set the default user.
160      *	bs=			Set the block size.
161      *	unhide		Show otherwise hidden files.
162      *	undelete	Show deleted files in lists.
163      *	adinicb		Embed data in the inode (default)
164      *	noadinicb	Don't embed data in the inode
165      *	shortad		Use short ad's
166      *	longad		Use long ad's (default)
167      *	strict		Set strict conformance
168      *	iocharset=	Set the NLS character set
169      *
170      *	The remaining are for debugging and disaster recovery:
171      *
172      *	novrs		Skip volume sequence recognition 
173      *
174      *	The following expect a offset from 0.
175      *
176      *	session=	Set the CDROM session (default= last session)
177      *	anchor=		Override standard anchor location. (default= 256)
178      *	volume=		Override the VolumeDesc location. (unused)
179      *	partition=	Override the PartitionDesc location. (unused)
180      *	lastblock=	Set the last block of the filesystem/
181      *
182      *	The following expect a offset from the partition root.
183      *
184      *	fileset=	Override the fileset block location. (unused)
185      *	rootdir=	Override the root directory location. (unused)
186      *		WARNING: overriding the rootdir to a non-directory may
187      *		yield highly unpredictable results.
188      *
189      * PRE-CONDITIONS
190      *	options		Pointer to mount options string.
191      *	uopts		Pointer to mount options variable.
192      *
193      * POST-CONDITIONS
194      *	<return>	0	Mount options parsed okay.
195      *	<return>	-1	Error parsing mount options.
196      *
197      * HISTORY
198      *	July 1, 1997 - Andrew E. Mileski
199      *	Written, tested, and released.
200      */
201     
202     static int
203     udf_parse_options(char *options, struct udf_options *uopt)
204     {
205     	char *opt, *val;
206     
207     	uopt->novrs = 0;
208     	uopt->blocksize = 2048;
209     	uopt->partition = 0xFFFF;
210     	uopt->session = 0xFFFFFFFF;
211     	uopt->lastblock = 0xFFFFFFFF;
212     	uopt->anchor = 0xFFFFFFFF;
213     	uopt->volume = 0xFFFFFFFF;
214     	uopt->rootdir = 0xFFFFFFFF;
215     	uopt->fileset = 0xFFFFFFFF;
216     	uopt->nls_map = NULL;
217     
218     	if (!options)
219     		return 1;
220     
221     	for (opt = strtok(options, ","); opt; opt = strtok(NULL, ","))
222     	{
223     		/* Make "opt=val" into two strings */
224     		val = strchr(opt, '=');
225     		if (val)
226     			*(val++) = 0;
227     		if (!strcmp(opt, "novrs") && !val)
228     			uopt->novrs = 1;
229     		else if (!strcmp(opt, "bs") && val)
230     			uopt->blocksize = simple_strtoul(val, NULL, 0);
231     		else if (!strcmp(opt, "unhide") && !val)
232     			uopt->flags |= (1 << UDF_FLAG_UNHIDE);
233     		else if (!strcmp(opt, "undelete") && !val)
234     			uopt->flags |= (1 << UDF_FLAG_UNDELETE);
235     		else if (!strcmp(opt, "noadinicb") && !val)
236     			uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
237     		else if (!strcmp(opt, "adinicb") && !val)
238     			uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
239     		else if (!strcmp(opt, "shortad") && !val)
240     			uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
241     		else if (!strcmp(opt, "longad") && !val)
242     			uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
243     		else if (!strcmp(opt, "gid") && val)
244     			uopt->gid = simple_strtoul(val, NULL, 0);
245     		else if (!strcmp(opt, "umask") && val)
246     			uopt->umask = simple_strtoul(val, NULL, 0);
247     		else if (!strcmp(opt, "strict") && !val)
248     			uopt->flags |= (1 << UDF_FLAG_STRICT);
249     		else if (!strcmp(opt, "uid") && val)
250     			uopt->uid = simple_strtoul(val, NULL, 0);
251     		else if (!strcmp(opt, "session") && val)
252     			uopt->session = simple_strtoul(val, NULL, 0);
253     		else if (!strcmp(opt, "lastblock") && val)
254     			uopt->lastblock = simple_strtoul(val, NULL, 0);
255     		else if (!strcmp(opt, "anchor") && val)
256     			uopt->anchor = simple_strtoul(val, NULL, 0);
257     		else if (!strcmp(opt, "volume") && val)
258     			uopt->volume = simple_strtoul(val, NULL, 0);
259     		else if (!strcmp(opt, "partition") && val)
260     			uopt->partition = simple_strtoul(val, NULL, 0);
261     		else if (!strcmp(opt, "fileset") && val)
262     			uopt->fileset = simple_strtoul(val, NULL, 0);
263     		else if (!strcmp(opt, "rootdir") && val)
264     			uopt->rootdir = simple_strtoul(val, NULL, 0);
265     #ifdef CONFIG_NLS
266     		else if (!strcmp(opt, "iocharset") && val)
267     		{
268     			uopt->nls_map = load_nls(val);
269     			uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
270     		}
271     #endif
272     		else if (!strcmp(opt, "utf8") && !val)
273     			uopt->flags |= (1 << UDF_FLAG_UTF8);
274     		else if (val)
275     		{
276     			printk(KERN_ERR "udf: bad mount option \"%s=%s\"\n",
277     				opt, val);
278     			return 0;
279     		}
280     		else
281     		{
282     			printk(KERN_ERR "udf: bad mount option \"%s\"\n",
283     				opt);
284     			return 0;
285     		}
286     	}
287     	return 1;
288     }
289     
290     void
291     udf_write_super(struct super_block *sb)
292     {
293     	if (!(sb->s_flags & MS_RDONLY))
294     		udf_open_lvid(sb);
295     	sb->s_dirt = 0;
296     }
297     
298     static int
299     udf_remount_fs(struct super_block *sb, int *flags, char *options)
300     {
301     	struct udf_options uopt;
302     
303     	uopt.flags = UDF_SB(sb)->s_flags ;
304     	uopt.uid   = UDF_SB(sb)->s_uid ;
305     	uopt.gid   = UDF_SB(sb)->s_gid ;
306     	uopt.umask = UDF_SB(sb)->s_umask ;
307     
308     	if ( !udf_parse_options(options, &uopt) )
309     		return -EINVAL;
310     
311     	UDF_SB(sb)->s_flags = uopt.flags;
312     	UDF_SB(sb)->s_uid   = uopt.uid;
313     	UDF_SB(sb)->s_gid   = uopt.gid;
314     	UDF_SB(sb)->s_umask = uopt.umask;
315     
316     #if UDFFS_RW != 1
317     	*flags |= MS_RDONLY;
318     #endif
319     
320     	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
321     		return 0;
322     	if (*flags & MS_RDONLY)
323     		udf_close_lvid(sb);
324     	else
325     		udf_open_lvid(sb);
326     
327     	return 0;
328     }
329     
330     /*
331      * udf_set_blocksize
332      *
333      * PURPOSE
334      *	Set the block size to be used in all transfers.
335      *
336      * DESCRIPTION
337      *	To allow room for a DMA transfer, it is best to guess big when unsure.
338      *	This routine picks 2048 bytes as the blocksize when guessing. This
339      *	should be adequate until devices with larger block sizes become common.
340      *
341      *	Note that the Linux kernel can currently only deal with blocksizes of
342      *	512, 1024, 2048, 4096, and 8192 bytes.
343      *
344      * PRE-CONDITIONS
345      *	sb			Pointer to _locked_ superblock.
346      *
347      * POST-CONDITIONS
348      *	sb->s_blocksize		Blocksize.
349      *	sb->s_blocksize_bits	log2 of blocksize.
350      *	<return>	0	Blocksize is valid.
351      *	<return>	1	Blocksize is invalid.
352      *
353      * HISTORY
354      *	July 1, 1997 - Andrew E. Mileski
355      *	Written, tested, and released.
356      */
357     static  int
358     udf_set_blocksize(struct super_block *sb, int bsize)
359     {
360     	/* Use specified block size if specified */
361     	if (bsize)
362     		sb->s_blocksize = bsize;
363     	if (get_hardsect_size(sb->s_dev) > sb->s_blocksize)
364     		sb->s_blocksize = get_hardsect_size(sb->s_dev); 
365     
366     	/* Block size must be an even multiple of 512 */
367     	switch (sb->s_blocksize)
368     	{
369     		case 512: sb->s_blocksize_bits = 9;	break;
370     		case 1024: sb->s_blocksize_bits = 10; break;
371     		case 2048: sb->s_blocksize_bits = 11; break;
372     		case 4096: sb->s_blocksize_bits = 12; break;
373     		case 8192: sb->s_blocksize_bits = 13; break;
374     		default:
375     		{
376     			udf_debug("Bad block size (%ld)\n", sb->s_blocksize);
377     			printk(KERN_ERR "udf: bad block size (%ld)\n", sb->s_blocksize);
378     			return 0;
379     		}
380     	}
381     
382     	/* Set the block size */
383     	set_blocksize(sb->s_dev, sb->s_blocksize);
384     	return sb->s_blocksize;
385     }
386     
387     static int
388     udf_vrs(struct super_block *sb, int silent)
389     {
390     	struct VolStructDesc *vsd = NULL;
391     	int sector = 32768;
392     	int sectorsize;
393     	struct buffer_head *bh = NULL;
394     	int iso9660=0;
395     	int nsr02=0;
396     	int nsr03=0;
397     
398     	/* Block size must be a multiple of 512 */
399     	if (sb->s_blocksize & 511)
400     		return 0;
401     
402     	if (sb->s_blocksize < sizeof(struct VolStructDesc))
403     		sectorsize = sizeof(struct VolStructDesc);
404     	else
405     		sectorsize = sb->s_blocksize;
406     
407     	sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
408     
409     	udf_debug("Starting at sector %u (%ld byte sectors)\n",
410     		(sector >> sb->s_blocksize_bits), sb->s_blocksize);
411     	/* Process the sequence (if applicable) */
412     	for (;!nsr02 && !nsr03; sector += sectorsize)
413     	{
414     		/* Read a block */
415     		bh = udf_tread(sb, sector >> sb->s_blocksize_bits, sb->s_blocksize);
416     		if (!bh)
417     			break;
418     
419     		/* Look for ISO  descriptors */
420     		vsd = (struct VolStructDesc *)(bh->b_data +
421     			(sector & (sb->s_blocksize - 1)));
422     
423     		if (vsd->stdIdent[0] == 0)
424     		{
425     			udf_release_data(bh);
426     			break;
427     		}
428     		else if (!strncmp(vsd->stdIdent, STD_ID_CD001, STD_ID_LEN))
429     		{
430     			iso9660 = sector;
431     			switch (vsd->structType)
432     			{
433     				case 0: 
434     					udf_debug("ISO9660 Boot Record found\n");
435     					break;
436     				case 1: 
437     					udf_debug("ISO9660 Primary Volume Descriptor found\n");
438     					break;
439     				case 2: 
440     					udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
441     					break;
442     				case 3: 
443     					udf_debug("ISO9660 Volume Partition Descriptor found\n");
444     					break;
445     				case 255: 
446     					udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
447     					break;
448     				default: 
449     					udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
450     					break;
451     			}
452     		}
453     		else if (!strncmp(vsd->stdIdent, STD_ID_BEA01, STD_ID_LEN))
454     		{
455     		}
456     		else if (!strncmp(vsd->stdIdent, STD_ID_TEA01, STD_ID_LEN))
457     		{
458     			udf_release_data(bh);
459     			break;
460     		}
461     		else if (!strncmp(vsd->stdIdent, STD_ID_NSR02, STD_ID_LEN))
462     		{
463     			nsr02 = sector;
464     		}
465     		else if (!strncmp(vsd->stdIdent, STD_ID_NSR03, STD_ID_LEN))
466     		{
467     			nsr03 = sector;
468     		}
469     		udf_release_data(bh);
470     	}
471     
472     	if (nsr03)
473     		return nsr03;
474     	else if (nsr02)
475     		return nsr02;
476     	else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
477     		return -1;
478     	else
479     		return 0;
480     }
481     
482     /*
483      * udf_find_anchor
484      *
485      * PURPOSE
486      *	Find an anchor volume descriptor.
487      *
488      * PRE-CONDITIONS
489      *	sb			Pointer to _locked_ superblock.
490      *	lastblock		Last block on media.
491      *
492      * POST-CONDITIONS
493      *	<return>		1 if not found, 0 if ok
494      *
495      * HISTORY
496      *	July 1, 1997 - Andrew E. Mileski
497      *	Written, tested, and released.
498      */
499     static int
500     udf_find_anchor(struct super_block *sb, int useranchor, int lastblock)
501     {
502     	int varlastblock = udf_variable_to_fixed(lastblock);
503     	int last[] =  { lastblock, lastblock - 2,
504     			lastblock - 150, lastblock - 152,
505     			varlastblock, varlastblock - 2,
506     			varlastblock - 150, varlastblock - 152 };
507     	struct buffer_head *bh = NULL;
508     	Uint16 ident;
509     	Uint32 location;
510     	int i;
511     
512     	UDF_SB_ANCHOR(sb)[0] = 0;
513     	UDF_SB_ANCHOR(sb)[1] = 0;
514     	UDF_SB_ANCHOR(sb)[2] = 0;
515     	UDF_SB_ANCHOR(sb)[3] = 256 + UDF_SB_SESSION(sb);
516     
517     	lastblock = 0;
518     
519     	/* Search for an anchor volume descriptor pointer */
520     
521     	/*  according to spec, anchor is in either:
522     	 *     block 256
523     	 *     lastblock-256
524     	 *     lastblock
525     	 *  however, if the disc isn't closed, it could be 512 */
526     
527     	for (i=0; (!lastblock && i<sizeof(last)/sizeof(int)); i++)
528     	{
529     		if (last[i] < 0 || !(bh = bread(sb->s_dev, last[i], sb->s_blocksize)))
530     		{
531     			ident = location = 0;
532     		}
533     		else
534     		{
535     			ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
536     			location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
537     			udf_release_data(bh);
538     		}
539     
540     		if (ident == TID_ANCHOR_VOL_DESC_PTR)
541     		{
542     			if (location == last[i] - UDF_SB_SESSION(sb))
543     			{
544     				lastblock = UDF_SB_ANCHOR(sb)[0] = last[i];
545     				UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
546     			}
547     			else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb))
548     			{
549     				UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
550     				lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]);
551     				UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
552     			}
553     			else
554     				udf_debug("Anchor found at block %d, location mismatch %d.\n",
555     					last[i], location);
556     		}
557     		else if (ident == TID_FILE_ENTRY || ident == TID_EXTENDED_FILE_ENTRY)
558     		{
559     			lastblock = last[i];
560     			UDF_SB_ANCHOR(sb)[2] = 512 + UDF_SB_SESSION(sb);
561     		}
562     		else
563     		{
564     			if (!(bh = bread(sb->s_dev, last[i] - 256, sb->s_blocksize)))
565     			{
566     				ident = location = 0;
567     			}
568     			else
569     			{
570     				ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
571     				location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
572     				udf_release_data(bh);
573     			}
574     
575     			if (ident == TID_ANCHOR_VOL_DESC_PTR &&
576     				location == last[i] - 256 - UDF_SB_SESSION(sb))
577     			{
578     				lastblock = last[i];
579     				UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
580     			}
581     			else
582     			{
583     				if (!(bh = bread(sb->s_dev, last[i] - 312 - UDF_SB_SESSION(sb),
584     					sb->s_blocksize)))
585     				{
586     					ident = location = 0;
587     				}
588     				else
589     				{
590     					ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
591     					location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
592     					udf_release_data(bh);
593     				}
594     
595     				if (ident == TID_ANCHOR_VOL_DESC_PTR &&
596     					location == udf_variable_to_fixed(last[i]) - 256)
597     				{
598     					UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
599     					lastblock = udf_variable_to_fixed(last[i]);
600     					UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
601     				}
602     			}
603     		}
604     	}
605     
606     	if (!lastblock)
607     	{
608     		/* We havn't found the lastblock. check 312 */
609     		if ((bh = bread(sb->s_dev, 312 + UDF_SB_SESSION(sb), sb->s_blocksize)))
610     		{
611     			ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
612     			location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
613     			udf_release_data(bh);
614     
615     			if (ident == TID_ANCHOR_VOL_DESC_PTR && location == 256)
616     				UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
617     		}
618     	}
619     
620     	for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
621     	{
622     		if (UDF_SB_ANCHOR(sb)[i])
623     		{
624     			if (!(bh = udf_read_tagged(sb,
625     				UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
626     			{
627     				UDF_SB_ANCHOR(sb)[i] = 0;
628     			}
629     			else
630     			{
631     				udf_release_data(bh);
632     				if ((ident != TID_ANCHOR_VOL_DESC_PTR) && (i ||
633     					(ident != TID_FILE_ENTRY && ident != TID_EXTENDED_FILE_ENTRY)))
634     				{
635     					UDF_SB_ANCHOR(sb)[i] = 0;
636     				}
637     			}
638     		}
639     		else if (useranchor != 0xFFFFFFFF)
640     		{
641     			UDF_SB_ANCHOR(sb)[i] = useranchor;
642     			useranchor = 0xFFFFFFFF;
643     			i --;
644     		}
645     	}
646     
647     	return lastblock;
648     }
649     
650     static int 
651     udf_find_fileset(struct super_block *sb, lb_addr *fileset, lb_addr *root)
652     {
653     	struct buffer_head *bh = NULL;
654     	long lastblock;
655     	Uint16 ident;
656     
657     	if (fileset->logicalBlockNum != 0xFFFFFFFF ||
658     		fileset->partitionReferenceNum != 0xFFFF)
659     	{
660     		bh = udf_read_ptagged(sb, *fileset, 0, &ident);
661     
662     		if (!bh)
663     			return 1;
664     		else if (ident != TID_FILE_SET_DESC)
665     		{
666     			udf_release_data(bh);
667     			return 1;
668     		}
669     			
670     	}
671     
672     	if (!bh) /* Search backwards through the partitions */
673     	{
674     		lb_addr newfileset;
675     
676     		return 1;
677     		
678     		for (newfileset.partitionReferenceNum=UDF_SB_NUMPARTS(sb)-1;
679     			(newfileset.partitionReferenceNum != 0xFFFF &&
680     				fileset->logicalBlockNum == 0xFFFFFFFF &&
681     				fileset->partitionReferenceNum == 0xFFFF);
682     			newfileset.partitionReferenceNum--)
683     		{
684     			lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum);
685     			newfileset.logicalBlockNum = 0;
686     
687     			do
688     			{
689     				bh = udf_read_ptagged(sb, newfileset, 0, &ident);
690     				if (!bh)
691     				{
692     					newfileset.logicalBlockNum ++;
693     					continue;
694     				}
695     
696     				switch (ident)
697     				{
698     					case TID_SPACE_BITMAP_DESC:
699     					{
700     						struct SpaceBitmapDesc *sp;
701     						sp = (struct SpaceBitmapDesc *)bh->b_data;
702     						newfileset.logicalBlockNum += 1 +
703     							((le32_to_cpu(sp->numOfBytes) + sizeof(struct SpaceBitmapDesc) - 1)
704     								>> sb->s_blocksize_bits);
705     						udf_release_data(bh);
706     						break;
707     					}
708     					case TID_FILE_SET_DESC:
709     					{
710     						*fileset = newfileset;
711     						break;
712     					}
713     					default:
714     					{
715     						newfileset.logicalBlockNum ++;
716     						udf_release_data(bh);
717     						bh = NULL;
718     						break;
719     					}
720     				}
721     			}
722     			while (newfileset.logicalBlockNum < lastblock &&
723     				fileset->logicalBlockNum == 0xFFFFFFFF &&
724     				fileset->partitionReferenceNum == 0xFFFF);
725     		}
726     	}
727     
728     	if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
729     		fileset->partitionReferenceNum != 0xFFFF) && bh)
730     	{
731     		udf_debug("Fileset at block=%d, partition=%d\n",
732     			fileset->logicalBlockNum, fileset->partitionReferenceNum);
733     
734     		UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
735     		udf_load_fileset(sb, bh, root);
736     		udf_release_data(bh);
737     		return 0;
738     	}
739     	return 1;
740     }
741     
742     static void 
743     udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
744     {
745     	struct PrimaryVolDesc *pvoldesc;
746     	time_t recording;
747     	long recording_usec;
748     	struct ustr instr;
749     	struct ustr outstr;
750     
751     	pvoldesc = (struct PrimaryVolDesc *)bh->b_data;
752     
753     	if ( udf_stamp_to_time(&recording, &recording_usec,
754     		lets_to_cpu(pvoldesc->recordingDateAndTime)) )
755     	{
756     		timestamp ts;
757     		ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
758     		udf_debug("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n",
759     			recording, recording_usec,
760     			ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.typeAndTimezone);
761     		UDF_SB_RECORDTIME(sb) = recording;
762     	}
763     
764     	if ( !udf_build_ustr(&instr, pvoldesc->volIdent, 32) )
765     	{
766     		if (udf_CS0toUTF8(&outstr, &instr))
767     		{
768     			strncpy( UDF_SB_VOLIDENT(sb), outstr.u_name,
769     				outstr.u_len > 31 ? 31 : outstr.u_len);
770     			udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
771     		}
772     	}
773     
774     	if ( !udf_build_ustr(&instr, pvoldesc->volSetIdent, 128) )
775     	{
776     		if (udf_CS0toUTF8(&outstr, &instr))
777     			udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
778     	}
779     }
780     
781     static void 
782     udf_load_fileset(struct super_block *sb, struct buffer_head *bh, lb_addr *root)
783     {
784     	struct FileSetDesc *fset;
785     
786     	fset = (struct FileSetDesc *)bh->b_data;
787     
788     	*root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
789     
790     	UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
791     
792     	udf_debug("Rootdir at block=%d, partition=%d\n", 
793     		root->logicalBlockNum, root->partitionReferenceNum);
794     }
795     
796     static void 
797     udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
798     {
799     	struct PartitionDesc *p;
800     	int i;
801     
802     	p = (struct PartitionDesc *)bh->b_data;
803     
804     	for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
805     	{
806     		udf_debug("Searching map: (%d == %d)\n", 
807     			UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
808     		if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber))
809     		{
810     			UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */
811     			UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation) + UDF_SB_SESSION(sb);
812     
813     			if (!strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR02) ||
814     				!strcmp(p->partitionContents.ident, PARTITION_CONTENTS_NSR03))
815     			{
816     				struct PartitionHeaderDesc *phd;
817     
818     				phd = (struct PartitionHeaderDesc *)(p->partitionContentsUse);
819     				if (phd->unallocatedSpaceTable.extLength)
820     				{
821     					lb_addr loc = { le32_to_cpu(phd->unallocatedSpaceTable.extPosition), i };
822     
823     					UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table =
824     						udf_iget(sb, loc);
825     					UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_TABLE;
826     					udf_debug("unallocatedSpaceTable (part %d) @ %ld\n",
827     						i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table->i_ino);
828     				}
829     				if (phd->unallocatedSpaceBitmap.extLength)
830     				{
831     					UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
832     					if (UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap != NULL)
833     					{
834     						UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extLength =
835     							le32_to_cpu(phd->unallocatedSpaceBitmap.extLength);
836     						UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition =
837     							le32_to_cpu(phd->unallocatedSpaceBitmap.extPosition);
838     						UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_BITMAP;
839     						udf_debug("unallocatedSpaceBitmap (part %d) @ %d\n",
840     							i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition);
841     					}
842     				}
843     				if (phd->partitionIntegrityTable.extLength)
844     					udf_debug("partitionIntegrityTable (part %d)\n", i);
845     				if (phd->freedSpaceTable.extLength)
846     				{
847     					lb_addr loc = { le32_to_cpu(phd->freedSpaceTable.extPosition), i };
848     
849     					UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table =
850     						udf_iget(sb, loc);
851     					UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_TABLE;
852     					udf_debug("freedSpaceTable (part %d) @ %ld\n",
853     						i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table->i_ino);
854     				}
855     				if (phd->freedSpaceBitmap.extLength)
856     				{
857     					UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
858     					if (UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap != NULL)
859     					{
860     						UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extLength =
861     							le32_to_cpu(phd->freedSpaceBitmap.extLength);
862     						UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition =
863     							le32_to_cpu(phd->freedSpaceBitmap.extPosition);
864     						UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_BITMAP;
865     						udf_debug("freedSpaceBitmap (part %d) @ %d\n",
866     							i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition);
867     					}
868     				}
869     			}
870     			break;
871     		}
872     	}
873     	if (i == UDF_SB_NUMPARTS(sb))
874     	{
875     		udf_debug("Partition (%d) not found in partition map\n", le16_to_cpu(p->partitionNumber));
876     	}
877     	else
878     	{
879     		udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
880     			le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i),
881     			UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i));
882     	}
883     }
884     
885     static int 
886     udf_load_logicalvol(struct super_block *sb, struct buffer_head * bh, lb_addr *fileset)
887     {
888     	struct LogicalVolDesc *lvd;
889     	int i, j, offset;
890     	Uint8 type;
891     
892     	lvd = (struct LogicalVolDesc *)bh->b_data;
893     
894     	UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
895     
896     	for (i=0,offset=0;
897     		 i<UDF_SB_NUMPARTS(sb) && offset<le32_to_cpu(lvd->mapTableLength);
898     		 i++,offset+=((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength)
899     	{
900     		type = ((struct GenericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
901     		if (type == 1)
902     		{
903     			struct GenericPartitionMap1 *gpm1 = (struct GenericPartitionMap1 *)&(lvd->partitionMaps[offset]);
904     			UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15;
905     			UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum);
906     			UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum);
907     			UDF_SB_PARTFUNC(sb,i) = NULL;
908     		}
909     		else if (type == 2)
910     		{
911     			struct UdfPartitionMap2 *upm2 = (struct UdfPartitionMap2 *)&(lvd->partitionMaps[offset]);
912     			if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL)))
913     			{
914     				if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0150)
915     				{
916     					UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15;
917     					UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt15;
918     				}
919     				else if (le16_to_cpu(((Uint16 *)upm2->partIdent.identSuffix)[0]) == 0x0200)
920     				{
921     					UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20;
922     					UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt20;
923     				}
924     			}
925     			else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE)))
926     			{
927     				Uint32 loc;
928     				Uint16 ident;
929     				struct SparingTable *st;
930     				struct SparablePartitionMap *spm = (struct SparablePartitionMap *)&(lvd->partitionMaps[offset]);
931     
932     				UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15;
933     				UDF_SB_TYPESPAR(sb,i).s_packet_len = le16_to_cpu(spm->packetLength);
934     				for (j=0; j<spm->numSparingTables; j++)
935     				{
936     					loc = le32_to_cpu(spm->locSparingTable[j]);
937     					UDF_SB_TYPESPAR(sb,i).s_spar_map[j] =
938     						udf_read_tagged(sb, loc, loc, &ident);
939     					if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL)
940     					{
941     						st = (struct SparingTable *)UDF_SB_TYPESPAR(sb,i).s_spar_map[j]->b_data;
942     						if (ident != 0 ||
943     							strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING)))
944     						{
945     							udf_release_data(UDF_SB_TYPESPAR(sb,i).s_spar_map[j]);
946     							UDF_SB_TYPESPAR(sb,i).s_spar_map[j] = NULL;
947     						}
948     					}
949     				}
950     				UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_spar15;
951     			}
952     			else
953     			{
954     				udf_debug("Unknown ident: %s\n", upm2->partIdent.ident);
955     				continue;
956     			}
957     			UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum);
958     			UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum);
959     		}
960     		udf_debug("Partition (%d:%d) type %d on volume %d\n",
961     			i, UDF_SB_PARTNUM(sb,i), type, UDF_SB_PARTVSN(sb,i));
962     	}
963     
964     	if (fileset)
965     	{
966     		long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
967     
968     		*fileset = lelb_to_cpu(la->extLocation);
969     		udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
970     			fileset->logicalBlockNum,
971     			fileset->partitionReferenceNum);
972     	}
973     	if (lvd->integritySeqExt.extLength)
974     		udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
975     	return 0;
976     }
977     
978     /*
979      * udf_load_logicalvolint
980      *
981      */
982     static void
983     udf_load_logicalvolint(struct super_block *sb, extent_ad loc)
984     {
985     	struct buffer_head *bh = NULL;
986     	Uint16 ident;
987     
988     	while ((bh = udf_read_tagged(sb, loc.extLocation, loc.extLocation, &ident)) &&
989     		ident == TID_LOGICAL_VOL_INTEGRITY_DESC && loc.extLength > 0)
990     	{
991     		UDF_SB_LVIDBH(sb) = bh;
992     		
993     		if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
994     			udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
995     		
996     		if (UDF_SB_LVIDBH(sb) != bh)
997     			udf_release_data(bh);
998     		loc.extLength -= sb->s_blocksize;
999     		loc.extLocation ++;
1000     	}
1001     	if (UDF_SB_LVIDBH(sb) != bh)
1002     		udf_release_data(bh);
1003     }
1004     
1005     /*
1006      * udf_process_sequence
1007      *
1008      * PURPOSE
1009      *	Process a main/reserve volume descriptor sequence.
1010      *
1011      * PRE-CONDITIONS
1012      *	sb			Pointer to _locked_ superblock.
1013      *	block			First block of first extent of the sequence.
1014      *	lastblock		Lastblock of first extent of the sequence.
1015      *
1016      * HISTORY
1017      *	July 1, 1997 - Andrew E. Mileski
1018      *	Written, tested, and released.
1019      */
1020     static  int
1021     udf_process_sequence(struct super_block *sb, long block, long lastblock, lb_addr *fileset)
1022     {
1023     	struct buffer_head *bh = NULL;
1024     	struct udf_vds_record vds[VDS_POS_LENGTH];
1025     	struct GenericDesc *gd;
1026     	struct VolDescPtr *vdp;
1027     	int done=0;
1028     	int i,j;
1029     	Uint32 vdsn;
1030     	Uint16 ident;
1031     	long next_s = 0, next_e = 0;
1032     
1033     	memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1034     
1035     	/* Read the main descriptor sequence */
1036     	for (;(!done && block <= lastblock); block++)
1037     	{
1038     
1039     		bh = udf_read_tagged(sb, block, block, &ident);
1040     		if (!bh) 
1041     			break;
1042     
1043     		/* Process each descriptor (ISO 13346 3/8.3-8.4) */
1044     		gd = (struct GenericDesc *)bh->b_data;
1045     		vdsn = le32_to_cpu(gd->volDescSeqNum);
1046     		switch (ident)
1047     		{
1048     			case TID_PRIMARY_VOL_DESC: /* ISO 13346 3/10.1 */
1049     				if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum)
1050     				{
1051     					vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
1052     					vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1053     				}
1054     				break;
1055     			case TID_VOL_DESC_PTR: /* ISO 13346 3/10.3 */
1056     				if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum)
1057     				{
1058     					vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1059     					vds[VDS_POS_VOL_DESC_PTR].block = block;
1060     
1061     					vdp = (struct VolDescPtr *)bh->b_data;
1062     					next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1063     					next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
1064     					next_e = next_e >> sb->s_blocksize_bits;
1065     					next_e += next_s;
1066     				}
1067     				break;
1068     			case TID_IMP_USE_VOL_DESC: /* ISO 13346 3/10.4 */
1069     				if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum)
1070     				{
1071     					vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
1072     					vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1073     				}
1074     				break;
1075     			case TID_PARTITION_DESC: /* ISO 13346 3/10.5 */
1076     				if (!vds[VDS_POS_PARTITION_DESC].block)
1077     					vds[VDS_POS_PARTITION_DESC].block = block;
1078     				break;
1079     			case TID_LOGICAL_VOL_DESC: /* ISO 13346 3/10.6 */
1080     				if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum)
1081     				{
1082     					vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
1083     					vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1084     				}
1085     				break;
1086     			case TID_UNALLOC_SPACE_DESC: /* ISO 13346 3/10.8 */
1087     				if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum)
1088     				{
1089     					vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
1090     					vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1091     				}
1092     				break;
1093     			case TID_TERMINATING_DESC: /* ISO 13346 3/10.9 */
1094     				vds[VDS_POS_TERMINATING_DESC].block = block;
1095     				if (next_e)
1096     				{
1097     					block = next_s;
1098     					lastblock = next_e;
1099     					next_s = next_e = 0;
1100     				}
1101     				else
1102     					done = 1;
1103     				break;
1104     		}
1105     		udf_release_data(bh);
1106     	}
1107     	for (i=0; i<VDS_POS_LENGTH; i++)
1108     	{
1109     		if (vds[i].block)
1110     		{
1111     			bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident);
1112     
1113     			if (i == VDS_POS_PRIMARY_VOL_DESC)
1114     				udf_load_pvoldesc(sb, bh);
1115     			else if (i == VDS_POS_LOGICAL_VOL_DESC)
1116     				udf_load_logicalvol(sb, bh, fileset);
1117     			else if (i == VDS_POS_PARTITION_DESC)
1118     			{
1119     				struct buffer_head *bh2 = NULL;
1120     				udf_load_partdesc(sb, bh);
1121     				for (j=vds[i].block+1; j<vds[VDS_POS_TERMINATING_DESC].block; j++)
1122     				{
1123     					bh2 = udf_read_tagged(sb, j, j, &ident);
1124     					gd = (struct GenericDesc *)bh2->b_data;
1125     					if (ident == TID_PARTITION_DESC)
1126     						udf_load_partdesc(sb, bh2);
1127     					udf_release_data(bh2);
1128     				}
1129     			}
1130     			udf_release_data(bh);
1131     		}
1132     	}
1133     
1134     	return 0;
1135     }
1136     
1137     /*
1138      * udf_check_valid()
1139      */
1140     static int
1141     udf_check_valid(struct super_block *sb, int novrs, int silent)
1142     {
1143     	long block;
1144     
1145     	if (novrs)
1146     	{
1147     		udf_debug("Validity check skipped because of novrs option\n");
1148     		return 0;
1149     	}
1150     	/* Check that it is NSR02 compliant */
1151     	/* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1152     	else if ((block = udf_vrs(sb, silent)) == -1)
1153     	{
1154     		udf_debug("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
1155     		return 0;
1156     	}
1157     	else 
1158     		return !block;
1159     }
1160     
1161     static int
1162     udf_load_partition(struct super_block *sb, lb_addr *fileset)
1163     {
1164     	struct AnchorVolDescPtr *anchor;
1165     	Uint16 ident;
1166     	struct buffer_head *bh;
1167     	long main_s, main_e, reserve_s, reserve_e;
1168     	int i, j;
1169     
1170     	if (!sb)
1171     		return 1;
1172     
1173     	for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
1174     	{
1175     		if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
1176     			UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
1177     		{
1178     			anchor = (struct AnchorVolDescPtr *)bh->b_data;
1179     
1180     			/* Locate the main sequence */
1181     			main_s = le32_to_cpu( anchor->mainVolDescSeqExt.extLocation );
1182     			main_e = le32_to_cpu( anchor->mainVolDescSeqExt.extLength );
1183     			main_e = main_e >> sb->s_blocksize_bits;
1184     			main_e += main_s;
1185     	
1186     			/* Locate the reserve sequence */
1187     			reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1188     			reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1189     			reserve_e = reserve_e >> sb->s_blocksize_bits;
1190     			reserve_e += reserve_s;
1191     
1192     			udf_release_data(bh);
1193     
1194     			/* Process the main & reserve sequences */
1195     			/* responsible for finding the PartitionDesc(s) */
1196     			if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1197     				udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1198     			{
1199     				break;
1200     			}
1201     		}
1202     	}
1203     
1204     	if (i == sizeof(UDF_SB_ANCHOR(sb))/sizeof(int))
1205     	{
1206     		udf_debug("No Anchor block found\n");
1207     		return 1;
1208     	}
1209     	else
1210     		udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1211     
1212     	for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
1213     	{
1214     		switch UDF_SB_PARTTYPE(sb, i)
1215     		{
1216     			case UDF_VIRTUAL_MAP15:
1217     			case UDF_VIRTUAL_MAP20:
1218     			{
1219     				lb_addr ino;
1220     
1221     				if (!UDF_SB_LASTBLOCK(sb))
1222     				{
1223     					udf_debug("Unable to determine Lastblock (For Virtual Partition)\n");
1224     					return 1;
1225     				}
1226     
1227     				for (j=0; j<UDF_SB_NUMPARTS(sb); j++)
1228     				{
1229     					if (j != i &&
1230     						UDF_SB_PARTVSN(sb,i) == UDF_SB_PARTVSN(sb,j) &&
1231     						UDF_SB_PARTNUM(sb,i) == UDF_SB_PARTNUM(sb,j))
1232     					{
1233     						ino.partitionReferenceNum = j;
1234     						ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) -
1235     							UDF_SB_PARTROOT(sb,j);
1236     						break;
1237     					}
1238     				}
1239     
1240     				if (j == UDF_SB_NUMPARTS(sb))
1241     					return 1;
1242     
1243     				if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1244     					return 1;
1245     
1246     				if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP15)
1247     				{
1248     					UDF_SB_TYPEVIRT(sb,i).s_start_offset = udf_ext0_offset(UDF_SB_VAT(sb));
1249     					UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1250     				}
1251     				else if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP20)
1252     				{
1253     					struct buffer_head *bh = NULL;
1254     					Uint32 pos;
1255     
1256     					pos = udf_block_map(UDF_SB_VAT(sb), 0);
1257     					bh = bread(sb->s_dev, pos, sb->s_blocksize);
1258     					UDF_SB_TYPEVIRT(sb,i).s_start_offset =
1259     						le16_to_cpu(((struct VirtualAllocationTable20 *)bh->b_data + udf_ext0_offset(UDF_SB_VAT(sb)))->lengthHeader) +
1260     							udf_ext0_offset(UDF_SB_VAT(sb));
1261     					UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
1262     						UDF_SB_TYPEVIRT(sb,i).s_start_offset) >> 2;
1263     					udf_release_data(bh);
1264     				}
1265     				UDF_SB_PARTROOT(sb,i) = udf_get_pblock(sb, 0, i, 0);
1266     				UDF_SB_PARTLEN(sb,i) = UDF_SB_PARTLEN(sb,ino.partitionReferenceNum);
1267     			}
1268     		}
1269     	}
1270     	return 0;
1271     }
1272     
1273     static void udf_open_lvid(struct super_block *sb)
1274     {
1275     	if (UDF_SB_LVIDBH(sb))
1276     	{
1277     		int i;
1278     		timestamp cpu_time;
1279     
1280     		UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1281     		UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1282     		if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1283     			UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1284     		UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_OPEN;
1285     
1286     		UDF_SB_LVID(sb)->descTag.descCRC =
1287     			cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1288     			le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1289     
1290     		UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1291     		for (i=0; i<16; i++)
1292     			if (i != 4)
1293     				UDF_SB_LVID(sb)->descTag.tagChecksum +=
1294     					((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1295     
1296     		mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1297     	}
1298     }
1299     
1300     static void udf_close_lvid(struct super_block *sb)
1301     {
1302     	if (UDF_SB_LVIDBH(sb) &&
1303     		UDF_SB_LVID(sb)->integrityType == INTEGRITY_TYPE_OPEN)
1304     	{
1305     		int i;
1306     		timestamp cpu_time;
1307     
1308     		UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1309     		UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1310     		if (udf_time_to_stamp(&cpu_time, CURRENT_TIME, CURRENT_UTIME))
1311     			UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1312     		if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1313     			UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1314     		if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1315     			UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1316     		if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1317     			UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1318     		UDF_SB_LVID(sb)->integrityType = INTEGRITY_TYPE_CLOSE;
1319     
1320     		UDF_SB_LVID(sb)->descTag.descCRC =
1321     			cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1322     			le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1323     
1324     		UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1325     		for (i=0; i<16; i++)
1326     			if (i != 4)
1327     				UDF_SB_LVID(sb)->descTag.tagChecksum +=
1328     					((Uint8 *)&(UDF_SB_LVID(sb)->descTag))[i];
1329     
1330     		mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1331     	}
1332     }
1333     
1334     /*
1335      * udf_read_super
1336      *
1337      * PURPOSE
1338      *	Complete the specified super block.
1339      *
1340      * PRE-CONDITIONS
1341      *	sb			Pointer to superblock to complete - never NULL.
1342      *	sb->s_dev		Device to read suberblock from.
1343      *	options			Pointer to mount options.
1344      *	silent			Silent flag.
1345      *
1346      * HISTORY
1347      *	July 1, 1997 - Andrew E. Mileski
1348      *	Written, tested, and released.
1349      */
1350     static struct super_block *
1351     udf_read_super(struct super_block *sb, void *options, int silent)
1352     {
1353     	int i;
1354     	struct inode *inode=NULL;
1355     	struct udf_options uopt;
1356     	lb_addr rootdir, fileset;
1357     
1358     	uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB);
1359     	uopt.uid = -1;
1360     	uopt.gid = -1;
1361     	uopt.umask = 0;
1362     
1363     	memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1364     
1365     #if UDFFS_RW != 1
1366     	sb->s_flags |= MS_RDONLY;
1367     #endif
1368     
1369     	if (!udf_parse_options((char *)options, &uopt))
1370     		goto error_out;
1371     
1372     	if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1373     	    uopt.flags & (1 << UDF_FLAG_NLS_MAP))
1374     	{
1375     		udf_error(sb, "udf_read_super",
1376     			"utf8 cannot be combined with iocharset\n");
1377     		goto error_out;
1378     	}
1379     #ifdef CONFIG_NLS
1380     	if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map)
1381     	{
1382     		uopt.nls_map = load_nls_default();
1383     		if (!uopt.nls_map)
1384     			uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1385     		else
1386     			udf_debug("Using default NLS map\n");
1387     	}
1388     #endif
1389     	if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1390     		uopt.flags |= (1 << UDF_FLAG_UTF8);
1391     
1392     	fileset.logicalBlockNum = 0xFFFFFFFF;
1393     	fileset.partitionReferenceNum = 0xFFFF;
1394     
1395     	UDF_SB(sb)->s_flags = uopt.flags;
1396     	UDF_SB(sb)->s_uid = uopt.uid;
1397     	UDF_SB(sb)->s_gid = uopt.gid;
1398     	UDF_SB(sb)->s_umask = uopt.umask;
1399     	UDF_SB(sb)->s_nls_map = uopt.nls_map;
1400     
1401     	/* Set the block size for all transfers */
1402     	if (!udf_set_blocksize(sb, uopt.blocksize))
1403     		goto error_out;
1404     
1405     	if ( uopt.session == 0xFFFFFFFF )
1406     		UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1407     	else
1408     		UDF_SB_SESSION(sb) = uopt.session;
1409     
1410     	udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1411     
1412     	if ( uopt.lastblock == 0xFFFFFFFF )
1413     		UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1414     	else
1415     		UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1416     
1417     	UDF_SB_LASTBLOCK(sb) = udf_find_anchor(sb, uopt.anchor, UDF_SB_LASTBLOCK(sb));
1418     
1419     	udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1420     
1421     	if (udf_check_valid(sb, uopt.novrs, silent)) /* read volume recognition sequences */
1422     	{
1423     		printk("UDF-fs: No VRS found\n");
1424      		goto error_out;
1425     	}
1426     
1427     	/* Fill in the rest of the superblock */
1428     	sb->s_op = &udf_sb_ops;
1429     	sb->dq_op = NULL;
1430     	sb->s_dirt = 0;
1431     	sb->s_magic = UDF_SUPER_MAGIC;
1432     
1433     	if (udf_load_partition(sb, &fileset))
1434     	{
1435     		printk("UDF-fs: No partition found (1)\n");
1436     		goto error_out;
1437     	}
1438     
1439     	if ( UDF_SB_LVIDBH(sb) )
1440     	{
1441     		Uint16 minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1442     		Uint16 minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
1443     		/* Uint16 maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1444     
1445     		if (minUDFReadRev > UDF_MAX_READ_VERSION)
1446     		{
1447     			printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
1448     				UDF_SB_LVIDIU(sb)->minUDFReadRev, UDF_MAX_READ_VERSION);
1449     			goto error_out;
1450     		}
1451     		else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1452     		{
1453     			sb->s_flags |= MS_RDONLY;
1454     		}
1455     
1456     		UDF_SB_UDFREV(sb) = minUDFWriteRev;
1457     
1458     		if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1459     			UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1460     		if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1461     			UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1462     	}
1463     
1464     	if ( !UDF_SB_NUMPARTS(sb) )
1465     	{
1466     		printk("UDF-fs: No partition found (2)\n");
1467     		goto error_out;
1468     	}
1469     
1470     	if ( udf_find_fileset(sb, &fileset, &rootdir) )
1471     	{
1472     		printk("UDF-fs: No fileset found\n");
1473     		goto error_out;
1474     	}
1475     
1476     	if (!silent)
1477     	{
1478     		timestamp ts;
1479     		udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb), 0);
1480     		udf_info("UDF %s-%s (%s) Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1481     			UDFFS_VERSION, UDFFS_RW ? "rw" : "ro", UDFFS_DATE,
1482     			UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute,
1483     			ts.typeAndTimezone);
1484     	}
1485     	if (!(sb->s_flags & MS_RDONLY))
1486     		udf_open_lvid(sb);
1487     
1488     	/* Assign the root inode */
1489     	/* assign inodes by physical block number */
1490     	/* perhaps it's not extensible enough, but for now ... */
1491     	inode = udf_iget(sb, rootdir); 
1492     	if (!inode)
1493     	{
1494     		printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
1495     			rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1496     		goto error_out;
1497     	}
1498     
1499     	/* Allocate a dentry for the root inode */
1500     	sb->s_root = d_alloc_root(inode);
1501     	if (!sb->s_root)
1502     	{
1503     		printk("UDF-fs: Couldn't allocate root dentry\n");
1504     		iput(inode);
1505     		goto error_out;
1506     	}
1507     	sb->s_maxbytes = ~0ULL;
1508     	return sb;
1509     
1510     error_out:
1511     	if (UDF_SB_VAT(sb))
1512     		iput(UDF_SB_VAT(sb));
1513     	if (UDF_SB_NUMPARTS(sb))
1514     	{
1515     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1516     			iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1517     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1518     			iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1519     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1520     		{
1521     			for (i=0; i<UDF_SB_BITMAP_NR_GROUPS(sb,UDF_SB_PARTITION(sb),s_uspace); i++)
1522     			{
1523     				if (UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace,i))
1524     					udf_release_data(UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace,i));
1525     			}
1526     			kfree(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
1527     		}
1528     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1529     		{
1530     			for (i=0; i<UDF_SB_BITMAP_NR_GROUPS(sb,UDF_SB_PARTITION(sb),s_fspace); i++)
1531     			{
1532     				if (UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace,i))
1533     					udf_release_data(UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace,i));
1534     			}
1535     			kfree(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
1536     		}
1537     		if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15)
1538     		{
1539     			for (i=0; i<4; i++)
1540     				udf_release_data(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
1541     		}
1542     	}
1543     #ifdef CONFIG_NLS
1544     	if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1545     		unload_nls(UDF_SB(sb)->s_nls_map);
1546     #endif
1547     	if (!(sb->s_flags & MS_RDONLY))
1548     		udf_close_lvid(sb);
1549     	udf_release_data(UDF_SB_LVIDBH(sb));
1550     	UDF_SB_FREE(sb);
1551     	return NULL;
1552     }
1553     
1554     void udf_error(struct super_block *sb, const char *function,
1555     	const char *fmt, ...)
1556     {
1557     	va_list args;
1558     
1559     	if (!(sb->s_flags & MS_RDONLY))
1560     	{
1561     		/* mark sb error */
1562     		sb->s_dirt = 1;
1563     	}
1564     	va_start(args, fmt);
1565     	vsprintf(error_buf, fmt, args);
1566     	va_end(args);
1567     	printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1568     		bdevname(sb->s_dev), function, error_buf);
1569     }
1570     
1571     void udf_warning(struct super_block *sb, const char *function,
1572     	const char *fmt, ...)
1573     {
1574     	va_list args;
1575     
1576     	va_start (args, fmt);
1577     	vsprintf(error_buf, fmt, args);
1578     	va_end(args);
1579     	printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1580     		bdevname(sb->s_dev), function, error_buf);
1581     }
1582     
1583     /*
1584      * udf_put_super
1585      *
1586      * PURPOSE
1587      *	Prepare for destruction of the superblock.
1588      *
1589      * DESCRIPTION
1590      *	Called before the filesystem is unmounted.
1591      *
1592      * HISTORY
1593      *	July 1, 1997 - Andrew E. Mileski
1594      *	Written, tested, and released.
1595      */
1596     static void
1597     udf_put_super(struct super_block *sb)
1598     {
1599     	int i;
1600     
1601     	if (UDF_SB_VAT(sb))
1602     		iput(UDF_SB_VAT(sb));
1603     	if (UDF_SB_NUMPARTS(sb))
1604     	{
1605     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1606     			iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1607     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1608     			iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1609     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1610     		{
1611     			for (i=0; i<UDF_SB_BITMAP_NR_GROUPS(sb,UDF_SB_PARTITION(sb),s_uspace); i++)
1612     			{
1613     				if (UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace,i))
1614     					udf_release_data(UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_uspace,i));
1615     			}
1616     			kfree(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
1617     		}
1618     		if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1619     		{
1620     			for (i=0; i<UDF_SB_BITMAP_NR_GROUPS(sb,UDF_SB_PARTITION(sb),s_fspace); i++)
1621     			{
1622     				if (UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace,i))
1623     					udf_release_data(UDF_SB_BITMAP(sb,UDF_SB_PARTITION(sb),s_fspace,i));
1624     			}
1625     			kfree(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
1626     		}
1627     		if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15)
1628     		{
1629     			for (i=0; i<4; i++)
1630     				udf_release_data(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
1631     		}
1632     	}
1633     #ifdef CONFIG_NLS
1634     	if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1635     		unload_nls(UDF_SB(sb)->s_nls_map);
1636     #endif
1637     	if (!(sb->s_flags & MS_RDONLY))
1638     		udf_close_lvid(sb);
1639     	udf_release_data(UDF_SB_LVIDBH(sb));
1640     	UDF_SB_FREE(sb);
1641     }
1642     
1643     /*
1644      * udf_stat_fs
1645      *
1646      * PURPOSE
1647      *	Return info about the filesystem.
1648      *
1649      * DESCRIPTION
1650      *	Called by sys_statfs()
1651      *
1652      * HISTORY
1653      *	July 1, 1997 - Andrew E. Mileski
1654      *	Written, tested, and released.
1655      */
1656     static int
1657     udf_statfs(struct super_block *sb, struct statfs *buf)
1658     {
1659     	buf->f_type = UDF_SUPER_MAGIC;
1660     	buf->f_bsize = sb->s_blocksize;
1661     	buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1662     	buf->f_bfree = udf_count_free(sb);
1663     	buf->f_bavail = buf->f_bfree;
1664     	buf->f_files = (UDF_SB_LVIDBH(sb) ?
1665     		(le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
1666     		le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree;
1667     	buf->f_ffree = buf->f_bfree;
1668     	/* __kernel_fsid_t f_fsid */
1669     	buf->f_namelen = UDF_NAME_LEN;
1670     
1671     	return 0;
1672     }
1673     
1674     static unsigned char udf_bitmap_lookup[16] = {
1675     	0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1676     };
1677     
1678     static unsigned int
1679     udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
1680     {
1681     	struct buffer_head *bh = NULL;
1682     	unsigned int accum = 0;
1683     	int index;
1684     	int block = 0, newblock;
1685     	lb_addr loc;
1686     	Uint32 bytes;
1687     	Uint8 value;
1688     	Uint8 *ptr;
1689     	Uint16 ident;
1690     	struct SpaceBitmapDesc *bm;
1691     
1692     	loc.logicalBlockNum = bitmap->s_extPosition;
1693     	loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1694     	bh = udf_read_ptagged(sb, loc, 0, &ident);
1695     
1696     	if (!bh)
1697     	{
1698     		printk(KERN_ERR "udf: udf_count_free failed\n");
1699     		return 0;
1700     	}
1701     	else if (ident != TID_SPACE_BITMAP_DESC)
1702     	{
1703     		udf_release_data(bh);
1704     		printk(KERN_ERR "udf: udf_count_free failed\n");
1705     		return 0;
1706     	}
1707     
1708     	bm = (struct SpaceBitmapDesc *)bh->b_data;
1709     	bytes = bm->numOfBytes;
1710     	index = sizeof(struct SpaceBitmapDesc); /* offset in first block only */
1711     	ptr = (Uint8 *)bh->b_data;
1712     
1713     	while ( bytes > 0 )
1714     	{
1715     		while ((bytes > 0) && (index < sb->s_blocksize))
1716     		{
1717     			value = ptr[index];
1718     			accum += udf_bitmap_lookup[ value & 0x0f ];
1719     			accum += udf_bitmap_lookup[ value >> 4 ];
1720     			index++;
1721     			bytes--;
1722     		}
1723     		if ( bytes )
1724     		{
1725     			udf_release_data(bh);
1726     			newblock = udf_get_lb_pblock(sb, loc, ++block);
1727     			bh = udf_tread(sb, newblock, sb->s_blocksize);
1728     			if (!bh)
1729     			{
1730     				udf_debug("read failed\n");
1731     				return accum;
1732     			}
1733     			index = 0;
1734     			ptr = (Uint8 *)bh->b_data;
1735     		}
1736     	}
1737     	udf_release_data(bh);
1738     	return accum;
1739     }
1740     
1741     static unsigned int
1742     udf_count_free_table(struct super_block *sb, struct inode * table)
1743     {
1744     	unsigned int accum = 0;
1745     	Uint32 extoffset, elen;
1746     	lb_addr bloc, eloc;
1747     	char etype;
1748     	struct buffer_head *bh = NULL;
1749     
1750     	bloc = UDF_I_LOCATION(table);
1751     	extoffset = sizeof(struct UnallocatedSpaceEntry);
1752     
1753     	while ((etype = udf_next_aext(table, &bloc, &extoffset, &eloc, &elen, &bh, 1)) != -1)
1754     	{
1755     		accum += (elen >> table->i_sb->s_blocksize_bits);
1756     	}
1757     	udf_release_data(bh);
1758     	return accum;
1759     }
1760     	
1761     static unsigned int
1762     udf_count_free(struct super_block *sb)
1763     {
1764     	unsigned int accum = 0;
1765     
1766     	if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1767     	{
1768     		accum += udf_count_free_bitmap(sb,
1769     			UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
1770     	}
1771     	if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1772     	{
1773     		accum += udf_count_free_bitmap(sb,
1774     			UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
1775     	}
1776     	if (accum)
1777     		return accum;
1778     
1779     	if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1780     	{
1781     		accum += udf_count_free_table(sb,
1782     			UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1783     	}
1784     	if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1785     	{
1786     		accum += udf_count_free_table(sb,
1787     			UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1788     	}
1789     	if (accum)
1790     		return accum;
1791     
1792     	if (UDF_SB_LVIDBH(sb))
1793     	{
1794     		if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb))
1795     		{
1796     			accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
1797     
1798     			if (accum == 0xFFFFFFFF)
1799     				accum = 0;
1800     		}
1801     	}
1802     	return accum;
1803     }
1804