Initial commit
[wrt350n-kernel.git] / fs / udf / super.c
blobf3ac4abfc9467a093ac54ebdef4283e9eaec166c
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 <asm/byteorder.h>
60 #include <linux/udf_fs.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 int udf_load_partition(struct super_block *, kernel_lb_addr *);
88 static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
89 kernel_lb_addr *);
90 static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
91 static void udf_find_anchor(struct super_block *);
92 static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
93 kernel_lb_addr *);
94 static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
95 static void udf_load_fileset(struct super_block *, struct buffer_head *,
96 kernel_lb_addr *);
97 static int udf_load_partdesc(struct super_block *, struct buffer_head *);
98 static void udf_open_lvid(struct super_block *);
99 static void udf_close_lvid(struct super_block *);
100 static unsigned int udf_count_free(struct super_block *);
101 static int udf_statfs(struct dentry *, struct kstatfs *);
102 static int udf_show_options(struct seq_file *, struct vfsmount *);
104 struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
106 struct logicalVolIntegrityDesc *lvid =
107 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
108 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
109 __u32 offset = number_of_partitions * 2 *
110 sizeof(uint32_t)/sizeof(uint8_t);
111 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
114 /* UDF filesystem type */
115 static int udf_get_sb(struct file_system_type *fs_type,
116 int flags, const char *dev_name, void *data,
117 struct vfsmount *mnt)
119 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
122 static struct file_system_type udf_fstype = {
123 .owner = THIS_MODULE,
124 .name = "udf",
125 .get_sb = udf_get_sb,
126 .kill_sb = kill_block_super,
127 .fs_flags = FS_REQUIRES_DEV,
130 static struct kmem_cache *udf_inode_cachep;
132 static struct inode *udf_alloc_inode(struct super_block *sb)
134 struct udf_inode_info *ei;
135 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
136 if (!ei)
137 return NULL;
139 ei->i_unique = 0;
140 ei->i_lenExtents = 0;
141 ei->i_next_alloc_block = 0;
142 ei->i_next_alloc_goal = 0;
143 ei->i_strat4096 = 0;
145 return &ei->vfs_inode;
148 static void udf_destroy_inode(struct inode *inode)
150 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
153 static void init_once(struct kmem_cache *cachep, void *foo)
155 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
157 ei->i_ext.i_data = NULL;
158 inode_init_once(&ei->vfs_inode);
161 static int init_inodecache(void)
163 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
164 sizeof(struct udf_inode_info),
165 0, (SLAB_RECLAIM_ACCOUNT |
166 SLAB_MEM_SPREAD),
167 init_once);
168 if (!udf_inode_cachep)
169 return -ENOMEM;
170 return 0;
173 static void destroy_inodecache(void)
175 kmem_cache_destroy(udf_inode_cachep);
178 /* Superblock operations */
179 static const struct super_operations udf_sb_ops = {
180 .alloc_inode = udf_alloc_inode,
181 .destroy_inode = udf_destroy_inode,
182 .write_inode = udf_write_inode,
183 .delete_inode = udf_delete_inode,
184 .clear_inode = udf_clear_inode,
185 .put_super = udf_put_super,
186 .write_super = udf_write_super,
187 .statfs = udf_statfs,
188 .remount_fs = udf_remount_fs,
189 .show_options = udf_show_options,
192 struct udf_options {
193 unsigned char novrs;
194 unsigned int blocksize;
195 unsigned int session;
196 unsigned int lastblock;
197 unsigned int anchor;
198 unsigned int volume;
199 unsigned short partition;
200 unsigned int fileset;
201 unsigned int rootdir;
202 unsigned int flags;
203 mode_t umask;
204 gid_t gid;
205 uid_t uid;
206 struct nls_table *nls_map;
209 static int __init init_udf_fs(void)
211 int err;
213 err = init_inodecache();
214 if (err)
215 goto out1;
216 err = register_filesystem(&udf_fstype);
217 if (err)
218 goto out;
220 return 0;
222 out:
223 destroy_inodecache();
225 out1:
226 return err;
229 static void __exit exit_udf_fs(void)
231 unregister_filesystem(&udf_fstype);
232 destroy_inodecache();
235 module_init(init_udf_fs)
236 module_exit(exit_udf_fs)
238 static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
240 struct udf_sb_info *sbi = UDF_SB(sb);
242 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
243 GFP_KERNEL);
244 if (!sbi->s_partmaps) {
245 udf_error(sb, __FUNCTION__,
246 "Unable to allocate space for %d partition maps",
247 count);
248 sbi->s_partitions = 0;
249 return -ENOMEM;
252 sbi->s_partitions = count;
253 return 0;
256 static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
258 struct super_block *sb = mnt->mnt_sb;
259 struct udf_sb_info *sbi = UDF_SB(sb);
261 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
262 seq_puts(seq, ",nostrict");
263 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
264 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
266 seq_puts(seq, ",unhide");
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
268 seq_puts(seq, ",undelete");
269 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
270 seq_puts(seq, ",noadinicb");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
272 seq_puts(seq, ",shortad");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
274 seq_puts(seq, ",uid=forget");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
276 seq_puts(seq, ",uid=ignore");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
278 seq_puts(seq, ",gid=forget");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
280 seq_puts(seq, ",gid=ignore");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
282 seq_printf(seq, ",uid=%u", sbi->s_uid);
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
284 seq_printf(seq, ",gid=%u", sbi->s_gid);
285 if (sbi->s_umask != 0)
286 seq_printf(seq, ",umask=%o", sbi->s_umask);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
288 seq_printf(seq, ",session=%u", sbi->s_session);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
290 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
292 * s_anchor[2] could be zeroed out in case there is no anchor
293 * in the specified block, but then the "anchor=N" option
294 * originally given by the user wasn't effective, so it's OK
295 * if we don't show it.
297 if (sbi->s_anchor[2] != 0)
298 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
300 * volume, partition, fileset and rootdir seem to be ignored
301 * currently
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
304 seq_puts(seq, ",utf8");
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
306 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
308 return 0;
312 * udf_parse_options
314 * PURPOSE
315 * Parse mount options.
317 * DESCRIPTION
318 * The following mount options are supported:
320 * gid= Set the default group.
321 * umask= Set the default umask.
322 * uid= Set the default user.
323 * bs= Set the block size.
324 * unhide Show otherwise hidden files.
325 * undelete Show deleted files in lists.
326 * adinicb Embed data in the inode (default)
327 * noadinicb Don't embed data in the inode
328 * shortad Use short ad's
329 * longad Use long ad's (default)
330 * nostrict Unset strict conformance
331 * iocharset= Set the NLS character set
333 * The remaining are for debugging and disaster recovery:
335 * novrs Skip volume sequence recognition
337 * The following expect a offset from 0.
339 * session= Set the CDROM session (default= last session)
340 * anchor= Override standard anchor location. (default= 256)
341 * volume= Override the VolumeDesc location. (unused)
342 * partition= Override the PartitionDesc location. (unused)
343 * lastblock= Set the last block of the filesystem/
345 * The following expect a offset from the partition root.
347 * fileset= Override the fileset block location. (unused)
348 * rootdir= Override the root directory location. (unused)
349 * WARNING: overriding the rootdir to a non-directory may
350 * yield highly unpredictable results.
352 * PRE-CONDITIONS
353 * options Pointer to mount options string.
354 * uopts Pointer to mount options variable.
356 * POST-CONDITIONS
357 * <return> 1 Mount options parsed okay.
358 * <return> 0 Error parsing mount options.
360 * HISTORY
361 * July 1, 1997 - Andrew E. Mileski
362 * Written, tested, and released.
365 enum {
366 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
367 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
368 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
369 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
370 Opt_rootdir, Opt_utf8, Opt_iocharset,
371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
374 static match_table_t tokens = {
375 {Opt_novrs, "novrs"},
376 {Opt_nostrict, "nostrict"},
377 {Opt_bs, "bs=%u"},
378 {Opt_unhide, "unhide"},
379 {Opt_undelete, "undelete"},
380 {Opt_noadinicb, "noadinicb"},
381 {Opt_adinicb, "adinicb"},
382 {Opt_shortad, "shortad"},
383 {Opt_longad, "longad"},
384 {Opt_uforget, "uid=forget"},
385 {Opt_uignore, "uid=ignore"},
386 {Opt_gforget, "gid=forget"},
387 {Opt_gignore, "gid=ignore"},
388 {Opt_gid, "gid=%u"},
389 {Opt_uid, "uid=%u"},
390 {Opt_umask, "umask=%o"},
391 {Opt_session, "session=%u"},
392 {Opt_lastblock, "lastblock=%u"},
393 {Opt_anchor, "anchor=%u"},
394 {Opt_volume, "volume=%u"},
395 {Opt_partition, "partition=%u"},
396 {Opt_fileset, "fileset=%u"},
397 {Opt_rootdir, "rootdir=%u"},
398 {Opt_utf8, "utf8"},
399 {Opt_iocharset, "iocharset=%s"},
400 {Opt_err, NULL}
403 static int udf_parse_options(char *options, struct udf_options *uopt,
404 bool remount)
406 char *p;
407 int option;
409 uopt->novrs = 0;
410 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
411 uopt->partition = 0xFFFF;
412 uopt->session = 0xFFFFFFFF;
413 uopt->lastblock = 0;
414 uopt->anchor = 0;
415 uopt->volume = 0xFFFFFFFF;
416 uopt->rootdir = 0xFFFFFFFF;
417 uopt->fileset = 0xFFFFFFFF;
418 uopt->nls_map = NULL;
420 if (!options)
421 return 1;
423 while ((p = strsep(&options, ",")) != NULL) {
424 substring_t args[MAX_OPT_ARGS];
425 int token;
426 if (!*p)
427 continue;
429 token = match_token(p, tokens, args);
430 switch (token) {
431 case Opt_novrs:
432 uopt->novrs = 1;
433 case Opt_bs:
434 if (match_int(&args[0], &option))
435 return 0;
436 uopt->blocksize = option;
437 break;
438 case Opt_unhide:
439 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
440 break;
441 case Opt_undelete:
442 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
443 break;
444 case Opt_noadinicb:
445 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_adinicb:
448 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
449 break;
450 case Opt_shortad:
451 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_longad:
454 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
455 break;
456 case Opt_gid:
457 if (match_int(args, &option))
458 return 0;
459 uopt->gid = option;
460 uopt->flags |= (1 << UDF_FLAG_GID_SET);
461 break;
462 case Opt_uid:
463 if (match_int(args, &option))
464 return 0;
465 uopt->uid = option;
466 uopt->flags |= (1 << UDF_FLAG_UID_SET);
467 break;
468 case Opt_umask:
469 if (match_octal(args, &option))
470 return 0;
471 uopt->umask = option;
472 break;
473 case Opt_nostrict:
474 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
475 break;
476 case Opt_session:
477 if (match_int(args, &option))
478 return 0;
479 uopt->session = option;
480 if (!remount)
481 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
482 break;
483 case Opt_lastblock:
484 if (match_int(args, &option))
485 return 0;
486 uopt->lastblock = option;
487 if (!remount)
488 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
489 break;
490 case Opt_anchor:
491 if (match_int(args, &option))
492 return 0;
493 uopt->anchor = option;
494 break;
495 case Opt_volume:
496 if (match_int(args, &option))
497 return 0;
498 uopt->volume = option;
499 break;
500 case Opt_partition:
501 if (match_int(args, &option))
502 return 0;
503 uopt->partition = option;
504 break;
505 case Opt_fileset:
506 if (match_int(args, &option))
507 return 0;
508 uopt->fileset = option;
509 break;
510 case Opt_rootdir:
511 if (match_int(args, &option))
512 return 0;
513 uopt->rootdir = option;
514 break;
515 case Opt_utf8:
516 uopt->flags |= (1 << UDF_FLAG_UTF8);
517 break;
518 #ifdef CONFIG_UDF_NLS
519 case Opt_iocharset:
520 uopt->nls_map = load_nls(args[0].from);
521 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
522 break;
523 #endif
524 case Opt_uignore:
525 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
526 break;
527 case Opt_uforget:
528 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
529 break;
530 case Opt_gignore:
531 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
532 break;
533 case Opt_gforget:
534 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
535 break;
536 default:
537 printk(KERN_ERR "udf: bad mount option \"%s\" "
538 "or missing value\n", p);
539 return 0;
542 return 1;
545 static void udf_write_super(struct super_block *sb)
547 lock_kernel();
549 if (!(sb->s_flags & MS_RDONLY))
550 udf_open_lvid(sb);
551 sb->s_dirt = 0;
553 unlock_kernel();
556 static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
558 struct udf_options uopt;
559 struct udf_sb_info *sbi = UDF_SB(sb);
561 uopt.flags = sbi->s_flags;
562 uopt.uid = sbi->s_uid;
563 uopt.gid = sbi->s_gid;
564 uopt.umask = sbi->s_umask;
566 if (!udf_parse_options(options, &uopt, true))
567 return -EINVAL;
569 sbi->s_flags = uopt.flags;
570 sbi->s_uid = uopt.uid;
571 sbi->s_gid = uopt.gid;
572 sbi->s_umask = uopt.umask;
574 if (sbi->s_lvid_bh) {
575 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
576 if (write_rev > UDF_MAX_WRITE_VERSION)
577 *flags |= MS_RDONLY;
580 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
581 return 0;
582 if (*flags & MS_RDONLY)
583 udf_close_lvid(sb);
584 else
585 udf_open_lvid(sb);
587 return 0;
591 * udf_set_blocksize
593 * PURPOSE
594 * Set the block size to be used in all transfers.
596 * DESCRIPTION
597 * To allow room for a DMA transfer, it is best to guess big when unsure.
598 * This routine picks 2048 bytes as the blocksize when guessing. This
599 * should be adequate until devices with larger block sizes become common.
601 * Note that the Linux kernel can currently only deal with blocksizes of
602 * 512, 1024, 2048, 4096, and 8192 bytes.
604 * PRE-CONDITIONS
605 * sb Pointer to _locked_ superblock.
607 * POST-CONDITIONS
608 * sb->s_blocksize Blocksize.
609 * sb->s_blocksize_bits log2 of blocksize.
610 * <return> 0 Blocksize is valid.
611 * <return> 1 Blocksize is invalid.
613 * HISTORY
614 * July 1, 1997 - Andrew E. Mileski
615 * Written, tested, and released.
617 static int udf_set_blocksize(struct super_block *sb, int bsize)
619 if (!sb_min_blocksize(sb, bsize)) {
620 udf_debug("Bad block size (%d)\n", bsize);
621 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
622 return 0;
625 return sb->s_blocksize;
628 static int udf_vrs(struct super_block *sb, int silent)
630 struct volStructDesc *vsd = NULL;
631 int sector = 32768;
632 int sectorsize;
633 struct buffer_head *bh = NULL;
634 int iso9660 = 0;
635 int nsr02 = 0;
636 int nsr03 = 0;
637 struct udf_sb_info *sbi;
639 /* Block size must be a multiple of 512 */
640 if (sb->s_blocksize & 511)
641 return 0;
642 sbi = UDF_SB(sb);
644 if (sb->s_blocksize < sizeof(struct volStructDesc))
645 sectorsize = sizeof(struct volStructDesc);
646 else
647 sectorsize = sb->s_blocksize;
649 sector += (sbi->s_session << sb->s_blocksize_bits);
651 udf_debug("Starting at sector %u (%ld byte sectors)\n",
652 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
653 /* Process the sequence (if applicable) */
654 for (; !nsr02 && !nsr03; sector += sectorsize) {
655 /* Read a block */
656 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
657 if (!bh)
658 break;
660 /* Look for ISO descriptors */
661 vsd = (struct volStructDesc *)(bh->b_data +
662 (sector & (sb->s_blocksize - 1)));
664 if (vsd->stdIdent[0] == 0) {
665 brelse(bh);
666 break;
667 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
668 VSD_STD_ID_LEN)) {
669 iso9660 = sector;
670 switch (vsd->structType) {
671 case 0:
672 udf_debug("ISO9660 Boot Record found\n");
673 break;
674 case 1:
675 udf_debug("ISO9660 Primary Volume Descriptor "
676 "found\n");
677 break;
678 case 2:
679 udf_debug("ISO9660 Supplementary Volume "
680 "Descriptor found\n");
681 break;
682 case 3:
683 udf_debug("ISO9660 Volume Partition Descriptor "
684 "found\n");
685 break;
686 case 255:
687 udf_debug("ISO9660 Volume Descriptor Set "
688 "Terminator found\n");
689 break;
690 default:
691 udf_debug("ISO9660 VRS (%u) found\n",
692 vsd->structType);
693 break;
695 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
696 VSD_STD_ID_LEN))
697 ; /* nothing */
698 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
699 VSD_STD_ID_LEN)) {
700 brelse(bh);
701 break;
702 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
703 VSD_STD_ID_LEN))
704 nsr02 = sector;
705 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
706 VSD_STD_ID_LEN))
707 nsr03 = sector;
708 brelse(bh);
711 if (nsr03)
712 return nsr03;
713 else if (nsr02)
714 return nsr02;
715 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
716 return -1;
717 else
718 return 0;
722 * udf_find_anchor
724 * PURPOSE
725 * Find an anchor volume descriptor.
727 * PRE-CONDITIONS
728 * sb Pointer to _locked_ superblock.
729 * lastblock Last block on media.
731 * POST-CONDITIONS
732 * <return> 1 if not found, 0 if ok
734 * HISTORY
735 * July 1, 1997 - Andrew E. Mileski
736 * Written, tested, and released.
738 static void udf_find_anchor(struct super_block *sb)
740 int lastblock;
741 struct buffer_head *bh = NULL;
742 uint16_t ident;
743 uint32_t location;
744 int i;
745 struct udf_sb_info *sbi;
747 sbi = UDF_SB(sb);
748 lastblock = sbi->s_last_block;
750 if (lastblock) {
751 int varlastblock = udf_variable_to_fixed(lastblock);
752 int last[] = { lastblock, lastblock - 2,
753 lastblock - 150, lastblock - 152,
754 varlastblock, varlastblock - 2,
755 varlastblock - 150, varlastblock - 152 };
757 lastblock = 0;
759 /* Search for an anchor volume descriptor pointer */
761 /* according to spec, anchor is in either:
762 * block 256
763 * lastblock-256
764 * lastblock
765 * however, if the disc isn't closed, it could be 512 */
767 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
768 ident = location = 0;
769 if (last[i] >= 0) {
770 bh = sb_bread(sb, last[i]);
771 if (bh) {
772 tag *t = (tag *)bh->b_data;
773 ident = le16_to_cpu(t->tagIdent);
774 location = le32_to_cpu(t->tagLocation);
775 brelse(bh);
779 if (ident == TAG_IDENT_AVDP) {
780 if (location == last[i] - sbi->s_session) {
781 lastblock = last[i] - sbi->s_session;
782 sbi->s_anchor[0] = lastblock;
783 sbi->s_anchor[1] = lastblock - 256;
784 } else if (location ==
785 udf_variable_to_fixed(last[i]) -
786 sbi->s_session) {
787 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
788 lastblock =
789 udf_variable_to_fixed(last[i]) -
790 sbi->s_session;
791 sbi->s_anchor[0] = lastblock;
792 sbi->s_anchor[1] = lastblock - 256 -
793 sbi->s_session;
794 } else {
795 udf_debug("Anchor found at block %d, "
796 "location mismatch %d.\n",
797 last[i], location);
799 } else if (ident == TAG_IDENT_FE ||
800 ident == TAG_IDENT_EFE) {
801 lastblock = last[i];
802 sbi->s_anchor[3] = 512;
803 } else {
804 ident = location = 0;
805 if (last[i] >= 256) {
806 bh = sb_bread(sb, last[i] - 256);
807 if (bh) {
808 tag *t = (tag *)bh->b_data;
809 ident = le16_to_cpu(
810 t->tagIdent);
811 location = le32_to_cpu(
812 t->tagLocation);
813 brelse(bh);
817 if (ident == TAG_IDENT_AVDP &&
818 location == last[i] - 256 -
819 sbi->s_session) {
820 lastblock = last[i];
821 sbi->s_anchor[1] = last[i] - 256;
822 } else {
823 ident = location = 0;
824 if (last[i] >= 312 + sbi->s_session) {
825 bh = sb_bread(sb,
826 last[i] - 312 -
827 sbi->s_session);
828 if (bh) {
829 tag *t = (tag *)
830 bh->b_data;
831 ident = le16_to_cpu(
832 t->tagIdent);
833 location = le32_to_cpu(
834 t->tagLocation);
835 brelse(bh);
839 if (ident == TAG_IDENT_AVDP &&
840 location == udf_variable_to_fixed(last[i]) - 256) {
841 UDF_SET_FLAG(sb,
842 UDF_FLAG_VARCONV);
843 lastblock = udf_variable_to_fixed(last[i]);
844 sbi->s_anchor[1] = lastblock - 256;
851 if (!lastblock) {
852 /* We haven't found the lastblock. check 312 */
853 bh = sb_bread(sb, 312 + sbi->s_session);
854 if (bh) {
855 tag *t = (tag *)bh->b_data;
856 ident = le16_to_cpu(t->tagIdent);
857 location = le32_to_cpu(t->tagLocation);
858 brelse(bh);
860 if (ident == TAG_IDENT_AVDP && location == 256)
861 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
865 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
866 if (sbi->s_anchor[i]) {
867 bh = udf_read_tagged(sb, sbi->s_anchor[i],
868 sbi->s_anchor[i], &ident);
869 if (!bh)
870 sbi->s_anchor[i] = 0;
871 else {
872 brelse(bh);
873 if ((ident != TAG_IDENT_AVDP) &&
874 (i || (ident != TAG_IDENT_FE &&
875 ident != TAG_IDENT_EFE)))
876 sbi->s_anchor[i] = 0;
881 sbi->s_last_block = lastblock;
884 static int udf_find_fileset(struct super_block *sb,
885 kernel_lb_addr *fileset,
886 kernel_lb_addr *root)
888 struct buffer_head *bh = NULL;
889 long lastblock;
890 uint16_t ident;
891 struct udf_sb_info *sbi;
893 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
894 fileset->partitionReferenceNum != 0xFFFF) {
895 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
897 if (!bh) {
898 return 1;
899 } else if (ident != TAG_IDENT_FSD) {
900 brelse(bh);
901 return 1;
906 sbi = UDF_SB(sb);
907 if (!bh) {
908 /* Search backwards through the partitions */
909 kernel_lb_addr newfileset;
911 /* --> cvg: FIXME - is it reasonable? */
912 return 1;
914 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
915 (newfileset.partitionReferenceNum != 0xFFFF &&
916 fileset->logicalBlockNum == 0xFFFFFFFF &&
917 fileset->partitionReferenceNum == 0xFFFF);
918 newfileset.partitionReferenceNum--) {
919 lastblock = sbi->s_partmaps
920 [newfileset.partitionReferenceNum]
921 .s_partition_len;
922 newfileset.logicalBlockNum = 0;
924 do {
925 bh = udf_read_ptagged(sb, newfileset, 0,
926 &ident);
927 if (!bh) {
928 newfileset.logicalBlockNum++;
929 continue;
932 switch (ident) {
933 case TAG_IDENT_SBD:
935 struct spaceBitmapDesc *sp;
936 sp = (struct spaceBitmapDesc *)
937 bh->b_data;
938 newfileset.logicalBlockNum += 1 +
939 ((le32_to_cpu(sp->numOfBytes) +
940 sizeof(struct spaceBitmapDesc)
941 - 1) >> sb->s_blocksize_bits);
942 brelse(bh);
943 break;
945 case TAG_IDENT_FSD:
946 *fileset = newfileset;
947 break;
948 default:
949 newfileset.logicalBlockNum++;
950 brelse(bh);
951 bh = NULL;
952 break;
954 } while (newfileset.logicalBlockNum < lastblock &&
955 fileset->logicalBlockNum == 0xFFFFFFFF &&
956 fileset->partitionReferenceNum == 0xFFFF);
960 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
961 fileset->partitionReferenceNum != 0xFFFF) && bh) {
962 udf_debug("Fileset at block=%d, partition=%d\n",
963 fileset->logicalBlockNum,
964 fileset->partitionReferenceNum);
966 sbi->s_partition = fileset->partitionReferenceNum;
967 udf_load_fileset(sb, bh, root);
968 brelse(bh);
969 return 0;
971 return 1;
974 static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
976 struct primaryVolDesc *pvoldesc;
977 time_t recording;
978 long recording_usec;
979 struct ustr instr;
980 struct ustr outstr;
982 pvoldesc = (struct primaryVolDesc *)bh->b_data;
984 if (udf_stamp_to_time(&recording, &recording_usec,
985 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
986 kernel_timestamp ts;
987 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
988 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
989 " %02u:%02u (%x)\n",
990 recording, recording_usec,
991 ts.year, ts.month, ts.day, ts.hour,
992 ts.minute, ts.typeAndTimezone);
993 UDF_SB(sb)->s_record_time.tv_sec = recording;
994 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
997 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
998 if (udf_CS0toUTF8(&outstr, &instr)) {
999 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
1000 outstr.u_len > 31 ? 31 : outstr.u_len);
1001 udf_debug("volIdent[] = '%s'\n",
1002 UDF_SB(sb)->s_volume_ident);
1005 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
1006 if (udf_CS0toUTF8(&outstr, &instr))
1007 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
1010 static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1011 kernel_lb_addr *root)
1013 struct fileSetDesc *fset;
1015 fset = (struct fileSetDesc *)bh->b_data;
1017 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1019 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1021 udf_debug("Rootdir at block=%d, partition=%d\n",
1022 root->logicalBlockNum, root->partitionReferenceNum);
1025 int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1027 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1028 return (map->s_partition_len +
1029 (sizeof(struct spaceBitmapDesc) << 3) +
1030 (sb->s_blocksize * 8) - 1) /
1031 (sb->s_blocksize * 8);
1034 static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1036 struct udf_bitmap *bitmap;
1037 int nr_groups;
1038 int size;
1040 nr_groups = udf_compute_nr_groups(sb, index);
1041 size = sizeof(struct udf_bitmap) +
1042 (sizeof(struct buffer_head *) * nr_groups);
1044 if (size <= PAGE_SIZE)
1045 bitmap = kmalloc(size, GFP_KERNEL);
1046 else
1047 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1049 if (bitmap == NULL) {
1050 udf_error(sb, __FUNCTION__,
1051 "Unable to allocate space for bitmap "
1052 "and %d buffer_head pointers", nr_groups);
1053 return NULL;
1056 memset(bitmap, 0x00, size);
1057 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1058 bitmap->s_nr_groups = nr_groups;
1059 return bitmap;
1062 static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
1064 struct partitionDesc *p;
1065 int i;
1066 struct udf_part_map *map;
1067 struct udf_sb_info *sbi;
1069 p = (struct partitionDesc *)bh->b_data;
1070 sbi = UDF_SB(sb);
1072 for (i = 0; i < sbi->s_partitions; i++) {
1073 map = &sbi->s_partmaps[i];
1074 udf_debug("Searching map: (%d == %d)\n",
1075 map->s_partition_num,
1076 le16_to_cpu(p->partitionNumber));
1077 if (map->s_partition_num ==
1078 le16_to_cpu(p->partitionNumber)) {
1079 map->s_partition_len =
1080 le32_to_cpu(p->partitionLength); /* blocks */
1081 map->s_partition_root =
1082 le32_to_cpu(p->partitionStartingLocation);
1083 if (p->accessType ==
1084 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1085 map->s_partition_flags |=
1086 UDF_PART_FLAG_READ_ONLY;
1087 if (p->accessType ==
1088 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1089 map->s_partition_flags |=
1090 UDF_PART_FLAG_WRITE_ONCE;
1091 if (p->accessType ==
1092 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1093 map->s_partition_flags |=
1094 UDF_PART_FLAG_REWRITABLE;
1095 if (p->accessType ==
1096 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1097 map->s_partition_flags |=
1098 UDF_PART_FLAG_OVERWRITABLE;
1100 if (!strcmp(p->partitionContents.ident,
1101 PD_PARTITION_CONTENTS_NSR02) ||
1102 !strcmp(p->partitionContents.ident,
1103 PD_PARTITION_CONTENTS_NSR03)) {
1104 struct partitionHeaderDesc *phd;
1106 phd = (struct partitionHeaderDesc *)
1107 (p->partitionContentsUse);
1108 if (phd->unallocSpaceTable.extLength) {
1109 kernel_lb_addr loc = {
1110 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1111 .partitionReferenceNum = i,
1114 map->s_uspace.s_table =
1115 udf_iget(sb, loc);
1116 if (!map->s_uspace.s_table) {
1117 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
1118 return 1;
1120 map->s_partition_flags |=
1121 UDF_PART_FLAG_UNALLOC_TABLE;
1122 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1123 i, map->s_uspace.s_table->i_ino);
1125 if (phd->unallocSpaceBitmap.extLength) {
1126 struct udf_bitmap *bitmap =
1127 udf_sb_alloc_bitmap(sb, i);
1128 map->s_uspace.s_bitmap = bitmap;
1129 if (bitmap != NULL) {
1130 bitmap->s_extLength =
1131 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
1132 bitmap->s_extPosition =
1133 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
1134 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1135 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1136 i, bitmap->s_extPosition);
1139 if (phd->partitionIntegrityTable.extLength)
1140 udf_debug("partitionIntegrityTable (part %d)\n", i);
1141 if (phd->freedSpaceTable.extLength) {
1142 kernel_lb_addr loc = {
1143 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1144 .partitionReferenceNum = i,
1147 map->s_fspace.s_table =
1148 udf_iget(sb, loc);
1149 if (!map->s_fspace.s_table) {
1150 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
1151 return 1;
1153 map->s_partition_flags |=
1154 UDF_PART_FLAG_FREED_TABLE;
1155 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1156 i, map->s_fspace.s_table->i_ino);
1158 if (phd->freedSpaceBitmap.extLength) {
1159 struct udf_bitmap *bitmap =
1160 udf_sb_alloc_bitmap(sb, i);
1161 map->s_fspace.s_bitmap = bitmap;
1162 if (bitmap != NULL) {
1163 bitmap->s_extLength =
1164 le32_to_cpu(phd->freedSpaceBitmap.extLength);
1165 bitmap->s_extPosition =
1166 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
1167 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1168 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1169 i, bitmap->s_extPosition);
1173 break;
1176 if (i == sbi->s_partitions)
1177 udf_debug("Partition (%d) not found in partition map\n",
1178 le16_to_cpu(p->partitionNumber));
1179 else
1180 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1181 "block length %d\n",
1182 le16_to_cpu(p->partitionNumber), i,
1183 map->s_partition_type,
1184 map->s_partition_root,
1185 map->s_partition_len);
1186 return 0;
1189 static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1190 kernel_lb_addr *fileset)
1192 struct logicalVolDesc *lvd;
1193 int i, j, offset;
1194 uint8_t type;
1195 struct udf_sb_info *sbi = UDF_SB(sb);
1196 struct genericPartitionMap *gpm;
1198 lvd = (struct logicalVolDesc *)bh->b_data;
1200 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1201 if (i != 0)
1202 return i;
1204 for (i = 0, offset = 0;
1205 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
1206 i++, offset += gpm->partitionMapLength) {
1207 struct udf_part_map *map = &sbi->s_partmaps[i];
1208 gpm = (struct genericPartitionMap *)
1209 &(lvd->partitionMaps[offset]);
1210 type = gpm->partitionMapType;
1211 if (type == 1) {
1212 struct genericPartitionMap1 *gpm1 =
1213 (struct genericPartitionMap1 *)gpm;
1214 map->s_partition_type = UDF_TYPE1_MAP15;
1215 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1216 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1217 map->s_partition_func = NULL;
1218 } else if (type == 2) {
1219 struct udfPartitionMap2 *upm2 =
1220 (struct udfPartitionMap2 *)gpm;
1221 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1222 strlen(UDF_ID_VIRTUAL))) {
1223 u16 suf =
1224 le16_to_cpu(((__le16 *)upm2->partIdent.
1225 identSuffix)[0]);
1226 if (suf == 0x0150) {
1227 map->s_partition_type =
1228 UDF_VIRTUAL_MAP15;
1229 map->s_partition_func =
1230 udf_get_pblock_virt15;
1231 } else if (suf == 0x0200) {
1232 map->s_partition_type =
1233 UDF_VIRTUAL_MAP20;
1234 map->s_partition_func =
1235 udf_get_pblock_virt20;
1237 } else if (!strncmp(upm2->partIdent.ident,
1238 UDF_ID_SPARABLE,
1239 strlen(UDF_ID_SPARABLE))) {
1240 uint32_t loc;
1241 uint16_t ident;
1242 struct sparingTable *st;
1243 struct sparablePartitionMap *spm =
1244 (struct sparablePartitionMap *)gpm;
1246 map->s_partition_type = UDF_SPARABLE_MAP15;
1247 map->s_type_specific.s_sparing.s_packet_len =
1248 le16_to_cpu(spm->packetLength);
1249 for (j = 0; j < spm->numSparingTables; j++) {
1250 struct buffer_head *bh2;
1252 loc = le32_to_cpu(
1253 spm->locSparingTable[j]);
1254 bh2 = udf_read_tagged(sb, loc, loc,
1255 &ident);
1256 map->s_type_specific.s_sparing.
1257 s_spar_map[j] = bh2;
1259 if (bh2 != NULL) {
1260 st = (struct sparingTable *)
1261 bh2->b_data;
1262 if (ident != 0 || strncmp(
1263 st->sparingIdent.ident,
1264 UDF_ID_SPARING,
1265 strlen(UDF_ID_SPARING))) {
1266 brelse(bh2);
1267 map->s_type_specific.
1268 s_sparing.
1269 s_spar_map[j] =
1270 NULL;
1274 map->s_partition_func = udf_get_pblock_spar15;
1275 } else {
1276 udf_debug("Unknown ident: %s\n",
1277 upm2->partIdent.ident);
1278 continue;
1280 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1281 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1283 udf_debug("Partition (%d:%d) type %d on volume %d\n",
1284 i, map->s_partition_num, type,
1285 map->s_volumeseqnum);
1288 if (fileset) {
1289 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1291 *fileset = lelb_to_cpu(la->extLocation);
1292 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1293 "partition=%d\n", fileset->logicalBlockNum,
1294 fileset->partitionReferenceNum);
1296 if (lvd->integritySeqExt.extLength)
1297 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1299 return 0;
1303 * udf_load_logicalvolint
1306 static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1308 struct buffer_head *bh = NULL;
1309 uint16_t ident;
1310 struct udf_sb_info *sbi = UDF_SB(sb);
1311 struct logicalVolIntegrityDesc *lvid;
1313 while (loc.extLength > 0 &&
1314 (bh = udf_read_tagged(sb, loc.extLocation,
1315 loc.extLocation, &ident)) &&
1316 ident == TAG_IDENT_LVID) {
1317 sbi->s_lvid_bh = bh;
1318 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1320 if (lvid->nextIntegrityExt.extLength)
1321 udf_load_logicalvolint(sb,
1322 leea_to_cpu(lvid->nextIntegrityExt));
1324 if (sbi->s_lvid_bh != bh)
1325 brelse(bh);
1326 loc.extLength -= sb->s_blocksize;
1327 loc.extLocation++;
1329 if (sbi->s_lvid_bh != bh)
1330 brelse(bh);
1334 * udf_process_sequence
1336 * PURPOSE
1337 * Process a main/reserve volume descriptor sequence.
1339 * PRE-CONDITIONS
1340 * sb Pointer to _locked_ superblock.
1341 * block First block of first extent of the sequence.
1342 * lastblock Lastblock of first extent of the sequence.
1344 * HISTORY
1345 * July 1, 1997 - Andrew E. Mileski
1346 * Written, tested, and released.
1348 static int udf_process_sequence(struct super_block *sb, long block,
1349 long lastblock, kernel_lb_addr *fileset)
1351 struct buffer_head *bh = NULL;
1352 struct udf_vds_record vds[VDS_POS_LENGTH];
1353 struct udf_vds_record *curr;
1354 struct generic_desc *gd;
1355 struct volDescPtr *vdp;
1356 int done = 0;
1357 int i, j;
1358 uint32_t vdsn;
1359 uint16_t ident;
1360 long next_s = 0, next_e = 0;
1362 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1364 /* Read the main descriptor sequence */
1365 for (; (!done && block <= lastblock); block++) {
1367 bh = udf_read_tagged(sb, block, block, &ident);
1368 if (!bh)
1369 break;
1371 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1372 gd = (struct generic_desc *)bh->b_data;
1373 vdsn = le32_to_cpu(gd->volDescSeqNum);
1374 switch (ident) {
1375 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1376 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1377 if (vdsn >= curr->volDescSeqNum) {
1378 curr->volDescSeqNum = vdsn;
1379 curr->block = block;
1381 break;
1382 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1383 curr = &vds[VDS_POS_VOL_DESC_PTR];
1384 if (vdsn >= curr->volDescSeqNum) {
1385 curr->volDescSeqNum = vdsn;
1386 curr->block = block;
1388 vdp = (struct volDescPtr *)bh->b_data;
1389 next_s = le32_to_cpu(
1390 vdp->nextVolDescSeqExt.extLocation);
1391 next_e = le32_to_cpu(
1392 vdp->nextVolDescSeqExt.extLength);
1393 next_e = next_e >> sb->s_blocksize_bits;
1394 next_e += next_s;
1396 break;
1397 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1398 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1399 if (vdsn >= curr->volDescSeqNum) {
1400 curr->volDescSeqNum = vdsn;
1401 curr->block = block;
1403 break;
1404 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1405 curr = &vds[VDS_POS_PARTITION_DESC];
1406 if (!curr->block)
1407 curr->block = block;
1408 break;
1409 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1410 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1411 if (vdsn >= curr->volDescSeqNum) {
1412 curr->volDescSeqNum = vdsn;
1413 curr->block = block;
1415 break;
1416 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1417 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1418 if (vdsn >= curr->volDescSeqNum) {
1419 curr->volDescSeqNum = vdsn;
1420 curr->block = block;
1422 break;
1423 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1424 vds[VDS_POS_TERMINATING_DESC].block = block;
1425 if (next_e) {
1426 block = next_s;
1427 lastblock = next_e;
1428 next_s = next_e = 0;
1429 } else
1430 done = 1;
1431 break;
1433 brelse(bh);
1435 for (i = 0; i < VDS_POS_LENGTH; i++) {
1436 if (vds[i].block) {
1437 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1438 &ident);
1440 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1441 udf_load_pvoldesc(sb, bh);
1442 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
1443 if (udf_load_logicalvol(sb, bh, fileset)) {
1444 brelse(bh);
1445 return 1;
1447 } else if (i == VDS_POS_PARTITION_DESC) {
1448 struct buffer_head *bh2 = NULL;
1449 if (udf_load_partdesc(sb, bh)) {
1450 brelse(bh);
1451 return 1;
1453 for (j = vds[i].block + 1;
1454 j < vds[VDS_POS_TERMINATING_DESC].block;
1455 j++) {
1456 bh2 = udf_read_tagged(sb, j, j, &ident);
1457 gd = (struct generic_desc *)bh2->b_data;
1458 if (ident == TAG_IDENT_PD)
1459 if (udf_load_partdesc(sb,
1460 bh2)) {
1461 brelse(bh);
1462 brelse(bh2);
1463 return 1;
1465 brelse(bh2);
1468 brelse(bh);
1472 return 0;
1476 * udf_check_valid()
1478 static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1480 long block;
1482 if (novrs) {
1483 udf_debug("Validity check skipped because of novrs option\n");
1484 return 0;
1486 /* Check that it is NSR02 compliant */
1487 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
1488 else {
1489 block = udf_vrs(sb, silent);
1490 if (block == -1) {
1491 struct udf_sb_info *sbi = UDF_SB(sb);
1492 udf_debug("Failed to read byte 32768. Assuming open "
1493 "disc. Skipping validity check\n");
1494 if (!sbi->s_last_block)
1495 sbi->s_last_block = udf_get_last_block(sb);
1496 return 0;
1497 } else
1498 return !block;
1502 static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1504 struct anchorVolDescPtr *anchor;
1505 uint16_t ident;
1506 struct buffer_head *bh;
1507 long main_s, main_e, reserve_s, reserve_e;
1508 int i, j;
1509 struct udf_sb_info *sbi;
1511 if (!sb)
1512 return 1;
1513 sbi = UDF_SB(sb);
1515 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1516 if (!sbi->s_anchor[i])
1517 continue;
1518 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1519 &ident);
1520 if (!bh)
1521 continue;
1523 anchor = (struct anchorVolDescPtr *)bh->b_data;
1525 /* Locate the main sequence */
1526 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1527 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1528 main_e = main_e >> sb->s_blocksize_bits;
1529 main_e += main_s;
1531 /* Locate the reserve sequence */
1532 reserve_s = le32_to_cpu(
1533 anchor->reserveVolDescSeqExt.extLocation);
1534 reserve_e = le32_to_cpu(
1535 anchor->reserveVolDescSeqExt.extLength);
1536 reserve_e = reserve_e >> sb->s_blocksize_bits;
1537 reserve_e += reserve_s;
1539 brelse(bh);
1541 /* Process the main & reserve sequences */
1542 /* responsible for finding the PartitionDesc(s) */
1543 if (!(udf_process_sequence(sb, main_s, main_e,
1544 fileset) &&
1545 udf_process_sequence(sb, reserve_s, reserve_e,
1546 fileset)))
1547 break;
1550 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1551 udf_debug("No Anchor block found\n");
1552 return 1;
1554 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1556 for (i = 0; i < sbi->s_partitions; i++) {
1557 kernel_lb_addr uninitialized_var(ino);
1558 struct udf_part_map *map = &sbi->s_partmaps[i];
1559 switch (map->s_partition_type) {
1560 case UDF_VIRTUAL_MAP15:
1561 case UDF_VIRTUAL_MAP20:
1562 if (!sbi->s_last_block) {
1563 sbi->s_last_block = udf_get_last_block(sb);
1564 udf_find_anchor(sb);
1567 if (!sbi->s_last_block) {
1568 udf_debug("Unable to determine Lastblock (For "
1569 "Virtual Partition)\n");
1570 return 1;
1573 for (j = 0; j < sbi->s_partitions; j++) {
1574 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1575 if (j != i &&
1576 map->s_volumeseqnum ==
1577 map2->s_volumeseqnum &&
1578 map->s_partition_num ==
1579 map2->s_partition_num) {
1580 ino.partitionReferenceNum = j;
1581 ino.logicalBlockNum =
1582 sbi->s_last_block -
1583 map2->s_partition_root;
1584 break;
1588 if (j == sbi->s_partitions)
1589 return 1;
1591 sbi->s_vat_inode = udf_iget(sb, ino);
1592 if (!sbi->s_vat_inode)
1593 return 1;
1595 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1596 map->s_type_specific.s_virtual.s_start_offset =
1597 udf_ext0_offset(sbi->s_vat_inode);
1598 map->s_type_specific.s_virtual.s_num_entries =
1599 (sbi->s_vat_inode->i_size - 36) >> 2;
1600 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1601 uint32_t pos;
1602 struct virtualAllocationTable20 *vat20;
1604 pos = udf_block_map(sbi->s_vat_inode, 0);
1605 bh = sb_bread(sb, pos);
1606 if (!bh)
1607 return 1;
1608 vat20 = (struct virtualAllocationTable20 *)
1609 bh->b_data +
1610 udf_ext0_offset(sbi->s_vat_inode);
1611 map->s_type_specific.s_virtual.s_start_offset =
1612 le16_to_cpu(vat20->lengthHeader) +
1613 udf_ext0_offset(sbi->s_vat_inode);
1614 map->s_type_specific.s_virtual.s_num_entries =
1615 (sbi->s_vat_inode->i_size -
1616 map->s_type_specific.s_virtual.
1617 s_start_offset) >> 2;
1618 brelse(bh);
1620 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1621 map->s_partition_len =
1622 sbi->s_partmaps[ino.partitionReferenceNum].
1623 s_partition_len;
1626 return 0;
1629 static void udf_open_lvid(struct super_block *sb)
1631 struct udf_sb_info *sbi = UDF_SB(sb);
1632 struct buffer_head *bh = sbi->s_lvid_bh;
1633 if (bh) {
1634 kernel_timestamp cpu_time;
1635 struct logicalVolIntegrityDesc *lvid =
1636 (struct logicalVolIntegrityDesc *)bh->b_data;
1637 struct logicalVolIntegrityDescImpUse *lvidiu =
1638 udf_sb_lvidiu(sbi);
1640 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1641 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1642 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1643 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1644 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1646 lvid->descTag.descCRC = cpu_to_le16(
1647 udf_crc((char *)lvid + sizeof(tag),
1648 le16_to_cpu(lvid->descTag.descCRCLength),
1649 0));
1651 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1652 mark_buffer_dirty(bh);
1656 static void udf_close_lvid(struct super_block *sb)
1658 kernel_timestamp cpu_time;
1659 struct udf_sb_info *sbi = UDF_SB(sb);
1660 struct buffer_head *bh = sbi->s_lvid_bh;
1661 struct logicalVolIntegrityDesc *lvid;
1663 if (!bh)
1664 return;
1666 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1668 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1669 struct logicalVolIntegrityDescImpUse *lvidiu =
1670 udf_sb_lvidiu(sbi);
1671 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1672 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1673 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
1674 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1675 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1676 lvidiu->maxUDFWriteRev =
1677 cpu_to_le16(UDF_MAX_WRITE_VERSION);
1678 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1679 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1680 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1681 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1682 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1684 lvid->descTag.descCRC = cpu_to_le16(
1685 udf_crc((char *)lvid + sizeof(tag),
1686 le16_to_cpu(lvid->descTag.descCRCLength),
1687 0));
1689 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1690 mark_buffer_dirty(bh);
1694 static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1696 int i;
1697 int nr_groups = bitmap->s_nr_groups;
1698 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1699 nr_groups);
1701 for (i = 0; i < nr_groups; i++)
1702 if (bitmap->s_block_bitmap[i])
1703 brelse(bitmap->s_block_bitmap[i]);
1705 if (size <= PAGE_SIZE)
1706 kfree(bitmap);
1707 else
1708 vfree(bitmap);
1712 * udf_read_super
1714 * PURPOSE
1715 * Complete the specified super block.
1717 * PRE-CONDITIONS
1718 * sb Pointer to superblock to complete - never NULL.
1719 * sb->s_dev Device to read suberblock from.
1720 * options Pointer to mount options.
1721 * silent Silent flag.
1723 * HISTORY
1724 * July 1, 1997 - Andrew E. Mileski
1725 * Written, tested, and released.
1727 static int udf_fill_super(struct super_block *sb, void *options, int silent)
1729 int i;
1730 struct inode *inode = NULL;
1731 struct udf_options uopt;
1732 kernel_lb_addr rootdir, fileset;
1733 struct udf_sb_info *sbi;
1735 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1736 uopt.uid = -1;
1737 uopt.gid = -1;
1738 uopt.umask = 0;
1740 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1741 if (!sbi)
1742 return -ENOMEM;
1744 sb->s_fs_info = sbi;
1746 mutex_init(&sbi->s_alloc_mutex);
1748 if (!udf_parse_options((char *)options, &uopt, false))
1749 goto error_out;
1751 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
1752 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1753 udf_error(sb, "udf_read_super",
1754 "utf8 cannot be combined with iocharset\n");
1755 goto error_out;
1757 #ifdef CONFIG_UDF_NLS
1758 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1759 uopt.nls_map = load_nls_default();
1760 if (!uopt.nls_map)
1761 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1762 else
1763 udf_debug("Using default NLS map\n");
1765 #endif
1766 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1767 uopt.flags |= (1 << UDF_FLAG_UTF8);
1769 fileset.logicalBlockNum = 0xFFFFFFFF;
1770 fileset.partitionReferenceNum = 0xFFFF;
1772 sbi->s_flags = uopt.flags;
1773 sbi->s_uid = uopt.uid;
1774 sbi->s_gid = uopt.gid;
1775 sbi->s_umask = uopt.umask;
1776 sbi->s_nls_map = uopt.nls_map;
1778 /* Set the block size for all transfers */
1779 if (!udf_set_blocksize(sb, uopt.blocksize))
1780 goto error_out;
1782 if (uopt.session == 0xFFFFFFFF)
1783 sbi->s_session = udf_get_last_session(sb);
1784 else
1785 sbi->s_session = uopt.session;
1787 udf_debug("Multi-session=%d\n", sbi->s_session);
1789 sbi->s_last_block = uopt.lastblock;
1790 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1791 sbi->s_anchor[2] = uopt.anchor;
1792 sbi->s_anchor[3] = 256;
1794 if (udf_check_valid(sb, uopt.novrs, silent)) {
1795 /* read volume recognition sequences */
1796 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1797 goto error_out;
1800 udf_find_anchor(sb);
1802 /* Fill in the rest of the superblock */
1803 sb->s_op = &udf_sb_ops;
1804 sb->dq_op = NULL;
1805 sb->s_dirt = 0;
1806 sb->s_magic = UDF_SUPER_MAGIC;
1807 sb->s_time_gran = 1000;
1809 if (udf_load_partition(sb, &fileset)) {
1810 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1811 goto error_out;
1814 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1816 if (sbi->s_lvid_bh) {
1817 struct logicalVolIntegrityDescImpUse *lvidiu =
1818 udf_sb_lvidiu(sbi);
1819 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1820 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1821 /* uint16_t maxUDFWriteRev =
1822 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1824 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
1825 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1826 "(max is %x)\n",
1827 le16_to_cpu(lvidiu->minUDFReadRev),
1828 UDF_MAX_READ_VERSION);
1829 goto error_out;
1830 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1831 sb->s_flags |= MS_RDONLY;
1833 sbi->s_udfrev = minUDFWriteRev;
1835 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1836 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1837 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1838 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1841 if (!sbi->s_partitions) {
1842 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1843 goto error_out;
1846 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1847 UDF_PART_FLAG_READ_ONLY) {
1848 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1849 "forcing readonly mount\n");
1850 sb->s_flags |= MS_RDONLY;
1853 if (udf_find_fileset(sb, &fileset, &rootdir)) {
1854 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1855 goto error_out;
1858 if (!silent) {
1859 kernel_timestamp ts;
1860 udf_time_to_stamp(&ts, sbi->s_record_time);
1861 udf_info("UDF: Mounting volume '%s', "
1862 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1863 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1864 ts.hour, ts.minute, ts.typeAndTimezone);
1866 if (!(sb->s_flags & MS_RDONLY))
1867 udf_open_lvid(sb);
1869 /* Assign the root inode */
1870 /* assign inodes by physical block number */
1871 /* perhaps it's not extensible enough, but for now ... */
1872 inode = udf_iget(sb, rootdir);
1873 if (!inode) {
1874 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1875 "partition=%d\n",
1876 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1877 goto error_out;
1880 /* Allocate a dentry for the root inode */
1881 sb->s_root = d_alloc_root(inode);
1882 if (!sb->s_root) {
1883 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1884 iput(inode);
1885 goto error_out;
1887 sb->s_maxbytes = MAX_LFS_FILESIZE;
1888 return 0;
1890 error_out:
1891 if (sbi->s_vat_inode)
1892 iput(sbi->s_vat_inode);
1893 if (sbi->s_partitions) {
1894 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1895 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1896 iput(map->s_uspace.s_table);
1897 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1898 iput(map->s_fspace.s_table);
1899 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1900 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1901 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1902 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1903 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1904 for (i = 0; i < 4; i++)
1905 brelse(map->s_type_specific.s_sparing.
1906 s_spar_map[i]);
1908 #ifdef CONFIG_UDF_NLS
1909 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1910 unload_nls(sbi->s_nls_map);
1911 #endif
1912 if (!(sb->s_flags & MS_RDONLY))
1913 udf_close_lvid(sb);
1914 brelse(sbi->s_lvid_bh);
1916 kfree(sbi->s_partmaps);
1917 kfree(sbi);
1918 sb->s_fs_info = NULL;
1920 return -EINVAL;
1923 void udf_error(struct super_block *sb, const char *function,
1924 const char *fmt, ...)
1926 va_list args;
1928 if (!(sb->s_flags & MS_RDONLY)) {
1929 /* mark sb error */
1930 sb->s_dirt = 1;
1932 va_start(args, fmt);
1933 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1934 va_end(args);
1935 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1936 sb->s_id, function, error_buf);
1939 void udf_warning(struct super_block *sb, const char *function,
1940 const char *fmt, ...)
1942 va_list args;
1944 va_start(args, fmt);
1945 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1946 va_end(args);
1947 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
1948 sb->s_id, function, error_buf);
1952 * udf_put_super
1954 * PURPOSE
1955 * Prepare for destruction of the superblock.
1957 * DESCRIPTION
1958 * Called before the filesystem is unmounted.
1960 * HISTORY
1961 * July 1, 1997 - Andrew E. Mileski
1962 * Written, tested, and released.
1964 static void udf_put_super(struct super_block *sb)
1966 int i;
1967 struct udf_sb_info *sbi;
1969 sbi = UDF_SB(sb);
1970 if (sbi->s_vat_inode)
1971 iput(sbi->s_vat_inode);
1972 if (sbi->s_partitions) {
1973 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1974 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1975 iput(map->s_uspace.s_table);
1976 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1977 iput(map->s_fspace.s_table);
1978 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1979 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1980 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1981 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1982 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1983 for (i = 0; i < 4; i++)
1984 brelse(map->s_type_specific.s_sparing.
1985 s_spar_map[i]);
1987 #ifdef CONFIG_UDF_NLS
1988 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1989 unload_nls(sbi->s_nls_map);
1990 #endif
1991 if (!(sb->s_flags & MS_RDONLY))
1992 udf_close_lvid(sb);
1993 brelse(sbi->s_lvid_bh);
1994 kfree(sbi->s_partmaps);
1995 kfree(sb->s_fs_info);
1996 sb->s_fs_info = NULL;
2000 * udf_stat_fs
2002 * PURPOSE
2003 * Return info about the filesystem.
2005 * DESCRIPTION
2006 * Called by sys_statfs()
2008 * HISTORY
2009 * July 1, 1997 - Andrew E. Mileski
2010 * Written, tested, and released.
2012 static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
2014 struct super_block *sb = dentry->d_sb;
2015 struct udf_sb_info *sbi = UDF_SB(sb);
2016 struct logicalVolIntegrityDescImpUse *lvidiu;
2018 if (sbi->s_lvid_bh != NULL)
2019 lvidiu = udf_sb_lvidiu(sbi);
2020 else
2021 lvidiu = NULL;
2023 buf->f_type = UDF_SUPER_MAGIC;
2024 buf->f_bsize = sb->s_blocksize;
2025 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
2026 buf->f_bfree = udf_count_free(sb);
2027 buf->f_bavail = buf->f_bfree;
2028 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2029 le32_to_cpu(lvidiu->numDirs)) : 0)
2030 + buf->f_bfree;
2031 buf->f_ffree = buf->f_bfree;
2032 /* __kernel_fsid_t f_fsid */
2033 buf->f_namelen = UDF_NAME_LEN - 2;
2035 return 0;
2038 static unsigned char udf_bitmap_lookup[16] = {
2039 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
2042 static unsigned int udf_count_free_bitmap(struct super_block *sb,
2043 struct udf_bitmap *bitmap)
2045 struct buffer_head *bh = NULL;
2046 unsigned int accum = 0;
2047 int index;
2048 int block = 0, newblock;
2049 kernel_lb_addr loc;
2050 uint32_t bytes;
2051 uint8_t value;
2052 uint8_t *ptr;
2053 uint16_t ident;
2054 struct spaceBitmapDesc *bm;
2056 lock_kernel();
2058 loc.logicalBlockNum = bitmap->s_extPosition;
2059 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
2060 bh = udf_read_ptagged(sb, loc, 0, &ident);
2062 if (!bh) {
2063 printk(KERN_ERR "udf: udf_count_free failed\n");
2064 goto out;
2065 } else if (ident != TAG_IDENT_SBD) {
2066 brelse(bh);
2067 printk(KERN_ERR "udf: udf_count_free failed\n");
2068 goto out;
2071 bm = (struct spaceBitmapDesc *)bh->b_data;
2072 bytes = le32_to_cpu(bm->numOfBytes);
2073 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2074 ptr = (uint8_t *)bh->b_data;
2076 while (bytes > 0) {
2077 while ((bytes > 0) && (index < sb->s_blocksize)) {
2078 value = ptr[index];
2079 accum += udf_bitmap_lookup[value & 0x0f];
2080 accum += udf_bitmap_lookup[value >> 4];
2081 index++;
2082 bytes--;
2084 if (bytes) {
2085 brelse(bh);
2086 newblock = udf_get_lb_pblock(sb, loc, ++block);
2087 bh = udf_tread(sb, newblock);
2088 if (!bh) {
2089 udf_debug("read failed\n");
2090 goto out;
2092 index = 0;
2093 ptr = (uint8_t *)bh->b_data;
2096 brelse(bh);
2098 out:
2099 unlock_kernel();
2101 return accum;
2104 static unsigned int udf_count_free_table(struct super_block *sb,
2105 struct inode *table)
2107 unsigned int accum = 0;
2108 uint32_t elen;
2109 kernel_lb_addr eloc;
2110 int8_t etype;
2111 struct extent_position epos;
2113 lock_kernel();
2115 epos.block = UDF_I(table)->i_location;
2116 epos.offset = sizeof(struct unallocSpaceEntry);
2117 epos.bh = NULL;
2119 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
2120 accum += (elen >> table->i_sb->s_blocksize_bits);
2122 brelse(epos.bh);
2124 unlock_kernel();
2126 return accum;
2129 static unsigned int udf_count_free(struct super_block *sb)
2131 unsigned int accum = 0;
2132 struct udf_sb_info *sbi;
2133 struct udf_part_map *map;
2135 sbi = UDF_SB(sb);
2136 if (sbi->s_lvid_bh) {
2137 struct logicalVolIntegrityDesc *lvid =
2138 (struct logicalVolIntegrityDesc *)
2139 sbi->s_lvid_bh->b_data;
2140 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
2141 accum = le32_to_cpu(
2142 lvid->freeSpaceTable[sbi->s_partition]);
2143 if (accum == 0xFFFFFFFF)
2144 accum = 0;
2148 if (accum)
2149 return accum;
2151 map = &sbi->s_partmaps[sbi->s_partition];
2152 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
2153 accum += udf_count_free_bitmap(sb,
2154 map->s_uspace.s_bitmap);
2156 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
2157 accum += udf_count_free_bitmap(sb,
2158 map->s_fspace.s_bitmap);
2160 if (accum)
2161 return accum;
2163 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
2164 accum += udf_count_free_table(sb,
2165 map->s_uspace.s_table);
2167 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
2168 accum += udf_count_free_table(sb,
2169 map->s_fspace.s_table);
2172 return accum;