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