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