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