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