fs/udf: Use DIV_ROUND_UP
[linux-3.10.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 DIV_ROUND_UP(map->s_partition_len +
990                             (sizeof(struct spaceBitmapDesc) << 3),
991                             sb->s_blocksize * 8);
992 }
993
994 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
995 {
996         struct udf_bitmap *bitmap;
997         int nr_groups;
998         int size;
999
1000         nr_groups = udf_compute_nr_groups(sb, index);
1001         size = sizeof(struct udf_bitmap) +
1002                 (sizeof(struct buffer_head *) * nr_groups);
1003
1004         if (size <= PAGE_SIZE)
1005                 bitmap = kmalloc(size, GFP_KERNEL);
1006         else
1007                 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1008
1009         if (bitmap == NULL) {
1010                 udf_error(sb, __FUNCTION__,
1011                           "Unable to allocate space for bitmap "
1012                           "and %d buffer_head pointers", nr_groups);
1013                 return NULL;
1014         }
1015
1016         memset(bitmap, 0x00, size);
1017         bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1018         bitmap->s_nr_groups = nr_groups;
1019         return bitmap;
1020 }
1021
1022 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
1023 {
1024         struct partitionDesc *p;
1025         int i;
1026         struct udf_part_map *map;
1027         struct udf_sb_info *sbi;
1028
1029         p = (struct partitionDesc *)bh->b_data;
1030         sbi = UDF_SB(sb);
1031
1032         for (i = 0; i < sbi->s_partitions; i++) {
1033                 map = &sbi->s_partmaps[i];
1034                 udf_debug("Searching map: (%d == %d)\n",
1035                           map->s_partition_num,
1036                           le16_to_cpu(p->partitionNumber));
1037                 if (map->s_partition_num ==
1038                                 le16_to_cpu(p->partitionNumber)) {
1039                         map->s_partition_len =
1040                                 le32_to_cpu(p->partitionLength); /* blocks */
1041                         map->s_partition_root =
1042                                 le32_to_cpu(p->partitionStartingLocation);
1043                         if (p->accessType ==
1044                                         cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1045                                 map->s_partition_flags |=
1046                                                 UDF_PART_FLAG_READ_ONLY;
1047                         if (p->accessType ==
1048                                         cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1049                                 map->s_partition_flags |=
1050                                                 UDF_PART_FLAG_WRITE_ONCE;
1051                         if (p->accessType ==
1052                                         cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1053                                 map->s_partition_flags |=
1054                                                 UDF_PART_FLAG_REWRITABLE;
1055                         if (p->accessType ==
1056                                     cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1057                                 map->s_partition_flags |=
1058                                                 UDF_PART_FLAG_OVERWRITABLE;
1059
1060                         if (!strcmp(p->partitionContents.ident,
1061                                     PD_PARTITION_CONTENTS_NSR02) ||
1062                             !strcmp(p->partitionContents.ident,
1063                                     PD_PARTITION_CONTENTS_NSR03)) {
1064                                 struct partitionHeaderDesc *phd;
1065
1066                                 phd = (struct partitionHeaderDesc *)
1067                                                 (p->partitionContentsUse);
1068                                 if (phd->unallocSpaceTable.extLength) {
1069                                         kernel_lb_addr loc = {
1070                                                 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1071                                                 .partitionReferenceNum = i,
1072                                         };
1073
1074                                         map->s_uspace.s_table =
1075                                                 udf_iget(sb, loc);
1076                                         if (!map->s_uspace.s_table) {
1077                                                 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
1078                                                 return 1;
1079                                         }
1080                                         map->s_partition_flags |=
1081                                                 UDF_PART_FLAG_UNALLOC_TABLE;
1082                                         udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1083                                                   i, map->s_uspace.s_table->i_ino);
1084                                 }
1085                                 if (phd->unallocSpaceBitmap.extLength) {
1086                                         struct udf_bitmap *bitmap =
1087                                                 udf_sb_alloc_bitmap(sb, i);
1088                                         map->s_uspace.s_bitmap = bitmap;
1089                                         if (bitmap != NULL) {
1090                                                 bitmap->s_extLength =
1091                                                         le32_to_cpu(phd->unallocSpaceBitmap.extLength);
1092                                                 bitmap->s_extPosition =
1093                                                         le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
1094                                                 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1095                                                 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1096                                                           i, bitmap->s_extPosition);
1097                                         }
1098                                 }
1099                                 if (phd->partitionIntegrityTable.extLength)
1100                                         udf_debug("partitionIntegrityTable (part %d)\n", i);
1101                                 if (phd->freedSpaceTable.extLength) {
1102                                         kernel_lb_addr loc = {
1103                                                 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1104                                                 .partitionReferenceNum = i,
1105                                         };
1106
1107                                         map->s_fspace.s_table =
1108                                                 udf_iget(sb, loc);
1109                                         if (!map->s_fspace.s_table) {
1110                                                 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
1111                                                 return 1;
1112                                         }
1113                                         map->s_partition_flags |=
1114                                                 UDF_PART_FLAG_FREED_TABLE;
1115                                         udf_debug("freedSpaceTable (part %d) @ %ld\n",
1116                                                   i, map->s_fspace.s_table->i_ino);
1117                                 }
1118                                 if (phd->freedSpaceBitmap.extLength) {
1119                                         struct udf_bitmap *bitmap =
1120                                                 udf_sb_alloc_bitmap(sb, i);
1121                                         map->s_fspace.s_bitmap = bitmap;
1122                                         if (bitmap != NULL) {
1123                                                 bitmap->s_extLength =
1124                                                         le32_to_cpu(phd->freedSpaceBitmap.extLength);
1125                                                 bitmap->s_extPosition =
1126                                                         le32_to_cpu(phd->freedSpaceBitmap.extPosition);
1127                                                 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1128                                                 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1129                                                           i, bitmap->s_extPosition);
1130                                         }
1131                                 }
1132                         }
1133                         break;
1134                 }
1135         }
1136         if (i == sbi->s_partitions)
1137                 udf_debug("Partition (%d) not found in partition map\n",
1138                           le16_to_cpu(p->partitionNumber));
1139         else
1140                 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1141                           "block length %d\n",
1142                           le16_to_cpu(p->partitionNumber), i,
1143                           map->s_partition_type,
1144                           map->s_partition_root,
1145                           map->s_partition_len);
1146         return 0;
1147 }
1148
1149 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1150                                kernel_lb_addr *fileset)
1151 {
1152         struct logicalVolDesc *lvd;
1153         int i, j, offset;
1154         uint8_t type;
1155         struct udf_sb_info *sbi = UDF_SB(sb);
1156         struct genericPartitionMap *gpm;
1157
1158         lvd = (struct logicalVolDesc *)bh->b_data;
1159
1160         i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1161         if (i != 0)
1162                 return i;
1163
1164         for (i = 0, offset = 0;
1165              i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1166              i++, offset += gpm->partitionMapLength) {
1167                 struct udf_part_map *map = &sbi->s_partmaps[i];
1168                 gpm = (struct genericPartitionMap *)
1169                                 &(lvd->partitionMaps[offset]);
1170                 type = gpm->partitionMapType;
1171                 if (type == 1) {
1172                         struct genericPartitionMap1 *gpm1 =
1173                                 (struct genericPartitionMap1 *)gpm;
1174                         map->s_partition_type = UDF_TYPE1_MAP15;
1175                         map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1176                         map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1177                         map->s_partition_func = NULL;
1178                 } else if (type == 2) {
1179                         struct udfPartitionMap2 *upm2 =
1180                                                 (struct udfPartitionMap2 *)gpm;
1181                         if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1182                                                 strlen(UDF_ID_VIRTUAL))) {
1183                                 u16 suf =
1184                                         le16_to_cpu(((__le16 *)upm2->partIdent.
1185                                                         identSuffix)[0]);
1186                                 if (suf == 0x0150) {
1187                                         map->s_partition_type =
1188                                                         UDF_VIRTUAL_MAP15;
1189                                         map->s_partition_func =
1190                                                         udf_get_pblock_virt15;
1191                                 } else if (suf == 0x0200) {
1192                                         map->s_partition_type =
1193                                                         UDF_VIRTUAL_MAP20;
1194                                         map->s_partition_func =
1195                                                         udf_get_pblock_virt20;
1196                                 }
1197                         } else if (!strncmp(upm2->partIdent.ident,
1198                                                 UDF_ID_SPARABLE,
1199                                                 strlen(UDF_ID_SPARABLE))) {
1200                                 uint32_t loc;
1201                                 uint16_t ident;
1202                                 struct sparingTable *st;
1203                                 struct sparablePartitionMap *spm =
1204                                         (struct sparablePartitionMap *)gpm;
1205
1206                                 map->s_partition_type = UDF_SPARABLE_MAP15;
1207                                 map->s_type_specific.s_sparing.s_packet_len =
1208                                                 le16_to_cpu(spm->packetLength);
1209                                 for (j = 0; j < spm->numSparingTables; j++) {
1210                                         struct buffer_head *bh2;
1211
1212                                         loc = le32_to_cpu(
1213                                                 spm->locSparingTable[j]);
1214                                         bh2 = udf_read_tagged(sb, loc, loc,
1215                                                              &ident);
1216                                         map->s_type_specific.s_sparing.
1217                                                         s_spar_map[j] = bh2;
1218
1219                                         if (bh2 != NULL) {
1220                                                 st = (struct sparingTable *)
1221                                                                 bh2->b_data;
1222                                                 if (ident != 0 || strncmp(
1223                                                         st->sparingIdent.ident,
1224                                                         UDF_ID_SPARING,
1225                                                         strlen(UDF_ID_SPARING))) {
1226                                                         brelse(bh2);
1227                                                         map->s_type_specific.
1228                                                                 s_sparing.
1229                                                                 s_spar_map[j] =
1230                                                                         NULL;
1231                                                 }
1232                                         }
1233                                 }
1234                                 map->s_partition_func = udf_get_pblock_spar15;
1235                         } else {
1236                                 udf_debug("Unknown ident: %s\n",
1237                                           upm2->partIdent.ident);
1238                                 continue;
1239                         }
1240                         map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1241                         map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1242                 }
1243                 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1244                           i, map->s_partition_num, type,
1245                           map->s_volumeseqnum);
1246         }
1247
1248         if (fileset) {
1249                 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1250
1251                 *fileset = lelb_to_cpu(la->extLocation);
1252                 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1253                           "partition=%d\n", fileset->logicalBlockNum,
1254                           fileset->partitionReferenceNum);
1255         }
1256         if (lvd->integritySeqExt.extLength)
1257                 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1258
1259         return 0;
1260 }
1261
1262 /*
1263  * udf_load_logicalvolint
1264  *
1265  */
1266 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1267 {
1268         struct buffer_head *bh = NULL;
1269         uint16_t ident;
1270         struct udf_sb_info *sbi = UDF_SB(sb);
1271         struct logicalVolIntegrityDesc *lvid;
1272
1273         while (loc.extLength > 0 &&
1274                (bh = udf_read_tagged(sb, loc.extLocation,
1275                                      loc.extLocation, &ident)) &&
1276                ident == TAG_IDENT_LVID) {
1277                 sbi->s_lvid_bh = bh;
1278                 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1279
1280                 if (lvid->nextIntegrityExt.extLength)
1281                         udf_load_logicalvolint(sb,
1282                                 leea_to_cpu(lvid->nextIntegrityExt));
1283
1284                 if (sbi->s_lvid_bh != bh)
1285                         brelse(bh);
1286                 loc.extLength -= sb->s_blocksize;
1287                 loc.extLocation++;
1288         }
1289         if (sbi->s_lvid_bh != bh)
1290                 brelse(bh);
1291 }
1292
1293 /*
1294  * udf_process_sequence
1295  *
1296  * PURPOSE
1297  *      Process a main/reserve volume descriptor sequence.
1298  *
1299  * PRE-CONDITIONS
1300  *      sb                      Pointer to _locked_ superblock.
1301  *      block                   First block of first extent of the sequence.
1302  *      lastblock               Lastblock of first extent of the sequence.
1303  *
1304  * HISTORY
1305  *      July 1, 1997 - Andrew E. Mileski
1306  *      Written, tested, and released.
1307  */
1308 static int udf_process_sequence(struct super_block *sb, long block,
1309                                 long lastblock, kernel_lb_addr *fileset)
1310 {
1311         struct buffer_head *bh = NULL;
1312         struct udf_vds_record vds[VDS_POS_LENGTH];
1313         struct udf_vds_record *curr;
1314         struct generic_desc *gd;
1315         struct volDescPtr *vdp;
1316         int done = 0;
1317         int i, j;
1318         uint32_t vdsn;
1319         uint16_t ident;
1320         long next_s = 0, next_e = 0;
1321
1322         memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1323
1324         /* Read the main descriptor sequence */
1325         for (; (!done && block <= lastblock); block++) {
1326
1327                 bh = udf_read_tagged(sb, block, block, &ident);
1328                 if (!bh)
1329                         break;
1330
1331                 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1332                 gd = (struct generic_desc *)bh->b_data;
1333                 vdsn = le32_to_cpu(gd->volDescSeqNum);
1334                 switch (ident) {
1335                 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1336                         curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1337                         if (vdsn >= curr->volDescSeqNum) {
1338                                 curr->volDescSeqNum = vdsn;
1339                                 curr->block = block;
1340                         }
1341                         break;
1342                 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1343                         curr = &vds[VDS_POS_VOL_DESC_PTR];
1344                         if (vdsn >= curr->volDescSeqNum) {
1345                                 curr->volDescSeqNum = vdsn;
1346                                 curr->block = block;
1347
1348                                 vdp = (struct volDescPtr *)bh->b_data;
1349                                 next_s = le32_to_cpu(
1350                                         vdp->nextVolDescSeqExt.extLocation);
1351                                 next_e = le32_to_cpu(
1352                                         vdp->nextVolDescSeqExt.extLength);
1353                                 next_e = next_e >> sb->s_blocksize_bits;
1354                                 next_e += next_s;
1355                         }
1356                         break;
1357                 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1358                         curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1359                         if (vdsn >= curr->volDescSeqNum) {
1360                                 curr->volDescSeqNum = vdsn;
1361                                 curr->block = block;
1362                         }
1363                         break;
1364                 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1365                         curr = &vds[VDS_POS_PARTITION_DESC];
1366                         if (!curr->block)
1367                                 curr->block = block;
1368                         break;
1369                 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1370                         curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1371                         if (vdsn >= curr->volDescSeqNum) {
1372                                 curr->volDescSeqNum = vdsn;
1373                                 curr->block = block;
1374                         }
1375                         break;
1376                 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1377                         curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1378                         if (vdsn >= curr->volDescSeqNum) {
1379                                 curr->volDescSeqNum = vdsn;
1380                                 curr->block = block;
1381                         }
1382                         break;
1383                 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1384                         vds[VDS_POS_TERMINATING_DESC].block = block;
1385                         if (next_e) {
1386                                 block = next_s;
1387                                 lastblock = next_e;
1388                                 next_s = next_e = 0;
1389                         } else
1390                                 done = 1;
1391                         break;
1392                 }
1393                 brelse(bh);
1394         }
1395         for (i = 0; i < VDS_POS_LENGTH; i++) {
1396                 if (vds[i].block) {
1397                         bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1398                                              &ident);
1399
1400                         if (i == VDS_POS_PRIMARY_VOL_DESC) {
1401                                 udf_load_pvoldesc(sb, bh);
1402                         } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1403                                 if (udf_load_logicalvol(sb, bh, fileset)) {
1404                                         brelse(bh);
1405                                         return 1;
1406                                 }
1407                         } else if (i == VDS_POS_PARTITION_DESC) {
1408                                 struct buffer_head *bh2 = NULL;
1409                                 if (udf_load_partdesc(sb, bh)) {
1410                                         brelse(bh);
1411                                         return 1;
1412                                 }
1413                                 for (j = vds[i].block + 1;
1414                                      j <  vds[VDS_POS_TERMINATING_DESC].block;
1415                                      j++) {
1416                                         bh2 = udf_read_tagged(sb, j, j, &ident);
1417                                         gd = (struct generic_desc *)bh2->b_data;
1418                                         if (ident == TAG_IDENT_PD)
1419                                                 if (udf_load_partdesc(sb,
1420                                                                       bh2)) {
1421                                                         brelse(bh);
1422                                                         brelse(bh2);
1423                                                         return 1;
1424                                                 }
1425                                         brelse(bh2);
1426                                 }
1427                         }
1428                         brelse(bh);
1429                 }
1430         }
1431
1432         return 0;
1433 }
1434
1435 /*
1436  * udf_check_valid()
1437  */
1438 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1439 {
1440         long block;
1441
1442         if (novrs) {
1443                 udf_debug("Validity check skipped because of novrs option\n");
1444                 return 0;
1445         }
1446         /* Check that it is NSR02 compliant */
1447         /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1448         else {
1449                 block = udf_vrs(sb, silent);
1450                 if (block == -1) {
1451                         struct udf_sb_info *sbi = UDF_SB(sb);
1452                         udf_debug("Failed to read byte 32768. Assuming open "
1453                                   "disc. Skipping validity check\n");
1454                         if (!sbi->s_last_block)
1455                                 sbi->s_last_block = udf_get_last_block(sb);
1456                         return 0;
1457                 } else
1458                         return !block;
1459         }
1460 }
1461
1462 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1463 {
1464         struct anchorVolDescPtr *anchor;
1465         uint16_t ident;
1466         struct buffer_head *bh;
1467         long main_s, main_e, reserve_s, reserve_e;
1468         int i, j;
1469         struct udf_sb_info *sbi;
1470
1471         if (!sb)
1472                 return 1;
1473         sbi = UDF_SB(sb);
1474
1475         for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1476                 if (!sbi->s_anchor[i])
1477                         continue;
1478                 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1479                                      &ident);
1480                 if (!bh)
1481                         continue;
1482
1483                 anchor = (struct anchorVolDescPtr *)bh->b_data;
1484
1485                 /* Locate the main sequence */
1486                 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1487                 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1488                 main_e = main_e >> sb->s_blocksize_bits;
1489                 main_e += main_s;
1490
1491                 /* Locate the reserve sequence */
1492                 reserve_s = le32_to_cpu(
1493                                 anchor->reserveVolDescSeqExt.extLocation);
1494                 reserve_e = le32_to_cpu(
1495                                 anchor->reserveVolDescSeqExt.extLength);
1496                 reserve_e = reserve_e >> sb->s_blocksize_bits;
1497                 reserve_e += reserve_s;
1498
1499                 brelse(bh);
1500
1501                 /* Process the main & reserve sequences */
1502                 /* responsible for finding the PartitionDesc(s) */
1503                 if (!(udf_process_sequence(sb, main_s, main_e,
1504                                            fileset) &&
1505                       udf_process_sequence(sb, reserve_s, reserve_e,
1506                                            fileset)))
1507                         break;
1508         }
1509
1510         if (i == ARRAY_SIZE(sbi->s_anchor)) {
1511                 udf_debug("No Anchor block found\n");
1512                 return 1;
1513         }
1514         udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1515
1516         for (i = 0; i < sbi->s_partitions; i++) {
1517                 kernel_lb_addr uninitialized_var(ino);
1518                 struct udf_part_map *map = &sbi->s_partmaps[i];
1519                 switch (map->s_partition_type) {
1520                 case UDF_VIRTUAL_MAP15:
1521                 case UDF_VIRTUAL_MAP20:
1522                         if (!sbi->s_last_block) {
1523                                 sbi->s_last_block = udf_get_last_block(sb);
1524                                 udf_find_anchor(sb);
1525                         }
1526
1527                         if (!sbi->s_last_block) {
1528                                 udf_debug("Unable to determine Lastblock (For "
1529                                           "Virtual Partition)\n");
1530                                 return 1;
1531                         }
1532
1533                         for (j = 0; j < sbi->s_partitions; j++) {
1534                                 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1535                                 if (j != i &&
1536                                     map->s_volumeseqnum ==
1537                                                 map2->s_volumeseqnum &&
1538                                     map->s_partition_num ==
1539                                                 map2->s_partition_num) {
1540                                         ino.partitionReferenceNum = j;
1541                                         ino.logicalBlockNum =
1542                                                 sbi->s_last_block -
1543                                                         map2->s_partition_root;
1544                                         break;
1545                                 }
1546                         }
1547
1548                         if (j == sbi->s_partitions)
1549                                 return 1;
1550
1551                         sbi->s_vat_inode = udf_iget(sb, ino);
1552                         if (!sbi->s_vat_inode)
1553                                 return 1;
1554
1555                         if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1556                                 map->s_type_specific.s_virtual.s_start_offset =
1557                                         udf_ext0_offset(sbi->s_vat_inode);
1558                                 map->s_type_specific.s_virtual.s_num_entries =
1559                                         (sbi->s_vat_inode->i_size - 36) >> 2;
1560                         } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1561                                 uint32_t pos;
1562                                 struct virtualAllocationTable20 *vat20;
1563
1564                                 pos = udf_block_map(sbi->s_vat_inode, 0);
1565                                 bh = sb_bread(sb, pos);
1566                                 if (!bh)
1567                                         return 1;
1568                                 vat20 = (struct virtualAllocationTable20 *)
1569                                         bh->b_data +
1570                                         udf_ext0_offset(sbi->s_vat_inode);
1571                                 map->s_type_specific.s_virtual.s_start_offset =
1572                                         le16_to_cpu(vat20->lengthHeader) +
1573                                         udf_ext0_offset(sbi->s_vat_inode);
1574                                 map->s_type_specific.s_virtual.s_num_entries =
1575                                         (sbi->s_vat_inode->i_size -
1576                                          map->s_type_specific.s_virtual.
1577                                                         s_start_offset) >> 2;
1578                                 brelse(bh);
1579                         }
1580                         map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1581                         map->s_partition_len =
1582                                 sbi->s_partmaps[ino.partitionReferenceNum].
1583                                                                 s_partition_len;
1584                 }
1585         }
1586         return 0;
1587 }
1588
1589 static void udf_open_lvid(struct super_block *sb)
1590 {
1591         struct udf_sb_info *sbi = UDF_SB(sb);
1592         struct buffer_head *bh = sbi->s_lvid_bh;
1593         if (bh) {
1594                 kernel_timestamp cpu_time;
1595                 struct logicalVolIntegrityDesc *lvid =
1596                                 (struct logicalVolIntegrityDesc *)bh->b_data;
1597                 struct logicalVolIntegrityDescImpUse *lvidiu =
1598                                                         udf_sb_lvidiu(sbi);
1599
1600                 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1601                 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1602                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1603                         lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1604                 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1605
1606                 lvid->descTag.descCRC = cpu_to_le16(
1607                         udf_crc((char *)lvid + sizeof(tag),
1608                                 le16_to_cpu(lvid->descTag.descCRCLength),
1609                                 0));
1610
1611                 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1612                 mark_buffer_dirty(bh);
1613         }
1614 }
1615
1616 static void udf_close_lvid(struct super_block *sb)
1617 {
1618         kernel_timestamp cpu_time;
1619         struct udf_sb_info *sbi = UDF_SB(sb);
1620         struct buffer_head *bh = sbi->s_lvid_bh;
1621         struct logicalVolIntegrityDesc *lvid;
1622
1623         if (!bh)
1624                 return;
1625
1626         lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1627
1628         if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1629                 struct logicalVolIntegrityDescImpUse *lvidiu =
1630                                                         udf_sb_lvidiu(sbi);
1631                 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1632                 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1633                 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1634                         lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1635                 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1636                         lvidiu->maxUDFWriteRev =
1637                                         cpu_to_le16(UDF_MAX_WRITE_VERSION);
1638                 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1639                         lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1640                 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1641                         lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1642                 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1643
1644                 lvid->descTag.descCRC = cpu_to_le16(
1645                         udf_crc((char *)lvid + sizeof(tag),
1646                                 le16_to_cpu(lvid->descTag.descCRCLength),
1647                                 0));
1648
1649                 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1650                 mark_buffer_dirty(bh);
1651         }
1652 }
1653
1654 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1655 {
1656         int i;
1657         int nr_groups = bitmap->s_nr_groups;
1658         int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1659                                                 nr_groups);
1660
1661         for (i = 0; i < nr_groups; i++)
1662                 if (bitmap->s_block_bitmap[i])
1663                         brelse(bitmap->s_block_bitmap[i]);
1664
1665         if (size <= PAGE_SIZE)
1666                 kfree(bitmap);
1667         else
1668                 vfree(bitmap);
1669 }
1670
1671 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1672 {
1673         int i;
1674         struct inode *inode = NULL;
1675         struct udf_options uopt;
1676         kernel_lb_addr rootdir, fileset;
1677         struct udf_sb_info *sbi;
1678
1679         uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1680         uopt.uid = -1;
1681         uopt.gid = -1;
1682         uopt.umask = 0;
1683
1684         sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1685         if (!sbi)
1686                 return -ENOMEM;
1687
1688         sb->s_fs_info = sbi;
1689
1690         mutex_init(&sbi->s_alloc_mutex);
1691
1692         if (!udf_parse_options((char *)options, &uopt, false))
1693                 goto error_out;
1694
1695         if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1696             uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1697                 udf_error(sb, "udf_read_super",
1698                           "utf8 cannot be combined with iocharset\n");
1699                 goto error_out;
1700         }
1701 #ifdef CONFIG_UDF_NLS
1702         if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1703                 uopt.nls_map = load_nls_default();
1704                 if (!uopt.nls_map)
1705                         uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1706                 else
1707                         udf_debug("Using default NLS map\n");
1708         }
1709 #endif
1710         if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1711                 uopt.flags |= (1 << UDF_FLAG_UTF8);
1712
1713         fileset.logicalBlockNum = 0xFFFFFFFF;
1714         fileset.partitionReferenceNum = 0xFFFF;
1715
1716         sbi->s_flags = uopt.flags;
1717         sbi->s_uid = uopt.uid;
1718         sbi->s_gid = uopt.gid;
1719         sbi->s_umask = uopt.umask;
1720         sbi->s_nls_map = uopt.nls_map;
1721
1722         /* Set the block size for all transfers */
1723         if (!sb_min_blocksize(sb, uopt.blocksize)) {
1724                 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1725                 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1726                 goto error_out;
1727         }
1728
1729         if (uopt.session == 0xFFFFFFFF)
1730                 sbi->s_session = udf_get_last_session(sb);
1731         else
1732                 sbi->s_session = uopt.session;
1733
1734         udf_debug("Multi-session=%d\n", sbi->s_session);
1735
1736         sbi->s_last_block = uopt.lastblock;
1737         sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1738         sbi->s_anchor[2] = uopt.anchor;
1739         sbi->s_anchor[3] = 256;
1740
1741         if (udf_check_valid(sb, uopt.novrs, silent)) {
1742                 /* read volume recognition sequences */
1743                 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1744                 goto error_out;
1745         }
1746
1747         udf_find_anchor(sb);
1748
1749         /* Fill in the rest of the superblock */
1750         sb->s_op = &udf_sb_ops;
1751         sb->dq_op = NULL;
1752         sb->s_dirt = 0;
1753         sb->s_magic = UDF_SUPER_MAGIC;
1754         sb->s_time_gran = 1000;
1755
1756         if (udf_load_partition(sb, &fileset)) {
1757                 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1758                 goto error_out;
1759         }
1760
1761         udf_debug("Lastblock=%d\n", sbi->s_last_block);
1762
1763         if (sbi->s_lvid_bh) {
1764                 struct logicalVolIntegrityDescImpUse *lvidiu =
1765                                                         udf_sb_lvidiu(sbi);
1766                 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1767                 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1768                 /* uint16_t maxUDFWriteRev =
1769                                 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1770
1771                 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1772                         printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1773                                         "(max is %x)\n",
1774                                le16_to_cpu(lvidiu->minUDFReadRev),
1775                                UDF_MAX_READ_VERSION);
1776                         goto error_out;
1777                 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1778                         sb->s_flags |= MS_RDONLY;
1779
1780                 sbi->s_udfrev = minUDFWriteRev;
1781
1782                 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1783                         UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1784                 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1785                         UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1786         }
1787
1788         if (!sbi->s_partitions) {
1789                 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1790                 goto error_out;
1791         }
1792
1793         if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1794                         UDF_PART_FLAG_READ_ONLY) {
1795                 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1796                                    "forcing readonly mount\n");
1797                 sb->s_flags |= MS_RDONLY;
1798         }
1799
1800         if (udf_find_fileset(sb, &fileset, &rootdir)) {
1801                 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1802                 goto error_out;
1803         }
1804
1805         if (!silent) {
1806                 kernel_timestamp ts;
1807                 udf_time_to_stamp(&ts, sbi->s_record_time);
1808                 udf_info("UDF: Mounting volume '%s', "
1809                          "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1810                          sbi->s_volume_ident, ts.year, ts.month, ts.day,
1811                          ts.hour, ts.minute, ts.typeAndTimezone);
1812         }
1813         if (!(sb->s_flags & MS_RDONLY))
1814                 udf_open_lvid(sb);
1815
1816         /* Assign the root inode */
1817         /* assign inodes by physical block number */
1818         /* perhaps it's not extensible enough, but for now ... */
1819         inode = udf_iget(sb, rootdir);
1820         if (!inode) {
1821                 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1822                                 "partition=%d\n",
1823                        rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1824                 goto error_out;
1825         }
1826
1827         /* Allocate a dentry for the root inode */
1828         sb->s_root = d_alloc_root(inode);
1829         if (!sb->s_root) {
1830                 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1831                 iput(inode);
1832                 goto error_out;
1833         }
1834         sb->s_maxbytes = MAX_LFS_FILESIZE;
1835         return 0;
1836
1837 error_out:
1838         if (sbi->s_vat_inode)
1839                 iput(sbi->s_vat_inode);
1840         if (sbi->s_partitions) {
1841                 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1842                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1843                         iput(map->s_uspace.s_table);
1844                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1845                         iput(map->s_fspace.s_table);
1846                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1847                         udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1848                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1849                         udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1850                 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1851                         for (i = 0; i < 4; i++)
1852                                 brelse(map->s_type_specific.s_sparing.
1853                                                 s_spar_map[i]);
1854         }
1855 #ifdef CONFIG_UDF_NLS
1856         if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1857                 unload_nls(sbi->s_nls_map);
1858 #endif
1859         if (!(sb->s_flags & MS_RDONLY))
1860                 udf_close_lvid(sb);
1861         brelse(sbi->s_lvid_bh);
1862
1863         kfree(sbi->s_partmaps);
1864         kfree(sbi);
1865         sb->s_fs_info = NULL;
1866
1867         return -EINVAL;
1868 }
1869
1870 void udf_error(struct super_block *sb, const char *function,
1871                const char *fmt, ...)
1872 {
1873         va_list args;
1874
1875         if (!(sb->s_flags & MS_RDONLY)) {
1876                 /* mark sb error */
1877                 sb->s_dirt = 1;
1878         }
1879         va_start(args, fmt);
1880         vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1881         va_end(args);
1882         printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1883                 sb->s_id, function, error_buf);
1884 }
1885
1886 void udf_warning(struct super_block *sb, const char *function,
1887                  const char *fmt, ...)
1888 {
1889         va_list args;
1890
1891         va_start(args, fmt);
1892         vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1893         va_end(args);
1894         printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1895                sb->s_id, function, error_buf);
1896 }
1897
1898 static void udf_put_super(struct super_block *sb)
1899 {
1900         int i;
1901         struct udf_sb_info *sbi;
1902
1903         sbi = UDF_SB(sb);
1904         if (sbi->s_vat_inode)
1905                 iput(sbi->s_vat_inode);
1906         if (sbi->s_partitions) {
1907                 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1908                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1909                         iput(map->s_uspace.s_table);
1910                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1911                         iput(map->s_fspace.s_table);
1912                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1913                         udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1914                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1915                         udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1916                 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1917                         for (i = 0; i < 4; i++)
1918                                 brelse(map->s_type_specific.s_sparing.
1919                                                 s_spar_map[i]);
1920         }
1921 #ifdef CONFIG_UDF_NLS
1922         if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1923                 unload_nls(sbi->s_nls_map);
1924 #endif
1925         if (!(sb->s_flags & MS_RDONLY))
1926                 udf_close_lvid(sb);
1927         brelse(sbi->s_lvid_bh);
1928         kfree(sbi->s_partmaps);
1929         kfree(sb->s_fs_info);
1930         sb->s_fs_info = NULL;
1931 }
1932
1933 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1934 {
1935         struct super_block *sb = dentry->d_sb;
1936         struct udf_sb_info *sbi = UDF_SB(sb);
1937         struct logicalVolIntegrityDescImpUse *lvidiu;
1938
1939         if (sbi->s_lvid_bh != NULL)
1940                 lvidiu = udf_sb_lvidiu(sbi);
1941         else
1942                 lvidiu = NULL;
1943
1944         buf->f_type = UDF_SUPER_MAGIC;
1945         buf->f_bsize = sb->s_blocksize;
1946         buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1947         buf->f_bfree = udf_count_free(sb);
1948         buf->f_bavail = buf->f_bfree;
1949         buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1950                                           le32_to_cpu(lvidiu->numDirs)) : 0)
1951                         + buf->f_bfree;
1952         buf->f_ffree = buf->f_bfree;
1953         /* __kernel_fsid_t f_fsid */
1954         buf->f_namelen = UDF_NAME_LEN - 2;
1955
1956         return 0;
1957 }
1958
1959 static unsigned char udf_bitmap_lookup[16] = {
1960         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1961 };
1962
1963 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1964                                           struct udf_bitmap *bitmap)
1965 {
1966         struct buffer_head *bh = NULL;
1967         unsigned int accum = 0;
1968         int index;
1969         int block = 0, newblock;
1970         kernel_lb_addr loc;
1971         uint32_t bytes;
1972         uint8_t value;
1973         uint8_t *ptr;
1974         uint16_t ident;
1975         struct spaceBitmapDesc *bm;
1976
1977         lock_kernel();
1978
1979         loc.logicalBlockNum = bitmap->s_extPosition;
1980         loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1981         bh = udf_read_ptagged(sb, loc, 0, &ident);
1982
1983         if (!bh) {
1984                 printk(KERN_ERR "udf: udf_count_free failed\n");
1985                 goto out;
1986         } else if (ident != TAG_IDENT_SBD) {
1987                 brelse(bh);
1988                 printk(KERN_ERR "udf: udf_count_free failed\n");
1989                 goto out;
1990         }
1991
1992         bm = (struct spaceBitmapDesc *)bh->b_data;
1993         bytes = le32_to_cpu(bm->numOfBytes);
1994         index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1995         ptr = (uint8_t *)bh->b_data;
1996
1997         while (bytes > 0) {
1998                 while ((bytes > 0) && (index < sb->s_blocksize)) {
1999                         value = ptr[index];
2000                         accum += udf_bitmap_lookup[value & 0x0f];
2001                         accum += udf_bitmap_lookup[value >> 4];
2002                         index++;
2003                         bytes--;
2004                 }
2005                 if (bytes) {
2006                         brelse(bh);
2007                         newblock = udf_get_lb_pblock(sb, loc, ++block);
2008                         bh = udf_tread(sb, newblock);
2009                         if (!bh) {
2010                                 udf_debug("read failed\n");
2011                                 goto out;
2012                         }
2013                         index = 0;
2014                         ptr = (uint8_t *)bh->b_data;
2015                 }
2016         }
2017         brelse(bh);
2018
2019 out:
2020         unlock_kernel();
2021
2022         return accum;
2023 }
2024
2025 static unsigned int udf_count_free_table(struct super_block *sb,
2026                                          struct inode *table)
2027 {
2028         unsigned int accum = 0;
2029         uint32_t elen;
2030         kernel_lb_addr eloc;
2031         int8_t etype;
2032         struct extent_position epos;
2033
2034         lock_kernel();
2035
2036         epos.block = UDF_I(table)->i_location;
2037         epos.offset = sizeof(struct unallocSpaceEntry);
2038         epos.bh = NULL;
2039
2040         while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2041                 accum += (elen >> table->i_sb->s_blocksize_bits);
2042
2043         brelse(epos.bh);
2044
2045         unlock_kernel();
2046
2047         return accum;
2048 }
2049
2050 static unsigned int udf_count_free(struct super_block *sb)
2051 {
2052         unsigned int accum = 0;
2053         struct udf_sb_info *sbi;
2054         struct udf_part_map *map;
2055
2056         sbi = UDF_SB(sb);
2057         if (sbi->s_lvid_bh) {
2058                 struct logicalVolIntegrityDesc *lvid =
2059                         (struct logicalVolIntegrityDesc *)
2060                         sbi->s_lvid_bh->b_data;
2061                 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2062                         accum = le32_to_cpu(
2063                                         lvid->freeSpaceTable[sbi->s_partition]);
2064                         if (accum == 0xFFFFFFFF)
2065                                 accum = 0;
2066                 }
2067         }
2068
2069         if (accum)
2070                 return accum;
2071
2072         map = &sbi->s_partmaps[sbi->s_partition];
2073         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2074                 accum += udf_count_free_bitmap(sb,
2075                                                map->s_uspace.s_bitmap);
2076         }
2077         if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2078                 accum += udf_count_free_bitmap(sb,
2079                                                map->s_fspace.s_bitmap);
2080         }
2081         if (accum)
2082                 return accum;
2083
2084         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2085                 accum += udf_count_free_table(sb,
2086                                               map->s_uspace.s_table);
2087         }
2088         if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2089                 accum += udf_count_free_table(sb,
2090                                               map->s_fspace.s_table);
2091         }
2092
2093         return accum;
2094 }