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