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