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