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