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