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