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