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