blob: 7f75a949e1525687369777813008fb90a972dbd5 [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>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/byteorder.h>
57
58#include <linux/udf_fs.h>
59#include "udf_sb.h"
60#include "udf_i.h"
61
62#include <linux/init.h>
63#include <asm/uaccess.h>
64
65#define VDS_POS_PRIMARY_VOL_DESC 0
66#define VDS_POS_UNALLOC_SPACE_DESC 1
67#define VDS_POS_LOGICAL_VOL_DESC 2
68#define VDS_POS_PARTITION_DESC 3
69#define VDS_POS_IMP_USE_VOL_DESC 4
70#define VDS_POS_VOL_DESC_PTR 5
71#define VDS_POS_TERMINATING_DESC 6
72#define VDS_POS_LENGTH 7
73
74static char error_buf[1024];
75
76/* These are the "meat" - everything else is stuffing */
77static int udf_fill_super(struct super_block *, void *, int);
78static void udf_put_super(struct super_block *);
79static void udf_write_super(struct super_block *);
80static int udf_remount_fs(struct super_block *, int *, char *);
81static int udf_check_valid(struct super_block *, int, int);
82static int udf_vrs(struct super_block *sb, int silent);
83static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
85 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
87static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
89 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
Jan Karabcec4472007-08-30 23:56:22 -070093static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_open_lvid(struct super_block *);
95static void udf_close_lvid(struct super_block *);
96static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070097static int udf_statfs(struct dentry *, struct kstatfs *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Marcin Slusarz6c79e982008-02-08 04:20:30 -080099struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
100{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800101 struct logicalVolIntegrityDesc *lvid =
102 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800103 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800104 __u32 offset = number_of_partitions * 2 *
105 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700110static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 int flags, const char *dev_name, void *data,
112 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
David Howells454e2392006-06-23 02:02:57 -0700114 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700118 .owner = THIS_MODULE,
119 .name = "udf",
120 .get_sb = udf_get_sb,
121 .kill_sb = kill_block_super,
122 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700125static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static struct inode *udf_alloc_inode(struct super_block *sb)
128{
129 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800130 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!ei)
132 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700133
134 ei->i_unique = 0;
135 ei->i_lenExtents = 0;
136 ei->i_next_alloc_block = 0;
137 ei->i_next_alloc_goal = 0;
138 ei->i_strat4096 = 0;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return &ei->vfs_inode;
141}
142
143static void udf_destroy_inode(struct inode *inode)
144{
145 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
146}
147
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700148static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Lametera35afb82007-05-16 22:10:57 -0700152 ei->i_ext.i_data = NULL;
153 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156static int init_inodecache(void)
157{
158 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
159 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160 0, (SLAB_RECLAIM_ACCOUNT |
161 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900162 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700163 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENOMEM;
165 return 0;
166}
167
168static void destroy_inodecache(void)
169{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700170 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800174static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175 .alloc_inode = udf_alloc_inode,
176 .destroy_inode = udf_destroy_inode,
177 .write_inode = udf_write_inode,
178 .delete_inode = udf_delete_inode,
179 .clear_inode = udf_clear_inode,
180 .put_super = udf_put_super,
181 .write_super = udf_write_super,
182 .statfs = udf_statfs,
183 .remount_fs = udf_remount_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};
185
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700186struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned char novrs;
188 unsigned int blocksize;
189 unsigned int session;
190 unsigned int lastblock;
191 unsigned int anchor;
192 unsigned int volume;
193 unsigned short partition;
194 unsigned int fileset;
195 unsigned int rootdir;
196 unsigned int flags;
197 mode_t umask;
198 gid_t gid;
199 uid_t uid;
200 struct nls_table *nls_map;
201};
202
203static int __init init_udf_fs(void)
204{
205 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 err = init_inodecache();
208 if (err)
209 goto out1;
210 err = register_filesystem(&udf_fstype);
211 if (err)
212 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700215
216out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
219out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return err;
221}
222
223static void __exit exit_udf_fs(void)
224{
225 unregister_filesystem(&udf_fstype);
226 destroy_inodecache();
227}
228
229module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700230module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800232static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
233{
234 struct udf_sb_info *sbi = UDF_SB(sb);
235
236 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
237 GFP_KERNEL);
238 if (!sbi->s_partmaps) {
239 udf_error(sb, __FUNCTION__,
240 "Unable to allocate space for %d partition maps",
241 count);
242 sbi->s_partitions = 0;
243 return -ENOMEM;
244 }
245
246 sbi->s_partitions = count;
247 return 0;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * udf_parse_options
252 *
253 * PURPOSE
254 * Parse mount options.
255 *
256 * DESCRIPTION
257 * The following mount options are supported:
258 *
259 * gid= Set the default group.
260 * umask= Set the default umask.
261 * uid= Set the default user.
262 * bs= Set the block size.
263 * unhide Show otherwise hidden files.
264 * undelete Show deleted files in lists.
265 * adinicb Embed data in the inode (default)
266 * noadinicb Don't embed data in the inode
267 * shortad Use short ad's
268 * longad Use long ad's (default)
269 * nostrict Unset strict conformance
270 * iocharset= Set the NLS character set
271 *
272 * The remaining are for debugging and disaster recovery:
273 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700274 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 *
276 * The following expect a offset from 0.
277 *
278 * session= Set the CDROM session (default= last session)
279 * anchor= Override standard anchor location. (default= 256)
280 * volume= Override the VolumeDesc location. (unused)
281 * partition= Override the PartitionDesc location. (unused)
282 * lastblock= Set the last block of the filesystem/
283 *
284 * The following expect a offset from the partition root.
285 *
286 * fileset= Override the fileset block location. (unused)
287 * rootdir= Override the root directory location. (unused)
288 * WARNING: overriding the rootdir to a non-directory may
289 * yield highly unpredictable results.
290 *
291 * PRE-CONDITIONS
292 * options Pointer to mount options string.
293 * uopts Pointer to mount options variable.
294 *
295 * POST-CONDITIONS
296 * <return> 1 Mount options parsed okay.
297 * <return> 0 Error parsing mount options.
298 *
299 * HISTORY
300 * July 1, 1997 - Andrew E. Mileski
301 * Written, tested, and released.
302 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304enum {
305 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
306 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
307 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
308 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
309 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800310 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700314 {Opt_novrs, "novrs"},
315 {Opt_nostrict, "nostrict"},
316 {Opt_bs, "bs=%u"},
317 {Opt_unhide, "unhide"},
318 {Opt_undelete, "undelete"},
319 {Opt_noadinicb, "noadinicb"},
320 {Opt_adinicb, "adinicb"},
321 {Opt_shortad, "shortad"},
322 {Opt_longad, "longad"},
323 {Opt_uforget, "uid=forget"},
324 {Opt_uignore, "uid=ignore"},
325 {Opt_gforget, "gid=forget"},
326 {Opt_gignore, "gid=ignore"},
327 {Opt_gid, "gid=%u"},
328 {Opt_uid, "uid=%u"},
329 {Opt_umask, "umask=%o"},
330 {Opt_session, "session=%u"},
331 {Opt_lastblock, "lastblock=%u"},
332 {Opt_anchor, "anchor=%u"},
333 {Opt_volume, "volume=%u"},
334 {Opt_partition, "partition=%u"},
335 {Opt_fileset, "fileset=%u"},
336 {Opt_rootdir, "rootdir=%u"},
337 {Opt_utf8, "utf8"},
338 {Opt_iocharset, "iocharset=%s"},
339 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340};
341
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700342static int udf_parse_options(char *options, struct udf_options *uopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 char *p;
345 int option;
346
347 uopt->novrs = 0;
348 uopt->blocksize = 2048;
349 uopt->partition = 0xFFFF;
350 uopt->session = 0xFFFFFFFF;
351 uopt->lastblock = 0;
352 uopt->anchor = 0;
353 uopt->volume = 0xFFFFFFFF;
354 uopt->rootdir = 0xFFFFFFFF;
355 uopt->fileset = 0xFFFFFFFF;
356 uopt->nls_map = NULL;
357
358 if (!options)
359 return 1;
360
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700361 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 substring_t args[MAX_OPT_ARGS];
363 int token;
364 if (!*p)
365 continue;
366
367 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700368 switch (token) {
369 case Opt_novrs:
370 uopt->novrs = 1;
371 case Opt_bs:
372 if (match_int(&args[0], &option))
373 return 0;
374 uopt->blocksize = option;
375 break;
376 case Opt_unhide:
377 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
378 break;
379 case Opt_undelete:
380 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
381 break;
382 case Opt_noadinicb:
383 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
384 break;
385 case Opt_adinicb:
386 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
387 break;
388 case Opt_shortad:
389 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
390 break;
391 case Opt_longad:
392 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
393 break;
394 case Opt_gid:
395 if (match_int(args, &option))
396 return 0;
397 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700398 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700399 break;
400 case Opt_uid:
401 if (match_int(args, &option))
402 return 0;
403 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700404 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700405 break;
406 case Opt_umask:
407 if (match_octal(args, &option))
408 return 0;
409 uopt->umask = option;
410 break;
411 case Opt_nostrict:
412 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
413 break;
414 case Opt_session:
415 if (match_int(args, &option))
416 return 0;
417 uopt->session = option;
418 break;
419 case Opt_lastblock:
420 if (match_int(args, &option))
421 return 0;
422 uopt->lastblock = option;
423 break;
424 case Opt_anchor:
425 if (match_int(args, &option))
426 return 0;
427 uopt->anchor = option;
428 break;
429 case Opt_volume:
430 if (match_int(args, &option))
431 return 0;
432 uopt->volume = option;
433 break;
434 case Opt_partition:
435 if (match_int(args, &option))
436 return 0;
437 uopt->partition = option;
438 break;
439 case Opt_fileset:
440 if (match_int(args, &option))
441 return 0;
442 uopt->fileset = option;
443 break;
444 case Opt_rootdir:
445 if (match_int(args, &option))
446 return 0;
447 uopt->rootdir = option;
448 break;
449 case Opt_utf8:
450 uopt->flags |= (1 << UDF_FLAG_UTF8);
451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700453 case Opt_iocharset:
454 uopt->nls_map = load_nls(args[0].from);
455 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458 case Opt_uignore:
459 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
460 break;
461 case Opt_uforget:
462 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
463 break;
464 case Opt_gignore:
465 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
466 break;
467 case Opt_gforget:
468 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
469 break;
470 default:
471 printk(KERN_ERR "udf: bad mount option \"%s\" "
472 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474 }
475 }
476 return 1;
477}
478
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800479static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!(sb->s_flags & MS_RDONLY))
484 udf_open_lvid(sb);
485 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unlock_kernel();
488}
489
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700490static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800493 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800495 uopt.flags = sbi->s_flags;
496 uopt.uid = sbi->s_uid;
497 uopt.gid = sbi->s_gid;
498 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700500 if (!udf_parse_options(options, &uopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EINVAL;
502
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800503 sbi->s_flags = uopt.flags;
504 sbi->s_uid = uopt.uid;
505 sbi->s_gid = uopt.gid;
506 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800508 if (sbi->s_lvid_bh) {
509 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (write_rev > UDF_MAX_WRITE_VERSION)
511 *flags |= MS_RDONLY;
512 }
513
514 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
515 return 0;
516 if (*flags & MS_RDONLY)
517 udf_close_lvid(sb);
518 else
519 udf_open_lvid(sb);
520
521 return 0;
522}
523
524/*
525 * udf_set_blocksize
526 *
527 * PURPOSE
528 * Set the block size to be used in all transfers.
529 *
530 * DESCRIPTION
531 * To allow room for a DMA transfer, it is best to guess big when unsure.
532 * This routine picks 2048 bytes as the blocksize when guessing. This
533 * should be adequate until devices with larger block sizes become common.
534 *
535 * Note that the Linux kernel can currently only deal with blocksizes of
536 * 512, 1024, 2048, 4096, and 8192 bytes.
537 *
538 * PRE-CONDITIONS
539 * sb Pointer to _locked_ superblock.
540 *
541 * POST-CONDITIONS
542 * sb->s_blocksize Blocksize.
543 * sb->s_blocksize_bits log2 of blocksize.
544 * <return> 0 Blocksize is valid.
545 * <return> 1 Blocksize is invalid.
546 *
547 * HISTORY
548 * July 1, 1997 - Andrew E. Mileski
549 * Written, tested, and released.
550 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 if (!sb_min_blocksize(sb, bsize)) {
554 udf_debug("Bad block size (%d)\n", bsize);
555 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
556 return 0;
557 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return sb->s_blocksize;
560}
561
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700562static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct volStructDesc *vsd = NULL;
565 int sector = 32768;
566 int sectorsize;
567 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700568 int iso9660 = 0;
569 int nsr02 = 0;
570 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800571 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Block size must be a multiple of 512 */
574 if (sb->s_blocksize & 511)
575 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800576 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (sb->s_blocksize < sizeof(struct volStructDesc))
579 sectorsize = sizeof(struct volStructDesc);
580 else
581 sectorsize = sb->s_blocksize;
582
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800583 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700586 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700588 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Read a block */
590 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
591 if (!bh)
592 break;
593
594 /* Look for ISO descriptors */
595 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800596 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700599 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800601 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
602 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700604 switch (vsd->structType) {
605 case 0:
606 udf_debug("ISO9660 Boot Record found\n");
607 break;
608 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800609 udf_debug("ISO9660 Primary Volume Descriptor "
610 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 break;
612 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800613 udf_debug("ISO9660 Supplementary Volume "
614 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 break;
616 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800617 udf_debug("ISO9660 Volume Partition Descriptor "
618 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700619 break;
620 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800621 udf_debug("ISO9660 Volume Descriptor Set "
622 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700623 break;
624 default:
625 udf_debug("ISO9660 VRS (%u) found\n",
626 vsd->structType);
627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800629 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
630 VSD_STD_ID_LEN))
631 ; /* nothing */
632 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
633 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700634 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800636 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
637 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800639 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
640 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700642 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 if (nsr03)
646 return nsr03;
647 else if (nsr02)
648 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800649 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -1;
651 else
652 return 0;
653}
654
655/*
656 * udf_find_anchor
657 *
658 * PURPOSE
659 * Find an anchor volume descriptor.
660 *
661 * PRE-CONDITIONS
662 * sb Pointer to _locked_ superblock.
663 * lastblock Last block on media.
664 *
665 * POST-CONDITIONS
666 * <return> 1 if not found, 0 if ok
667 *
668 * HISTORY
669 * July 1, 1997 - Andrew E. Mileski
670 * Written, tested, and released.
671 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700672static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800674 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct buffer_head *bh = NULL;
676 uint16_t ident;
677 uint32_t location;
678 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800679 struct udf_sb_info *sbi;
680
681 sbi = UDF_SB(sb);
682 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700684 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700686 int last[] = { lastblock, lastblock - 2,
687 lastblock - 150, lastblock - 152,
688 varlastblock, varlastblock - 2,
689 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 lastblock = 0;
692
693 /* Search for an anchor volume descriptor pointer */
694
695 /* according to spec, anchor is in either:
696 * block 256
697 * lastblock-256
698 * lastblock
699 * however, if the disc isn't closed, it could be 512 */
700
Tobias Klausere8c96f82006-03-24 03:15:34 -0800701 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800702 ident = location = 0;
703 if (last[i] >= 0) {
704 bh = sb_bread(sb, last[i]);
705 if (bh) {
706 tag *t = (tag *)bh->b_data;
707 ident = le16_to_cpu(t->tagIdent);
708 location = le32_to_cpu(t->tagLocation);
709 brelse(bh);
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800712
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700713 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800714 if (location == last[i] - sbi->s_session) {
715 lastblock = last[i] - sbi->s_session;
716 sbi->s_anchor[0] = lastblock;
717 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800718 } else if (location ==
719 udf_variable_to_fixed(last[i]) -
720 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800722 lastblock =
723 udf_variable_to_fixed(last[i]) -
724 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800725 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800726 sbi->s_anchor[1] = lastblock - 256 -
727 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700728 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800729 udf_debug("Anchor found at block %d, "
730 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700731 last[i], location);
732 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800733 } else if (ident == TAG_IDENT_FE ||
734 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800736 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700737 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800738 ident = location = 0;
739 if (last[i] >= 256) {
740 bh = sb_bread(sb, last[i] - 256);
741 if (bh) {
742 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800743 ident = le16_to_cpu(
744 t->tagIdent);
745 location = le32_to_cpu(
746 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800747 brelse(bh);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 location == last[i] - 256 -
753 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800755 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700756 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800757 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800758 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 bh = sb_bread(sb,
760 last[i] - 312 -
761 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800762 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 tag *t = (tag *)
764 bh->b_data;
765 ident = le16_to_cpu(
766 t->tagIdent);
767 location = le32_to_cpu(
768 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800769 brelse(bh);
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700774 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800775 UDF_SET_FLAG(sb,
776 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700777 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800778 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780 }
781 }
782 }
783 }
784
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700785 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800786 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800787 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800788 if (bh) {
789 tag *t = (tag *)bh->b_data;
790 ident = le16_to_cpu(t->tagIdent);
791 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700792 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (ident == TAG_IDENT_AVDP && location == 256)
795 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
796 }
797 }
798
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800799 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
800 if (sbi->s_anchor[i]) {
801 bh = udf_read_tagged(sb, sbi->s_anchor[i],
802 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800803 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800804 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800805 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700806 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800808 (i || (ident != TAG_IDENT_FE &&
809 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800810 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
813 }
814
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800815 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800818static int udf_find_fileset(struct super_block *sb,
819 kernel_lb_addr *fileset,
820 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 struct buffer_head *bh = NULL;
823 long lastblock;
824 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800825 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
830
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700831 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700833 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700834 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 1;
836 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800841 if (!bh) {
842 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kernel_lb_addr newfileset;
844
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700845/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700847
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800848 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700849 (newfileset.partitionReferenceNum != 0xFFFF &&
850 fileset->logicalBlockNum == 0xFFFFFFFF &&
851 fileset->partitionReferenceNum == 0xFFFF);
852 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800853 lastblock = sbi->s_partmaps
854 [newfileset.partitionReferenceNum]
855 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 newfileset.logicalBlockNum = 0;
857
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800859 bh = udf_read_ptagged(sb, newfileset, 0,
860 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700861 if (!bh) {
862 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 continue;
864 }
865
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 switch (ident) {
867 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700868 {
869 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800870 sp = (struct spaceBitmapDesc *)
871 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700872 newfileset.logicalBlockNum += 1 +
873 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800874 sizeof(struct spaceBitmapDesc)
875 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700876 brelse(bh);
877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700879 case TAG_IDENT_FSD:
880 *fileset = newfileset;
881 break;
882 default:
883 newfileset.logicalBlockNum++;
884 brelse(bh);
885 bh = NULL;
886 break;
887 }
888 } while (newfileset.logicalBlockNum < lastblock &&
889 fileset->logicalBlockNum == 0xFFFFFFFF &&
890 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 }
893
894 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700895 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700897 fileset->logicalBlockNum,
898 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800900 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700902 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904 }
905 return 1;
906}
907
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700908static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct primaryVolDesc *pvoldesc;
911 time_t recording;
912 long recording_usec;
913 struct ustr instr;
914 struct ustr outstr;
915
916 pvoldesc = (struct primaryVolDesc *)bh->b_data;
917
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700918 if (udf_stamp_to_time(&recording, &recording_usec,
919 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 kernel_timestamp ts;
921 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800922 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
923 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700924 recording, recording_usec,
925 ts.year, ts.month, ts.day, ts.hour,
926 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800927 UDF_SB(sb)->s_record_time.tv_sec = recording;
928 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
Marcin Slusarz4b111112008-02-08 04:20:36 -0800931 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700932 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800933 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800935 udf_debug("volIdent[] = '%s'\n",
936 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Marcin Slusarz4b111112008-02-08 04:20:36 -0800939 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (udf_CS0toUTF8(&outstr, &instr))
941 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700944static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
945 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct fileSetDesc *fset;
948
949 fset = (struct fileSetDesc *)bh->b_data;
950
951 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
952
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800953 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700955 udf_debug("Rootdir at block=%d, partition=%d\n",
956 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800959int udf_compute_nr_groups(struct super_block *sb, u32 partition)
960{
961 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
962 return (map->s_partition_len +
963 (sizeof(struct spaceBitmapDesc) << 3) +
964 (sb->s_blocksize * 8) - 1) /
965 (sb->s_blocksize * 8);
966}
967
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800968static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
969{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800970 struct udf_bitmap *bitmap;
971 int nr_groups;
972 int size;
973
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800974 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800975 size = sizeof(struct udf_bitmap) +
976 (sizeof(struct buffer_head *) * nr_groups);
977
978 if (size <= PAGE_SIZE)
979 bitmap = kmalloc(size, GFP_KERNEL);
980 else
981 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
982
983 if (bitmap == NULL) {
984 udf_error(sb, __FUNCTION__,
985 "Unable to allocate space for bitmap "
986 "and %d buffer_head pointers", nr_groups);
987 return NULL;
988 }
989
990 memset(bitmap, 0x00, size);
991 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
992 bitmap->s_nr_groups = nr_groups;
993 return bitmap;
994}
995
Jan Karabcec4472007-08-30 23:56:22 -0700996static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 struct partitionDesc *p;
999 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001000 struct udf_part_map *map;
1001 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001004 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001006 for (i = 0; i < sbi->s_partitions; i++) {
1007 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001008 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001009 map->s_partition_num,
1010 le16_to_cpu(p->partitionNumber));
1011 if (map->s_partition_num ==
1012 le16_to_cpu(p->partitionNumber)) {
1013 map->s_partition_len =
1014 le32_to_cpu(p->partitionLength); /* blocks */
1015 map->s_partition_root =
1016 le32_to_cpu(p->partitionStartingLocation);
1017 if (le32_to_cpu(p->accessType) ==
1018 PD_ACCESS_TYPE_READ_ONLY)
1019 map->s_partition_flags |=
1020 UDF_PART_FLAG_READ_ONLY;
1021 if (le32_to_cpu(p->accessType) ==
1022 PD_ACCESS_TYPE_WRITE_ONCE)
1023 map->s_partition_flags |=
1024 UDF_PART_FLAG_WRITE_ONCE;
1025 if (le32_to_cpu(p->accessType) ==
1026 PD_ACCESS_TYPE_REWRITABLE)
1027 map->s_partition_flags |=
1028 UDF_PART_FLAG_REWRITABLE;
1029 if (le32_to_cpu(p->accessType) ==
1030 PD_ACCESS_TYPE_OVERWRITABLE)
1031 map->s_partition_flags |=
1032 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001034 if (!strcmp(p->partitionContents.ident,
1035 PD_PARTITION_CONTENTS_NSR02) ||
1036 !strcmp(p->partitionContents.ident,
1037 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 struct partitionHeaderDesc *phd;
1039
Marcin Slusarz4b111112008-02-08 04:20:36 -08001040 phd = (struct partitionHeaderDesc *)
1041 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001042 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001043 kernel_lb_addr loc = {
1044 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1045 .partitionReferenceNum = i,
1046 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001048 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001049 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001050 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001051 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001052 return 1;
1053 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001054 map->s_partition_flags |=
1055 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001056 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001057 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001059 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001060 struct udf_bitmap *bitmap =
1061 udf_sb_alloc_bitmap(sb, i);
1062 map->s_uspace.s_bitmap = bitmap;
1063 if (bitmap != NULL) {
1064 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001065 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001066 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001067 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001068 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001069 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001070 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072 }
1073 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001074 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001075 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001076 kernel_lb_addr loc = {
1077 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1078 .partitionReferenceNum = i,
1079 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001081 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001082 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001083 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001084 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001085 return 1;
1086 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001087 map->s_partition_flags |=
1088 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001089 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001090 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001092 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001093 struct udf_bitmap *bitmap =
1094 udf_sb_alloc_bitmap(sb, i);
1095 map->s_fspace.s_bitmap = bitmap;
1096 if (bitmap != NULL) {
1097 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001098 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001099 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001100 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001101 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001103 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 }
1106 }
1107 break;
1108 }
1109 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001110 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001111 udf_debug("Partition (%d) not found in partition map\n",
1112 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001113 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001114 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1115 "block length %d\n",
1116 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001117 map->s_partition_type,
1118 map->s_partition_root,
1119 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001123static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1124 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
1126 struct logicalVolDesc *lvd;
1127 int i, j, offset;
1128 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001129 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001130 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 lvd = (struct logicalVolDesc *)bh->b_data;
1133
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001134 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1135 if (i != 0)
1136 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001138 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001139 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001140 i++, offset += gpm->partitionMapLength) {
1141 struct udf_part_map *map = &sbi->s_partmaps[i];
1142 gpm = (struct genericPartitionMap *)
1143 &(lvd->partitionMaps[offset]);
1144 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001145 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001146 struct genericPartitionMap1 *gpm1 =
1147 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001148 map->s_partition_type = UDF_TYPE1_MAP15;
1149 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1150 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1151 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001152 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001153 struct udfPartitionMap2 *upm2 =
1154 (struct udfPartitionMap2 *)gpm;
1155 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1156 strlen(UDF_ID_VIRTUAL))) {
1157 u16 suf =
1158 le16_to_cpu(((__le16 *)upm2->partIdent.
1159 identSuffix)[0]);
1160 if (suf == 0x0150) {
1161 map->s_partition_type =
1162 UDF_VIRTUAL_MAP15;
1163 map->s_partition_func =
1164 udf_get_pblock_virt15;
1165 } else if (suf == 0x0200) {
1166 map->s_partition_type =
1167 UDF_VIRTUAL_MAP20;
1168 map->s_partition_func =
1169 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001171 } else if (!strncmp(upm2->partIdent.ident,
1172 UDF_ID_SPARABLE,
1173 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 uint32_t loc;
1175 uint16_t ident;
1176 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001177 struct sparablePartitionMap *spm =
1178 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001180 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001181 map->s_type_specific.s_sparing.s_packet_len =
1182 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001183 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001184 struct buffer_head *bh2;
1185
1186 loc = le32_to_cpu(
1187 spm->locSparingTable[j]);
1188 bh2 = udf_read_tagged(sb, loc, loc,
1189 &ident);
1190 map->s_type_specific.s_sparing.
1191 s_spar_map[j] = bh2;
1192
1193 if (bh2 != NULL) {
1194 st = (struct sparingTable *)
1195 bh2->b_data;
1196 if (ident != 0 || strncmp(
1197 st->sparingIdent.ident,
1198 UDF_ID_SPARING,
1199 strlen(UDF_ID_SPARING))) {
1200 brelse(bh2);
1201 map->s_type_specific.
1202 s_sparing.
1203 s_spar_map[j] =
1204 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
1207 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001208 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001209 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001210 udf_debug("Unknown ident: %s\n",
1211 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 continue;
1213 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001214 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1215 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001218 i, map->s_partition_num, type,
1219 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001222 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001223 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001226 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1227 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001228 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230 if (lvd->integritySeqExt.extLength)
1231 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234}
1235
1236/*
1237 * udf_load_logicalvolint
1238 *
1239 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001240static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct buffer_head *bh = NULL;
1243 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001244 struct udf_sb_info *sbi = UDF_SB(sb);
1245 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001248 (bh = udf_read_tagged(sb, loc.extLocation,
1249 loc.extLocation, &ident)) &&
1250 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001251 sbi->s_lvid_bh = bh;
1252 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001253
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001254 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001255 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001256 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001257
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001258 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001259 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001261 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001263 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001264 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
1267/*
1268 * udf_process_sequence
1269 *
1270 * PURPOSE
1271 * Process a main/reserve volume descriptor sequence.
1272 *
1273 * PRE-CONDITIONS
1274 * sb Pointer to _locked_ superblock.
1275 * block First block of first extent of the sequence.
1276 * lastblock Lastblock of first extent of the sequence.
1277 *
1278 * HISTORY
1279 * July 1, 1997 - Andrew E. Mileski
1280 * Written, tested, and released.
1281 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001282static int udf_process_sequence(struct super_block *sb, long block,
1283 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct buffer_head *bh = NULL;
1286 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001287 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct generic_desc *gd;
1289 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 int done = 0;
1291 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 uint32_t vdsn;
1293 uint16_t ident;
1294 long next_s = 0, next_e = 0;
1295
1296 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1297
1298 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001299 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001302 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304
1305 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1306 gd = (struct generic_desc *)bh->b_data;
1307 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001308 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001309 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001310 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1311 if (vdsn >= curr->volDescSeqNum) {
1312 curr->volDescSeqNum = vdsn;
1313 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 }
1315 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001316 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001317 curr = &vds[VDS_POS_VOL_DESC_PTR];
1318 if (vdsn >= curr->volDescSeqNum) {
1319 curr->volDescSeqNum = vdsn;
1320 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001322 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001323 next_s = le32_to_cpu(
1324 vdp->nextVolDescSeqExt.extLocation);
1325 next_e = le32_to_cpu(
1326 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001327 next_e = next_e >> sb->s_blocksize_bits;
1328 next_e += next_s;
1329 }
1330 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001331 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001332 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1333 if (vdsn >= curr->volDescSeqNum) {
1334 curr->volDescSeqNum = vdsn;
1335 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001336 }
1337 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001339 curr = &vds[VDS_POS_PARTITION_DESC];
1340 if (!curr->block)
1341 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001343 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001344 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1345 if (vdsn >= curr->volDescSeqNum) {
1346 curr->volDescSeqNum = vdsn;
1347 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001348 }
1349 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001350 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001351 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1352 if (vdsn >= curr->volDescSeqNum) {
1353 curr->volDescSeqNum = vdsn;
1354 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 }
1356 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001357 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001358 vds[VDS_POS_TERMINATING_DESC].block = block;
1359 if (next_e) {
1360 block = next_s;
1361 lastblock = next_e;
1362 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001363 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001364 done = 1;
1365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001367 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001369 for (i = 0; i < VDS_POS_LENGTH; i++) {
1370 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001371 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1372 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001374 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001376 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001377 if (udf_load_logicalvol(sb, bh, fileset)) {
1378 brelse(bh);
1379 return 1;
1380 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001381 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001383 if (udf_load_partdesc(sb, bh)) {
1384 brelse(bh);
1385 return 1;
1386 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001387 for (j = vds[i].block + 1;
1388 j < vds[VDS_POS_TERMINATING_DESC].block;
1389 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 bh2 = udf_read_tagged(sb, j, j, &ident);
1391 gd = (struct generic_desc *)bh2->b_data;
1392 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001393 if (udf_load_partdesc(sb,
1394 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001395 brelse(bh);
1396 brelse(bh2);
1397 return 1;
1398 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001399 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001402 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404 }
1405
1406 return 0;
1407}
1408
1409/*
1410 * udf_check_valid()
1411 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001412static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 long block;
1415
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001416 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 udf_debug("Validity check skipped because of novrs option\n");
1418 return 0;
1419 }
1420 /* Check that it is NSR02 compliant */
1421 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001422 else {
1423 block = udf_vrs(sb, silent);
1424 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001425 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001426 udf_debug("Failed to read byte 32768. Assuming open "
1427 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001428 if (!sbi->s_last_block)
1429 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001430 return 0;
1431 } else
1432 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001436static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct anchorVolDescPtr *anchor;
1439 uint16_t ident;
1440 struct buffer_head *bh;
1441 long main_s, main_e, reserve_s, reserve_e;
1442 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001443 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 if (!sb)
1446 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001447 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001449 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1450 if (sbi->s_anchor[i] &&
1451 (bh = udf_read_tagged(sb, sbi->s_anchor[i],
1452 sbi->s_anchor[i], &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 anchor = (struct anchorVolDescPtr *)bh->b_data;
1454
1455 /* Locate the main sequence */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001456 main_s = le32_to_cpu(
1457 anchor->mainVolDescSeqExt.extLocation);
1458 main_e = le32_to_cpu(
1459 anchor->mainVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 main_e = main_e >> sb->s_blocksize_bits;
1461 main_e += main_s;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Locate the reserve sequence */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001464 reserve_s = le32_to_cpu(
1465 anchor->reserveVolDescSeqExt.extLocation);
1466 reserve_e = le32_to_cpu(
1467 anchor->reserveVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 reserve_e = reserve_e >> sb->s_blocksize_bits;
1469 reserve_e += reserve_s;
1470
Jan Kara3bf25cb2007-05-08 00:35:16 -07001471 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* Process the main & reserve sequences */
1474 /* responsible for finding the PartitionDesc(s) */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001475 if (!(udf_process_sequence(sb, main_s, main_e,
1476 fileset) &&
1477 udf_process_sequence(sb, reserve_s, reserve_e,
1478 fileset)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 }
1482
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001483 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 udf_debug("No Anchor block found\n");
1485 return 1;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001486 } else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001487 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001489 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001490 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001491 struct udf_part_map *map = &sbi->s_partmaps[i];
1492 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001493 case UDF_VIRTUAL_MAP15:
1494 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001495 if (!sbi->s_last_block) {
1496 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001497 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001499
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001500 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001501 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001502 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001503 return 1;
1504 }
1505
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001506 for (j = 0; j < sbi->s_partitions; j++) {
1507 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001508 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001509 map->s_volumeseqnum ==
1510 map2->s_volumeseqnum &&
1511 map->s_partition_num ==
1512 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001513 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001514 ino.logicalBlockNum =
1515 sbi->s_last_block -
1516 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001517 break;
1518 }
1519 }
1520
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001521 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001522 return 1;
1523
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001524 sbi->s_vat_inode = udf_iget(sb, ino);
1525 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001526 return 1;
1527
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001528 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1529 map->s_type_specific.s_virtual.s_start_offset =
1530 udf_ext0_offset(sbi->s_vat_inode);
1531 map->s_type_specific.s_virtual.s_num_entries =
1532 (sbi->s_vat_inode->i_size - 36) >> 2;
1533 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001534 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001535 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001536
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001537 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 bh = sb_bread(sb, pos);
1539 if (!bh)
1540 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001541 vat20 = (struct virtualAllocationTable20 *)
1542 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001543 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001544 map->s_type_specific.s_virtual.s_start_offset =
1545 le16_to_cpu(vat20->lengthHeader) +
1546 udf_ext0_offset(sbi->s_vat_inode);
1547 map->s_type_specific.s_virtual.s_num_entries =
1548 (sbi->s_vat_inode->i_size -
1549 map->s_type_specific.s_virtual.
1550 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001551 brelse(bh);
1552 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1554 map->s_partition_len =
1555 sbi->s_partmaps[ino.partitionReferenceNum].
1556 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 }
1559 return 0;
1560}
1561
1562static void udf_open_lvid(struct super_block *sb)
1563{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001564 struct udf_sb_info *sbi = UDF_SB(sb);
1565 struct buffer_head *bh = sbi->s_lvid_bh;
1566 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 int i;
1568 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001569 struct logicalVolIntegrityDesc *lvid =
1570 (struct logicalVolIntegrityDesc *)bh->b_data;
1571 struct logicalVolIntegrityDescImpUse *lvidiu =
1572 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001574 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1575 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001577 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1578 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Marcin Slusarz4b111112008-02-08 04:20:36 -08001580 lvid->descTag.descCRC = cpu_to_le16(
1581 udf_crc((char *)lvid + sizeof(tag),
1582 le16_to_cpu(lvid->descTag.descCRCLength),
1583 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001585 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001586 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001588 lvid->descTag.tagChecksum +=
1589 ((uint8_t *) &(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001591 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593}
1594
1595static void udf_close_lvid(struct super_block *sb)
1596{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001597 kernel_timestamp cpu_time;
1598 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001599 struct udf_sb_info *sbi = UDF_SB(sb);
1600 struct buffer_head *bh = sbi->s_lvid_bh;
1601 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001602
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001603 if (!bh)
1604 return;
1605
1606 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1607
1608 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001609 struct logicalVolIntegrityDescImpUse *lvidiu =
1610 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001611 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1612 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001614 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1615 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001616 lvidiu->maxUDFWriteRev =
1617 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001618 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1619 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1620 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1621 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1622 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Marcin Slusarz4b111112008-02-08 04:20:36 -08001624 lvid->descTag.descCRC = cpu_to_le16(
1625 udf_crc((char *)lvid + sizeof(tag),
1626 le16_to_cpu(lvid->descTag.descCRCLength),
1627 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001629 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001630 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001632 lvid->descTag.tagChecksum +=
1633 ((uint8_t *)&(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001635 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637}
1638
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001639static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1640{
1641 int i;
1642 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001643 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1644 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001645
1646 for (i = 0; i < nr_groups; i++)
1647 if (bitmap->s_block_bitmap[i])
1648 brelse(bitmap->s_block_bitmap[i]);
1649
1650 if (size <= PAGE_SIZE)
1651 kfree(bitmap);
1652 else
1653 vfree(bitmap);
1654}
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656/*
1657 * udf_read_super
1658 *
1659 * PURPOSE
1660 * Complete the specified super block.
1661 *
1662 * PRE-CONDITIONS
1663 * sb Pointer to superblock to complete - never NULL.
1664 * sb->s_dev Device to read suberblock from.
1665 * options Pointer to mount options.
1666 * silent Silent flag.
1667 *
1668 * HISTORY
1669 * July 1, 1997 - Andrew E. Mileski
1670 * Written, tested, and released.
1671 */
1672static int udf_fill_super(struct super_block *sb, void *options, int silent)
1673{
1674 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct udf_options uopt;
1677 kernel_lb_addr rootdir, fileset;
1678 struct udf_sb_info *sbi;
1679
1680 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1681 uopt.uid = -1;
1682 uopt.gid = -1;
1683 uopt.umask = 0;
1684
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001685 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (!sbi)
1687 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001691 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 if (!udf_parse_options((char *)options, &uopt))
1694 goto error_out;
1695
1696 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001697 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001699 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 goto error_out;
1701 }
1702#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 uopt.nls_map = load_nls_default();
1705 if (!uopt.nls_map)
1706 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1707 else
1708 udf_debug("Using default NLS map\n");
1709 }
1710#endif
1711 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1712 uopt.flags |= (1 << UDF_FLAG_UTF8);
1713
1714 fileset.logicalBlockNum = 0xFFFFFFFF;
1715 fileset.partitionReferenceNum = 0xFFFF;
1716
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001717 sbi->s_flags = uopt.flags;
1718 sbi->s_uid = uopt.uid;
1719 sbi->s_gid = uopt.gid;
1720 sbi->s_umask = uopt.umask;
1721 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 /* Set the block size for all transfers */
1724 if (!udf_set_blocksize(sb, uopt.blocksize))
1725 goto error_out;
1726
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001727 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001728 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001730 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001732 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001734 sbi->s_last_block = uopt.lastblock;
1735 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1736 sbi->s_anchor[2] = uopt.anchor;
1737 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001739 if (udf_check_valid(sb, uopt.novrs, silent)) {
1740 /* read volume recognition sequences */
1741 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001742 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744
1745 udf_find_anchor(sb);
1746
1747 /* Fill in the rest of the superblock */
1748 sb->s_op = &udf_sb_ops;
1749 sb->dq_op = NULL;
1750 sb->s_dirt = 0;
1751 sb->s_magic = UDF_SUPER_MAGIC;
1752 sb->s_time_gran = 1000;
1753
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001754 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001755 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 goto error_out;
1757 }
1758
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001759 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001761 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001762 struct logicalVolIntegrityDescImpUse *lvidiu =
1763 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001764 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1765 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001766 /* uint16_t maxUDFWriteRev =
1767 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001769 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001770 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1771 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001772 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001773 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001775 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001778 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1781 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1782 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1783 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1784 }
1785
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001786 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001787 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto error_out;
1789 }
1790
Marcin Slusarz4b111112008-02-08 04:20:36 -08001791 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1792 UDF_PART_FLAG_READ_ONLY) {
1793 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1794 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001795 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001796 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001797
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001798 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001799 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto error_out;
1801 }
1802
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001803 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001805 udf_time_to_stamp(&ts, sbi->s_record_time);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001806 udf_info("UDF %s (%s) Mounting volume '%s', "
1807 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1808 UDFFS_VERSION, UDFFS_DATE,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001809 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1810 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812 if (!(sb->s_flags & MS_RDONLY))
1813 udf_open_lvid(sb);
1814
1815 /* Assign the root inode */
1816 /* assign inodes by physical block number */
1817 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001818 inode = udf_iget(sb, rootdir);
1819 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001820 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1821 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001822 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto error_out;
1824 }
1825
1826 /* Allocate a dentry for the root inode */
1827 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001828 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001829 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 iput(inode);
1831 goto error_out;
1832 }
Jan Kara31170b62007-05-08 00:35:21 -07001833 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001836error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001837 if (sbi->s_vat_inode)
1838 iput(sbi->s_vat_inode);
1839 if (sbi->s_partitions) {
1840 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1841 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1842 iput(map->s_uspace.s_table);
1843 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1844 iput(map->s_fspace.s_table);
1845 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001846 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001847 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001848 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001849 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001850 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001851 brelse(map->s_type_specific.s_sparing.
1852 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
1854#ifdef CONFIG_UDF_NLS
1855 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001856 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#endif
1858 if (!(sb->s_flags & MS_RDONLY))
1859 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001860 brelse(sbi->s_lvid_bh);
1861
1862 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 kfree(sbi);
1864 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return -EINVAL;
1867}
1868
1869void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001870 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 va_list args;
1873
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001874 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* mark sb error */
1876 sb->s_dirt = 1;
1877 }
1878 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001879 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001881 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001882 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001886 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 va_list args;
1889
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001890 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001891 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 va_end(args);
1893 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001894 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
1897/*
1898 * udf_put_super
1899 *
1900 * PURPOSE
1901 * Prepare for destruction of the superblock.
1902 *
1903 * DESCRIPTION
1904 * Called before the filesystem is unmounted.
1905 *
1906 * HISTORY
1907 * July 1, 1997 - Andrew E. Mileski
1908 * Written, tested, and released.
1909 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001910static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
1912 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001913 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001915 sbi = UDF_SB(sb);
1916 if (sbi->s_vat_inode)
1917 iput(sbi->s_vat_inode);
1918 if (sbi->s_partitions) {
1919 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1920 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1921 iput(map->s_uspace.s_table);
1922 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1923 iput(map->s_fspace.s_table);
1924 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001925 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001926 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001927 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001928 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001929 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001930 brelse(map->s_type_specific.s_sparing.
1931 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933#ifdef CONFIG_UDF_NLS
1934 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001935 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936#endif
1937 if (!(sb->s_flags & MS_RDONLY))
1938 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001939 brelse(sbi->s_lvid_bh);
1940 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 kfree(sb->s_fs_info);
1942 sb->s_fs_info = NULL;
1943}
1944
1945/*
1946 * udf_stat_fs
1947 *
1948 * PURPOSE
1949 * Return info about the filesystem.
1950 *
1951 * DESCRIPTION
1952 * Called by sys_statfs()
1953 *
1954 * HISTORY
1955 * July 1, 1997 - Andrew E. Mileski
1956 * Written, tested, and released.
1957 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001958static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
David Howells726c3342006-06-23 02:02:58 -07001960 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001961 struct udf_sb_info *sbi = UDF_SB(sb);
1962 struct logicalVolIntegrityDescImpUse *lvidiu;
1963
1964 if (sbi->s_lvid_bh != NULL)
1965 lvidiu = udf_sb_lvidiu(sbi);
1966 else
1967 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 buf->f_type = UDF_SUPER_MAGIC;
1970 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001971 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 buf->f_bfree = udf_count_free(sb);
1973 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001974 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1975 le32_to_cpu(lvidiu->numDirs)) : 0)
1976 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 buf->f_ffree = buf->f_bfree;
1978 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001979 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 return 0;
1982}
1983
1984static unsigned char udf_bitmap_lookup[16] = {
1985 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1986};
1987
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988static unsigned int udf_count_free_bitmap(struct super_block *sb,
1989 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 struct buffer_head *bh = NULL;
1992 unsigned int accum = 0;
1993 int index;
1994 int block = 0, newblock;
1995 kernel_lb_addr loc;
1996 uint32_t bytes;
1997 uint8_t value;
1998 uint8_t *ptr;
1999 uint16_t ident;
2000 struct spaceBitmapDesc *bm;
2001
2002 lock_kernel();
2003
2004 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002005 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 bh = udf_read_ptagged(sb, loc, 0, &ident);
2007
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002008 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 printk(KERN_ERR "udf: udf_count_free failed\n");
2010 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002011 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002012 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 printk(KERN_ERR "udf: udf_count_free failed\n");
2014 goto out;
2015 }
2016
2017 bm = (struct spaceBitmapDesc *)bh->b_data;
2018 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002019 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2020 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002022 while (bytes > 0) {
2023 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002025 accum += udf_bitmap_lookup[value & 0x0f];
2026 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 index++;
2028 bytes--;
2029 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002031 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 newblock = udf_get_lb_pblock(sb, loc, ++block);
2033 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002034 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 udf_debug("read failed\n");
2036 goto out;
2037 }
2038 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002039 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002042 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002044out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 unlock_kernel();
2046
2047 return accum;
2048}
2049
Marcin Slusarz4b111112008-02-08 04:20:36 -08002050static unsigned int udf_count_free_table(struct super_block *sb,
2051 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002054 uint32_t elen;
2055 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002057 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 lock_kernel();
2060
Jan Karaff116fc2007-05-08 00:35:14 -07002061 epos.block = UDF_I_LOCATION(table);
2062 epos.offset = sizeof(struct unallocSpaceEntry);
2063 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002065 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002067
Jan Kara3bf25cb2007-05-08 00:35:16 -07002068 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 unlock_kernel();
2071
2072 return accum;
2073}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002074
2075static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002078 struct udf_sb_info *sbi;
2079 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002081 sbi = UDF_SB(sb);
2082 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002083 struct logicalVolIntegrityDesc *lvid =
2084 (struct logicalVolIntegrityDesc *)
2085 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002086 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002087 accum = le32_to_cpu(
2088 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (accum == 0xFFFFFFFF)
2090 accum = 0;
2091 }
2092 }
2093
2094 if (accum)
2095 return accum;
2096
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002097 map = &sbi->s_partmaps[sbi->s_partition];
2098 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002099 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002100 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002102 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002103 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002104 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106 if (accum)
2107 return accum;
2108
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002109 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002110 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002111 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002113 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002114 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002115 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
2117
2118 return accum;
2119}