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