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