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