udf: Handle VAT packed inside inode properly
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / udf / super.c
blobabdb9b31da468ad942f5eed1085bab2b09d24676
1 /*
2 * super.c
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
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/
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.
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
27 * HISTORY
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)
41 #include "udfdecl.h"
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/smp_lock.h>
52 #include <linux/buffer_head.h>
53 #include <linux/vfs.h>
54 #include <linux/vmalloc.h>
55 #include <linux/errno.h>
56 #include <linux/mount.h>
57 #include <linux/seq_file.h>
58 #include <linux/bitmap.h>
59 #include <asm/byteorder.h>
61 #include "udf_sb.h"
62 #include "udf_i.h"
64 #include <linux/init.h>
65 #include <asm/uaccess.h>
67 #define VDS_POS_PRIMARY_VOL_DESC 0
68 #define VDS_POS_UNALLOC_SPACE_DESC 1
69 #define VDS_POS_LOGICAL_VOL_DESC 2
70 #define VDS_POS_PARTITION_DESC 3
71 #define VDS_POS_IMP_USE_VOL_DESC 4
72 #define VDS_POS_VOL_DESC_PTR 5
73 #define VDS_POS_TERMINATING_DESC 6
74 #define VDS_POS_LENGTH 7
76 #define UDF_DEFAULT_BLOCKSIZE 2048
78 static char error_buf[1024];
80 /* These are the "meat" - everything else is stuffing */
81 static int udf_fill_super(struct super_block *, void *, int);
82 static void udf_put_super(struct super_block *);
83 static void udf_write_super(struct super_block *);
84 static int udf_remount_fs(struct super_block *, int *, char *);
85 static int udf_check_valid(struct super_block *, int, int);
86 static int udf_vrs(struct super_block *sb, int silent);
87 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
88 static void udf_find_anchor(struct super_block *);
89 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
90 kernel_lb_addr *);
91 static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
93 static void udf_open_lvid(struct super_block *);
94 static void udf_close_lvid(struct super_block *);
95 static unsigned int udf_count_free(struct super_block *);
96 static int udf_statfs(struct dentry *, struct kstatfs *);
97 static int udf_show_options(struct seq_file *, struct vfsmount *);
98 static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
101 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
111 /* UDF filesystem type */
112 static int udf_get_sb(struct file_system_type *fs_type,
113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
119 static struct file_system_type udf_fstype = {
120 .owner = THIS_MODULE,
121 .name = "udf",
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
127 static struct kmem_cache *udf_inode_cachep;
129 static struct inode *udf_alloc_inode(struct super_block *sb)
131 struct udf_inode_info *ei;
132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
133 if (!ei)
134 return NULL;
136 ei->i_unique = 0;
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
140 ei->i_strat4096 = 0;
142 return &ei->vfs_inode;
145 static void udf_destroy_inode(struct inode *inode)
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
150 static void init_once(struct kmem_cache *cachep, void *foo)
152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
158 static int init_inodecache(void)
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
162 0, (SLAB_RECLAIM_ACCOUNT |
163 SLAB_MEM_SPREAD),
164 init_once);
165 if (!udf_inode_cachep)
166 return -ENOMEM;
167 return 0;
170 static void destroy_inodecache(void)
172 kmem_cache_destroy(udf_inode_cachep);
175 /* Superblock operations */
176 static const struct super_operations udf_sb_ops = {
177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
186 .show_options = udf_show_options,
189 struct udf_options {
190 unsigned char novrs;
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
194 unsigned int anchor;
195 unsigned int volume;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
199 unsigned int flags;
200 mode_t umask;
201 gid_t gid;
202 uid_t uid;
203 struct nls_table *nls_map;
206 static int __init init_udf_fs(void)
208 int err;
210 err = init_inodecache();
211 if (err)
212 goto out1;
213 err = register_filesystem(&udf_fstype);
214 if (err)
215 goto out;
217 return 0;
219 out:
220 destroy_inodecache();
222 out1:
223 return err;
226 static void __exit exit_udf_fs(void)
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
232 module_init(init_udf_fs)
233 module_exit(exit_udf_fs)
235 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
237 struct udf_sb_info *sbi = UDF_SB(sb);
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
240 GFP_KERNEL);
241 if (!sbi->s_partmaps) {
242 udf_error(sb, __FUNCTION__,
243 "Unable to allocate space for %d partition maps",
244 count);
245 sbi->s_partitions = 0;
246 return -ENOMEM;
249 sbi->s_partitions = count;
250 return 0;
253 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
260 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
285 seq_printf(seq, ",session=%u", sbi->s_session);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
287 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
289 * s_anchor[2] could be zeroed out in case there is no anchor
290 * in the specified block, but then the "anchor=N" option
291 * originally given by the user wasn't effective, so it's OK
292 * if we don't show it.
294 if (sbi->s_anchor[2] != 0)
295 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
297 * volume, partition, fileset and rootdir seem to be ignored
298 * currently
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
301 seq_puts(seq, ",utf8");
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
303 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
305 return 0;
309 * udf_parse_options
311 * PURPOSE
312 * Parse mount options.
314 * DESCRIPTION
315 * The following mount options are supported:
317 * gid= Set the default group.
318 * umask= Set the default umask.
319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
330 * The remaining are for debugging and disaster recovery:
332 * novrs Skip volume sequence recognition
334 * The following expect a offset from 0.
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
342 * The following expect a offset from the partition root.
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
349 * PRE-CONDITIONS
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
353 * POST-CONDITIONS
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
357 * HISTORY
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
362 enum {
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
371 static match_table_t tokens = {
372 {Opt_novrs, "novrs"},
373 {Opt_nostrict, "nostrict"},
374 {Opt_bs, "bs=%u"},
375 {Opt_unhide, "unhide"},
376 {Opt_undelete, "undelete"},
377 {Opt_noadinicb, "noadinicb"},
378 {Opt_adinicb, "adinicb"},
379 {Opt_shortad, "shortad"},
380 {Opt_longad, "longad"},
381 {Opt_uforget, "uid=forget"},
382 {Opt_uignore, "uid=ignore"},
383 {Opt_gforget, "gid=forget"},
384 {Opt_gignore, "gid=ignore"},
385 {Opt_gid, "gid=%u"},
386 {Opt_uid, "uid=%u"},
387 {Opt_umask, "umask=%o"},
388 {Opt_session, "session=%u"},
389 {Opt_lastblock, "lastblock=%u"},
390 {Opt_anchor, "anchor=%u"},
391 {Opt_volume, "volume=%u"},
392 {Opt_partition, "partition=%u"},
393 {Opt_fileset, "fileset=%u"},
394 {Opt_rootdir, "rootdir=%u"},
395 {Opt_utf8, "utf8"},
396 {Opt_iocharset, "iocharset=%s"},
397 {Opt_err, NULL}
400 static int udf_parse_options(char *options, struct udf_options *uopt,
401 bool remount)
403 char *p;
404 int option;
406 uopt->novrs = 0;
407 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
408 uopt->partition = 0xFFFF;
409 uopt->session = 0xFFFFFFFF;
410 uopt->lastblock = 0;
411 uopt->anchor = 0;
412 uopt->volume = 0xFFFFFFFF;
413 uopt->rootdir = 0xFFFFFFFF;
414 uopt->fileset = 0xFFFFFFFF;
415 uopt->nls_map = NULL;
417 if (!options)
418 return 1;
420 while ((p = strsep(&options, ",")) != NULL) {
421 substring_t args[MAX_OPT_ARGS];
422 int token;
423 if (!*p)
424 continue;
426 token = match_token(p, tokens, args);
427 switch (token) {
428 case Opt_novrs:
429 uopt->novrs = 1;
430 case Opt_bs:
431 if (match_int(&args[0], &option))
432 return 0;
433 uopt->blocksize = option;
434 break;
435 case Opt_unhide:
436 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
437 break;
438 case Opt_undelete:
439 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
440 break;
441 case Opt_noadinicb:
442 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
443 break;
444 case Opt_adinicb:
445 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_shortad:
448 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
449 break;
450 case Opt_longad:
451 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_gid:
454 if (match_int(args, &option))
455 return 0;
456 uopt->gid = option;
457 uopt->flags |= (1 << UDF_FLAG_GID_SET);
458 break;
459 case Opt_uid:
460 if (match_int(args, &option))
461 return 0;
462 uopt->uid = option;
463 uopt->flags |= (1 << UDF_FLAG_UID_SET);
464 break;
465 case Opt_umask:
466 if (match_octal(args, &option))
467 return 0;
468 uopt->umask = option;
469 break;
470 case Opt_nostrict:
471 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
472 break;
473 case Opt_session:
474 if (match_int(args, &option))
475 return 0;
476 uopt->session = option;
477 if (!remount)
478 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
479 break;
480 case Opt_lastblock:
481 if (match_int(args, &option))
482 return 0;
483 uopt->lastblock = option;
484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
486 break;
487 case Opt_anchor:
488 if (match_int(args, &option))
489 return 0;
490 uopt->anchor = option;
491 break;
492 case Opt_volume:
493 if (match_int(args, &option))
494 return 0;
495 uopt->volume = option;
496 break;
497 case Opt_partition:
498 if (match_int(args, &option))
499 return 0;
500 uopt->partition = option;
501 break;
502 case Opt_fileset:
503 if (match_int(args, &option))
504 return 0;
505 uopt->fileset = option;
506 break;
507 case Opt_rootdir:
508 if (match_int(args, &option))
509 return 0;
510 uopt->rootdir = option;
511 break;
512 case Opt_utf8:
513 uopt->flags |= (1 << UDF_FLAG_UTF8);
514 break;
515 #ifdef CONFIG_UDF_NLS
516 case Opt_iocharset:
517 uopt->nls_map = load_nls(args[0].from);
518 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
519 break;
520 #endif
521 case Opt_uignore:
522 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
523 break;
524 case Opt_uforget:
525 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
526 break;
527 case Opt_gignore:
528 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
529 break;
530 case Opt_gforget:
531 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
532 break;
533 default:
534 printk(KERN_ERR "udf: bad mount option \"%s\" "
535 "or missing value\n", p);
536 return 0;
539 return 1;
542 static void udf_write_super(struct super_block *sb)
544 lock_kernel();
546 if (!(sb->s_flags & MS_RDONLY))
547 udf_open_lvid(sb);
548 sb->s_dirt = 0;
550 unlock_kernel();
553 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
555 struct udf_options uopt;
556 struct udf_sb_info *sbi = UDF_SB(sb);
558 uopt.flags = sbi->s_flags;
559 uopt.uid = sbi->s_uid;
560 uopt.gid = sbi->s_gid;
561 uopt.umask = sbi->s_umask;
563 if (!udf_parse_options(options, &uopt, true))
564 return -EINVAL;
566 sbi->s_flags = uopt.flags;
567 sbi->s_uid = uopt.uid;
568 sbi->s_gid = uopt.gid;
569 sbi->s_umask = uopt.umask;
571 if (sbi->s_lvid_bh) {
572 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
573 if (write_rev > UDF_MAX_WRITE_VERSION)
574 *flags |= MS_RDONLY;
577 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
578 return 0;
579 if (*flags & MS_RDONLY)
580 udf_close_lvid(sb);
581 else
582 udf_open_lvid(sb);
584 return 0;
587 static int udf_vrs(struct super_block *sb, int silent)
589 struct volStructDesc *vsd = NULL;
590 int sector = 32768;
591 int sectorsize;
592 struct buffer_head *bh = NULL;
593 int iso9660 = 0;
594 int nsr02 = 0;
595 int nsr03 = 0;
596 struct udf_sb_info *sbi;
598 /* Block size must be a multiple of 512 */
599 if (sb->s_blocksize & 511)
600 return 0;
601 sbi = UDF_SB(sb);
603 if (sb->s_blocksize < sizeof(struct volStructDesc))
604 sectorsize = sizeof(struct volStructDesc);
605 else
606 sectorsize = sb->s_blocksize;
608 sector += (sbi->s_session << sb->s_blocksize_bits);
610 udf_debug("Starting at sector %u (%ld byte sectors)\n",
611 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
612 /* Process the sequence (if applicable) */
613 for (; !nsr02 && !nsr03; sector += sectorsize) {
614 /* Read a block */
615 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
616 if (!bh)
617 break;
619 /* Look for ISO descriptors */
620 vsd = (struct volStructDesc *)(bh->b_data +
621 (sector & (sb->s_blocksize - 1)));
623 if (vsd->stdIdent[0] == 0) {
624 brelse(bh);
625 break;
626 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
627 VSD_STD_ID_LEN)) {
628 iso9660 = sector;
629 switch (vsd->structType) {
630 case 0:
631 udf_debug("ISO9660 Boot Record found\n");
632 break;
633 case 1:
634 udf_debug("ISO9660 Primary Volume Descriptor "
635 "found\n");
636 break;
637 case 2:
638 udf_debug("ISO9660 Supplementary Volume "
639 "Descriptor found\n");
640 break;
641 case 3:
642 udf_debug("ISO9660 Volume Partition Descriptor "
643 "found\n");
644 break;
645 case 255:
646 udf_debug("ISO9660 Volume Descriptor Set "
647 "Terminator found\n");
648 break;
649 default:
650 udf_debug("ISO9660 VRS (%u) found\n",
651 vsd->structType);
652 break;
654 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
655 VSD_STD_ID_LEN))
656 ; /* nothing */
657 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
658 VSD_STD_ID_LEN)) {
659 brelse(bh);
660 break;
661 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
662 VSD_STD_ID_LEN))
663 nsr02 = sector;
664 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
665 VSD_STD_ID_LEN))
666 nsr03 = sector;
667 brelse(bh);
670 if (nsr03)
671 return nsr03;
672 else if (nsr02)
673 return nsr02;
674 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
675 return -1;
676 else
677 return 0;
681 * Check whether there is an anchor block in the given block
683 static int udf_check_anchor_block(struct super_block *sb, sector_t block,
684 bool varconv)
686 struct buffer_head *bh = NULL;
687 tag *t;
688 uint16_t ident;
689 uint32_t location;
691 if (varconv) {
692 if (udf_fixed_to_variable(block) >=
693 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
694 return 0;
695 bh = sb_bread(sb, udf_fixed_to_variable(block));
697 else
698 bh = sb_bread(sb, block);
700 if (!bh)
701 return 0;
703 t = (tag *)bh->b_data;
704 ident = le16_to_cpu(t->tagIdent);
705 location = le32_to_cpu(t->tagLocation);
706 brelse(bh);
707 if (ident != TAG_IDENT_AVDP)
708 return 0;
709 return location == block;
712 /* Search for an anchor volume descriptor pointer */
713 static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
714 sector_t lastblock)
716 sector_t last[6];
717 int i;
718 struct udf_sb_info *sbi = UDF_SB(sb);
720 last[0] = lastblock;
721 last[1] = last[0] - 1;
722 last[2] = last[0] + 1;
723 last[3] = last[0] - 2;
724 last[4] = last[0] - 150;
725 last[5] = last[0] - 152;
727 /* according to spec, anchor is in either:
728 * block 256
729 * lastblock-256
730 * lastblock
731 * however, if the disc isn't closed, it could be 512 */
733 for (i = 0; i < ARRAY_SIZE(last); i++) {
734 if (last[i] < 0)
735 continue;
736 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
737 sb->s_blocksize_bits)
738 continue;
740 if (udf_check_anchor_block(sb, last[i], varconv)) {
741 sbi->s_anchor[0] = last[i];
742 sbi->s_anchor[1] = last[i] - 256;
743 return last[i];
746 if (last[i] < 256)
747 continue;
749 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
750 sbi->s_anchor[1] = last[i] - 256;
751 return last[i];
755 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
756 sbi->s_anchor[0] = sbi->s_session + 256;
757 return last[0];
759 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
760 sbi->s_anchor[0] = sbi->s_session + 512;
761 return last[0];
763 return 0;
767 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
768 * be the last block on the media.
770 * Return 1 if not found, 0 if ok
773 static void udf_find_anchor(struct super_block *sb)
775 sector_t lastblock;
776 struct buffer_head *bh = NULL;
777 uint16_t ident;
778 int i;
779 struct udf_sb_info *sbi = UDF_SB(sb);
781 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
782 if (lastblock)
783 goto check_anchor;
785 /* No anchor found? Try VARCONV conversion of block numbers */
786 /* Firstly, we try to not convert number of the last block */
787 lastblock = udf_scan_anchors(sb, 1,
788 udf_variable_to_fixed(sbi->s_last_block));
789 if (lastblock) {
790 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
791 goto check_anchor;
794 /* Secondly, we try with converted number of the last block */
795 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
796 if (lastblock)
797 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
799 check_anchor:
801 * Check located anchors and the anchor block supplied via
802 * mount options
804 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
805 if (!sbi->s_anchor[i])
806 continue;
807 bh = udf_read_tagged(sb, sbi->s_anchor[i],
808 sbi->s_anchor[i], &ident);
809 if (!bh)
810 sbi->s_anchor[i] = 0;
811 else {
812 brelse(bh);
813 if (ident != TAG_IDENT_AVDP)
814 sbi->s_anchor[i] = 0;
818 sbi->s_last_block = lastblock;
821 static int udf_find_fileset(struct super_block *sb,
822 kernel_lb_addr *fileset,
823 kernel_lb_addr *root)
825 struct buffer_head *bh = NULL;
826 long lastblock;
827 uint16_t ident;
828 struct udf_sb_info *sbi;
830 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
831 fileset->partitionReferenceNum != 0xFFFF) {
832 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
834 if (!bh) {
835 return 1;
836 } else if (ident != TAG_IDENT_FSD) {
837 brelse(bh);
838 return 1;
843 sbi = UDF_SB(sb);
844 if (!bh) {
845 /* Search backwards through the partitions */
846 kernel_lb_addr newfileset;
848 /* --> cvg: FIXME - is it reasonable? */
849 return 1;
851 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
852 (newfileset.partitionReferenceNum != 0xFFFF &&
853 fileset->logicalBlockNum == 0xFFFFFFFF &&
854 fileset->partitionReferenceNum == 0xFFFF);
855 newfileset.partitionReferenceNum--) {
856 lastblock = sbi->s_partmaps
857 [newfileset.partitionReferenceNum]
858 .s_partition_len;
859 newfileset.logicalBlockNum = 0;
861 do {
862 bh = udf_read_ptagged(sb, newfileset, 0,
863 &ident);
864 if (!bh) {
865 newfileset.logicalBlockNum++;
866 continue;
869 switch (ident) {
870 case TAG_IDENT_SBD:
872 struct spaceBitmapDesc *sp;
873 sp = (struct spaceBitmapDesc *)
874 bh->b_data;
875 newfileset.logicalBlockNum += 1 +
876 ((le32_to_cpu(sp->numOfBytes) +
877 sizeof(struct spaceBitmapDesc)
878 - 1) >> sb->s_blocksize_bits);
879 brelse(bh);
880 break;
882 case TAG_IDENT_FSD:
883 *fileset = newfileset;
884 break;
885 default:
886 newfileset.logicalBlockNum++;
887 brelse(bh);
888 bh = NULL;
889 break;
891 } while (newfileset.logicalBlockNum < lastblock &&
892 fileset->logicalBlockNum == 0xFFFFFFFF &&
893 fileset->partitionReferenceNum == 0xFFFF);
897 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
898 fileset->partitionReferenceNum != 0xFFFF) && bh) {
899 udf_debug("Fileset at block=%d, partition=%d\n",
900 fileset->logicalBlockNum,
901 fileset->partitionReferenceNum);
903 sbi->s_partition = fileset->partitionReferenceNum;
904 udf_load_fileset(sb, bh, root);
905 brelse(bh);
906 return 0;
908 return 1;
911 static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
913 struct primaryVolDesc *pvoldesc;
914 struct ustr instr;
915 struct ustr outstr;
916 struct buffer_head *bh;
917 uint16_t ident;
919 bh = udf_read_tagged(sb, block, block, &ident);
920 if (!bh)
921 return 1;
922 BUG_ON(ident != TAG_IDENT_PVD);
924 pvoldesc = (struct primaryVolDesc *)bh->b_data;
926 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
927 pvoldesc->recordingDateAndTime)) {
928 #ifdef UDFFS_DEBUG
929 timestamp *ts = &pvoldesc->recordingDateAndTime;
930 udf_debug("recording time %04u/%02u/%02u"
931 " %02u:%02u (%x)\n",
932 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
933 ts->minute, le16_to_cpu(ts->typeAndTimezone));
934 #endif
937 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
938 if (udf_CS0toUTF8(&outstr, &instr)) {
939 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
940 outstr.u_len > 31 ? 31 : outstr.u_len);
941 udf_debug("volIdent[] = '%s'\n",
942 UDF_SB(sb)->s_volume_ident);
945 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
946 if (udf_CS0toUTF8(&outstr, &instr))
947 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
949 brelse(bh);
950 return 0;
953 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
954 kernel_lb_addr *root)
956 struct fileSetDesc *fset;
958 fset = (struct fileSetDesc *)bh->b_data;
960 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
962 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
964 udf_debug("Rootdir at block=%d, partition=%d\n",
965 root->logicalBlockNum, root->partitionReferenceNum);
968 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
970 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
971 return DIV_ROUND_UP(map->s_partition_len +
972 (sizeof(struct spaceBitmapDesc) << 3),
973 sb->s_blocksize * 8);
976 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
978 struct udf_bitmap *bitmap;
979 int nr_groups;
980 int size;
982 nr_groups = udf_compute_nr_groups(sb, index);
983 size = sizeof(struct udf_bitmap) +
984 (sizeof(struct buffer_head *) * nr_groups);
986 if (size <= PAGE_SIZE)
987 bitmap = kmalloc(size, GFP_KERNEL);
988 else
989 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
991 if (bitmap == NULL) {
992 udf_error(sb, __FUNCTION__,
993 "Unable to allocate space for bitmap "
994 "and %d buffer_head pointers", nr_groups);
995 return NULL;
998 memset(bitmap, 0x00, size);
999 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1000 bitmap->s_nr_groups = nr_groups;
1001 return bitmap;
1004 static int udf_fill_partdesc_info(struct super_block *sb,
1005 struct partitionDesc *p, int p_index)
1007 struct udf_part_map *map;
1008 struct udf_sb_info *sbi = UDF_SB(sb);
1009 struct partitionHeaderDesc *phd;
1011 map = &sbi->s_partmaps[p_index];
1013 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1014 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1016 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1017 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1018 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1019 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1020 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1021 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1022 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1023 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1025 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1026 "block length %d\n", partitionNumber, p_index,
1027 map->s_partition_type, map->s_partition_root,
1028 map->s_partition_len);
1030 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1031 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1032 return 0;
1034 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1035 if (phd->unallocSpaceTable.extLength) {
1036 kernel_lb_addr loc = {
1037 .logicalBlockNum = le32_to_cpu(
1038 phd->unallocSpaceTable.extPosition),
1039 .partitionReferenceNum = p_index,
1042 map->s_uspace.s_table = udf_iget(sb, loc);
1043 if (!map->s_uspace.s_table) {
1044 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1045 p_index);
1046 return 1;
1048 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1049 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1050 p_index, map->s_uspace.s_table->i_ino);
1053 if (phd->unallocSpaceBitmap.extLength) {
1054 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1055 if (!bitmap)
1056 return 1;
1057 map->s_uspace.s_bitmap = bitmap;
1058 bitmap->s_extLength = le32_to_cpu(
1059 phd->unallocSpaceBitmap.extLength);
1060 bitmap->s_extPosition = le32_to_cpu(
1061 phd->unallocSpaceBitmap.extPosition);
1062 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1063 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1064 bitmap->s_extPosition);
1067 if (phd->partitionIntegrityTable.extLength)
1068 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1070 if (phd->freedSpaceTable.extLength) {
1071 kernel_lb_addr loc = {
1072 .logicalBlockNum = le32_to_cpu(
1073 phd->freedSpaceTable.extPosition),
1074 .partitionReferenceNum = p_index,
1077 map->s_fspace.s_table = udf_iget(sb, loc);
1078 if (!map->s_fspace.s_table) {
1079 udf_debug("cannot load freedSpaceTable (part %d)\n",
1080 p_index);
1081 return 1;
1084 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1085 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1086 p_index, map->s_fspace.s_table->i_ino);
1089 if (phd->freedSpaceBitmap.extLength) {
1090 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1091 if (!bitmap)
1092 return 1;
1093 map->s_fspace.s_bitmap = bitmap;
1094 bitmap->s_extLength = le32_to_cpu(
1095 phd->freedSpaceBitmap.extLength);
1096 bitmap->s_extPosition = le32_to_cpu(
1097 phd->freedSpaceBitmap.extPosition);
1098 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1099 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1100 bitmap->s_extPosition);
1102 return 0;
1105 static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1107 struct udf_sb_info *sbi = UDF_SB(sb);
1108 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1109 kernel_lb_addr ino;
1110 struct buffer_head *bh = NULL;
1111 struct udf_inode_info *vati;
1112 uint32_t pos;
1113 struct virtualAllocationTable20 *vat20;
1115 /* VAT file entry is in the last recorded block */
1116 ino.partitionReferenceNum = type1_index;
1117 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1118 sbi->s_vat_inode = udf_iget(sb, ino);
1119 if (!sbi->s_vat_inode)
1120 return 1;
1122 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1123 map->s_type_specific.s_virtual.s_start_offset =
1124 udf_ext0_offset(sbi->s_vat_inode);
1125 map->s_type_specific.s_virtual.s_num_entries =
1126 (sbi->s_vat_inode->i_size - 36) >> 2;
1127 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1128 vati = UDF_I(sbi->s_vat_inode);
1129 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1130 pos = udf_block_map(sbi->s_vat_inode, 0);
1131 bh = sb_bread(sb, pos);
1132 if (!bh)
1133 return 1;
1134 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1135 } else {
1136 vat20 = (struct virtualAllocationTable20 *)
1137 vati->i_ext.i_data;
1140 map->s_type_specific.s_virtual.s_start_offset =
1141 le16_to_cpu(vat20->lengthHeader) +
1142 udf_ext0_offset(sbi->s_vat_inode);
1143 map->s_type_specific.s_virtual.s_num_entries =
1144 (sbi->s_vat_inode->i_size -
1145 map->s_type_specific.s_virtual.
1146 s_start_offset) >> 2;
1147 brelse(bh);
1149 return 0;
1152 static int udf_load_partdesc(struct super_block *sb, sector_t block)
1154 struct buffer_head *bh;
1155 struct partitionDesc *p;
1156 struct udf_part_map *map;
1157 struct udf_sb_info *sbi = UDF_SB(sb);
1158 int i, type1_idx;
1159 uint16_t partitionNumber;
1160 uint16_t ident;
1161 int ret = 0;
1163 bh = udf_read_tagged(sb, block, block, &ident);
1164 if (!bh)
1165 return 1;
1166 if (ident != TAG_IDENT_PD)
1167 goto out_bh;
1169 p = (struct partitionDesc *)bh->b_data;
1170 partitionNumber = le16_to_cpu(p->partitionNumber);
1172 /* First scan for TYPE1 and SPARABLE partitions */
1173 for (i = 0; i < sbi->s_partitions; i++) {
1174 map = &sbi->s_partmaps[i];
1175 udf_debug("Searching map: (%d == %d)\n",
1176 map->s_partition_num, partitionNumber);
1177 if (map->s_partition_num == partitionNumber &&
1178 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1179 map->s_partition_type == UDF_SPARABLE_MAP15))
1180 break;
1183 if (i >= sbi->s_partitions) {
1184 udf_debug("Partition (%d) not found in partition map\n",
1185 partitionNumber);
1186 goto out_bh;
1189 ret = udf_fill_partdesc_info(sb, p, i);
1192 * Now rescan for VIRTUAL partitions when TYPE1 partitions are
1193 * already set up
1195 type1_idx = i;
1196 for (i = 0; i < sbi->s_partitions; i++) {
1197 map = &sbi->s_partmaps[i];
1199 if (map->s_partition_num == partitionNumber &&
1200 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1201 map->s_partition_type == UDF_VIRTUAL_MAP20))
1202 break;
1205 if (i >= sbi->s_partitions)
1206 goto out_bh;
1208 ret = udf_fill_partdesc_info(sb, p, i);
1209 if (ret)
1210 goto out_bh;
1212 ret = udf_load_vat(sb, i, type1_idx);
1213 out_bh:
1214 /* In case loading failed, we handle cleanup in udf_fill_super */
1215 brelse(bh);
1216 return ret;
1219 static int udf_load_logicalvol(struct super_block *sb, sector_t block,
1220 kernel_lb_addr *fileset)
1222 struct logicalVolDesc *lvd;
1223 int i, j, offset;
1224 uint8_t type;
1225 struct udf_sb_info *sbi = UDF_SB(sb);
1226 struct genericPartitionMap *gpm;
1227 uint16_t ident;
1228 struct buffer_head *bh;
1229 int ret = 0;
1231 bh = udf_read_tagged(sb, block, block, &ident);
1232 if (!bh)
1233 return 1;
1234 BUG_ON(ident != TAG_IDENT_LVD);
1235 lvd = (struct logicalVolDesc *)bh->b_data;
1237 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1238 if (i != 0) {
1239 ret = i;
1240 goto out_bh;
1243 for (i = 0, offset = 0;
1244 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1245 i++, offset += gpm->partitionMapLength) {
1246 struct udf_part_map *map = &sbi->s_partmaps[i];
1247 gpm = (struct genericPartitionMap *)
1248 &(lvd->partitionMaps[offset]);
1249 type = gpm->partitionMapType;
1250 if (type == 1) {
1251 struct genericPartitionMap1 *gpm1 =
1252 (struct genericPartitionMap1 *)gpm;
1253 map->s_partition_type = UDF_TYPE1_MAP15;
1254 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1255 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1256 map->s_partition_func = NULL;
1257 } else if (type == 2) {
1258 struct udfPartitionMap2 *upm2 =
1259 (struct udfPartitionMap2 *)gpm;
1260 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1261 strlen(UDF_ID_VIRTUAL))) {
1262 u16 suf =
1263 le16_to_cpu(((__le16 *)upm2->partIdent.
1264 identSuffix)[0]);
1265 if (suf < 0x0200) {
1266 map->s_partition_type =
1267 UDF_VIRTUAL_MAP15;
1268 map->s_partition_func =
1269 udf_get_pblock_virt15;
1270 } else {
1271 map->s_partition_type =
1272 UDF_VIRTUAL_MAP20;
1273 map->s_partition_func =
1274 udf_get_pblock_virt20;
1276 } else if (!strncmp(upm2->partIdent.ident,
1277 UDF_ID_SPARABLE,
1278 strlen(UDF_ID_SPARABLE))) {
1279 uint32_t loc;
1280 struct sparingTable *st;
1281 struct sparablePartitionMap *spm =
1282 (struct sparablePartitionMap *)gpm;
1284 map->s_partition_type = UDF_SPARABLE_MAP15;
1285 map->s_type_specific.s_sparing.s_packet_len =
1286 le16_to_cpu(spm->packetLength);
1287 for (j = 0; j < spm->numSparingTables; j++) {
1288 struct buffer_head *bh2;
1290 loc = le32_to_cpu(
1291 spm->locSparingTable[j]);
1292 bh2 = udf_read_tagged(sb, loc, loc,
1293 &ident);
1294 map->s_type_specific.s_sparing.
1295 s_spar_map[j] = bh2;
1297 if (bh2 == NULL)
1298 continue;
1300 st = (struct sparingTable *)bh2->b_data;
1301 if (ident != 0 || strncmp(
1302 st->sparingIdent.ident,
1303 UDF_ID_SPARING,
1304 strlen(UDF_ID_SPARING))) {
1305 brelse(bh2);
1306 map->s_type_specific.s_sparing.
1307 s_spar_map[j] = NULL;
1310 map->s_partition_func = udf_get_pblock_spar15;
1311 } else {
1312 udf_debug("Unknown ident: %s\n",
1313 upm2->partIdent.ident);
1314 continue;
1316 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1317 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1319 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1320 i, map->s_partition_num, type,
1321 map->s_volumeseqnum);
1324 if (fileset) {
1325 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1327 *fileset = lelb_to_cpu(la->extLocation);
1328 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1329 "partition=%d\n", fileset->logicalBlockNum,
1330 fileset->partitionReferenceNum);
1332 if (lvd->integritySeqExt.extLength)
1333 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1335 out_bh:
1336 brelse(bh);
1337 return ret;
1341 * udf_load_logicalvolint
1344 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1346 struct buffer_head *bh = NULL;
1347 uint16_t ident;
1348 struct udf_sb_info *sbi = UDF_SB(sb);
1349 struct logicalVolIntegrityDesc *lvid;
1351 while (loc.extLength > 0 &&
1352 (bh = udf_read_tagged(sb, loc.extLocation,
1353 loc.extLocation, &ident)) &&
1354 ident == TAG_IDENT_LVID) {
1355 sbi->s_lvid_bh = bh;
1356 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1358 if (lvid->nextIntegrityExt.extLength)
1359 udf_load_logicalvolint(sb,
1360 leea_to_cpu(lvid->nextIntegrityExt));
1362 if (sbi->s_lvid_bh != bh)
1363 brelse(bh);
1364 loc.extLength -= sb->s_blocksize;
1365 loc.extLocation++;
1367 if (sbi->s_lvid_bh != bh)
1368 brelse(bh);
1372 * udf_process_sequence
1374 * PURPOSE
1375 * Process a main/reserve volume descriptor sequence.
1377 * PRE-CONDITIONS
1378 * sb Pointer to _locked_ superblock.
1379 * block First block of first extent of the sequence.
1380 * lastblock Lastblock of first extent of the sequence.
1382 * HISTORY
1383 * July 1, 1997 - Andrew E. Mileski
1384 * Written, tested, and released.
1386 static noinline int udf_process_sequence(struct super_block *sb, long block,
1387 long lastblock, kernel_lb_addr *fileset)
1389 struct buffer_head *bh = NULL;
1390 struct udf_vds_record vds[VDS_POS_LENGTH];
1391 struct udf_vds_record *curr;
1392 struct generic_desc *gd;
1393 struct volDescPtr *vdp;
1394 int done = 0;
1395 uint32_t vdsn;
1396 uint16_t ident;
1397 long next_s = 0, next_e = 0;
1399 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1402 * Read the main descriptor sequence and find which descriptors
1403 * are in it.
1405 for (; (!done && block <= lastblock); block++) {
1407 bh = udf_read_tagged(sb, block, block, &ident);
1408 if (!bh) {
1409 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1410 "sequence is corrupted or we could not read "
1411 "it.\n", (unsigned long long)block);
1412 return 1;
1415 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1416 gd = (struct generic_desc *)bh->b_data;
1417 vdsn = le32_to_cpu(gd->volDescSeqNum);
1418 switch (ident) {
1419 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1420 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1421 if (vdsn >= curr->volDescSeqNum) {
1422 curr->volDescSeqNum = vdsn;
1423 curr->block = block;
1425 break;
1426 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1427 curr = &vds[VDS_POS_VOL_DESC_PTR];
1428 if (vdsn >= curr->volDescSeqNum) {
1429 curr->volDescSeqNum = vdsn;
1430 curr->block = block;
1432 vdp = (struct volDescPtr *)bh->b_data;
1433 next_s = le32_to_cpu(
1434 vdp->nextVolDescSeqExt.extLocation);
1435 next_e = le32_to_cpu(
1436 vdp->nextVolDescSeqExt.extLength);
1437 next_e = next_e >> sb->s_blocksize_bits;
1438 next_e += next_s;
1440 break;
1441 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1442 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1443 if (vdsn >= curr->volDescSeqNum) {
1444 curr->volDescSeqNum = vdsn;
1445 curr->block = block;
1447 break;
1448 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1449 curr = &vds[VDS_POS_PARTITION_DESC];
1450 if (!curr->block)
1451 curr->block = block;
1452 break;
1453 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1454 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1455 if (vdsn >= curr->volDescSeqNum) {
1456 curr->volDescSeqNum = vdsn;
1457 curr->block = block;
1459 break;
1460 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1461 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1462 if (vdsn >= curr->volDescSeqNum) {
1463 curr->volDescSeqNum = vdsn;
1464 curr->block = block;
1466 break;
1467 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1468 vds[VDS_POS_TERMINATING_DESC].block = block;
1469 if (next_e) {
1470 block = next_s;
1471 lastblock = next_e;
1472 next_s = next_e = 0;
1473 } else
1474 done = 1;
1475 break;
1477 brelse(bh);
1480 * Now read interesting descriptors again and process them
1481 * in a suitable order
1483 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1484 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1485 return 1;
1487 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1488 return 1;
1490 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1491 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1492 return 1;
1494 if (vds[VDS_POS_PARTITION_DESC].block) {
1496 * We rescan the whole descriptor sequence to find
1497 * partition descriptor blocks and process them.
1499 for (block = vds[VDS_POS_PARTITION_DESC].block;
1500 block < vds[VDS_POS_TERMINATING_DESC].block;
1501 block++)
1502 if (udf_load_partdesc(sb, block))
1503 return 1;
1506 return 0;
1510 * udf_check_valid()
1512 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1514 long block;
1515 struct udf_sb_info *sbi = UDF_SB(sb);
1517 if (novrs) {
1518 udf_debug("Validity check skipped because of novrs option\n");
1519 return 0;
1521 /* Check that it is NSR02 compliant */
1522 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1523 block = udf_vrs(sb, silent);
1524 if (block == -1)
1525 udf_debug("Failed to read byte 32768. Assuming open "
1526 "disc. Skipping validity check\n");
1527 if (block && !sbi->s_last_block)
1528 sbi->s_last_block = udf_get_last_block(sb);
1529 return !block;
1532 static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
1534 struct anchorVolDescPtr *anchor;
1535 uint16_t ident;
1536 struct buffer_head *bh;
1537 long main_s, main_e, reserve_s, reserve_e;
1538 int i;
1539 struct udf_sb_info *sbi;
1541 if (!sb)
1542 return 1;
1543 sbi = UDF_SB(sb);
1545 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1546 if (!sbi->s_anchor[i])
1547 continue;
1549 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1550 &ident);
1551 if (!bh)
1552 continue;
1554 anchor = (struct anchorVolDescPtr *)bh->b_data;
1556 /* Locate the main sequence */
1557 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1558 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1559 main_e = main_e >> sb->s_blocksize_bits;
1560 main_e += main_s;
1562 /* Locate the reserve sequence */
1563 reserve_s = le32_to_cpu(
1564 anchor->reserveVolDescSeqExt.extLocation);
1565 reserve_e = le32_to_cpu(
1566 anchor->reserveVolDescSeqExt.extLength);
1567 reserve_e = reserve_e >> sb->s_blocksize_bits;
1568 reserve_e += reserve_s;
1570 brelse(bh);
1572 /* Process the main & reserve sequences */
1573 /* responsible for finding the PartitionDesc(s) */
1574 if (!(udf_process_sequence(sb, main_s, main_e,
1575 fileset) &&
1576 udf_process_sequence(sb, reserve_s, reserve_e,
1577 fileset)))
1578 break;
1581 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1582 udf_debug("No Anchor block found\n");
1583 return 1;
1585 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1587 return 0;
1590 static void udf_open_lvid(struct super_block *sb)
1592 struct udf_sb_info *sbi = UDF_SB(sb);
1593 struct buffer_head *bh = sbi->s_lvid_bh;
1594 struct logicalVolIntegrityDesc *lvid;
1595 struct logicalVolIntegrityDescImpUse *lvidiu;
1596 if (!bh)
1597 return;
1599 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1600 lvidiu = udf_sb_lvidiu(sbi);
1602 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1603 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1604 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1605 CURRENT_TIME);
1606 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1608 lvid->descTag.descCRC = cpu_to_le16(
1609 udf_crc((char *)lvid + sizeof(tag),
1610 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1612 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1613 mark_buffer_dirty(bh);
1616 static void udf_close_lvid(struct super_block *sb)
1618 struct udf_sb_info *sbi = UDF_SB(sb);
1619 struct buffer_head *bh = sbi->s_lvid_bh;
1620 struct logicalVolIntegrityDesc *lvid;
1621 struct logicalVolIntegrityDescImpUse *lvidiu;
1623 if (!bh)
1624 return;
1626 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1628 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1629 return;
1631 lvidiu = udf_sb_lvidiu(sbi);
1632 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1633 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1634 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1635 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1636 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1637 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1638 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1639 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1640 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1641 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1643 lvid->descTag.descCRC = cpu_to_le16(
1644 udf_crc((char *)lvid + sizeof(tag),
1645 le16_to_cpu(lvid->descTag.descCRCLength),
1646 0));
1648 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1649 mark_buffer_dirty(bh);
1652 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1654 int i;
1655 int nr_groups = bitmap->s_nr_groups;
1656 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1657 nr_groups);
1659 for (i = 0; i < nr_groups; i++)
1660 if (bitmap->s_block_bitmap[i])
1661 brelse(bitmap->s_block_bitmap[i]);
1663 if (size <= PAGE_SIZE)
1664 kfree(bitmap);
1665 else
1666 vfree(bitmap);
1669 static void udf_free_partition(struct udf_part_map *map)
1671 int i;
1673 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1674 iput(map->s_uspace.s_table);
1675 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1676 iput(map->s_fspace.s_table);
1677 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1678 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1679 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1680 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1681 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1682 for (i = 0; i < 4; i++)
1683 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1686 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1688 int i;
1689 struct inode *inode = NULL;
1690 struct udf_options uopt;
1691 kernel_lb_addr rootdir, fileset;
1692 struct udf_sb_info *sbi;
1694 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1695 uopt.uid = -1;
1696 uopt.gid = -1;
1697 uopt.umask = 0;
1699 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1700 if (!sbi)
1701 return -ENOMEM;
1703 sb->s_fs_info = sbi;
1705 mutex_init(&sbi->s_alloc_mutex);
1707 if (!udf_parse_options((char *)options, &uopt, false))
1708 goto error_out;
1710 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1711 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1712 udf_error(sb, "udf_read_super",
1713 "utf8 cannot be combined with iocharset\n");
1714 goto error_out;
1716 #ifdef CONFIG_UDF_NLS
1717 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1718 uopt.nls_map = load_nls_default();
1719 if (!uopt.nls_map)
1720 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1721 else
1722 udf_debug("Using default NLS map\n");
1724 #endif
1725 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1726 uopt.flags |= (1 << UDF_FLAG_UTF8);
1728 fileset.logicalBlockNum = 0xFFFFFFFF;
1729 fileset.partitionReferenceNum = 0xFFFF;
1731 sbi->s_flags = uopt.flags;
1732 sbi->s_uid = uopt.uid;
1733 sbi->s_gid = uopt.gid;
1734 sbi->s_umask = uopt.umask;
1735 sbi->s_nls_map = uopt.nls_map;
1737 /* Set the block size for all transfers */
1738 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1739 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1740 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
1741 goto error_out;
1744 if (uopt.session == 0xFFFFFFFF)
1745 sbi->s_session = udf_get_last_session(sb);
1746 else
1747 sbi->s_session = uopt.session;
1749 udf_debug("Multi-session=%d\n", sbi->s_session);
1751 sbi->s_last_block = uopt.lastblock;
1752 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1753 sbi->s_anchor[2] = uopt.anchor;
1755 if (udf_check_valid(sb, uopt.novrs, silent)) {
1756 /* read volume recognition sequences */
1757 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1758 goto error_out;
1761 udf_find_anchor(sb);
1763 /* Fill in the rest of the superblock */
1764 sb->s_op = &udf_sb_ops;
1765 sb->dq_op = NULL;
1766 sb->s_dirt = 0;
1767 sb->s_magic = UDF_SUPER_MAGIC;
1768 sb->s_time_gran = 1000;
1770 if (udf_load_sequence(sb, &fileset)) {
1771 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1772 goto error_out;
1775 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1777 if (sbi->s_lvid_bh) {
1778 struct logicalVolIntegrityDescImpUse *lvidiu =
1779 udf_sb_lvidiu(sbi);
1780 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1781 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1782 /* uint16_t maxUDFWriteRev =
1783 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1785 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1786 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1787 "(max is %x)\n",
1788 le16_to_cpu(lvidiu->minUDFReadRev),
1789 UDF_MAX_READ_VERSION);
1790 goto error_out;
1791 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1792 sb->s_flags |= MS_RDONLY;
1794 sbi->s_udfrev = minUDFWriteRev;
1796 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1797 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1798 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1799 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1802 if (!sbi->s_partitions) {
1803 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1804 goto error_out;
1807 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1808 UDF_PART_FLAG_READ_ONLY) {
1809 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1810 "forcing readonly mount\n");
1811 sb->s_flags |= MS_RDONLY;
1814 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1815 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1816 goto error_out;
1819 if (!silent) {
1820 timestamp ts;
1821 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
1822 udf_info("UDF: Mounting volume '%s', "
1823 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1824 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1825 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
1827 if (!(sb->s_flags & MS_RDONLY))
1828 udf_open_lvid(sb);
1830 /* Assign the root inode */
1831 /* assign inodes by physical block number */
1832 /* perhaps it's not extensible enough, but for now ... */
1833 inode = udf_iget(sb, rootdir);
1834 if (!inode) {
1835 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1836 "partition=%d\n",
1837 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1838 goto error_out;
1841 /* Allocate a dentry for the root inode */
1842 sb->s_root = d_alloc_root(inode);
1843 if (!sb->s_root) {
1844 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1845 iput(inode);
1846 goto error_out;
1848 sb->s_maxbytes = MAX_LFS_FILESIZE;
1849 return 0;
1851 error_out:
1852 if (sbi->s_vat_inode)
1853 iput(sbi->s_vat_inode);
1854 if (sbi->s_partitions)
1855 for (i = 0; i < sbi->s_partitions; i++)
1856 udf_free_partition(&sbi->s_partmaps[i]);
1857 #ifdef CONFIG_UDF_NLS
1858 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1859 unload_nls(sbi->s_nls_map);
1860 #endif
1861 if (!(sb->s_flags & MS_RDONLY))
1862 udf_close_lvid(sb);
1863 brelse(sbi->s_lvid_bh);
1865 kfree(sbi->s_partmaps);
1866 kfree(sbi);
1867 sb->s_fs_info = NULL;
1869 return -EINVAL;
1872 static void udf_error(struct super_block *sb, const char *function,
1873 const char *fmt, ...)
1875 va_list args;
1877 if (!(sb->s_flags & MS_RDONLY)) {
1878 /* mark sb error */
1879 sb->s_dirt = 1;
1881 va_start(args, fmt);
1882 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1883 va_end(args);
1884 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1885 sb->s_id, function, error_buf);
1888 void udf_warning(struct super_block *sb, const char *function,
1889 const char *fmt, ...)
1891 va_list args;
1893 va_start(args, fmt);
1894 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1895 va_end(args);
1896 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1897 sb->s_id, function, error_buf);
1900 static void udf_put_super(struct super_block *sb)
1902 int i;
1903 struct udf_sb_info *sbi;
1905 sbi = UDF_SB(sb);
1906 if (sbi->s_vat_inode)
1907 iput(sbi->s_vat_inode);
1908 if (sbi->s_partitions)
1909 for (i = 0; i < sbi->s_partitions; i++)
1910 udf_free_partition(&sbi->s_partmaps[i]);
1911 #ifdef CONFIG_UDF_NLS
1912 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1913 unload_nls(sbi->s_nls_map);
1914 #endif
1915 if (!(sb->s_flags & MS_RDONLY))
1916 udf_close_lvid(sb);
1917 brelse(sbi->s_lvid_bh);
1918 kfree(sbi->s_partmaps);
1919 kfree(sb->s_fs_info);
1920 sb->s_fs_info = NULL;
1923 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1925 struct super_block *sb = dentry->d_sb;
1926 struct udf_sb_info *sbi = UDF_SB(sb);
1927 struct logicalVolIntegrityDescImpUse *lvidiu;
1929 if (sbi->s_lvid_bh != NULL)
1930 lvidiu = udf_sb_lvidiu(sbi);
1931 else
1932 lvidiu = NULL;
1934 buf->f_type = UDF_SUPER_MAGIC;
1935 buf->f_bsize = sb->s_blocksize;
1936 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1937 buf->f_bfree = udf_count_free(sb);
1938 buf->f_bavail = buf->f_bfree;
1939 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1940 le32_to_cpu(lvidiu->numDirs)) : 0)
1941 + buf->f_bfree;
1942 buf->f_ffree = buf->f_bfree;
1943 /* __kernel_fsid_t f_fsid */
1944 buf->f_namelen = UDF_NAME_LEN - 2;
1946 return 0;
1949 static unsigned int udf_count_free_bitmap(struct super_block *sb,
1950 struct udf_bitmap *bitmap)
1952 struct buffer_head *bh = NULL;
1953 unsigned int accum = 0;
1954 int index;
1955 int block = 0, newblock;
1956 kernel_lb_addr loc;
1957 uint32_t bytes;
1958 uint8_t *ptr;
1959 uint16_t ident;
1960 struct spaceBitmapDesc *bm;
1962 lock_kernel();
1964 loc.logicalBlockNum = bitmap->s_extPosition;
1965 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1966 bh = udf_read_ptagged(sb, loc, 0, &ident);
1968 if (!bh) {
1969 printk(KERN_ERR "udf: udf_count_free failed\n");
1970 goto out;
1971 } else if (ident != TAG_IDENT_SBD) {
1972 brelse(bh);
1973 printk(KERN_ERR "udf: udf_count_free failed\n");
1974 goto out;
1977 bm = (struct spaceBitmapDesc *)bh->b_data;
1978 bytes = le32_to_cpu(bm->numOfBytes);
1979 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1980 ptr = (uint8_t *)bh->b_data;
1982 while (bytes > 0) {
1983 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
1984 accum += bitmap_weight((const unsigned long *)(ptr + index),
1985 cur_bytes * 8);
1986 bytes -= cur_bytes;
1987 if (bytes) {
1988 brelse(bh);
1989 newblock = udf_get_lb_pblock(sb, loc, ++block);
1990 bh = udf_tread(sb, newblock);
1991 if (!bh) {
1992 udf_debug("read failed\n");
1993 goto out;
1995 index = 0;
1996 ptr = (uint8_t *)bh->b_data;
1999 brelse(bh);
2001 out:
2002 unlock_kernel();
2004 return accum;
2007 static unsigned int udf_count_free_table(struct super_block *sb,
2008 struct inode *table)
2010 unsigned int accum = 0;
2011 uint32_t elen;
2012 kernel_lb_addr eloc;
2013 int8_t etype;
2014 struct extent_position epos;
2016 lock_kernel();
2018 epos.block = UDF_I(table)->i_location;
2019 epos.offset = sizeof(struct unallocSpaceEntry);
2020 epos.bh = NULL;
2022 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2023 accum += (elen >> table->i_sb->s_blocksize_bits);
2025 brelse(epos.bh);
2027 unlock_kernel();
2029 return accum;
2032 static unsigned int udf_count_free(struct super_block *sb)
2034 unsigned int accum = 0;
2035 struct udf_sb_info *sbi;
2036 struct udf_part_map *map;
2038 sbi = UDF_SB(sb);
2039 if (sbi->s_lvid_bh) {
2040 struct logicalVolIntegrityDesc *lvid =
2041 (struct logicalVolIntegrityDesc *)
2042 sbi->s_lvid_bh->b_data;
2043 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2044 accum = le32_to_cpu(
2045 lvid->freeSpaceTable[sbi->s_partition]);
2046 if (accum == 0xFFFFFFFF)
2047 accum = 0;
2051 if (accum)
2052 return accum;
2054 map = &sbi->s_partmaps[sbi->s_partition];
2055 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2056 accum += udf_count_free_bitmap(sb,
2057 map->s_uspace.s_bitmap);
2059 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2060 accum += udf_count_free_bitmap(sb,
2061 map->s_fspace.s_bitmap);
2063 if (accum)
2064 return accum;
2066 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2067 accum += udf_count_free_table(sb,
2068 map->s_uspace.s_table);
2070 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2071 accum += udf_count_free_table(sb,
2072 map->s_fspace.s_table);
2075 return accum;