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