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