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