]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/udf/super.c
udf: convert macros related to bitmaps to functions
[mirror_ubuntu-artful-kernel.git] / fs / udf / super.c
CommitLineData
1da177e4
LT
1/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
1da177e4
LT
17 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
3a71fc5d
MS
36 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
1da177e4
LT
38 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
cb00ea35 41#include "udfdecl.h"
1da177e4 42
1da177e4
LT
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>
dc5d39be 55#include <linux/errno.h>
1da177e4
LT
56#include <asm/byteorder.h>
57
58#include <linux/udf_fs.h>
59#include "udf_sb.h"
60#include "udf_i.h"
61
62#include <linux/init.h>
63#include <asm/uaccess.h>
64
65#define VDS_POS_PRIMARY_VOL_DESC 0
66#define VDS_POS_UNALLOC_SPACE_DESC 1
67#define VDS_POS_LOGICAL_VOL_DESC 2
68#define VDS_POS_PARTITION_DESC 3
69#define VDS_POS_IMP_USE_VOL_DESC 4
70#define VDS_POS_VOL_DESC_PTR 5
71#define VDS_POS_TERMINATING_DESC 6
72#define VDS_POS_LENGTH 7
73
74static char error_buf[1024];
75
76/* These are the "meat" - everything else is stuffing */
77static int udf_fill_super(struct super_block *, void *, int);
78static void udf_put_super(struct super_block *);
79static void udf_write_super(struct super_block *);
80static int udf_remount_fs(struct super_block *, int *, char *);
81static int udf_check_valid(struct super_block *, int, int);
82static int udf_vrs(struct super_block *sb, int silent);
83static int udf_load_partition(struct super_block *, kernel_lb_addr *);
cb00ea35
CG
84static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
85 kernel_lb_addr *);
1da177e4
LT
86static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
87static void udf_find_anchor(struct super_block *);
cb00ea35
CG
88static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
89 kernel_lb_addr *);
1da177e4 90static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
cb00ea35
CG
91static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
bcec4477 93static int udf_load_partdesc(struct super_block *, struct buffer_head *);
1da177e4
LT
94static void udf_open_lvid(struct super_block *);
95static void udf_close_lvid(struct super_block *);
96static unsigned int udf_count_free(struct super_block *);
726c3342 97static int udf_statfs(struct dentry *, struct kstatfs *);
1da177e4 98
6c79e987
MS
99struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
100{
101 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
102 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
103 __u32 offset = number_of_partitions * 2 * sizeof(uint32_t)/sizeof(uint8_t);
104 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
105}
106
1da177e4 107/* UDF filesystem type */
454e2398 108static int udf_get_sb(struct file_system_type *fs_type,
cb00ea35
CG
109 int flags, const char *dev_name, void *data,
110 struct vfsmount *mnt)
1da177e4 111{
454e2398 112 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
1da177e4
LT
113}
114
115static struct file_system_type udf_fstype = {
28de7948
CG
116 .owner = THIS_MODULE,
117 .name = "udf",
118 .get_sb = udf_get_sb,
119 .kill_sb = kill_block_super,
120 .fs_flags = FS_REQUIRES_DEV,
1da177e4
LT
121};
122
cb00ea35 123static struct kmem_cache *udf_inode_cachep;
1da177e4
LT
124
125static struct inode *udf_alloc_inode(struct super_block *sb)
126{
127 struct udf_inode_info *ei;
3a71fc5d 128 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
1da177e4
LT
129 if (!ei)
130 return NULL;
95f8797f
DB
131
132 ei->i_unique = 0;
133 ei->i_lenExtents = 0;
134 ei->i_next_alloc_block = 0;
135 ei->i_next_alloc_goal = 0;
136 ei->i_strat4096 = 0;
137
1da177e4
LT
138 return &ei->vfs_inode;
139}
140
141static void udf_destroy_inode(struct inode *inode)
142{
143 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
144}
145
4ba9b9d0 146static void init_once(struct kmem_cache *cachep, void *foo)
1da177e4 147{
cb00ea35 148 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
1da177e4 149
a35afb83
CL
150 ei->i_ext.i_data = NULL;
151 inode_init_once(&ei->vfs_inode);
1da177e4
LT
152}
153
154static int init_inodecache(void)
155{
156 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
157 sizeof(struct udf_inode_info),
cb00ea35
CG
158 0, (SLAB_RECLAIM_ACCOUNT |
159 SLAB_MEM_SPREAD),
20c2df83 160 init_once);
28de7948 161 if (!udf_inode_cachep)
1da177e4
LT
162 return -ENOMEM;
163 return 0;
164}
165
166static void destroy_inodecache(void)
167{
1a1d92c1 168 kmem_cache_destroy(udf_inode_cachep);
1da177e4
LT
169}
170
171/* Superblock operations */
ee9b6d61 172static const struct super_operations udf_sb_ops = {
28de7948
CG
173 .alloc_inode = udf_alloc_inode,
174 .destroy_inode = udf_destroy_inode,
175 .write_inode = udf_write_inode,
176 .delete_inode = udf_delete_inode,
177 .clear_inode = udf_clear_inode,
178 .put_super = udf_put_super,
179 .write_super = udf_write_super,
180 .statfs = udf_statfs,
181 .remount_fs = udf_remount_fs,
1da177e4
LT
182};
183
cb00ea35 184struct udf_options {
1da177e4
LT
185 unsigned char novrs;
186 unsigned int blocksize;
187 unsigned int session;
188 unsigned int lastblock;
189 unsigned int anchor;
190 unsigned int volume;
191 unsigned short partition;
192 unsigned int fileset;
193 unsigned int rootdir;
194 unsigned int flags;
195 mode_t umask;
196 gid_t gid;
197 uid_t uid;
198 struct nls_table *nls_map;
199};
200
201static int __init init_udf_fs(void)
202{
203 int err;
28de7948 204
1da177e4
LT
205 err = init_inodecache();
206 if (err)
207 goto out1;
208 err = register_filesystem(&udf_fstype);
209 if (err)
210 goto out;
28de7948 211
1da177e4 212 return 0;
28de7948
CG
213
214out:
1da177e4 215 destroy_inodecache();
28de7948
CG
216
217out1:
1da177e4
LT
218 return err;
219}
220
221static void __exit exit_udf_fs(void)
222{
223 unregister_filesystem(&udf_fstype);
224 destroy_inodecache();
225}
226
227module_init(init_udf_fs)
28de7948 228module_exit(exit_udf_fs)
1da177e4 229
dc5d39be
MS
230static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
231{
232 struct udf_sb_info *sbi = UDF_SB(sb);
233
234 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
235 GFP_KERNEL);
236 if (!sbi->s_partmaps) {
237 udf_error(sb, __FUNCTION__,
238 "Unable to allocate space for %d partition maps",
239 count);
240 sbi->s_partitions = 0;
241 return -ENOMEM;
242 }
243
244 sbi->s_partitions = count;
245 return 0;
246}
247
1da177e4
LT
248/*
249 * udf_parse_options
250 *
251 * PURPOSE
252 * Parse mount options.
253 *
254 * DESCRIPTION
255 * The following mount options are supported:
256 *
257 * gid= Set the default group.
258 * umask= Set the default umask.
259 * uid= Set the default user.
260 * bs= Set the block size.
261 * unhide Show otherwise hidden files.
262 * undelete Show deleted files in lists.
263 * adinicb Embed data in the inode (default)
264 * noadinicb Don't embed data in the inode
265 * shortad Use short ad's
266 * longad Use long ad's (default)
267 * nostrict Unset strict conformance
268 * iocharset= Set the NLS character set
269 *
270 * The remaining are for debugging and disaster recovery:
271 *
28de7948 272 * novrs Skip volume sequence recognition
1da177e4
LT
273 *
274 * The following expect a offset from 0.
275 *
276 * session= Set the CDROM session (default= last session)
277 * anchor= Override standard anchor location. (default= 256)
278 * volume= Override the VolumeDesc location. (unused)
279 * partition= Override the PartitionDesc location. (unused)
280 * lastblock= Set the last block of the filesystem/
281 *
282 * The following expect a offset from the partition root.
283 *
284 * fileset= Override the fileset block location. (unused)
285 * rootdir= Override the root directory location. (unused)
286 * WARNING: overriding the rootdir to a non-directory may
287 * yield highly unpredictable results.
288 *
289 * PRE-CONDITIONS
290 * options Pointer to mount options string.
291 * uopts Pointer to mount options variable.
292 *
293 * POST-CONDITIONS
294 * <return> 1 Mount options parsed okay.
295 * <return> 0 Error parsing mount options.
296 *
297 * HISTORY
298 * July 1, 1997 - Andrew E. Mileski
299 * Written, tested, and released.
300 */
28de7948 301
1da177e4
LT
302enum {
303 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
304 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
305 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
306 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
307 Opt_rootdir, Opt_utf8, Opt_iocharset,
4d6660eb 308 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
1da177e4
LT
309};
310
311static match_table_t tokens = {
28de7948
CG
312 {Opt_novrs, "novrs"},
313 {Opt_nostrict, "nostrict"},
314 {Opt_bs, "bs=%u"},
315 {Opt_unhide, "unhide"},
316 {Opt_undelete, "undelete"},
317 {Opt_noadinicb, "noadinicb"},
318 {Opt_adinicb, "adinicb"},
319 {Opt_shortad, "shortad"},
320 {Opt_longad, "longad"},
321 {Opt_uforget, "uid=forget"},
322 {Opt_uignore, "uid=ignore"},
323 {Opt_gforget, "gid=forget"},
324 {Opt_gignore, "gid=ignore"},
325 {Opt_gid, "gid=%u"},
326 {Opt_uid, "uid=%u"},
327 {Opt_umask, "umask=%o"},
328 {Opt_session, "session=%u"},
329 {Opt_lastblock, "lastblock=%u"},
330 {Opt_anchor, "anchor=%u"},
331 {Opt_volume, "volume=%u"},
332 {Opt_partition, "partition=%u"},
333 {Opt_fileset, "fileset=%u"},
334 {Opt_rootdir, "rootdir=%u"},
335 {Opt_utf8, "utf8"},
336 {Opt_iocharset, "iocharset=%s"},
337 {Opt_err, NULL}
1da177e4
LT
338};
339
cb00ea35 340static int udf_parse_options(char *options, struct udf_options *uopt)
1da177e4
LT
341{
342 char *p;
343 int option;
344
345 uopt->novrs = 0;
346 uopt->blocksize = 2048;
347 uopt->partition = 0xFFFF;
348 uopt->session = 0xFFFFFFFF;
349 uopt->lastblock = 0;
350 uopt->anchor = 0;
351 uopt->volume = 0xFFFFFFFF;
352 uopt->rootdir = 0xFFFFFFFF;
353 uopt->fileset = 0xFFFFFFFF;
354 uopt->nls_map = NULL;
355
356 if (!options)
357 return 1;
358
cb00ea35 359 while ((p = strsep(&options, ",")) != NULL) {
1da177e4
LT
360 substring_t args[MAX_OPT_ARGS];
361 int token;
362 if (!*p)
363 continue;
364
365 token = match_token(p, tokens, args);
cb00ea35
CG
366 switch (token) {
367 case Opt_novrs:
368 uopt->novrs = 1;
369 case Opt_bs:
370 if (match_int(&args[0], &option))
371 return 0;
372 uopt->blocksize = option;
373 break;
374 case Opt_unhide:
375 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
376 break;
377 case Opt_undelete:
378 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
379 break;
380 case Opt_noadinicb:
381 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
382 break;
383 case Opt_adinicb:
384 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
385 break;
386 case Opt_shortad:
387 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
388 break;
389 case Opt_longad:
390 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
391 break;
392 case Opt_gid:
393 if (match_int(args, &option))
394 return 0;
395 uopt->gid = option;
ca76d2d8 396 uopt->flags |= (1 << UDF_FLAG_GID_SET);
cb00ea35
CG
397 break;
398 case Opt_uid:
399 if (match_int(args, &option))
400 return 0;
401 uopt->uid = option;
ca76d2d8 402 uopt->flags |= (1 << UDF_FLAG_UID_SET);
cb00ea35
CG
403 break;
404 case Opt_umask:
405 if (match_octal(args, &option))
406 return 0;
407 uopt->umask = option;
408 break;
409 case Opt_nostrict:
410 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
411 break;
412 case Opt_session:
413 if (match_int(args, &option))
414 return 0;
415 uopt->session = option;
416 break;
417 case Opt_lastblock:
418 if (match_int(args, &option))
419 return 0;
420 uopt->lastblock = option;
421 break;
422 case Opt_anchor:
423 if (match_int(args, &option))
424 return 0;
425 uopt->anchor = option;
426 break;
427 case Opt_volume:
428 if (match_int(args, &option))
429 return 0;
430 uopt->volume = option;
431 break;
432 case Opt_partition:
433 if (match_int(args, &option))
434 return 0;
435 uopt->partition = option;
436 break;
437 case Opt_fileset:
438 if (match_int(args, &option))
439 return 0;
440 uopt->fileset = option;
441 break;
442 case Opt_rootdir:
443 if (match_int(args, &option))
444 return 0;
445 uopt->rootdir = option;
446 break;
447 case Opt_utf8:
448 uopt->flags |= (1 << UDF_FLAG_UTF8);
449 break;
1da177e4 450#ifdef CONFIG_UDF_NLS
cb00ea35
CG
451 case Opt_iocharset:
452 uopt->nls_map = load_nls(args[0].from);
453 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
454 break;
1da177e4 455#endif
cb00ea35
CG
456 case Opt_uignore:
457 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
458 break;
459 case Opt_uforget:
460 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
461 break;
462 case Opt_gignore:
463 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
464 break;
465 case Opt_gforget:
466 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
467 break;
468 default:
469 printk(KERN_ERR "udf: bad mount option \"%s\" "
470 "or missing value\n", p);
1da177e4
LT
471 return 0;
472 }
473 }
474 return 1;
475}
476
cb00ea35 477void udf_write_super(struct super_block *sb)
1da177e4
LT
478{
479 lock_kernel();
28de7948 480
1da177e4
LT
481 if (!(sb->s_flags & MS_RDONLY))
482 udf_open_lvid(sb);
483 sb->s_dirt = 0;
28de7948 484
1da177e4
LT
485 unlock_kernel();
486}
487
cb00ea35 488static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
1da177e4
LT
489{
490 struct udf_options uopt;
6c79e987 491 struct udf_sb_info *sbi = UDF_SB(sb);
1da177e4 492
6c79e987
MS
493 uopt.flags = sbi->s_flags;
494 uopt.uid = sbi->s_uid;
495 uopt.gid = sbi->s_gid;
496 uopt.umask = sbi->s_umask;
1da177e4 497
cb00ea35 498 if (!udf_parse_options(options, &uopt))
1da177e4
LT
499 return -EINVAL;
500
6c79e987
MS
501 sbi->s_flags = uopt.flags;
502 sbi->s_uid = uopt.uid;
503 sbi->s_gid = uopt.gid;
504 sbi->s_umask = uopt.umask;
1da177e4 505
6c79e987
MS
506 if (sbi->s_lvid_bh) {
507 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
1da177e4
LT
508 if (write_rev > UDF_MAX_WRITE_VERSION)
509 *flags |= MS_RDONLY;
510 }
511
512 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
513 return 0;
514 if (*flags & MS_RDONLY)
515 udf_close_lvid(sb);
516 else
517 udf_open_lvid(sb);
518
519 return 0;
520}
521
522/*
523 * udf_set_blocksize
524 *
525 * PURPOSE
526 * Set the block size to be used in all transfers.
527 *
528 * DESCRIPTION
529 * To allow room for a DMA transfer, it is best to guess big when unsure.
530 * This routine picks 2048 bytes as the blocksize when guessing. This
531 * should be adequate until devices with larger block sizes become common.
532 *
533 * Note that the Linux kernel can currently only deal with blocksizes of
534 * 512, 1024, 2048, 4096, and 8192 bytes.
535 *
536 * PRE-CONDITIONS
537 * sb Pointer to _locked_ superblock.
538 *
539 * POST-CONDITIONS
540 * sb->s_blocksize Blocksize.
541 * sb->s_blocksize_bits log2 of blocksize.
542 * <return> 0 Blocksize is valid.
543 * <return> 1 Blocksize is invalid.
544 *
545 * HISTORY
546 * July 1, 1997 - Andrew E. Mileski
547 * Written, tested, and released.
548 */
cb00ea35 549static int udf_set_blocksize(struct super_block *sb, int bsize)
1da177e4
LT
550{
551 if (!sb_min_blocksize(sb, bsize)) {
552 udf_debug("Bad block size (%d)\n", bsize);
553 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
554 return 0;
555 }
28de7948 556
1da177e4
LT
557 return sb->s_blocksize;
558}
559
cb00ea35 560static int udf_vrs(struct super_block *sb, int silent)
1da177e4
LT
561{
562 struct volStructDesc *vsd = NULL;
563 int sector = 32768;
564 int sectorsize;
565 struct buffer_head *bh = NULL;
cb00ea35
CG
566 int iso9660 = 0;
567 int nsr02 = 0;
568 int nsr03 = 0;
6c79e987 569 struct udf_sb_info *sbi;
1da177e4
LT
570
571 /* Block size must be a multiple of 512 */
572 if (sb->s_blocksize & 511)
573 return 0;
6c79e987 574 sbi = UDF_SB(sb);
1da177e4
LT
575
576 if (sb->s_blocksize < sizeof(struct volStructDesc))
577 sectorsize = sizeof(struct volStructDesc);
578 else
579 sectorsize = sb->s_blocksize;
580
6c79e987 581 sector += (sbi->s_session << sb->s_blocksize_bits);
1da177e4
LT
582
583 udf_debug("Starting at sector %u (%ld byte sectors)\n",
cb00ea35 584 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
1da177e4 585 /* Process the sequence (if applicable) */
cb00ea35 586 for (; !nsr02 && !nsr03; sector += sectorsize) {
1da177e4
LT
587 /* Read a block */
588 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
589 if (!bh)
590 break;
591
592 /* Look for ISO descriptors */
593 vsd = (struct volStructDesc *)(bh->b_data +
3a71fc5d 594 (sector & (sb->s_blocksize - 1)));
1da177e4 595
cb00ea35 596 if (vsd->stdIdent[0] == 0) {
3bf25cb4 597 brelse(bh);
1da177e4 598 break;
3a71fc5d
MS
599 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
600 VSD_STD_ID_LEN)) {
1da177e4 601 iso9660 = sector;
cb00ea35
CG
602 switch (vsd->structType) {
603 case 0:
604 udf_debug("ISO9660 Boot Record found\n");
605 break;
606 case 1:
3a71fc5d
MS
607 udf_debug("ISO9660 Primary Volume Descriptor "
608 "found\n");
cb00ea35
CG
609 break;
610 case 2:
3a71fc5d
MS
611 udf_debug("ISO9660 Supplementary Volume "
612 "Descriptor found\n");
cb00ea35
CG
613 break;
614 case 3:
3a71fc5d
MS
615 udf_debug("ISO9660 Volume Partition Descriptor "
616 "found\n");
cb00ea35
CG
617 break;
618 case 255:
3a71fc5d
MS
619 udf_debug("ISO9660 Volume Descriptor Set "
620 "Terminator found\n");
cb00ea35
CG
621 break;
622 default:
623 udf_debug("ISO9660 VRS (%u) found\n",
624 vsd->structType);
625 break;
1da177e4 626 }
3a71fc5d
MS
627 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
628 VSD_STD_ID_LEN))
629 ; /* nothing */
630 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
631 VSD_STD_ID_LEN)) {
3bf25cb4 632 brelse(bh);
1da177e4 633 break;
3a71fc5d
MS
634 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
635 VSD_STD_ID_LEN))
1da177e4 636 nsr02 = sector;
3a71fc5d
MS
637 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
638 VSD_STD_ID_LEN))
1da177e4 639 nsr03 = sector;
3bf25cb4 640 brelse(bh);
1da177e4
LT
641 }
642
643 if (nsr03)
644 return nsr03;
645 else if (nsr02)
646 return nsr02;
6c79e987 647 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
1da177e4
LT
648 return -1;
649 else
650 return 0;
651}
652
653/*
654 * udf_find_anchor
655 *
656 * PURPOSE
657 * Find an anchor volume descriptor.
658 *
659 * PRE-CONDITIONS
660 * sb Pointer to _locked_ superblock.
661 * lastblock Last block on media.
662 *
663 * POST-CONDITIONS
664 * <return> 1 if not found, 0 if ok
665 *
666 * HISTORY
667 * July 1, 1997 - Andrew E. Mileski
668 * Written, tested, and released.
669 */
cb00ea35 670static void udf_find_anchor(struct super_block *sb)
1da177e4 671{
6c79e987 672 int lastblock;
1da177e4
LT
673 struct buffer_head *bh = NULL;
674 uint16_t ident;
675 uint32_t location;
676 int i;
6c79e987
MS
677 struct udf_sb_info *sbi;
678
679 sbi = UDF_SB(sb);
680 lastblock = sbi->s_last_block;
1da177e4 681
cb00ea35 682 if (lastblock) {
1da177e4 683 int varlastblock = udf_variable_to_fixed(lastblock);
28de7948
CG
684 int last[] = { lastblock, lastblock - 2,
685 lastblock - 150, lastblock - 152,
686 varlastblock, varlastblock - 2,
687 varlastblock - 150, varlastblock - 152 };
1da177e4
LT
688
689 lastblock = 0;
690
691 /* Search for an anchor volume descriptor pointer */
692
693 /* according to spec, anchor is in either:
694 * block 256
695 * lastblock-256
696 * lastblock
697 * however, if the disc isn't closed, it could be 512 */
698
e8c96f8c 699 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
3a71fc5d
MS
700 ident = location = 0;
701 if (last[i] >= 0) {
702 bh = sb_bread(sb, last[i]);
703 if (bh) {
704 tag *t = (tag *)bh->b_data;
705 ident = le16_to_cpu(t->tagIdent);
706 location = le32_to_cpu(t->tagLocation);
707 brelse(bh);
708 }
1da177e4 709 }
e8c96f8c 710
cb00ea35 711 if (ident == TAG_IDENT_AVDP) {
6c79e987
MS
712 if (location == last[i] - sbi->s_session) {
713 lastblock = last[i] - sbi->s_session;
714 sbi->s_anchor[0] = lastblock;
715 sbi->s_anchor[1] = lastblock - 256;
716 } else if (location == udf_variable_to_fixed(last[i]) - sbi->s_session) {
1da177e4 717 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
6c79e987
MS
718 lastblock = udf_variable_to_fixed(last[i]) - sbi->s_session;
719 sbi->s_anchor[0] = lastblock;
720 sbi->s_anchor[1] = lastblock - 256 - sbi->s_session;
28de7948
CG
721 } else {
722 udf_debug("Anchor found at block %d, location mismatch %d.\n",
723 last[i], location);
724 }
725 } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) {
1da177e4 726 lastblock = last[i];
6c79e987 727 sbi->s_anchor[3] = 512;
cb00ea35 728 } else {
3a71fc5d
MS
729 ident = location = 0;
730 if (last[i] >= 256) {
731 bh = sb_bread(sb, last[i] - 256);
732 if (bh) {
733 tag *t = (tag *)bh->b_data;
734 ident = le16_to_cpu(t->tagIdent);
735 location = le32_to_cpu(t->tagLocation);
736 brelse(bh);
737 }
1da177e4 738 }
cb00ea35 739
1da177e4 740 if (ident == TAG_IDENT_AVDP &&
6c79e987 741 location == last[i] - 256 - sbi->s_session) {
1da177e4 742 lastblock = last[i];
6c79e987 743 sbi->s_anchor[1] = last[i] - 256;
cb00ea35 744 } else {
3a71fc5d 745 ident = location = 0;
6c79e987
MS
746 if (last[i] >= 312 + sbi->s_session) {
747 bh = sb_bread(sb, last[i] - 312 - sbi->s_session);
3a71fc5d
MS
748 if (bh) {
749 tag *t = (tag *)bh->b_data;
750 ident = le16_to_cpu(t->tagIdent);
751 location = le32_to_cpu(t->tagLocation);
752 brelse(bh);
753 }
1da177e4 754 }
cb00ea35 755
1da177e4 756 if (ident == TAG_IDENT_AVDP &&
28de7948
CG
757 location == udf_variable_to_fixed(last[i]) - 256) {
758 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
759 lastblock = udf_variable_to_fixed(last[i]);
6c79e987 760 sbi->s_anchor[1] = lastblock - 256;
1da177e4
LT
761 }
762 }
763 }
764 }
765 }
766
cb00ea35 767 if (!lastblock) {
3a71fc5d 768 /* We haven't found the lastblock. check 312 */
6c79e987 769 bh = sb_bread(sb, 312 + sbi->s_session);
3a71fc5d
MS
770 if (bh) {
771 tag *t = (tag *)bh->b_data;
772 ident = le16_to_cpu(t->tagIdent);
773 location = le32_to_cpu(t->tagLocation);
3bf25cb4 774 brelse(bh);
1da177e4
LT
775
776 if (ident == TAG_IDENT_AVDP && location == 256)
777 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
778 }
779 }
780
6c79e987
MS
781 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
782 if (sbi->s_anchor[i]) {
783 bh = udf_read_tagged(sb, sbi->s_anchor[i],
784 sbi->s_anchor[i], &ident);
3a71fc5d 785 if (!bh)
6c79e987 786 sbi->s_anchor[i] = 0;
3a71fc5d 787 else {
3bf25cb4 788 brelse(bh);
28de7948 789 if ((ident != TAG_IDENT_AVDP) &&
3a71fc5d 790 (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE)))
6c79e987 791 sbi->s_anchor[i] = 0;
1da177e4
LT
792 }
793 }
794 }
795
6c79e987 796 sbi->s_last_block = lastblock;
1da177e4
LT
797}
798
3a71fc5d
MS
799static int udf_find_fileset(struct super_block *sb,
800 kernel_lb_addr *fileset,
801 kernel_lb_addr *root)
1da177e4
LT
802{
803 struct buffer_head *bh = NULL;
804 long lastblock;
805 uint16_t ident;
6c79e987 806 struct udf_sb_info *sbi;
1da177e4
LT
807
808 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
cb00ea35 809 fileset->partitionReferenceNum != 0xFFFF) {
1da177e4
LT
810 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
811
28de7948 812 if (!bh) {
1da177e4 813 return 1;
28de7948 814 } else if (ident != TAG_IDENT_FSD) {
3bf25cb4 815 brelse(bh);
1da177e4
LT
816 return 1;
817 }
cb00ea35 818
1da177e4
LT
819 }
820
6c79e987 821 sbi = UDF_SB(sb);
3a71fc5d
MS
822 if (!bh) {
823 /* Search backwards through the partitions */
1da177e4
LT
824 kernel_lb_addr newfileset;
825
28de7948 826/* --> cvg: FIXME - is it reasonable? */
1da177e4 827 return 1;
cb00ea35 828
6c79e987 829 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
cb00ea35
CG
830 (newfileset.partitionReferenceNum != 0xFFFF &&
831 fileset->logicalBlockNum == 0xFFFFFFFF &&
832 fileset->partitionReferenceNum == 0xFFFF);
833 newfileset.partitionReferenceNum--) {
6c79e987
MS
834 lastblock = sbi->s_partmaps
835 [newfileset.partitionReferenceNum]
836 .s_partition_len;
1da177e4
LT
837 newfileset.logicalBlockNum = 0;
838
cb00ea35 839 do {
3a71fc5d
MS
840 bh = udf_read_ptagged(sb, newfileset, 0,
841 &ident);
cb00ea35
CG
842 if (!bh) {
843 newfileset.logicalBlockNum++;
1da177e4
LT
844 continue;
845 }
846
cb00ea35
CG
847 switch (ident) {
848 case TAG_IDENT_SBD:
28de7948
CG
849 {
850 struct spaceBitmapDesc *sp;
851 sp = (struct spaceBitmapDesc *)bh->b_data;
852 newfileset.logicalBlockNum += 1 +
853 ((le32_to_cpu(sp->numOfBytes) +
854 sizeof(struct spaceBitmapDesc) - 1)
855 >> sb->s_blocksize_bits);
856 brelse(bh);
857 break;
858 }
cb00ea35 859 case TAG_IDENT_FSD:
28de7948
CG
860 *fileset = newfileset;
861 break;
cb00ea35 862 default:
28de7948
CG
863 newfileset.logicalBlockNum++;
864 brelse(bh);
865 bh = NULL;
866 break;
1da177e4 867 }
28de7948
CG
868 } while (newfileset.logicalBlockNum < lastblock &&
869 fileset->logicalBlockNum == 0xFFFFFFFF &&
870 fileset->partitionReferenceNum == 0xFFFF);
1da177e4
LT
871 }
872 }
873
874 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
cb00ea35 875 fileset->partitionReferenceNum != 0xFFFF) && bh) {
1da177e4 876 udf_debug("Fileset at block=%d, partition=%d\n",
cb00ea35
CG
877 fileset->logicalBlockNum,
878 fileset->partitionReferenceNum);
1da177e4 879
6c79e987 880 sbi->s_partition = fileset->partitionReferenceNum;
1da177e4 881 udf_load_fileset(sb, bh, root);
3bf25cb4 882 brelse(bh);
1da177e4
LT
883 return 0;
884 }
885 return 1;
886}
887
cb00ea35 888static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
1da177e4
LT
889{
890 struct primaryVolDesc *pvoldesc;
891 time_t recording;
892 long recording_usec;
893 struct ustr instr;
894 struct ustr outstr;
895
896 pvoldesc = (struct primaryVolDesc *)bh->b_data;
897
cb00ea35
CG
898 if (udf_stamp_to_time(&recording, &recording_usec,
899 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
1da177e4
LT
900 kernel_timestamp ts;
901 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
3a71fc5d
MS
902 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
903 " %02u:%02u (%x)\n",
28de7948
CG
904 recording, recording_usec,
905 ts.year, ts.month, ts.day, ts.hour,
906 ts.minute, ts.typeAndTimezone);
6c79e987
MS
907 UDF_SB(sb)->s_record_time.tv_sec = recording;
908 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
1da177e4
LT
909 }
910
cb00ea35
CG
911 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
912 if (udf_CS0toUTF8(&outstr, &instr)) {
6c79e987 913 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
1da177e4 914 outstr.u_len > 31 ? 31 : outstr.u_len);
6c79e987 915 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
1da177e4
LT
916 }
917 }
918
cb00ea35 919 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
1da177e4
LT
920 if (udf_CS0toUTF8(&outstr, &instr))
921 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
922 }
923}
924
28de7948
CG
925static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
926 kernel_lb_addr *root)
1da177e4
LT
927{
928 struct fileSetDesc *fset;
929
930 fset = (struct fileSetDesc *)bh->b_data;
931
932 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
933
6c79e987 934 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1da177e4 935
cb00ea35
CG
936 udf_debug("Rootdir at block=%d, partition=%d\n",
937 root->logicalBlockNum, root->partitionReferenceNum);
1da177e4
LT
938}
939
66e1da3f
MS
940static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
941{
942 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[index];
943 struct udf_bitmap *bitmap;
944 int nr_groups;
945 int size;
946
947 /* TODO: move calculating of nr_groups into helper function */
948 nr_groups = (map->s_partition_len +
949 (sizeof(struct spaceBitmapDesc) << 3) +
950 (sb->s_blocksize * 8) - 1) /
951 (sb->s_blocksize * 8);
952 size = sizeof(struct udf_bitmap) +
953 (sizeof(struct buffer_head *) * nr_groups);
954
955 if (size <= PAGE_SIZE)
956 bitmap = kmalloc(size, GFP_KERNEL);
957 else
958 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
959
960 if (bitmap == NULL) {
961 udf_error(sb, __FUNCTION__,
962 "Unable to allocate space for bitmap "
963 "and %d buffer_head pointers", nr_groups);
964 return NULL;
965 }
966
967 memset(bitmap, 0x00, size);
968 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
969 bitmap->s_nr_groups = nr_groups;
970 return bitmap;
971}
972
bcec4477 973static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
1da177e4
LT
974{
975 struct partitionDesc *p;
976 int i;
6c79e987
MS
977 struct udf_part_map *map;
978 struct udf_sb_info *sbi;
1da177e4
LT
979
980 p = (struct partitionDesc *)bh->b_data;
6c79e987 981 sbi = UDF_SB(sb);
1da177e4 982
6c79e987
MS
983 for (i = 0; i < sbi->s_partitions; i++) {
984 map = &sbi->s_partmaps[i];
cb00ea35 985 udf_debug("Searching map: (%d == %d)\n",
6c79e987
MS
986 map->s_partition_num, le16_to_cpu(p->partitionNumber));
987 if (map->s_partition_num == le16_to_cpu(p->partitionNumber)) {
988 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
989 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
28de7948 990 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
6c79e987 991 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
28de7948 992 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
6c79e987 993 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
28de7948 994 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
6c79e987 995 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
28de7948 996 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
6c79e987 997 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
28de7948 998
3a71fc5d
MS
999 if (!strcmp(p->partitionContents.ident,
1000 PD_PARTITION_CONTENTS_NSR02) ||
1001 !strcmp(p->partitionContents.ident,
1002 PD_PARTITION_CONTENTS_NSR03)) {
1da177e4
LT
1003 struct partitionHeaderDesc *phd;
1004
28de7948 1005 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
cb00ea35 1006 if (phd->unallocSpaceTable.extLength) {
28de7948
CG
1007 kernel_lb_addr loc = {
1008 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1009 .partitionReferenceNum = i,
1010 };
1011
6c79e987 1012 map->s_uspace.s_table =
28de7948 1013 udf_iget(sb, loc);
6c79e987 1014 if (!map->s_uspace.s_table) {
b1e7a4b1 1015 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
bcec4477
JK
1016 return 1;
1017 }
6c79e987 1018 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
28de7948 1019 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
6c79e987 1020 i, map->s_uspace.s_table->i_ino);
1da177e4 1021 }
cb00ea35 1022 if (phd->unallocSpaceBitmap.extLength) {
66e1da3f 1023 map->s_uspace.s_bitmap = udf_sb_alloc_bitmap(sb, i);
6c79e987
MS
1024 if (map->s_uspace.s_bitmap != NULL) {
1025 map->s_uspace.s_bitmap->s_extLength =
28de7948 1026 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
6c79e987 1027 map->s_uspace.s_bitmap->s_extPosition =
28de7948 1028 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
6c79e987 1029 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
28de7948 1030 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
6c79e987 1031 i, map->s_uspace.s_bitmap->s_extPosition);
1da177e4
LT
1032 }
1033 }
1034 if (phd->partitionIntegrityTable.extLength)
28de7948 1035 udf_debug("partitionIntegrityTable (part %d)\n", i);
cb00ea35 1036 if (phd->freedSpaceTable.extLength) {
28de7948
CG
1037 kernel_lb_addr loc = {
1038 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1039 .partitionReferenceNum = i,
1040 };
1041
6c79e987 1042 map->s_fspace.s_table =
28de7948 1043 udf_iget(sb, loc);
6c79e987 1044 if (!map->s_fspace.s_table) {
b1e7a4b1 1045 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
bcec4477
JK
1046 return 1;
1047 }
6c79e987 1048 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
28de7948 1049 udf_debug("freedSpaceTable (part %d) @ %ld\n",
6c79e987 1050 i, map->s_fspace.s_table->i_ino);
1da177e4 1051 }
cb00ea35 1052 if (phd->freedSpaceBitmap.extLength) {
66e1da3f 1053 map->s_fspace.s_bitmap = udf_sb_alloc_bitmap(sb, i);
6c79e987
MS
1054 if (map->s_fspace.s_bitmap != NULL) {
1055 map->s_fspace.s_bitmap->s_extLength =
28de7948 1056 le32_to_cpu(phd->freedSpaceBitmap.extLength);
6c79e987 1057 map->s_fspace.s_bitmap->s_extPosition =
28de7948 1058 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
6c79e987 1059 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
28de7948 1060 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
6c79e987 1061 i, map->s_fspace.s_bitmap->s_extPosition);
1da177e4
LT
1062 }
1063 }
1064 }
1065 break;
1066 }
1067 }
6c79e987 1068 if (i == sbi->s_partitions) {
cb00ea35
CG
1069 udf_debug("Partition (%d) not found in partition map\n",
1070 le16_to_cpu(p->partitionNumber));
1071 } else {
3a71fc5d
MS
1072 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1073 "block length %d\n",
1074 le16_to_cpu(p->partitionNumber), i,
6c79e987
MS
1075 map->s_partition_type,
1076 map->s_partition_root,
1077 map->s_partition_len);
1da177e4 1078 }
bcec4477 1079 return 0;
1da177e4
LT
1080}
1081
28de7948
CG
1082static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1083 kernel_lb_addr *fileset)
1da177e4
LT
1084{
1085 struct logicalVolDesc *lvd;
1086 int i, j, offset;
1087 uint8_t type;
6c79e987 1088 struct udf_sb_info *sbi = UDF_SB(sb);
1da177e4
LT
1089
1090 lvd = (struct logicalVolDesc *)bh->b_data;
1091
dc5d39be
MS
1092 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1093 if (i != 0)
1094 return i;
1da177e4 1095
cb00ea35 1096 for (i = 0, offset = 0;
6c79e987 1097 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
28de7948 1098 i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) {
6c79e987 1099 struct udf_part_map *map = &sbi->s_partmaps[i];
28de7948 1100 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
cb00ea35 1101 if (type == 1) {
28de7948 1102 struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
6c79e987
MS
1103 map->s_partition_type = UDF_TYPE1_MAP15;
1104 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1105 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1106 map->s_partition_func = NULL;
cb00ea35 1107 } else if (type == 2) {
28de7948
CG
1108 struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
1109 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) {
1110 if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) {
6c79e987
MS
1111 map->s_partition_type = UDF_VIRTUAL_MAP15;
1112 map->s_partition_func = udf_get_pblock_virt15;
28de7948 1113 } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) {
6c79e987
MS
1114 map->s_partition_type = UDF_VIRTUAL_MAP20;
1115 map->s_partition_func = udf_get_pblock_virt20;
1da177e4 1116 }
28de7948 1117 } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) {
1da177e4
LT
1118 uint32_t loc;
1119 uint16_t ident;
1120 struct sparingTable *st;
28de7948
CG
1121 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
1122
6c79e987
MS
1123 map->s_partition_type = UDF_SPARABLE_MAP15;
1124 map->s_type_specific.s_sparing.s_packet_len = le16_to_cpu(spm->packetLength);
cb00ea35 1125 for (j = 0; j < spm->numSparingTables; j++) {
28de7948 1126 loc = le32_to_cpu(spm->locSparingTable[j]);
6c79e987 1127 map->s_type_specific.s_sparing.s_spar_map[j] =
28de7948 1128 udf_read_tagged(sb, loc, loc, &ident);
6c79e987
MS
1129 if (map->s_type_specific.s_sparing.s_spar_map[j] != NULL) {
1130 st = (struct sparingTable *)map->s_type_specific.s_sparing.s_spar_map[j]->b_data;
28de7948
CG
1131 if (ident != 0 ||
1132 strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) {
6c79e987
MS
1133 brelse(map->s_type_specific.s_sparing.s_spar_map[j]);
1134 map->s_type_specific.s_sparing.s_spar_map[j] = NULL;
1da177e4
LT
1135 }
1136 }
1137 }
6c79e987 1138 map->s_partition_func = udf_get_pblock_spar15;
cb00ea35 1139 } else {
3a71fc5d
MS
1140 udf_debug("Unknown ident: %s\n",
1141 upm2->partIdent.ident);
1da177e4
LT
1142 continue;
1143 }
6c79e987
MS
1144 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1145 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1da177e4
LT
1146 }
1147 udf_debug("Partition (%d:%d) type %d on volume %d\n",
6c79e987
MS
1148 i, map->s_partition_num, type,
1149 map->s_volumeseqnum);
1da177e4
LT
1150 }
1151
cb00ea35 1152 if (fileset) {
28de7948 1153 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
1da177e4
LT
1154
1155 *fileset = lelb_to_cpu(la->extLocation);
3a71fc5d
MS
1156 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1157 "partition=%d\n", fileset->logicalBlockNum,
28de7948 1158 fileset->partitionReferenceNum);
1da177e4
LT
1159 }
1160 if (lvd->integritySeqExt.extLength)
1161 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
28de7948 1162
1da177e4
LT
1163 return 0;
1164}
1165
1166/*
1167 * udf_load_logicalvolint
1168 *
1169 */
cb00ea35 1170static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
1da177e4
LT
1171{
1172 struct buffer_head *bh = NULL;
1173 uint16_t ident;
6c79e987
MS
1174 struct udf_sb_info *sbi = UDF_SB(sb);
1175 struct logicalVolIntegrityDesc *lvid;
1da177e4
LT
1176
1177 while (loc.extLength > 0 &&
cb00ea35
CG
1178 (bh = udf_read_tagged(sb, loc.extLocation,
1179 loc.extLocation, &ident)) &&
1180 ident == TAG_IDENT_LVID) {
6c79e987
MS
1181 sbi->s_lvid_bh = bh;
1182 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
cb00ea35 1183
6c79e987 1184 if (lvid->nextIntegrityExt.extLength)
3a71fc5d 1185 udf_load_logicalvolint(sb,
6c79e987 1186 leea_to_cpu(lvid->nextIntegrityExt));
cb00ea35 1187
6c79e987 1188 if (sbi->s_lvid_bh != bh)
3bf25cb4 1189 brelse(bh);
1da177e4 1190 loc.extLength -= sb->s_blocksize;
cb00ea35 1191 loc.extLocation++;
1da177e4 1192 }
6c79e987 1193 if (sbi->s_lvid_bh != bh)
3bf25cb4 1194 brelse(bh);
1da177e4
LT
1195}
1196
1197/*
1198 * udf_process_sequence
1199 *
1200 * PURPOSE
1201 * Process a main/reserve volume descriptor sequence.
1202 *
1203 * PRE-CONDITIONS
1204 * sb Pointer to _locked_ superblock.
1205 * block First block of first extent of the sequence.
1206 * lastblock Lastblock of first extent of the sequence.
1207 *
1208 * HISTORY
1209 * July 1, 1997 - Andrew E. Mileski
1210 * Written, tested, and released.
1211 */
3a71fc5d
MS
1212static int udf_process_sequence(struct super_block *sb, long block,
1213 long lastblock, kernel_lb_addr *fileset)
1da177e4
LT
1214{
1215 struct buffer_head *bh = NULL;
1216 struct udf_vds_record vds[VDS_POS_LENGTH];
1217 struct generic_desc *gd;
1218 struct volDescPtr *vdp;
cb00ea35
CG
1219 int done = 0;
1220 int i, j;
1da177e4
LT
1221 uint32_t vdsn;
1222 uint16_t ident;
1223 long next_s = 0, next_e = 0;
1224
1225 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1226
1227 /* Read the main descriptor sequence */
cb00ea35 1228 for (; (!done && block <= lastblock); block++) {
1da177e4
LT
1229
1230 bh = udf_read_tagged(sb, block, block, &ident);
cb00ea35 1231 if (!bh)
1da177e4
LT
1232 break;
1233
1234 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1235 gd = (struct generic_desc *)bh->b_data;
1236 vdsn = le32_to_cpu(gd->volDescSeqNum);
cb00ea35 1237 switch (ident) {
28de7948 1238 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
cb00ea35 1239 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
28de7948 1240 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
cb00ea35
CG
1241 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1242 }
1243 break;
28de7948 1244 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
cb00ea35
CG
1245 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1246 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1247 vds[VDS_POS_VOL_DESC_PTR].block = block;
1248
1249 vdp = (struct volDescPtr *)bh->b_data;
28de7948
CG
1250 next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1251 next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
cb00ea35
CG
1252 next_e = next_e >> sb->s_blocksize_bits;
1253 next_e += next_s;
1254 }
1255 break;
28de7948 1256 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
cb00ea35 1257 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
28de7948 1258 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
cb00ea35
CG
1259 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1260 }
1261 break;
28de7948 1262 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
cb00ea35
CG
1263 if (!vds[VDS_POS_PARTITION_DESC].block)
1264 vds[VDS_POS_PARTITION_DESC].block = block;
1265 break;
28de7948 1266 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
cb00ea35 1267 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
28de7948 1268 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
cb00ea35
CG
1269 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1270 }
1271 break;
28de7948
CG
1272 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1273 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1274 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
cb00ea35
CG
1275 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1276 }
1277 break;
28de7948 1278 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
cb00ea35
CG
1279 vds[VDS_POS_TERMINATING_DESC].block = block;
1280 if (next_e) {
1281 block = next_s;
1282 lastblock = next_e;
1283 next_s = next_e = 0;
28de7948 1284 } else {
cb00ea35 1285 done = 1;
28de7948 1286 }
cb00ea35 1287 break;
1da177e4 1288 }
3bf25cb4 1289 brelse(bh);
1da177e4 1290 }
cb00ea35
CG
1291 for (i = 0; i < VDS_POS_LENGTH; i++) {
1292 if (vds[i].block) {
3a71fc5d
MS
1293 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1294 &ident);
1da177e4 1295
28de7948 1296 if (i == VDS_POS_PRIMARY_VOL_DESC) {
1da177e4 1297 udf_load_pvoldesc(sb, bh);
28de7948 1298 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
deae6cfc
MS
1299 if (udf_load_logicalvol(sb, bh, fileset)) {
1300 brelse(bh);
1301 return 1;
1302 }
28de7948 1303 } else if (i == VDS_POS_PARTITION_DESC) {
1da177e4 1304 struct buffer_head *bh2 = NULL;
bcec4477
JK
1305 if (udf_load_partdesc(sb, bh)) {
1306 brelse(bh);
1307 return 1;
1308 }
3a71fc5d
MS
1309 for (j = vds[i].block + 1;
1310 j < vds[VDS_POS_TERMINATING_DESC].block;
1311 j++) {
1da177e4
LT
1312 bh2 = udf_read_tagged(sb, j, j, &ident);
1313 gd = (struct generic_desc *)bh2->b_data;
1314 if (ident == TAG_IDENT_PD)
3a71fc5d
MS
1315 if (udf_load_partdesc(sb,
1316 bh2)) {
bcec4477
JK
1317 brelse(bh);
1318 brelse(bh2);
1319 return 1;
1320 }
3bf25cb4 1321 brelse(bh2);
1da177e4
LT
1322 }
1323 }
3bf25cb4 1324 brelse(bh);
1da177e4
LT
1325 }
1326 }
1327
1328 return 0;
1329}
1330
1331/*
1332 * udf_check_valid()
1333 */
cb00ea35 1334static int udf_check_valid(struct super_block *sb, int novrs, int silent)
1da177e4
LT
1335{
1336 long block;
1337
cb00ea35 1338 if (novrs) {
1da177e4
LT
1339 udf_debug("Validity check skipped because of novrs option\n");
1340 return 0;
1341 }
1342 /* Check that it is NSR02 compliant */
1343 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
3a71fc5d
MS
1344 else {
1345 block = udf_vrs(sb, silent);
1346 if (block == -1) {
6c79e987 1347 struct udf_sb_info *sbi = UDF_SB(sb);
3a71fc5d
MS
1348 udf_debug("Failed to read byte 32768. Assuming open "
1349 "disc. Skipping validity check\n");
6c79e987
MS
1350 if (!sbi->s_last_block)
1351 sbi->s_last_block = udf_get_last_block(sb);
3a71fc5d
MS
1352 return 0;
1353 } else
1354 return !block;
28de7948 1355 }
1da177e4
LT
1356}
1357
28de7948 1358static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
1da177e4
LT
1359{
1360 struct anchorVolDescPtr *anchor;
1361 uint16_t ident;
1362 struct buffer_head *bh;
1363 long main_s, main_e, reserve_s, reserve_e;
1364 int i, j;
6c79e987 1365 struct udf_sb_info *sbi;
1da177e4
LT
1366
1367 if (!sb)
1368 return 1;
6c79e987 1369 sbi = UDF_SB(sb);
1da177e4 1370
6c79e987
MS
1371 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1372 if (sbi->s_anchor[i] &&
1373 (bh = udf_read_tagged(sb, sbi->s_anchor[i],
1374 sbi->s_anchor[i], &ident))) {
1da177e4
LT
1375 anchor = (struct anchorVolDescPtr *)bh->b_data;
1376
1377 /* Locate the main sequence */
28de7948 1378 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
3a71fc5d 1379 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1da177e4
LT
1380 main_e = main_e >> sb->s_blocksize_bits;
1381 main_e += main_s;
e8c96f8c 1382
1da177e4 1383 /* Locate the reserve sequence */
28de7948
CG
1384 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1385 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1da177e4
LT
1386 reserve_e = reserve_e >> sb->s_blocksize_bits;
1387 reserve_e += reserve_s;
1388
3bf25cb4 1389 brelse(bh);
1da177e4
LT
1390
1391 /* Process the main & reserve sequences */
1392 /* responsible for finding the PartitionDesc(s) */
28de7948 1393 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
3a71fc5d 1394 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
1da177e4 1395 break;
1da177e4
LT
1396 }
1397 }
1398
6c79e987 1399 if (i == ARRAY_SIZE(sbi->s_anchor)) {
1da177e4
LT
1400 udf_debug("No Anchor block found\n");
1401 return 1;
e8c96f8c 1402 } else
6c79e987 1403 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
1da177e4 1404
6c79e987 1405 for (i = 0; i < sbi->s_partitions; i++) {
28de7948 1406 kernel_lb_addr uninitialized_var(ino);
6c79e987
MS
1407 struct udf_part_map *map = &sbi->s_partmaps[i];
1408 switch (map->s_partition_type) {
cb00ea35
CG
1409 case UDF_VIRTUAL_MAP15:
1410 case UDF_VIRTUAL_MAP20:
6c79e987
MS
1411 if (!sbi->s_last_block) {
1412 sbi->s_last_block = udf_get_last_block(sb);
28de7948
CG
1413 udf_find_anchor(sb);
1414 }
1da177e4 1415
6c79e987 1416 if (!sbi->s_last_block) {
28de7948 1417 udf_debug("Unable to determine Lastblock (For "
b1e7a4b1 1418 "Virtual Partition)\n");
28de7948
CG
1419 return 1;
1420 }
1da177e4 1421
6c79e987
MS
1422 for (j = 0; j < sbi->s_partitions; j++) {
1423 struct udf_part_map *map2 = &sbi->s_partmaps[j];
b1e7a4b1 1424 if (j != i &&
6c79e987
MS
1425 map->s_volumeseqnum == map2->s_volumeseqnum &&
1426 map->s_partition_num == map2->s_partition_num) {
28de7948 1427 ino.partitionReferenceNum = j;
6c79e987 1428 ino.logicalBlockNum = sbi->s_last_block - map2->s_partition_root;
28de7948 1429 break;
1da177e4 1430 }
28de7948 1431 }
1da177e4 1432
6c79e987 1433 if (j == sbi->s_partitions)
28de7948 1434 return 1;
1da177e4 1435
6c79e987
MS
1436 sbi->s_vat_inode = udf_iget(sb, ino);
1437 if (!sbi->s_vat_inode)
28de7948 1438 return 1;
1da177e4 1439
6c79e987
MS
1440 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1441 map->s_type_specific.s_virtual.s_start_offset =
1442 udf_ext0_offset(sbi->s_vat_inode);
1443 map->s_type_specific.s_virtual.s_num_entries =
1444 (sbi->s_vat_inode->i_size - 36) >> 2;
1445 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
28de7948
CG
1446 struct buffer_head *bh = NULL;
1447 uint32_t pos;
1da177e4 1448
6c79e987 1449 pos = udf_block_map(sbi->s_vat_inode, 0);
28de7948
CG
1450 bh = sb_bread(sb, pos);
1451 if (!bh)
1452 return 1;
6c79e987 1453 map->s_type_specific.s_virtual.s_start_offset =
b1e7a4b1 1454 le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data +
6c79e987
MS
1455 udf_ext0_offset(sbi->s_vat_inode))->lengthHeader) +
1456 udf_ext0_offset(sbi->s_vat_inode);
1457 map->s_type_specific.s_virtual.s_num_entries = (sbi->s_vat_inode->i_size -
1458 map->s_type_specific.s_virtual.s_start_offset) >> 2;
28de7948 1459 brelse(bh);
1da177e4 1460 }
6c79e987
MS
1461 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1462 map->s_partition_len =
1463 sbi->s_partmaps[ino.partitionReferenceNum].
1464 s_partition_len;
1da177e4
LT
1465 }
1466 }
1467 return 0;
1468}
1469
1470static void udf_open_lvid(struct super_block *sb)
1471{
6c79e987
MS
1472 struct udf_sb_info *sbi = UDF_SB(sb);
1473 struct buffer_head *bh = sbi->s_lvid_bh;
1474 if (bh) {
1da177e4
LT
1475 int i;
1476 kernel_timestamp cpu_time;
6c79e987
MS
1477 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1478 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1da177e4 1479
6c79e987
MS
1480 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1481 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1da177e4 1482 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
6c79e987
MS
1483 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1484 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1da177e4 1485
6c79e987
MS
1486 lvid->descTag.descCRC = cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1487 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1da177e4 1488
6c79e987 1489 lvid->descTag.tagChecksum = 0;
cb00ea35 1490 for (i = 0; i < 16; i++)
1da177e4 1491 if (i != 4)
6c79e987
MS
1492 lvid->descTag.tagChecksum +=
1493 ((uint8_t *) &(lvid->descTag))[i];
1da177e4 1494
6c79e987 1495 mark_buffer_dirty(bh);
1da177e4
LT
1496 }
1497}
1498
1499static void udf_close_lvid(struct super_block *sb)
1500{
28de7948
CG
1501 kernel_timestamp cpu_time;
1502 int i;
6c79e987
MS
1503 struct udf_sb_info *sbi = UDF_SB(sb);
1504 struct buffer_head *bh = sbi->s_lvid_bh;
1505 struct logicalVolIntegrityDesc *lvid;
28de7948 1506
6c79e987
MS
1507 if (!bh)
1508 return;
1509
1510 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1511
1512 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1513 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1514 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1515 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1da177e4 1516 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
6c79e987
MS
1517 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1518 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1519 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1520 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1521 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1522 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1523 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1524 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1525
1526 lvid->descTag.descCRC =
1527 cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1528 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1529
1530 lvid->descTag.tagChecksum = 0;
cb00ea35 1531 for (i = 0; i < 16; i++)
1da177e4 1532 if (i != 4)
6c79e987
MS
1533 lvid->descTag.tagChecksum +=
1534 ((uint8_t *)&(lvid->descTag))[i];
1da177e4 1535
6c79e987 1536 mark_buffer_dirty(bh);
1da177e4
LT
1537 }
1538}
1539
66e1da3f
MS
1540static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1541{
1542 int i;
1543 int nr_groups = bitmap->s_nr_groups;
1544 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) * nr_groups);
1545
1546 for (i = 0; i < nr_groups; i++)
1547 if (bitmap->s_block_bitmap[i])
1548 brelse(bitmap->s_block_bitmap[i]);
1549
1550 if (size <= PAGE_SIZE)
1551 kfree(bitmap);
1552 else
1553 vfree(bitmap);
1554}
1555
1da177e4
LT
1556/*
1557 * udf_read_super
1558 *
1559 * PURPOSE
1560 * Complete the specified super block.
1561 *
1562 * PRE-CONDITIONS
1563 * sb Pointer to superblock to complete - never NULL.
1564 * sb->s_dev Device to read suberblock from.
1565 * options Pointer to mount options.
1566 * silent Silent flag.
1567 *
1568 * HISTORY
1569 * July 1, 1997 - Andrew E. Mileski
1570 * Written, tested, and released.
1571 */
1572static int udf_fill_super(struct super_block *sb, void *options, int silent)
1573{
1574 int i;
cb00ea35 1575 struct inode *inode = NULL;
1da177e4
LT
1576 struct udf_options uopt;
1577 kernel_lb_addr rootdir, fileset;
1578 struct udf_sb_info *sbi;
1579
1580 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1581 uopt.uid = -1;
1582 uopt.gid = -1;
1583 uopt.umask = 0;
1584
6c79e987 1585 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1da177e4
LT
1586 if (!sbi)
1587 return -ENOMEM;
28de7948 1588
1da177e4 1589 sb->s_fs_info = sbi;
1da177e4 1590
1e7933de 1591 mutex_init(&sbi->s_alloc_mutex);
1da177e4
LT
1592
1593 if (!udf_parse_options((char *)options, &uopt))
1594 goto error_out;
1595
1596 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
cb00ea35 1597 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1da177e4 1598 udf_error(sb, "udf_read_super",
cb00ea35 1599 "utf8 cannot be combined with iocharset\n");
1da177e4
LT
1600 goto error_out;
1601 }
1602#ifdef CONFIG_UDF_NLS
cb00ea35 1603 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1da177e4
LT
1604 uopt.nls_map = load_nls_default();
1605 if (!uopt.nls_map)
1606 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1607 else
1608 udf_debug("Using default NLS map\n");
1609 }
1610#endif
1611 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1612 uopt.flags |= (1 << UDF_FLAG_UTF8);
1613
1614 fileset.logicalBlockNum = 0xFFFFFFFF;
1615 fileset.partitionReferenceNum = 0xFFFF;
1616
6c79e987
MS
1617 sbi->s_flags = uopt.flags;
1618 sbi->s_uid = uopt.uid;
1619 sbi->s_gid = uopt.gid;
1620 sbi->s_umask = uopt.umask;
1621 sbi->s_nls_map = uopt.nls_map;
1da177e4
LT
1622
1623 /* Set the block size for all transfers */
1624 if (!udf_set_blocksize(sb, uopt.blocksize))
1625 goto error_out;
1626
cb00ea35 1627 if (uopt.session == 0xFFFFFFFF)
6c79e987 1628 sbi->s_session = udf_get_last_session(sb);
1da177e4 1629 else
6c79e987 1630 sbi->s_session = uopt.session;
1da177e4 1631
6c79e987 1632 udf_debug("Multi-session=%d\n", sbi->s_session);
1da177e4 1633
6c79e987
MS
1634 sbi->s_last_block = uopt.lastblock;
1635 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1636 sbi->s_anchor[2] = uopt.anchor;
1637 sbi->s_anchor[3] = 256;
1da177e4 1638
3a71fc5d
MS
1639 if (udf_check_valid(sb, uopt.novrs, silent)) {
1640 /* read volume recognition sequences */
1641 printk(KERN_WARNING "UDF-fs: No VRS found\n");
cb00ea35 1642 goto error_out;
1da177e4
LT
1643 }
1644
1645 udf_find_anchor(sb);
1646
1647 /* Fill in the rest of the superblock */
1648 sb->s_op = &udf_sb_ops;
1649 sb->dq_op = NULL;
1650 sb->s_dirt = 0;
1651 sb->s_magic = UDF_SUPER_MAGIC;
1652 sb->s_time_gran = 1000;
1653
cb00ea35 1654 if (udf_load_partition(sb, &fileset)) {
3a71fc5d 1655 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1da177e4
LT
1656 goto error_out;
1657 }
1658
6c79e987 1659 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1da177e4 1660
6c79e987
MS
1661 if (sbi->s_lvid_bh) {
1662 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1663 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1664 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1665 /* uint16_t maxUDFWriteRev = le16_to_cpu(lvidiu->maxUDFWriteRev); */
1da177e4 1666
cb00ea35 1667 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
3a71fc5d 1668 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x (max is %x)\n",
6c79e987 1669 le16_to_cpu(lvidiu->minUDFReadRev),
cb00ea35 1670 UDF_MAX_READ_VERSION);
1da177e4 1671 goto error_out;
cb00ea35 1672 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
1da177e4
LT
1673 sb->s_flags |= MS_RDONLY;
1674 }
1675
6c79e987 1676 sbi->s_udfrev = minUDFWriteRev;
1da177e4
LT
1677
1678 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1679 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1680 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1681 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1682 }
1683
6c79e987 1684 if (!sbi->s_partitions) {
3a71fc5d 1685 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1da177e4
LT
1686 goto error_out;
1687 }
1688
6c79e987 1689 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags & UDF_PART_FLAG_READ_ONLY) {
3a71fc5d 1690 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; forcing readonly mount\n");
39b3f6d6 1691 sb->s_flags |= MS_RDONLY;
c1a26e7d 1692 }
39b3f6d6 1693
cb00ea35 1694 if (udf_find_fileset(sb, &fileset, &rootdir)) {
3a71fc5d 1695 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1da177e4
LT
1696 goto error_out;
1697 }
1698
cb00ea35 1699 if (!silent) {
1da177e4 1700 kernel_timestamp ts;
6c79e987 1701 udf_time_to_stamp(&ts, sbi->s_record_time);
28de7948
CG
1702 udf_info("UDF %s (%s) Mounting volume '%s', "
1703 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1704 UDFFS_VERSION, UDFFS_DATE,
6c79e987
MS
1705 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1706 ts.hour, ts.minute, ts.typeAndTimezone);
1da177e4
LT
1707 }
1708 if (!(sb->s_flags & MS_RDONLY))
1709 udf_open_lvid(sb);
1710
1711 /* Assign the root inode */
1712 /* assign inodes by physical block number */
1713 /* perhaps it's not extensible enough, but for now ... */
cb00ea35
CG
1714 inode = udf_iget(sb, rootdir);
1715 if (!inode) {
3a71fc5d 1716 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
cb00ea35 1717 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1da177e4
LT
1718 goto error_out;
1719 }
1720
1721 /* Allocate a dentry for the root inode */
1722 sb->s_root = d_alloc_root(inode);
cb00ea35 1723 if (!sb->s_root) {
3a71fc5d 1724 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1da177e4
LT
1725 iput(inode);
1726 goto error_out;
1727 }
31170b6a 1728 sb->s_maxbytes = MAX_LFS_FILESIZE;
1da177e4
LT
1729 return 0;
1730
28de7948 1731error_out:
6c79e987
MS
1732 if (sbi->s_vat_inode)
1733 iput(sbi->s_vat_inode);
1734 if (sbi->s_partitions) {
1735 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1736 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1737 iput(map->s_uspace.s_table);
1738 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1739 iput(map->s_fspace.s_table);
1740 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
66e1da3f 1741 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
6c79e987 1742 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
66e1da3f 1743 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
6c79e987 1744 if (map->s_partition_type == UDF_SPARABLE_MAP15)
cb00ea35 1745 for (i = 0; i < 4; i++)
6c79e987 1746 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1da177e4
LT
1747 }
1748#ifdef CONFIG_UDF_NLS
1749 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
6c79e987 1750 unload_nls(sbi->s_nls_map);
1da177e4
LT
1751#endif
1752 if (!(sb->s_flags & MS_RDONLY))
1753 udf_close_lvid(sb);
6c79e987
MS
1754 brelse(sbi->s_lvid_bh);
1755
1756 kfree(sbi->s_partmaps);
1da177e4
LT
1757 kfree(sbi);
1758 sb->s_fs_info = NULL;
28de7948 1759
1da177e4
LT
1760 return -EINVAL;
1761}
1762
1763void udf_error(struct super_block *sb, const char *function,
cb00ea35 1764 const char *fmt, ...)
1da177e4
LT
1765{
1766 va_list args;
1767
cb00ea35 1768 if (!(sb->s_flags & MS_RDONLY)) {
1da177e4
LT
1769 /* mark sb error */
1770 sb->s_dirt = 1;
1771 }
1772 va_start(args, fmt);
4a6e617a 1773 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1da177e4 1774 va_end(args);
3a71fc5d 1775 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
28de7948 1776 sb->s_id, function, error_buf);
1da177e4
LT
1777}
1778
1779void udf_warning(struct super_block *sb, const char *function,
cb00ea35 1780 const char *fmt, ...)
1da177e4
LT
1781{
1782 va_list args;
1783
cb00ea35 1784 va_start(args, fmt);
4a6e617a 1785 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1da177e4
LT
1786 va_end(args);
1787 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
cb00ea35 1788 sb->s_id, function, error_buf);
1da177e4
LT
1789}
1790
1791/*
1792 * udf_put_super
1793 *
1794 * PURPOSE
1795 * Prepare for destruction of the superblock.
1796 *
1797 * DESCRIPTION
1798 * Called before the filesystem is unmounted.
1799 *
1800 * HISTORY
1801 * July 1, 1997 - Andrew E. Mileski
1802 * Written, tested, and released.
1803 */
cb00ea35 1804static void udf_put_super(struct super_block *sb)
1da177e4
LT
1805{
1806 int i;
6c79e987 1807 struct udf_sb_info *sbi;
1da177e4 1808
6c79e987
MS
1809 sbi = UDF_SB(sb);
1810 if (sbi->s_vat_inode)
1811 iput(sbi->s_vat_inode);
1812 if (sbi->s_partitions) {
1813 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1814 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1815 iput(map->s_uspace.s_table);
1816 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1817 iput(map->s_fspace.s_table);
1818 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
66e1da3f 1819 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
6c79e987 1820 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
66e1da3f 1821 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
6c79e987 1822 if (map->s_partition_type == UDF_SPARABLE_MAP15)
cb00ea35 1823 for (i = 0; i < 4; i++)
6c79e987 1824 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1da177e4
LT
1825 }
1826#ifdef CONFIG_UDF_NLS
1827 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
6c79e987 1828 unload_nls(sbi->s_nls_map);
1da177e4
LT
1829#endif
1830 if (!(sb->s_flags & MS_RDONLY))
1831 udf_close_lvid(sb);
6c79e987
MS
1832 brelse(sbi->s_lvid_bh);
1833 kfree(sbi->s_partmaps);
1da177e4
LT
1834 kfree(sb->s_fs_info);
1835 sb->s_fs_info = NULL;
1836}
1837
1838/*
1839 * udf_stat_fs
1840 *
1841 * PURPOSE
1842 * Return info about the filesystem.
1843 *
1844 * DESCRIPTION
1845 * Called by sys_statfs()
1846 *
1847 * HISTORY
1848 * July 1, 1997 - Andrew E. Mileski
1849 * Written, tested, and released.
1850 */
cb00ea35 1851static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 1852{
726c3342 1853 struct super_block *sb = dentry->d_sb;
6c79e987
MS
1854 struct udf_sb_info *sbi = UDF_SB(sb);
1855 struct logicalVolIntegrityDescImpUse *lvidiu;
1856
1857 if (sbi->s_lvid_bh != NULL)
1858 lvidiu = udf_sb_lvidiu(sbi);
1859 else
1860 lvidiu = NULL;
726c3342 1861
1da177e4
LT
1862 buf->f_type = UDF_SUPER_MAGIC;
1863 buf->f_bsize = sb->s_blocksize;
6c79e987 1864 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1da177e4
LT
1865 buf->f_bfree = udf_count_free(sb);
1866 buf->f_bavail = buf->f_bfree;
6c79e987
MS
1867 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1868 le32_to_cpu(lvidiu->numDirs)) : 0)
1869 + buf->f_bfree;
1da177e4
LT
1870 buf->f_ffree = buf->f_bfree;
1871 /* __kernel_fsid_t f_fsid */
cb00ea35 1872 buf->f_namelen = UDF_NAME_LEN - 2;
1da177e4
LT
1873
1874 return 0;
1875}
1876
1877static unsigned char udf_bitmap_lookup[16] = {
1878 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1879};
1880
28de7948 1881static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
1da177e4
LT
1882{
1883 struct buffer_head *bh = NULL;
1884 unsigned int accum = 0;
1885 int index;
1886 int block = 0, newblock;
1887 kernel_lb_addr loc;
1888 uint32_t bytes;
1889 uint8_t value;
1890 uint8_t *ptr;
1891 uint16_t ident;
1892 struct spaceBitmapDesc *bm;
1893
1894 lock_kernel();
1895
1896 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 1897 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
1da177e4
LT
1898 bh = udf_read_ptagged(sb, loc, 0, &ident);
1899
cb00ea35 1900 if (!bh) {
1da177e4
LT
1901 printk(KERN_ERR "udf: udf_count_free failed\n");
1902 goto out;
cb00ea35 1903 } else if (ident != TAG_IDENT_SBD) {
3bf25cb4 1904 brelse(bh);
1da177e4
LT
1905 printk(KERN_ERR "udf: udf_count_free failed\n");
1906 goto out;
1907 }
1908
1909 bm = (struct spaceBitmapDesc *)bh->b_data;
1910 bytes = le32_to_cpu(bm->numOfBytes);
28de7948
CG
1911 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1912 ptr = (uint8_t *)bh->b_data;
1da177e4 1913
cb00ea35
CG
1914 while (bytes > 0) {
1915 while ((bytes > 0) && (index < sb->s_blocksize)) {
1da177e4 1916 value = ptr[index];
cb00ea35
CG
1917 accum += udf_bitmap_lookup[value & 0x0f];
1918 accum += udf_bitmap_lookup[value >> 4];
1da177e4
LT
1919 index++;
1920 bytes--;
1921 }
cb00ea35 1922 if (bytes) {
3bf25cb4 1923 brelse(bh);
1da177e4
LT
1924 newblock = udf_get_lb_pblock(sb, loc, ++block);
1925 bh = udf_tread(sb, newblock);
cb00ea35 1926 if (!bh) {
1da177e4
LT
1927 udf_debug("read failed\n");
1928 goto out;
1929 }
1930 index = 0;
28de7948 1931 ptr = (uint8_t *)bh->b_data;
1da177e4
LT
1932 }
1933 }
3bf25cb4 1934 brelse(bh);
1da177e4 1935
28de7948 1936out:
1da177e4
LT
1937 unlock_kernel();
1938
1939 return accum;
1940}
1941
28de7948 1942static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table)
1da177e4
LT
1943{
1944 unsigned int accum = 0;
ff116fc8
JK
1945 uint32_t elen;
1946 kernel_lb_addr eloc;
1da177e4 1947 int8_t etype;
ff116fc8 1948 struct extent_position epos;
1da177e4
LT
1949
1950 lock_kernel();
1951
ff116fc8
JK
1952 epos.block = UDF_I_LOCATION(table);
1953 epos.offset = sizeof(struct unallocSpaceEntry);
1954 epos.bh = NULL;
1da177e4 1955
3a71fc5d 1956 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
1da177e4 1957 accum += (elen >> table->i_sb->s_blocksize_bits);
3a71fc5d 1958
3bf25cb4 1959 brelse(epos.bh);
1da177e4
LT
1960
1961 unlock_kernel();
1962
1963 return accum;
1964}
cb00ea35
CG
1965
1966static unsigned int udf_count_free(struct super_block *sb)
1da177e4
LT
1967{
1968 unsigned int accum = 0;
6c79e987
MS
1969 struct udf_sb_info *sbi;
1970 struct udf_part_map *map;
1da177e4 1971
6c79e987
MS
1972 sbi = UDF_SB(sb);
1973 if (sbi->s_lvid_bh) {
1974 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
1975 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
1976 accum = le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]);
1da177e4
LT
1977 if (accum == 0xFFFFFFFF)
1978 accum = 0;
1979 }
1980 }
1981
1982 if (accum)
1983 return accum;
1984
6c79e987
MS
1985 map = &sbi->s_partmaps[sbi->s_partition];
1986 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
28de7948 1987 accum += udf_count_free_bitmap(sb,
6c79e987 1988 map->s_uspace.s_bitmap);
1da177e4 1989 }
6c79e987 1990 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
28de7948 1991 accum += udf_count_free_bitmap(sb,
6c79e987 1992 map->s_fspace.s_bitmap);
1da177e4
LT
1993 }
1994 if (accum)
1995 return accum;
1996
6c79e987 1997 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
28de7948 1998 accum += udf_count_free_table(sb,
6c79e987 1999 map->s_uspace.s_table);
1da177e4 2000 }
6c79e987 2001 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
28de7948 2002 accum += udf_count_free_table(sb,
6c79e987 2003 map->s_fspace.s_table);
1da177e4
LT
2004 }
2005
2006 return accum;
2007}