blob: 911387aa181019f943509b535e5ae21028651177 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * 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
36 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced vol descs
37 * rewrote option handling based on isofs
38 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
55#include <asm/byteorder.h>
56
57#include <linux/udf_fs.h>
58#include "udf_sb.h"
59#include "udf_i.h"
60
61#include <linux/init.h>
62#include <asm/uaccess.h>
63
64#define VDS_POS_PRIMARY_VOL_DESC 0
65#define VDS_POS_UNALLOC_SPACE_DESC 1
66#define VDS_POS_LOGICAL_VOL_DESC 2
67#define VDS_POS_PARTITION_DESC 3
68#define VDS_POS_IMP_USE_VOL_DESC 4
69#define VDS_POS_VOL_DESC_PTR 5
70#define VDS_POS_TERMINATING_DESC 6
71#define VDS_POS_LENGTH 7
72
73static char error_buf[1024];
74
75/* These are the "meat" - everything else is stuffing */
76static int udf_fill_super(struct super_block *, void *, int);
77static void udf_put_super(struct super_block *);
78static void udf_write_super(struct super_block *);
79static int udf_remount_fs(struct super_block *, int *, char *);
80static int udf_check_valid(struct super_block *, int, int);
81static int udf_vrs(struct super_block *sb, int silent);
82static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070083static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
84 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
86static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070087static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
88 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090static void udf_load_fileset(struct super_block *, struct buffer_head *,
91 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void udf_load_partdesc(struct super_block *, struct buffer_head *);
93static void udf_open_lvid(struct super_block *);
94static void udf_close_lvid(struct super_block *);
95static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070096static int udf_statfs(struct dentry *, struct kstatfs *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -070099static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700100 int flags, const char *dev_name, void *data,
101 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
David Howells454e2392006-06-23 02:02:57 -0700103 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static struct file_system_type udf_fstype = {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 .owner = THIS_MODULE,
108 .name = "udf",
109 .get_sb = udf_get_sb,
110 .kill_sb = kill_block_super,
111 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct inode *udf_alloc_inode(struct super_block *sb)
117{
118 struct udf_inode_info *ei;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700119 ei = (struct udf_inode_info *)kmem_cache_alloc(udf_inode_cachep,
120 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!ei)
122 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700123
124 ei->i_unique = 0;
125 ei->i_lenExtents = 0;
126 ei->i_next_alloc_block = 0;
127 ei->i_next_alloc_goal = 0;
128 ei->i_strat4096 = 0;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return &ei->vfs_inode;
131}
132
133static void udf_destroy_inode(struct inode *inode)
134{
135 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
136}
137
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700138static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700140 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Christoph Lametera35afb82007-05-16 22:10:57 -0700142 ei->i_ext.i_data = NULL;
143 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
146static int init_inodecache(void)
147{
148 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
149 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 0, (SLAB_RECLAIM_ACCOUNT |
151 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 init_once, NULL);
153 if (udf_inode_cachep == NULL)
154 return -ENOMEM;
155 return 0;
156}
157
158static void destroy_inodecache(void)
159{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700160 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800164static const struct super_operations udf_sb_ops = {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 .alloc_inode = udf_alloc_inode,
166 .destroy_inode = udf_destroy_inode,
167 .write_inode = udf_write_inode,
168 .delete_inode = udf_delete_inode,
169 .clear_inode = udf_clear_inode,
170 .put_super = udf_put_super,
171 .write_super = udf_write_super,
172 .statfs = udf_statfs,
173 .remount_fs = udf_remount_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700176struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 unsigned char novrs;
178 unsigned int blocksize;
179 unsigned int session;
180 unsigned int lastblock;
181 unsigned int anchor;
182 unsigned int volume;
183 unsigned short partition;
184 unsigned int fileset;
185 unsigned int rootdir;
186 unsigned int flags;
187 mode_t umask;
188 gid_t gid;
189 uid_t uid;
190 struct nls_table *nls_map;
191};
192
193static int __init init_udf_fs(void)
194{
195 int err;
196 err = init_inodecache();
197 if (err)
198 goto out1;
199 err = register_filesystem(&udf_fstype);
200 if (err)
201 goto out;
202 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 destroy_inodecache();
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return err;
207}
208
209static void __exit exit_udf_fs(void)
210{
211 unregister_filesystem(&udf_fstype);
212 destroy_inodecache();
213}
214
215module_init(init_udf_fs)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700216 module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/*
219 * udf_parse_options
220 *
221 * PURPOSE
222 * Parse mount options.
223 *
224 * DESCRIPTION
225 * The following mount options are supported:
226 *
227 * gid= Set the default group.
228 * umask= Set the default umask.
229 * uid= Set the default user.
230 * bs= Set the block size.
231 * unhide Show otherwise hidden files.
232 * undelete Show deleted files in lists.
233 * adinicb Embed data in the inode (default)
234 * noadinicb Don't embed data in the inode
235 * shortad Use short ad's
236 * longad Use long ad's (default)
237 * nostrict Unset strict conformance
238 * iocharset= Set the NLS character set
239 *
240 * The remaining are for debugging and disaster recovery:
241 *
242 * novrs Skip volume sequence recognition
243 *
244 * The following expect a offset from 0.
245 *
246 * session= Set the CDROM session (default= last session)
247 * anchor= Override standard anchor location. (default= 256)
248 * volume= Override the VolumeDesc location. (unused)
249 * partition= Override the PartitionDesc location. (unused)
250 * lastblock= Set the last block of the filesystem/
251 *
252 * The following expect a offset from the partition root.
253 *
254 * fileset= Override the fileset block location. (unused)
255 * rootdir= Override the root directory location. (unused)
256 * WARNING: overriding the rootdir to a non-directory may
257 * yield highly unpredictable results.
258 *
259 * PRE-CONDITIONS
260 * options Pointer to mount options string.
261 * uopts Pointer to mount options variable.
262 *
263 * POST-CONDITIONS
264 * <return> 1 Mount options parsed okay.
265 * <return> 0 Error parsing mount options.
266 *
267 * HISTORY
268 * July 1, 1997 - Andrew E. Mileski
269 * Written, tested, and released.
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271enum {
272 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
273 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
274 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
275 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
276 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800277 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
280static match_table_t tokens = {
281 {Opt_novrs, "novrs"},
282 {Opt_nostrict, "nostrict"},
283 {Opt_bs, "bs=%u"},
284 {Opt_unhide, "unhide"},
285 {Opt_undelete, "undelete"},
286 {Opt_noadinicb, "noadinicb"},
287 {Opt_adinicb, "adinicb"},
288 {Opt_shortad, "shortad"},
289 {Opt_longad, "longad"},
Phillip Susi4d6660e2006-03-07 21:55:24 -0800290 {Opt_uforget, "uid=forget"},
291 {Opt_uignore, "uid=ignore"},
292 {Opt_gforget, "gid=forget"},
293 {Opt_gignore, "gid=ignore"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 {Opt_gid, "gid=%u"},
295 {Opt_uid, "uid=%u"},
296 {Opt_umask, "umask=%o"},
297 {Opt_session, "session=%u"},
298 {Opt_lastblock, "lastblock=%u"},
299 {Opt_anchor, "anchor=%u"},
300 {Opt_volume, "volume=%u"},
301 {Opt_partition, "partition=%u"},
302 {Opt_fileset, "fileset=%u"},
303 {Opt_rootdir, "rootdir=%u"},
304 {Opt_utf8, "utf8"},
305 {Opt_iocharset, "iocharset=%s"},
306 {Opt_err, NULL}
307};
308
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700309static int udf_parse_options(char *options, struct udf_options *uopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 char *p;
312 int option;
313
314 uopt->novrs = 0;
315 uopt->blocksize = 2048;
316 uopt->partition = 0xFFFF;
317 uopt->session = 0xFFFFFFFF;
318 uopt->lastblock = 0;
319 uopt->anchor = 0;
320 uopt->volume = 0xFFFFFFFF;
321 uopt->rootdir = 0xFFFFFFFF;
322 uopt->fileset = 0xFFFFFFFF;
323 uopt->nls_map = NULL;
324
325 if (!options)
326 return 1;
327
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700328 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 substring_t args[MAX_OPT_ARGS];
330 int token;
331 if (!*p)
332 continue;
333
334 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700335 switch (token) {
336 case Opt_novrs:
337 uopt->novrs = 1;
338 case Opt_bs:
339 if (match_int(&args[0], &option))
340 return 0;
341 uopt->blocksize = option;
342 break;
343 case Opt_unhide:
344 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
345 break;
346 case Opt_undelete:
347 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
348 break;
349 case Opt_noadinicb:
350 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
351 break;
352 case Opt_adinicb:
353 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
354 break;
355 case Opt_shortad:
356 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
357 break;
358 case Opt_longad:
359 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
360 break;
361 case Opt_gid:
362 if (match_int(args, &option))
363 return 0;
364 uopt->gid = option;
365 break;
366 case Opt_uid:
367 if (match_int(args, &option))
368 return 0;
369 uopt->uid = option;
370 break;
371 case Opt_umask:
372 if (match_octal(args, &option))
373 return 0;
374 uopt->umask = option;
375 break;
376 case Opt_nostrict:
377 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
378 break;
379 case Opt_session:
380 if (match_int(args, &option))
381 return 0;
382 uopt->session = option;
383 break;
384 case Opt_lastblock:
385 if (match_int(args, &option))
386 return 0;
387 uopt->lastblock = option;
388 break;
389 case Opt_anchor:
390 if (match_int(args, &option))
391 return 0;
392 uopt->anchor = option;
393 break;
394 case Opt_volume:
395 if (match_int(args, &option))
396 return 0;
397 uopt->volume = option;
398 break;
399 case Opt_partition:
400 if (match_int(args, &option))
401 return 0;
402 uopt->partition = option;
403 break;
404 case Opt_fileset:
405 if (match_int(args, &option))
406 return 0;
407 uopt->fileset = option;
408 break;
409 case Opt_rootdir:
410 if (match_int(args, &option))
411 return 0;
412 uopt->rootdir = option;
413 break;
414 case Opt_utf8:
415 uopt->flags |= (1 << UDF_FLAG_UTF8);
416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700418 case Opt_iocharset:
419 uopt->nls_map = load_nls(args[0].from);
420 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700423 case Opt_uignore:
424 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
425 break;
426 case Opt_uforget:
427 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
428 break;
429 case Opt_gignore:
430 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
431 break;
432 case Opt_gforget:
433 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
434 break;
435 default:
436 printk(KERN_ERR "udf: bad mount option \"%s\" "
437 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 }
440 }
441 return 1;
442}
443
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700444void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 lock_kernel();
447 if (!(sb->s_flags & MS_RDONLY))
448 udf_open_lvid(sb);
449 sb->s_dirt = 0;
450 unlock_kernel();
451}
452
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700453static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct udf_options uopt;
456
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700457 uopt.flags = UDF_SB(sb)->s_flags;
458 uopt.uid = UDF_SB(sb)->s_uid;
459 uopt.gid = UDF_SB(sb)->s_gid;
460 uopt.umask = UDF_SB(sb)->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 if (!udf_parse_options(options, &uopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -EINVAL;
464
465 UDF_SB(sb)->s_flags = uopt.flags;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 UDF_SB(sb)->s_uid = uopt.uid;
467 UDF_SB(sb)->s_gid = uopt.gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 UDF_SB(sb)->s_umask = uopt.umask;
469
470 if (UDF_SB_LVIDBH(sb)) {
471 int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
472 if (write_rev > UDF_MAX_WRITE_VERSION)
473 *flags |= MS_RDONLY;
474 }
475
476 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
477 return 0;
478 if (*flags & MS_RDONLY)
479 udf_close_lvid(sb);
480 else
481 udf_open_lvid(sb);
482
483 return 0;
484}
485
486/*
487 * udf_set_blocksize
488 *
489 * PURPOSE
490 * Set the block size to be used in all transfers.
491 *
492 * DESCRIPTION
493 * To allow room for a DMA transfer, it is best to guess big when unsure.
494 * This routine picks 2048 bytes as the blocksize when guessing. This
495 * should be adequate until devices with larger block sizes become common.
496 *
497 * Note that the Linux kernel can currently only deal with blocksizes of
498 * 512, 1024, 2048, 4096, and 8192 bytes.
499 *
500 * PRE-CONDITIONS
501 * sb Pointer to _locked_ superblock.
502 *
503 * POST-CONDITIONS
504 * sb->s_blocksize Blocksize.
505 * sb->s_blocksize_bits log2 of blocksize.
506 * <return> 0 Blocksize is valid.
507 * <return> 1 Blocksize is invalid.
508 *
509 * HISTORY
510 * July 1, 1997 - Andrew E. Mileski
511 * Written, tested, and released.
512 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700513static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 if (!sb_min_blocksize(sb, bsize)) {
516 udf_debug("Bad block size (%d)\n", bsize);
517 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
518 return 0;
519 }
520 return sb->s_blocksize;
521}
522
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700523static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct volStructDesc *vsd = NULL;
526 int sector = 32768;
527 int sectorsize;
528 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700529 int iso9660 = 0;
530 int nsr02 = 0;
531 int nsr03 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Block size must be a multiple of 512 */
534 if (sb->s_blocksize & 511)
535 return 0;
536
537 if (sb->s_blocksize < sizeof(struct volStructDesc))
538 sectorsize = sizeof(struct volStructDesc);
539 else
540 sectorsize = sb->s_blocksize;
541
542 sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
543
544 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700545 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Read a block */
549 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
550 if (!bh)
551 break;
552
553 /* Look for ISO descriptors */
554 vsd = (struct volStructDesc *)(bh->b_data +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 (sector &
556 (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700558 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700559 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700561 } else
562 if (!strncmp
563 (vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700565 switch (vsd->structType) {
566 case 0:
567 udf_debug("ISO9660 Boot Record found\n");
568 break;
569 case 1:
570 udf_debug
571 ("ISO9660 Primary Volume Descriptor found\n");
572 break;
573 case 2:
574 udf_debug
575 ("ISO9660 Supplementary Volume Descriptor found\n");
576 break;
577 case 3:
578 udf_debug
579 ("ISO9660 Volume Partition Descriptor found\n");
580 break;
581 case 255:
582 udf_debug
583 ("ISO9660 Volume Descriptor Set Terminator found\n");
584 break;
585 default:
586 udf_debug("ISO9660 VRS (%u) found\n",
587 vsd->structType);
588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700590 } else
591 if (!strncmp
592 (vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN)) {
593 } else
594 if (!strncmp
595 (vsd->stdIdent, VSD_STD_ID_TEA01, VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700596 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 } else
599 if (!strncmp
600 (vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 nsr02 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700602 } else
603 if (!strncmp
604 (vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 nsr03 = sector;
606 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700607 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 if (nsr03)
611 return nsr03;
612 else if (nsr02)
613 return nsr02;
614 else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
615 return -1;
616 else
617 return 0;
618}
619
620/*
621 * udf_find_anchor
622 *
623 * PURPOSE
624 * Find an anchor volume descriptor.
625 *
626 * PRE-CONDITIONS
627 * sb Pointer to _locked_ superblock.
628 * lastblock Last block on media.
629 *
630 * POST-CONDITIONS
631 * <return> 1 if not found, 0 if ok
632 *
633 * HISTORY
634 * July 1, 1997 - Andrew E. Mileski
635 * Written, tested, and released.
636 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 int lastblock = UDF_SB_LASTBLOCK(sb);
640 struct buffer_head *bh = NULL;
641 uint16_t ident;
642 uint32_t location;
643 int i;
644
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700647 int last[] = { lastblock, lastblock - 2,
648 lastblock - 150, lastblock - 152,
649 varlastblock, varlastblock - 2,
650 varlastblock - 150, varlastblock - 152
651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 lastblock = 0;
654
655 /* Search for an anchor volume descriptor pointer */
656
657 /* according to spec, anchor is in either:
658 * block 256
659 * lastblock-256
660 * lastblock
661 * however, if the disc isn't closed, it could be 512 */
662
Tobias Klausere8c96f82006-03-24 03:15:34 -0800663 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700664 if (last[i] < 0 || !(bh = sb_bread(sb, last[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700666 } else {
667 ident =
668 le16_to_cpu(((tag *) bh->b_data)->tagIdent);
669 location =
670 le32_to_cpu(((tag *) bh->b_data)->
671 tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700672 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800674
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700675 if (ident == TAG_IDENT_AVDP) {
676 if (location == last[i] - UDF_SB_SESSION(sb)) {
677 lastblock = UDF_SB_ANCHOR(sb)[0] =
678 last[i] - UDF_SB_SESSION(sb);
679 UDF_SB_ANCHOR(sb)[1] =
680 last[i] - 256 - UDF_SB_SESSION(sb);
681 } else if (location ==
682 udf_variable_to_fixed(last[i]) -
683 UDF_SB_SESSION(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700685 lastblock = UDF_SB_ANCHOR(sb)[0] =
686 udf_variable_to_fixed(last[i]) -
687 UDF_SB_SESSION(sb);
688 UDF_SB_ANCHOR(sb)[1] =
689 lastblock - 256 -
690 UDF_SB_SESSION(sb);
691 } else
692 udf_debug
693 ("Anchor found at block %d, location mismatch %d.\n",
694 last[i], location);
695 } else if (ident == TAG_IDENT_FE
696 || ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 lastblock = last[i];
698 UDF_SB_ANCHOR(sb)[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700699 } else {
700 if (last[i] < 256
701 || !(bh = sb_bread(sb, last[i] - 256))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700703 } else {
704 ident =
705 le16_to_cpu(((tag *) bh->b_data)->
706 tagIdent);
707 location =
708 le32_to_cpu(((tag *) bh->b_data)->
709 tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700710 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700714 location ==
715 last[i] - 256 - UDF_SB_SESSION(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 lastblock = last[i];
717 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700718 } else {
719 if (last[i] < 312 + UDF_SB_SESSION(sb)
720 || !(bh =
721 sb_bread(sb,
722 last[i] - 312 -
723 UDF_SB_SESSION(sb))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 {
725 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700726 } else {
727 ident =
728 le16_to_cpu(((tag *) bh->
729 b_data)->
730 tagIdent);
731 location =
732 le32_to_cpu(((tag *) bh->
733 b_data)->
734 tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700735 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 location ==
740 udf_variable_to_fixed(last[i]) -
741 256) {
742 UDF_SET_FLAG(sb,
743 UDF_FLAG_VARCONV);
744 lastblock =
745 udf_variable_to_fixed(last
746 [i]);
747 UDF_SB_ANCHOR(sb)[1] =
748 lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 }
751 }
752 }
753 }
754
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755 if (!lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /* We havn't found the lastblock. check 312 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700757 if ((bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb)))) {
758 ident = le16_to_cpu(((tag *) bh->b_data)->tagIdent);
759 location =
760 le32_to_cpu(((tag *) bh->b_data)->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700761 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 if (ident == TAG_IDENT_AVDP && location == 256)
764 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
765 }
766 }
767
Tobias Klausere8c96f82006-03-24 03:15:34 -0800768 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769 if (UDF_SB_ANCHOR(sb)[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!(bh = udf_read_tagged(sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 UDF_SB_ANCHOR(sb)[i],
772 UDF_SB_ANCHOR(sb)[i],
773 &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 UDF_SB_ANCHOR(sb)[i] = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700775 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700776 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if ((ident != TAG_IDENT_AVDP) && (i ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 (ident !=
779 TAG_IDENT_FE
780 && ident !=
781 TAG_IDENT_EFE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 {
783 UDF_SB_ANCHOR(sb)[i] = 0;
784 }
785 }
786 }
787 }
788
789 UDF_SB_LASTBLOCK(sb) = lastblock;
790}
791
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700792static int
793udf_find_fileset(struct super_block *sb, kernel_lb_addr * fileset,
794 kernel_lb_addr * root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct buffer_head *bh = NULL;
797 long lastblock;
798 uint16_t ident;
799
800 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
803
804 if (!bh)
805 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700806 else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700807 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 1;
809 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700813 if (!bh) { /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 kernel_lb_addr newfileset;
815
816 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700817
818 for (newfileset.partitionReferenceNum = UDF_SB_NUMPARTS(sb) - 1;
819 (newfileset.partitionReferenceNum != 0xFFFF &&
820 fileset->logicalBlockNum == 0xFFFFFFFF &&
821 fileset->partitionReferenceNum == 0xFFFF);
822 newfileset.partitionReferenceNum--) {
823 lastblock =
824 UDF_SB_PARTLEN(sb,
825 newfileset.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 newfileset.logicalBlockNum = 0;
827
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828 do {
829 bh = udf_read_ptagged(sb, newfileset, 0,
830 &ident);
831 if (!bh) {
832 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 continue;
834 }
835
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836 switch (ident) {
837 case TAG_IDENT_SBD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 {
839 struct spaceBitmapDesc *sp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700840 sp = (struct spaceBitmapDesc *)
841 bh->b_data;
842 newfileset.logicalBlockNum +=
843 1 +
844 ((le32_to_cpu
845 (sp->numOfBytes) +
846 sizeof(struct
847 spaceBitmapDesc) -
848 1)
849 >> sb->s_blocksize_bits);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700850 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 break;
852 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 case TAG_IDENT_FSD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 {
855 *fileset = newfileset;
856 break;
857 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700860 newfileset.logicalBlockNum++;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700861 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 bh = NULL;
863 break;
864 }
865 }
866 }
867 while (newfileset.logicalBlockNum < lastblock &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700868 fileset->logicalBlockNum == 0xFFFFFFFF &&
869 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 }
872
873 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700876 fileset->logicalBlockNum,
877 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
880 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700881 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return 0;
883 }
884 return 1;
885}
886
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct primaryVolDesc *pvoldesc;
890 time_t recording;
891 long recording_usec;
892 struct ustr instr;
893 struct ustr outstr;
894
895 pvoldesc = (struct primaryVolDesc *)bh->b_data;
896
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700897 if (udf_stamp_to_time(&recording, &recording_usec,
898 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 kernel_timestamp ts;
900 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 udf_debug
902 ("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n",
903 recording, recording_usec, ts.year, ts.month, ts.day,
904 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 UDF_SB_RECORDTIME(sb).tv_sec = recording;
906 UDF_SB_RECORDTIME(sb).tv_nsec = recording_usec * 1000;
907 }
908
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700909 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
910 if (udf_CS0toUTF8(&outstr, &instr)) {
911 strncpy(UDF_SB_VOLIDENT(sb), outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 outstr.u_len > 31 ? 31 : outstr.u_len);
913 udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
914 }
915 }
916
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (udf_CS0toUTF8(&outstr, &instr))
919 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
920 }
921}
922
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700923static void
924udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
925 kernel_lb_addr * root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct fileSetDesc *fset;
928
929 fset = (struct fileSetDesc *)bh->b_data;
930
931 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
932
933 UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
934
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700935 udf_debug("Rootdir at block=%d, partition=%d\n",
936 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700939static void udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct partitionDesc *p;
942 int i;
943
944 p = (struct partitionDesc *)bh->b_data;
945
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700946 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
947 udf_debug("Searching map: (%d == %d)\n",
948 UDF_SB_PARTMAPS(sb)[i].s_partition_num,
949 le16_to_cpu(p->partitionNumber));
950 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num ==
951 le16_to_cpu(p->partitionNumber)) {
952 UDF_SB_PARTLEN(sb, i) = le32_to_cpu(p->partitionLength); /* blocks */
953 UDF_SB_PARTROOT(sb, i) =
954 le32_to_cpu(p->partitionStartingLocation);
955 if (le32_to_cpu(p->accessType) ==
956 PD_ACCESS_TYPE_READ_ONLY)
957 UDF_SB_PARTFLAGS(sb, i) |=
958 UDF_PART_FLAG_READ_ONLY;
959 if (le32_to_cpu(p->accessType) ==
960 PD_ACCESS_TYPE_WRITE_ONCE)
961 UDF_SB_PARTFLAGS(sb, i) |=
962 UDF_PART_FLAG_WRITE_ONCE;
963 if (le32_to_cpu(p->accessType) ==
964 PD_ACCESS_TYPE_REWRITABLE)
965 UDF_SB_PARTFLAGS(sb, i) |=
966 UDF_PART_FLAG_REWRITABLE;
967 if (le32_to_cpu(p->accessType) ==
968 PD_ACCESS_TYPE_OVERWRITABLE)
969 UDF_SB_PARTFLAGS(sb, i) |=
970 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700972 if (!strcmp
973 (p->partitionContents.ident,
974 PD_PARTITION_CONTENTS_NSR02)
975 || !strcmp(p->partitionContents.ident,
976 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct partitionHeaderDesc *phd;
978
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700979 phd =
980 (struct partitionHeaderDesc *)(p->
981 partitionContentsUse);
982 if (phd->unallocSpaceTable.extLength) {
983 kernel_lb_addr loc =
984 { le32_to_cpu(phd->
985 unallocSpaceTable.
986 extPosition), i };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700988 UDF_SB_PARTMAPS(sb)[i].s_uspace.
989 s_table = udf_iget(sb, loc);
990 UDF_SB_PARTFLAGS(sb, i) |=
991 UDF_PART_FLAG_UNALLOC_TABLE;
992 udf_debug
993 ("unallocSpaceTable (part %d) @ %ld\n",
994 i,
995 UDF_SB_PARTMAPS(sb)[i].s_uspace.
996 s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700998 if (phd->unallocSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001000 if (UDF_SB_PARTMAPS(sb)[i].s_uspace.
1001 s_bitmap != NULL) {
1002 UDF_SB_PARTMAPS(sb)[i].s_uspace.
1003 s_bitmap->s_extLength =
1004 le32_to_cpu(phd->
1005 unallocSpaceBitmap.
1006 extLength);
1007 UDF_SB_PARTMAPS(sb)[i].s_uspace.
1008 s_bitmap->s_extPosition =
1009 le32_to_cpu(phd->
1010 unallocSpaceBitmap.
1011 extPosition);
1012 UDF_SB_PARTFLAGS(sb, i) |=
1013 UDF_PART_FLAG_UNALLOC_BITMAP;
1014 udf_debug
1015 ("unallocSpaceBitmap (part %d) @ %d\n",
1016 i,
1017 UDF_SB_PARTMAPS(sb)[i].
1018 s_uspace.s_bitmap->
1019 s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
1022 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001023 udf_debug
1024 ("partitionIntegrityTable (part %d)\n",
1025 i);
1026 if (phd->freedSpaceTable.extLength) {
1027 kernel_lb_addr loc =
1028 { le32_to_cpu(phd->freedSpaceTable.
1029 extPosition), i };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001031 UDF_SB_PARTMAPS(sb)[i].s_fspace.
1032 s_table = udf_iget(sb, loc);
1033 UDF_SB_PARTFLAGS(sb, i) |=
1034 UDF_PART_FLAG_FREED_TABLE;
1035 udf_debug
1036 ("freedSpaceTable (part %d) @ %ld\n",
1037 i,
1038 UDF_SB_PARTMAPS(sb)[i].s_fspace.
1039 s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001041 if (phd->freedSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001043 if (UDF_SB_PARTMAPS(sb)[i].s_fspace.
1044 s_bitmap != NULL) {
1045 UDF_SB_PARTMAPS(sb)[i].s_fspace.
1046 s_bitmap->s_extLength =
1047 le32_to_cpu(phd->
1048 freedSpaceBitmap.
1049 extLength);
1050 UDF_SB_PARTMAPS(sb)[i].s_fspace.
1051 s_bitmap->s_extPosition =
1052 le32_to_cpu(phd->
1053 freedSpaceBitmap.
1054 extPosition);
1055 UDF_SB_PARTFLAGS(sb, i) |=
1056 UDF_PART_FLAG_FREED_BITMAP;
1057 udf_debug
1058 ("freedSpaceBitmap (part %d) @ %d\n",
1059 i,
1060 UDF_SB_PARTMAPS(sb)[i].
1061 s_fspace.s_bitmap->
1062 s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 }
1065 }
1066 break;
1067 }
1068 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001069 if (i == UDF_SB_NUMPARTS(sb)) {
1070 udf_debug("Partition (%d) not found in partition map\n",
1071 le16_to_cpu(p->partitionNumber));
1072 } else {
1073 udf_debug
1074 ("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
1075 le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb, i),
1076 UDF_SB_PARTROOT(sb, i), UDF_SB_PARTLEN(sb, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078}
1079
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001080static int
1081udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1082 kernel_lb_addr * fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct logicalVolDesc *lvd;
1085 int i, j, offset;
1086 uint8_t type;
1087
1088 lvd = (struct logicalVolDesc *)bh->b_data;
1089
1090 UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
1091
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001092 for (i = 0, offset = 0;
1093 i < UDF_SB_NUMPARTS(sb)
1094 && offset < le32_to_cpu(lvd->mapTableLength);
1095 i++, offset +=
1096 ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->
1097 partitionMapLength) {
1098 type =
1099 ((struct genericPartitionMap *)
1100 &(lvd->partitionMaps[offset]))->partitionMapType;
1101 if (type == 1) {
1102 struct genericPartitionMap1 *gpm1 =
1103 (struct genericPartitionMap1 *)&(lvd->
1104 partitionMaps
1105 [offset]);
1106 UDF_SB_PARTTYPE(sb, i) = UDF_TYPE1_MAP15;
1107 UDF_SB_PARTVSN(sb, i) = le16_to_cpu(gpm1->volSeqNum);
1108 UDF_SB_PARTNUM(sb, i) = le16_to_cpu(gpm1->partitionNum);
1109 UDF_SB_PARTFUNC(sb, i) = NULL;
1110 } else if (type == 2) {
1111 struct udfPartitionMap2 *upm2 =
1112 (struct udfPartitionMap2 *)&(lvd->
1113 partitionMaps[offset]);
1114 if (!strncmp
1115 (upm2->partIdent.ident, UDF_ID_VIRTUAL,
1116 strlen(UDF_ID_VIRTUAL))) {
1117 if (le16_to_cpu
1118 (((__le16 *) upm2->partIdent.
1119 identSuffix)[0]) == 0x0150) {
1120 UDF_SB_PARTTYPE(sb, i) =
1121 UDF_VIRTUAL_MAP15;
1122 UDF_SB_PARTFUNC(sb, i) =
1123 udf_get_pblock_virt15;
1124 } else
1125 if (le16_to_cpu
1126 (((__le16 *) upm2->partIdent.
1127 identSuffix)[0]) == 0x0200) {
1128 UDF_SB_PARTTYPE(sb, i) =
1129 UDF_VIRTUAL_MAP20;
1130 UDF_SB_PARTFUNC(sb, i) =
1131 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001133 } else
1134 if (!strncmp
1135 (upm2->partIdent.ident, UDF_ID_SPARABLE,
1136 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 uint32_t loc;
1138 uint16_t ident;
1139 struct sparingTable *st;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001140 struct sparablePartitionMap *spm =
1141 (struct sparablePartitionMap *)&(lvd->
1142 partitionMaps
1143 [offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001145 UDF_SB_PARTTYPE(sb, i) = UDF_SPARABLE_MAP15;
1146 UDF_SB_TYPESPAR(sb, i).s_packet_len =
1147 le16_to_cpu(spm->packetLength);
1148 for (j = 0; j < spm->numSparingTables; j++) {
1149 loc =
1150 le32_to_cpu(spm->
1151 locSparingTable[j]);
1152 UDF_SB_TYPESPAR(sb, i).s_spar_map[j] =
1153 udf_read_tagged(sb, loc, loc,
1154 &ident);
1155 if (UDF_SB_TYPESPAR(sb, i).
1156 s_spar_map[j] != NULL) {
1157 st = (struct sparingTable *)
1158 UDF_SB_TYPESPAR(sb,
1159 i).
1160 s_spar_map[j]->b_data;
1161 if (ident != 0
1162 || strncmp(st->sparingIdent.
1163 ident,
1164 UDF_ID_SPARING,
1165 strlen
1166 (UDF_ID_SPARING)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001168 brelse(UDF_SB_TYPESPAR
1169 (sb,
1170 i).
1171 s_spar_map[j]);
1172 UDF_SB_TYPESPAR(sb,
1173 i).
1174 s_spar_map[j] =
1175 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 }
1178 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001179 UDF_SB_PARTFUNC(sb, i) = udf_get_pblock_spar15;
1180 } else {
1181 udf_debug("Unknown ident: %s\n",
1182 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 continue;
1184 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001185 UDF_SB_PARTVSN(sb, i) = le16_to_cpu(upm2->volSeqNum);
1186 UDF_SB_PARTNUM(sb, i) = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001189 i, UDF_SB_PARTNUM(sb, i), type, UDF_SB_PARTVSN(sb,
1190 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001193 if (fileset) {
1194 long_ad *la = (long_ad *) & (lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 *fileset = lelb_to_cpu(la->extLocation);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001197 udf_debug
1198 ("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1199 fileset->logicalBlockNum, fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 if (lvd->integritySeqExt.extLength)
1202 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
1203 return 0;
1204}
1205
1206/*
1207 * udf_load_logicalvolint
1208 *
1209 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001210static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct buffer_head *bh = NULL;
1213 uint16_t ident;
1214
1215 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001216 (bh = udf_read_tagged(sb, loc.extLocation,
1217 loc.extLocation, &ident)) &&
1218 ident == TAG_IDENT_LVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 UDF_SB_LVIDBH(sb) = bh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001222 udf_load_logicalvolint(sb,
1223 leea_to_cpu(UDF_SB_LVID(sb)->
1224 nextIntegrityExt));
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (UDF_SB_LVIDBH(sb) != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001227 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001229 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 if (UDF_SB_LVIDBH(sb) != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001232 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235/*
1236 * udf_process_sequence
1237 *
1238 * PURPOSE
1239 * Process a main/reserve volume descriptor sequence.
1240 *
1241 * PRE-CONDITIONS
1242 * sb Pointer to _locked_ superblock.
1243 * block First block of first extent of the sequence.
1244 * lastblock Lastblock of first extent of the sequence.
1245 *
1246 * HISTORY
1247 * July 1, 1997 - Andrew E. Mileski
1248 * Written, tested, and released.
1249 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001250static int
1251udf_process_sequence(struct super_block *sb, long block, long lastblock,
1252 kernel_lb_addr * fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct buffer_head *bh = NULL;
1255 struct udf_vds_record vds[VDS_POS_LENGTH];
1256 struct generic_desc *gd;
1257 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001258 int done = 0;
1259 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 uint32_t vdsn;
1261 uint16_t ident;
1262 long next_s = 0, next_e = 0;
1263
1264 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1265
1266 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001267 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272
1273 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1274 gd = (struct generic_desc *)bh->b_data;
1275 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001276 switch (ident) {
1277 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
1278 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
1279 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum =
1280 vdsn;
1281 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1282 }
1283 break;
1284 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
1285 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1286 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1287 vds[VDS_POS_VOL_DESC_PTR].block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 vdp = (struct volDescPtr *)bh->b_data;
1290 next_s =
1291 le32_to_cpu(vdp->nextVolDescSeqExt.
1292 extLocation);
1293 next_e =
1294 le32_to_cpu(vdp->nextVolDescSeqExt.
1295 extLength);
1296 next_e = next_e >> sb->s_blocksize_bits;
1297 next_e += next_s;
1298 }
1299 break;
1300 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
1301 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
1302 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum =
1303 vdsn;
1304 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1305 }
1306 break;
1307 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1308 if (!vds[VDS_POS_PARTITION_DESC].block)
1309 vds[VDS_POS_PARTITION_DESC].block = block;
1310 break;
1311 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1312 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
1313 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum =
1314 vdsn;
1315 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1316 }
1317 break;
1318 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1319 if (vdsn >=
1320 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1321 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum =
1322 vdsn;
1323 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1324 }
1325 break;
1326 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
1327 vds[VDS_POS_TERMINATING_DESC].block = block;
1328 if (next_e) {
1329 block = next_s;
1330 lastblock = next_e;
1331 next_s = next_e = 0;
1332 } else
1333 done = 1;
1334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001336 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001338 for (i = 0; i < VDS_POS_LENGTH; i++) {
1339 if (vds[i].block) {
1340 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1341 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (i == VDS_POS_PRIMARY_VOL_DESC)
1344 udf_load_pvoldesc(sb, bh);
1345 else if (i == VDS_POS_LOGICAL_VOL_DESC)
1346 udf_load_logicalvol(sb, bh, fileset);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001347 else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct buffer_head *bh2 = NULL;
1349 udf_load_partdesc(sb, bh);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001350 for (j = vds[i].block + 1;
1351 j < vds[VDS_POS_TERMINATING_DESC].block;
1352 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 bh2 = udf_read_tagged(sb, j, j, &ident);
1354 gd = (struct generic_desc *)bh2->b_data;
1355 if (ident == TAG_IDENT_PD)
1356 udf_load_partdesc(sb, bh2);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001357 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001360 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 }
1363
1364 return 0;
1365}
1366
1367/*
1368 * udf_check_valid()
1369 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372 long block;
1373
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001374 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 udf_debug("Validity check skipped because of novrs option\n");
1376 return 0;
1377 }
1378 /* Check that it is NSR02 compliant */
1379 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001380 else if ((block = udf_vrs(sb, silent)) == -1) {
1381 udf_debug
1382 ("Failed to read byte 32768. Assuming open disc. Skipping validity check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!UDF_SB_LASTBLOCK(sb))
1384 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1385 return 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001386 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return !block;
1388}
1389
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001390static int udf_load_partition(struct super_block *sb, kernel_lb_addr * fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct anchorVolDescPtr *anchor;
1393 uint16_t ident;
1394 struct buffer_head *bh;
1395 long main_s, main_e, reserve_s, reserve_e;
1396 int i, j;
1397
1398 if (!sb)
1399 return 1;
1400
Tobias Klausere8c96f82006-03-24 03:15:34 -08001401 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (UDF_SB_ANCHOR(sb)[i] && (bh = udf_read_tagged(sb,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001403 UDF_SB_ANCHOR
1404 (sb)[i],
1405 UDF_SB_ANCHOR
1406 (sb)[i],
1407 &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 anchor = (struct anchorVolDescPtr *)bh->b_data;
1409
1410 /* Locate the main sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001411 main_s =
1412 le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1413 main_e =
1414 le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 main_e = main_e >> sb->s_blocksize_bits;
1416 main_e += main_s;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* Locate the reserve sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001419 reserve_s =
1420 le32_to_cpu(anchor->reserveVolDescSeqExt.
1421 extLocation);
1422 reserve_e =
1423 le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 reserve_e = reserve_e >> sb->s_blocksize_bits;
1425 reserve_e += reserve_s;
1426
Jan Kara3bf25cb2007-05-08 00:35:16 -07001427 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* Process the main & reserve sequences */
1430 /* responsible for finding the PartitionDesc(s) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001431 if (!
1432 (udf_process_sequence(sb, main_s, main_e, fileset)
1433 && udf_process_sequence(sb, reserve_s, reserve_e,
1434 fileset))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 }
1437 }
1438 }
1439
Tobias Klausere8c96f82006-03-24 03:15:34 -08001440 if (i == ARRAY_SIZE(UDF_SB_ANCHOR(sb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 udf_debug("No Anchor block found\n");
1442 return 1;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001443 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1445
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001446 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
1447 switch (UDF_SB_PARTTYPE(sb, i)) {
1448 case UDF_VIRTUAL_MAP15:
1449 case UDF_VIRTUAL_MAP20:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 {
Jeff Garzik8e1c0912007-07-17 05:40:59 -04001451 kernel_lb_addr uninitialized_var(ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001453 if (!UDF_SB_LASTBLOCK(sb)) {
1454 UDF_SB_LASTBLOCK(sb) =
1455 udf_get_last_block(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 udf_find_anchor(sb);
1457 }
1458
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001459 if (!UDF_SB_LASTBLOCK(sb)) {
1460 udf_debug
1461 ("Unable to determine Lastblock (For Virtual Partition)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return 1;
1463 }
1464
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001465 for (j = 0; j < UDF_SB_NUMPARTS(sb); j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (j != i &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 UDF_SB_PARTVSN(sb,
1468 i) ==
1469 UDF_SB_PARTVSN(sb, j)
1470 && UDF_SB_PARTNUM(sb,
1471 i) ==
1472 UDF_SB_PARTNUM(sb, j)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ino.partitionReferenceNum = j;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001474 ino.logicalBlockNum =
1475 UDF_SB_LASTBLOCK(sb) -
1476 UDF_SB_PARTROOT(sb, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 break;
1478 }
1479 }
1480
1481 if (j == UDF_SB_NUMPARTS(sb))
1482 return 1;
1483
1484 if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1485 return 1;
1486
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001487 if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP15) {
1488 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1489 udf_ext0_offset(UDF_SB_VAT(sb));
1490 UDF_SB_TYPEVIRT(sb, i).s_num_entries =
1491 (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1492 } else if (UDF_SB_PARTTYPE(sb, i) ==
1493 UDF_VIRTUAL_MAP20) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct buffer_head *bh = NULL;
1495 uint32_t pos;
1496
1497 pos = udf_block_map(UDF_SB_VAT(sb), 0);
1498 bh = sb_bread(sb, pos);
Dmitriy Monakhov951744f2007-05-08 00:35:22 -07001499 if (!bh)
1500 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1502 le16_to_cpu(((struct
1503 virtualAllocationTable20
1504 *)bh->b_data +
1505 udf_ext0_offset
1506 (UDF_SB_VAT(sb)))->
1507 lengthHeader) +
1508 udf_ext0_offset(UDF_SB_VAT(sb));
1509 UDF_SB_TYPEVIRT(sb, i).s_num_entries =
1510 (UDF_SB_VAT(sb)->i_size -
1511 UDF_SB_TYPEVIRT(sb,
1512 i).
1513 s_start_offset) >> 2;
Jan Kara3bf25cb2007-05-08 00:35:16 -07001514 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001516 UDF_SB_PARTROOT(sb, i) =
1517 udf_get_pblock(sb, 0, i, 0);
1518 UDF_SB_PARTLEN(sb, i) =
1519 UDF_SB_PARTLEN(sb,
1520 ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 }
1523 }
1524 return 0;
1525}
1526
1527static void udf_open_lvid(struct super_block *sb)
1528{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001529 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 int i;
1531 kernel_timestamp cpu_time;
1532
1533 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1534 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1535 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001536 UDF_SB_LVID(sb)->recordingDateAndTime =
1537 cpu_to_lets(cpu_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1539
1540 UDF_SB_LVID(sb)->descTag.descCRC =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001541 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1542 le16_to_cpu(UDF_SB_LVID(sb)->descTag.
1543 descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001546 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (i != 4)
1548 UDF_SB_LVID(sb)->descTag.tagChecksum +=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001549 ((uint8_t *) &
1550 (UDF_SB_LVID(sb)->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1553 }
1554}
1555
1556static void udf_close_lvid(struct super_block *sb)
1557{
1558 if (UDF_SB_LVIDBH(sb) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001559 UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int i;
1561 kernel_timestamp cpu_time;
1562
1563 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1564 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1565 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001566 UDF_SB_LVID(sb)->recordingDateAndTime =
1567 cpu_to_lets(cpu_time);
1568 if (UDF_MAX_WRITE_VERSION >
1569 le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1570 UDF_SB_LVIDIU(sb)->maxUDFWriteRev =
1571 cpu_to_le16(UDF_MAX_WRITE_VERSION);
1572 if (UDF_SB_UDFREV(sb) >
1573 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1574 UDF_SB_LVIDIU(sb)->minUDFReadRev =
1575 cpu_to_le16(UDF_SB_UDFREV(sb));
1576 if (UDF_SB_UDFREV(sb) >
1577 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1578 UDF_SB_LVIDIU(sb)->minUDFWriteRev =
1579 cpu_to_le16(UDF_SB_UDFREV(sb));
1580 UDF_SB_LVID(sb)->integrityType =
1581 cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 UDF_SB_LVID(sb)->descTag.descCRC =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001584 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1585 le16_to_cpu(UDF_SB_LVID(sb)->descTag.
1586 descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001589 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (i != 4)
1591 UDF_SB_LVID(sb)->descTag.tagChecksum +=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001592 ((uint8_t *) &
1593 (UDF_SB_LVID(sb)->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1596 }
1597}
1598
1599/*
1600 * udf_read_super
1601 *
1602 * PURPOSE
1603 * Complete the specified super block.
1604 *
1605 * PRE-CONDITIONS
1606 * sb Pointer to superblock to complete - never NULL.
1607 * sb->s_dev Device to read suberblock from.
1608 * options Pointer to mount options.
1609 * silent Silent flag.
1610 *
1611 * HISTORY
1612 * July 1, 1997 - Andrew E. Mileski
1613 * Written, tested, and released.
1614 */
1615static int udf_fill_super(struct super_block *sb, void *options, int silent)
1616{
1617 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001618 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct udf_options uopt;
1620 kernel_lb_addr rootdir, fileset;
1621 struct udf_sb_info *sbi;
1622
1623 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1624 uopt.uid = -1;
1625 uopt.gid = -1;
1626 uopt.umask = 0;
1627
1628 sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1629 if (!sbi)
1630 return -ENOMEM;
1631 sb->s_fs_info = sbi;
1632 memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1633
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001634 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 if (!udf_parse_options((char *)options, &uopt))
1637 goto error_out;
1638
1639 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001640 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001642 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto error_out;
1644 }
1645#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001646 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 uopt.nls_map = load_nls_default();
1648 if (!uopt.nls_map)
1649 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1650 else
1651 udf_debug("Using default NLS map\n");
1652 }
1653#endif
1654 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1655 uopt.flags |= (1 << UDF_FLAG_UTF8);
1656
1657 fileset.logicalBlockNum = 0xFFFFFFFF;
1658 fileset.partitionReferenceNum = 0xFFFF;
1659
1660 UDF_SB(sb)->s_flags = uopt.flags;
1661 UDF_SB(sb)->s_uid = uopt.uid;
1662 UDF_SB(sb)->s_gid = uopt.gid;
1663 UDF_SB(sb)->s_umask = uopt.umask;
1664 UDF_SB(sb)->s_nls_map = uopt.nls_map;
1665
1666 /* Set the block size for all transfers */
1667 if (!udf_set_blocksize(sb, uopt.blocksize))
1668 goto error_out;
1669
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001670 if (uopt.session == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1672 else
1673 UDF_SB_SESSION(sb) = uopt.session;
1674
1675 udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1676
1677 UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1678 UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0;
1679 UDF_SB_ANCHOR(sb)[2] = uopt.anchor;
1680 UDF_SB_ANCHOR(sb)[3] = 256;
1681
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001682 if (udf_check_valid(sb, uopt.novrs, silent)) { /* read volume recognition sequences */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 printk("UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001684 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
1687 udf_find_anchor(sb);
1688
1689 /* Fill in the rest of the superblock */
1690 sb->s_op = &udf_sb_ops;
1691 sb->dq_op = NULL;
1692 sb->s_dirt = 0;
1693 sb->s_magic = UDF_SUPER_MAGIC;
1694 sb->s_time_gran = 1000;
1695
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 if (udf_load_partition(sb, &fileset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 printk("UDF-fs: No partition found (1)\n");
1698 goto error_out;
1699 }
1700
1701 udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1702
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 if (UDF_SB_LVIDBH(sb)) {
1704 uint16_t minUDFReadRev =
1705 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1706 uint16_t minUDFWriteRev =
1707 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1709
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001710 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001712 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev),
1713 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto error_out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001715 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 sb->s_flags |= MS_RDONLY;
1717 }
1718
1719 UDF_SB_UDFREV(sb) = minUDFWriteRev;
1720
1721 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1722 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1723 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1724 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1725 }
1726
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001727 if (!UDF_SB_NUMPARTS(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 printk("UDF-fs: No partition found (2)\n");
1729 goto error_out;
1730 }
1731
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001732 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1733 UDF_PART_FLAG_READ_ONLY) {
1734 printk
1735 ("UDF-fs: Partition marked readonly; forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001736 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001737 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001738
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001739 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 printk("UDF-fs: No fileset found\n");
1741 goto error_out;
1742 }
1743
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001744 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 kernel_timestamp ts;
1746 udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001747 udf_info
1748 ("UDF %s (%s) Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1749 UDFFS_VERSION, UDFFS_DATE, UDF_SB_VOLIDENT(sb), ts.year,
1750 ts.month, ts.day, ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752 if (!(sb->s_flags & MS_RDONLY))
1753 udf_open_lvid(sb);
1754
1755 /* Assign the root inode */
1756 /* assign inodes by physical block number */
1757 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 inode = udf_iget(sb, rootdir);
1759 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001761 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto error_out;
1763 }
1764
1765 /* Allocate a dentry for the root inode */
1766 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001767 if (!sb->s_root) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 printk("UDF-fs: Couldn't allocate root dentry\n");
1769 iput(inode);
1770 goto error_out;
1771 }
Jan Kara31170b62007-05-08 00:35:21 -07001772 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001775 error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (UDF_SB_VAT(sb))
1777 iput(UDF_SB_VAT(sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001778 if (UDF_SB_NUMPARTS(sb)) {
1779 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1780 UDF_PART_FLAG_UNALLOC_TABLE)
1781 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.
1782 s_table);
1783 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1784 UDF_PART_FLAG_FREED_TABLE)
1785 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.
1786 s_table);
1787 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1788 UDF_PART_FLAG_UNALLOC_BITMAP)
1789 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_uspace);
1790 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1791 UDF_PART_FLAG_FREED_BITMAP)
1792 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_fspace);
1793 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) ==
1794 UDF_SPARABLE_MAP15) {
1795 for (i = 0; i < 4; i++)
1796 brelse(UDF_SB_TYPESPAR
1797 (sb,
1798 UDF_SB_PARTITION(sb)).s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 }
1801#ifdef CONFIG_UDF_NLS
1802 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1803 unload_nls(UDF_SB(sb)->s_nls_map);
1804#endif
1805 if (!(sb->s_flags & MS_RDONLY))
1806 udf_close_lvid(sb);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001807 brelse(UDF_SB_LVIDBH(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 UDF_SB_FREE(sb);
1809 kfree(sbi);
1810 sb->s_fs_info = NULL;
1811 return -EINVAL;
1812}
1813
1814void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001815 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 va_list args;
1818
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001819 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /* mark sb error */
1821 sb->s_dirt = 1;
1822 }
1823 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001824 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 va_end(args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001826 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1827 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001831 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 va_list args;
1834
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001835 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001836 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 va_end(args);
1838 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001839 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/*
1843 * udf_put_super
1844 *
1845 * PURPOSE
1846 * Prepare for destruction of the superblock.
1847 *
1848 * DESCRIPTION
1849 * Called before the filesystem is unmounted.
1850 *
1851 * HISTORY
1852 * July 1, 1997 - Andrew E. Mileski
1853 * Written, tested, and released.
1854 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001855static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 int i;
1858
1859 if (UDF_SB_VAT(sb))
1860 iput(UDF_SB_VAT(sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001861 if (UDF_SB_NUMPARTS(sb)) {
1862 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1863 UDF_PART_FLAG_UNALLOC_TABLE)
1864 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.
1865 s_table);
1866 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1867 UDF_PART_FLAG_FREED_TABLE)
1868 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.
1869 s_table);
1870 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1871 UDF_PART_FLAG_UNALLOC_BITMAP)
1872 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_uspace);
1873 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
1874 UDF_PART_FLAG_FREED_BITMAP)
1875 UDF_SB_FREE_BITMAP(sb, UDF_SB_PARTITION(sb), s_fspace);
1876 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) ==
1877 UDF_SPARABLE_MAP15) {
1878 for (i = 0; i < 4; i++)
1879 brelse(UDF_SB_TYPESPAR
1880 (sb,
1881 UDF_SB_PARTITION(sb)).s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 }
1884#ifdef CONFIG_UDF_NLS
1885 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1886 unload_nls(UDF_SB(sb)->s_nls_map);
1887#endif
1888 if (!(sb->s_flags & MS_RDONLY))
1889 udf_close_lvid(sb);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001890 brelse(UDF_SB_LVIDBH(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 UDF_SB_FREE(sb);
1892 kfree(sb->s_fs_info);
1893 sb->s_fs_info = NULL;
1894}
1895
1896/*
1897 * udf_stat_fs
1898 *
1899 * PURPOSE
1900 * Return info about the filesystem.
1901 *
1902 * DESCRIPTION
1903 * Called by sys_statfs()
1904 *
1905 * HISTORY
1906 * July 1, 1997 - Andrew E. Mileski
1907 * Written, tested, and released.
1908 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001909static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
David Howells726c3342006-06-23 02:02:58 -07001911 struct super_block *sb = dentry->d_sb;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 buf->f_type = UDF_SUPER_MAGIC;
1914 buf->f_bsize = sb->s_blocksize;
1915 buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1916 buf->f_bfree = udf_count_free(sb);
1917 buf->f_bavail = buf->f_bfree;
1918 buf->f_files = (UDF_SB_LVIDBH(sb) ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001919 (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
1920 le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) +
1921 buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 buf->f_ffree = buf->f_bfree;
1923 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001924 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 return 0;
1927}
1928
1929static unsigned char udf_bitmap_lookup[16] = {
1930 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1931};
1932
1933static unsigned int
1934udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
1935{
1936 struct buffer_head *bh = NULL;
1937 unsigned int accum = 0;
1938 int index;
1939 int block = 0, newblock;
1940 kernel_lb_addr loc;
1941 uint32_t bytes;
1942 uint8_t value;
1943 uint8_t *ptr;
1944 uint16_t ident;
1945 struct spaceBitmapDesc *bm;
1946
1947 lock_kernel();
1948
1949 loc.logicalBlockNum = bitmap->s_extPosition;
1950 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1951 bh = udf_read_ptagged(sb, loc, 0, &ident);
1952
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001953 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 printk(KERN_ERR "udf: udf_count_free failed\n");
1955 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001956 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001957 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk(KERN_ERR "udf: udf_count_free failed\n");
1959 goto out;
1960 }
1961
1962 bm = (struct spaceBitmapDesc *)bh->b_data;
1963 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001964 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1965 ptr = (uint8_t *) bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001967 while (bytes > 0) {
1968 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001970 accum += udf_bitmap_lookup[value & 0x0f];
1971 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 index++;
1973 bytes--;
1974 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001975 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001976 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 newblock = udf_get_lb_pblock(sb, loc, ++block);
1978 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001979 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 udf_debug("read failed\n");
1981 goto out;
1982 }
1983 index = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001984 ptr = (uint8_t *) bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001987 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001989 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 unlock_kernel();
1991
1992 return accum;
1993}
1994
1995static unsigned int
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001996udf_count_free_table(struct super_block *sb, struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07001999 uint32_t elen;
2000 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002002 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 lock_kernel();
2005
Jan Karaff116fc2007-05-08 00:35:14 -07002006 epos.block = UDF_I_LOCATION(table);
2007 epos.offset = sizeof(struct unallocSpaceEntry);
2008 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Jan Karaff116fc2007-05-08 00:35:14 -07002010 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 accum += (elen >> table->i_sb->s_blocksize_bits);
Jan Kara3bf25cb2007-05-08 00:35:16 -07002012 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 unlock_kernel();
2015
2016 return accum;
2017}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002018
2019static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 unsigned int accum = 0;
2022
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002023 if (UDF_SB_LVIDBH(sb)) {
2024 if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) >
2025 UDF_SB_PARTITION(sb)) {
2026 accum =
2027 le32_to_cpu(UDF_SB_LVID(sb)->
2028 freeSpaceTable[UDF_SB_PARTITION(sb)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 if (accum == 0xFFFFFFFF)
2031 accum = 0;
2032 }
2033 }
2034
2035 if (accum)
2036 return accum;
2037
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002038 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
2039 UDF_PART_FLAG_UNALLOC_BITMAP) {
2040 accum +=
2041 udf_count_free_bitmap(sb,
2042 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION
2043 (sb)].s_uspace.
2044 s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002046 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
2047 UDF_PART_FLAG_FREED_BITMAP) {
2048 accum +=
2049 udf_count_free_bitmap(sb,
2050 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION
2051 (sb)].s_fspace.
2052 s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054 if (accum)
2055 return accum;
2056
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002057 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
2058 UDF_PART_FLAG_UNALLOC_TABLE) {
2059 accum +=
2060 udf_count_free_table(sb,
2061 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION
2062 (sb)].s_uspace.
2063 s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002065 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) &
2066 UDF_PART_FLAG_FREED_TABLE) {
2067 accum +=
2068 udf_count_free_table(sb,
2069 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION
2070 (sb)].s_fspace.
2071 s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
2074 return accum;
2075}