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