ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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-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 *, lb_addr *);
89 static int udf_load_logicalvol(struct super_block *, struct buffer_head *, lb_addr *);
90 static void udf_load_logicalvolint(struct super_block *, extent_ad);
91 static void udf_find_anchor(struct super_block *);
92 static int udf_find_fileset(struct super_block *, lb_addr *, 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 *, 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_HWCACHE_ALIGN|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         .read_inode             = udf_read_inode,
166         .write_inode            = udf_write_inode,
167         .put_inode              = udf_put_inode,
168         .delete_inode           = udf_delete_inode,
169         .clear_inode            = udf_clear_inode,
170         .put_super              = udf_put_super,
171         .write_super            = udf_write_super,
172         .statfs                 = udf_statfs,
173         .remount_fs             = udf_remount_fs,
174 };
175
176 struct udf_options
177 {
178         unsigned char novrs;
179         unsigned int blocksize;
180         unsigned int session;
181         unsigned int lastblock;
182         unsigned int anchor;
183         unsigned int volume;
184         unsigned short partition;
185         unsigned int fileset;
186         unsigned int rootdir;
187         unsigned int flags;
188         mode_t umask;
189         gid_t gid;
190         uid_t uid;
191         struct nls_table *nls_map;
192 };
193
194 static int __init init_udf_fs(void)
195 {
196         int err;
197         printk(KERN_NOTICE "udf: registering filesystem\n");
198         err = init_inodecache();
199         if (err)
200                 goto out1;
201         err = register_filesystem(&udf_fstype);
202         if (err)
203                 goto out;
204         return 0;
205 out:
206         destroy_inodecache();
207 out1:
208         return err;
209 }
210
211 static void __exit exit_udf_fs(void)
212 {
213         printk(KERN_NOTICE "udf: unregistering filesystem\n");
214         unregister_filesystem(&udf_fstype);
215         destroy_inodecache();
216 }
217
218 module_init(init_udf_fs)
219 module_exit(exit_udf_fs)
220
221 /*
222  * udf_parse_options
223  *
224  * PURPOSE
225  *      Parse mount options.
226  *
227  * DESCRIPTION
228  *      The following mount options are supported:
229  *
230  *      gid=            Set the default group.
231  *      umask=          Set the default umask.
232  *      uid=            Set the default user.
233  *      bs=             Set the block size.
234  *      unhide          Show otherwise hidden files.
235  *      undelete        Show deleted files in lists.
236  *      adinicb         Embed data in the inode (default)
237  *      noadinicb       Don't embed data in the inode
238  *      shortad         Use short ad's
239  *      longad          Use long ad's (default)
240  *      nostrict        Unset strict conformance
241  *      iocharset=      Set the NLS character set
242  *
243  *      The remaining are for debugging and disaster recovery:
244  *
245  *      novrs           Skip volume sequence recognition 
246  *
247  *      The following expect a offset from 0.
248  *
249  *      session=        Set the CDROM session (default= last session)
250  *      anchor=         Override standard anchor location. (default= 256)
251  *      volume=         Override the VolumeDesc location. (unused)
252  *      partition=      Override the PartitionDesc location. (unused)
253  *      lastblock=      Set the last block of the filesystem/
254  *
255  *      The following expect a offset from the partition root.
256  *
257  *      fileset=        Override the fileset block location. (unused)
258  *      rootdir=        Override the root directory location. (unused)
259  *              WARNING: overriding the rootdir to a non-directory may
260  *              yield highly unpredictable results.
261  *
262  * PRE-CONDITIONS
263  *      options         Pointer to mount options string.
264  *      uopts           Pointer to mount options variable.
265  *
266  * POST-CONDITIONS
267  *      <return>        1       Mount options parsed okay.
268  *      <return>        0       Error parsing mount options.
269  *
270  * HISTORY
271  *      July 1, 1997 - Andrew E. Mileski
272  *      Written, tested, and released.
273  */
274
275 enum {
276         Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
277         Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
278         Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
279         Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
280         Opt_rootdir, Opt_utf8, Opt_iocharset,
281         Opt_err
282 };
283
284 static match_table_t tokens = {
285         {Opt_novrs, "novrs"},
286         {Opt_nostrict, "nostrict"},
287         {Opt_bs, "bs=%u"},
288         {Opt_unhide, "unhide"},
289         {Opt_undelete, "undelete"},
290         {Opt_noadinicb, "noadinicb"},
291         {Opt_adinicb, "adinicb"},
292         {Opt_shortad, "shortad"},
293         {Opt_longad, "longad"},
294         {Opt_gid, "gid=%u"},
295         {Opt_uid, "uid=%u"},
296         {Opt_umask, "umask=%o"},
297         {Opt_session, "session=%u"},
298         {Opt_lastblock, "lastblock=%u"},
299         {Opt_anchor, "anchor=%u"},
300         {Opt_volume, "volume=%u"},
301         {Opt_partition, "partition=%u"},
302         {Opt_fileset, "fileset=%u"},
303         {Opt_rootdir, "rootdir=%u"},
304         {Opt_utf8, "utf8"},
305         {Opt_iocharset, "iocharset=%s"},
306         {Opt_err, NULL}
307 };
308
309 static int
310 udf_parse_options(char *options, struct udf_options *uopt)
311 {
312         char *p;
313         int option;
314
315         uopt->novrs = 0;
316         uopt->blocksize = 2048;
317         uopt->partition = 0xFFFF;
318         uopt->session = 0xFFFFFFFF;
319         uopt->lastblock = 0;
320         uopt->anchor = 0;
321         uopt->volume = 0xFFFFFFFF;
322         uopt->rootdir = 0xFFFFFFFF;
323         uopt->fileset = 0xFFFFFFFF;
324         uopt->nls_map = NULL;
325
326         if (!options)
327                 return 1;
328
329         while ((p = strsep(&options, ",")) != NULL)
330         {
331                 substring_t args[MAX_OPT_ARGS];
332                 int token;
333                 if (!*p)
334                         continue;
335
336                 token = match_token(p, tokens, args);
337                 switch (token)
338                 {
339                         case Opt_novrs:
340                                 uopt->novrs = 1;
341                         case Opt_bs:
342                                 if (match_int(&args[0], &option))
343                                         return 0;
344                                 uopt->blocksize = option;
345                                 break;
346                         case Opt_unhide:
347                                 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
348                                 break;
349                         case Opt_undelete:
350                                 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
351                                 break;
352                         case Opt_noadinicb:
353                                 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
354                                 break;
355                         case Opt_adinicb:
356                                 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
357                                 break;
358                         case Opt_shortad:
359                                 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
360                                 break;
361                         case Opt_longad:
362                                 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
363                                 break;
364                         case Opt_gid:
365                                 if (match_int(args, &option))
366                                         return 0;
367                                 uopt->gid = option;
368                                 break;
369                         case Opt_uid:
370                                 if (match_int(args, &option))
371                                         return 0;
372                                 uopt->uid = option;
373                                 break;
374                         case Opt_umask:
375                                 if (match_octal(args, &option))
376                                         return 0;
377                                 uopt->umask = option;
378                                 break;
379                         case Opt_nostrict:
380                                 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
381                                 break;
382                         case Opt_session:
383                                 if (match_int(args, &option))
384                                         return 0;
385                                 uopt->session = option;
386                                 break;
387                         case Opt_lastblock:
388                                 if (match_int(args, &option))
389                                         return 0;
390                                 uopt->lastblock = option;
391                                 break;
392                         case Opt_anchor:
393                                 if (match_int(args, &option))
394                                         return 0;
395                                 uopt->anchor = option;
396                                 break;
397                         case Opt_volume:
398                                 if (match_int(args, &option))
399                                         return 0;
400                                 uopt->volume = option;
401                                 break;
402                         case Opt_partition:
403                                 if (match_int(args, &option))
404                                         return 0;
405                                 uopt->partition = option;
406                                 break;
407                         case Opt_fileset:
408                                 if (match_int(args, &option))
409                                         return 0;
410                                 uopt->fileset = option;
411                                 break;
412                         case Opt_rootdir:
413                                 if (match_int(args, &option))
414                                         return 0;
415                                 uopt->rootdir = option;
416                                 break;
417                         case Opt_utf8:
418                                 uopt->flags |= (1 << UDF_FLAG_UTF8);
419                                 break;
420 #if defined(CONFIG_NLS) || defined(CONFIG_NLS_MODULE)
421                         case Opt_iocharset:
422                                 uopt->nls_map = load_nls(args[0].from);
423                                 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
424                                 break;
425 #endif
426                         default:
427                                 printk(KERN_ERR "udf: bad mount option \"%s\" "
428                                                 "or missing value\n", p);
429                         return 0;
430                 }
431         }
432         return 1;
433 }
434
435 void
436 udf_write_super(struct super_block *sb)
437 {
438         lock_kernel();
439         if (!(sb->s_flags & MS_RDONLY))
440                 udf_open_lvid(sb);
441         sb->s_dirt = 0;
442         unlock_kernel();
443 }
444
445 static int
446 udf_remount_fs(struct super_block *sb, int *flags, char *options)
447 {
448         struct udf_options uopt;
449
450         uopt.flags = UDF_SB(sb)->s_flags ;
451         uopt.uid   = UDF_SB(sb)->s_uid ;
452         uopt.gid   = UDF_SB(sb)->s_gid ;
453         uopt.umask = UDF_SB(sb)->s_umask ;
454
455         if ( !udf_parse_options(options, &uopt) )
456                 return -EINVAL;
457
458         UDF_SB(sb)->s_flags = uopt.flags;
459         UDF_SB(sb)->s_uid   = uopt.uid;
460         UDF_SB(sb)->s_gid   = uopt.gid;
461         UDF_SB(sb)->s_umask = uopt.umask;
462
463         if (UDF_SB_LVIDBH(sb)) {
464                 int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
465                 if (write_rev > UDF_MAX_WRITE_VERSION)
466                         *flags |= MS_RDONLY;
467         }
468
469         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
470                 return 0;
471         if (*flags & MS_RDONLY)
472                 udf_close_lvid(sb);
473         else
474                 udf_open_lvid(sb);
475
476         return 0;
477 }
478
479 /*
480  * udf_set_blocksize
481  *
482  * PURPOSE
483  *      Set the block size to be used in all transfers.
484  *
485  * DESCRIPTION
486  *      To allow room for a DMA transfer, it is best to guess big when unsure.
487  *      This routine picks 2048 bytes as the blocksize when guessing. This
488  *      should be adequate until devices with larger block sizes become common.
489  *
490  *      Note that the Linux kernel can currently only deal with blocksizes of
491  *      512, 1024, 2048, 4096, and 8192 bytes.
492  *
493  * PRE-CONDITIONS
494  *      sb                      Pointer to _locked_ superblock.
495  *
496  * POST-CONDITIONS
497  *      sb->s_blocksize         Blocksize.
498  *      sb->s_blocksize_bits    log2 of blocksize.
499  *      <return>        0       Blocksize is valid.
500  *      <return>        1       Blocksize is invalid.
501  *
502  * HISTORY
503  *      July 1, 1997 - Andrew E. Mileski
504  *      Written, tested, and released.
505  */
506 static  int
507 udf_set_blocksize(struct super_block *sb, int bsize)
508 {
509         if (!sb_min_blocksize(sb, bsize)) {
510                 udf_debug("Bad block size (%d)\n", bsize);
511                 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
512                 return 0;
513         }
514         return sb->s_blocksize;
515 }
516
517 static int
518 udf_vrs(struct super_block *sb, int silent)
519 {
520         struct volStructDesc *vsd = NULL;
521         int sector = 32768;
522         int sectorsize;
523         struct buffer_head *bh = NULL;
524         int iso9660=0;
525         int nsr02=0;
526         int nsr03=0;
527
528         /* Block size must be a multiple of 512 */
529         if (sb->s_blocksize & 511)
530                 return 0;
531
532         if (sb->s_blocksize < sizeof(struct volStructDesc))
533                 sectorsize = sizeof(struct volStructDesc);
534         else
535                 sectorsize = sb->s_blocksize;
536
537         sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
538
539         udf_debug("Starting at sector %u (%ld byte sectors)\n",
540                 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
541         /* Process the sequence (if applicable) */
542         for (;!nsr02 && !nsr03; sector += sectorsize)
543         {
544                 /* Read a block */
545                 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
546                 if (!bh)
547                         break;
548
549                 /* Look for ISO  descriptors */
550                 vsd = (struct volStructDesc *)(bh->b_data +
551                         (sector & (sb->s_blocksize - 1)));
552
553                 if (vsd->stdIdent[0] == 0)
554                 {
555                         udf_release_data(bh);
556                         break;
557                 }
558                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN))
559                 {
560                         iso9660 = sector;
561                         switch (vsd->structType)
562                         {
563                                 case 0: 
564                                         udf_debug("ISO9660 Boot Record found\n");
565                                         break;
566                                 case 1: 
567                                         udf_debug("ISO9660 Primary Volume Descriptor found\n");
568                                         break;
569                                 case 2: 
570                                         udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
571                                         break;
572                                 case 3: 
573                                         udf_debug("ISO9660 Volume Partition Descriptor found\n");
574                                         break;
575                                 case 255: 
576                                         udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
577                                         break;
578                                 default: 
579                                         udf_debug("ISO9660 VRS (%u) found\n", vsd->structType);
580                                         break;
581                         }
582                 }
583                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN))
584                 {
585                 }
586                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01, VSD_STD_ID_LEN))
587                 {
588                         udf_release_data(bh);
589                         break;
590                 }
591                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN))
592                 {
593                         nsr02 = sector;
594                 }
595                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN))
596                 {
597                         nsr03 = sector;
598                 }
599                 udf_release_data(bh);
600         }
601
602         if (nsr03)
603                 return nsr03;
604         else if (nsr02)
605                 return nsr02;
606         else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
607                 return -1;
608         else
609                 return 0;
610 }
611
612 /*
613  * udf_find_anchor
614  *
615  * PURPOSE
616  *      Find an anchor volume descriptor.
617  *
618  * PRE-CONDITIONS
619  *      sb                      Pointer to _locked_ superblock.
620  *      lastblock               Last block on media.
621  *
622  * POST-CONDITIONS
623  *      <return>                1 if not found, 0 if ok
624  *
625  * HISTORY
626  *      July 1, 1997 - Andrew E. Mileski
627  *      Written, tested, and released.
628  */
629 static void
630 udf_find_anchor(struct super_block *sb)
631 {
632         int lastblock = UDF_SB_LASTBLOCK(sb);
633         struct buffer_head *bh = NULL;
634         uint16_t ident;
635         uint32_t location;
636         int i;
637
638         if (lastblock)
639         {
640                 int varlastblock = udf_variable_to_fixed(lastblock);
641                 int last[] =  { lastblock, lastblock - 2,
642                                 lastblock - 150, lastblock - 152,
643                                 varlastblock, varlastblock - 2,
644                                 varlastblock - 150, varlastblock - 152 };
645
646                 lastblock = 0;
647
648                 /* Search for an anchor volume descriptor pointer */
649
650                 /*  according to spec, anchor is in either:
651                  *     block 256
652                  *     lastblock-256
653                  *     lastblock
654                  *  however, if the disc isn't closed, it could be 512 */
655
656                 for (i=0; (!lastblock && i<sizeof(last)/sizeof(int)); i++)
657                 {
658                         if (last[i] < 0 || !(bh = sb_bread(sb, last[i])))
659                         {
660                                 ident = location = 0;
661                         }
662                         else
663                         {
664                                 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
665                                 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
666                                 udf_release_data(bh);
667                         }
668         
669                         if (ident == TAG_IDENT_AVDP)
670                         {
671                                 if (location == last[i] - UDF_SB_SESSION(sb))
672                                 {
673                                         lastblock = UDF_SB_ANCHOR(sb)[0] = last[i] - UDF_SB_SESSION(sb);
674                                         UDF_SB_ANCHOR(sb)[1] = last[i] - 256 - UDF_SB_SESSION(sb);
675                                 }
676                                 else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb))
677                                 {
678                                         UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
679                                         lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb);
680                                         UDF_SB_ANCHOR(sb)[1] = lastblock - 256 - UDF_SB_SESSION(sb);
681                                 }
682                                 else
683                                         udf_debug("Anchor found at block %d, location mismatch %d.\n",
684                                                 last[i], location);
685                         }
686                         else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE)
687                         {
688                                 lastblock = last[i];
689                                 UDF_SB_ANCHOR(sb)[3] = 512;
690                         }
691                         else
692                         {
693                                 if (last[i] < 256 || !(bh = sb_bread(sb, last[i] - 256)))
694                                 {
695                                         ident = location = 0;
696                                 }
697                                 else
698                                 {
699                                         ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
700                                         location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
701                                         udf_release_data(bh);
702                                 }
703         
704                                 if (ident == TAG_IDENT_AVDP &&
705                                         location == last[i] - 256 - UDF_SB_SESSION(sb))
706                                 {
707                                         lastblock = last[i];
708                                         UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
709                                 }
710                                 else
711                                 {
712                                         if (last[i] < 312 + UDF_SB_SESSION(sb) || !(bh = sb_bread(sb, last[i] - 312 - UDF_SB_SESSION(sb))))
713                                         {
714                                                 ident = location = 0;
715                                         }
716                                         else
717                                         {
718                                                 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
719                                                 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
720                                                 udf_release_data(bh);
721                                         }
722         
723                                         if (ident == TAG_IDENT_AVDP &&
724                                                 location == udf_variable_to_fixed(last[i]) - 256)
725                                         {
726                                                 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
727                                                 lastblock = udf_variable_to_fixed(last[i]);
728                                                 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
729                                         }
730                                 }
731                         }
732                 }
733         }
734
735         if (!lastblock)
736         {
737                 /* We havn't found the lastblock. check 312 */
738                 if ((bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb))))
739                 {
740                         ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
741                         location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
742                         udf_release_data(bh);
743
744                         if (ident == TAG_IDENT_AVDP && location == 256)
745                                 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
746                 }
747         }
748
749         for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
750         {
751                 if (UDF_SB_ANCHOR(sb)[i])
752                 {
753                         if (!(bh = udf_read_tagged(sb,
754                                 UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
755                         {
756                                 UDF_SB_ANCHOR(sb)[i] = 0;
757                         }
758                         else
759                         {
760                                 udf_release_data(bh);
761                                 if ((ident != TAG_IDENT_AVDP) && (i ||
762                                         (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE)))
763                                 {
764                                         UDF_SB_ANCHOR(sb)[i] = 0;
765                                 }
766                         }
767                 }
768         }
769
770         UDF_SB_LASTBLOCK(sb) = lastblock;
771 }
772
773 static int 
774 udf_find_fileset(struct super_block *sb, lb_addr *fileset, lb_addr *root)
775 {
776         struct buffer_head *bh = NULL;
777         long lastblock;
778         uint16_t ident;
779
780         if (fileset->logicalBlockNum != 0xFFFFFFFF ||
781                 fileset->partitionReferenceNum != 0xFFFF)
782         {
783                 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
784
785                 if (!bh)
786                         return 1;
787                 else if (ident != TAG_IDENT_FSD)
788                 {
789                         udf_release_data(bh);
790                         return 1;
791                 }
792                         
793         }
794
795         if (!bh) /* Search backwards through the partitions */
796         {
797                 lb_addr newfileset;
798
799                 return 1;
800                 
801                 for (newfileset.partitionReferenceNum=UDF_SB_NUMPARTS(sb)-1;
802                         (newfileset.partitionReferenceNum != 0xFFFF &&
803                                 fileset->logicalBlockNum == 0xFFFFFFFF &&
804                                 fileset->partitionReferenceNum == 0xFFFF);
805                         newfileset.partitionReferenceNum--)
806                 {
807                         lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum);
808                         newfileset.logicalBlockNum = 0;
809
810                         do
811                         {
812                                 bh = udf_read_ptagged(sb, newfileset, 0, &ident);
813                                 if (!bh)
814                                 {
815                                         newfileset.logicalBlockNum ++;
816                                         continue;
817                                 }
818
819                                 switch (ident)
820                                 {
821                                         case TAG_IDENT_SBD:
822                                         {
823                                                 struct spaceBitmapDesc *sp;
824                                                 sp = (struct spaceBitmapDesc *)bh->b_data;
825                                                 newfileset.logicalBlockNum += 1 +
826                                                         ((le32_to_cpu(sp->numOfBytes) + sizeof(struct spaceBitmapDesc) - 1)
827                                                                 >> sb->s_blocksize_bits);
828                                                 udf_release_data(bh);
829                                                 break;
830                                         }
831                                         case TAG_IDENT_FSD:
832                                         {
833                                                 *fileset = newfileset;
834                                                 break;
835                                         }
836                                         default:
837                                         {
838                                                 newfileset.logicalBlockNum ++;
839                                                 udf_release_data(bh);
840                                                 bh = NULL;
841                                                 break;
842                                         }
843                                 }
844                         }
845                         while (newfileset.logicalBlockNum < lastblock &&
846                                 fileset->logicalBlockNum == 0xFFFFFFFF &&
847                                 fileset->partitionReferenceNum == 0xFFFF);
848                 }
849         }
850
851         if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
852                 fileset->partitionReferenceNum != 0xFFFF) && bh)
853         {
854                 udf_debug("Fileset at block=%d, partition=%d\n",
855                         fileset->logicalBlockNum, fileset->partitionReferenceNum);
856
857                 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
858                 udf_load_fileset(sb, bh, root);
859                 udf_release_data(bh);
860                 return 0;
861         }
862         return 1;
863 }
864
865 static void 
866 udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
867 {
868         struct primaryVolDesc *pvoldesc;
869         time_t recording;
870         long recording_usec;
871         struct ustr instr;
872         struct ustr outstr;
873
874         pvoldesc = (struct primaryVolDesc *)bh->b_data;
875
876         if ( udf_stamp_to_time(&recording, &recording_usec,
877                 lets_to_cpu(pvoldesc->recordingDateAndTime)) )
878         {
879                 timestamp ts;
880                 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
881                 udf_debug("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n",
882                         recording, recording_usec,
883                         ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.typeAndTimezone);
884                 UDF_SB_RECORDTIME(sb).tv_sec = recording;
885                 UDF_SB_RECORDTIME(sb).tv_nsec = recording_usec * 1000;
886         }
887
888         if ( !udf_build_ustr(&instr, pvoldesc->volIdent, 32) )
889         {
890                 if (udf_CS0toUTF8(&outstr, &instr))
891                 {
892                         strncpy( UDF_SB_VOLIDENT(sb), outstr.u_name,
893                                 outstr.u_len > 31 ? 31 : outstr.u_len);
894                         udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
895                 }
896         }
897
898         if ( !udf_build_ustr(&instr, pvoldesc->volSetIdent, 128) )
899         {
900                 if (udf_CS0toUTF8(&outstr, &instr))
901                         udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
902         }
903 }
904
905 static void 
906 udf_load_fileset(struct super_block *sb, struct buffer_head *bh, lb_addr *root)
907 {
908         struct fileSetDesc *fset;
909
910         fset = (struct fileSetDesc *)bh->b_data;
911
912         *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
913
914         UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
915
916         udf_debug("Rootdir at block=%d, partition=%d\n", 
917                 root->logicalBlockNum, root->partitionReferenceNum);
918 }
919
920 static void 
921 udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
922 {
923         struct partitionDesc *p;
924         int i;
925
926         p = (struct partitionDesc *)bh->b_data;
927
928         for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
929         {
930                 udf_debug("Searching map: (%d == %d)\n", 
931                         UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
932                 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber))
933                 {
934                         UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */
935                         UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation);
936                         if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
937                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_READ_ONLY;
938                         if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
939                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_WRITE_ONCE;
940                         if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
941                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_REWRITABLE;
942                         if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
943                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_OVERWRITABLE;
944
945                         if (!strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) ||
946                                 !strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
947                         {
948                                 struct partitionHeaderDesc *phd;
949
950                                 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
951                                 if (phd->unallocSpaceTable.extLength)
952                                 {
953                                         lb_addr loc = { le32_to_cpu(phd->unallocSpaceTable.extPosition), i };
954
955                                         UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table =
956                                                 udf_iget(sb, loc);
957                                         UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_TABLE;
958                                         udf_debug("unallocSpaceTable (part %d) @ %ld\n",
959                                                 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table->i_ino);
960                                 }
961                                 if (phd->unallocSpaceBitmap.extLength)
962                                 {
963                                         UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
964                                         if (UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap != NULL)
965                                         {
966                                                 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extLength =
967                                                         le32_to_cpu(phd->unallocSpaceBitmap.extLength);
968                                                 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition =
969                                                         le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
970                                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_BITMAP;
971                                                 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
972                                                         i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition);
973                                         }
974                                 }
975                                 if (phd->partitionIntegrityTable.extLength)
976                                         udf_debug("partitionIntegrityTable (part %d)\n", i);
977                                 if (phd->freedSpaceTable.extLength)
978                                 {
979                                         lb_addr loc = { le32_to_cpu(phd->freedSpaceTable.extPosition), i };
980
981                                         UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table =
982                                                 udf_iget(sb, loc);
983                                         UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_TABLE;
984                                         udf_debug("freedSpaceTable (part %d) @ %ld\n",
985                                                 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table->i_ino);
986                                 }
987                                 if (phd->freedSpaceBitmap.extLength)
988                                 {
989                                         UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
990                                         if (UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap != NULL)
991                                         {
992                                                 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extLength =
993                                                         le32_to_cpu(phd->freedSpaceBitmap.extLength);
994                                                 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition =
995                                                         le32_to_cpu(phd->freedSpaceBitmap.extPosition);
996                                                 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_BITMAP;
997                                                 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
998                                                         i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition);
999                                         }
1000                                 }
1001                         }
1002                         break;
1003                 }
1004         }
1005         if (i == UDF_SB_NUMPARTS(sb))
1006         {
1007                 udf_debug("Partition (%d) not found in partition map\n", le16_to_cpu(p->partitionNumber));
1008         }
1009         else
1010         {
1011                 udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
1012                         le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i),
1013                         UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i));
1014         }
1015 }
1016
1017 static int 
1018 udf_load_logicalvol(struct super_block *sb, struct buffer_head * bh, lb_addr *fileset)
1019 {
1020         struct logicalVolDesc *lvd;
1021         int i, j, offset;
1022         uint8_t type;
1023
1024         lvd = (struct logicalVolDesc *)bh->b_data;
1025
1026         UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
1027
1028         for (i=0,offset=0;
1029                  i<UDF_SB_NUMPARTS(sb) && offset<le32_to_cpu(lvd->mapTableLength);
1030                  i++,offset+=((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength)
1031         {
1032                 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
1033                 if (type == 1)
1034                 {
1035                         struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
1036                         UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15;
1037                         UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum);
1038                         UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum);
1039                         UDF_SB_PARTFUNC(sb,i) = NULL;
1040                 }
1041                 else if (type == 2)
1042                 {
1043                         struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
1044                         if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL)))
1045                         {
1046                                 if (le16_to_cpu(((uint16_t *)upm2->partIdent.identSuffix)[0]) == 0x0150)
1047                                 {
1048                                         UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15;
1049                                         UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt15;
1050                                 }
1051                                 else if (le16_to_cpu(((uint16_t *)upm2->partIdent.identSuffix)[0]) == 0x0200)
1052                                 {
1053                                         UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20;
1054                                         UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt20;
1055                                 }
1056                         }
1057                         else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE)))
1058                         {
1059                                 uint32_t loc;
1060                                 uint16_t ident;
1061                                 struct sparingTable *st;
1062                                 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
1063
1064                                 UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15;
1065                                 UDF_SB_TYPESPAR(sb,i).s_packet_len = le16_to_cpu(spm->packetLength);
1066                                 for (j=0; j<spm->numSparingTables; j++)
1067                                 {
1068                                         loc = le32_to_cpu(spm->locSparingTable[j]);
1069                                         UDF_SB_TYPESPAR(sb,i).s_spar_map[j] =
1070                                                 udf_read_tagged(sb, loc, loc, &ident);
1071                                         if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL)
1072                                         {
1073                                                 st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,i).s_spar_map[j]->b_data;
1074                                                 if (ident != 0 ||
1075                                                         strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING)))
1076                                                 {
1077                                                         udf_release_data(UDF_SB_TYPESPAR(sb,i).s_spar_map[j]);
1078                                                         UDF_SB_TYPESPAR(sb,i).s_spar_map[j] = NULL;
1079                                                 }
1080                                         }
1081                                 }
1082                                 UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_spar15;
1083                         }
1084                         else
1085                         {
1086                                 udf_debug("Unknown ident: %s\n", upm2->partIdent.ident);
1087                                 continue;
1088                         }
1089                         UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum);
1090                         UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum);
1091                 }
1092                 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1093                         i, UDF_SB_PARTNUM(sb,i), type, UDF_SB_PARTVSN(sb,i));
1094         }
1095
1096         if (fileset)
1097         {
1098                 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1099
1100                 *fileset = lelb_to_cpu(la->extLocation);
1101                 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1102                         fileset->logicalBlockNum,
1103                         fileset->partitionReferenceNum);
1104         }
1105         if (lvd->integritySeqExt.extLength)
1106                 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1107         return 0;
1108 }
1109
1110 /*
1111  * udf_load_logicalvolint
1112  *
1113  */
1114 static void
1115 udf_load_logicalvolint(struct super_block *sb, extent_ad loc)
1116 {
1117         struct buffer_head *bh = NULL;
1118         uint16_t ident;
1119
1120         while (loc.extLength > 0 &&
1121                 (bh = udf_read_tagged(sb, loc.extLocation,
1122                         loc.extLocation, &ident)) &&
1123                 ident == TAG_IDENT_LVID)
1124         {
1125                 UDF_SB_LVIDBH(sb) = bh;
1126                 
1127                 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
1128                         udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
1129                 
1130                 if (UDF_SB_LVIDBH(sb) != bh)
1131                         udf_release_data(bh);
1132                 loc.extLength -= sb->s_blocksize;
1133                 loc.extLocation ++;
1134         }
1135         if (UDF_SB_LVIDBH(sb) != bh)
1136                 udf_release_data(bh);
1137 }
1138
1139 /*
1140  * udf_process_sequence
1141  *
1142  * PURPOSE
1143  *      Process a main/reserve volume descriptor sequence.
1144  *
1145  * PRE-CONDITIONS
1146  *      sb                      Pointer to _locked_ superblock.
1147  *      block                   First block of first extent of the sequence.
1148  *      lastblock               Lastblock of first extent of the sequence.
1149  *
1150  * HISTORY
1151  *      July 1, 1997 - Andrew E. Mileski
1152  *      Written, tested, and released.
1153  */
1154 static  int
1155 udf_process_sequence(struct super_block *sb, long block, long lastblock, lb_addr *fileset)
1156 {
1157         struct buffer_head *bh = NULL;
1158         struct udf_vds_record vds[VDS_POS_LENGTH];
1159         struct generic_desc *gd;
1160         struct volDescPtr *vdp;
1161         int done=0;
1162         int i,j;
1163         uint32_t vdsn;
1164         uint16_t ident;
1165         long next_s = 0, next_e = 0;
1166
1167         memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1168
1169         /* Read the main descriptor sequence */
1170         for (;(!done && block <= lastblock); block++)
1171         {
1172
1173                 bh = udf_read_tagged(sb, block, block, &ident);
1174                 if (!bh) 
1175                         break;
1176
1177                 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1178                 gd = (struct generic_desc *)bh->b_data;
1179                 vdsn = le32_to_cpu(gd->volDescSeqNum);
1180                 switch (ident)
1181                 {
1182                         case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1183                                 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum)
1184                                 {
1185                                         vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
1186                                         vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1187                                 }
1188                                 break;
1189                         case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1190                                 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum)
1191                                 {
1192                                         vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1193                                         vds[VDS_POS_VOL_DESC_PTR].block = block;
1194
1195                                         vdp = (struct volDescPtr *)bh->b_data;
1196                                         next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1197                                         next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
1198                                         next_e = next_e >> sb->s_blocksize_bits;
1199                                         next_e += next_s;
1200                                 }
1201                                 break;
1202                         case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1203                                 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum)
1204                                 {
1205                                         vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
1206                                         vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1207                                 }
1208                                 break;
1209                         case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1210                                 if (!vds[VDS_POS_PARTITION_DESC].block)
1211                                         vds[VDS_POS_PARTITION_DESC].block = block;
1212                                 break;
1213                         case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1214                                 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum)
1215                                 {
1216                                         vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
1217                                         vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1218                                 }
1219                                 break;
1220                         case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1221                                 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum)
1222                                 {
1223                                         vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
1224                                         vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1225                                 }
1226                                 break;
1227                         case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1228                                 vds[VDS_POS_TERMINATING_DESC].block = block;
1229                                 if (next_e)
1230                                 {
1231                                         block = next_s;
1232                                         lastblock = next_e;
1233                                         next_s = next_e = 0;
1234                                 }
1235                                 else
1236                                         done = 1;
1237                                 break;
1238                 }
1239                 udf_release_data(bh);
1240         }
1241         for (i=0; i<VDS_POS_LENGTH; i++)
1242         {
1243                 if (vds[i].block)
1244                 {
1245                         bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident);
1246
1247                         if (i == VDS_POS_PRIMARY_VOL_DESC)
1248                                 udf_load_pvoldesc(sb, bh);
1249                         else if (i == VDS_POS_LOGICAL_VOL_DESC)
1250                                 udf_load_logicalvol(sb, bh, fileset);
1251                         else if (i == VDS_POS_PARTITION_DESC)
1252                         {
1253                                 struct buffer_head *bh2 = NULL;
1254                                 udf_load_partdesc(sb, bh);
1255                                 for (j=vds[i].block+1; j<vds[VDS_POS_TERMINATING_DESC].block; j++)
1256                                 {
1257                                         bh2 = udf_read_tagged(sb, j, j, &ident);
1258                                         gd = (struct generic_desc *)bh2->b_data;
1259                                         if (ident == TAG_IDENT_PD)
1260                                                 udf_load_partdesc(sb, bh2);
1261                                         udf_release_data(bh2);
1262                                 }
1263                         }
1264                         udf_release_data(bh);
1265                 }
1266         }
1267
1268         return 0;
1269 }
1270
1271 /*
1272  * udf_check_valid()
1273  */
1274 static int
1275 udf_check_valid(struct super_block *sb, int novrs, int silent)
1276 {
1277         long block;
1278
1279         if (novrs)
1280         {
1281                 udf_debug("Validity check skipped because of novrs option\n");
1282                 return 0;
1283         }
1284         /* Check that it is NSR02 compliant */
1285         /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1286         else if ((block = udf_vrs(sb, silent)) == -1)
1287         {
1288                 udf_debug("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
1289                 if (!UDF_SB_LASTBLOCK(sb))
1290                         UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1291                 return 0;
1292         }
1293         else 
1294                 return !block;
1295 }
1296
1297 static int
1298 udf_load_partition(struct super_block *sb, lb_addr *fileset)
1299 {
1300         struct anchorVolDescPtr *anchor;
1301         uint16_t ident;
1302         struct buffer_head *bh;
1303         long main_s, main_e, reserve_s, reserve_e;
1304         int i, j;
1305
1306         if (!sb)
1307                 return 1;
1308
1309         for (i=0; i<sizeof(UDF_SB_ANCHOR(sb))/sizeof(int); i++)
1310         {
1311                 if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
1312                         UDF_SB_ANCHOR(sb)[i], UDF_SB_ANCHOR(sb)[i], &ident)))
1313                 {
1314                         anchor = (struct anchorVolDescPtr *)bh->b_data;
1315
1316                         /* Locate the main sequence */
1317                         main_s = le32_to_cpu( anchor->mainVolDescSeqExt.extLocation );
1318                         main_e = le32_to_cpu( anchor->mainVolDescSeqExt.extLength );
1319                         main_e = main_e >> sb->s_blocksize_bits;
1320                         main_e += main_s;
1321         
1322                         /* Locate the reserve sequence */
1323                         reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1324                         reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1325                         reserve_e = reserve_e >> sb->s_blocksize_bits;
1326                         reserve_e += reserve_s;
1327
1328                         udf_release_data(bh);
1329
1330                         /* Process the main & reserve sequences */
1331                         /* responsible for finding the PartitionDesc(s) */
1332                         if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1333                                 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1334                         {
1335                                 break;
1336                         }
1337                 }
1338         }
1339
1340         if (i == sizeof(UDF_SB_ANCHOR(sb))/sizeof(int))
1341         {
1342                 udf_debug("No Anchor block found\n");
1343                 return 1;
1344         }
1345         else
1346                 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1347
1348         for (i=0; i<UDF_SB_NUMPARTS(sb); i++)
1349         {
1350                 switch UDF_SB_PARTTYPE(sb, i)
1351                 {
1352                         case UDF_VIRTUAL_MAP15:
1353                         case UDF_VIRTUAL_MAP20:
1354                         {
1355                                 lb_addr ino;
1356
1357                                 if (!UDF_SB_LASTBLOCK(sb))
1358                                 {
1359                                         UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1360                                         udf_find_anchor(sb);
1361                                 }
1362
1363                                 if (!UDF_SB_LASTBLOCK(sb))
1364                                 {
1365                                         udf_debug("Unable to determine Lastblock (For Virtual Partition)\n");
1366                                         return 1;
1367                                 }
1368
1369                                 for (j=0; j<UDF_SB_NUMPARTS(sb); j++)
1370                                 {
1371                                         if (j != i &&
1372                                                 UDF_SB_PARTVSN(sb,i) == UDF_SB_PARTVSN(sb,j) &&
1373                                                 UDF_SB_PARTNUM(sb,i) == UDF_SB_PARTNUM(sb,j))
1374                                         {
1375                                                 ino.partitionReferenceNum = j;
1376                                                 ino.logicalBlockNum = UDF_SB_LASTBLOCK(sb) -
1377                                                         UDF_SB_PARTROOT(sb,j);
1378                                                 break;
1379                                         }
1380                                 }
1381
1382                                 if (j == UDF_SB_NUMPARTS(sb))
1383                                         return 1;
1384
1385                                 if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1386                                         return 1;
1387
1388                                 if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP15)
1389                                 {
1390                                         UDF_SB_TYPEVIRT(sb,i).s_start_offset = udf_ext0_offset(UDF_SB_VAT(sb));
1391                                         UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1392                                 }
1393                                 else if (UDF_SB_PARTTYPE(sb,i) == UDF_VIRTUAL_MAP20)
1394                                 {
1395                                         struct buffer_head *bh = NULL;
1396                                         uint32_t pos;
1397
1398                                         pos = udf_block_map(UDF_SB_VAT(sb), 0);
1399                                         bh = sb_bread(sb, pos);
1400                                         UDF_SB_TYPEVIRT(sb,i).s_start_offset =
1401                                                 le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data + udf_ext0_offset(UDF_SB_VAT(sb)))->lengthHeader) +
1402                                                         udf_ext0_offset(UDF_SB_VAT(sb));
1403                                         UDF_SB_TYPEVIRT(sb,i).s_num_entries = (UDF_SB_VAT(sb)->i_size -
1404                                                 UDF_SB_TYPEVIRT(sb,i).s_start_offset) >> 2;
1405                                         udf_release_data(bh);
1406                                 }
1407                                 UDF_SB_PARTROOT(sb,i) = udf_get_pblock(sb, 0, i, 0);
1408                                 UDF_SB_PARTLEN(sb,i) = UDF_SB_PARTLEN(sb,ino.partitionReferenceNum);
1409                         }
1410                 }
1411         }
1412         return 0;
1413 }
1414
1415 static void udf_open_lvid(struct super_block *sb)
1416 {
1417         if (UDF_SB_LVIDBH(sb))
1418         {
1419                 int i;
1420                 timestamp cpu_time;
1421
1422                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1423                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1424                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1425                         UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1426                 UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1427
1428                 UDF_SB_LVID(sb)->descTag.descCRC =
1429                         cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1430                         le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1431
1432                 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1433                 for (i=0; i<16; i++)
1434                         if (i != 4)
1435                                 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1436                                         ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
1437
1438                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1439         }
1440 }
1441
1442 static void udf_close_lvid(struct super_block *sb)
1443 {
1444         if (UDF_SB_LVIDBH(sb) &&
1445                 UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN)
1446         {
1447                 int i;
1448                 timestamp cpu_time;
1449
1450                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1451                 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1452                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1453                         UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1454                 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1455                         UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1456                 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1457                         UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1458                 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1459                         UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1460                 UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_CLOSE;
1461
1462                 UDF_SB_LVID(sb)->descTag.descCRC =
1463                         cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1464                         le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
1465
1466                 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
1467                 for (i=0; i<16; i++)
1468                         if (i != 4)
1469                                 UDF_SB_LVID(sb)->descTag.tagChecksum +=
1470                                         ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
1471
1472                 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1473         }
1474 }
1475
1476 /*
1477  * udf_read_super
1478  *
1479  * PURPOSE
1480  *      Complete the specified super block.
1481  *
1482  * PRE-CONDITIONS
1483  *      sb                      Pointer to superblock to complete - never NULL.
1484  *      sb->s_dev               Device to read suberblock from.
1485  *      options                 Pointer to mount options.
1486  *      silent                  Silent flag.
1487  *
1488  * HISTORY
1489  *      July 1, 1997 - Andrew E. Mileski
1490  *      Written, tested, and released.
1491  */
1492 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1493 {
1494         int i;
1495         struct inode *inode=NULL;
1496         struct udf_options uopt;
1497         lb_addr rootdir, fileset;
1498         struct udf_sb_info *sbi;
1499
1500         uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1501         uopt.uid = -1;
1502         uopt.gid = -1;
1503         uopt.umask = 0;
1504
1505         sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1506         if (!sbi)
1507                 return -ENOMEM;
1508         sb->s_fs_info = sbi;
1509         memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1510
1511         if (!udf_parse_options((char *)options, &uopt))
1512                 goto error_out;
1513
1514         if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1515             uopt.flags & (1 << UDF_FLAG_NLS_MAP))
1516         {
1517                 udf_error(sb, "udf_read_super",
1518                         "utf8 cannot be combined with iocharset\n");
1519                 goto error_out;
1520         }
1521 #if defined(CONFIG_NLS) || defined(CONFIG_NLS_MODULE)
1522         if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map)
1523         {
1524                 uopt.nls_map = load_nls_default();
1525                 if (!uopt.nls_map)
1526                         uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1527                 else
1528                         udf_debug("Using default NLS map\n");
1529         }
1530 #endif
1531         if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1532                 uopt.flags |= (1 << UDF_FLAG_UTF8);
1533
1534         fileset.logicalBlockNum = 0xFFFFFFFF;
1535         fileset.partitionReferenceNum = 0xFFFF;
1536
1537         UDF_SB(sb)->s_flags = uopt.flags;
1538         UDF_SB(sb)->s_uid = uopt.uid;
1539         UDF_SB(sb)->s_gid = uopt.gid;
1540         UDF_SB(sb)->s_umask = uopt.umask;
1541         UDF_SB(sb)->s_nls_map = uopt.nls_map;
1542
1543         /* Set the block size for all transfers */
1544         if (!udf_set_blocksize(sb, uopt.blocksize))
1545                 goto error_out;
1546
1547         if ( uopt.session == 0xFFFFFFFF )
1548                 UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1549         else
1550                 UDF_SB_SESSION(sb) = uopt.session;
1551
1552         udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1553
1554         UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1555         UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0;
1556         UDF_SB_ANCHOR(sb)[2] = uopt.anchor;
1557         UDF_SB_ANCHOR(sb)[3] = 256;
1558
1559         if (udf_check_valid(sb, uopt.novrs, silent)) /* read volume recognition sequences */
1560         {
1561                 printk("UDF-fs: No VRS found\n");
1562                 goto error_out;
1563         }
1564
1565         udf_find_anchor(sb);
1566
1567         /* Fill in the rest of the superblock */
1568         sb->s_op = &udf_sb_ops;
1569         sb->dq_op = NULL;
1570         sb->s_dirt = 0;
1571         sb->s_magic = UDF_SUPER_MAGIC;
1572
1573         if (udf_load_partition(sb, &fileset))
1574         {
1575                 printk("UDF-fs: No partition found (1)\n");
1576                 goto error_out;
1577         }
1578
1579         udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1580
1581         if ( UDF_SB_LVIDBH(sb) )
1582         {
1583                 uint16_t minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1584                 uint16_t minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
1585                 /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1586
1587                 if (minUDFReadRev > UDF_MAX_READ_VERSION)
1588                 {
1589                         printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
1590                                 UDF_SB_LVIDIU(sb)->minUDFReadRev, 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                 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 #if defined(CONFIG_NLS) || defined(CONFIG_NLS_MODULE)
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 #if defined(CONFIG_NLS) || defined(CONFIG_NLS_MODULE)
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         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 = 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         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 }