blob: 382be7be5ae34e66b6619d5c7bab8e20852edd48 [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 Gorcunov28de7942007-07-21 04:37:18 -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 Gorcunov28de7942007-07-21 04:37:18 -0700119 ei = (struct udf_inode_info *)kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!ei)
121 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700122
123 ei->i_unique = 0;
124 ei->i_lenExtents = 0;
125 ei->i_next_alloc_block = 0;
126 ei->i_next_alloc_goal = 0;
127 ei->i_strat4096 = 0;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return &ei->vfs_inode;
130}
131
132static void udf_destroy_inode(struct inode *inode)
133{
134 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
135}
136
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700137static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700139 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Christoph Lametera35afb82007-05-16 22:10:57 -0700141 ei->i_ext.i_data = NULL;
142 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static int init_inodecache(void)
146{
147 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
148 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149 0, (SLAB_RECLAIM_ACCOUNT |
150 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900151 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700152 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -ENOMEM;
154 return 0;
155}
156
157static void destroy_inodecache(void)
158{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700159 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800163static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700164 .alloc_inode = udf_alloc_inode,
165 .destroy_inode = udf_destroy_inode,
166 .write_inode = udf_write_inode,
167 .delete_inode = udf_delete_inode,
168 .clear_inode = udf_clear_inode,
169 .put_super = udf_put_super,
170 .write_super = udf_write_super,
171 .statfs = udf_statfs,
172 .remount_fs = udf_remount_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173};
174
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700175struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 unsigned char novrs;
177 unsigned int blocksize;
178 unsigned int session;
179 unsigned int lastblock;
180 unsigned int anchor;
181 unsigned int volume;
182 unsigned short partition;
183 unsigned int fileset;
184 unsigned int rootdir;
185 unsigned int flags;
186 mode_t umask;
187 gid_t gid;
188 uid_t uid;
189 struct nls_table *nls_map;
190};
191
192static int __init init_udf_fs(void)
193{
194 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 err = init_inodecache();
197 if (err)
198 goto out1;
199 err = register_filesystem(&udf_fstype);
200 if (err)
201 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700204
205out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700207
208out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return err;
210}
211
212static void __exit exit_udf_fs(void)
213{
214 unregister_filesystem(&udf_fstype);
215 destroy_inodecache();
216}
217
218module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
222 * udf_parse_options
223 *
224 * PURPOSE
225 * Parse mount options.
226 *
227 * DESCRIPTION
228 * The following mount options are supported:
229 *
230 * gid= Set the default group.
231 * umask= Set the default umask.
232 * uid= Set the default user.
233 * bs= Set the block size.
234 * unhide Show otherwise hidden files.
235 * undelete Show deleted files in lists.
236 * adinicb Embed data in the inode (default)
237 * noadinicb Don't embed data in the inode
238 * shortad Use short ad's
239 * longad Use long ad's (default)
240 * nostrict Unset strict conformance
241 * iocharset= Set the NLS character set
242 *
243 * The remaining are for debugging and disaster recovery:
244 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700245 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 *
247 * The following expect a offset from 0.
248 *
249 * session= Set the CDROM session (default= last session)
250 * anchor= Override standard anchor location. (default= 256)
251 * volume= Override the VolumeDesc location. (unused)
252 * partition= Override the PartitionDesc location. (unused)
253 * lastblock= Set the last block of the filesystem/
254 *
255 * The following expect a offset from the partition root.
256 *
257 * fileset= Override the fileset block location. (unused)
258 * rootdir= Override the root directory location. (unused)
259 * WARNING: overriding the rootdir to a non-directory may
260 * yield highly unpredictable results.
261 *
262 * PRE-CONDITIONS
263 * options Pointer to mount options string.
264 * uopts Pointer to mount options variable.
265 *
266 * POST-CONDITIONS
267 * <return> 1 Mount options parsed okay.
268 * <return> 0 Error parsing mount options.
269 *
270 * HISTORY
271 * July 1, 1997 - Andrew E. Mileski
272 * Written, tested, and released.
273 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275enum {
276 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
277 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
278 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
279 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
280 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800281 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700285 {Opt_novrs, "novrs"},
286 {Opt_nostrict, "nostrict"},
287 {Opt_bs, "bs=%u"},
288 {Opt_unhide, "unhide"},
289 {Opt_undelete, "undelete"},
290 {Opt_noadinicb, "noadinicb"},
291 {Opt_adinicb, "adinicb"},
292 {Opt_shortad, "shortad"},
293 {Opt_longad, "longad"},
294 {Opt_uforget, "uid=forget"},
295 {Opt_uignore, "uid=ignore"},
296 {Opt_gforget, "gid=forget"},
297 {Opt_gignore, "gid=ignore"},
298 {Opt_gid, "gid=%u"},
299 {Opt_uid, "uid=%u"},
300 {Opt_umask, "umask=%o"},
301 {Opt_session, "session=%u"},
302 {Opt_lastblock, "lastblock=%u"},
303 {Opt_anchor, "anchor=%u"},
304 {Opt_volume, "volume=%u"},
305 {Opt_partition, "partition=%u"},
306 {Opt_fileset, "fileset=%u"},
307 {Opt_rootdir, "rootdir=%u"},
308 {Opt_utf8, "utf8"},
309 {Opt_iocharset, "iocharset=%s"},
310 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700313static int udf_parse_options(char *options, struct udf_options *uopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 char *p;
316 int option;
317
318 uopt->novrs = 0;
319 uopt->blocksize = 2048;
320 uopt->partition = 0xFFFF;
321 uopt->session = 0xFFFFFFFF;
322 uopt->lastblock = 0;
323 uopt->anchor = 0;
324 uopt->volume = 0xFFFFFFFF;
325 uopt->rootdir = 0xFFFFFFFF;
326 uopt->fileset = 0xFFFFFFFF;
327 uopt->nls_map = NULL;
328
329 if (!options)
330 return 1;
331
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700332 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 substring_t args[MAX_OPT_ARGS];
334 int token;
335 if (!*p)
336 continue;
337
338 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700339 switch (token) {
340 case Opt_novrs:
341 uopt->novrs = 1;
342 case Opt_bs:
343 if (match_int(&args[0], &option))
344 return 0;
345 uopt->blocksize = option;
346 break;
347 case Opt_unhide:
348 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
349 break;
350 case Opt_undelete:
351 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
352 break;
353 case Opt_noadinicb:
354 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
355 break;
356 case Opt_adinicb:
357 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
358 break;
359 case Opt_shortad:
360 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
361 break;
362 case Opt_longad:
363 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
364 break;
365 case Opt_gid:
366 if (match_int(args, &option))
367 return 0;
368 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700369 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700370 break;
371 case Opt_uid:
372 if (match_int(args, &option))
373 return 0;
374 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700375 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700376 break;
377 case Opt_umask:
378 if (match_octal(args, &option))
379 return 0;
380 uopt->umask = option;
381 break;
382 case Opt_nostrict:
383 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
384 break;
385 case Opt_session:
386 if (match_int(args, &option))
387 return 0;
388 uopt->session = option;
389 break;
390 case Opt_lastblock:
391 if (match_int(args, &option))
392 return 0;
393 uopt->lastblock = option;
394 break;
395 case Opt_anchor:
396 if (match_int(args, &option))
397 return 0;
398 uopt->anchor = option;
399 break;
400 case Opt_volume:
401 if (match_int(args, &option))
402 return 0;
403 uopt->volume = option;
404 break;
405 case Opt_partition:
406 if (match_int(args, &option))
407 return 0;
408 uopt->partition = option;
409 break;
410 case Opt_fileset:
411 if (match_int(args, &option))
412 return 0;
413 uopt->fileset = option;
414 break;
415 case Opt_rootdir:
416 if (match_int(args, &option))
417 return 0;
418 uopt->rootdir = option;
419 break;
420 case Opt_utf8:
421 uopt->flags |= (1 << UDF_FLAG_UTF8);
422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424 case Opt_iocharset:
425 uopt->nls_map = load_nls(args[0].from);
426 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700429 case Opt_uignore:
430 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
431 break;
432 case Opt_uforget:
433 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
434 break;
435 case Opt_gignore:
436 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
437 break;
438 case Opt_gforget:
439 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
440 break;
441 default:
442 printk(KERN_ERR "udf: bad mount option \"%s\" "
443 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return 0;
445 }
446 }
447 return 1;
448}
449
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700450void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (!(sb->s_flags & MS_RDONLY))
455 udf_open_lvid(sb);
456 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 unlock_kernel();
459}
460
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700461static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct udf_options uopt;
464
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 uopt.flags = UDF_SB(sb)->s_flags;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700466 uopt.uid = UDF_SB(sb)->s_uid;
467 uopt.gid = UDF_SB(sb)->s_gid;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 uopt.umask = UDF_SB(sb)->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470 if (!udf_parse_options(options, &uopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return -EINVAL;
472
473 UDF_SB(sb)->s_flags = uopt.flags;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700474 UDF_SB(sb)->s_uid = uopt.uid;
475 UDF_SB(sb)->s_gid = uopt.gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 UDF_SB(sb)->s_umask = uopt.umask;
477
478 if (UDF_SB_LVIDBH(sb)) {
479 int write_rev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
480 if (write_rev > UDF_MAX_WRITE_VERSION)
481 *flags |= MS_RDONLY;
482 }
483
484 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
485 return 0;
486 if (*flags & MS_RDONLY)
487 udf_close_lvid(sb);
488 else
489 udf_open_lvid(sb);
490
491 return 0;
492}
493
494/*
495 * udf_set_blocksize
496 *
497 * PURPOSE
498 * Set the block size to be used in all transfers.
499 *
500 * DESCRIPTION
501 * To allow room for a DMA transfer, it is best to guess big when unsure.
502 * This routine picks 2048 bytes as the blocksize when guessing. This
503 * should be adequate until devices with larger block sizes become common.
504 *
505 * Note that the Linux kernel can currently only deal with blocksizes of
506 * 512, 1024, 2048, 4096, and 8192 bytes.
507 *
508 * PRE-CONDITIONS
509 * sb Pointer to _locked_ superblock.
510 *
511 * POST-CONDITIONS
512 * sb->s_blocksize Blocksize.
513 * sb->s_blocksize_bits log2 of blocksize.
514 * <return> 0 Blocksize is valid.
515 * <return> 1 Blocksize is invalid.
516 *
517 * HISTORY
518 * July 1, 1997 - Andrew E. Mileski
519 * Written, tested, and released.
520 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 if (!sb_min_blocksize(sb, bsize)) {
524 udf_debug("Bad block size (%d)\n", bsize);
525 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
526 return 0;
527 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return sb->s_blocksize;
530}
531
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700532static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct volStructDesc *vsd = NULL;
535 int sector = 32768;
536 int sectorsize;
537 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700538 int iso9660 = 0;
539 int nsr02 = 0;
540 int nsr03 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /* Block size must be a multiple of 512 */
543 if (sb->s_blocksize & 511)
544 return 0;
545
546 if (sb->s_blocksize < sizeof(struct volStructDesc))
547 sectorsize = sizeof(struct volStructDesc);
548 else
549 sectorsize = sb->s_blocksize;
550
551 sector += (UDF_SB_SESSION(sb) << sb->s_blocksize_bits);
552
553 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700554 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Read a block */
558 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
559 if (!bh)
560 break;
561
562 /* Look for ISO descriptors */
563 vsd = (struct volStructDesc *)(bh->b_data +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700564 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700567 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700569 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700571 switch (vsd->structType) {
572 case 0:
573 udf_debug("ISO9660 Boot Record found\n");
574 break;
575 case 1:
576 udf_debug
577 ("ISO9660 Primary Volume Descriptor found\n");
578 break;
579 case 2:
580 udf_debug
581 ("ISO9660 Supplementary Volume Descriptor found\n");
582 break;
583 case 3:
584 udf_debug
585 ("ISO9660 Volume Partition Descriptor found\n");
586 break;
587 case 255:
588 udf_debug
589 ("ISO9660 Volume Descriptor Set Terminator found\n");
590 break;
591 default:
592 udf_debug("ISO9660 VRS (%u) found\n",
593 vsd->structType);
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700596 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01, VSD_STD_ID_LEN)) {
597 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01, VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700598 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700600 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 nsr02 = sector;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700602 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03, VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 nsr03 = sector;
604 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700605 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 if (nsr03)
609 return nsr03;
610 else if (nsr02)
611 return nsr02;
612 else if (sector - (UDF_SB_SESSION(sb) << sb->s_blocksize_bits) == 32768)
613 return -1;
614 else
615 return 0;
616}
617
618/*
619 * udf_find_anchor
620 *
621 * PURPOSE
622 * Find an anchor volume descriptor.
623 *
624 * PRE-CONDITIONS
625 * sb Pointer to _locked_ superblock.
626 * lastblock Last block on media.
627 *
628 * POST-CONDITIONS
629 * <return> 1 if not found, 0 if ok
630 *
631 * HISTORY
632 * July 1, 1997 - Andrew E. Mileski
633 * Written, tested, and released.
634 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700635static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 int lastblock = UDF_SB_LASTBLOCK(sb);
638 struct buffer_head *bh = NULL;
639 uint16_t ident;
640 uint32_t location;
641 int i;
642
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700645 int last[] = { lastblock, lastblock - 2,
646 lastblock - 150, lastblock - 152,
647 varlastblock, varlastblock - 2,
648 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 lastblock = 0;
651
652 /* Search for an anchor volume descriptor pointer */
653
654 /* according to spec, anchor is in either:
655 * block 256
656 * lastblock-256
657 * lastblock
658 * however, if the disc isn't closed, it could be 512 */
659
Tobias Klausere8c96f82006-03-24 03:15:34 -0800660 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700661 if (last[i] < 0 || !(bh = sb_bread(sb, last[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700664 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
665 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700666 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800668
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700669 if (ident == TAG_IDENT_AVDP) {
670 if (location == last[i] - UDF_SB_SESSION(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700671 lastblock = UDF_SB_ANCHOR(sb)[0] = last[i] - UDF_SB_SESSION(sb);
672 UDF_SB_ANCHOR(sb)[1] = last[i] - 256 - UDF_SB_SESSION(sb);
673 } else if (location == udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700675 lastblock = UDF_SB_ANCHOR(sb)[0] = udf_variable_to_fixed(last[i]) - UDF_SB_SESSION(sb);
676 UDF_SB_ANCHOR(sb)[1] = lastblock - 256 - UDF_SB_SESSION(sb);
677 } else {
678 udf_debug("Anchor found at block %d, location mismatch %d.\n",
679 last[i], location);
680 }
681 } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 lastblock = last[i];
683 UDF_SB_ANCHOR(sb)[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700684 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700685 if (last[i] < 256 || !(bh = sb_bread(sb, last[i] - 256))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700687 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700688 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
689 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700690 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700694 location == last[i] - 256 - UDF_SB_SESSION(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 lastblock = last[i];
696 UDF_SB_ANCHOR(sb)[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700697 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700698 if (last[i] < 312 + UDF_SB_SESSION(sb) ||
699 !(bh = sb_bread(sb, last[i] - 312 - UDF_SB_SESSION(sb)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ident = location = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700701 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700702 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
703 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700704 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700708 location == udf_variable_to_fixed(last[i]) - 256) {
709 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
710 lastblock = udf_variable_to_fixed(last[i]);
711 UDF_SB_ANCHOR(sb)[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 }
714 }
715 }
716 }
717
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700718 if (!lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* We havn't found the lastblock. check 312 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720 if ((bh = sb_bread(sb, 312 + UDF_SB_SESSION(sb)))) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700721 ident = le16_to_cpu(((tag *)bh->b_data)->tagIdent);
722 location = le32_to_cpu(((tag *)bh->b_data)->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700723 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (ident == TAG_IDENT_AVDP && location == 256)
726 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
727 }
728 }
729
Tobias Klausere8c96f82006-03-24 03:15:34 -0800730 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700731 if (UDF_SB_ANCHOR(sb)[i]) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700732 if (!(bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i],
733 UDF_SB_ANCHOR(sb)[i], &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 UDF_SB_ANCHOR(sb)[i] = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 } else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700736 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700737 if ((ident != TAG_IDENT_AVDP) &&
738 (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 UDF_SB_ANCHOR(sb)[i] = 0;
740 }
741 }
742 }
743 }
744
745 UDF_SB_LASTBLOCK(sb) = lastblock;
746}
747
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700748static int udf_find_fileset(struct super_block *sb, kernel_lb_addr *fileset, kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct buffer_head *bh = NULL;
751 long lastblock;
752 uint16_t ident;
753
754 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
757
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700758 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700760 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700761 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return 1;
763 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700767 if (!bh) { /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kernel_lb_addr newfileset;
769
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700770/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700772
773 for (newfileset.partitionReferenceNum = UDF_SB_NUMPARTS(sb) - 1;
774 (newfileset.partitionReferenceNum != 0xFFFF &&
775 fileset->logicalBlockNum == 0xFFFFFFFF &&
776 fileset->partitionReferenceNum == 0xFFFF);
777 newfileset.partitionReferenceNum--) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700778 lastblock = UDF_SB_PARTLEN(sb, newfileset.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 newfileset.logicalBlockNum = 0;
780
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 do {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700782 bh = udf_read_ptagged(sb, newfileset, 0, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 if (!bh) {
784 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 continue;
786 }
787
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700788 switch (ident) {
789 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700790 {
791 struct spaceBitmapDesc *sp;
792 sp = (struct spaceBitmapDesc *)bh->b_data;
793 newfileset.logicalBlockNum += 1 +
794 ((le32_to_cpu(sp->numOfBytes) +
795 sizeof(struct spaceBitmapDesc) - 1)
796 >> sb->s_blocksize_bits);
797 brelse(bh);
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700800 case TAG_IDENT_FSD:
801 *fileset = newfileset;
802 break;
803 default:
804 newfileset.logicalBlockNum++;
805 brelse(bh);
806 bh = NULL;
807 break;
808 }
809 } while (newfileset.logicalBlockNum < lastblock &&
810 fileset->logicalBlockNum == 0xFFFFFFFF &&
811 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813 }
814
815 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700816 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700818 fileset->logicalBlockNum,
819 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 UDF_SB_PARTITION(sb) = fileset->partitionReferenceNum;
822 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700823 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return 0;
825 }
826 return 1;
827}
828
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700829static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 struct primaryVolDesc *pvoldesc;
832 time_t recording;
833 long recording_usec;
834 struct ustr instr;
835 struct ustr outstr;
836
837 pvoldesc = (struct primaryVolDesc *)bh->b_data;
838
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839 if (udf_stamp_to_time(&recording, &recording_usec,
840 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kernel_timestamp ts;
842 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700843 udf_debug("recording time %ld/%ld, %04u/%02u/%02u %02u:%02u (%x)\n",
844 recording, recording_usec,
845 ts.year, ts.month, ts.day, ts.hour,
846 ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 UDF_SB_RECORDTIME(sb).tv_sec = recording;
848 UDF_SB_RECORDTIME(sb).tv_nsec = recording_usec * 1000;
849 }
850
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
852 if (udf_CS0toUTF8(&outstr, &instr)) {
853 strncpy(UDF_SB_VOLIDENT(sb), outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 outstr.u_len > 31 ? 31 : outstr.u_len);
855 udf_debug("volIdent[] = '%s'\n", UDF_SB_VOLIDENT(sb));
856 }
857 }
858
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700859 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (udf_CS0toUTF8(&outstr, &instr))
861 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
862 }
863}
864
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700865static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
866 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct fileSetDesc *fset;
869
870 fset = (struct fileSetDesc *)bh->b_data;
871
872 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
873
874 UDF_SB_SERIALNUM(sb) = le16_to_cpu(fset->descTag.tagSerialNum);
875
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700876 udf_debug("Rootdir at block=%d, partition=%d\n",
877 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700880static void udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 struct partitionDesc *p;
883 int i;
884
885 p = (struct partitionDesc *)bh->b_data;
886
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
888 udf_debug("Searching map: (%d == %d)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700889 UDF_SB_PARTMAPS(sb)[i].s_partition_num, le16_to_cpu(p->partitionNumber));
890 if (UDF_SB_PARTMAPS(sb)[i].s_partition_num == le16_to_cpu(p->partitionNumber)) {
891 UDF_SB_PARTLEN(sb,i) = le32_to_cpu(p->partitionLength); /* blocks */
892 UDF_SB_PARTROOT(sb,i) = le32_to_cpu(p->partitionStartingLocation);
893 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
894 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_READ_ONLY;
895 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
896 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_WRITE_ONCE;
897 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
898 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_REWRITABLE;
899 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
900 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700902 if (!strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) ||
903 !strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct partitionHeaderDesc *phd;
905
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700906 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700907 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700908 kernel_lb_addr loc = {
909 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
910 .partitionReferenceNum = i,
911 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700913 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table =
914 udf_iget(sb, loc);
915 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_TABLE;
916 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
917 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700919 if (phd->unallocSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700921 if (UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap != NULL) {
922 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extLength =
923 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
924 UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition =
925 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
926 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_UNALLOC_BITMAP;
927 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
928 i, UDF_SB_PARTMAPS(sb)[i].s_uspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 }
931 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700932 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700933 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700934 kernel_lb_addr loc = {
935 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
936 .partitionReferenceNum = i,
937 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700939 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table =
940 udf_iget(sb, loc);
941 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_TABLE;
942 udf_debug("freedSpaceTable (part %d) @ %ld\n",
943 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700945 if (phd->freedSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700947 if (UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap != NULL) {
948 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extLength =
949 le32_to_cpu(phd->freedSpaceBitmap.extLength);
950 UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition =
951 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
952 UDF_SB_PARTFLAGS(sb,i) |= UDF_PART_FLAG_FREED_BITMAP;
953 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
954 i, UDF_SB_PARTMAPS(sb)[i].s_fspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 }
957 }
958 break;
959 }
960 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 if (i == UDF_SB_NUMPARTS(sb)) {
962 udf_debug("Partition (%d) not found in partition map\n",
963 le16_to_cpu(p->partitionNumber));
964 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700965 udf_debug("Partition (%d:%d type %x) starts at physical %d, block length %d\n",
966 le16_to_cpu(p->partitionNumber), i, UDF_SB_PARTTYPE(sb,i),
967 UDF_SB_PARTROOT(sb,i), UDF_SB_PARTLEN(sb,i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969}
970
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700971static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
972 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct logicalVolDesc *lvd;
975 int i, j, offset;
976 uint8_t type;
977
978 lvd = (struct logicalVolDesc *)bh->b_data;
979
980 UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
981
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 for (i = 0, offset = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700983 i < UDF_SB_NUMPARTS(sb) && offset < le32_to_cpu(lvd->mapTableLength);
984 i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) {
985 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 if (type == 1) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700987 struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
988 UDF_SB_PARTTYPE(sb,i) = UDF_TYPE1_MAP15;
989 UDF_SB_PARTVSN(sb,i) = le16_to_cpu(gpm1->volSeqNum);
990 UDF_SB_PARTNUM(sb,i) = le16_to_cpu(gpm1->partitionNum);
991 UDF_SB_PARTFUNC(sb,i) = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700992 } else if (type == 2) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700993 struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
994 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) {
995 if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) {
996 UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP15;
997 UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt15;
998 } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) {
999 UDF_SB_PARTTYPE(sb,i) = UDF_VIRTUAL_MAP20;
1000 UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001002 } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 uint32_t loc;
1004 uint16_t ident;
1005 struct sparingTable *st;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001006 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001008 UDF_SB_PARTTYPE(sb,i) = UDF_SPARABLE_MAP15;
1009 UDF_SB_TYPESPAR(sb,i).s_packet_len = le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 for (j = 0; j < spm->numSparingTables; j++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001011 loc = le32_to_cpu(spm->locSparingTable[j]);
1012 UDF_SB_TYPESPAR(sb,i).s_spar_map[j] =
1013 udf_read_tagged(sb, loc, loc, &ident);
1014 if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL) {
1015 st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,i).s_spar_map[j]->b_data;
1016 if (ident != 0 ||
1017 strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) {
1018 brelse(UDF_SB_TYPESPAR(sb,i).s_spar_map[j]);
1019 UDF_SB_TYPESPAR(sb,i).s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 }
1022 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001023 UDF_SB_PARTFUNC(sb,i) = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001024 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001025 udf_debug("Unknown ident: %s\n", upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 continue;
1027 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001028 UDF_SB_PARTVSN(sb,i) = le16_to_cpu(upm2->volSeqNum);
1029 UDF_SB_PARTNUM(sb,i) = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001032 i, UDF_SB_PARTNUM(sb,i), type, UDF_SB_PARTVSN(sb,i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001035 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001036 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 *fileset = lelb_to_cpu(la->extLocation);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001039 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1040 fileset->logicalBlockNum,
1041 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 if (lvd->integritySeqExt.extLength)
1044 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
1047}
1048
1049/*
1050 * udf_load_logicalvolint
1051 *
1052 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 struct buffer_head *bh = NULL;
1056 uint16_t ident;
1057
1058 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 (bh = udf_read_tagged(sb, loc.extLocation,
1060 loc.extLocation, &ident)) &&
1061 ident == TAG_IDENT_LVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 UDF_SB_LVIDBH(sb) = bh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (UDF_SB_LVID(sb)->nextIntegrityExt.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001065 udf_load_logicalvolint(sb, leea_to_cpu(UDF_SB_LVID(sb)->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (UDF_SB_LVIDBH(sb) != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001068 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001070 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 if (UDF_SB_LVIDBH(sb) != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001073 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076/*
1077 * udf_process_sequence
1078 *
1079 * PURPOSE
1080 * Process a main/reserve volume descriptor sequence.
1081 *
1082 * PRE-CONDITIONS
1083 * sb Pointer to _locked_ superblock.
1084 * block First block of first extent of the sequence.
1085 * lastblock Lastblock of first extent of the sequence.
1086 *
1087 * HISTORY
1088 * July 1, 1997 - Andrew E. Mileski
1089 * Written, tested, and released.
1090 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001091static int udf_process_sequence(struct super_block *sb, long block, long lastblock,
1092 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct buffer_head *bh = NULL;
1095 struct udf_vds_record vds[VDS_POS_LENGTH];
1096 struct generic_desc *gd;
1097 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001098 int done = 0;
1099 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 uint32_t vdsn;
1101 uint16_t ident;
1102 long next_s = 0, next_e = 0;
1103
1104 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1105
1106 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001107 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001110 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112
1113 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1114 gd = (struct generic_desc *)bh->b_data;
1115 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001116 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001117 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001118 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001119 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001120 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1121 }
1122 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001123 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001124 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1125 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1126 vds[VDS_POS_VOL_DESC_PTR].block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001128 vdp = (struct volDescPtr *)bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001129 next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1130 next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001131 next_e = next_e >> sb->s_blocksize_bits;
1132 next_e += next_s;
1133 }
1134 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001135 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001136 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001137 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001138 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1139 }
1140 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001141 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001142 if (!vds[VDS_POS_PARTITION_DESC].block)
1143 vds[VDS_POS_PARTITION_DESC].block = block;
1144 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001145 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001146 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001147 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001148 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1149 }
1150 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001151 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1152 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1153 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001154 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1155 }
1156 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001157 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001158 vds[VDS_POS_TERMINATING_DESC].block = block;
1159 if (next_e) {
1160 block = next_s;
1161 lastblock = next_e;
1162 next_s = next_e = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001163 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001164 done = 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001165 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001168 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001170 for (i = 0; i < VDS_POS_LENGTH; i++) {
1171 if (vds[i].block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001172 bh = udf_read_tagged(sb, vds[i].block, vds[i].block, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001174 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001176 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 udf_load_logicalvol(sb, bh, fileset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001178 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct buffer_head *bh2 = NULL;
1180 udf_load_partdesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001181 for (j = vds[i].block + 1; j < vds[VDS_POS_TERMINATING_DESC].block; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 bh2 = udf_read_tagged(sb, j, j, &ident);
1183 gd = (struct generic_desc *)bh2->b_data;
1184 if (ident == TAG_IDENT_PD)
1185 udf_load_partdesc(sb, bh2);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001186 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
1188 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001189 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 }
1192
1193 return 0;
1194}
1195
1196/*
1197 * udf_check_valid()
1198 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001199static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 long block;
1202
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 udf_debug("Validity check skipped because of novrs option\n");
1205 return 0;
1206 }
1207 /* Check that it is NSR02 compliant */
1208 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001209 else if ((block = udf_vrs(sb, silent)) == -1) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001210 udf_debug("Failed to read byte 32768. Assuming open disc. "
1211 "Skipping validity check\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (!UDF_SB_LASTBLOCK(sb))
1213 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1214 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001215 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001220static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct anchorVolDescPtr *anchor;
1223 uint16_t ident;
1224 struct buffer_head *bh;
1225 long main_s, main_e, reserve_s, reserve_e;
1226 int i, j;
1227
1228 if (!sb)
1229 return 1;
1230
Tobias Klausere8c96f82006-03-24 03:15:34 -08001231 for (i = 0; i < ARRAY_SIZE(UDF_SB_ANCHOR(sb)); i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001232 if (UDF_SB_ANCHOR(sb)[i] &&
1233 (bh = udf_read_tagged(sb, UDF_SB_ANCHOR(sb)[i],
1234 UDF_SB_ANCHOR(sb)[i], &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 anchor = (struct anchorVolDescPtr *)bh->b_data;
1236
1237 /* Locate the main sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001238 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1239 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 main_e = main_e >> sb->s_blocksize_bits;
1241 main_e += main_s;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* Locate the reserve sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001244 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1245 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 reserve_e = reserve_e >> sb->s_blocksize_bits;
1247 reserve_e += reserve_s;
1248
Jan Kara3bf25cb2007-05-08 00:35:16 -07001249 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* Process the main & reserve sequences */
1252 /* responsible for finding the PartitionDesc(s) */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001253 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
1254 udf_process_sequence(sb, reserve_s, reserve_e, fileset))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 break;
1256 }
1257 }
1258 }
1259
Tobias Klausere8c96f82006-03-24 03:15:34 -08001260 if (i == ARRAY_SIZE(UDF_SB_ANCHOR(sb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 udf_debug("No Anchor block found\n");
1262 return 1;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001263 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 udf_debug("Using anchor in block %d\n", UDF_SB_ANCHOR(sb)[i]);
1265
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001266 for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001267 kernel_lb_addr uninitialized_var(ino);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001268 switch (UDF_SB_PARTTYPE(sb, i)) {
1269 case UDF_VIRTUAL_MAP15:
1270 case UDF_VIRTUAL_MAP20:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001271 if (!UDF_SB_LASTBLOCK(sb)) {
1272 UDF_SB_LASTBLOCK(sb) = udf_get_last_block(sb);
1273 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001275
1276 if (!UDF_SB_LASTBLOCK(sb)) {
1277 udf_debug("Unable to determine Lastblock (For "
1278 "Virtual Partition)\n");
1279 return 1;
1280 }
1281
1282 for (j = 0; j < UDF_SB_NUMPARTS(sb); j++) {
1283 if (j != i && UDF_SB_PARTVSN(sb, i) ==
1284 UDF_SB_PARTVSN(sb, j) &&
1285 UDF_SB_PARTNUM(sb, i) ==
1286 UDF_SB_PARTNUM(sb, j)) {
1287 ino.partitionReferenceNum = j;
1288 ino.logicalBlockNum =
1289 UDF_SB_LASTBLOCK(sb) -
1290 UDF_SB_PARTROOT(sb, j);
1291 break;
1292 }
1293 }
1294
1295 if (j == UDF_SB_NUMPARTS(sb))
1296 return 1;
1297
1298 if (!(UDF_SB_VAT(sb) = udf_iget(sb, ino)))
1299 return 1;
1300
1301 if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP15) {
1302 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1303 udf_ext0_offset(UDF_SB_VAT(sb));
1304 UDF_SB_TYPEVIRT(sb, i).s_num_entries =
1305 (UDF_SB_VAT(sb)->i_size - 36) >> 2;
1306 } else if (UDF_SB_PARTTYPE(sb, i) == UDF_VIRTUAL_MAP20) {
1307 struct buffer_head *bh = NULL;
1308 uint32_t pos;
1309
1310 pos = udf_block_map(UDF_SB_VAT(sb), 0);
1311 bh = sb_bread(sb, pos);
1312 if (!bh)
1313 return 1;
1314 UDF_SB_TYPEVIRT(sb, i).s_start_offset =
1315 le16_to_cpu(((struct
1316 virtualAllocationTable20 *)bh->b_data +
1317 udf_ext0_offset(UDF_SB_VAT(sb)))->
1318 lengthHeader) +
1319 udf_ext0_offset(UDF_SB_VAT(sb));
1320 UDF_SB_TYPEVIRT(sb, i).s_num_entries =
1321 (UDF_SB_VAT(sb)->i_size -
1322 UDF_SB_TYPEVIRT(sb, i).s_start_offset) >> 2;
1323 brelse(bh);
1324 }
1325 UDF_SB_PARTROOT(sb, i) = udf_get_pblock(sb, 0, i, 0);
1326 UDF_SB_PARTLEN(sb, i) = UDF_SB_PARTLEN(sb,
1327 ino.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 }
1330 return 0;
1331}
1332
1333static void udf_open_lvid(struct super_block *sb)
1334{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001335 if (UDF_SB_LVIDBH(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int i;
1337 kernel_timestamp cpu_time;
1338
1339 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1340 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1341 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 UDF_SB_LVID(sb)->recordingDateAndTime =
1343 cpu_to_lets(cpu_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 UDF_SB_LVID(sb)->integrityType = LVID_INTEGRITY_TYPE_OPEN;
1345
1346 UDF_SB_LVID(sb)->descTag.descCRC =
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001347 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1348 le16_to_cpu(UDF_SB_LVID(sb)->descTag.
1349 descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001352 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (i != 4)
1354 UDF_SB_LVID(sb)->descTag.tagChecksum +=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 ((uint8_t *) &
1356 (UDF_SB_LVID(sb)->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1359 }
1360}
1361
1362static void udf_close_lvid(struct super_block *sb)
1363{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001364 kernel_timestamp cpu_time;
1365 int i;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (UDF_SB_LVIDBH(sb) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001368 UDF_SB_LVID(sb)->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1370 UDF_SB_LVIDIU(sb)->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1371 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001372 UDF_SB_LVID(sb)->recordingDateAndTime = cpu_to_lets(cpu_time);
1373 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev))
1374 UDF_SB_LVIDIU(sb)->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1375 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev))
1376 UDF_SB_LVIDIU(sb)->minUDFReadRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1377 if (UDF_SB_UDFREV(sb) > le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev))
1378 UDF_SB_LVIDIU(sb)->minUDFWriteRev = cpu_to_le16(UDF_SB_UDFREV(sb));
1379 UDF_SB_LVID(sb)->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 UDF_SB_LVID(sb)->descTag.descCRC =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001382 cpu_to_le16(udf_crc((char *)UDF_SB_LVID(sb) + sizeof(tag),
1383 le16_to_cpu(UDF_SB_LVID(sb)->descTag.descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 UDF_SB_LVID(sb)->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001386 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (i != 4)
1388 UDF_SB_LVID(sb)->descTag.tagChecksum +=
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001389 ((uint8_t *)&(UDF_SB_LVID(sb)->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 mark_buffer_dirty(UDF_SB_LVIDBH(sb));
1392 }
1393}
1394
1395/*
1396 * udf_read_super
1397 *
1398 * PURPOSE
1399 * Complete the specified super block.
1400 *
1401 * PRE-CONDITIONS
1402 * sb Pointer to superblock to complete - never NULL.
1403 * sb->s_dev Device to read suberblock from.
1404 * options Pointer to mount options.
1405 * silent Silent flag.
1406 *
1407 * HISTORY
1408 * July 1, 1997 - Andrew E. Mileski
1409 * Written, tested, and released.
1410 */
1411static int udf_fill_super(struct super_block *sb, void *options, int silent)
1412{
1413 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001414 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct udf_options uopt;
1416 kernel_lb_addr rootdir, fileset;
1417 struct udf_sb_info *sbi;
1418
1419 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1420 uopt.uid = -1;
1421 uopt.gid = -1;
1422 uopt.umask = 0;
1423
1424 sbi = kmalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
1425 if (!sbi)
1426 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 sb->s_fs_info = sbi;
1429 memset(UDF_SB(sb), 0x00, sizeof(struct udf_sb_info));
1430
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001431 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if (!udf_parse_options((char *)options, &uopt))
1434 goto error_out;
1435
1436 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001437 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001439 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 goto error_out;
1441 }
1442#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001443 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 uopt.nls_map = load_nls_default();
1445 if (!uopt.nls_map)
1446 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1447 else
1448 udf_debug("Using default NLS map\n");
1449 }
1450#endif
1451 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1452 uopt.flags |= (1 << UDF_FLAG_UTF8);
1453
1454 fileset.logicalBlockNum = 0xFFFFFFFF;
1455 fileset.partitionReferenceNum = 0xFFFF;
1456
1457 UDF_SB(sb)->s_flags = uopt.flags;
1458 UDF_SB(sb)->s_uid = uopt.uid;
1459 UDF_SB(sb)->s_gid = uopt.gid;
1460 UDF_SB(sb)->s_umask = uopt.umask;
1461 UDF_SB(sb)->s_nls_map = uopt.nls_map;
1462
1463 /* Set the block size for all transfers */
1464 if (!udf_set_blocksize(sb, uopt.blocksize))
1465 goto error_out;
1466
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001467 if (uopt.session == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 UDF_SB_SESSION(sb) = udf_get_last_session(sb);
1469 else
1470 UDF_SB_SESSION(sb) = uopt.session;
1471
1472 udf_debug("Multi-session=%d\n", UDF_SB_SESSION(sb));
1473
1474 UDF_SB_LASTBLOCK(sb) = uopt.lastblock;
1475 UDF_SB_ANCHOR(sb)[0] = UDF_SB_ANCHOR(sb)[1] = 0;
1476 UDF_SB_ANCHOR(sb)[2] = uopt.anchor;
1477 UDF_SB_ANCHOR(sb)[3] = 256;
1478
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001479 if (udf_check_valid(sb, uopt.novrs, silent)) { /* read volume recognition sequences */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 printk("UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001481 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 udf_find_anchor(sb);
1485
1486 /* Fill in the rest of the superblock */
1487 sb->s_op = &udf_sb_ops;
1488 sb->dq_op = NULL;
1489 sb->s_dirt = 0;
1490 sb->s_magic = UDF_SUPER_MAGIC;
1491 sb->s_time_gran = 1000;
1492
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001493 if (udf_load_partition(sb, &fileset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 printk("UDF-fs: No partition found (1)\n");
1495 goto error_out;
1496 }
1497
1498 udf_debug("Lastblock=%d\n", UDF_SB_LASTBLOCK(sb));
1499
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001500 if (UDF_SB_LVIDBH(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001501 uint16_t minUDFReadRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev);
1502 uint16_t minUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 /* uint16_t maxUDFWriteRev = le16_to_cpu(UDF_SB_LVIDIU(sb)->maxUDFWriteRev); */
1504
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001505 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 printk("UDF-fs: minUDFReadRev=%x (max is %x)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001507 le16_to_cpu(UDF_SB_LVIDIU(sb)->minUDFReadRev),
1508 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto error_out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001510 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 sb->s_flags |= MS_RDONLY;
1512 }
1513
1514 UDF_SB_UDFREV(sb) = minUDFWriteRev;
1515
1516 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1517 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1518 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1519 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1520 }
1521
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001522 if (!UDF_SB_NUMPARTS(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 printk("UDF-fs: No partition found (2)\n");
1524 goto error_out;
1525 }
1526
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001527 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_READ_ONLY) {
1528 printk("UDF-fs: Partition marked readonly; forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001529 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001530 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001531
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001532 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 printk("UDF-fs: No fileset found\n");
1534 goto error_out;
1535 }
1536
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001537 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 kernel_timestamp ts;
1539 udf_time_to_stamp(&ts, UDF_SB_RECORDTIME(sb));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001540 udf_info("UDF %s (%s) Mounting volume '%s', "
1541 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1542 UDFFS_VERSION, UDFFS_DATE,
1543 UDF_SB_VOLIDENT(sb), ts.year, ts.month, ts.day, ts.hour, ts.minute,
1544 ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 if (!(sb->s_flags & MS_RDONLY))
1547 udf_open_lvid(sb);
1548
1549 /* Assign the root inode */
1550 /* assign inodes by physical block number */
1551 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001552 inode = udf_iget(sb, rootdir);
1553 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 printk("UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001555 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 goto error_out;
1557 }
1558
1559 /* Allocate a dentry for the root inode */
1560 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001561 if (!sb->s_root) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 printk("UDF-fs: Couldn't allocate root dentry\n");
1563 iput(inode);
1564 goto error_out;
1565 }
Jan Kara31170b62007-05-08 00:35:21 -07001566 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return 0;
1568
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001569error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (UDF_SB_VAT(sb))
1571 iput(UDF_SB_VAT(sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001572 if (UDF_SB_NUMPARTS(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001573 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1574 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1575 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1576 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1577 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1578 UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_uspace);
1579 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1580 UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_fspace);
1581 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001582 for (i = 0; i < 4; i++)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001583 brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
1586#ifdef CONFIG_UDF_NLS
1587 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1588 unload_nls(UDF_SB(sb)->s_nls_map);
1589#endif
1590 if (!(sb->s_flags & MS_RDONLY))
1591 udf_close_lvid(sb);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001592 brelse(UDF_SB_LVIDBH(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 UDF_SB_FREE(sb);
1594 kfree(sbi);
1595 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 return -EINVAL;
1598}
1599
1600void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001601 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 va_list args;
1604
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001605 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* mark sb error */
1607 sb->s_dirt = 1;
1608 }
1609 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001610 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 va_end(args);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001612 printk (KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
1613 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001617 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 va_list args;
1620
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001621 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001622 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 va_end(args);
1624 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001625 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
1629 * udf_put_super
1630 *
1631 * PURPOSE
1632 * Prepare for destruction of the superblock.
1633 *
1634 * DESCRIPTION
1635 * Called before the filesystem is unmounted.
1636 *
1637 * HISTORY
1638 * July 1, 1997 - Andrew E. Mileski
1639 * Written, tested, and released.
1640 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001641static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int i;
1644
1645 if (UDF_SB_VAT(sb))
1646 iput(UDF_SB_VAT(sb));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001647 if (UDF_SB_NUMPARTS(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001648 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE)
1649 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
1650 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE)
1651 iput(UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
1652 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP)
1653 UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_uspace);
1654 if (UDF_SB_PARTFLAGS(sb, UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP)
1655 UDF_SB_FREE_BITMAP(sb,UDF_SB_PARTITION(sb), s_fspace);
1656 if (UDF_SB_PARTTYPE(sb, UDF_SB_PARTITION(sb)) == UDF_SPARABLE_MAP15) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001657 for (i = 0; i < 4; i++)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001658 brelse(UDF_SB_TYPESPAR(sb, UDF_SB_PARTITION(sb)).s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 }
1661#ifdef CONFIG_UDF_NLS
1662 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
1663 unload_nls(UDF_SB(sb)->s_nls_map);
1664#endif
1665 if (!(sb->s_flags & MS_RDONLY))
1666 udf_close_lvid(sb);
Jan Kara3bf25cb2007-05-08 00:35:16 -07001667 brelse(UDF_SB_LVIDBH(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 UDF_SB_FREE(sb);
1669 kfree(sb->s_fs_info);
1670 sb->s_fs_info = NULL;
1671}
1672
1673/*
1674 * udf_stat_fs
1675 *
1676 * PURPOSE
1677 * Return info about the filesystem.
1678 *
1679 * DESCRIPTION
1680 * Called by sys_statfs()
1681 *
1682 * HISTORY
1683 * July 1, 1997 - Andrew E. Mileski
1684 * Written, tested, and released.
1685 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001686static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
David Howells726c3342006-06-23 02:02:58 -07001688 struct super_block *sb = dentry->d_sb;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 buf->f_type = UDF_SUPER_MAGIC;
1691 buf->f_bsize = sb->s_blocksize;
1692 buf->f_blocks = UDF_SB_PARTLEN(sb, UDF_SB_PARTITION(sb));
1693 buf->f_bfree = udf_count_free(sb);
1694 buf->f_bavail = buf->f_bfree;
1695 buf->f_files = (UDF_SB_LVIDBH(sb) ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 (le32_to_cpu(UDF_SB_LVIDIU(sb)->numFiles) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001697 le32_to_cpu(UDF_SB_LVIDIU(sb)->numDirs)) : 0) + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 buf->f_ffree = buf->f_bfree;
1699 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001700 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 return 0;
1703}
1704
1705static unsigned char udf_bitmap_lookup[16] = {
1706 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1707};
1708
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001709static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct buffer_head *bh = NULL;
1712 unsigned int accum = 0;
1713 int index;
1714 int block = 0, newblock;
1715 kernel_lb_addr loc;
1716 uint32_t bytes;
1717 uint8_t value;
1718 uint8_t *ptr;
1719 uint16_t ident;
1720 struct spaceBitmapDesc *bm;
1721
1722 lock_kernel();
1723
1724 loc.logicalBlockNum = bitmap->s_extPosition;
1725 loc.partitionReferenceNum = UDF_SB_PARTITION(sb);
1726 bh = udf_read_ptagged(sb, loc, 0, &ident);
1727
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001728 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 printk(KERN_ERR "udf: udf_count_free failed\n");
1730 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001731 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001732 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 printk(KERN_ERR "udf: udf_count_free failed\n");
1734 goto out;
1735 }
1736
1737 bm = (struct spaceBitmapDesc *)bh->b_data;
1738 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001739 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1740 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001742 while (bytes > 0) {
1743 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001745 accum += udf_bitmap_lookup[value & 0x0f];
1746 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 index++;
1748 bytes--;
1749 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001750 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001751 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 newblock = udf_get_lb_pblock(sb, loc, ++block);
1753 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001754 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 udf_debug("read failed\n");
1756 goto out;
1757 }
1758 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001759 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001762 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001764out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 unlock_kernel();
1766
1767 return accum;
1768}
1769
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001770static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07001773 uint32_t elen;
1774 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07001776 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 lock_kernel();
1779
Jan Karaff116fc2007-05-08 00:35:14 -07001780 epos.block = UDF_I_LOCATION(table);
1781 epos.offset = sizeof(struct unallocSpaceEntry);
1782 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001784 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 accum += (elen >> table->i_sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001786 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001787 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 unlock_kernel();
1790
1791 return accum;
1792}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001793
1794static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 unsigned int accum = 0;
1797
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001798 if (UDF_SB_LVIDBH(sb)) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001799 if (le32_to_cpu(UDF_SB_LVID(sb)->numOfPartitions) > UDF_SB_PARTITION(sb)) {
1800 accum = le32_to_cpu(UDF_SB_LVID(sb)->freeSpaceTable[UDF_SB_PARTITION(sb)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (accum == 0xFFFFFFFF)
1802 accum = 0;
1803 }
1804 }
1805
1806 if (accum)
1807 return accum;
1808
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_BITMAP) {
1810 accum += udf_count_free_bitmap(sb,
1811 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001813 if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_BITMAP) {
1814 accum += udf_count_free_bitmap(sb,
1815 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 if (accum)
1818 return accum;
1819
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001820 if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_UNALLOC_TABLE) {
1821 accum += udf_count_free_table(sb,
1822 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001824 if (UDF_SB_PARTFLAGS(sb,UDF_SB_PARTITION(sb)) & UDF_PART_FLAG_FREED_TABLE) {
1825 accum += udf_count_free_table(sb,
1826 UDF_SB_PARTMAPS(sb)[UDF_SB_PARTITION(sb)].s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
1829 return accum;
1830}