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