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