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