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