blob: 3cd5a855033b3eba3fef3c27ec686b4f5157fa1e [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{
101 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
102 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
103 __u32 offset = number_of_partitions * 2 * sizeof(uint32_t)/sizeof(uint8_t);
104 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
105}
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700108static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109 int flags, const char *dev_name, void *data,
110 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David Howells454e2392006-06-23 02:02:57 -0700112 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700116 .owner = THIS_MODULE,
117 .name = "udf",
118 .get_sb = udf_get_sb,
119 .kill_sb = kill_block_super,
120 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700123static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125static struct inode *udf_alloc_inode(struct super_block *sb)
126{
127 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800128 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!ei)
130 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700131
132 ei->i_unique = 0;
133 ei->i_lenExtents = 0;
134 ei->i_next_alloc_block = 0;
135 ei->i_next_alloc_goal = 0;
136 ei->i_strat4096 = 0;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return &ei->vfs_inode;
139}
140
141static void udf_destroy_inode(struct inode *inode)
142{
143 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
144}
145
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700146static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Christoph Lametera35afb82007-05-16 22:10:57 -0700150 ei->i_ext.i_data = NULL;
151 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154static int init_inodecache(void)
155{
156 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
157 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700158 0, (SLAB_RECLAIM_ACCOUNT |
159 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900160 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700161 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -ENOMEM;
163 return 0;
164}
165
166static void destroy_inodecache(void)
167{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700168 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800172static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700173 .alloc_inode = udf_alloc_inode,
174 .destroy_inode = udf_destroy_inode,
175 .write_inode = udf_write_inode,
176 .delete_inode = udf_delete_inode,
177 .clear_inode = udf_clear_inode,
178 .put_super = udf_put_super,
179 .write_super = udf_write_super,
180 .statfs = udf_statfs,
181 .remount_fs = udf_remount_fs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182};
183
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned char novrs;
186 unsigned int blocksize;
187 unsigned int session;
188 unsigned int lastblock;
189 unsigned int anchor;
190 unsigned int volume;
191 unsigned short partition;
192 unsigned int fileset;
193 unsigned int rootdir;
194 unsigned int flags;
195 mode_t umask;
196 gid_t gid;
197 uid_t uid;
198 struct nls_table *nls_map;
199};
200
201static int __init init_udf_fs(void)
202{
203 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 err = init_inodecache();
206 if (err)
207 goto out1;
208 err = register_filesystem(&udf_fstype);
209 if (err)
210 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700213
214out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216
217out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return err;
219}
220
221static void __exit exit_udf_fs(void)
222{
223 unregister_filesystem(&udf_fstype);
224 destroy_inodecache();
225}
226
227module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700228module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800230static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
231{
232 struct udf_sb_info *sbi = UDF_SB(sb);
233
234 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
235 GFP_KERNEL);
236 if (!sbi->s_partmaps) {
237 udf_error(sb, __FUNCTION__,
238 "Unable to allocate space for %d partition maps",
239 count);
240 sbi->s_partitions = 0;
241 return -ENOMEM;
242 }
243
244 sbi->s_partitions = count;
245 return 0;
246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * udf_parse_options
250 *
251 * PURPOSE
252 * Parse mount options.
253 *
254 * DESCRIPTION
255 * The following mount options are supported:
256 *
257 * gid= Set the default group.
258 * umask= Set the default umask.
259 * uid= Set the default user.
260 * bs= Set the block size.
261 * unhide Show otherwise hidden files.
262 * undelete Show deleted files in lists.
263 * adinicb Embed data in the inode (default)
264 * noadinicb Don't embed data in the inode
265 * shortad Use short ad's
266 * longad Use long ad's (default)
267 * nostrict Unset strict conformance
268 * iocharset= Set the NLS character set
269 *
270 * The remaining are for debugging and disaster recovery:
271 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700272 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 *
274 * The following expect a offset from 0.
275 *
276 * session= Set the CDROM session (default= last session)
277 * anchor= Override standard anchor location. (default= 256)
278 * volume= Override the VolumeDesc location. (unused)
279 * partition= Override the PartitionDesc location. (unused)
280 * lastblock= Set the last block of the filesystem/
281 *
282 * The following expect a offset from the partition root.
283 *
284 * fileset= Override the fileset block location. (unused)
285 * rootdir= Override the root directory location. (unused)
286 * WARNING: overriding the rootdir to a non-directory may
287 * yield highly unpredictable results.
288 *
289 * PRE-CONDITIONS
290 * options Pointer to mount options string.
291 * uopts Pointer to mount options variable.
292 *
293 * POST-CONDITIONS
294 * <return> 1 Mount options parsed okay.
295 * <return> 0 Error parsing mount options.
296 *
297 * HISTORY
298 * July 1, 1997 - Andrew E. Mileski
299 * Written, tested, and released.
300 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302enum {
303 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
304 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
305 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
306 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
307 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800308 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700312 {Opt_novrs, "novrs"},
313 {Opt_nostrict, "nostrict"},
314 {Opt_bs, "bs=%u"},
315 {Opt_unhide, "unhide"},
316 {Opt_undelete, "undelete"},
317 {Opt_noadinicb, "noadinicb"},
318 {Opt_adinicb, "adinicb"},
319 {Opt_shortad, "shortad"},
320 {Opt_longad, "longad"},
321 {Opt_uforget, "uid=forget"},
322 {Opt_uignore, "uid=ignore"},
323 {Opt_gforget, "gid=forget"},
324 {Opt_gignore, "gid=ignore"},
325 {Opt_gid, "gid=%u"},
326 {Opt_uid, "uid=%u"},
327 {Opt_umask, "umask=%o"},
328 {Opt_session, "session=%u"},
329 {Opt_lastblock, "lastblock=%u"},
330 {Opt_anchor, "anchor=%u"},
331 {Opt_volume, "volume=%u"},
332 {Opt_partition, "partition=%u"},
333 {Opt_fileset, "fileset=%u"},
334 {Opt_rootdir, "rootdir=%u"},
335 {Opt_utf8, "utf8"},
336 {Opt_iocharset, "iocharset=%s"},
337 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338};
339
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700340static int udf_parse_options(char *options, struct udf_options *uopt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 char *p;
343 int option;
344
345 uopt->novrs = 0;
346 uopt->blocksize = 2048;
347 uopt->partition = 0xFFFF;
348 uopt->session = 0xFFFFFFFF;
349 uopt->lastblock = 0;
350 uopt->anchor = 0;
351 uopt->volume = 0xFFFFFFFF;
352 uopt->rootdir = 0xFFFFFFFF;
353 uopt->fileset = 0xFFFFFFFF;
354 uopt->nls_map = NULL;
355
356 if (!options)
357 return 1;
358
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700359 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 substring_t args[MAX_OPT_ARGS];
361 int token;
362 if (!*p)
363 continue;
364
365 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700366 switch (token) {
367 case Opt_novrs:
368 uopt->novrs = 1;
369 case Opt_bs:
370 if (match_int(&args[0], &option))
371 return 0;
372 uopt->blocksize = option;
373 break;
374 case Opt_unhide:
375 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
376 break;
377 case Opt_undelete:
378 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
379 break;
380 case Opt_noadinicb:
381 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
382 break;
383 case Opt_adinicb:
384 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
385 break;
386 case Opt_shortad:
387 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
388 break;
389 case Opt_longad:
390 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
391 break;
392 case Opt_gid:
393 if (match_int(args, &option))
394 return 0;
395 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700396 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700397 break;
398 case Opt_uid:
399 if (match_int(args, &option))
400 return 0;
401 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700402 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700403 break;
404 case Opt_umask:
405 if (match_octal(args, &option))
406 return 0;
407 uopt->umask = option;
408 break;
409 case Opt_nostrict:
410 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
411 break;
412 case Opt_session:
413 if (match_int(args, &option))
414 return 0;
415 uopt->session = option;
416 break;
417 case Opt_lastblock:
418 if (match_int(args, &option))
419 return 0;
420 uopt->lastblock = option;
421 break;
422 case Opt_anchor:
423 if (match_int(args, &option))
424 return 0;
425 uopt->anchor = option;
426 break;
427 case Opt_volume:
428 if (match_int(args, &option))
429 return 0;
430 uopt->volume = option;
431 break;
432 case Opt_partition:
433 if (match_int(args, &option))
434 return 0;
435 uopt->partition = option;
436 break;
437 case Opt_fileset:
438 if (match_int(args, &option))
439 return 0;
440 uopt->fileset = option;
441 break;
442 case Opt_rootdir:
443 if (match_int(args, &option))
444 return 0;
445 uopt->rootdir = option;
446 break;
447 case Opt_utf8:
448 uopt->flags |= (1 << UDF_FLAG_UTF8);
449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700451 case Opt_iocharset:
452 uopt->nls_map = load_nls(args[0].from);
453 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
454 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700456 case Opt_uignore:
457 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
458 break;
459 case Opt_uforget:
460 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
461 break;
462 case Opt_gignore:
463 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
464 break;
465 case Opt_gforget:
466 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
467 break;
468 default:
469 printk(KERN_ERR "udf: bad mount option \"%s\" "
470 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472 }
473 }
474 return 1;
475}
476
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800477static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!(sb->s_flags & MS_RDONLY))
482 udf_open_lvid(sb);
483 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unlock_kernel();
486}
487
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700488static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800491 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800493 uopt.flags = sbi->s_flags;
494 uopt.uid = sbi->s_uid;
495 uopt.gid = sbi->s_gid;
496 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700498 if (!udf_parse_options(options, &uopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -EINVAL;
500
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800501 sbi->s_flags = uopt.flags;
502 sbi->s_uid = uopt.uid;
503 sbi->s_gid = uopt.gid;
504 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800506 if (sbi->s_lvid_bh) {
507 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (write_rev > UDF_MAX_WRITE_VERSION)
509 *flags |= MS_RDONLY;
510 }
511
512 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
513 return 0;
514 if (*flags & MS_RDONLY)
515 udf_close_lvid(sb);
516 else
517 udf_open_lvid(sb);
518
519 return 0;
520}
521
522/*
523 * udf_set_blocksize
524 *
525 * PURPOSE
526 * Set the block size to be used in all transfers.
527 *
528 * DESCRIPTION
529 * To allow room for a DMA transfer, it is best to guess big when unsure.
530 * This routine picks 2048 bytes as the blocksize when guessing. This
531 * should be adequate until devices with larger block sizes become common.
532 *
533 * Note that the Linux kernel can currently only deal with blocksizes of
534 * 512, 1024, 2048, 4096, and 8192 bytes.
535 *
536 * PRE-CONDITIONS
537 * sb Pointer to _locked_ superblock.
538 *
539 * POST-CONDITIONS
540 * sb->s_blocksize Blocksize.
541 * sb->s_blocksize_bits log2 of blocksize.
542 * <return> 0 Blocksize is valid.
543 * <return> 1 Blocksize is invalid.
544 *
545 * HISTORY
546 * July 1, 1997 - Andrew E. Mileski
547 * Written, tested, and released.
548 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700549static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 if (!sb_min_blocksize(sb, bsize)) {
552 udf_debug("Bad block size (%d)\n", bsize);
553 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
554 return 0;
555 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return sb->s_blocksize;
558}
559
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700560static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct volStructDesc *vsd = NULL;
563 int sector = 32768;
564 int sectorsize;
565 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 int iso9660 = 0;
567 int nsr02 = 0;
568 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800569 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* Block size must be a multiple of 512 */
572 if (sb->s_blocksize & 511)
573 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800574 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (sb->s_blocksize < sizeof(struct volStructDesc))
577 sectorsize = sizeof(struct volStructDesc);
578 else
579 sectorsize = sb->s_blocksize;
580
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800581 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700584 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700586 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Read a block */
588 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
589 if (!bh)
590 break;
591
592 /* Look for ISO descriptors */
593 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800594 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700596 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700597 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800599 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
600 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700602 switch (vsd->structType) {
603 case 0:
604 udf_debug("ISO9660 Boot Record found\n");
605 break;
606 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800607 udf_debug("ISO9660 Primary Volume Descriptor "
608 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700609 break;
610 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800611 udf_debug("ISO9660 Supplementary Volume "
612 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 break;
614 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800615 udf_debug("ISO9660 Volume Partition Descriptor "
616 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617 break;
618 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800619 udf_debug("ISO9660 Volume Descriptor Set "
620 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700621 break;
622 default:
623 udf_debug("ISO9660 VRS (%u) found\n",
624 vsd->structType);
625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800627 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
628 VSD_STD_ID_LEN))
629 ; /* nothing */
630 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
631 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700632 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800634 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
635 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800637 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
638 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700640 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 if (nsr03)
644 return nsr03;
645 else if (nsr02)
646 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800647 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -1;
649 else
650 return 0;
651}
652
653/*
654 * udf_find_anchor
655 *
656 * PURPOSE
657 * Find an anchor volume descriptor.
658 *
659 * PRE-CONDITIONS
660 * sb Pointer to _locked_ superblock.
661 * lastblock Last block on media.
662 *
663 * POST-CONDITIONS
664 * <return> 1 if not found, 0 if ok
665 *
666 * HISTORY
667 * July 1, 1997 - Andrew E. Mileski
668 * Written, tested, and released.
669 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700670static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800672 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct buffer_head *bh = NULL;
674 uint16_t ident;
675 uint32_t location;
676 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800677 struct udf_sb_info *sbi;
678
679 sbi = UDF_SB(sb);
680 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700682 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700684 int last[] = { lastblock, lastblock - 2,
685 lastblock - 150, lastblock - 152,
686 varlastblock, varlastblock - 2,
687 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 lastblock = 0;
690
691 /* Search for an anchor volume descriptor pointer */
692
693 /* according to spec, anchor is in either:
694 * block 256
695 * lastblock-256
696 * lastblock
697 * however, if the disc isn't closed, it could be 512 */
698
Tobias Klausere8c96f82006-03-24 03:15:34 -0800699 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800700 ident = location = 0;
701 if (last[i] >= 0) {
702 bh = sb_bread(sb, last[i]);
703 if (bh) {
704 tag *t = (tag *)bh->b_data;
705 ident = le16_to_cpu(t->tagIdent);
706 location = le32_to_cpu(t->tagLocation);
707 brelse(bh);
708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800710
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700711 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800712 if (location == last[i] - sbi->s_session) {
713 lastblock = last[i] - sbi->s_session;
714 sbi->s_anchor[0] = lastblock;
715 sbi->s_anchor[1] = lastblock - 256;
716 } else if (location == udf_variable_to_fixed(last[i]) - sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800718 lastblock = udf_variable_to_fixed(last[i]) - sbi->s_session;
719 sbi->s_anchor[0] = lastblock;
720 sbi->s_anchor[1] = lastblock - 256 - sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700721 } else {
722 udf_debug("Anchor found at block %d, location mismatch %d.\n",
723 last[i], location);
724 }
725 } else if (ident == TAG_IDENT_FE || ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800727 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800729 ident = location = 0;
730 if (last[i] >= 256) {
731 bh = sb_bread(sb, last[i] - 256);
732 if (bh) {
733 tag *t = (tag *)bh->b_data;
734 ident = le16_to_cpu(t->tagIdent);
735 location = le32_to_cpu(t->tagLocation);
736 brelse(bh);
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800741 location == last[i] - 256 - sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800743 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700744 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800745 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800746 if (last[i] >= 312 + sbi->s_session) {
747 bh = sb_bread(sb, last[i] - 312 - sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800748 if (bh) {
749 tag *t = (tag *)bh->b_data;
750 ident = le16_to_cpu(t->tagIdent);
751 location = le32_to_cpu(t->tagLocation);
752 brelse(bh);
753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700757 location == udf_variable_to_fixed(last[i]) - 256) {
758 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
759 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800760 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762 }
763 }
764 }
765 }
766
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700767 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800768 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800769 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800770 if (bh) {
771 tag *t = (tag *)bh->b_data;
772 ident = le16_to_cpu(t->tagIdent);
773 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700774 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (ident == TAG_IDENT_AVDP && location == 256)
777 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
778 }
779 }
780
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800781 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
782 if (sbi->s_anchor[i]) {
783 bh = udf_read_tagged(sb, sbi->s_anchor[i],
784 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800785 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800786 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800787 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700788 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700789 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800790 (i || (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800791 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 }
794 }
795
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800796 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800799static int udf_find_fileset(struct super_block *sb,
800 kernel_lb_addr *fileset,
801 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct buffer_head *bh = NULL;
804 long lastblock;
805 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800806 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700809 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
811
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700812 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700814 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700815 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return 1;
817 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800821 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800822 if (!bh) {
823 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 kernel_lb_addr newfileset;
825
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700826/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700828
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800829 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700830 (newfileset.partitionReferenceNum != 0xFFFF &&
831 fileset->logicalBlockNum == 0xFFFFFFFF &&
832 fileset->partitionReferenceNum == 0xFFFF);
833 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 lastblock = sbi->s_partmaps
835 [newfileset.partitionReferenceNum]
836 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 newfileset.logicalBlockNum = 0;
838
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700839 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800840 bh = udf_read_ptagged(sb, newfileset, 0,
841 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842 if (!bh) {
843 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 continue;
845 }
846
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700847 switch (ident) {
848 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700849 {
850 struct spaceBitmapDesc *sp;
851 sp = (struct spaceBitmapDesc *)bh->b_data;
852 newfileset.logicalBlockNum += 1 +
853 ((le32_to_cpu(sp->numOfBytes) +
854 sizeof(struct spaceBitmapDesc) - 1)
855 >> sb->s_blocksize_bits);
856 brelse(bh);
857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700859 case TAG_IDENT_FSD:
860 *fileset = newfileset;
861 break;
862 default:
863 newfileset.logicalBlockNum++;
864 brelse(bh);
865 bh = NULL;
866 break;
867 }
868 } while (newfileset.logicalBlockNum < lastblock &&
869 fileset->logicalBlockNum == 0xFFFFFFFF &&
870 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 }
873
874 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700877 fileset->logicalBlockNum,
878 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800880 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700882 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return 0;
884 }
885 return 1;
886}
887
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700888static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct primaryVolDesc *pvoldesc;
891 time_t recording;
892 long recording_usec;
893 struct ustr instr;
894 struct ustr outstr;
895
896 pvoldesc = (struct primaryVolDesc *)bh->b_data;
897
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700898 if (udf_stamp_to_time(&recording, &recording_usec,
899 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 kernel_timestamp ts;
901 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800902 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
903 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700904 recording, recording_usec,
905 ts.year, ts.month, ts.day, ts.hour,
906 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800907 UDF_SB(sb)->s_record_time.tv_sec = recording;
908 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700911 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32)) {
912 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800913 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800915 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 }
918
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700919 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (udf_CS0toUTF8(&outstr, &instr))
921 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
922 }
923}
924
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700925static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
926 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct fileSetDesc *fset;
929
930 fset = (struct fileSetDesc *)bh->b_data;
931
932 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
933
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800934 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700936 udf_debug("Rootdir at block=%d, partition=%d\n",
937 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800940int udf_compute_nr_groups(struct super_block *sb, u32 partition)
941{
942 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
943 return (map->s_partition_len +
944 (sizeof(struct spaceBitmapDesc) << 3) +
945 (sb->s_blocksize * 8) - 1) /
946 (sb->s_blocksize * 8);
947}
948
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800949static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
950{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800951 struct udf_bitmap *bitmap;
952 int nr_groups;
953 int size;
954
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800955 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800956 size = sizeof(struct udf_bitmap) +
957 (sizeof(struct buffer_head *) * nr_groups);
958
959 if (size <= PAGE_SIZE)
960 bitmap = kmalloc(size, GFP_KERNEL);
961 else
962 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
963
964 if (bitmap == NULL) {
965 udf_error(sb, __FUNCTION__,
966 "Unable to allocate space for bitmap "
967 "and %d buffer_head pointers", nr_groups);
968 return NULL;
969 }
970
971 memset(bitmap, 0x00, size);
972 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
973 bitmap->s_nr_groups = nr_groups;
974 return bitmap;
975}
976
Jan Karabcec4472007-08-30 23:56:22 -0700977static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct partitionDesc *p;
980 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800981 struct udf_part_map *map;
982 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800985 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800987 for (i = 0; i < sbi->s_partitions; i++) {
988 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700989 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800990 map->s_partition_num, le16_to_cpu(p->partitionNumber));
991 if (map->s_partition_num == le16_to_cpu(p->partitionNumber)) {
992 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
993 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700994 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_READ_ONLY)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800995 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700996 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_WRITE_ONCE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800997 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700998 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_REWRITABLE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800999 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001000 if (le32_to_cpu(p->accessType) == PD_ACCESS_TYPE_OVERWRITABLE)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001001 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001003 if (!strcmp(p->partitionContents.ident,
1004 PD_PARTITION_CONTENTS_NSR02) ||
1005 !strcmp(p->partitionContents.ident,
1006 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct partitionHeaderDesc *phd;
1008
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001009 phd = (struct partitionHeaderDesc *)(p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001010 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001011 kernel_lb_addr loc = {
1012 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1013 .partitionReferenceNum = i,
1014 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001016 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001017 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001018 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001019 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001020 return 1;
1021 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001022 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001023 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001024 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001026 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001027 map->s_uspace.s_bitmap = udf_sb_alloc_bitmap(sb, i);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001028 if (map->s_uspace.s_bitmap != NULL) {
1029 map->s_uspace.s_bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001030 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001031 map->s_uspace.s_bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001032 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001033 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001034 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001035 i, map->s_uspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 }
1038 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001039 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001040 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001041 kernel_lb_addr loc = {
1042 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1043 .partitionReferenceNum = i,
1044 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001046 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001047 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001048 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001049 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001050 return 1;
1051 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001052 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001053 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001054 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001056 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001057 map->s_fspace.s_bitmap = udf_sb_alloc_bitmap(sb, i);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001058 if (map->s_fspace.s_bitmap != NULL) {
1059 map->s_fspace.s_bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001060 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001061 map->s_fspace.s_bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001062 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001063 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001064 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001065 i, map->s_fspace.s_bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067 }
1068 }
1069 break;
1070 }
1071 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001072 if (i == sbi->s_partitions) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001073 udf_debug("Partition (%d) not found in partition map\n",
1074 le16_to_cpu(p->partitionNumber));
1075 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001076 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1077 "block length %d\n",
1078 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001079 map->s_partition_type,
1080 map->s_partition_root,
1081 map->s_partition_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
Jan Karabcec4472007-08-30 23:56:22 -07001083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001086static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1087 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 struct logicalVolDesc *lvd;
1090 int i, j, offset;
1091 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001092 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 lvd = (struct logicalVolDesc *)bh->b_data;
1095
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001096 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1097 if (i != 0)
1098 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001100 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001101 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102 i++, offset += ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapLength) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001103 struct udf_part_map *map = &sbi->s_partmaps[i];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001104 type = ((struct genericPartitionMap *)&(lvd->partitionMaps[offset]))->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001105 if (type == 1) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001106 struct genericPartitionMap1 *gpm1 = (struct genericPartitionMap1 *)&(lvd->partitionMaps[offset]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001107 map->s_partition_type = UDF_TYPE1_MAP15;
1108 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1109 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1110 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001111 } else if (type == 2) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001112 struct udfPartitionMap2 *upm2 = (struct udfPartitionMap2 *)&(lvd->partitionMaps[offset]);
1113 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, strlen(UDF_ID_VIRTUAL))) {
1114 if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0150) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001115 map->s_partition_type = UDF_VIRTUAL_MAP15;
1116 map->s_partition_func = udf_get_pblock_virt15;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001117 } else if (le16_to_cpu(((__le16 *)upm2->partIdent.identSuffix)[0]) == 0x0200) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001118 map->s_partition_type = UDF_VIRTUAL_MAP20;
1119 map->s_partition_func = udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001121 } else if (!strncmp(upm2->partIdent.ident, UDF_ID_SPARABLE, strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 uint32_t loc;
1123 uint16_t ident;
1124 struct sparingTable *st;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001125 struct sparablePartitionMap *spm = (struct sparablePartitionMap *)&(lvd->partitionMaps[offset]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001127 map->s_partition_type = UDF_SPARABLE_MAP15;
1128 map->s_type_specific.s_sparing.s_packet_len = le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001129 for (j = 0; j < spm->numSparingTables; j++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130 loc = le32_to_cpu(spm->locSparingTable[j]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001131 map->s_type_specific.s_sparing.s_spar_map[j] =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001132 udf_read_tagged(sb, loc, loc, &ident);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001133 if (map->s_type_specific.s_sparing.s_spar_map[j] != NULL) {
1134 st = (struct sparingTable *)map->s_type_specific.s_sparing.s_spar_map[j]->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001135 if (ident != 0 ||
1136 strncmp(st->sparingIdent.ident, UDF_ID_SPARING, strlen(UDF_ID_SPARING))) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001137 brelse(map->s_type_specific.s_sparing.s_spar_map[j]);
1138 map->s_type_specific.s_sparing.s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 }
1141 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001142 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001143 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001144 udf_debug("Unknown ident: %s\n",
1145 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 continue;
1147 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001148 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1149 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001152 i, map->s_partition_num, type,
1153 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001156 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001157 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001160 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1161 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001162 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 if (lvd->integritySeqExt.extLength)
1165 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
1168}
1169
1170/*
1171 * udf_load_logicalvolint
1172 *
1173 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001174static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
1176 struct buffer_head *bh = NULL;
1177 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001178 struct udf_sb_info *sbi = UDF_SB(sb);
1179 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001182 (bh = udf_read_tagged(sb, loc.extLocation,
1183 loc.extLocation, &ident)) &&
1184 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001185 sbi->s_lvid_bh = bh;
1186 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001187
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001188 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001189 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001190 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001192 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001193 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001195 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001197 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001198 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201/*
1202 * udf_process_sequence
1203 *
1204 * PURPOSE
1205 * Process a main/reserve volume descriptor sequence.
1206 *
1207 * PRE-CONDITIONS
1208 * sb Pointer to _locked_ superblock.
1209 * block First block of first extent of the sequence.
1210 * lastblock Lastblock of first extent of the sequence.
1211 *
1212 * HISTORY
1213 * July 1, 1997 - Andrew E. Mileski
1214 * Written, tested, and released.
1215 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001216static int udf_process_sequence(struct super_block *sb, long block,
1217 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct buffer_head *bh = NULL;
1220 struct udf_vds_record vds[VDS_POS_LENGTH];
1221 struct generic_desc *gd;
1222 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001223 int done = 0;
1224 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 uint32_t vdsn;
1226 uint16_t ident;
1227 long next_s = 0, next_e = 0;
1228
1229 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1230
1231 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001232 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001235 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237
1238 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1239 gd = (struct generic_desc *)bh->b_data;
1240 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001241 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001242 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001243 if (vdsn >= vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001244 vds[VDS_POS_PRIMARY_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001245 vds[VDS_POS_PRIMARY_VOL_DESC].block = block;
1246 }
1247 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001248 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001249 if (vdsn >= vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum) {
1250 vds[VDS_POS_VOL_DESC_PTR].volDescSeqNum = vdsn;
1251 vds[VDS_POS_VOL_DESC_PTR].block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001253 vdp = (struct volDescPtr *)bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001254 next_s = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1255 next_e = le32_to_cpu(vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001256 next_e = next_e >> sb->s_blocksize_bits;
1257 next_e += next_s;
1258 }
1259 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001260 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001261 if (vdsn >= vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001262 vds[VDS_POS_IMP_USE_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001263 vds[VDS_POS_IMP_USE_VOL_DESC].block = block;
1264 }
1265 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001266 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001267 if (!vds[VDS_POS_PARTITION_DESC].block)
1268 vds[VDS_POS_PARTITION_DESC].block = block;
1269 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001270 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001271 if (vdsn >= vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001272 vds[VDS_POS_LOGICAL_VOL_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001273 vds[VDS_POS_LOGICAL_VOL_DESC].block = block;
1274 }
1275 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001276 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
1277 if (vdsn >= vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum) {
1278 vds[VDS_POS_UNALLOC_SPACE_DESC].volDescSeqNum = vdsn;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001279 vds[VDS_POS_UNALLOC_SPACE_DESC].block = block;
1280 }
1281 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001282 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001283 vds[VDS_POS_TERMINATING_DESC].block = block;
1284 if (next_e) {
1285 block = next_s;
1286 lastblock = next_e;
1287 next_s = next_e = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001288 } else {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 done = 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001290 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001293 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001295 for (i = 0; i < VDS_POS_LENGTH; i++) {
1296 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001297 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1298 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001300 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001302 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001303 if (udf_load_logicalvol(sb, bh, fileset)) {
1304 brelse(bh);
1305 return 1;
1306 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001307 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001309 if (udf_load_partdesc(sb, bh)) {
1310 brelse(bh);
1311 return 1;
1312 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001313 for (j = vds[i].block + 1;
1314 j < vds[VDS_POS_TERMINATING_DESC].block;
1315 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 bh2 = udf_read_tagged(sb, j, j, &ident);
1317 gd = (struct generic_desc *)bh2->b_data;
1318 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001319 if (udf_load_partdesc(sb,
1320 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001321 brelse(bh);
1322 brelse(bh2);
1323 return 1;
1324 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001325 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001328 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330 }
1331
1332 return 0;
1333}
1334
1335/*
1336 * udf_check_valid()
1337 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001338static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 long block;
1341
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 udf_debug("Validity check skipped because of novrs option\n");
1344 return 0;
1345 }
1346 /* Check that it is NSR02 compliant */
1347 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001348 else {
1349 block = udf_vrs(sb, silent);
1350 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001351 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001352 udf_debug("Failed to read byte 32768. Assuming open "
1353 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001354 if (!sbi->s_last_block)
1355 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001356 return 0;
1357 } else
1358 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001362static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct anchorVolDescPtr *anchor;
1365 uint16_t ident;
1366 struct buffer_head *bh;
1367 long main_s, main_e, reserve_s, reserve_e;
1368 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001369 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (!sb)
1372 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001373 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001375 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
1376 if (sbi->s_anchor[i] &&
1377 (bh = udf_read_tagged(sb, sbi->s_anchor[i],
1378 sbi->s_anchor[i], &ident))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 anchor = (struct anchorVolDescPtr *)bh->b_data;
1380
1381 /* Locate the main sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001382 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001383 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 main_e = main_e >> sb->s_blocksize_bits;
1385 main_e += main_s;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /* Locate the reserve sequence */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001388 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1389 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 reserve_e = reserve_e >> sb->s_blocksize_bits;
1391 reserve_e += reserve_s;
1392
Jan Kara3bf25cb2007-05-08 00:35:16 -07001393 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* Process the main & reserve sequences */
1396 /* responsible for finding the PartitionDesc(s) */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001397 if (!(udf_process_sequence(sb, main_s, main_e, fileset) &&
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001398 udf_process_sequence(sb, reserve_s, reserve_e, fileset)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401 }
1402
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001403 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 udf_debug("No Anchor block found\n");
1405 return 1;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001406 } else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001407 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001409 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001410 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001411 struct udf_part_map *map = &sbi->s_partmaps[i];
1412 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001413 case UDF_VIRTUAL_MAP15:
1414 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001415 if (!sbi->s_last_block) {
1416 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001417 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001419
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001420 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001421 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001422 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001423 return 1;
1424 }
1425
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001426 for (j = 0; j < sbi->s_partitions; j++) {
1427 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001428 if (j != i &&
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001429 map->s_volumeseqnum == map2->s_volumeseqnum &&
1430 map->s_partition_num == map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001431 ino.partitionReferenceNum = j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001432 ino.logicalBlockNum = sbi->s_last_block - map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001433 break;
1434 }
1435 }
1436
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001437 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001438 return 1;
1439
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001440 sbi->s_vat_inode = udf_iget(sb, ino);
1441 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001442 return 1;
1443
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001444 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1445 map->s_type_specific.s_virtual.s_start_offset =
1446 udf_ext0_offset(sbi->s_vat_inode);
1447 map->s_type_specific.s_virtual.s_num_entries =
1448 (sbi->s_vat_inode->i_size - 36) >> 2;
1449 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001450 uint32_t pos;
1451
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001452 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001453 bh = sb_bread(sb, pos);
1454 if (!bh)
1455 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001456 map->s_type_specific.s_virtual.s_start_offset =
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001457 le16_to_cpu(((struct virtualAllocationTable20 *)bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001458 udf_ext0_offset(sbi->s_vat_inode))->lengthHeader) +
1459 udf_ext0_offset(sbi->s_vat_inode);
1460 map->s_type_specific.s_virtual.s_num_entries = (sbi->s_vat_inode->i_size -
1461 map->s_type_specific.s_virtual.s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001462 brelse(bh);
1463 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001464 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1465 map->s_partition_len =
1466 sbi->s_partmaps[ino.partitionReferenceNum].
1467 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
1469 }
1470 return 0;
1471}
1472
1473static void udf_open_lvid(struct super_block *sb)
1474{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001475 struct udf_sb_info *sbi = UDF_SB(sb);
1476 struct buffer_head *bh = sbi->s_lvid_bh;
1477 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int i;
1479 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001480 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1481 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001483 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1484 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001486 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1487 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001489 lvid->descTag.descCRC = cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1490 le16_to_cpu(lvid->descTag.descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001492 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001493 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001495 lvid->descTag.tagChecksum +=
1496 ((uint8_t *) &(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001498 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500}
1501
1502static void udf_close_lvid(struct super_block *sb)
1503{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001504 kernel_timestamp cpu_time;
1505 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001506 struct udf_sb_info *sbi = UDF_SB(sb);
1507 struct buffer_head *bh = sbi->s_lvid_bh;
1508 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001509
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001510 if (!bh)
1511 return;
1512
1513 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1514
1515 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
1516 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1517 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1518 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001520 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1521 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1522 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1523 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1524 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1525 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1526 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1527 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001529 lvid->descTag.descCRC =
1530 cpu_to_le16(udf_crc((char *)lvid + sizeof(tag),
1531 le16_to_cpu(lvid->descTag.descCRCLength), 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001533 lvid->descTag.tagChecksum = 0;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001534 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (i != 4)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001536 lvid->descTag.tagChecksum +=
1537 ((uint8_t *)&(lvid->descTag))[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001539 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541}
1542
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001543static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1544{
1545 int i;
1546 int nr_groups = bitmap->s_nr_groups;
1547 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) * nr_groups);
1548
1549 for (i = 0; i < nr_groups; i++)
1550 if (bitmap->s_block_bitmap[i])
1551 brelse(bitmap->s_block_bitmap[i]);
1552
1553 if (size <= PAGE_SIZE)
1554 kfree(bitmap);
1555 else
1556 vfree(bitmap);
1557}
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559/*
1560 * udf_read_super
1561 *
1562 * PURPOSE
1563 * Complete the specified super block.
1564 *
1565 * PRE-CONDITIONS
1566 * sb Pointer to superblock to complete - never NULL.
1567 * sb->s_dev Device to read suberblock from.
1568 * options Pointer to mount options.
1569 * silent Silent flag.
1570 *
1571 * HISTORY
1572 * July 1, 1997 - Andrew E. Mileski
1573 * Written, tested, and released.
1574 */
1575static int udf_fill_super(struct super_block *sb, void *options, int silent)
1576{
1577 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001578 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 struct udf_options uopt;
1580 kernel_lb_addr rootdir, fileset;
1581 struct udf_sb_info *sbi;
1582
1583 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1584 uopt.uid = -1;
1585 uopt.gid = -1;
1586 uopt.umask = 0;
1587
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001588 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (!sbi)
1590 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001594 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (!udf_parse_options((char *)options, &uopt))
1597 goto error_out;
1598
1599 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001602 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto error_out;
1604 }
1605#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001606 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 uopt.nls_map = load_nls_default();
1608 if (!uopt.nls_map)
1609 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1610 else
1611 udf_debug("Using default NLS map\n");
1612 }
1613#endif
1614 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1615 uopt.flags |= (1 << UDF_FLAG_UTF8);
1616
1617 fileset.logicalBlockNum = 0xFFFFFFFF;
1618 fileset.partitionReferenceNum = 0xFFFF;
1619
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001620 sbi->s_flags = uopt.flags;
1621 sbi->s_uid = uopt.uid;
1622 sbi->s_gid = uopt.gid;
1623 sbi->s_umask = uopt.umask;
1624 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* Set the block size for all transfers */
1627 if (!udf_set_blocksize(sb, uopt.blocksize))
1628 goto error_out;
1629
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001630 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001631 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001633 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001635 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001637 sbi->s_last_block = uopt.lastblock;
1638 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1639 sbi->s_anchor[2] = uopt.anchor;
1640 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001642 if (udf_check_valid(sb, uopt.novrs, silent)) {
1643 /* read volume recognition sequences */
1644 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001645 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
1648 udf_find_anchor(sb);
1649
1650 /* Fill in the rest of the superblock */
1651 sb->s_op = &udf_sb_ops;
1652 sb->dq_op = NULL;
1653 sb->s_dirt = 0;
1654 sb->s_magic = UDF_SUPER_MAGIC;
1655 sb->s_time_gran = 1000;
1656
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001657 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001658 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto error_out;
1660 }
1661
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001662 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001664 if (sbi->s_lvid_bh) {
1665 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sbi);
1666 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1667 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
1668 /* uint16_t maxUDFWriteRev = le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001670 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001671 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x (max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001672 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001673 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 goto error_out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 sb->s_flags |= MS_RDONLY;
1677 }
1678
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001679 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1682 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1683 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1684 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1685 }
1686
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001687 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001688 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 goto error_out;
1690 }
1691
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001692 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags & UDF_PART_FLAG_READ_ONLY) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001693 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001694 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001695 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001696
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001697 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001698 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 goto error_out;
1700 }
1701
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001702 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001704 udf_time_to_stamp(&ts, sbi->s_record_time);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001705 udf_info("UDF %s (%s) Mounting volume '%s', "
1706 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
1707 UDFFS_VERSION, UDFFS_DATE,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001708 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1709 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711 if (!(sb->s_flags & MS_RDONLY))
1712 udf_open_lvid(sb);
1713
1714 /* Assign the root inode */
1715 /* assign inodes by physical block number */
1716 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001717 inode = udf_iget(sb, rootdir);
1718 if (!inode) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001719 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001720 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto error_out;
1722 }
1723
1724 /* Allocate a dentry for the root inode */
1725 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001726 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001727 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 iput(inode);
1729 goto error_out;
1730 }
Jan Kara31170b62007-05-08 00:35:21 -07001731 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return 0;
1733
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001734error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001735 if (sbi->s_vat_inode)
1736 iput(sbi->s_vat_inode);
1737 if (sbi->s_partitions) {
1738 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1739 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1740 iput(map->s_uspace.s_table);
1741 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1742 iput(map->s_fspace.s_table);
1743 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001744 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001745 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001746 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001747 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001748 for (i = 0; i < 4; i++)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001749 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751#ifdef CONFIG_UDF_NLS
1752 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001753 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#endif
1755 if (!(sb->s_flags & MS_RDONLY))
1756 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001757 brelse(sbi->s_lvid_bh);
1758
1759 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 kfree(sbi);
1761 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EINVAL;
1764}
1765
1766void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001767 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 va_list args;
1770
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* mark sb error */
1773 sb->s_dirt = 1;
1774 }
1775 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001776 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001778 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001779 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
1782void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001783 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 va_list args;
1786
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001787 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001788 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 va_end(args);
1790 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001791 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794/*
1795 * udf_put_super
1796 *
1797 * PURPOSE
1798 * Prepare for destruction of the superblock.
1799 *
1800 * DESCRIPTION
1801 * Called before the filesystem is unmounted.
1802 *
1803 * HISTORY
1804 * July 1, 1997 - Andrew E. Mileski
1805 * Written, tested, and released.
1806 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001807static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001810 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001812 sbi = UDF_SB(sb);
1813 if (sbi->s_vat_inode)
1814 iput(sbi->s_vat_inode);
1815 if (sbi->s_partitions) {
1816 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1817 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1818 iput(map->s_uspace.s_table);
1819 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1820 iput(map->s_fspace.s_table);
1821 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001822 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001823 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001824 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001825 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001826 for (i = 0; i < 4; i++)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001827 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829#ifdef CONFIG_UDF_NLS
1830 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001831 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#endif
1833 if (!(sb->s_flags & MS_RDONLY))
1834 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001835 brelse(sbi->s_lvid_bh);
1836 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 kfree(sb->s_fs_info);
1838 sb->s_fs_info = NULL;
1839}
1840
1841/*
1842 * udf_stat_fs
1843 *
1844 * PURPOSE
1845 * Return info about the filesystem.
1846 *
1847 * DESCRIPTION
1848 * Called by sys_statfs()
1849 *
1850 * HISTORY
1851 * July 1, 1997 - Andrew E. Mileski
1852 * Written, tested, and released.
1853 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001854static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
David Howells726c3342006-06-23 02:02:58 -07001856 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001857 struct udf_sb_info *sbi = UDF_SB(sb);
1858 struct logicalVolIntegrityDescImpUse *lvidiu;
1859
1860 if (sbi->s_lvid_bh != NULL)
1861 lvidiu = udf_sb_lvidiu(sbi);
1862 else
1863 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 buf->f_type = UDF_SUPER_MAGIC;
1866 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001867 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 buf->f_bfree = udf_count_free(sb);
1869 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001870 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1871 le32_to_cpu(lvidiu->numDirs)) : 0)
1872 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 buf->f_ffree = buf->f_bfree;
1874 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001875 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 return 0;
1878}
1879
1880static unsigned char udf_bitmap_lookup[16] = {
1881 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1882};
1883
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001884static unsigned int udf_count_free_bitmap(struct super_block *sb, struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 struct buffer_head *bh = NULL;
1887 unsigned int accum = 0;
1888 int index;
1889 int block = 0, newblock;
1890 kernel_lb_addr loc;
1891 uint32_t bytes;
1892 uint8_t value;
1893 uint8_t *ptr;
1894 uint16_t ident;
1895 struct spaceBitmapDesc *bm;
1896
1897 lock_kernel();
1898
1899 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001900 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 bh = udf_read_ptagged(sb, loc, 0, &ident);
1902
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001903 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 printk(KERN_ERR "udf: udf_count_free failed\n");
1905 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001906 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001907 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 printk(KERN_ERR "udf: udf_count_free failed\n");
1909 goto out;
1910 }
1911
1912 bm = (struct spaceBitmapDesc *)bh->b_data;
1913 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001914 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1915 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001917 while (bytes > 0) {
1918 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 accum += udf_bitmap_lookup[value & 0x0f];
1921 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 index++;
1923 bytes--;
1924 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001925 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001926 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 newblock = udf_get_lb_pblock(sb, loc, ++block);
1928 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001929 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 udf_debug("read failed\n");
1931 goto out;
1932 }
1933 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001934 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001937 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001939out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 unlock_kernel();
1941
1942 return accum;
1943}
1944
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001945static unsigned int udf_count_free_table(struct super_block *sb, struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07001948 uint32_t elen;
1949 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07001951 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 lock_kernel();
1954
Jan Karaff116fc2007-05-08 00:35:14 -07001955 epos.block = UDF_I_LOCATION(table);
1956 epos.offset = sizeof(struct unallocSpaceEntry);
1957 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001959 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001961
Jan Kara3bf25cb2007-05-08 00:35:16 -07001962 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 unlock_kernel();
1965
1966 return accum;
1967}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001968
1969static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001972 struct udf_sb_info *sbi;
1973 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001975 sbi = UDF_SB(sb);
1976 if (sbi->s_lvid_bh) {
1977 struct logicalVolIntegrityDesc *lvid = (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
1978 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
1979 accum = le32_to_cpu(lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (accum == 0xFFFFFFFF)
1981 accum = 0;
1982 }
1983 }
1984
1985 if (accum)
1986 return accum;
1987
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001988 map = &sbi->s_partmaps[sbi->s_partition];
1989 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001990 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001991 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001993 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001994 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001995 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 if (accum)
1998 return accum;
1999
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002000 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002001 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002002 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002004 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002005 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002006 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008
2009 return accum;
2010}