]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/udf/super.c
reiserfs: only call unlock_new_inode() if I_NEW
[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/
248727a4
AK
14 * https://www.ecma.ch/
15 * https://www.iso.org/
1da177e4 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{
871b9b14 770 struct domainIdentSuffix *suffix;
8b47ea6c
JK
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 }
49be68c4 776 if (ident->flags & ENTITYID_FLAGS_DIRTY) {
8b47ea6c
JK
777 udf_warn(sb, "Possibly not OSTA UDF compliant %s descriptor.\n",
778 dname);
779 goto force_ro;
780 }
871b9b14
PR
781 suffix = (struct domainIdentSuffix *)ident->identSuffix;
782 if ((suffix->domainFlags & DOMAIN_FLAGS_HARD_WRITE_PROTECT) ||
783 (suffix->domainFlags & DOMAIN_FLAGS_SOFT_WRITE_PROTECT)) {
8b47ea6c
JK
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 1008 struct udf_bitmap *bitmap;
256ccb9b 1009 int nr_groups = udf_compute_nr_groups(sb, index);
66e1da3f 1010
256ccb9b
DE
1011 bitmap = kvzalloc(struct_size(bitmap, s_block_bitmap, nr_groups),
1012 GFP_KERNEL);
ba2eb866 1013 if (!bitmap)
66e1da3f 1014 return NULL;
66e1da3f 1015
66e1da3f
MS
1016 bitmap->s_nr_groups = nr_groups;
1017 return bitmap;
1018}
1019
b085fbe2
JK
1020static int check_partition_desc(struct super_block *sb,
1021 struct partitionDesc *p,
1022 struct udf_part_map *map)
1023{
1024 bool umap, utable, fmap, ftable;
1025 struct partitionHeaderDesc *phd;
1026
1027 switch (le32_to_cpu(p->accessType)) {
1028 case PD_ACCESS_TYPE_READ_ONLY:
1029 case PD_ACCESS_TYPE_WRITE_ONCE:
b085fbe2
JK
1030 case PD_ACCESS_TYPE_NONE:
1031 goto force_ro;
1032 }
1033
1034 /* No Partition Header Descriptor? */
1035 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1036 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1037 goto force_ro;
1038
1039 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1040 utable = phd->unallocSpaceTable.extLength;
1041 umap = phd->unallocSpaceBitmap.extLength;
1042 ftable = phd->freedSpaceTable.extLength;
1043 fmap = phd->freedSpaceBitmap.extLength;
1044
1045 /* No allocation info? */
1046 if (!utable && !umap && !ftable && !fmap)
1047 goto force_ro;
1048
1049 /* We don't support blocks that require erasing before overwrite */
1050 if (ftable || fmap)
1051 goto force_ro;
1052 /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
1053 if (utable && umap)
1054 goto force_ro;
1055
1056 if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
57debb81
PR
1057 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1058 map->s_partition_type == UDF_METADATA_MAP25)
b085fbe2
JK
1059 goto force_ro;
1060
1061 return 0;
1062force_ro:
1063 if (!sb_rdonly(sb))
1064 return -EACCES;
1065 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1066 return 0;
1067}
1068
3fb38dfa
JK
1069static int udf_fill_partdesc_info(struct super_block *sb,
1070 struct partitionDesc *p, int p_index)
1da177e4 1071{
6c79e987 1072 struct udf_part_map *map;
165923fa 1073 struct udf_sb_info *sbi = UDF_SB(sb);
3fb38dfa 1074 struct partitionHeaderDesc *phd;
b085fbe2 1075 int err;
165923fa 1076
3fb38dfa 1077 map = &sbi->s_partmaps[p_index];
165923fa
MS
1078
1079 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1080 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1081
1082 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1083 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1084 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1085 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1086 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1087 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1088 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1089 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1090
fcbf7637 1091 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
a983f368
JP
1092 p_index, map->s_partition_type,
1093 map->s_partition_root, map->s_partition_len);
165923fa 1094
b085fbe2
JK
1095 err = check_partition_desc(sb, p, map);
1096 if (err)
1097 return err;
1098
1099 /*
1100 * Skip loading allocation info it we cannot ever write to the fs.
1101 * This is a correctness thing as we may have decided to force ro mount
1102 * to avoid allocation info we don't support.
1103 */
1104 if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
3fb38dfa 1105 return 0;
165923fa
MS
1106
1107 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1108 if (phd->unallocSpaceTable.extLength) {
5ca4e4be 1109 struct kernel_lb_addr loc = {
165923fa
MS
1110 .logicalBlockNum = le32_to_cpu(
1111 phd->unallocSpaceTable.extPosition),
3fb38dfa 1112 .partitionReferenceNum = p_index,
165923fa 1113 };
6d3d5e86 1114 struct inode *inode;
165923fa 1115
6174c2eb 1116 inode = udf_iget_special(sb, &loc);
6d3d5e86 1117 if (IS_ERR(inode)) {
165923fa 1118 udf_debug("cannot load unallocSpaceTable (part %d)\n",
a983f368 1119 p_index);
6d3d5e86 1120 return PTR_ERR(inode);
165923fa 1121 }
6d3d5e86 1122 map->s_uspace.s_table = inode;
165923fa 1123 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
fcbf7637 1124 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
a983f368 1125 p_index, map->s_uspace.s_table->i_ino);
165923fa
MS
1126 }
1127
1128 if (phd->unallocSpaceBitmap.extLength) {
3fb38dfa
JK
1129 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1130 if (!bitmap)
d759bfa4 1131 return -ENOMEM;
165923fa 1132 map->s_uspace.s_bitmap = bitmap;
2e0838fd 1133 bitmap->s_extPosition = le32_to_cpu(
165923fa 1134 phd->unallocSpaceBitmap.extPosition);
2e0838fd 1135 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
fcbf7637 1136 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
a983f368 1137 p_index, bitmap->s_extPosition);
165923fa
MS
1138 }
1139
3fb38dfa
JK
1140 return 0;
1141}
1142
e971b0b9
JK
1143static void udf_find_vat_block(struct super_block *sb, int p_index,
1144 int type1_index, sector_t start_block)
38b74a53
JK
1145{
1146 struct udf_sb_info *sbi = UDF_SB(sb);
1147 struct udf_part_map *map = &sbi->s_partmaps[p_index];
e971b0b9 1148 sector_t vat_block;
5ca4e4be 1149 struct kernel_lb_addr ino;
6d3d5e86 1150 struct inode *inode;
e971b0b9
JK
1151
1152 /*
1153 * VAT file entry is in the last recorded block. Some broken disks have
1154 * it a few blocks before so try a bit harder...
1155 */
1156 ino.partitionReferenceNum = type1_index;
1157 for (vat_block = start_block;
1158 vat_block >= map->s_partition_root &&
6d3d5e86 1159 vat_block >= start_block - 3; vat_block--) {
e971b0b9 1160 ino.logicalBlockNum = vat_block - map->s_partition_root;
6174c2eb 1161 inode = udf_iget_special(sb, &ino);
6d3d5e86
JK
1162 if (!IS_ERR(inode)) {
1163 sbi->s_vat_inode = inode;
1164 break;
1165 }
e971b0b9
JK
1166 }
1167}
1168
1169static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1170{
1171 struct udf_sb_info *sbi = UDF_SB(sb);
1172 struct udf_part_map *map = &sbi->s_partmaps[p_index];
fa5e0815
JK
1173 struct buffer_head *bh = NULL;
1174 struct udf_inode_info *vati;
1175 uint32_t pos;
1176 struct virtualAllocationTable20 *vat20;
23bcda11
FF
1177 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1178 sb->s_blocksize_bits;
38b74a53 1179
e971b0b9 1180 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
4bf17af0
JK
1181 if (!sbi->s_vat_inode &&
1182 sbi->s_last_block != blocks - 1) {
78ace70c
JP
1183 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1184 (unsigned long)sbi->s_last_block,
1185 (unsigned long)blocks - 1);
e971b0b9 1186 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
4bf17af0 1187 }
38b74a53 1188 if (!sbi->s_vat_inode)
d759bfa4 1189 return -EIO;
38b74a53
JK
1190
1191 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
47c9358a 1192 map->s_type_specific.s_virtual.s_start_offset = 0;
38b74a53
JK
1193 map->s_type_specific.s_virtual.s_num_entries =
1194 (sbi->s_vat_inode->i_size - 36) >> 2;
1195 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
fa5e0815
JK
1196 vati = UDF_I(sbi->s_vat_inode);
1197 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1198 pos = udf_block_map(sbi->s_vat_inode, 0);
1199 bh = sb_bread(sb, pos);
1200 if (!bh)
d759bfa4 1201 return -EIO;
fa5e0815
JK
1202 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1203 } else {
1204 vat20 = (struct virtualAllocationTable20 *)
1205 vati->i_ext.i_data;
1206 }
38b74a53 1207
38b74a53 1208 map->s_type_specific.s_virtual.s_start_offset =
47c9358a 1209 le16_to_cpu(vat20->lengthHeader);
38b74a53
JK
1210 map->s_type_specific.s_virtual.s_num_entries =
1211 (sbi->s_vat_inode->i_size -
1212 map->s_type_specific.s_virtual.
1213 s_start_offset) >> 2;
1214 brelse(bh);
1215 }
1216 return 0;
1217}
1218
d759bfa4
JK
1219/*
1220 * Load partition descriptor block
1221 *
1222 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1223 * sequence.
1224 */
3fb38dfa
JK
1225static int udf_load_partdesc(struct super_block *sb, sector_t block)
1226{
1227 struct buffer_head *bh;
1228 struct partitionDesc *p;
1229 struct udf_part_map *map;
1230 struct udf_sb_info *sbi = UDF_SB(sb);
38b74a53 1231 int i, type1_idx;
3fb38dfa
JK
1232 uint16_t partitionNumber;
1233 uint16_t ident;
d759bfa4 1234 int ret;
3fb38dfa
JK
1235
1236 bh = udf_read_tagged(sb, block, block, &ident);
1237 if (!bh)
d759bfa4
JK
1238 return -EAGAIN;
1239 if (ident != TAG_IDENT_PD) {
1240 ret = 0;
3fb38dfa 1241 goto out_bh;
d759bfa4 1242 }
3fb38dfa
JK
1243
1244 p = (struct partitionDesc *)bh->b_data;
1245 partitionNumber = le16_to_cpu(p->partitionNumber);
38b74a53 1246
7888824b 1247 /* First scan for TYPE1 and SPARABLE partitions */
3fb38dfa
JK
1248 for (i = 0; i < sbi->s_partitions; i++) {
1249 map = &sbi->s_partmaps[i];
fcbf7637 1250 udf_debug("Searching map: (%u == %u)\n",
3fb38dfa 1251 map->s_partition_num, partitionNumber);
38b74a53
JK
1252 if (map->s_partition_num == partitionNumber &&
1253 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1254 map->s_partition_type == UDF_SPARABLE_MAP15))
3fb38dfa
JK
1255 break;
1256 }
1257
38b74a53 1258 if (i >= sbi->s_partitions) {
fcbf7637 1259 udf_debug("Partition (%u) not found in partition map\n",
3fb38dfa 1260 partitionNumber);
d759bfa4 1261 ret = 0;
3fb38dfa
JK
1262 goto out_bh;
1263 }
165923fa 1264
3fb38dfa 1265 ret = udf_fill_partdesc_info(sb, p, i);
d759bfa4
JK
1266 if (ret < 0)
1267 goto out_bh;
38b74a53
JK
1268
1269 /*
bfb257a5
JK
1270 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1271 * PHYSICAL partitions are already set up
38b74a53
JK
1272 */
1273 type1_idx = i;
44499602 1274 map = NULL; /* supress 'maybe used uninitialized' warning */
38b74a53
JK
1275 for (i = 0; i < sbi->s_partitions; i++) {
1276 map = &sbi->s_partmaps[i];
1277
1278 if (map->s_partition_num == partitionNumber &&
1279 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
bfb257a5
JK
1280 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1281 map->s_partition_type == UDF_METADATA_MAP25))
38b74a53
JK
1282 break;
1283 }
1284
d759bfa4
JK
1285 if (i >= sbi->s_partitions) {
1286 ret = 0;
38b74a53 1287 goto out_bh;
d759bfa4 1288 }
38b74a53
JK
1289
1290 ret = udf_fill_partdesc_info(sb, p, i);
d759bfa4 1291 if (ret < 0)
38b74a53
JK
1292 goto out_bh;
1293
bfb257a5 1294 if (map->s_partition_type == UDF_METADATA_MAP25) {
7888824b 1295 ret = udf_load_metadata_files(sb, i, type1_idx);
d759bfa4 1296 if (ret < 0) {
78ace70c
JP
1297 udf_err(sb, "error loading MetaData partition map %d\n",
1298 i);
bfb257a5
JK
1299 goto out_bh;
1300 }
1301 } else {
e729eac6
JK
1302 /*
1303 * If we have a partition with virtual map, we don't handle
1304 * writing to it (we overwrite blocks instead of relocating
1305 * them).
1306 */
bc98a42c 1307 if (!sb_rdonly(sb)) {
e729eac6
JK
1308 ret = -EACCES;
1309 goto out_bh;
1310 }
a9ad01bc 1311 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
bfb257a5 1312 ret = udf_load_vat(sb, i, type1_idx);
d759bfa4 1313 if (ret < 0)
bfb257a5 1314 goto out_bh;
bfb257a5 1315 }
d759bfa4 1316 ret = 0;
c0eb31ed 1317out_bh:
2e0838fd 1318 /* In case loading failed, we handle cleanup in udf_fill_super */
c0eb31ed
JK
1319 brelse(bh);
1320 return ret;
1da177e4
LT
1321}
1322
1df2ae31
JK
1323static int udf_load_sparable_map(struct super_block *sb,
1324 struct udf_part_map *map,
1325 struct sparablePartitionMap *spm)
1326{
1327 uint32_t loc;
1328 uint16_t ident;
1329 struct sparingTable *st;
1330 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1331 int i;
1332 struct buffer_head *bh;
1333
1334 map->s_partition_type = UDF_SPARABLE_MAP15;
1335 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1336 if (!is_power_of_2(sdata->s_packet_len)) {
1337 udf_err(sb, "error loading logical volume descriptor: "
1338 "Invalid packet length %u\n",
1339 (unsigned)sdata->s_packet_len);
1340 return -EIO;
1341 }
1342 if (spm->numSparingTables > 4) {
1343 udf_err(sb, "error loading logical volume descriptor: "
1344 "Too many sparing tables (%d)\n",
1345 (int)spm->numSparingTables);
1346 return -EIO;
1347 }
1348
1349 for (i = 0; i < spm->numSparingTables; i++) {
1350 loc = le32_to_cpu(spm->locSparingTable[i]);
1351 bh = udf_read_tagged(sb, loc, loc, &ident);
1352 if (!bh)
1353 continue;
1354
1355 st = (struct sparingTable *)bh->b_data;
1356 if (ident != 0 ||
1357 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1358 strlen(UDF_ID_SPARING)) ||
1359 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1360 sb->s_blocksize) {
1361 brelse(bh);
1362 continue;
1363 }
1364
1365 sdata->s_spar_map[i] = bh;
1366 }
1367 map->s_partition_func = udf_get_pblock_spar15;
1368 return 0;
1369}
1370
c0eb31ed 1371static int udf_load_logicalvol(struct super_block *sb, sector_t block,
5ca4e4be 1372 struct kernel_lb_addr *fileset)
1da177e4
LT
1373{
1374 struct logicalVolDesc *lvd;
1df2ae31 1375 int i, offset;
1da177e4 1376 uint8_t type;
6c79e987 1377 struct udf_sb_info *sbi = UDF_SB(sb);
4b11111a 1378 struct genericPartitionMap *gpm;
c0eb31ed
JK
1379 uint16_t ident;
1380 struct buffer_head *bh;
adee11b2 1381 unsigned int table_len;
d759bfa4 1382 int ret;
1da177e4 1383
c0eb31ed
JK
1384 bh = udf_read_tagged(sb, block, block, &ident);
1385 if (!bh)
d759bfa4 1386 return -EAGAIN;
c0eb31ed 1387 BUG_ON(ident != TAG_IDENT_LVD);
1da177e4 1388 lvd = (struct logicalVolDesc *)bh->b_data;
adee11b2 1389 table_len = le32_to_cpu(lvd->mapTableLength);
57b9655d 1390 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
adee11b2
JK
1391 udf_err(sb, "error loading logical volume descriptor: "
1392 "Partition table too long (%u > %lu)\n", table_len,
1393 sb->s_blocksize - sizeof(*lvd));
d759bfa4 1394 ret = -EIO;
adee11b2
JK
1395 goto out_bh;
1396 }
1da177e4 1397
2dee5aac
JK
1398 ret = udf_verify_domain_identifier(sb, &lvd->domainIdent,
1399 "logical volume");
1400 if (ret)
1401 goto out_bh;
cb14d340
JK
1402 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1403 if (ret)
c0eb31ed 1404 goto out_bh;
1da177e4 1405
cb00ea35 1406 for (i = 0, offset = 0;
adee11b2 1407 i < sbi->s_partitions && offset < table_len;
4b11111a
MS
1408 i++, offset += gpm->partitionMapLength) {
1409 struct udf_part_map *map = &sbi->s_partmaps[i];
1410 gpm = (struct genericPartitionMap *)
1411 &(lvd->partitionMaps[offset]);
1412 type = gpm->partitionMapType;
cb00ea35 1413 if (type == 1) {
4b11111a
MS
1414 struct genericPartitionMap1 *gpm1 =
1415 (struct genericPartitionMap1 *)gpm;
6c79e987
MS
1416 map->s_partition_type = UDF_TYPE1_MAP15;
1417 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1418 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1419 map->s_partition_func = NULL;
cb00ea35 1420 } else if (type == 2) {
4b11111a
MS
1421 struct udfPartitionMap2 *upm2 =
1422 (struct udfPartitionMap2 *)gpm;
1423 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1424 strlen(UDF_ID_VIRTUAL))) {
1425 u16 suf =
1426 le16_to_cpu(((__le16 *)upm2->partIdent.
1427 identSuffix)[0]);
c82a1275 1428 if (suf < 0x0200) {
4b11111a
MS
1429 map->s_partition_type =
1430 UDF_VIRTUAL_MAP15;
1431 map->s_partition_func =
1432 udf_get_pblock_virt15;
c82a1275 1433 } else {
4b11111a
MS
1434 map->s_partition_type =
1435 UDF_VIRTUAL_MAP20;
1436 map->s_partition_func =
1437 udf_get_pblock_virt20;
1da177e4 1438 }
4b11111a
MS
1439 } else if (!strncmp(upm2->partIdent.ident,
1440 UDF_ID_SPARABLE,
1441 strlen(UDF_ID_SPARABLE))) {
d759bfa4
JK
1442 ret = udf_load_sparable_map(sb, map,
1443 (struct sparablePartitionMap *)gpm);
1444 if (ret < 0)
1df2ae31 1445 goto out_bh;
bfb257a5
JK
1446 } else if (!strncmp(upm2->partIdent.ident,
1447 UDF_ID_METADATA,
1448 strlen(UDF_ID_METADATA))) {
1449 struct udf_meta_data *mdata =
1450 &map->s_type_specific.s_metadata;
1451 struct metadataPartitionMap *mdm =
1452 (struct metadataPartitionMap *)
1453 &(lvd->partitionMaps[offset]);
fcbf7637 1454 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
a983f368 1455 i, type, UDF_ID_METADATA);
bfb257a5
JK
1456
1457 map->s_partition_type = UDF_METADATA_MAP25;
1458 map->s_partition_func = udf_get_pblock_meta25;
1459
1460 mdata->s_meta_file_loc =
1461 le32_to_cpu(mdm->metadataFileLoc);
1462 mdata->s_mirror_file_loc =
1463 le32_to_cpu(mdm->metadataMirrorFileLoc);
1464 mdata->s_bitmap_file_loc =
1465 le32_to_cpu(mdm->metadataBitmapFileLoc);
1466 mdata->s_alloc_unit_size =
1467 le32_to_cpu(mdm->allocUnitSize);
1468 mdata->s_align_unit_size =
1469 le16_to_cpu(mdm->alignUnitSize);
ed47a7d0
JK
1470 if (mdm->flags & 0x01)
1471 mdata->s_flags |= MF_DUPLICATE_MD;
bfb257a5
JK
1472
1473 udf_debug("Metadata Ident suffix=0x%x\n",
a983f368
JP
1474 le16_to_cpu(*(__le16 *)
1475 mdm->partIdent.identSuffix));
fcbf7637 1476 udf_debug("Metadata part num=%u\n",
a983f368 1477 le16_to_cpu(mdm->partitionNum));
fcbf7637 1478 udf_debug("Metadata part alloc unit size=%u\n",
a983f368 1479 le32_to_cpu(mdm->allocUnitSize));
fcbf7637 1480 udf_debug("Metadata file loc=%u\n",
a983f368 1481 le32_to_cpu(mdm->metadataFileLoc));
fcbf7637 1482 udf_debug("Mirror file loc=%u\n",
a983f368 1483 le32_to_cpu(mdm->metadataMirrorFileLoc));
fcbf7637 1484 udf_debug("Bitmap file loc=%u\n",
a983f368 1485 le32_to_cpu(mdm->metadataBitmapFileLoc));
fcbf7637 1486 udf_debug("Flags: %d %u\n",
ed47a7d0 1487 mdata->s_flags, mdm->flags);
cb00ea35 1488 } else {
3a71fc5d
MS
1489 udf_debug("Unknown ident: %s\n",
1490 upm2->partIdent.ident);
1da177e4
LT
1491 continue;
1492 }
6c79e987
MS
1493 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1494 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1da177e4 1495 }
fcbf7637 1496 udf_debug("Partition (%d:%u) type %u on volume %u\n",
a983f368 1497 i, map->s_partition_num, type, map->s_volumeseqnum);
1da177e4
LT
1498 }
1499
cb00ea35 1500 if (fileset) {
5ca4e4be 1501 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1da177e4
LT
1502
1503 *fileset = lelb_to_cpu(la->extLocation);
fcbf7637 1504 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
a983f368 1505 fileset->logicalBlockNum,
28de7948 1506 fileset->partitionReferenceNum);
1da177e4
LT
1507 }
1508 if (lvd->integritySeqExt.extLength)
1509 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
d759bfa4 1510 ret = 0;
4f5edd82
SM
1511
1512 if (!sbi->s_lvid_bh) {
1513 /* We can't generate unique IDs without a valid LVID */
1514 if (sb_rdonly(sb)) {
1515 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1516 } else {
1517 udf_warn(sb, "Damaged or missing LVID, forcing "
1518 "readonly mount\n");
1519 ret = -EACCES;
1520 }
1521 }
c0eb31ed
JK
1522out_bh:
1523 brelse(bh);
1524 return ret;
1da177e4
LT
1525}
1526
1527/*
a47241cd 1528 * Find the prevailing Logical Volume Integrity Descriptor.
1da177e4 1529 */
5ca4e4be 1530static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1da177e4 1531{
a47241cd 1532 struct buffer_head *bh, *final_bh;
1da177e4 1533 uint16_t ident;
6c79e987
MS
1534 struct udf_sb_info *sbi = UDF_SB(sb);
1535 struct logicalVolIntegrityDesc *lvid;
a47241cd
AT
1536 int indirections = 0;
1537
1538 while (++indirections <= UDF_MAX_LVID_NESTING) {
1539 final_bh = NULL;
1540 while (loc.extLength > 0 &&
1541 (bh = udf_read_tagged(sb, loc.extLocation,
1542 loc.extLocation, &ident))) {
1543 if (ident != TAG_IDENT_LVID) {
1544 brelse(bh);
1545 break;
1546 }
1547
1548 brelse(final_bh);
1549 final_bh = bh;
1da177e4 1550
a47241cd
AT
1551 loc.extLength -= sb->s_blocksize;
1552 loc.extLocation++;
1553 }
cb00ea35 1554
a47241cd
AT
1555 if (!final_bh)
1556 return;
cb00ea35 1557
a47241cd
AT
1558 brelse(sbi->s_lvid_bh);
1559 sbi->s_lvid_bh = final_bh;
1560
1561 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1562 if (lvid->nextIntegrityExt.extLength == 0)
1563 return;
1564
1565 loc = leea_to_cpu(lvid->nextIntegrityExt);
1da177e4 1566 }
a47241cd
AT
1567
1568 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1569 UDF_MAX_LVID_NESTING);
1570 brelse(sbi->s_lvid_bh);
1571 sbi->s_lvid_bh = NULL;
1da177e4
LT
1572}
1573
7b78fd02
JK
1574/*
1575 * Step for reallocation of table of partition descriptor sequence numbers.
1576 * Must be power of 2.
1577 */
1578#define PART_DESC_ALLOC_STEP 32
1579
ee4af50c
JK
1580struct part_desc_seq_scan_data {
1581 struct udf_vds_record rec;
1582 u32 partnum;
1583};
1584
7b78fd02
JK
1585struct desc_seq_scan_data {
1586 struct udf_vds_record vds[VDS_POS_LENGTH];
1587 unsigned int size_part_descs;
ee4af50c
JK
1588 unsigned int num_part_descs;
1589 struct part_desc_seq_scan_data *part_descs_loc;
7b78fd02
JK
1590};
1591
1592static struct udf_vds_record *handle_partition_descriptor(
1593 struct buffer_head *bh,
1594 struct desc_seq_scan_data *data)
1595{
1596 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1597 int partnum;
ee4af50c 1598 int i;
7b78fd02
JK
1599
1600 partnum = le16_to_cpu(desc->partitionNumber);
ee4af50c
JK
1601 for (i = 0; i < data->num_part_descs; i++)
1602 if (partnum == data->part_descs_loc[i].partnum)
1603 return &(data->part_descs_loc[i].rec);
1604 if (data->num_part_descs >= data->size_part_descs) {
1605 struct part_desc_seq_scan_data *new_loc;
7b78fd02
JK
1606 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1607
6396bb22 1608 new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
7b78fd02
JK
1609 if (!new_loc)
1610 return ERR_PTR(-ENOMEM);
1611 memcpy(new_loc, data->part_descs_loc,
1612 data->size_part_descs * sizeof(*new_loc));
1613 kfree(data->part_descs_loc);
1614 data->part_descs_loc = new_loc;
1615 data->size_part_descs = new_size;
1616 }
ee4af50c 1617 return &(data->part_descs_loc[data->num_part_descs++].rec);
7b78fd02
JK
1618}
1619
1620
1621static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1622 struct buffer_head *bh, struct desc_seq_scan_data *data)
18cf4781
JK
1623{
1624 switch (ident) {
1625 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
7b78fd02 1626 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
18cf4781 1627 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
7b78fd02 1628 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
18cf4781 1629 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
7b78fd02 1630 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
18cf4781 1631 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
7b78fd02
JK
1632 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1633 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1634 return handle_partition_descriptor(bh, data);
18cf4781
JK
1635 }
1636 return NULL;
1637}
e7a4eb86 1638
1da177e4 1639/*
d759bfa4
JK
1640 * Process a main/reserve volume descriptor sequence.
1641 * @block First block of first extent of the sequence.
1642 * @lastblock Lastblock of first extent of the sequence.
1643 * @fileset There we store extent containing root fileset
1da177e4 1644 *
d759bfa4
JK
1645 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1646 * sequence
1da177e4 1647 */
d759bfa4
JK
1648static noinline int udf_process_sequence(
1649 struct super_block *sb,
1650 sector_t block, sector_t lastblock,
1651 struct kernel_lb_addr *fileset)
1da177e4
LT
1652{
1653 struct buffer_head *bh = NULL;
4b11111a 1654 struct udf_vds_record *curr;
1da177e4
LT
1655 struct generic_desc *gd;
1656 struct volDescPtr *vdp;
2b8f9421 1657 bool done = false;
1da177e4
LT
1658 uint32_t vdsn;
1659 uint16_t ident;
d759bfa4 1660 int ret;
e7a4eb86 1661 unsigned int indirections = 0;
7b78fd02
JK
1662 struct desc_seq_scan_data data;
1663 unsigned int i;
1664
1665 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1666 data.size_part_descs = PART_DESC_ALLOC_STEP;
ee4af50c 1667 data.num_part_descs = 0;
6396bb22
KC
1668 data.part_descs_loc = kcalloc(data.size_part_descs,
1669 sizeof(*data.part_descs_loc),
1670 GFP_KERNEL);
7b78fd02
JK
1671 if (!data.part_descs_loc)
1672 return -ENOMEM;
1da177e4 1673
c0eb31ed
JK
1674 /*
1675 * Read the main descriptor sequence and find which descriptors
1676 * are in it.
1677 */
cb00ea35 1678 for (; (!done && block <= lastblock); block++) {
1da177e4 1679 bh = udf_read_tagged(sb, block, block, &ident);
67621675
JK
1680 if (!bh)
1681 break;
1da177e4
LT
1682
1683 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1684 gd = (struct generic_desc *)bh->b_data;
1685 vdsn = le32_to_cpu(gd->volDescSeqNum);
cb00ea35 1686 switch (ident) {
28de7948 1687 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
7b568cba
JK
1688 if (++indirections > UDF_MAX_TD_NESTING) {
1689 udf_err(sb, "too many Volume Descriptor "
1690 "Pointers (max %u supported)\n",
1691 UDF_MAX_TD_NESTING);
1692 brelse(bh);
1693 return -EIO;
cb00ea35 1694 }
7b568cba
JK
1695
1696 vdp = (struct volDescPtr *)bh->b_data;
1697 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1698 lastblock = le32_to_cpu(
1699 vdp->nextVolDescSeqExt.extLength) >>
1700 sb->s_blocksize_bits;
1701 lastblock += block - 1;
1702 /* For loop is going to increment 'block' again */
1703 block--;
cb00ea35 1704 break;
18cf4781 1705 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
28de7948 1706 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
18cf4781
JK
1707 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1708 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
7b78fd02
JK
1709 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1710 curr = get_volume_descriptor_record(ident, bh, &data);
1711 if (IS_ERR(curr)) {
1712 brelse(bh);
1713 return PTR_ERR(curr);
1714 }
1715 /* Descriptor we don't care about? */
1716 if (!curr)
1717 break;
4b11111a
MS
1718 if (vdsn >= curr->volDescSeqNum) {
1719 curr->volDescSeqNum = vdsn;
1720 curr->block = block;
cb00ea35
CG
1721 }
1722 break;
28de7948 1723 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
7b568cba 1724 done = true;
cb00ea35 1725 break;
1da177e4 1726 }
3bf25cb4 1727 brelse(bh);
1da177e4 1728 }
c0eb31ed
JK
1729 /*
1730 * Now read interesting descriptors again and process them
1731 * in a suitable order
1732 */
7b78fd02 1733 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
78ace70c 1734 udf_err(sb, "Primary Volume Descriptor not found!\n");
d759bfa4
JK
1735 return -EAGAIN;
1736 }
7b78fd02 1737 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
d759bfa4
JK
1738 if (ret < 0)
1739 return ret;
1740
7b78fd02 1741 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
d759bfa4 1742 ret = udf_load_logicalvol(sb,
7b78fd02
JK
1743 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1744 fileset);
d759bfa4
JK
1745 if (ret < 0)
1746 return ret;
c0eb31ed 1747 }
165923fa 1748
7b78fd02 1749 /* Now handle prevailing Partition Descriptors */
ee4af50c
JK
1750 for (i = 0; i < data.num_part_descs; i++) {
1751 ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
1752 if (ret < 0)
1753 return ret;
1da177e4
LT
1754 }
1755
1756 return 0;
1757}
1758
d759bfa4
JK
1759/*
1760 * Load Volume Descriptor Sequence described by anchor in bh
1761 *
1762 * Returns <0 on error, 0 on success
1763 */
40346005
JK
1764static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1765 struct kernel_lb_addr *fileset)
1da177e4 1766{
40346005 1767 struct anchorVolDescPtr *anchor;
d759bfa4
JK
1768 sector_t main_s, main_e, reserve_s, reserve_e;
1769 int ret;
1da177e4 1770
40346005
JK
1771 anchor = (struct anchorVolDescPtr *)bh->b_data;
1772
1773 /* Locate the main sequence */
1774 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1775 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1776 main_e = main_e >> sb->s_blocksize_bits;
91c9c9ec 1777 main_e += main_s - 1;
40346005
JK
1778
1779 /* Locate the reserve sequence */
1780 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1781 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1782 reserve_e = reserve_e >> sb->s_blocksize_bits;
91c9c9ec 1783 reserve_e += reserve_s - 1;
40346005
JK
1784
1785 /* Process the main & reserve sequences */
1786 /* responsible for finding the PartitionDesc(s) */
d759bfa4
JK
1787 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1788 if (ret != -EAGAIN)
1789 return ret;
bff943af 1790 udf_sb_free_partitions(sb);
d759bfa4
JK
1791 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1792 if (ret < 0) {
1793 udf_sb_free_partitions(sb);
1794 /* No sequence was OK, return -EIO */
1795 if (ret == -EAGAIN)
1796 ret = -EIO;
1797 }
1798 return ret;
1da177e4
LT
1799}
1800
40346005
JK
1801/*
1802 * Check whether there is an anchor block in the given block and
1803 * load Volume Descriptor Sequence if so.
d759bfa4
JK
1804 *
1805 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1806 * block
40346005
JK
1807 */
1808static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1809 struct kernel_lb_addr *fileset)
1197e4df 1810{
40346005
JK
1811 struct buffer_head *bh;
1812 uint16_t ident;
1813 int ret;
1197e4df 1814
40346005
JK
1815 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1816 udf_fixed_to_variable(block) >=
23bcda11 1817 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
d759bfa4 1818 return -EAGAIN;
40346005
JK
1819
1820 bh = udf_read_tagged(sb, block, block, &ident);
1821 if (!bh)
d759bfa4 1822 return -EAGAIN;
40346005
JK
1823 if (ident != TAG_IDENT_AVDP) {
1824 brelse(bh);
d759bfa4 1825 return -EAGAIN;
1197e4df 1826 }
40346005
JK
1827 ret = udf_load_sequence(sb, bh, fileset);
1828 brelse(bh);
1829 return ret;
1197e4df
CL
1830}
1831
d759bfa4
JK
1832/*
1833 * Search for an anchor volume descriptor pointer.
1834 *
1835 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1836 * of anchors.
1837 */
1838static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1839 struct kernel_lb_addr *fileset)
1da177e4 1840{
40346005 1841 sector_t last[6];
38b74a53 1842 int i;
40346005
JK
1843 struct udf_sb_info *sbi = UDF_SB(sb);
1844 int last_count = 0;
d759bfa4 1845 int ret;
1da177e4 1846
40346005
JK
1847 /* First try user provided anchor */
1848 if (sbi->s_anchor) {
d759bfa4
JK
1849 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1850 if (ret != -EAGAIN)
1851 return ret;
40346005
JK
1852 }
1853 /*
1854 * according to spec, anchor is in either:
1855 * block 256
1856 * lastblock-256
1857 * lastblock
1858 * however, if the disc isn't closed, it could be 512.
1859 */
d759bfa4
JK
1860 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1861 if (ret != -EAGAIN)
1862 return ret;
40346005
JK
1863 /*
1864 * The trouble is which block is the last one. Drives often misreport
1865 * this so we try various possibilities.
1866 */
d759bfa4
JK
1867 last[last_count++] = *lastblock;
1868 if (*lastblock >= 1)
1869 last[last_count++] = *lastblock - 1;
1870 last[last_count++] = *lastblock + 1;
1871 if (*lastblock >= 2)
1872 last[last_count++] = *lastblock - 2;
1873 if (*lastblock >= 150)
1874 last[last_count++] = *lastblock - 150;
1875 if (*lastblock >= 152)
1876 last[last_count++] = *lastblock - 152;
1da177e4 1877
40346005 1878 for (i = 0; i < last_count; i++) {
23bcda11 1879 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
40346005 1880 sb->s_blocksize_bits)
28f7c4d4 1881 continue;
d759bfa4
JK
1882 ret = udf_check_anchor_block(sb, last[i], fileset);
1883 if (ret != -EAGAIN) {
1884 if (!ret)
1885 *lastblock = last[i];
1886 return ret;
1887 }
40346005 1888 if (last[i] < 256)
28f7c4d4 1889 continue;
d759bfa4
JK
1890 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1891 if (ret != -EAGAIN) {
1892 if (!ret)
1893 *lastblock = last[i];
1894 return ret;
1895 }
40346005 1896 }
28f7c4d4 1897
40346005 1898 /* Finally try block 512 in case media is open */
d759bfa4 1899 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
40346005 1900}
28f7c4d4 1901
40346005
JK
1902/*
1903 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1904 * area specified by it. The function expects sbi->s_lastblock to be the last
1905 * block on the media.
1906 *
d759bfa4
JK
1907 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1908 * was not found.
40346005
JK
1909 */
1910static int udf_find_anchor(struct super_block *sb,
1911 struct kernel_lb_addr *fileset)
1912{
40346005 1913 struct udf_sb_info *sbi = UDF_SB(sb);
d759bfa4
JK
1914 sector_t lastblock = sbi->s_last_block;
1915 int ret;
28f7c4d4 1916
d759bfa4
JK
1917 ret = udf_scan_anchors(sb, &lastblock, fileset);
1918 if (ret != -EAGAIN)
40346005 1919 goto out;
1da177e4 1920
40346005
JK
1921 /* No anchor found? Try VARCONV conversion of block numbers */
1922 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
d759bfa4 1923 lastblock = udf_variable_to_fixed(sbi->s_last_block);
40346005 1924 /* Firstly, we try to not convert number of the last block */
d759bfa4
JK
1925 ret = udf_scan_anchors(sb, &lastblock, fileset);
1926 if (ret != -EAGAIN)
40346005 1927 goto out;
1da177e4 1928
d759bfa4 1929 lastblock = sbi->s_last_block;
40346005 1930 /* Secondly, we try with converted number of the last block */
d759bfa4
JK
1931 ret = udf_scan_anchors(sb, &lastblock, fileset);
1932 if (ret < 0) {
40346005
JK
1933 /* VARCONV didn't help. Clear it. */
1934 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1da177e4 1935 }
40346005 1936out:
d759bfa4
JK
1937 if (ret == 0)
1938 sbi->s_last_block = lastblock;
1939 return ret;
40346005 1940}
1da177e4 1941
40346005
JK
1942/*
1943 * Check Volume Structure Descriptor, find Anchor block and load Volume
d759bfa4
JK
1944 * Descriptor Sequence.
1945 *
1946 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1947 * block was not found.
40346005
JK
1948 */
1949static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1950 int silent, struct kernel_lb_addr *fileset)
1951{
1952 struct udf_sb_info *sbi = UDF_SB(sb);
ba54aef0 1953 int nsr = 0;
d759bfa4 1954 int ret;
40346005
JK
1955
1956 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1957 if (!silent)
78ace70c 1958 udf_warn(sb, "Bad block size\n");
d759bfa4 1959 return -EINVAL;
40346005
JK
1960 }
1961 sbi->s_last_block = uopt->lastblock;
1962 if (!uopt->novrs) {
1963 /* Check that it is NSR02 compliant */
ba54aef0
SM
1964 nsr = udf_check_vsd(sb);
1965 if (!nsr) {
40346005 1966 if (!silent)
78ace70c 1967 udf_warn(sb, "No VRS found\n");
70f16cef 1968 return -EINVAL;
40346005 1969 }
ba54aef0 1970 if (nsr == -1)
44499602
PF
1971 udf_debug("Failed to read sector at offset %d. "
1972 "Assuming open disc. Skipping validity "
1973 "check\n", VSD_FIRST_SECTOR_OFFSET);
40346005
JK
1974 if (!sbi->s_last_block)
1975 sbi->s_last_block = udf_get_last_block(sb);
1976 } else {
1977 udf_debug("Validity check skipped because of novrs option\n");
28f7c4d4 1978 }
1da177e4 1979
40346005
JK
1980 /* Look for anchor block and load Volume Descriptor Sequence */
1981 sbi->s_anchor = uopt->anchor;
d759bfa4
JK
1982 ret = udf_find_anchor(sb, fileset);
1983 if (ret < 0) {
1984 if (!silent && ret == -EAGAIN)
78ace70c 1985 udf_warn(sb, "No anchor found\n");
d759bfa4 1986 return ret;
40346005 1987 }
d759bfa4 1988 return 0;
1da177e4
LT
1989}
1990
ebbd5e99
SM
1991static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid)
1992{
1993 struct timespec64 ts;
1994
1995 ktime_get_real_ts64(&ts);
1996 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
1997 lvid->descTag.descCRC = cpu_to_le16(
1998 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
1999 le16_to_cpu(lvid->descTag.descCRCLength)));
2000 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2001}
2002
1da177e4
LT
2003static void udf_open_lvid(struct super_block *sb)
2004{
6c79e987
MS
2005 struct udf_sb_info *sbi = UDF_SB(sb);
2006 struct buffer_head *bh = sbi->s_lvid_bh;
165923fa
MS
2007 struct logicalVolIntegrityDesc *lvid;
2008 struct logicalVolIntegrityDescImpUse *lvidiu;
146bca72 2009
165923fa
MS
2010 if (!bh)
2011 return;
165923fa 2012 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
69d75671
JK
2013 lvidiu = udf_sb_lvidiu(sb);
2014 if (!lvidiu)
2015 return;
165923fa 2016
69d75671 2017 mutex_lock(&sbi->s_alloc_mutex);
165923fa
MS
2018 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2019 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
b72e632c
JK
2020 if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
2021 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
2022 else
2023 UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
165923fa 2024
ebbd5e99 2025 udf_finalize_lvid(lvid);
165923fa 2026 mark_buffer_dirty(bh);
146bca72 2027 sbi->s_lvid_dirty = 0;
949f4a7c 2028 mutex_unlock(&sbi->s_alloc_mutex);
9734c971
JK
2029 /* Make opening of filesystem visible on the media immediately */
2030 sync_dirty_buffer(bh);
1da177e4
LT
2031}
2032
2033static void udf_close_lvid(struct super_block *sb)
2034{
6c79e987
MS
2035 struct udf_sb_info *sbi = UDF_SB(sb);
2036 struct buffer_head *bh = sbi->s_lvid_bh;
2037 struct logicalVolIntegrityDesc *lvid;
165923fa 2038 struct logicalVolIntegrityDescImpUse *lvidiu;
28de7948 2039
6c79e987
MS
2040 if (!bh)
2041 return;
69d75671
JK
2042 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2043 lvidiu = udf_sb_lvidiu(sb);
2044 if (!lvidiu)
2045 return;
6c79e987 2046
949f4a7c 2047 mutex_lock(&sbi->s_alloc_mutex);
165923fa
MS
2048 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2049 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
165923fa
MS
2050 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2051 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2052 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2053 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2054 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2055 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
b72e632c
JK
2056 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
2057 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
165923fa 2058
853a0c25
JK
2059 /*
2060 * We set buffer uptodate unconditionally here to avoid spurious
2061 * warnings from mark_buffer_dirty() when previous EIO has marked
2062 * the buffer as !uptodate
2063 */
2064 set_buffer_uptodate(bh);
ebbd5e99 2065 udf_finalize_lvid(lvid);
165923fa 2066 mark_buffer_dirty(bh);
146bca72 2067 sbi->s_lvid_dirty = 0;
949f4a7c 2068 mutex_unlock(&sbi->s_alloc_mutex);
9734c971
JK
2069 /* Make closing of filesystem visible on the media immediately */
2070 sync_dirty_buffer(bh);
1da177e4
LT
2071}
2072
d664b6af
JK
2073u64 lvid_get_unique_id(struct super_block *sb)
2074{
2075 struct buffer_head *bh;
2076 struct udf_sb_info *sbi = UDF_SB(sb);
2077 struct logicalVolIntegrityDesc *lvid;
2078 struct logicalVolHeaderDesc *lvhd;
2079 u64 uniqueID;
2080 u64 ret;
2081
2082 bh = sbi->s_lvid_bh;
2083 if (!bh)
2084 return 0;
2085
2086 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2087 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2088
2089 mutex_lock(&sbi->s_alloc_mutex);
2090 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2091 if (!(++uniqueID & 0xFFFFFFFF))
2092 uniqueID += 16;
2093 lvhd->uniqueID = cpu_to_le64(uniqueID);
e8b42747 2094 udf_updated_lvid(sb);
d664b6af 2095 mutex_unlock(&sbi->s_alloc_mutex);
d664b6af
JK
2096
2097 return ret;
1da177e4
LT
2098}
2099
1da177e4
LT
2100static int udf_fill_super(struct super_block *sb, void *options, int silent)
2101{
d759bfa4 2102 int ret = -EINVAL;
cb00ea35 2103 struct inode *inode = NULL;
1da177e4 2104 struct udf_options uopt;
5ca4e4be 2105 struct kernel_lb_addr rootdir, fileset;
1da177e4 2106 struct udf_sb_info *sbi;
9181f8bf 2107 bool lvid_open = false;
1da177e4
LT
2108
2109 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
116e5258
JK
2110 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2111 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2112 uopt.gid = make_kgid(current_user_ns(), overflowgid);
1da177e4 2113 uopt.umask = 0;
87bc730c
MS
2114 uopt.fmode = UDF_INVALID_MODE;
2115 uopt.dmode = UDF_INVALID_MODE;
785dffe1 2116 uopt.nls_map = NULL;
1da177e4 2117
033c9da0 2118 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
9db9f9e3 2119 if (!sbi)
1da177e4 2120 return -ENOMEM;
28de7948 2121
1da177e4 2122 sb->s_fs_info = sbi;
1da177e4 2123
1e7933de 2124 mutex_init(&sbi->s_alloc_mutex);
1da177e4 2125
6da80894 2126 if (!udf_parse_options((char *)options, &uopt, false))
fdf2657b 2127 goto parse_options_failure;
1da177e4
LT
2128
2129 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
cb00ea35 2130 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
8076c363 2131 udf_err(sb, "utf8 cannot be combined with iocharset\n");
fdf2657b 2132 goto parse_options_failure;
1da177e4 2133 }
cb00ea35 2134 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1da177e4
LT
2135 uopt.nls_map = load_nls_default();
2136 if (!uopt.nls_map)
2137 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2138 else
2139 udf_debug("Using default NLS map\n");
2140 }
1da177e4
LT
2141 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2142 uopt.flags |= (1 << UDF_FLAG_UTF8);
2143
2144 fileset.logicalBlockNum = 0xFFFFFFFF;
2145 fileset.partitionReferenceNum = 0xFFFF;
2146
6c79e987
MS
2147 sbi->s_flags = uopt.flags;
2148 sbi->s_uid = uopt.uid;
2149 sbi->s_gid = uopt.gid;
2150 sbi->s_umask = uopt.umask;
7ac9bcd5
MS
2151 sbi->s_fmode = uopt.fmode;
2152 sbi->s_dmode = uopt.dmode;
6c79e987 2153 sbi->s_nls_map = uopt.nls_map;
c03cad24 2154 rwlock_init(&sbi->s_cred_lock);
1da177e4 2155
cb00ea35 2156 if (uopt.session == 0xFFFFFFFF)
6c79e987 2157 sbi->s_session = udf_get_last_session(sb);
1da177e4 2158 else
6c79e987 2159 sbi->s_session = uopt.session;
1da177e4 2160
6c79e987 2161 udf_debug("Multi-session=%d\n", sbi->s_session);
1da177e4 2162
40346005
JK
2163 /* Fill in the rest of the superblock */
2164 sb->s_op = &udf_sb_ops;
2165 sb->s_export_op = &udf_export_ops;
123e9caf 2166
40346005
JK
2167 sb->s_magic = UDF_SUPER_MAGIC;
2168 sb->s_time_gran = 1000;
2169
1197e4df 2170 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
40346005 2171 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1197e4df 2172 } else {
e1defc4f 2173 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
70f16cef 2174 while (uopt.blocksize <= 4096) {
40346005 2175 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
70f16cef
FF
2176 if (ret < 0) {
2177 if (!silent && ret != -EACCES) {
fcbf7637 2178 pr_notice("Scanning with blocksize %u failed\n",
70f16cef
FF
2179 uopt.blocksize);
2180 }
2181 brelse(sbi->s_lvid_bh);
2182 sbi->s_lvid_bh = NULL;
2183 /*
2184 * EACCES is special - we want to propagate to
2185 * upper layers that we cannot handle RW mount.
2186 */
2187 if (ret == -EACCES)
2188 break;
2189 } else
2190 break;
2191
2192 uopt.blocksize <<= 1;
1197e4df 2193 }
1da177e4 2194 }
d759bfa4
JK
2195 if (ret < 0) {
2196 if (ret == -EAGAIN) {
2197 udf_warn(sb, "No partition found (1)\n");
2198 ret = -EINVAL;
2199 }
1da177e4
LT
2200 goto error_out;
2201 }
2202
fcbf7637 2203 udf_debug("Lastblock=%u\n", sbi->s_last_block);
1da177e4 2204
6c79e987 2205 if (sbi->s_lvid_bh) {
4b11111a 2206 struct logicalVolIntegrityDescImpUse *lvidiu =
69d75671
JK
2207 udf_sb_lvidiu(sb);
2208 uint16_t minUDFReadRev;
2209 uint16_t minUDFWriteRev;
1da177e4 2210
69d75671
JK
2211 if (!lvidiu) {
2212 ret = -EINVAL;
2213 goto error_out;
2214 }
2215 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2216 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
cb00ea35 2217 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
78ace70c 2218 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
69d75671 2219 minUDFReadRev,
78ace70c 2220 UDF_MAX_READ_VERSION);
d759bfa4 2221 ret = -EINVAL;
1da177e4 2222 goto error_out;
a9ad01bc
JK
2223 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
2224 if (!sb_rdonly(sb)) {
2225 ret = -EACCES;
2226 goto error_out;
2227 }
2228 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
e729eac6 2229 }
1da177e4 2230
6c79e987 2231 sbi->s_udfrev = minUDFWriteRev;
1da177e4
LT
2232
2233 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2234 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2235 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2236 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2237 }
2238
6c79e987 2239 if (!sbi->s_partitions) {
78ace70c 2240 udf_warn(sb, "No partition found (2)\n");
d759bfa4 2241 ret = -EINVAL;
1da177e4
LT
2242 goto error_out;
2243 }
2244
4b11111a 2245 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
a9ad01bc
JK
2246 UDF_PART_FLAG_READ_ONLY) {
2247 if (!sb_rdonly(sb)) {
2248 ret = -EACCES;
2249 goto error_out;
2250 }
2251 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
c1a26e7d 2252 }
39b3f6d6 2253
2dee5aac
JK
2254 ret = udf_find_fileset(sb, &fileset, &rootdir);
2255 if (ret < 0) {
78ace70c 2256 udf_warn(sb, "No fileset found\n");
1da177e4
LT
2257 goto error_out;
2258 }
2259
cb00ea35 2260 if (!silent) {
5ca4e4be 2261 struct timestamp ts;
56774805 2262 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
78ace70c
JP
2263 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2264 sbi->s_volume_ident,
2265 le16_to_cpu(ts.year), ts.month, ts.day,
56774805 2266 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
1da177e4 2267 }
bc98a42c 2268 if (!sb_rdonly(sb)) {
1da177e4 2269 udf_open_lvid(sb);
9181f8bf
JK
2270 lvid_open = true;
2271 }
1da177e4
LT
2272
2273 /* Assign the root inode */
2274 /* assign inodes by physical block number */
2275 /* perhaps it's not extensible enough, but for now ... */
97e961fd 2276 inode = udf_iget(sb, &rootdir);
6d3d5e86 2277 if (IS_ERR(inode)) {
fcbf7637 2278 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
cb00ea35 2279 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
6d3d5e86 2280 ret = PTR_ERR(inode);
1da177e4
LT
2281 goto error_out;
2282 }
2283
2284 /* Allocate a dentry for the root inode */
48fde701 2285 sb->s_root = d_make_root(inode);
cb00ea35 2286 if (!sb->s_root) {
78ace70c 2287 udf_err(sb, "Couldn't allocate root dentry\n");
d759bfa4 2288 ret = -ENOMEM;
1da177e4
LT
2289 goto error_out;
2290 }
31170b6a 2291 sb->s_maxbytes = MAX_LFS_FILESIZE;
8de52778 2292 sb->s_max_links = UDF_MAX_LINKS;
1da177e4
LT
2293 return 0;
2294
28de7948 2295error_out:
0d454e4a 2296 iput(sbi->s_vat_inode);
fdf2657b 2297parse_options_failure:
785dffe1
CX
2298 if (uopt.nls_map)
2299 unload_nls(uopt.nls_map);
9181f8bf 2300 if (lvid_open)
1da177e4 2301 udf_close_lvid(sb);
6c79e987 2302 brelse(sbi->s_lvid_bh);
bff943af 2303 udf_sb_free_partitions(sb);
1da177e4
LT
2304 kfree(sbi);
2305 sb->s_fs_info = NULL;
28de7948 2306
d759bfa4 2307 return ret;
1da177e4
LT
2308}
2309
8076c363
JP
2310void _udf_err(struct super_block *sb, const char *function,
2311 const char *fmt, ...)
1da177e4 2312{
c2bff36c 2313 struct va_format vaf;
1da177e4
LT
2314 va_list args;
2315
1da177e4 2316 va_start(args, fmt);
c2bff36c
JP
2317
2318 vaf.fmt = fmt;
2319 vaf.va = &args;
2320
2321 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2322
1da177e4 2323 va_end(args);
1da177e4
LT
2324}
2325
a40ecd7b
JP
2326void _udf_warn(struct super_block *sb, const char *function,
2327 const char *fmt, ...)
1da177e4 2328{
c2bff36c 2329 struct va_format vaf;
1da177e4
LT
2330 va_list args;
2331
cb00ea35 2332 va_start(args, fmt);
c2bff36c
JP
2333
2334 vaf.fmt = fmt;
2335 vaf.va = &args;
2336
2337 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2338
1da177e4 2339 va_end(args);
1da177e4
LT
2340}
2341
cb00ea35 2342static void udf_put_super(struct super_block *sb)
1da177e4 2343{
6c79e987 2344 struct udf_sb_info *sbi;
1da177e4 2345
6c79e987 2346 sbi = UDF_SB(sb);
6cfd0148 2347
0d454e4a 2348 iput(sbi->s_vat_inode);
1da177e4 2349 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
6c79e987 2350 unload_nls(sbi->s_nls_map);
bc98a42c 2351 if (!sb_rdonly(sb))
1da177e4 2352 udf_close_lvid(sb);
6c79e987 2353 brelse(sbi->s_lvid_bh);
bff943af 2354 udf_sb_free_partitions(sb);
bbe48dd8 2355 mutex_destroy(&sbi->s_alloc_mutex);
1da177e4
LT
2356 kfree(sb->s_fs_info);
2357 sb->s_fs_info = NULL;
2358}
2359
146bca72
JK
2360static int udf_sync_fs(struct super_block *sb, int wait)
2361{
2362 struct udf_sb_info *sbi = UDF_SB(sb);
2363
2364 mutex_lock(&sbi->s_alloc_mutex);
2365 if (sbi->s_lvid_dirty) {
e8b42747 2366 struct buffer_head *bh = sbi->s_lvid_bh;
52b9666e 2367 struct logicalVolIntegrityDesc *lvid;
e8b42747 2368
52b9666e
JK
2369 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2370 udf_finalize_lvid(lvid);
e8b42747 2371
146bca72
JK
2372 /*
2373 * Blockdevice will be synced later so we don't have to submit
2374 * the buffer for IO
2375 */
e8b42747 2376 mark_buffer_dirty(bh);
146bca72
JK
2377 sbi->s_lvid_dirty = 0;
2378 }
2379 mutex_unlock(&sbi->s_alloc_mutex);
2380
2381 return 0;
2382}
2383
cb00ea35 2384static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 2385{
726c3342 2386 struct super_block *sb = dentry->d_sb;
6c79e987
MS
2387 struct udf_sb_info *sbi = UDF_SB(sb);
2388 struct logicalVolIntegrityDescImpUse *lvidiu;
557f5a14 2389 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
6c79e987 2390
69d75671 2391 lvidiu = udf_sb_lvidiu(sb);
1da177e4
LT
2392 buf->f_type = UDF_SUPER_MAGIC;
2393 buf->f_bsize = sb->s_blocksize;
6c79e987 2394 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1da177e4
LT
2395 buf->f_bfree = udf_count_free(sb);
2396 buf->f_bavail = buf->f_bfree;
356557be
JK
2397 /*
2398 * Let's pretend each free block is also a free 'inode' since UDF does
2399 * not have separate preallocated table of inodes.
2400 */
6c79e987
MS
2401 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2402 le32_to_cpu(lvidiu->numDirs)) : 0)
2403 + buf->f_bfree;
1da177e4 2404 buf->f_ffree = buf->f_bfree;
9fba7056 2405 buf->f_namelen = UDF_NAME_LEN;
557f5a14
CL
2406 buf->f_fsid.val[0] = (u32)id;
2407 buf->f_fsid.val[1] = (u32)(id >> 32);
1da177e4
LT
2408
2409 return 0;
2410}
2411
4b11111a
MS
2412static unsigned int udf_count_free_bitmap(struct super_block *sb,
2413 struct udf_bitmap *bitmap)
1da177e4
LT
2414{
2415 struct buffer_head *bh = NULL;
2416 unsigned int accum = 0;
2417 int index;
b490bdd6 2418 udf_pblk_t block = 0, newblock;
5ca4e4be 2419 struct kernel_lb_addr loc;
1da177e4 2420 uint32_t bytes;
1da177e4
LT
2421 uint8_t *ptr;
2422 uint16_t ident;
2423 struct spaceBitmapDesc *bm;
2424
1da177e4 2425 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 2426 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
97e961fd 2427 bh = udf_read_ptagged(sb, &loc, 0, &ident);
1da177e4 2428
cb00ea35 2429 if (!bh) {
78ace70c 2430 udf_err(sb, "udf_count_free failed\n");
1da177e4 2431 goto out;
cb00ea35 2432 } else if (ident != TAG_IDENT_SBD) {
3bf25cb4 2433 brelse(bh);
78ace70c 2434 udf_err(sb, "udf_count_free failed\n");
1da177e4
LT
2435 goto out;
2436 }
2437
2438 bm = (struct spaceBitmapDesc *)bh->b_data;
2439 bytes = le32_to_cpu(bm->numOfBytes);
28de7948
CG
2440 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2441 ptr = (uint8_t *)bh->b_data;
1da177e4 2442
cb00ea35 2443 while (bytes > 0) {
01b954a3
MS
2444 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2445 accum += bitmap_weight((const unsigned long *)(ptr + index),
2446 cur_bytes * 8);
2447 bytes -= cur_bytes;
cb00ea35 2448 if (bytes) {
3bf25cb4 2449 brelse(bh);
97e961fd 2450 newblock = udf_get_lb_pblock(sb, &loc, ++block);
1da177e4 2451 bh = udf_tread(sb, newblock);
cb00ea35 2452 if (!bh) {
1da177e4
LT
2453 udf_debug("read failed\n");
2454 goto out;
2455 }
2456 index = 0;
28de7948 2457 ptr = (uint8_t *)bh->b_data;
1da177e4
LT
2458 }
2459 }
3bf25cb4 2460 brelse(bh);
28de7948 2461out:
1da177e4
LT
2462 return accum;
2463}
2464
4b11111a
MS
2465static unsigned int udf_count_free_table(struct super_block *sb,
2466 struct inode *table)
1da177e4
LT
2467{
2468 unsigned int accum = 0;
ff116fc8 2469 uint32_t elen;
5ca4e4be 2470 struct kernel_lb_addr eloc;
1da177e4 2471 int8_t etype;
ff116fc8 2472 struct extent_position epos;
1da177e4 2473
d1668fe3 2474 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
c0b34438 2475 epos.block = UDF_I(table)->i_location;
ff116fc8
JK
2476 epos.offset = sizeof(struct unallocSpaceEntry);
2477 epos.bh = NULL;
1da177e4 2478
3a71fc5d 2479 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
1da177e4 2480 accum += (elen >> table->i_sb->s_blocksize_bits);
3a71fc5d 2481
3bf25cb4 2482 brelse(epos.bh);
d1668fe3 2483 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
1da177e4
LT
2484
2485 return accum;
2486}
cb00ea35
CG
2487
2488static unsigned int udf_count_free(struct super_block *sb)
1da177e4
LT
2489{
2490 unsigned int accum = 0;
a4a8b99e 2491 struct udf_sb_info *sbi = UDF_SB(sb);
6c79e987 2492 struct udf_part_map *map;
a4a8b99e
JK
2493 unsigned int part = sbi->s_partition;
2494 int ptype = sbi->s_partmaps[part].s_partition_type;
2495
2496 if (ptype == UDF_METADATA_MAP25) {
2497 part = sbi->s_partmaps[part].s_type_specific.s_metadata.
2498 s_phys_partition_ref;
2499 } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
2500 /*
2501 * Filesystems with VAT are append-only and we cannot write to
2502 * them. Let's just report 0 here.
2503 */
2504 return 0;
2505 }
1da177e4 2506
6c79e987 2507 if (sbi->s_lvid_bh) {
4b11111a
MS
2508 struct logicalVolIntegrityDesc *lvid =
2509 (struct logicalVolIntegrityDesc *)
2510 sbi->s_lvid_bh->b_data;
a4a8b99e 2511 if (le32_to_cpu(lvid->numOfPartitions) > part) {
4b11111a 2512 accum = le32_to_cpu(
a4a8b99e 2513 lvid->freeSpaceTable[part]);
1da177e4
LT
2514 if (accum == 0xFFFFFFFF)
2515 accum = 0;
2516 }
2517 }
2518
2519 if (accum)
2520 return accum;
2521
a4a8b99e 2522 map = &sbi->s_partmaps[part];
6c79e987 2523 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
28de7948 2524 accum += udf_count_free_bitmap(sb,
6c79e987 2525 map->s_uspace.s_bitmap);
1da177e4 2526 }
1da177e4
LT
2527 if (accum)
2528 return accum;
2529
6c79e987 2530 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
28de7948 2531 accum += udf_count_free_table(sb,
6c79e987 2532 map->s_uspace.s_table);
1da177e4 2533 }
1da177e4
LT
2534 return accum;
2535}
54bb60d5
FF
2536
2537MODULE_AUTHOR("Ben Fennema");
2538MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2539MODULE_LICENSE("GPL");
2540module_init(init_udf_fs)
2541module_exit(exit_udf_fs)