udf: simple cleanup of truncate.c
[linux-2.6-block.git] / fs / udf / super.c
1 /*
2  * super.c
3  *
4  * PURPOSE
5  *  Super block routines for the OSTA-UDF(tm) filesystem.
6  *
7  * DESCRIPTION
8  *  OSTA-UDF(tm) = Optical Storage Technology Association
9  *  Universal Disk Format.
10  *
11  *  This code is based on version 2.00 of the UDF specification,
12  *  and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13  *    http://www.osta.org/
14  *    http://www.ecma.ch/
15  *    http://www.iso.org/
16  *
17  * COPYRIGHT
18  *  This file is distributed under the terms of the GNU General Public
19  *  License (GPL). Copies of the GPL can be obtained from:
20  *    ftp://prep.ai.mit.edu/pub/gnu/GPL
21  *  Each contributing author retains all rights to their own work.
22  *
23  *  (C) 1998 Dave Boynton
24  *  (C) 1998-2004 Ben Fennema
25  *  (C) 2000 Stelias Computing Inc
26  *
27  * HISTORY
28  *
29  *  09/24/98 dgb  changed to allow compiling outside of kernel, and
30  *                added some debugging.
31  *  10/01/98 dgb  updated to allow (some) possibility of compiling w/2.0.34
32  *  10/16/98      attempting some multi-session support
33  *  10/17/98      added freespace count for "df"
34  *  11/11/98 gr   added novrs option
35  *  11/26/98 dgb  added fileset,anchor mount options
36  *  12/06/98 blf  really hosed things royally. vat/sparing support. sequenced
37  *                vol descs. rewrote option handling based on isofs
38  *  12/20/98      find the free space bitmap (if it exists)
39  */
40
41 #include "udfdecl.h"
42
43 #include <linux/blkdev.h>
44 #include <linux/slab.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/parser.h>
48 #include <linux/stat.h>
49 #include <linux/cdrom.h>
50 #include <linux/nls.h>
51 #include <linux/smp_lock.h>
52 #include <linux/buffer_head.h>
53 #include <linux/vfs.h>
54 #include <linux/vmalloc.h>
55 #include <linux/errno.h>
56 #include <linux/mount.h>
57 #include <linux/seq_file.h>
58 #include <asm/byteorder.h>
59
60 #include "udf_sb.h"
61 #include "udf_i.h"
62
63 #include <linux/init.h>
64 #include <asm/uaccess.h>
65
66 #define VDS_POS_PRIMARY_VOL_DESC        0
67 #define VDS_POS_UNALLOC_SPACE_DESC      1
68 #define VDS_POS_LOGICAL_VOL_DESC        2
69 #define VDS_POS_PARTITION_DESC          3
70 #define VDS_POS_IMP_USE_VOL_DESC        4
71 #define VDS_POS_VOL_DESC_PTR            5
72 #define VDS_POS_TERMINATING_DESC        6
73 #define VDS_POS_LENGTH                  7
74
75 #define UDF_DEFAULT_BLOCKSIZE 2048
76
77 static char error_buf[1024];
78
79 /* These are the "meat" - everything else is stuffing */
80 static int udf_fill_super(struct super_block *, void *, int);
81 static void udf_put_super(struct super_block *);
82 static void udf_write_super(struct super_block *);
83 static int udf_remount_fs(struct super_block *, int *, char *);
84 static int udf_check_valid(struct super_block *, int, int);
85 static int udf_vrs(struct super_block *sb, int silent);
86 static int udf_load_partition(struct super_block *, kernel_lb_addr *);
87 static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
88                                kernel_lb_addr *);
89 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
90 static void udf_find_anchor(struct super_block *);
91 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
92                             kernel_lb_addr *);
93 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
94 static void udf_load_fileset(struct super_block *, struct buffer_head *,
95                              kernel_lb_addr *);
96 static int udf_load_partdesc(struct super_block *, struct buffer_head *);
97 static void udf_open_lvid(struct super_block *);
98 static void udf_close_lvid(struct super_block *);
99 static unsigned int udf_count_free(struct super_block *);
100 static int udf_statfs(struct dentry *, struct kstatfs *);
101 static int udf_show_options(struct seq_file *, struct vfsmount *);
102 static void udf_error(struct super_block *sb, const char *function,
103                       const char *fmt, ...);
104
105 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
106 {
107         struct logicalVolIntegrityDesc *lvid =
108                 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
109         __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
110         __u32 offset = number_of_partitions * 2 *
111                                 sizeof(uint32_t)/sizeof(uint8_t);
112         return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
113 }
114
115 /* UDF filesystem type */
116 static int udf_get_sb(struct file_system_type *fs_type,
117                       int flags, const char *dev_name, void *data,
118                       struct vfsmount *mnt)
119 {
120         return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
121 }
122
123 static struct file_system_type udf_fstype = {
124         .owner          = THIS_MODULE,
125         .name           = "udf",
126         .get_sb         = udf_get_sb,
127         .kill_sb        = kill_block_super,
128         .fs_flags       = FS_REQUIRES_DEV,
129 };
130
131 static struct kmem_cache *udf_inode_cachep;
132
133 static struct inode *udf_alloc_inode(struct super_block *sb)
134 {
135         struct udf_inode_info *ei;
136         ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
137         if (!ei)
138                 return NULL;
139
140         ei->i_unique = 0;
141         ei->i_lenExtents = 0;
142         ei->i_next_alloc_block = 0;
143         ei->i_next_alloc_goal = 0;
144         ei->i_strat4096 = 0;
145
146         return &ei->vfs_inode;
147 }
148
149 static void udf_destroy_inode(struct inode *inode)
150 {
151         kmem_cache_free(udf_inode_cachep, UDF_I(inode));
152 }
153
154 static void init_once(struct kmem_cache *cachep, void *foo)
155 {
156         struct udf_inode_info *ei = (struct udf_inode_info *)foo;
157
158         ei->i_ext.i_data = NULL;
159         inode_init_once(&ei->vfs_inode);
160 }
161
162 static int init_inodecache(void)
163 {
164         udf_inode_cachep = kmem_cache_create("udf_inode_cache",
165                                              sizeof(struct udf_inode_info),
166                                              0, (SLAB_RECLAIM_ACCOUNT |
167                                                  SLAB_MEM_SPREAD),
168                                              init_once);
169         if (!udf_inode_cachep)
170                 return -ENOMEM;
171         return 0;
172 }
173
174 static void destroy_inodecache(void)
175 {
176         kmem_cache_destroy(udf_inode_cachep);
177 }
178
179 /* Superblock operations */
180 static const struct super_operations udf_sb_ops = {
181         .alloc_inode    = udf_alloc_inode,
182         .destroy_inode  = udf_destroy_inode,
183         .write_inode    = udf_write_inode,
184         .delete_inode   = udf_delete_inode,
185         .clear_inode    = udf_clear_inode,
186         .put_super      = udf_put_super,
187         .write_super    = udf_write_super,
188         .statfs         = udf_statfs,
189         .remount_fs     = udf_remount_fs,
190         .show_options   = udf_show_options,
191 };
192
193 struct udf_options {
194         unsigned char novrs;
195         unsigned int blocksize;
196         unsigned int session;
197         unsigned int lastblock;
198         unsigned int anchor;
199         unsigned int volume;
200         unsigned short partition;
201         unsigned int fileset;
202         unsigned int rootdir;
203         unsigned int flags;
204         mode_t umask;
205         gid_t gid;
206         uid_t uid;
207         struct nls_table *nls_map;
208 };
209
210 static int __init init_udf_fs(void)
211 {
212         int err;
213
214         err = init_inodecache();
215         if (err)
216                 goto out1;
217         err = register_filesystem(&udf_fstype);
218         if (err)
219                 goto out;
220
221         return 0;
222
223 out:
224         destroy_inodecache();
225
226 out1:
227         return err;
228 }
229
230 static void __exit exit_udf_fs(void)
231 {
232         unregister_filesystem(&udf_fstype);
233         destroy_inodecache();
234 }
235
236 module_init(init_udf_fs)
237 module_exit(exit_udf_fs)
238
239 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
240 {
241         struct udf_sb_info *sbi = UDF_SB(sb);
242
243         sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
244                                   GFP_KERNEL);
245         if (!sbi->s_partmaps) {
246                 udf_error(sb, __FUNCTION__,
247                           "Unable to allocate space for %d partition maps",
248                           count);
249                 sbi->s_partitions = 0;
250                 return -ENOMEM;
251         }
252
253         sbi->s_partitions = count;
254         return 0;
255 }
256
257 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
258 {
259         struct super_block *sb = mnt->mnt_sb;
260         struct udf_sb_info *sbi = UDF_SB(sb);
261
262         if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
263                 seq_puts(seq, ",nostrict");
264         if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
265                 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
266         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
267                 seq_puts(seq, ",unhide");
268         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
269                 seq_puts(seq, ",undelete");
270         if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
271                 seq_puts(seq, ",noadinicb");
272         if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
273                 seq_puts(seq, ",shortad");
274         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
275                 seq_puts(seq, ",uid=forget");
276         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
277                 seq_puts(seq, ",uid=ignore");
278         if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
279                 seq_puts(seq, ",gid=forget");
280         if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
281                 seq_puts(seq, ",gid=ignore");
282         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
283                 seq_printf(seq, ",uid=%u", sbi->s_uid);
284         if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
285                 seq_printf(seq, ",gid=%u", sbi->s_gid);
286         if (sbi->s_umask != 0)
287                 seq_printf(seq, ",umask=%o", sbi->s_umask);
288         if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
289                 seq_printf(seq, ",session=%u", sbi->s_session);
290         if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
291                 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
292         /*
293          * s_anchor[2] could be zeroed out in case there is no anchor
294          * in the specified block, but then the "anchor=N" option
295          * originally given by the user wasn't effective, so it's OK
296          * if we don't show it.
297          */
298         if (sbi->s_anchor[2] != 0)
299                 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
300         /*
301          * volume, partition, fileset and rootdir seem to be ignored
302          * currently
303          */
304         if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
305                 seq_puts(seq, ",utf8");
306         if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
307                 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
308
309         return 0;
310 }
311
312 /*
313  * udf_parse_options
314  *
315  * PURPOSE
316  *      Parse mount options.
317  *
318  * DESCRIPTION
319  *      The following mount options are supported:
320  *
321  *      gid=            Set the default group.
322  *      umask=          Set the default umask.
323  *      uid=            Set the default user.
324  *      bs=             Set the block size.
325  *      unhide          Show otherwise hidden files.
326  *      undelete        Show deleted files in lists.
327  *      adinicb         Embed data in the inode (default)
328  *      noadinicb       Don't embed data in the inode
329  *      shortad         Use short ad's
330  *      longad          Use long ad's (default)
331  *      nostrict        Unset strict conformance
332  *      iocharset=      Set the NLS character set
333  *
334  *      The remaining are for debugging and disaster recovery:
335  *
336  *      novrs           Skip volume sequence recognition
337  *
338  *      The following expect a offset from 0.
339  *
340  *      session=        Set the CDROM session (default= last session)
341  *      anchor=         Override standard anchor location. (default= 256)
342  *      volume=         Override the VolumeDesc location. (unused)
343  *      partition=      Override the PartitionDesc location. (unused)
344  *      lastblock=      Set the last block of the filesystem/
345  *
346  *      The following expect a offset from the partition root.
347  *
348  *      fileset=        Override the fileset block location. (unused)
349  *      rootdir=        Override the root directory location. (unused)
350  *              WARNING: overriding the rootdir to a non-directory may
351  *              yield highly unpredictable results.
352  *
353  * PRE-CONDITIONS
354  *      options         Pointer to mount options string.
355  *      uopts           Pointer to mount options variable.
356  *
357  * POST-CONDITIONS
358  *      <return>        1       Mount options parsed okay.
359  *      <return>        0       Error parsing mount options.
360  *
361  * HISTORY
362  *      July 1, 1997 - Andrew E. Mileski
363  *      Written, tested, and released.
364  */
365
366 enum {
367         Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
368         Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
369         Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
370         Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
371         Opt_rootdir, Opt_utf8, Opt_iocharset,
372         Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
373 };
374
375 static match_table_t tokens = {
376         {Opt_novrs,     "novrs"},
377         {Opt_nostrict,  "nostrict"},
378         {Opt_bs,        "bs=%u"},
379         {Opt_unhide,    "unhide"},
380         {Opt_undelete,  "undelete"},
381         {Opt_noadinicb, "noadinicb"},
382         {Opt_adinicb,   "adinicb"},
383         {Opt_shortad,   "shortad"},
384         {Opt_longad,    "longad"},
385         {Opt_uforget,   "uid=forget"},
386         {Opt_uignore,   "uid=ignore"},
387         {Opt_gforget,   "gid=forget"},
388         {Opt_gignore,   "gid=ignore"},
389         {Opt_gid,       "gid=%u"},
390         {Opt_uid,       "uid=%u"},
391         {Opt_umask,     "umask=%o"},
392         {Opt_session,   "session=%u"},
393         {Opt_lastblock, "lastblock=%u"},
394         {Opt_anchor,    "anchor=%u"},
395         {Opt_volume,    "volume=%u"},
396         {Opt_partition, "partition=%u"},
397         {Opt_fileset,   "fileset=%u"},
398         {Opt_rootdir,   "rootdir=%u"},
399         {Opt_utf8,      "utf8"},
400         {Opt_iocharset, "iocharset=%s"},
401         {Opt_err,       NULL}
402 };
403
404 static int udf_parse_options(char *options, struct udf_options *uopt,
405                              bool remount)
406 {
407         char *p;
408         int option;
409
410         uopt->novrs = 0;
411         uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
412         uopt->partition = 0xFFFF;
413         uopt->session = 0xFFFFFFFF;
414         uopt->lastblock = 0;
415         uopt->anchor = 0;
416         uopt->volume = 0xFFFFFFFF;
417         uopt->rootdir = 0xFFFFFFFF;
418         uopt->fileset = 0xFFFFFFFF;
419         uopt->nls_map = NULL;
420
421         if (!options)
422                 return 1;
423
424         while ((p = strsep(&options, ",")) != NULL) {
425                 substring_t args[MAX_OPT_ARGS];
426                 int token;
427                 if (!*p)
428                         continue;
429
430                 token = match_token(p, tokens, args);
431                 switch (token) {
432                 case Opt_novrs:
433                         uopt->novrs = 1;
434                 case Opt_bs:
435                         if (match_int(&args[0], &option))
436                                 return 0;
437                         uopt->blocksize = option;
438                         break;
439                 case Opt_unhide:
440                         uopt->flags |= (1 << UDF_FLAG_UNHIDE);
441                         break;
442                 case Opt_undelete:
443                         uopt->flags |= (1 << UDF_FLAG_UNDELETE);
444                         break;
445                 case Opt_noadinicb:
446                         uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
447                         break;
448                 case Opt_adinicb:
449                         uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
450                         break;
451                 case Opt_shortad:
452                         uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
453                         break;
454                 case Opt_longad:
455                         uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
456                         break;
457                 case Opt_gid:
458                         if (match_int(args, &option))
459                                 return 0;
460                         uopt->gid = option;
461                         uopt->flags |= (1 << UDF_FLAG_GID_SET);
462                         break;
463                 case Opt_uid:
464                         if (match_int(args, &option))
465                                 return 0;
466                         uopt->uid = option;
467                         uopt->flags |= (1 << UDF_FLAG_UID_SET);
468                         break;
469                 case Opt_umask:
470                         if (match_octal(args, &option))
471                                 return 0;
472                         uopt->umask = option;
473                         break;
474                 case Opt_nostrict:
475                         uopt->flags &= ~(1 << UDF_FLAG_STRICT);
476                         break;
477                 case Opt_session:
478                         if (match_int(args, &option))
479                                 return 0;
480                         uopt->session = option;
481                         if (!remount)
482                                 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
483                         break;
484                 case Opt_lastblock:
485                         if (match_int(args, &option))
486                                 return 0;
487                         uopt->lastblock = option;
488                         if (!remount)
489                                 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
490                         break;
491                 case Opt_anchor:
492                         if (match_int(args, &option))
493                                 return 0;
494                         uopt->anchor = option;
495                         break;
496                 case Opt_volume:
497                         if (match_int(args, &option))
498                                 return 0;
499                         uopt->volume = option;
500                         break;
501                 case Opt_partition:
502                         if (match_int(args, &option))
503                                 return 0;
504                         uopt->partition = option;
505                         break;
506                 case Opt_fileset:
507                         if (match_int(args, &option))
508                                 return 0;
509                         uopt->fileset = option;
510                         break;
511                 case Opt_rootdir:
512                         if (match_int(args, &option))
513                                 return 0;
514                         uopt->rootdir = option;
515                         break;
516                 case Opt_utf8:
517                         uopt->flags |= (1 << UDF_FLAG_UTF8);
518                         break;
519 #ifdef CONFIG_UDF_NLS
520                 case Opt_iocharset:
521                         uopt->nls_map = load_nls(args[0].from);
522                         uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
523                         break;
524 #endif
525                 case Opt_uignore:
526                         uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
527                         break;
528                 case Opt_uforget:
529                         uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
530                         break;
531                 case Opt_gignore:
532                         uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
533                         break;
534                 case Opt_gforget:
535                         uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
536                         break;
537                 default:
538                         printk(KERN_ERR "udf: bad mount option \"%s\" "
539                                "or missing value\n", p);
540                         return 0;
541                 }
542         }
543         return 1;
544 }
545
546 static void udf_write_super(struct super_block *sb)
547 {
548         lock_kernel();
549
550         if (!(sb->s_flags & MS_RDONLY))
551                 udf_open_lvid(sb);
552         sb->s_dirt = 0;
553
554         unlock_kernel();
555 }
556
557 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
558 {
559         struct udf_options uopt;
560         struct udf_sb_info *sbi = UDF_SB(sb);
561
562         uopt.flags = sbi->s_flags;
563         uopt.uid   = sbi->s_uid;
564         uopt.gid   = sbi->s_gid;
565         uopt.umask = sbi->s_umask;
566
567         if (!udf_parse_options(options, &uopt, true))
568                 return -EINVAL;
569
570         sbi->s_flags = uopt.flags;
571         sbi->s_uid   = uopt.uid;
572         sbi->s_gid   = uopt.gid;
573         sbi->s_umask = uopt.umask;
574
575         if (sbi->s_lvid_bh) {
576                 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
577                 if (write_rev > UDF_MAX_WRITE_VERSION)
578                         *flags |= MS_RDONLY;
579         }
580
581         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
582                 return 0;
583         if (*flags & MS_RDONLY)
584                 udf_close_lvid(sb);
585         else
586                 udf_open_lvid(sb);
587
588         return 0;
589 }
590
591 static int udf_vrs(struct super_block *sb, int silent)
592 {
593         struct volStructDesc *vsd = NULL;
594         int sector = 32768;
595         int sectorsize;
596         struct buffer_head *bh = NULL;
597         int iso9660 = 0;
598         int nsr02 = 0;
599         int nsr03 = 0;
600         struct udf_sb_info *sbi;
601
602         /* Block size must be a multiple of 512 */
603         if (sb->s_blocksize & 511)
604                 return 0;
605         sbi = UDF_SB(sb);
606
607         if (sb->s_blocksize < sizeof(struct volStructDesc))
608                 sectorsize = sizeof(struct volStructDesc);
609         else
610                 sectorsize = sb->s_blocksize;
611
612         sector += (sbi->s_session << sb->s_blocksize_bits);
613
614         udf_debug("Starting at sector %u (%ld byte sectors)\n",
615                   (sector >> sb->s_blocksize_bits), sb->s_blocksize);
616         /* Process the sequence (if applicable) */
617         for (; !nsr02 && !nsr03; sector += sectorsize) {
618                 /* Read a block */
619                 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
620                 if (!bh)
621                         break;
622
623                 /* Look for ISO  descriptors */
624                 vsd = (struct volStructDesc *)(bh->b_data +
625                                               (sector & (sb->s_blocksize - 1)));
626
627                 if (vsd->stdIdent[0] == 0) {
628                         brelse(bh);
629                         break;
630                 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
631                                     VSD_STD_ID_LEN)) {
632                         iso9660 = sector;
633                         switch (vsd->structType) {
634                         case 0:
635                                 udf_debug("ISO9660 Boot Record found\n");
636                                 break;
637                         case 1:
638                                 udf_debug("ISO9660 Primary Volume Descriptor "
639                                           "found\n");
640                                 break;
641                         case 2:
642                                 udf_debug("ISO9660 Supplementary Volume "
643                                           "Descriptor found\n");
644                                 break;
645                         case 3:
646                                 udf_debug("ISO9660 Volume Partition Descriptor "
647                                           "found\n");
648                                 break;
649                         case 255:
650                                 udf_debug("ISO9660 Volume Descriptor Set "
651                                           "Terminator found\n");
652                                 break;
653                         default:
654                                 udf_debug("ISO9660 VRS (%u) found\n",
655                                           vsd->structType);
656                                 break;
657                         }
658                 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
659                                     VSD_STD_ID_LEN))
660                         ; /* nothing */
661                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
662                                     VSD_STD_ID_LEN)) {
663                         brelse(bh);
664                         break;
665                 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
666                                     VSD_STD_ID_LEN))
667                         nsr02 = sector;
668                 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
669                                     VSD_STD_ID_LEN))
670                         nsr03 = sector;
671                 brelse(bh);
672         }
673
674         if (nsr03)
675                 return nsr03;
676         else if (nsr02)
677                 return nsr02;
678         else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
679                 return -1;
680         else
681                 return 0;
682 }
683
684 /*
685  * udf_find_anchor
686  *
687  * PURPOSE
688  *      Find an anchor volume descriptor.
689  *
690  * PRE-CONDITIONS
691  *      sb                      Pointer to _locked_ superblock.
692  *      lastblock               Last block on media.
693  *
694  * POST-CONDITIONS
695  *      <return>                1 if not found, 0 if ok
696  *
697  * HISTORY
698  *      July 1, 1997 - Andrew E. Mileski
699  *      Written, tested, and released.
700  */
701 static void udf_find_anchor(struct super_block *sb)
702 {
703         int lastblock;
704         struct buffer_head *bh = NULL;
705         uint16_t ident;
706         uint32_t location;
707         int i;
708         struct udf_sb_info *sbi;
709
710         sbi = UDF_SB(sb);
711         lastblock = sbi->s_last_block;
712
713         if (lastblock) {
714                 int varlastblock = udf_variable_to_fixed(lastblock);
715                 int last[] =  { lastblock, lastblock - 2,
716                                 lastblock - 150, lastblock - 152,
717                                 varlastblock, varlastblock - 2,
718                                 varlastblock - 150, varlastblock - 152 };
719
720                 lastblock = 0;
721
722                 /* Search for an anchor volume descriptor pointer */
723
724                 /*  according to spec, anchor is in either:
725                  *     block 256
726                  *     lastblock-256
727                  *     lastblock
728                  *  however, if the disc isn't closed, it could be 512 */
729
730                 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
731                         ident = location = 0;
732                         if (last[i] >= 0) {
733                                 bh = sb_bread(sb, last[i]);
734                                 if (bh) {
735                                         tag *t = (tag *)bh->b_data;
736                                         ident = le16_to_cpu(t->tagIdent);
737                                         location = le32_to_cpu(t->tagLocation);
738                                         brelse(bh);
739                                 }
740                         }
741
742                         if (ident == TAG_IDENT_AVDP) {
743                                 if (location == last[i] - sbi->s_session) {
744                                         lastblock = last[i] - sbi->s_session;
745                                         sbi->s_anchor[0] = lastblock;
746                                         sbi->s_anchor[1] = lastblock - 256;
747                                 } else if (location ==
748                                                 udf_variable_to_fixed(last[i]) -
749                                                         sbi->s_session) {
750                                         UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
751                                         lastblock =
752                                                 udf_variable_to_fixed(last[i]) -
753                                                         sbi->s_session;
754                                         sbi->s_anchor[0] = lastblock;
755                                         sbi->s_anchor[1] = lastblock - 256 -
756                                                                 sbi->s_session;
757                                 } else {
758                                         udf_debug("Anchor found at block %d, "
759                                                   "location mismatch %d.\n",
760                                                   last[i], location);
761                                 }
762                         } else if (ident == TAG_IDENT_FE ||
763                                         ident == TAG_IDENT_EFE) {
764                                 lastblock = last[i];
765                                 sbi->s_anchor[3] = 512;
766                         } else {
767                                 ident = location = 0;
768                                 if (last[i] >= 256) {
769                                         bh = sb_bread(sb, last[i] - 256);
770                                         if (bh) {
771                                                 tag *t = (tag *)bh->b_data;
772                                                 ident = le16_to_cpu(
773                                                                 t->tagIdent);
774                                                 location = le32_to_cpu(
775                                                                 t->tagLocation);
776                                                 brelse(bh);
777                                         }
778                                 }
779
780                                 if (ident == TAG_IDENT_AVDP &&
781                                     location == last[i] - 256 -
782                                                 sbi->s_session) {
783                                         lastblock = last[i];
784                                         sbi->s_anchor[1] = last[i] - 256;
785                                 } else {
786                                         ident = location = 0;
787                                         if (last[i] >= 312 + sbi->s_session) {
788                                                 bh = sb_bread(sb,
789                                                                 last[i] - 312 -
790                                                                 sbi->s_session);
791                                                 if (bh) {
792                                                         tag *t = (tag *)
793                                                                  bh->b_data;
794                                                         ident = le16_to_cpu(
795                                                                 t->tagIdent);
796                                                         location = le32_to_cpu(
797                                                                 t->tagLocation);
798                                                         brelse(bh);
799                                                 }
800                                         }
801
802                                         if (ident == TAG_IDENT_AVDP &&
803                                             location == udf_variable_to_fixed(last[i]) - 256) {
804                                                 UDF_SET_FLAG(sb,
805                                                              UDF_FLAG_VARCONV);
806                                                 lastblock = udf_variable_to_fixed(last[i]);
807                                                 sbi->s_anchor[1] = lastblock - 256;
808                                         }
809                                 }
810                         }
811                 }
812         }
813
814         if (!lastblock) {
815                 /* We haven't found the lastblock. check 312 */
816                 bh = sb_bread(sb, 312 + sbi->s_session);
817                 if (bh) {
818                         tag *t = (tag *)bh->b_data;
819                         ident = le16_to_cpu(t->tagIdent);
820                         location = le32_to_cpu(t->tagLocation);
821                         brelse(bh);
822
823                         if (ident == TAG_IDENT_AVDP && location == 256)
824                                 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
825                 }
826         }
827
828         for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
829                 if (sbi->s_anchor[i]) {
830                         bh = udf_read_tagged(sb, sbi->s_anchor[i],
831                                              sbi->s_anchor[i], &ident);
832                         if (!bh)
833                                 sbi->s_anchor[i] = 0;
834                         else {
835                                 brelse(bh);
836                                 if ((ident != TAG_IDENT_AVDP) &&
837                                     (i || (ident != TAG_IDENT_FE &&
838                                            ident != TAG_IDENT_EFE)))
839                                         sbi->s_anchor[i] = 0;
840                         }
841                 }
842         }
843
844         sbi->s_last_block = lastblock;
845 }
846
847 static int udf_find_fileset(struct super_block *sb,
848                             kernel_lb_addr *fileset,
849                             kernel_lb_addr *root)
850 {
851         struct buffer_head *bh = NULL;
852         long lastblock;
853         uint16_t ident;
854         struct udf_sb_info *sbi;
855
856         if (fileset->logicalBlockNum != 0xFFFFFFFF ||
857             fileset->partitionReferenceNum != 0xFFFF) {
858                 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
859
860                 if (!bh) {
861                         return 1;
862                 } else if (ident != TAG_IDENT_FSD) {
863                         brelse(bh);
864                         return 1;
865                 }
866
867         }
868
869         sbi = UDF_SB(sb);
870         if (!bh) {
871                 /* Search backwards through the partitions */
872                 kernel_lb_addr newfileset;
873
874 /* --> cvg: FIXME - is it reasonable? */
875                 return 1;
876
877                 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
878                      (newfileset.partitionReferenceNum != 0xFFFF &&
879                       fileset->logicalBlockNum == 0xFFFFFFFF &&
880                       fileset->partitionReferenceNum == 0xFFFF);
881                      newfileset.partitionReferenceNum--) {
882                         lastblock = sbi->s_partmaps
883                                         [newfileset.partitionReferenceNum]
884                                                 .s_partition_len;
885                         newfileset.logicalBlockNum = 0;
886
887                         do {
888                                 bh = udf_read_ptagged(sb, newfileset, 0,
889                                                       &ident);
890                                 if (!bh) {
891                                         newfileset.logicalBlockNum++;
892                                         continue;
893                                 }
894
895                                 switch (ident) {
896                                 case TAG_IDENT_SBD:
897                                 {
898                                         struct spaceBitmapDesc *sp;
899                                         sp = (struct spaceBitmapDesc *)
900                                                                 bh->b_data;
901                                         newfileset.logicalBlockNum += 1 +
902                                                 ((le32_to_cpu(sp->numOfBytes) +
903                                                   sizeof(struct spaceBitmapDesc)
904                                                   - 1) >> sb->s_blocksize_bits);
905                                         brelse(bh);
906                                         break;
907                                 }
908                                 case TAG_IDENT_FSD:
909                                         *fileset = newfileset;
910                                         break;
911                                 default:
912                                         newfileset.logicalBlockNum++;
913                                         brelse(bh);
914                                         bh = NULL;
915                                         break;
916                                 }
917                         } while (newfileset.logicalBlockNum < lastblock &&
918                                  fileset->logicalBlockNum == 0xFFFFFFFF &&
919                                  fileset->partitionReferenceNum == 0xFFFF);
920                 }
921         }
922
923         if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
924              fileset->partitionReferenceNum != 0xFFFF) && bh) {
925                 udf_debug("Fileset at block=%d, partition=%d\n",
926                           fileset->logicalBlockNum,
927                           fileset->partitionReferenceNum);
928
929                 sbi->s_partition = fileset->partitionReferenceNum;
930                 udf_load_fileset(sb, bh, root);
931                 brelse(bh);
932                 return 0;
933         }
934         return 1;
935 }
936
937 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
938 {
939         struct primaryVolDesc *pvoldesc;
940         time_t recording;
941         long recording_usec;
942         struct ustr instr;
943         struct ustr outstr;
944
945         pvoldesc = (struct primaryVolDesc *)bh->b_data;
946
947         if (udf_stamp_to_time(&recording, &recording_usec,
948                               lets_to_cpu(pvoldesc->recordingDateAndTime))) {
949                 kernel_timestamp ts;
950                 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
951                 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
952                           " %02u:%02u (%x)\n",
953                           recording, recording_usec,
954                           ts.year, ts.month, ts.day, ts.hour,
955                           ts.minute, ts.typeAndTimezone);
956                 UDF_SB(sb)->s_record_time.tv_sec = recording;
957                 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
958         }
959
960         if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
961                 if (udf_CS0toUTF8(&outstr, &instr)) {
962                         strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
963                                 outstr.u_len > 31 ? 31 : outstr.u_len);
964                         udf_debug("volIdent[] = '%s'\n",
965                                         UDF_SB(sb)->s_volume_ident);
966                 }
967
968         if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
969                 if (udf_CS0toUTF8(&outstr, &instr))
970                         udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
971 }
972
973 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
974                              kernel_lb_addr *root)
975 {
976         struct fileSetDesc *fset;
977
978         fset = (struct fileSetDesc *)bh->b_data;
979
980         *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
981
982         UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
983
984         udf_debug("Rootdir at block=%d, partition=%d\n",
985                   root->logicalBlockNum, root->partitionReferenceNum);
986 }
987
988 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
989 {
990         struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
991         return DIV_ROUND_UP(map->s_partition_len +
992                             (sizeof(struct spaceBitmapDesc) << 3),
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 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1674 {
1675         int i;
1676         struct inode *inode = NULL;
1677         struct udf_options uopt;
1678         kernel_lb_addr rootdir, fileset;
1679         struct udf_sb_info *sbi;
1680
1681         uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1682         uopt.uid = -1;
1683         uopt.gid = -1;
1684         uopt.umask = 0;
1685
1686         sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1687         if (!sbi)
1688                 return -ENOMEM;
1689
1690         sb->s_fs_info = sbi;
1691
1692         mutex_init(&sbi->s_alloc_mutex);
1693
1694         if (!udf_parse_options((char *)options, &uopt, false))
1695                 goto error_out;
1696
1697         if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1698             uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1699                 udf_error(sb, "udf_read_super",
1700                           "utf8 cannot be combined with iocharset\n");
1701                 goto error_out;
1702         }
1703 #ifdef CONFIG_UDF_NLS
1704         if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1705                 uopt.nls_map = load_nls_default();
1706                 if (!uopt.nls_map)
1707                         uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1708                 else
1709                         udf_debug("Using default NLS map\n");
1710         }
1711 #endif
1712         if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1713                 uopt.flags |= (1 << UDF_FLAG_UTF8);
1714
1715         fileset.logicalBlockNum = 0xFFFFFFFF;
1716         fileset.partitionReferenceNum = 0xFFFF;
1717
1718         sbi->s_flags = uopt.flags;
1719         sbi->s_uid = uopt.uid;
1720         sbi->s_gid = uopt.gid;
1721         sbi->s_umask = uopt.umask;
1722         sbi->s_nls_map = uopt.nls_map;
1723
1724         /* Set the block size for all transfers */
1725         if (!sb_min_blocksize(sb, uopt.blocksize)) {
1726                 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1727                 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1728                 goto error_out;
1729         }
1730
1731         if (uopt.session == 0xFFFFFFFF)
1732                 sbi->s_session = udf_get_last_session(sb);
1733         else
1734                 sbi->s_session = uopt.session;
1735
1736         udf_debug("Multi-session=%d\n", sbi->s_session);
1737
1738         sbi->s_last_block = uopt.lastblock;
1739         sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1740         sbi->s_anchor[2] = uopt.anchor;
1741         sbi->s_anchor[3] = 256;
1742
1743         if (udf_check_valid(sb, uopt.novrs, silent)) {
1744                 /* read volume recognition sequences */
1745                 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1746                 goto error_out;
1747         }
1748
1749         udf_find_anchor(sb);
1750
1751         /* Fill in the rest of the superblock */
1752         sb->s_op = &udf_sb_ops;
1753         sb->dq_op = NULL;
1754         sb->s_dirt = 0;
1755         sb->s_magic = UDF_SUPER_MAGIC;
1756         sb->s_time_gran = 1000;
1757
1758         if (udf_load_partition(sb, &fileset)) {
1759                 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1760                 goto error_out;
1761         }
1762
1763         udf_debug("Lastblock=%d\n", sbi->s_last_block);
1764
1765         if (sbi->s_lvid_bh) {
1766                 struct logicalVolIntegrityDescImpUse *lvidiu =
1767                                                         udf_sb_lvidiu(sbi);
1768                 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1769                 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1770                 /* uint16_t maxUDFWriteRev =
1771                                 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1772
1773                 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1774                         printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1775                                         "(max is %x)\n",
1776                                le16_to_cpu(lvidiu->minUDFReadRev),
1777                                UDF_MAX_READ_VERSION);
1778                         goto error_out;
1779                 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1780                         sb->s_flags |= MS_RDONLY;
1781
1782                 sbi->s_udfrev = minUDFWriteRev;
1783
1784                 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1785                         UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1786                 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1787                         UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1788         }
1789
1790         if (!sbi->s_partitions) {
1791                 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1792                 goto error_out;
1793         }
1794
1795         if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1796                         UDF_PART_FLAG_READ_ONLY) {
1797                 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1798                                    "forcing readonly mount\n");
1799                 sb->s_flags |= MS_RDONLY;
1800         }
1801
1802         if (udf_find_fileset(sb, &fileset, &rootdir)) {
1803                 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1804                 goto error_out;
1805         }
1806
1807         if (!silent) {
1808                 kernel_timestamp ts;
1809                 udf_time_to_stamp(&ts, sbi->s_record_time);
1810                 udf_info("UDF: Mounting volume '%s', "
1811                          "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1812                          sbi->s_volume_ident, ts.year, ts.month, ts.day,
1813                          ts.hour, ts.minute, ts.typeAndTimezone);
1814         }
1815         if (!(sb->s_flags & MS_RDONLY))
1816                 udf_open_lvid(sb);
1817
1818         /* Assign the root inode */
1819         /* assign inodes by physical block number */
1820         /* perhaps it's not extensible enough, but for now ... */
1821         inode = udf_iget(sb, rootdir);
1822         if (!inode) {
1823                 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1824                                 "partition=%d\n",
1825                        rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1826                 goto error_out;
1827         }
1828
1829         /* Allocate a dentry for the root inode */
1830         sb->s_root = d_alloc_root(inode);
1831         if (!sb->s_root) {
1832                 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1833                 iput(inode);
1834                 goto error_out;
1835         }
1836         sb->s_maxbytes = MAX_LFS_FILESIZE;
1837         return 0;
1838
1839 error_out:
1840         if (sbi->s_vat_inode)
1841                 iput(sbi->s_vat_inode);
1842         if (sbi->s_partitions) {
1843                 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1844                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1845                         iput(map->s_uspace.s_table);
1846                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1847                         iput(map->s_fspace.s_table);
1848                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1849                         udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1850                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1851                         udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1852                 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1853                         for (i = 0; i < 4; i++)
1854                                 brelse(map->s_type_specific.s_sparing.
1855                                                 s_spar_map[i]);
1856         }
1857 #ifdef CONFIG_UDF_NLS
1858         if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1859                 unload_nls(sbi->s_nls_map);
1860 #endif
1861         if (!(sb->s_flags & MS_RDONLY))
1862                 udf_close_lvid(sb);
1863         brelse(sbi->s_lvid_bh);
1864
1865         kfree(sbi->s_partmaps);
1866         kfree(sbi);
1867         sb->s_fs_info = NULL;
1868
1869         return -EINVAL;
1870 }
1871
1872 static void udf_error(struct super_block *sb, const char *function,
1873                       const char *fmt, ...)
1874 {
1875         va_list args;
1876
1877         if (!(sb->s_flags & MS_RDONLY)) {
1878                 /* mark sb error */
1879                 sb->s_dirt = 1;
1880         }
1881         va_start(args, fmt);
1882         vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1883         va_end(args);
1884         printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1885                 sb->s_id, function, error_buf);
1886 }
1887
1888 void udf_warning(struct super_block *sb, const char *function,
1889                  const char *fmt, ...)
1890 {
1891         va_list args;
1892
1893         va_start(args, fmt);
1894         vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1895         va_end(args);
1896         printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1897                sb->s_id, function, error_buf);
1898 }
1899
1900 static void udf_put_super(struct super_block *sb)
1901 {
1902         int i;
1903         struct udf_sb_info *sbi;
1904
1905         sbi = UDF_SB(sb);
1906         if (sbi->s_vat_inode)
1907                 iput(sbi->s_vat_inode);
1908         if (sbi->s_partitions) {
1909                 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1910                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1911                         iput(map->s_uspace.s_table);
1912                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1913                         iput(map->s_fspace.s_table);
1914                 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1915                         udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1916                 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1917                         udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1918                 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1919                         for (i = 0; i < 4; i++)
1920                                 brelse(map->s_type_specific.s_sparing.
1921                                                 s_spar_map[i]);
1922         }
1923 #ifdef CONFIG_UDF_NLS
1924         if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1925                 unload_nls(sbi->s_nls_map);
1926 #endif
1927         if (!(sb->s_flags & MS_RDONLY))
1928                 udf_close_lvid(sb);
1929         brelse(sbi->s_lvid_bh);
1930         kfree(sbi->s_partmaps);
1931         kfree(sb->s_fs_info);
1932         sb->s_fs_info = NULL;
1933 }
1934
1935 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1936 {
1937         struct super_block *sb = dentry->d_sb;
1938         struct udf_sb_info *sbi = UDF_SB(sb);
1939         struct logicalVolIntegrityDescImpUse *lvidiu;
1940
1941         if (sbi->s_lvid_bh != NULL)
1942                 lvidiu = udf_sb_lvidiu(sbi);
1943         else
1944                 lvidiu = NULL;
1945
1946         buf->f_type = UDF_SUPER_MAGIC;
1947         buf->f_bsize = sb->s_blocksize;
1948         buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1949         buf->f_bfree = udf_count_free(sb);
1950         buf->f_bavail = buf->f_bfree;
1951         buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1952                                           le32_to_cpu(lvidiu->numDirs)) : 0)
1953                         + buf->f_bfree;
1954         buf->f_ffree = buf->f_bfree;
1955         /* __kernel_fsid_t f_fsid */
1956         buf->f_namelen = UDF_NAME_LEN - 2;
1957
1958         return 0;
1959 }
1960
1961 static unsigned char udf_bitmap_lookup[16] = {
1962         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1963 };
1964
1965 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1966                                           struct udf_bitmap *bitmap)
1967 {
1968         struct buffer_head *bh = NULL;
1969         unsigned int accum = 0;
1970         int index;
1971         int block = 0, newblock;
1972         kernel_lb_addr loc;
1973         uint32_t bytes;
1974         uint8_t value;
1975         uint8_t *ptr;
1976         uint16_t ident;
1977         struct spaceBitmapDesc *bm;
1978
1979         lock_kernel();
1980
1981         loc.logicalBlockNum = bitmap->s_extPosition;
1982         loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1983         bh = udf_read_ptagged(sb, loc, 0, &ident);
1984
1985         if (!bh) {
1986                 printk(KERN_ERR "udf: udf_count_free failed\n");
1987                 goto out;
1988         } else if (ident != TAG_IDENT_SBD) {
1989                 brelse(bh);
1990                 printk(KERN_ERR "udf: udf_count_free failed\n");
1991                 goto out;
1992         }
1993
1994         bm = (struct spaceBitmapDesc *)bh->b_data;
1995         bytes = le32_to_cpu(bm->numOfBytes);
1996         index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1997         ptr = (uint8_t *)bh->b_data;
1998
1999         while (bytes > 0) {
2000                 while ((bytes > 0) && (index < sb->s_blocksize)) {
2001                         value = ptr[index];
2002                         accum += udf_bitmap_lookup[value & 0x0f];
2003                         accum += udf_bitmap_lookup[value >> 4];
2004                         index++;
2005                         bytes--;
2006                 }
2007                 if (bytes) {
2008                         brelse(bh);
2009                         newblock = udf_get_lb_pblock(sb, loc, ++block);
2010                         bh = udf_tread(sb, newblock);
2011                         if (!bh) {
2012                                 udf_debug("read failed\n");
2013                                 goto out;
2014                         }
2015                         index = 0;
2016                         ptr = (uint8_t *)bh->b_data;
2017                 }
2018         }
2019         brelse(bh);
2020
2021 out:
2022         unlock_kernel();
2023
2024         return accum;
2025 }
2026
2027 static unsigned int udf_count_free_table(struct super_block *sb,
2028                                          struct inode *table)
2029 {
2030         unsigned int accum = 0;
2031         uint32_t elen;
2032         kernel_lb_addr eloc;
2033         int8_t etype;
2034         struct extent_position epos;
2035
2036         lock_kernel();
2037
2038         epos.block = UDF_I(table)->i_location;
2039         epos.offset = sizeof(struct unallocSpaceEntry);
2040         epos.bh = NULL;
2041
2042         while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2043                 accum += (elen >> table->i_sb->s_blocksize_bits);
2044
2045         brelse(epos.bh);
2046
2047         unlock_kernel();
2048
2049         return accum;
2050 }
2051
2052 static unsigned int udf_count_free(struct super_block *sb)
2053 {
2054         unsigned int accum = 0;
2055         struct udf_sb_info *sbi;
2056         struct udf_part_map *map;
2057
2058         sbi = UDF_SB(sb);
2059         if (sbi->s_lvid_bh) {
2060                 struct logicalVolIntegrityDesc *lvid =
2061                         (struct logicalVolIntegrityDesc *)
2062                         sbi->s_lvid_bh->b_data;
2063                 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2064                         accum = le32_to_cpu(
2065                                         lvid->freeSpaceTable[sbi->s_partition]);
2066                         if (accum == 0xFFFFFFFF)
2067                                 accum = 0;
2068                 }
2069         }
2070
2071         if (accum)
2072                 return accum;
2073
2074         map = &sbi->s_partmaps[sbi->s_partition];
2075         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2076                 accum += udf_count_free_bitmap(sb,
2077                                                map->s_uspace.s_bitmap);
2078         }
2079         if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2080                 accum += udf_count_free_bitmap(sb,
2081                                                map->s_fspace.s_bitmap);
2082         }
2083         if (accum)
2084                 return accum;
2085
2086         if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2087                 accum += udf_count_free_table(sb,
2088                                               map->s_uspace.s_table);
2089         }
2090         if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2091                 accum += udf_count_free_table(sb,
2092                                               map->s_fspace.s_table);
2093         }
2094
2095         return accum;
2096 }