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