blob: 0ca2deb5b992c0f64bc7e2b05ab4715c635dff4f [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
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 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 *);
Jan Karabcec4472007-08-30 23:56:22 -070092static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static 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
Marcin Slusarz6c79e982008-02-08 04:20:30 -080098struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
99{
100 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
101 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
102 __u32 offset = number_of_partitions * 2 * sizeof(uint32_t)/sizeof(uint8_t);
103 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700107static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 int flags, const char *dev_name, void *data,
109 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
David Howells454e2392006-06-23 02:02:57 -0700111 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700115 .owner = THIS_MODULE,
116 .name = "udf",
117 .get_sb = udf_get_sb,
118 .kill_sb = kill_block_super,
119 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120};
121
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700122static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124static struct inode *udf_alloc_inode(struct super_block *sb)
125{
126 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800127 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (!ei)
129 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700130
131 ei->i_unique = 0;
132 ei->i_lenExtents = 0;
133 ei->i_next_alloc_block = 0;
134 ei->i_next_alloc_goal = 0;
135 ei->i_strat4096 = 0;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return &ei->vfs_inode;
138}
139
140static void udf_destroy_inode(struct inode *inode)
141{
142 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
143}
144
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700145static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Christoph Lametera35afb82007-05-16 22:10:57 -0700149 ei->i_ext.i_data = NULL;
150 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static int init_inodecache(void)
154{
155 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
156 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700157 0, (SLAB_RECLAIM_ACCOUNT |
158 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900159 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700160 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return -ENOMEM;
162 return 0;
163}
164
165static void destroy_inodecache(void)
166{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700167 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800171static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700172 .alloc_inode = udf_alloc_inode,
173 .destroy_inode = udf_destroy_inode,
174 .write_inode = udf_write_inode,
175 .delete_inode = udf_delete_inode,
176 .clear_inode = udf_clear_inode,
177 .put_super = udf_put_super,
178 .write_super = udf_write_super,
179 .statfs = udf_statfs,
180 .remount_fs = udf_remount_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700183struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 unsigned char novrs;
185 unsigned int blocksize;
186 unsigned int session;
187 unsigned int lastblock;
188 unsigned int anchor;
189 unsigned int volume;
190 unsigned short partition;
191 unsigned int fileset;
192 unsigned int rootdir;
193 unsigned int flags;
194 mode_t umask;
195 gid_t gid;
196 uid_t uid;
197 struct nls_table *nls_map;
198};
199
200static int __init init_udf_fs(void)
201{
202 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err = init_inodecache();
205 if (err)
206 goto out1;
207 err = register_filesystem(&udf_fstype);
208 if (err)
209 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700212
213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215
216out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return err;
218}
219
220static void __exit exit_udf_fs(void)
221{
222 unregister_filesystem(&udf_fstype);
223 destroy_inodecache();
224}
225
226module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229/*
230 * udf_parse_options
231 *
232 * PURPOSE
233 * Parse mount options.
234 *
235 * DESCRIPTION
236 * The following mount options are supported:
237 *
238 * gid= Set the default group.
239 * umask= Set the default umask.
240 * uid= Set the default user.
241 * bs= Set the block size.
242 * unhide Show otherwise hidden files.
243 * undelete Show deleted files in lists.
244 * adinicb Embed data in the inode (default)
245 * noadinicb Don't embed data in the inode
246 * shortad Use short ad's
247 * longad Use long ad's (default)
248 * nostrict Unset strict conformance
249 * iocharset= Set the NLS character set
250 *
251 * The remaining are for debugging and disaster recovery:
252 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700253 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
255 * The following expect a offset from 0.
256 *
257 * session= Set the CDROM session (default= last session)
258 * anchor= Override standard anchor location. (default= 256)
259 * volume= Override the VolumeDesc location. (unused)
260 * partition= Override the PartitionDesc location. (unused)
261 * lastblock= Set the last block of the filesystem/
262 *
263 * The following expect a offset from the partition root.
264 *
265 * fileset= Override the fileset block location. (unused)
266 * rootdir= Override the root directory location. (unused)
267 * WARNING: overriding the rootdir to a non-directory may
268 * yield highly unpredictable results.
269 *
270 * PRE-CONDITIONS
271 * options Pointer to mount options string.
272 * uopts Pointer to mount options variable.
273 *
274 * POST-CONDITIONS
275 * <return> 1 Mount options parsed okay.
276 * <return> 0 Error parsing mount options.
277 *
278 * HISTORY
279 * July 1, 1997 - Andrew E. Mileski
280 * Written, tested, and released.
281 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283enum {
284 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
285 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
286 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
287 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
288 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800289 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290};
291
292static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700293 {Opt_novrs, "novrs"},
294 {Opt_nostrict, "nostrict"},
295 {Opt_bs, "bs=%u"},
296 {Opt_unhide, "unhide"},
297 {Opt_undelete, "undelete"},
298 {Opt_noadinicb, "noadinicb"},
299 {Opt_adinicb, "adinicb"},
300 {Opt_shortad, "shortad"},
301 {Opt_longad, "longad"},
302 {Opt_uforget, "uid=forget"},
303 {Opt_uignore, "uid=ignore"},
304 {Opt_gforget, "gid=forget"},
305 {Opt_gignore, "gid=ignore"},
306 {Opt_gid, "gid=%u"},
307 {Opt_uid, "uid=%u"},
308 {Opt_umask, "umask=%o"},
309 {Opt_session, "session=%u"},
310 {Opt_lastblock, "lastblock=%u"},
311 {Opt_anchor, "anchor=%u"},
312 {Opt_volume, "volume=%u"},
313 {Opt_partition, "partition=%u"},
314 {Opt_fileset, "fileset=%u"},
315 {Opt_rootdir, "rootdir=%u"},
316 {Opt_utf8, "utf8"},
317 {Opt_iocharset, "iocharset=%s"},
318 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700321static int udf_parse_options(char *options, struct udf_options *uopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 char *p;
324 int option;
325
326 uopt->novrs = 0;
327 uopt->blocksize = 2048;
328 uopt->partition = 0xFFFF;
329 uopt->session = 0xFFFFFFFF;
330 uopt->lastblock = 0;
331 uopt->anchor = 0;
332 uopt->volume = 0xFFFFFFFF;
333 uopt->rootdir = 0xFFFFFFFF;
334 uopt->fileset = 0xFFFFFFFF;
335 uopt->nls_map = NULL;
336
337 if (!options)
338 return 1;
339
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700340 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 substring_t args[MAX_OPT_ARGS];
342 int token;
343 if (!*p)
344 continue;
345
346 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700347 switch (token) {
348 case Opt_novrs:
349 uopt->novrs = 1;
350 case Opt_bs:
351 if (match_int(&args[0], &option))
352 return 0;
353 uopt->blocksize = option;
354 break;
355 case Opt_unhide:
356 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
357 break;
358 case Opt_undelete:
359 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
360 break;
361 case Opt_noadinicb:
362 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
363 break;
364 case Opt_adinicb:
365 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
366 break;
367 case Opt_shortad:
368 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
369 break;
370 case Opt_longad:
371 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
372 break;
373 case Opt_gid:
374 if (match_int(args, &option))
375 return 0;
376 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700377 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700378 break;
379 case Opt_uid:
380 if (match_int(args, &option))
381 return 0;
382 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700383 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700384 break;
385 case Opt_umask:
386 if (match_octal(args, &option))
387 return 0;
388 uopt->umask = option;
389 break;
390 case Opt_nostrict:
391 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
392 break;
393 case Opt_session:
394 if (match_int(args, &option))
395 return 0;
396 uopt->session = option;
397 break;
398 case Opt_lastblock:
399 if (match_int(args, &option))
400 return 0;
401 uopt->lastblock = option;
402 break;
403 case Opt_anchor:
404 if (match_int(args, &option))
405 return 0;
406 uopt->anchor = option;
407 break;
408 case Opt_volume:
409 if (match_int(args, &option))
410 return 0;
411 uopt->volume = option;
412 break;
413 case Opt_partition:
414 if (match_int(args, &option))
415 return 0;
416 uopt->partition = option;
417 break;
418 case Opt_fileset:
419 if (match_int(args, &option))
420 return 0;
421 uopt->fileset = option;
422 break;
423 case Opt_rootdir:
424 if (match_int(args, &option))
425 return 0;
426 uopt->rootdir = option;
427 break;
428 case Opt_utf8:
429 uopt->flags |= (1 << UDF_FLAG_UTF8);
430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 case Opt_iocharset:
433 uopt->nls_map = load_nls(args[0].from);
434 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700437 case Opt_uignore:
438 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
439 break;
440 case Opt_uforget:
441 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
442 break;
443 case Opt_gignore:
444 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
445 break;
446 case Opt_gforget:
447 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
448 break;
449 default:
450 printk(KERN_ERR "udf: bad mount option \"%s\" "
451 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453 }
454 }
455 return 1;
456}
457
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!(sb->s_flags & MS_RDONLY))
463 udf_open_lvid(sb);
464 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 unlock_kernel();
467}
468
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800472 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800474 uopt.flags = sbi->s_flags;
475 uopt.uid = sbi->s_uid;
476 uopt.gid = sbi->s_gid;
477 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479 if (!udf_parse_options(options, &uopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return -EINVAL;
481
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800482 sbi->s_flags = uopt.flags;
483 sbi->s_uid = uopt.uid;
484 sbi->s_gid = uopt.gid;
485 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800487 if (sbi->s_lvid_bh) {
488 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (write_rev > UDF_MAX_WRITE_VERSION)
490 *flags |= MS_RDONLY;
491 }
492
493 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
494 return 0;
495 if (*flags & MS_RDONLY)
496 udf_close_lvid(sb);
497 else
498 udf_open_lvid(sb);
499
500 return 0;
501}
502
503/*
504 * udf_set_blocksize
505 *
506 * PURPOSE
507 * Set the block size to be used in all transfers.
508 *
509 * DESCRIPTION
510 * To allow room for a DMA transfer, it is best to guess big when unsure.
511 * This routine picks 2048 bytes as the blocksize when guessing. This
512 * should be adequate until devices with larger block sizes become common.
513 *
514 * Note that the Linux kernel can currently only deal with blocksizes of
515 * 512, 1024, 2048, 4096, and 8192 bytes.
516 *
517 * PRE-CONDITIONS
518 * sb Pointer to _locked_ superblock.
519 *
520 * POST-CONDITIONS
521 * sb->s_blocksize Blocksize.
522 * sb->s_blocksize_bits log2 of blocksize.
523 * <return> 0 Blocksize is valid.
524 * <return> 1 Blocksize is invalid.
525 *
526 * HISTORY
527 * July 1, 1997 - Andrew E. Mileski
528 * Written, tested, and released.
529 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700530static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 if (!sb_min_blocksize(sb, bsize)) {
533 udf_debug("Bad block size (%d)\n", bsize);
534 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
535 return 0;
536 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return sb->s_blocksize;
539}
540
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700541static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct volStructDesc *vsd = NULL;
544 int sector = 32768;
545 int sectorsize;
546 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 int iso9660 = 0;
548 int nsr02 = 0;
549 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800550 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /* Block size must be a multiple of 512 */
553 if (sb->s_blocksize & 511)
554 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800555 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (sb->s_blocksize < sizeof(struct volStructDesc))
558 sectorsize = sizeof(struct volStructDesc);
559 else
560 sectorsize = sb->s_blocksize;
561
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800562 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700565 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700567 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Read a block */
569 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
570 if (!bh)
571 break;
572
573 /* Look for ISO descriptors */
574 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800575 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700577 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700578 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800580 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
581 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700583 switch (vsd->structType) {
584 case 0:
585 udf_debug("ISO9660 Boot Record found\n");
586 break;
587 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800588 udf_debug("ISO9660 Primary Volume Descriptor "
589 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700590 break;
591 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800592 udf_debug("ISO9660 Supplementary Volume "
593 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 break;
595 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800596 udf_debug("ISO9660 Volume Partition Descriptor "
597 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 break;
599 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800600 udf_debug("ISO9660 Volume Descriptor Set "
601 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700602 break;
603 default:
604 udf_debug("ISO9660 VRS (%u) found\n",
605 vsd->structType);
606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800608 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
609 VSD_STD_ID_LEN))
610 ; /* nothing */
611 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
612 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700613 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800615 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
616 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800618 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
619 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700621 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 if (nsr03)
625 return nsr03;
626 else if (nsr02)
627 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800628 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -1;
630 else
631 return 0;
632}
633
634/*
635 * udf_find_anchor
636 *
637 * PURPOSE
638 * Find an anchor volume descriptor.
639 *
640 * PRE-CONDITIONS
641 * sb Pointer to _locked_ superblock.
642 * lastblock Last block on media.
643 *
644 * POST-CONDITIONS
645 * <return> 1 if not found, 0 if ok
646 *
647 * HISTORY
648 * July 1, 1997 - Andrew E. Mileski
649 * Written, tested, and released.
650 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700651static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800653 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct buffer_head *bh = NULL;
655 uint16_t ident;
656 uint32_t location;
657 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800658 struct udf_sb_info *sbi;
659
660 sbi = UDF_SB(sb);
661 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700665 int last[] = { lastblock, lastblock - 2,
666 lastblock - 150, lastblock - 152,
667 varlastblock, varlastblock - 2,
668 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 lastblock = 0;
671
672 /* Search for an anchor volume descriptor pointer */
673
674 /* according to spec, anchor is in either:
675 * block 256
676 * lastblock-256
677 * lastblock
678 * however, if the disc isn't closed, it could be 512 */
679
Tobias Klausere8c96f82006-03-24 03:15:34 -0800680 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800681 ident = location = 0;
682 if (last[i] >= 0) {
683 bh = sb_bread(sb, last[i]);
684 if (bh) {
685 tag *t = (tag *)bh->b_data;
686 ident = le16_to_cpu(t->tagIdent);
687 location = le32_to_cpu(t->tagLocation);
688 brelse(bh);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800691
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700692 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800693 if (location == last[i] - sbi->s_session) {
694 lastblock = last[i] - sbi->s_session;
695 sbi->s_anchor[0] = lastblock;
696 sbi->s_anchor[1] = lastblock - 256;
697 } else if (location == udf_variable_to_fixed(last[i]) - sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800699 lastblock = udf_variable_to_fixed(last[i]) - sbi->s_session;
700 sbi->s_anchor[0] = lastblock;
701 sbi->s_anchor[1] = lastblock - 256 - sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700702 } else {
703 udf_debug("Anchor found at block %d, location mismatch %d.\n",
704 last[i], location);
705 }
706 } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800708 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800710 ident = location = 0;
711 if (last[i] >= 256) {
712 bh = sb_bread(sb, last[i] - 256);
713 if (bh) {
714 tag *t = (tag *)bh->b_data;
715 ident = le16_to_cpu(t->tagIdent);
716 location = le32_to_cpu(t->tagLocation);
717 brelse(bh);
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800722 location == last[i] - 256 - sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800724 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700725 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800726 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800727 if (last[i] >= 312 + sbi->s_session) {
728 bh = sb_bread(sb, last[i] - 312 - sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800729 if (bh) {
730 tag *t = (tag *)bh->b_data;
731 ident = le16_to_cpu(t->tagIdent);
732 location = le32_to_cpu(t->tagLocation);
733 brelse(bh);
734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700738 location == udf_variable_to_fixed(last[i]) - 256) {
739 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
740 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800741 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 }
744 }
745 }
746 }
747
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700748 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800749 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800750 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800751 if (bh) {
752 tag *t = (tag *)bh->b_data;
753 ident = le16_to_cpu(t->tagIdent);
754 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700755 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (ident == TAG_IDENT_AVDP && location == 256)
758 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
759 }
760 }
761
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800762 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
763 if (sbi->s_anchor[i]) {
764 bh = udf_read_tagged(sb, sbi->s_anchor[i],
765 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800766 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800767 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800768 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700769 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700770 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800771 (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800772 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 }
775 }
776
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800777 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800780static int udf_find_fileset(struct super_block *sb,
781 kernel_lb_addr *fileset,
782 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct buffer_head *bh = NULL;
785 long lastblock;
786 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800787 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700790 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
792
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700793 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700795 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700796 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 1;
798 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800802 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800803 if (!bh) {
804 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 kernel_lb_addr newfileset;
806
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800810 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700811 (newfileset.partitionReferenceNum != 0xFFFF &&
812 fileset->logicalBlockNum == 0xFFFFFFFF &&
813 fileset->partitionReferenceNum == 0xFFFF);
814 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800815 lastblock = sbi->s_partmaps
816 [newfileset.partitionReferenceNum]
817 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 newfileset.logicalBlockNum = 0;
819
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700820 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800821 bh = udf_read_ptagged(sb, newfileset, 0,
822 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 if (!bh) {
824 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 continue;
826 }
827
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828 switch (ident) {
829 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700830 {
831 struct spaceBitmapDesc *sp;
832 sp = (struct spaceBitmapDesc *)bh->b_data;
833 newfileset.logicalBlockNum += 1 +
834 ((le32_to_cpu(sp->numOfBytes) +
835 sizeof(struct spaceBitmapDesc) - 1)
836 >> sb->s_blocksize_bits);
837 brelse(bh);
838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700840 case TAG_IDENT_FSD:
841 *fileset = newfileset;
842 break;
843 default:
844 newfileset.logicalBlockNum++;
845 brelse(bh);
846 bh = NULL;
847 break;
848 }
849 } while (newfileset.logicalBlockNum < lastblock &&
850 fileset->logicalBlockNum == 0xFFFFFFFF &&
851 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 }
854
855 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700856 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 fileset->logicalBlockNum,
859 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800861 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700863 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865 }
866 return 1;
867}
868
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700869static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct primaryVolDesc *pvoldesc;
872 time_t recording;
873 long recording_usec;
874 struct ustr instr;
875 struct ustr outstr;
876
877 pvoldesc = (struct primaryVolDesc *)bh->b_data;
878
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700879 if (udf_stamp_to_time(&recording, &recording_usec,
880 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 kernel_timestamp ts;
882 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800883 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
884 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700885 recording, recording_usec,
886 ts.year, ts.month, ts.day, ts.hour,
887 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800888 UDF_SB(sb)->s_record_time.tv_sec = recording;
889 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700892 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
893 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800894 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800896 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 }
899
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700900 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (udf_CS0toUTF8(&outstr, &instr))
902 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
903 }
904}
905
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700906static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
907 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct fileSetDesc *fset;
910
911 fset = (struct fileSetDesc *)bh->b_data;
912
913 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
914
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800915 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700917 udf_debug("Rootdir at block=%d, partition=%d\n",
918 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Jan Karabcec4472007-08-30 23:56:22 -0700921static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct partitionDesc *p;
924 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800925 struct udf_part_map *map;
926 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800929 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800931 for (i = 0; i < sbi->s_partitions; i++) {
932 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700933 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800934 map->s_partition_num, le16_to_cpu(p->partitionNumber));
935 if (map->s_partition_num == le16_to_cpu(p->partitionNumber)) {
936 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
937 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700938 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800939 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700940 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800941 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700942 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800943 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700944 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800945 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800947 if (!strcmp(p->partitionContents.ident,
948 PD_PARTITION_CONTENTS_NSR02) ||
949 !strcmp(p->partitionContents.ident,
950 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct partitionHeaderDesc *phd;
952
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700953 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700954 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700955 kernel_lb_addr loc = {
956 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
957 .partitionReferenceNum = i,
958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800960 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700961 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800962 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -0700963 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -0700964 return 1;
965 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800966 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700967 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800968 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700970 if (phd->unallocSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 UDF_SB_ALLOC_BITMAP(sb, i, s_uspace);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800972 if (map->s_uspace.s_bitmap != NULL) {
973 map->s_uspace.s_bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700974 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800975 map->s_uspace.s_bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700976 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800977 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700978 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800979 i, map->s_uspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981 }
982 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700983 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700985 kernel_lb_addr loc = {
986 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
987 .partitionReferenceNum = i,
988 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800990 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700991 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800992 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -0700993 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -0700994 return 1;
995 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800996 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700997 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800998 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001000 if (phd->freedSpaceBitmap.extLength) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 UDF_SB_ALLOC_BITMAP(sb, i, s_fspace);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001002 if (map->s_fspace.s_bitmap != NULL) {
1003 map->s_fspace.s_bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001004 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001005 map->s_fspace.s_bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001006 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001007 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001008 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001009 i, map->s_fspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 }
1012 }
1013 break;
1014 }
1015 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001016 if (i == sbi->s_partitions) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001017 udf_debug("Partition (%d) not found in partition map\n",
1018 le16_to_cpu(p->partitionNumber));
1019 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001020 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1021 "block length %d\n",
1022 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001023 map->s_partition_type,
1024 map->s_partition_root,
1025 map->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Jan Karabcec4472007-08-30 23:56:22 -07001027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001030static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1031 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 struct logicalVolDesc *lvd;
1034 int i, j, offset;
1035 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001036 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 lvd = (struct logicalVolDesc *)bh->b_data;
1039
1040 UDF_SB_ALLOC_PARTMAPS(sb, le32_to_cpu(lvd->numPartitionMaps));
1041
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001042 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001043 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001044 i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001045 struct udf_part_map *map = &sbi->s_partmaps[i];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001046 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001047 if (type == 1) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001048 struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001049 map->s_partition_type = UDF_TYPE1_MAP15;
1050 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1051 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1052 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001053 } else if (type == 2) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001054 struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
1055 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) {
1056 if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001057 map->s_partition_type = UDF_VIRTUAL_MAP15;
1058 map->s_partition_func = udf_get_pblock_virt15;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001059 } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001060 map->s_partition_type = UDF_VIRTUAL_MAP20;
1061 map->s_partition_func = udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001063 } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 uint32_t loc;
1065 uint16_t ident;
1066 struct sparingTable *st;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001067 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001069 map->s_partition_type = UDF_SPARABLE_MAP15;
1070 map->s_type_specific.s_sparing.s_packet_len = le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001071 for (j = 0; j < spm->numSparingTables; j++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001072 loc = le32_to_cpu(spm->locSparingTable[j]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001073 map->s_type_specific.s_sparing.s_spar_map[j] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001074 udf_read_tagged(sb, loc, loc, &ident);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001075 if (map->s_type_specific.s_sparing.s_spar_map[j] != NULL) {
1076 st = (struct sparingTable *)map->s_type_specific.s_sparing.s_spar_map[j]->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001077 if (ident != 0 ||
1078 strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001079 brelse(map->s_type_specific.s_sparing.s_spar_map[j]);
1080 map->s_type_specific.s_sparing.s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 }
1083 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001084 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001086 udf_debug("Unknown ident: %s\n",
1087 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 continue;
1089 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001090 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1091 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001094 i, map->s_partition_num, type,
1095 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001098 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001099 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001102 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1103 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001104 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 if (lvd->integritySeqExt.extLength)
1107 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return 0;
1110}
1111
1112/*
1113 * udf_load_logicalvolint
1114 *
1115 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001116static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 struct buffer_head *bh = NULL;
1119 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001120 struct udf_sb_info *sbi = UDF_SB(sb);
1121 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001124 (bh = udf_read_tagged(sb, loc.extLocation,
1125 loc.extLocation, &ident)) &&
1126 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001127 sbi->s_lvid_bh = bh;
1128 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001129
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001130 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001131 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001132 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001133
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001134 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001135 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001137 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001139 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001140 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143/*
1144 * udf_process_sequence
1145 *
1146 * PURPOSE
1147 * Process a main/reserve volume descriptor sequence.
1148 *
1149 * PRE-CONDITIONS
1150 * sb Pointer to _locked_ superblock.
1151 * block First block of first extent of the sequence.
1152 * lastblock Lastblock of first extent of the sequence.
1153 *
1154 * HISTORY
1155 * July 1, 1997 - Andrew E. Mileski
1156 * Written, tested, and released.
1157 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001158static int udf_process_sequence(struct super_block *sb, long block,
1159 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 struct buffer_head *bh = NULL;
1162 struct udf_vds_record vds[VDS_POS_LENGTH];
1163 struct generic_desc *gd;
1164 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001165 int done = 0;
1166 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 uint32_t vdsn;
1168 uint16_t ident;
1169 long next_s = 0, next_e = 0;
1170
1171 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1172
1173 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001174 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001177 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179
1180 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1181 gd = (struct generic_desc *)bh->b_data;
1182 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001184 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001185 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001186 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1188 }
1189 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001190 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1192 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1193 vds[VDS_POS_VOL_DESC_PTR].block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001195 vdp = (struct volDescPtr *)bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001196 next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1197 next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001198 next_e = next_e >> sb->s_blocksize_bits;
1199 next_e += next_s;
1200 }
1201 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001202 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001203 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001204 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001205 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1206 }
1207 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001208 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001209 if (!vds[VDS_POS_PARTITION_DESC].block)
1210 vds[VDS_POS_PARTITION_DESC].block = block;
1211 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001212 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001213 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001214 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001215 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1216 }
1217 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001218 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1219 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1220 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001221 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1222 }
1223 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001224 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001225 vds[VDS_POS_TERMINATING_DESC].block = block;
1226 if (next_e) {
1227 block = next_s;
1228 lastblock = next_e;
1229 next_s = next_e = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001230 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001231 done = 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001232 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001235 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001237 for (i = 0; i < VDS_POS_LENGTH; i++) {
1238 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001239 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1240 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001242 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001244 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 udf_load_logicalvol(sb, bh, fileset);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001246 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001248 if (udf_load_partdesc(sb, bh)) {
1249 brelse(bh);
1250 return 1;
1251 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001252 for (j = vds[i].block + 1;
1253 j < vds[VDS_POS_TERMINATING_DESC].block;
1254 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 bh2 = udf_read_tagged(sb, j, j, &ident);
1256 gd = (struct generic_desc *)bh2->b_data;
1257 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001258 if (udf_load_partdesc(sb,
1259 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001260 brelse(bh);
1261 brelse(bh2);
1262 return 1;
1263 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001264 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001267 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269 }
1270
1271 return 0;
1272}
1273
1274/*
1275 * udf_check_valid()
1276 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 long block;
1280
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001281 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 udf_debug("Validity check skipped because of novrs option\n");
1283 return 0;
1284 }
1285 /* Check that it is NSR02 compliant */
1286 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001287 else {
1288 block = udf_vrs(sb, silent);
1289 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001290 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001291 udf_debug("Failed to read byte 32768. Assuming open "
1292 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001293 if (!sbi->s_last_block)
1294 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001295 return 0;
1296 } else
1297 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001301static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct anchorVolDescPtr *anchor;
1304 uint16_t ident;
1305 struct buffer_head *bh;
1306 long main_s, main_e, reserve_s, reserve_e;
1307 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001308 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (!sb)
1311 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001312 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001314 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1315 if (sbi->s_anchor[i] &&
1316 (bh = udf_read_tagged(sb, sbi->s_anchor[i],
1317 sbi->s_anchor[i], &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 anchor = (struct anchorVolDescPtr *)bh->b_data;
1319
1320 /* Locate the main sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001321 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001322 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 main_e = main_e >> sb->s_blocksize_bits;
1324 main_e += main_s;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* Locate the reserve sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001327 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1328 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 reserve_e = reserve_e >> sb->s_blocksize_bits;
1330 reserve_e += reserve_s;
1331
Jan Kara3bf25cb2007-05-08 00:35:16 -07001332 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Process the main & reserve sequences */
1335 /* responsible for finding the PartitionDesc(s) */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001336 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001337 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 }
1341
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001342 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 udf_debug("No Anchor block found\n");
1344 return 1;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001345 } else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001346 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001348 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001349 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001350 struct udf_part_map *map = &sbi->s_partmaps[i];
1351 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001352 case UDF_VIRTUAL_MAP15:
1353 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001354 if (!sbi->s_last_block) {
1355 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001356 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001358
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001359 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001360 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001361 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001362 return 1;
1363 }
1364
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001365 for (j = 0; j < sbi->s_partitions; j++) {
1366 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001367 if (j != i &&
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001368 map->s_volumeseqnum == map2->s_volumeseqnum &&
1369 map->s_partition_num == map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001370 ino.partitionReferenceNum = j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001371 ino.logicalBlockNum = sbi->s_last_block - map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001372 break;
1373 }
1374 }
1375
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001376 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001377 return 1;
1378
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001379 sbi->s_vat_inode = udf_iget(sb, ino);
1380 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001381 return 1;
1382
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001383 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1384 map->s_type_specific.s_virtual.s_start_offset =
1385 udf_ext0_offset(sbi->s_vat_inode);
1386 map->s_type_specific.s_virtual.s_num_entries =
1387 (sbi->s_vat_inode->i_size - 36) >> 2;
1388 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001389 struct buffer_head *bh = NULL;
1390 uint32_t pos;
1391
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001392 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001393 bh = sb_bread(sb, pos);
1394 if (!bh)
1395 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001396 map->s_type_specific.s_virtual.s_start_offset =
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001397 le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001398 udf_ext0_offset(sbi->s_vat_inode))->lengthHeader) +
1399 udf_ext0_offset(sbi->s_vat_inode);
1400 map->s_type_specific.s_virtual.s_num_entries = (sbi->s_vat_inode->i_size -
1401 map->s_type_specific.s_virtual.s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001402 brelse(bh);
1403 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001404 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1405 map->s_partition_len =
1406 sbi->s_partmaps[ino.partitionReferenceNum].
1407 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 }
1410 return 0;
1411}
1412
1413static void udf_open_lvid(struct super_block *sb)
1414{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001415 struct udf_sb_info *sbi = UDF_SB(sb);
1416 struct buffer_head *bh = sbi->s_lvid_bh;
1417 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int i;
1419 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001420 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1421 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001423 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1424 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001426 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1427 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001429 lvid->descTag.descCRC = cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1430 le16_to_cpu(lvid->descTag.descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001432 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001433 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001435 lvid->descTag.tagChecksum +=
1436 ((uint8_t *) &(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001438 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440}
1441
1442static void udf_close_lvid(struct super_block *sb)
1443{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001444 kernel_timestamp cpu_time;
1445 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001446 struct udf_sb_info *sbi = UDF_SB(sb);
1447 struct buffer_head *bh = sbi->s_lvid_bh;
1448 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001449
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001450 if (!bh)
1451 return;
1452
1453 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1454
1455 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1456 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1457 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1458 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001460 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1461 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1462 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1463 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1464 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1465 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1466 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1467 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001469 lvid->descTag.descCRC =
1470 cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1471 le16_to_cpu(lvid->descTag.descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001473 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001474 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001476 lvid->descTag.tagChecksum +=
1477 ((uint8_t *)&(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001479 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481}
1482
1483/*
1484 * udf_read_super
1485 *
1486 * PURPOSE
1487 * Complete the specified super block.
1488 *
1489 * PRE-CONDITIONS
1490 * sb Pointer to superblock to complete - never NULL.
1491 * sb->s_dev Device to read suberblock from.
1492 * options Pointer to mount options.
1493 * silent Silent flag.
1494 *
1495 * HISTORY
1496 * July 1, 1997 - Andrew E. Mileski
1497 * Written, tested, and released.
1498 */
1499static int udf_fill_super(struct super_block *sb, void *options, int silent)
1500{
1501 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 struct udf_options uopt;
1504 kernel_lb_addr rootdir, fileset;
1505 struct udf_sb_info *sbi;
1506
1507 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1508 uopt.uid = -1;
1509 uopt.gid = -1;
1510 uopt.umask = 0;
1511
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001512 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (!sbi)
1514 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001518 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (!udf_parse_options((char *)options, &uopt))
1521 goto error_out;
1522
1523 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001524 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001526 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 goto error_out;
1528 }
1529#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001530 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 uopt.nls_map = load_nls_default();
1532 if (!uopt.nls_map)
1533 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1534 else
1535 udf_debug("Using default NLS map\n");
1536 }
1537#endif
1538 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1539 uopt.flags |= (1 << UDF_FLAG_UTF8);
1540
1541 fileset.logicalBlockNum = 0xFFFFFFFF;
1542 fileset.partitionReferenceNum = 0xFFFF;
1543
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001544 sbi->s_flags = uopt.flags;
1545 sbi->s_uid = uopt.uid;
1546 sbi->s_gid = uopt.gid;
1547 sbi->s_umask = uopt.umask;
1548 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* Set the block size for all transfers */
1551 if (!udf_set_blocksize(sb, uopt.blocksize))
1552 goto error_out;
1553
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001554 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001555 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001557 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001559 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001561 sbi->s_last_block = uopt.lastblock;
1562 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1563 sbi->s_anchor[2] = uopt.anchor;
1564 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001566 if (udf_check_valid(sb, uopt.novrs, silent)) {
1567 /* read volume recognition sequences */
1568 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001569 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 udf_find_anchor(sb);
1573
1574 /* Fill in the rest of the superblock */
1575 sb->s_op = &udf_sb_ops;
1576 sb->dq_op = NULL;
1577 sb->s_dirt = 0;
1578 sb->s_magic = UDF_SUPER_MAGIC;
1579 sb->s_time_gran = 1000;
1580
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001581 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001582 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto error_out;
1584 }
1585
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001586 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001588 if (sbi->s_lvid_bh) {
1589 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1590 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1591 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1592 /* uint16_t maxUDFWriteRev = le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001594 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001595 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x (max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001596 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001597 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 goto error_out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001599 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 sb->s_flags |= MS_RDONLY;
1601 }
1602
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001603 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1606 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1607 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1608 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1609 }
1610
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001611 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001612 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto error_out;
1614 }
1615
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001616 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags & UDF_PART_FLAG_READ_ONLY) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001617 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001618 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001619 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001620
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001621 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001622 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto error_out;
1624 }
1625
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001626 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001628 udf_time_to_stamp(&ts, sbi->s_record_time);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001629 udf_info("UDF %s (%s) Mounting volume '%s', "
1630 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1631 UDFFS_VERSION, UDFFS_DATE,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001632 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1633 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635 if (!(sb->s_flags & MS_RDONLY))
1636 udf_open_lvid(sb);
1637
1638 /* Assign the root inode */
1639 /* assign inodes by physical block number */
1640 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001641 inode = udf_iget(sb, rootdir);
1642 if (!inode) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001643 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001644 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto error_out;
1646 }
1647
1648 /* Allocate a dentry for the root inode */
1649 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001650 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001651 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 iput(inode);
1653 goto error_out;
1654 }
Jan Kara31170b62007-05-08 00:35:21 -07001655 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001658error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001659 if (sbi->s_vat_inode)
1660 iput(sbi->s_vat_inode);
1661 if (sbi->s_partitions) {
1662 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1663 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1664 iput(map->s_uspace.s_table);
1665 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1666 iput(map->s_fspace.s_table);
1667 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1668 UDF_SB_FREE_BITMAP(sb, sbi->s_partition, s_uspace);
1669 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1670 UDF_SB_FREE_BITMAP(sb, sbi->s_partition, s_fspace);
1671 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001672 for (i = 0; i < 4; i++)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001673 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675#ifdef CONFIG_UDF_NLS
1676 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001677 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678#endif
1679 if (!(sb->s_flags & MS_RDONLY))
1680 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001681 brelse(sbi->s_lvid_bh);
1682
1683 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 kfree(sbi);
1685 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -EINVAL;
1688}
1689
1690void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001691 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 va_list args;
1694
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001695 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /* mark sb error */
1697 sb->s_dirt = 1;
1698 }
1699 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001700 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001702 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001703 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 va_list args;
1710
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001711 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001712 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 va_end(args);
1714 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001715 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718/*
1719 * udf_put_super
1720 *
1721 * PURPOSE
1722 * Prepare for destruction of the superblock.
1723 *
1724 * DESCRIPTION
1725 * Called before the filesystem is unmounted.
1726 *
1727 * HISTORY
1728 * July 1, 1997 - Andrew E. Mileski
1729 * Written, tested, and released.
1730 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001731static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001734 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001736 sbi = UDF_SB(sb);
1737 if (sbi->s_vat_inode)
1738 iput(sbi->s_vat_inode);
1739 if (sbi->s_partitions) {
1740 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1741 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1742 iput(map->s_uspace.s_table);
1743 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1744 iput(map->s_fspace.s_table);
1745 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1746 UDF_SB_FREE_BITMAP(sb, sbi->s_partition, s_uspace);
1747 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1748 UDF_SB_FREE_BITMAP(sb, sbi->s_partition, s_fspace);
1749 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001750 for (i = 0; i < 4; i++)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001751 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753#ifdef CONFIG_UDF_NLS
1754 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001755 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756#endif
1757 if (!(sb->s_flags & MS_RDONLY))
1758 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001759 brelse(sbi->s_lvid_bh);
1760 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 kfree(sb->s_fs_info);
1762 sb->s_fs_info = NULL;
1763}
1764
1765/*
1766 * udf_stat_fs
1767 *
1768 * PURPOSE
1769 * Return info about the filesystem.
1770 *
1771 * DESCRIPTION
1772 * Called by sys_statfs()
1773 *
1774 * HISTORY
1775 * July 1, 1997 - Andrew E. Mileski
1776 * Written, tested, and released.
1777 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001778static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
David Howells726c3342006-06-23 02:02:58 -07001780 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001781 struct udf_sb_info *sbi = UDF_SB(sb);
1782 struct logicalVolIntegrityDescImpUse *lvidiu;
1783
1784 if (sbi->s_lvid_bh != NULL)
1785 lvidiu = udf_sb_lvidiu(sbi);
1786 else
1787 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 buf->f_type = UDF_SUPER_MAGIC;
1790 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001791 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 buf->f_bfree = udf_count_free(sb);
1793 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001794 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1795 le32_to_cpu(lvidiu->numDirs)) : 0)
1796 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 buf->f_ffree = buf->f_bfree;
1798 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001799 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 return 0;
1802}
1803
1804static unsigned char udf_bitmap_lookup[16] = {
1805 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1806};
1807
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001808static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 struct buffer_head *bh = NULL;
1811 unsigned int accum = 0;
1812 int index;
1813 int block = 0, newblock;
1814 kernel_lb_addr loc;
1815 uint32_t bytes;
1816 uint8_t value;
1817 uint8_t *ptr;
1818 uint16_t ident;
1819 struct spaceBitmapDesc *bm;
1820
1821 lock_kernel();
1822
1823 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001824 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 bh = udf_read_ptagged(sb, loc, 0, &ident);
1826
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001827 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 printk(KERN_ERR "udf: udf_count_free failed\n");
1829 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001830 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001831 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 printk(KERN_ERR "udf: udf_count_free failed\n");
1833 goto out;
1834 }
1835
1836 bm = (struct spaceBitmapDesc *)bh->b_data;
1837 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001838 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1839 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 while (bytes > 0) {
1842 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001844 accum += udf_bitmap_lookup[value & 0x0f];
1845 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 index++;
1847 bytes--;
1848 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001849 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001850 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 newblock = udf_get_lb_pblock(sb, loc, ++block);
1852 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 udf_debug("read failed\n");
1855 goto out;
1856 }
1857 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001858 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001861 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001863out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 unlock_kernel();
1865
1866 return accum;
1867}
1868
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001869static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
1871 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07001872 uint32_t elen;
1873 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07001875 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 lock_kernel();
1878
Jan Karaff116fc2007-05-08 00:35:14 -07001879 epos.block = UDF_I_LOCATION(table);
1880 epos.offset = sizeof(struct unallocSpaceEntry);
1881 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001883 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001885
Jan Kara3bf25cb2007-05-08 00:35:16 -07001886 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 unlock_kernel();
1889
1890 return accum;
1891}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001892
1893static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001896 struct udf_sb_info *sbi;
1897 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001899 sbi = UDF_SB(sb);
1900 if (sbi->s_lvid_bh) {
1901 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
1902 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
1903 accum = le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (accum == 0xFFFFFFFF)
1905 accum = 0;
1906 }
1907 }
1908
1909 if (accum)
1910 return accum;
1911
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001912 map = &sbi->s_partmaps[sbi->s_partition];
1913 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001914 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001915 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001917 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001918 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001919 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 if (accum)
1922 return accum;
1923
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001924 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001925 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001926 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001928 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001929 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001930 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
1933 return accum;
1934}