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