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