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