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