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