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