blob: b9e719de07048afa43cc1e5a625d0ff2eae10cec [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>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
88static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
90 kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void udf_open_lvid(struct super_block *);
94static void udf_close_lvid(struct super_block *);
95static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070096static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080097static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020098static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800101struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
102{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700112static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells454e2392006-06-23 02:02:57 -0700116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700120 .owner = THIS_MODULE,
121 .name = "udf",
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct inode *udf_alloc_inode(struct super_block *sb)
130{
131 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!ei)
134 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700135
136 ei->i_unique = 0;
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
140 ei->i_strat4096 = 0;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return &ei->vfs_inode;
143}
144
145static void udf_destroy_inode(struct inode *inode)
146{
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
148}
149
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700150static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Lametera35afb82007-05-16 22:10:57 -0700154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int init_inodecache(void)
159{
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 0, (SLAB_RECLAIM_ACCOUNT |
163 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900164 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENOMEM;
167 return 0;
168}
169
170static void destroy_inodecache(void)
171{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700172 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800176static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800186 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char novrs;
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
194 unsigned int anchor;
195 unsigned int volume;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
199 unsigned int flags;
200 mode_t umask;
201 gid_t gid;
202 uid_t uid;
203 struct nls_table *nls_map;
204};
205
206static int __init init_udf_fs(void)
207{
208 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = init_inodecache();
211 if (err)
212 goto out1;
213 err = register_filesystem(&udf_fstype);
214 if (err)
215 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return err;
224}
225
226static void __exit exit_udf_fs(void)
227{
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
230}
231
232module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700233module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800235static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
236{
237 struct udf_sb_info *sbi = UDF_SB(sb);
238
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
240 GFP_KERNEL);
241 if (!sbi->s_partmaps) {
242 udf_error(sb, __FUNCTION__,
243 "Unable to allocate space for %d partition maps",
244 count);
245 sbi->s_partitions = 0;
246 return -ENOMEM;
247 }
248
249 sbi->s_partitions = count;
250 return 0;
251}
252
Miklos Szeredi6da80892008-02-08 04:21:50 -0800253static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
254{
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
257
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
260 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
285 seq_printf(seq, ",session=%u", sbi->s_session);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
287 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
288 /*
289 * s_anchor[2] could be zeroed out in case there is no anchor
290 * in the specified block, but then the "anchor=N" option
291 * originally given by the user wasn't effective, so it's OK
292 * if we don't show it.
293 */
294 if (sbi->s_anchor[2] != 0)
295 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
296 /*
297 * volume, partition, fileset and rootdir seem to be ignored
298 * currently
299 */
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
301 seq_puts(seq, ",utf8");
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
303 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
304
305 return 0;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * udf_parse_options
310 *
311 * PURPOSE
312 * Parse mount options.
313 *
314 * DESCRIPTION
315 * The following mount options are supported:
316 *
317 * gid= Set the default group.
318 * umask= Set the default umask.
319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
329 *
330 * The remaining are for debugging and disaster recovery:
331 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * The following expect a offset from 0.
335 *
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
341 *
342 * The following expect a offset from the partition root.
343 *
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
348 *
349 * PRE-CONDITIONS
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
352 *
353 * POST-CONDITIONS
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
356 *
357 * HISTORY
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
360 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362enum {
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 {Opt_novrs, "novrs"},
373 {Opt_nostrict, "nostrict"},
374 {Opt_bs, "bs=%u"},
375 {Opt_unhide, "unhide"},
376 {Opt_undelete, "undelete"},
377 {Opt_noadinicb, "noadinicb"},
378 {Opt_adinicb, "adinicb"},
379 {Opt_shortad, "shortad"},
380 {Opt_longad, "longad"},
381 {Opt_uforget, "uid=forget"},
382 {Opt_uignore, "uid=ignore"},
383 {Opt_gforget, "gid=forget"},
384 {Opt_gignore, "gid=ignore"},
385 {Opt_gid, "gid=%u"},
386 {Opt_uid, "uid=%u"},
387 {Opt_umask, "umask=%o"},
388 {Opt_session, "session=%u"},
389 {Opt_lastblock, "lastblock=%u"},
390 {Opt_anchor, "anchor=%u"},
391 {Opt_volume, "volume=%u"},
392 {Opt_partition, "partition=%u"},
393 {Opt_fileset, "fileset=%u"},
394 {Opt_rootdir, "rootdir=%u"},
395 {Opt_utf8, "utf8"},
396 {Opt_iocharset, "iocharset=%s"},
397 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Miklos Szeredi6da80892008-02-08 04:21:50 -0800400static int udf_parse_options(char *options, struct udf_options *uopt,
401 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 char *p;
404 int option;
405
406 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800407 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 uopt->partition = 0xFFFF;
409 uopt->session = 0xFFFFFFFF;
410 uopt->lastblock = 0;
411 uopt->anchor = 0;
412 uopt->volume = 0xFFFFFFFF;
413 uopt->rootdir = 0xFFFFFFFF;
414 uopt->fileset = 0xFFFFFFFF;
415 uopt->nls_map = NULL;
416
417 if (!options)
418 return 1;
419
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 substring_t args[MAX_OPT_ARGS];
422 int token;
423 if (!*p)
424 continue;
425
426 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 switch (token) {
428 case Opt_novrs:
429 uopt->novrs = 1;
430 case Opt_bs:
431 if (match_int(&args[0], &option))
432 return 0;
433 uopt->blocksize = option;
434 break;
435 case Opt_unhide:
436 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
437 break;
438 case Opt_undelete:
439 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
440 break;
441 case Opt_noadinicb:
442 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
443 break;
444 case Opt_adinicb:
445 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_shortad:
448 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
449 break;
450 case Opt_longad:
451 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_gid:
454 if (match_int(args, &option))
455 return 0;
456 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700457 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458 break;
459 case Opt_uid:
460 if (match_int(args, &option))
461 return 0;
462 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700463 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700464 break;
465 case Opt_umask:
466 if (match_octal(args, &option))
467 return 0;
468 uopt->umask = option;
469 break;
470 case Opt_nostrict:
471 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
472 break;
473 case Opt_session:
474 if (match_int(args, &option))
475 return 0;
476 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800477 if (!remount)
478 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479 break;
480 case Opt_lastblock:
481 if (match_int(args, &option))
482 return 0;
483 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 break;
487 case Opt_anchor:
488 if (match_int(args, &option))
489 return 0;
490 uopt->anchor = option;
491 break;
492 case Opt_volume:
493 if (match_int(args, &option))
494 return 0;
495 uopt->volume = option;
496 break;
497 case Opt_partition:
498 if (match_int(args, &option))
499 return 0;
500 uopt->partition = option;
501 break;
502 case Opt_fileset:
503 if (match_int(args, &option))
504 return 0;
505 uopt->fileset = option;
506 break;
507 case Opt_rootdir:
508 if (match_int(args, &option))
509 return 0;
510 uopt->rootdir = option;
511 break;
512 case Opt_utf8:
513 uopt->flags |= (1 << UDF_FLAG_UTF8);
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 case Opt_iocharset:
517 uopt->nls_map = load_nls(args[0].from);
518 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 case Opt_uignore:
522 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
523 break;
524 case Opt_uforget:
525 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
526 break;
527 case Opt_gignore:
528 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
529 break;
530 case Opt_gforget:
531 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
532 break;
533 default:
534 printk(KERN_ERR "udf: bad mount option \"%s\" "
535 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537 }
538 }
539 return 1;
540}
541
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800542static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!(sb->s_flags & MS_RDONLY))
547 udf_open_lvid(sb);
548 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unlock_kernel();
551}
552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800556 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800558 uopt.flags = sbi->s_flags;
559 uopt.uid = sbi->s_uid;
560 uopt.gid = sbi->s_gid;
561 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Miklos Szeredi6da80892008-02-08 04:21:50 -0800563 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EINVAL;
565
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800566 sbi->s_flags = uopt.flags;
567 sbi->s_uid = uopt.uid;
568 sbi->s_gid = uopt.gid;
569 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800571 if (sbi->s_lvid_bh) {
572 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (write_rev > UDF_MAX_WRITE_VERSION)
574 *flags |= MS_RDONLY;
575 }
576
577 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
578 return 0;
579 if (*flags & MS_RDONLY)
580 udf_close_lvid(sb);
581 else
582 udf_open_lvid(sb);
583
584 return 0;
585}
586
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct volStructDesc *vsd = NULL;
590 int sector = 32768;
591 int sectorsize;
592 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 int iso9660 = 0;
594 int nsr02 = 0;
595 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800596 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Block size must be a multiple of 512 */
599 if (sb->s_blocksize & 511)
600 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800601 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (sb->s_blocksize < sizeof(struct volStructDesc))
604 sectorsize = sizeof(struct volStructDesc);
605 else
606 sectorsize = sb->s_blocksize;
607
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800608 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Read a block */
615 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
616 if (!bh)
617 break;
618
619 /* Look for ISO descriptors */
620 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800621 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700623 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700624 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800626 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
627 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 switch (vsd->structType) {
630 case 0:
631 udf_debug("ISO9660 Boot Record found\n");
632 break;
633 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800634 udf_debug("ISO9660 Primary Volume Descriptor "
635 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636 break;
637 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800638 udf_debug("ISO9660 Supplementary Volume "
639 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 break;
641 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800642 udf_debug("ISO9660 Volume Partition Descriptor "
643 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 break;
645 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800646 udf_debug("ISO9660 Volume Descriptor Set "
647 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 break;
649 default:
650 udf_debug("ISO9660 VRS (%u) found\n",
651 vsd->structType);
652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800654 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
655 VSD_STD_ID_LEN))
656 ; /* nothing */
657 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
658 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700659 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
662 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800664 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
665 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700667 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 if (nsr03)
671 return nsr03;
672 else if (nsr02)
673 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800674 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -1;
676 else
677 return 0;
678}
679
680/*
Jan Kara423cf6d2008-04-07 15:59:23 +0200681 * Check whether there is an anchor block in the given block
682 */
683static int udf_check_anchor_block(struct super_block *sb, sector_t block,
684 bool varconv)
685{
686 struct buffer_head *bh = NULL;
687 tag *t;
688 uint16_t ident;
689 uint32_t location;
690
691 if (varconv)
692 bh = sb_bread(sb, udf_fixed_to_variable(block));
693 else
694 bh = sb_bread(sb, block);
695
696 if (!bh)
697 return 0;
698
699 t = (tag *)bh->b_data;
700 ident = le16_to_cpu(t->tagIdent);
701 location = le32_to_cpu(t->tagLocation);
702 brelse(bh);
703 if (ident != TAG_IDENT_AVDP)
704 return 0;
705 return location == block;
706}
707
708/* Search for an anchor volume descriptor pointer */
709static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
710 sector_t lastblock)
711{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200712 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200713 int i;
714 struct udf_sb_info *sbi = UDF_SB(sb);
715
716 last[0] = lastblock;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200717 last[1] = last[0] - 1;
718 last[2] = last[0] + 1;
719 last[3] = last[0] - 2;
720 last[4] = last[0] - 150;
721 last[5] = last[0] - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200722
723 /* according to spec, anchor is in either:
724 * block 256
725 * lastblock-256
726 * lastblock
727 * however, if the disc isn't closed, it could be 512 */
728
729 for (i = 0; i < ARRAY_SIZE(last); i++) {
730 if (last[i] < 0)
731 continue;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200732 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
733 sb->s_blocksize_bits)
734 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200735
736 if (udf_check_anchor_block(sb, last[i], varconv)) {
737 sbi->s_anchor[0] = last[i];
738 sbi->s_anchor[1] = last[i] - 256;
739 return last[i];
740 }
741
742 if (last[i] < 256)
743 continue;
744
745 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
746 sbi->s_anchor[1] = last[i] - 256;
747 return last[i];
748 }
749 }
750
751 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
752 sbi->s_anchor[0] = sbi->s_session + 256;
753 return last[0];
754 }
755 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
756 sbi->s_anchor[0] = sbi->s_session + 512;
757 return last[0];
758 }
759 return 0;
760}
761
762/*
763 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
764 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200766 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Jan Kara423cf6d2008-04-07 15:59:23 +0200771 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct buffer_head *bh = NULL;
773 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int i;
Jan Kara423cf6d2008-04-07 15:59:23 +0200775 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800776
Jan Kara423cf6d2008-04-07 15:59:23 +0200777 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
778 if (lastblock)
779 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Jan Kara423cf6d2008-04-07 15:59:23 +0200781 /* No anchor found? Try VARCONV conversion of block numbers */
782 /* Firstly, we try to not convert number of the last block */
783 lastblock = udf_scan_anchors(sb, 1,
784 udf_variable_to_fixed(sbi->s_last_block));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700785 if (lastblock) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200786 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
787 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Jan Kara423cf6d2008-04-07 15:59:23 +0200790 /* Secondly, we try with converted number of the last block */
791 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
792 if (lastblock)
793 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Jan Kara423cf6d2008-04-07 15:59:23 +0200795check_anchor:
796 /*
797 * Check located anchors and the anchor block supplied via
798 * mount options
799 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800800 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100801 if (!sbi->s_anchor[i])
802 continue;
803 bh = udf_read_tagged(sb, sbi->s_anchor[i],
804 sbi->s_anchor[i], &ident);
805 if (!bh)
806 sbi->s_anchor[i] = 0;
807 else {
808 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200809 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800810 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
813
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800814 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800817static int udf_find_fileset(struct super_block *sb,
818 kernel_lb_addr *fileset,
819 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct buffer_head *bh = NULL;
822 long lastblock;
823 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800824 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700827 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
829
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700830 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700832 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700833 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return 1;
835 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800839 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800840 if (!bh) {
841 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 kernel_lb_addr newfileset;
843
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700844/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700846
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800847 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700848 (newfileset.partitionReferenceNum != 0xFFFF &&
849 fileset->logicalBlockNum == 0xFFFFFFFF &&
850 fileset->partitionReferenceNum == 0xFFFF);
851 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800852 lastblock = sbi->s_partmaps
853 [newfileset.partitionReferenceNum]
854 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 newfileset.logicalBlockNum = 0;
856
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800858 bh = udf_read_ptagged(sb, newfileset, 0,
859 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700860 if (!bh) {
861 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 continue;
863 }
864
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 switch (ident) {
866 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700867 {
868 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800869 sp = (struct spaceBitmapDesc *)
870 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700871 newfileset.logicalBlockNum += 1 +
872 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800873 sizeof(struct spaceBitmapDesc)
874 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700875 brelse(bh);
876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700878 case TAG_IDENT_FSD:
879 *fileset = newfileset;
880 break;
881 default:
882 newfileset.logicalBlockNum++;
883 brelse(bh);
884 bh = NULL;
885 break;
886 }
887 } while (newfileset.logicalBlockNum < lastblock &&
888 fileset->logicalBlockNum == 0xFFFFFFFF &&
889 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 }
892
893 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700894 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700896 fileset->logicalBlockNum,
897 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800899 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700901 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return 0;
903 }
904 return 1;
905}
906
Jan Karac0eb31e2008-04-01 16:50:35 +0200907static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct primaryVolDesc *pvoldesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct ustr instr;
911 struct ustr outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200912 struct buffer_head *bh;
913 uint16_t ident;
914
915 bh = udf_read_tagged(sb, block, block, &ident);
916 if (!bh)
917 return 1;
918 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 pvoldesc = (struct primaryVolDesc *)bh->b_data;
921
Marcin Slusarz56774802008-02-10 11:25:31 +0100922 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
923 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100924#ifdef UDFFS_DEBUG
925 timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100926 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800927 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100928 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
929 ts->minute, le16_to_cpu(ts->typeAndTimezone));
930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Marcin Slusarz4b111112008-02-08 04:20:36 -0800933 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700934 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800935 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800937 udf_debug("volIdent[] = '%s'\n",
938 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Marcin Slusarz4b111112008-02-08 04:20:36 -0800941 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (udf_CS0toUTF8(&outstr, &instr))
943 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200944
945 brelse(bh);
946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700949static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
950 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct fileSetDesc *fset;
953
954 fset = (struct fileSetDesc *)bh->b_data;
955
956 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
957
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800958 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700960 udf_debug("Rootdir at block=%d, partition=%d\n",
961 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800964int udf_compute_nr_groups(struct super_block *sb, u32 partition)
965{
966 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100967 return DIV_ROUND_UP(map->s_partition_len +
968 (sizeof(struct spaceBitmapDesc) << 3),
969 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800970}
971
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800972static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
973{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800974 struct udf_bitmap *bitmap;
975 int nr_groups;
976 int size;
977
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800978 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800979 size = sizeof(struct udf_bitmap) +
980 (sizeof(struct buffer_head *) * nr_groups);
981
982 if (size <= PAGE_SIZE)
983 bitmap = kmalloc(size, GFP_KERNEL);
984 else
985 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
986
987 if (bitmap == NULL) {
988 udf_error(sb, __FUNCTION__,
989 "Unable to allocate space for bitmap "
990 "and %d buffer_head pointers", nr_groups);
991 return NULL;
992 }
993
994 memset(bitmap, 0x00, size);
995 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
996 bitmap->s_nr_groups = nr_groups;
997 return bitmap;
998}
999
Jan Kara3fb38df2008-04-02 12:26:36 +02001000static int udf_fill_partdesc_info(struct super_block *sb,
1001 struct partitionDesc *p, int p_index)
1002{
1003 struct udf_part_map *map;
1004 struct udf_sb_info *sbi = UDF_SB(sb);
1005 struct partitionHeaderDesc *phd;
1006
1007 map = &sbi->s_partmaps[p_index];
1008
1009 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1010 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1011
1012 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1013 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1014 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1015 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1016 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1017 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1018 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1019 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1020
1021 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1022 "block length %d\n", partitionNumber, p_index,
1023 map->s_partition_type, map->s_partition_root,
1024 map->s_partition_len);
1025
1026 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1027 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1028 return 0;
1029
1030 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1031 if (phd->unallocSpaceTable.extLength) {
1032 kernel_lb_addr loc = {
1033 .logicalBlockNum = le32_to_cpu(
1034 phd->unallocSpaceTable.extPosition),
1035 .partitionReferenceNum = p_index,
1036 };
1037
1038 map->s_uspace.s_table = udf_iget(sb, loc);
1039 if (!map->s_uspace.s_table) {
1040 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1041 p_index);
1042 return 1;
1043 }
1044 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1045 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1046 p_index, map->s_uspace.s_table->i_ino);
1047 }
1048
1049 if (phd->unallocSpaceBitmap.extLength) {
1050 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1051 if (!bitmap)
1052 return 1;
1053 map->s_uspace.s_bitmap = bitmap;
1054 bitmap->s_extLength = le32_to_cpu(
1055 phd->unallocSpaceBitmap.extLength);
1056 bitmap->s_extPosition = le32_to_cpu(
1057 phd->unallocSpaceBitmap.extPosition);
1058 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1059 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1060 bitmap->s_extPosition);
1061 }
1062
1063 if (phd->partitionIntegrityTable.extLength)
1064 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1065
1066 if (phd->freedSpaceTable.extLength) {
1067 kernel_lb_addr loc = {
1068 .logicalBlockNum = le32_to_cpu(
1069 phd->freedSpaceTable.extPosition),
1070 .partitionReferenceNum = p_index,
1071 };
1072
1073 map->s_fspace.s_table = udf_iget(sb, loc);
1074 if (!map->s_fspace.s_table) {
1075 udf_debug("cannot load freedSpaceTable (part %d)\n",
1076 p_index);
1077 return 1;
1078 }
1079
1080 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1081 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1082 p_index, map->s_fspace.s_table->i_ino);
1083 }
1084
1085 if (phd->freedSpaceBitmap.extLength) {
1086 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1087 if (!bitmap)
1088 return 1;
1089 map->s_fspace.s_bitmap = bitmap;
1090 bitmap->s_extLength = le32_to_cpu(
1091 phd->freedSpaceBitmap.extLength);
1092 bitmap->s_extPosition = le32_to_cpu(
1093 phd->freedSpaceBitmap.extPosition);
1094 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1095 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1096 bitmap->s_extPosition);
1097 }
1098 return 0;
1099}
1100
Jan Kara38b74a52008-04-02 16:01:35 +02001101static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1102{
1103 struct udf_sb_info *sbi = UDF_SB(sb);
1104 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1105 kernel_lb_addr ino;
1106 struct buffer_head *bh;
1107
1108 /* VAT file entry is in the last recorded block */
1109 ino.partitionReferenceNum = type1_index;
1110 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1111 sbi->s_vat_inode = udf_iget(sb, ino);
1112 if (!sbi->s_vat_inode)
1113 return 1;
1114
1115 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1116 map->s_type_specific.s_virtual.s_start_offset =
1117 udf_ext0_offset(sbi->s_vat_inode);
1118 map->s_type_specific.s_virtual.s_num_entries =
1119 (sbi->s_vat_inode->i_size - 36) >> 2;
1120 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1121 uint32_t pos;
1122 struct virtualAllocationTable20 *vat20;
1123
1124 pos = udf_block_map(sbi->s_vat_inode, 0);
1125 bh = sb_bread(sb, pos);
1126 if (!bh)
1127 return 1;
1128 vat20 = (struct virtualAllocationTable20 *)bh->b_data +
1129 udf_ext0_offset(sbi->s_vat_inode);
1130 map->s_type_specific.s_virtual.s_start_offset =
1131 le16_to_cpu(vat20->lengthHeader) +
1132 udf_ext0_offset(sbi->s_vat_inode);
1133 map->s_type_specific.s_virtual.s_num_entries =
1134 (sbi->s_vat_inode->i_size -
1135 map->s_type_specific.s_virtual.
1136 s_start_offset) >> 2;
1137 brelse(bh);
1138 }
1139 return 0;
1140}
1141
Jan Karac0eb31e2008-04-01 16:50:35 +02001142static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jan Karac0eb31e2008-04-01 16:50:35 +02001144 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001145 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001146 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001147 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001148 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001149 uint16_t partitionNumber;
1150 uint16_t ident;
1151 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jan Karac0eb31e2008-04-01 16:50:35 +02001153 bh = udf_read_tagged(sb, block, block, &ident);
1154 if (!bh)
1155 return 1;
1156 if (ident != TAG_IDENT_PD)
1157 goto out_bh;
1158
1159 p = (struct partitionDesc *)bh->b_data;
1160 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001161
1162 /* First scan for TYPE1 and SPARABLE partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001163 for (i = 0; i < sbi->s_partitions; i++) {
1164 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001165 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001166 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001167 if (map->s_partition_num == partitionNumber &&
1168 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1169 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001171 }
1172
Jan Kara38b74a52008-04-02 16:01:35 +02001173 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001174 udf_debug("Partition (%d) not found in partition map\n",
1175 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001176 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001177 }
1178
Jan Kara3fb38df2008-04-02 12:26:36 +02001179 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001180
1181 /*
1182 * Now rescan for VIRTUAL partitions when TYPE1 partitions are
1183 * already set up
1184 */
1185 type1_idx = i;
1186 for (i = 0; i < sbi->s_partitions; i++) {
1187 map = &sbi->s_partmaps[i];
1188
1189 if (map->s_partition_num == partitionNumber &&
1190 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1191 map->s_partition_type == UDF_VIRTUAL_MAP20))
1192 break;
1193 }
1194
1195 if (i >= sbi->s_partitions)
1196 goto out_bh;
1197
1198 ret = udf_fill_partdesc_info(sb, p, i);
1199 if (ret)
1200 goto out_bh;
1201
Jan Kara38b74a52008-04-02 16:01:35 +02001202 ret = udf_load_vat(sb, i, type1_idx);
Jan Karac0eb31e2008-04-01 16:50:35 +02001203out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001204 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001205 brelse(bh);
1206 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Jan Karac0eb31e2008-04-01 16:50:35 +02001209static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001210 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct logicalVolDesc *lvd;
1213 int i, j, offset;
1214 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001215 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001216 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001217 uint16_t ident;
1218 struct buffer_head *bh;
1219 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jan Karac0eb31e2008-04-01 16:50:35 +02001221 bh = udf_read_tagged(sb, block, block, &ident);
1222 if (!bh)
1223 return 1;
1224 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 lvd = (struct logicalVolDesc *)bh->b_data;
1226
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001227 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001228 if (i != 0) {
1229 ret = i;
1230 goto out_bh;
1231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001233 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001234 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001235 i++, offset += gpm->partitionMapLength) {
1236 struct udf_part_map *map = &sbi->s_partmaps[i];
1237 gpm = (struct genericPartitionMap *)
1238 &(lvd->partitionMaps[offset]);
1239 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001240 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001241 struct genericPartitionMap1 *gpm1 =
1242 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001243 map->s_partition_type = UDF_TYPE1_MAP15;
1244 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1245 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1246 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001247 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001248 struct udfPartitionMap2 *upm2 =
1249 (struct udfPartitionMap2 *)gpm;
1250 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1251 strlen(UDF_ID_VIRTUAL))) {
1252 u16 suf =
1253 le16_to_cpu(((__le16 *)upm2->partIdent.
1254 identSuffix)[0]);
1255 if (suf == 0x0150) {
1256 map->s_partition_type =
1257 UDF_VIRTUAL_MAP15;
1258 map->s_partition_func =
1259 udf_get_pblock_virt15;
1260 } else if (suf == 0x0200) {
1261 map->s_partition_type =
1262 UDF_VIRTUAL_MAP20;
1263 map->s_partition_func =
1264 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001266 } else if (!strncmp(upm2->partIdent.ident,
1267 UDF_ID_SPARABLE,
1268 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001271 struct sparablePartitionMap *spm =
1272 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001274 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001275 map->s_type_specific.s_sparing.s_packet_len =
1276 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001278 struct buffer_head *bh2;
1279
1280 loc = le32_to_cpu(
1281 spm->locSparingTable[j]);
1282 bh2 = udf_read_tagged(sb, loc, loc,
1283 &ident);
1284 map->s_type_specific.s_sparing.
1285 s_spar_map[j] = bh2;
1286
Marcin Slusarz165923f2008-02-10 11:33:08 +01001287 if (bh2 == NULL)
1288 continue;
1289
1290 st = (struct sparingTable *)bh2->b_data;
1291 if (ident != 0 || strncmp(
1292 st->sparingIdent.ident,
1293 UDF_ID_SPARING,
1294 strlen(UDF_ID_SPARING))) {
1295 brelse(bh2);
1296 map->s_type_specific.s_sparing.
1297 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001300 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001301 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001302 udf_debug("Unknown ident: %s\n",
1303 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 continue;
1305 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001306 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1307 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001310 i, map->s_partition_num, type,
1311 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001315 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001318 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1319 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001320 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 if (lvd->integritySeqExt.extLength)
1323 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001324
Jan Karac0eb31e2008-04-01 16:50:35 +02001325out_bh:
1326 brelse(bh);
1327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330/*
1331 * udf_load_logicalvolint
1332 *
1333 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001334static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
1336 struct buffer_head *bh = NULL;
1337 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001338 struct udf_sb_info *sbi = UDF_SB(sb);
1339 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 (bh = udf_read_tagged(sb, loc.extLocation,
1343 loc.extLocation, &ident)) &&
1344 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001345 sbi->s_lvid_bh = bh;
1346 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001347
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001348 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001349 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001350 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001351
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001352 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001353 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001357 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001358 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
1361/*
1362 * udf_process_sequence
1363 *
1364 * PURPOSE
1365 * Process a main/reserve volume descriptor sequence.
1366 *
1367 * PRE-CONDITIONS
1368 * sb Pointer to _locked_ superblock.
1369 * block First block of first extent of the sequence.
1370 * lastblock Lastblock of first extent of the sequence.
1371 *
1372 * HISTORY
1373 * July 1, 1997 - Andrew E. Mileski
1374 * Written, tested, and released.
1375 */
Jan Kara200a3592008-03-04 13:03:09 +01001376static noinline int udf_process_sequence(struct super_block *sb, long block,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001377 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct buffer_head *bh = NULL;
1380 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001381 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 struct generic_desc *gd;
1383 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001384 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 uint32_t vdsn;
1386 uint16_t ident;
1387 long next_s = 0, next_e = 0;
1388
1389 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1390
Jan Karac0eb31e2008-04-01 16:50:35 +02001391 /*
1392 * Read the main descriptor sequence and find which descriptors
1393 * are in it.
1394 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001398 if (!bh) {
1399 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1400 "sequence is corrupted or we could not read "
1401 "it.\n", (unsigned long long)block);
1402 return 1;
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1406 gd = (struct generic_desc *)bh->b_data;
1407 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001408 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001409 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001410 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1411 if (vdsn >= curr->volDescSeqNum) {
1412 curr->volDescSeqNum = vdsn;
1413 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001414 }
1415 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001416 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001417 curr = &vds[VDS_POS_VOL_DESC_PTR];
1418 if (vdsn >= curr->volDescSeqNum) {
1419 curr->volDescSeqNum = vdsn;
1420 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001422 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001423 next_s = le32_to_cpu(
1424 vdp->nextVolDescSeqExt.extLocation);
1425 next_e = le32_to_cpu(
1426 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001427 next_e = next_e >> sb->s_blocksize_bits;
1428 next_e += next_s;
1429 }
1430 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001431 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001432 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1433 if (vdsn >= curr->volDescSeqNum) {
1434 curr->volDescSeqNum = vdsn;
1435 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001436 }
1437 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001438 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001439 curr = &vds[VDS_POS_PARTITION_DESC];
1440 if (!curr->block)
1441 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001442 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001443 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001444 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1445 if (vdsn >= curr->volDescSeqNum) {
1446 curr->volDescSeqNum = vdsn;
1447 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001448 }
1449 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001450 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001451 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1452 if (vdsn >= curr->volDescSeqNum) {
1453 curr->volDescSeqNum = vdsn;
1454 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001455 }
1456 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001457 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001458 vds[VDS_POS_TERMINATING_DESC].block = block;
1459 if (next_e) {
1460 block = next_s;
1461 lastblock = next_e;
1462 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001463 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001464 done = 1;
1465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001467 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001469 /*
1470 * Now read interesting descriptors again and process them
1471 * in a suitable order
1472 */
1473 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1474 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1475 return 1;
1476 }
1477 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1478 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jan Karac0eb31e2008-04-01 16:50:35 +02001480 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1481 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1482 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001483
Jan Karac0eb31e2008-04-01 16:50:35 +02001484 if (vds[VDS_POS_PARTITION_DESC].block) {
1485 /*
1486 * We rescan the whole descriptor sequence to find
1487 * partition descriptor blocks and process them.
1488 */
1489 for (block = vds[VDS_POS_PARTITION_DESC].block;
1490 block < vds[VDS_POS_TERMINATING_DESC].block;
1491 block++)
1492 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001493 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 return 0;
1497}
1498
1499/*
1500 * udf_check_valid()
1501 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001505 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001507 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 udf_debug("Validity check skipped because of novrs option\n");
1509 return 0;
1510 }
1511 /* Check that it is NSR02 compliant */
1512 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001513 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001514 if (block == -1)
1515 udf_debug("Failed to read byte 32768. Assuming open "
1516 "disc. Skipping validity check\n");
1517 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001518 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001519 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
Jan Kara38b74a52008-04-02 16:01:35 +02001522static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 struct anchorVolDescPtr *anchor;
1525 uint16_t ident;
1526 struct buffer_head *bh;
1527 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001528 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001529 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 if (!sb)
1532 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001533 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001535 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001536 if (!sbi->s_anchor[i])
1537 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001538
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001539 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1540 &ident);
1541 if (!bh)
1542 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001544 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001545
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001546 /* Locate the main sequence */
1547 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1548 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1549 main_e = main_e >> sb->s_blocksize_bits;
1550 main_e += main_s;
1551
1552 /* Locate the reserve sequence */
1553 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001554 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001555 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001556 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001557 reserve_e = reserve_e >> sb->s_blocksize_bits;
1558 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001560 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001562 /* Process the main & reserve sequences */
1563 /* responsible for finding the PartitionDesc(s) */
1564 if (!(udf_process_sequence(sb, main_s, main_e,
1565 fileset) &&
1566 udf_process_sequence(sb, reserve_s, reserve_e,
1567 fileset)))
1568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001571 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 udf_debug("No Anchor block found\n");
1573 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001574 }
1575 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return 0;
1578}
1579
1580static void udf_open_lvid(struct super_block *sb)
1581{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001582 struct udf_sb_info *sbi = UDF_SB(sb);
1583 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001584 struct logicalVolIntegrityDesc *lvid;
1585 struct logicalVolIntegrityDescImpUse *lvidiu;
1586 if (!bh)
1587 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Marcin Slusarz165923f2008-02-10 11:33:08 +01001589 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1590 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Marcin Slusarz165923f2008-02-10 11:33:08 +01001592 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1593 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1594 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1595 CURRENT_TIME);
1596 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Marcin Slusarz165923f2008-02-10 11:33:08 +01001598 lvid->descTag.descCRC = cpu_to_le16(
1599 udf_crc((char *)lvid + sizeof(tag),
1600 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1601
1602 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1603 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606static void udf_close_lvid(struct super_block *sb)
1607{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001608 struct udf_sb_info *sbi = UDF_SB(sb);
1609 struct buffer_head *bh = sbi->s_lvid_bh;
1610 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001611 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001612
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001613 if (!bh)
1614 return;
1615
1616 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1617
Marcin Slusarz165923f2008-02-10 11:33:08 +01001618 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Marcin Slusarz165923f2008-02-10 11:33:08 +01001621 lvidiu = udf_sb_lvidiu(sbi);
1622 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1623 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1624 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1625 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1626 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1627 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1628 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1629 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1630 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1631 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Marcin Slusarz165923f2008-02-10 11:33:08 +01001633 lvid->descTag.descCRC = cpu_to_le16(
1634 udf_crc((char *)lvid + sizeof(tag),
1635 le16_to_cpu(lvid->descTag.descCRCLength),
1636 0));
1637
1638 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1639 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001642static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1643{
1644 int i;
1645 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001646 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1647 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001648
1649 for (i = 0; i < nr_groups; i++)
1650 if (bitmap->s_block_bitmap[i])
1651 brelse(bitmap->s_block_bitmap[i]);
1652
1653 if (size <= PAGE_SIZE)
1654 kfree(bitmap);
1655 else
1656 vfree(bitmap);
1657}
1658
Jan Kara2e0838f2008-04-01 18:08:51 +02001659static void udf_free_partition(struct udf_part_map *map)
1660{
1661 int i;
1662
1663 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1664 iput(map->s_uspace.s_table);
1665 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1666 iput(map->s_fspace.s_table);
1667 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1668 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1669 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1670 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1671 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1672 for (i = 0; i < 4; i++)
1673 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1674}
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676static int udf_fill_super(struct super_block *sb, void *options, int silent)
1677{
1678 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001679 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct udf_options uopt;
1681 kernel_lb_addr rootdir, fileset;
1682 struct udf_sb_info *sbi;
1683
1684 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1685 uopt.uid = -1;
1686 uopt.gid = -1;
1687 uopt.umask = 0;
1688
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001689 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (!sbi)
1691 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001695 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Miklos Szeredi6da80892008-02-08 04:21:50 -08001697 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto error_out;
1699
1700 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001701 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto error_out;
1705 }
1706#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 uopt.nls_map = load_nls_default();
1709 if (!uopt.nls_map)
1710 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1711 else
1712 udf_debug("Using default NLS map\n");
1713 }
1714#endif
1715 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1716 uopt.flags |= (1 << UDF_FLAG_UTF8);
1717
1718 fileset.logicalBlockNum = 0xFFFFFFFF;
1719 fileset.partitionReferenceNum = 0xFFFF;
1720
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001721 sbi->s_flags = uopt.flags;
1722 sbi->s_uid = uopt.uid;
1723 sbi->s_gid = uopt.gid;
1724 sbi->s_umask = uopt.umask;
1725 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001728 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1729 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1730 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001734 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001735 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001737 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001739 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001741 sbi->s_last_block = uopt.lastblock;
1742 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1743 sbi->s_anchor[2] = uopt.anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001745 if (udf_check_valid(sb, uopt.novrs, silent)) {
1746 /* read volume recognition sequences */
1747 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001748 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750
1751 udf_find_anchor(sb);
1752
1753 /* Fill in the rest of the superblock */
1754 sb->s_op = &udf_sb_ops;
1755 sb->dq_op = NULL;
1756 sb->s_dirt = 0;
1757 sb->s_magic = UDF_SUPER_MAGIC;
1758 sb->s_time_gran = 1000;
1759
Jan Kara38b74a52008-04-02 16:01:35 +02001760 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001761 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto error_out;
1763 }
1764
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001765 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001767 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001768 struct logicalVolIntegrityDescImpUse *lvidiu =
1769 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001770 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1771 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001772 /* uint16_t maxUDFWriteRev =
1773 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001775 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001776 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1777 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001778 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001779 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001781 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001784 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1787 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1788 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1789 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1790 }
1791
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001792 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001793 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto error_out;
1795 }
1796
Marcin Slusarz4b111112008-02-08 04:20:36 -08001797 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1798 UDF_PART_FLAG_READ_ONLY) {
1799 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1800 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001801 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001802 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001803
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001804 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001805 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto error_out;
1807 }
1808
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001809 if (!silent) {
Marcin Slusarz56774802008-02-10 11:25:31 +01001810 timestamp ts;
1811 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001812 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001813 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001814 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1815 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817 if (!(sb->s_flags & MS_RDONLY))
1818 udf_open_lvid(sb);
1819
1820 /* Assign the root inode */
1821 /* assign inodes by physical block number */
1822 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001823 inode = udf_iget(sb, rootdir);
1824 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001825 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1826 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001827 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 goto error_out;
1829 }
1830
1831 /* Allocate a dentry for the root inode */
1832 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001833 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001834 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 iput(inode);
1836 goto error_out;
1837 }
Jan Kara31170b62007-05-08 00:35:21 -07001838 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
1840
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001841error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001842 if (sbi->s_vat_inode)
1843 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001844 if (sbi->s_partitions)
1845 for (i = 0; i < sbi->s_partitions; i++)
1846 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847#ifdef CONFIG_UDF_NLS
1848 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001849 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850#endif
1851 if (!(sb->s_flags & MS_RDONLY))
1852 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001853 brelse(sbi->s_lvid_bh);
1854
1855 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 kfree(sbi);
1857 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return -EINVAL;
1860}
1861
Adrian Bunkb8145a72008-02-17 10:19:55 +02001862static void udf_error(struct super_block *sb, const char *function,
1863 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 va_list args;
1866
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001867 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /* mark sb error */
1869 sb->s_dirt = 1;
1870 }
1871 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001872 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001874 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001875 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
1878void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001879 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
1881 va_list args;
1882
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001883 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001884 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 va_end(args);
1886 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001887 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001890static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001893 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001895 sbi = UDF_SB(sb);
1896 if (sbi->s_vat_inode)
1897 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001898 if (sbi->s_partitions)
1899 for (i = 0; i < sbi->s_partitions; i++)
1900 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901#ifdef CONFIG_UDF_NLS
1902 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001903 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904#endif
1905 if (!(sb->s_flags & MS_RDONLY))
1906 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001907 brelse(sbi->s_lvid_bh);
1908 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 kfree(sb->s_fs_info);
1910 sb->s_fs_info = NULL;
1911}
1912
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
David Howells726c3342006-06-23 02:02:58 -07001915 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001916 struct udf_sb_info *sbi = UDF_SB(sb);
1917 struct logicalVolIntegrityDescImpUse *lvidiu;
1918
1919 if (sbi->s_lvid_bh != NULL)
1920 lvidiu = udf_sb_lvidiu(sbi);
1921 else
1922 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 buf->f_type = UDF_SUPER_MAGIC;
1925 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001926 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 buf->f_bfree = udf_count_free(sb);
1928 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001929 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1930 le32_to_cpu(lvidiu->numDirs)) : 0)
1931 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 buf->f_ffree = buf->f_bfree;
1933 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001934 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 return 0;
1937}
1938
Marcin Slusarz4b111112008-02-08 04:20:36 -08001939static unsigned int udf_count_free_bitmap(struct super_block *sb,
1940 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 struct buffer_head *bh = NULL;
1943 unsigned int accum = 0;
1944 int index;
1945 int block = 0, newblock;
1946 kernel_lb_addr loc;
1947 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 uint8_t *ptr;
1949 uint16_t ident;
1950 struct spaceBitmapDesc *bm;
1951
1952 lock_kernel();
1953
1954 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001955 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 bh = udf_read_ptagged(sb, loc, 0, &ident);
1957
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001958 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 printk(KERN_ERR "udf: udf_count_free failed\n");
1960 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001961 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001962 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 printk(KERN_ERR "udf: udf_count_free failed\n");
1964 goto out;
1965 }
1966
1967 bm = (struct spaceBitmapDesc *)bh->b_data;
1968 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001969 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1970 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001972 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01001973 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
1974 accum += bitmap_weight((const unsigned long *)(ptr + index),
1975 cur_bytes * 8);
1976 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001977 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001978 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 newblock = udf_get_lb_pblock(sb, loc, ++block);
1980 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001981 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 udf_debug("read failed\n");
1983 goto out;
1984 }
1985 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001986 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001989 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001991out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 unlock_kernel();
1993
1994 return accum;
1995}
1996
Marcin Slusarz4b111112008-02-08 04:20:36 -08001997static unsigned int udf_count_free_table(struct super_block *sb,
1998 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002001 uint32_t elen;
2002 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002004 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 lock_kernel();
2007
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002008 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002009 epos.offset = sizeof(struct unallocSpaceEntry);
2010 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002012 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002014
Jan Kara3bf25cb2007-05-08 00:35:16 -07002015 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 unlock_kernel();
2018
2019 return accum;
2020}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021
2022static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002025 struct udf_sb_info *sbi;
2026 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002028 sbi = UDF_SB(sb);
2029 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002030 struct logicalVolIntegrityDesc *lvid =
2031 (struct logicalVolIntegrityDesc *)
2032 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002033 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002034 accum = le32_to_cpu(
2035 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (accum == 0xFFFFFFFF)
2037 accum = 0;
2038 }
2039 }
2040
2041 if (accum)
2042 return accum;
2043
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002044 map = &sbi->s_partmaps[sbi->s_partition];
2045 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002046 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002047 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002049 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002050 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002051 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 if (accum)
2054 return accum;
2055
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002056 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002057 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002058 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002060 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002061 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002062 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064
2065 return accum;
2066}