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