blob: b9dc6adfdd2d3e5cc9e307bc73258bb6109930d9 [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>
Bob Copelandf845fce2008-04-17 09:47:48 +020059#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/byteorder.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "udf_sb.h"
63#include "udf_i.h"
64
65#include <linux/init.h>
66#include <asm/uaccess.h>
67
68#define VDS_POS_PRIMARY_VOL_DESC 0
69#define VDS_POS_UNALLOC_SPACE_DESC 1
70#define VDS_POS_LOGICAL_VOL_DESC 2
71#define VDS_POS_PARTITION_DESC 3
72#define VDS_POS_IMP_USE_VOL_DESC 4
73#define VDS_POS_VOL_DESC_PTR 5
74#define VDS_POS_TERMINATING_DESC 6
75#define VDS_POS_LENGTH 7
76
Miklos Szeredi6da80892008-02-08 04:21:50 -080077#define UDF_DEFAULT_BLOCKSIZE 2048
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static char error_buf[1024];
80
81/* These are the "meat" - everything else is stuffing */
82static int udf_fill_super(struct super_block *, void *, int);
83static void udf_put_super(struct super_block *);
84static void udf_write_super(struct super_block *);
85static int udf_remount_fs(struct super_block *, int *, char *);
86static int udf_check_valid(struct super_block *, int, int);
87static int udf_vrs(struct super_block *sb, int silent);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020088static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void udf_find_anchor(struct super_block *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020090static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
91 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020093 struct kernel_lb_addr *);
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 *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080098static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020099static void udf_error(struct super_block *sb, const char *function,
100 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800102struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
103{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800104 struct logicalVolIntegrityDesc *lvid =
105 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 __u32 offset = number_of_partitions * 2 *
108 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700113static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 int flags, const char *dev_name, void *data,
115 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David Howells454e2392006-06-23 02:02:57 -0700117 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700121 .owner = THIS_MODULE,
122 .name = "udf",
123 .get_sb = udf_get_sb,
124 .kill_sb = kill_block_super,
125 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static struct inode *udf_alloc_inode(struct super_block *sb)
131{
132 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800133 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!ei)
135 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700136
137 ei->i_unique = 0;
138 ei->i_lenExtents = 0;
139 ei->i_next_alloc_block = 0;
140 ei->i_next_alloc_goal = 0;
141 ei->i_strat4096 = 0;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return &ei->vfs_inode;
144}
145
146static void udf_destroy_inode(struct inode *inode)
147{
148 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
149}
150
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700151static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700153 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Lametera35afb82007-05-16 22:10:57 -0700155 ei->i_ext.i_data = NULL;
156 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static int init_inodecache(void)
160{
161 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
162 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 0, (SLAB_RECLAIM_ACCOUNT |
164 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900165 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700166 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -ENOMEM;
168 return 0;
169}
170
171static void destroy_inodecache(void)
172{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700173 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800177static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700178 .alloc_inode = udf_alloc_inode,
179 .destroy_inode = udf_destroy_inode,
180 .write_inode = udf_write_inode,
181 .delete_inode = udf_delete_inode,
182 .clear_inode = udf_clear_inode,
183 .put_super = udf_put_super,
184 .write_super = udf_write_super,
185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800187 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned char novrs;
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
195 unsigned int anchor;
196 unsigned int volume;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
200 unsigned int flags;
201 mode_t umask;
202 gid_t gid;
203 uid_t uid;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100204 mode_t fmode;
205 mode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 struct nls_table *nls_map;
207};
208
209static int __init init_udf_fs(void)
210{
211 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = init_inodecache();
214 if (err)
215 goto out1;
216 err = register_filesystem(&udf_fstype);
217 if (err)
218 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700224
225out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return err;
227}
228
229static void __exit exit_udf_fs(void)
230{
231 unregister_filesystem(&udf_fstype);
232 destroy_inodecache();
233}
234
235module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800238static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
239{
240 struct udf_sb_info *sbi = UDF_SB(sb);
241
242 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
243 GFP_KERNEL);
244 if (!sbi->s_partmaps) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700245 udf_error(sb, __func__,
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800246 "Unable to allocate space for %d partition maps",
247 count);
248 sbi->s_partitions = 0;
249 return -ENOMEM;
250 }
251
252 sbi->s_partitions = count;
253 return 0;
254}
255
Miklos Szeredi6da80892008-02-08 04:21:50 -0800256static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
257{
258 struct super_block *sb = mnt->mnt_sb;
259 struct udf_sb_info *sbi = UDF_SB(sb);
260
261 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
262 seq_puts(seq, ",nostrict");
263 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
264 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
266 seq_puts(seq, ",unhide");
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
268 seq_puts(seq, ",undelete");
269 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
270 seq_puts(seq, ",noadinicb");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
272 seq_puts(seq, ",shortad");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
274 seq_puts(seq, ",uid=forget");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
276 seq_puts(seq, ",uid=ignore");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
278 seq_puts(seq, ",gid=forget");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
280 seq_puts(seq, ",gid=ignore");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
282 seq_printf(seq, ",uid=%u", sbi->s_uid);
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
284 seq_printf(seq, ",gid=%u", sbi->s_gid);
285 if (sbi->s_umask != 0)
286 seq_printf(seq, ",umask=%o", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100287 if (sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100288 seq_printf(seq, ",mode=%o", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100289 if (sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100290 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
292 seq_printf(seq, ",session=%u", sbi->s_session);
293 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
294 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
295 /*
296 * s_anchor[2] could be zeroed out in case there is no anchor
297 * in the specified block, but then the "anchor=N" option
298 * originally given by the user wasn't effective, so it's OK
299 * if we don't show it.
300 */
301 if (sbi->s_anchor[2] != 0)
302 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
303 /*
304 * volume, partition, fileset and rootdir seem to be ignored
305 * currently
306 */
307 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
308 seq_puts(seq, ",utf8");
309 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
310 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
311
312 return 0;
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
316 * udf_parse_options
317 *
318 * PURPOSE
319 * Parse mount options.
320 *
321 * DESCRIPTION
322 * The following mount options are supported:
323 *
324 * gid= Set the default group.
325 * umask= Set the default umask.
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100326 * mode= Set the default file permissions.
327 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * uid= Set the default user.
329 * bs= Set the block size.
330 * unhide Show otherwise hidden files.
331 * undelete Show deleted files in lists.
332 * adinicb Embed data in the inode (default)
333 * noadinicb Don't embed data in the inode
334 * shortad Use short ad's
335 * longad Use long ad's (default)
336 * nostrict Unset strict conformance
337 * iocharset= Set the NLS character set
338 *
339 * The remaining are for debugging and disaster recovery:
340 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700341 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
343 * The following expect a offset from 0.
344 *
345 * session= Set the CDROM session (default= last session)
346 * anchor= Override standard anchor location. (default= 256)
347 * volume= Override the VolumeDesc location. (unused)
348 * partition= Override the PartitionDesc location. (unused)
349 * lastblock= Set the last block of the filesystem/
350 *
351 * The following expect a offset from the partition root.
352 *
353 * fileset= Override the fileset block location. (unused)
354 * rootdir= Override the root directory location. (unused)
355 * WARNING: overriding the rootdir to a non-directory may
356 * yield highly unpredictable results.
357 *
358 * PRE-CONDITIONS
359 * options Pointer to mount options string.
360 * uopts Pointer to mount options variable.
361 *
362 * POST-CONDITIONS
363 * <return> 1 Mount options parsed okay.
364 * <return> 0 Error parsing mount options.
365 *
366 * HISTORY
367 * July 1, 1997 - Andrew E. Mileski
368 * Written, tested, and released.
369 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371enum {
372 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
373 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
374 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
375 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
376 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100377 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
378 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
Steven Whitehousea447c092008-10-13 10:46:57 +0100381static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700382 {Opt_novrs, "novrs"},
383 {Opt_nostrict, "nostrict"},
384 {Opt_bs, "bs=%u"},
385 {Opt_unhide, "unhide"},
386 {Opt_undelete, "undelete"},
387 {Opt_noadinicb, "noadinicb"},
388 {Opt_adinicb, "adinicb"},
389 {Opt_shortad, "shortad"},
390 {Opt_longad, "longad"},
391 {Opt_uforget, "uid=forget"},
392 {Opt_uignore, "uid=ignore"},
393 {Opt_gforget, "gid=forget"},
394 {Opt_gignore, "gid=ignore"},
395 {Opt_gid, "gid=%u"},
396 {Opt_uid, "uid=%u"},
397 {Opt_umask, "umask=%o"},
398 {Opt_session, "session=%u"},
399 {Opt_lastblock, "lastblock=%u"},
400 {Opt_anchor, "anchor=%u"},
401 {Opt_volume, "volume=%u"},
402 {Opt_partition, "partition=%u"},
403 {Opt_fileset, "fileset=%u"},
404 {Opt_rootdir, "rootdir=%u"},
405 {Opt_utf8, "utf8"},
406 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100407 {Opt_fmode, "mode=%o"},
408 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700409 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410};
411
Miklos Szeredi6da80892008-02-08 04:21:50 -0800412static int udf_parse_options(char *options, struct udf_options *uopt,
413 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 char *p;
416 int option;
417
418 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800419 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 uopt->partition = 0xFFFF;
421 uopt->session = 0xFFFFFFFF;
422 uopt->lastblock = 0;
423 uopt->anchor = 0;
424 uopt->volume = 0xFFFFFFFF;
425 uopt->rootdir = 0xFFFFFFFF;
426 uopt->fileset = 0xFFFFFFFF;
427 uopt->nls_map = NULL;
428
429 if (!options)
430 return 1;
431
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 substring_t args[MAX_OPT_ARGS];
434 int token;
435 if (!*p)
436 continue;
437
438 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 switch (token) {
440 case Opt_novrs:
441 uopt->novrs = 1;
442 case Opt_bs:
443 if (match_int(&args[0], &option))
444 return 0;
445 uopt->blocksize = option;
446 break;
447 case Opt_unhide:
448 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
449 break;
450 case Opt_undelete:
451 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
452 break;
453 case Opt_noadinicb:
454 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
455 break;
456 case Opt_adinicb:
457 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
458 break;
459 case Opt_shortad:
460 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
461 break;
462 case Opt_longad:
463 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
464 break;
465 case Opt_gid:
466 if (match_int(args, &option))
467 return 0;
468 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700469 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470 break;
471 case Opt_uid:
472 if (match_int(args, &option))
473 return 0;
474 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700475 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700476 break;
477 case Opt_umask:
478 if (match_octal(args, &option))
479 return 0;
480 uopt->umask = option;
481 break;
482 case Opt_nostrict:
483 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
484 break;
485 case Opt_session:
486 if (match_int(args, &option))
487 return 0;
488 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800489 if (!remount)
490 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491 break;
492 case Opt_lastblock:
493 if (match_int(args, &option))
494 return 0;
495 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800496 if (!remount)
497 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700498 break;
499 case Opt_anchor:
500 if (match_int(args, &option))
501 return 0;
502 uopt->anchor = option;
503 break;
504 case Opt_volume:
505 if (match_int(args, &option))
506 return 0;
507 uopt->volume = option;
508 break;
509 case Opt_partition:
510 if (match_int(args, &option))
511 return 0;
512 uopt->partition = option;
513 break;
514 case Opt_fileset:
515 if (match_int(args, &option))
516 return 0;
517 uopt->fileset = option;
518 break;
519 case Opt_rootdir:
520 if (match_int(args, &option))
521 return 0;
522 uopt->rootdir = option;
523 break;
524 case Opt_utf8:
525 uopt->flags |= (1 << UDF_FLAG_UTF8);
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700528 case Opt_iocharset:
529 uopt->nls_map = load_nls(args[0].from);
530 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700533 case Opt_uignore:
534 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
535 break;
536 case Opt_uforget:
537 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
538 break;
539 case Opt_gignore:
540 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
541 break;
542 case Opt_gforget:
543 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
544 break;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100545 case Opt_fmode:
546 if (match_octal(args, &option))
547 return 0;
548 uopt->fmode = option & 0777;
549 break;
550 case Opt_dmode:
551 if (match_octal(args, &option))
552 return 0;
553 uopt->dmode = option & 0777;
554 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 default:
556 printk(KERN_ERR "udf: bad mount option \"%s\" "
557 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559 }
560 }
561 return 1;
562}
563
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800564static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!(sb->s_flags & MS_RDONLY))
569 udf_open_lvid(sb);
570 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unlock_kernel();
573}
574
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800578 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800580 uopt.flags = sbi->s_flags;
581 uopt.uid = sbi->s_uid;
582 uopt.gid = sbi->s_gid;
583 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100584 uopt.fmode = sbi->s_fmode;
585 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Miklos Szeredi6da80892008-02-08 04:21:50 -0800587 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EINVAL;
589
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800590 sbi->s_flags = uopt.flags;
591 sbi->s_uid = uopt.uid;
592 sbi->s_gid = uopt.gid;
593 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100594 sbi->s_fmode = uopt.fmode;
595 sbi->s_dmode = uopt.dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800597 if (sbi->s_lvid_bh) {
598 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (write_rev > UDF_MAX_WRITE_VERSION)
600 *flags |= MS_RDONLY;
601 }
602
603 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
604 return 0;
605 if (*flags & MS_RDONLY)
606 udf_close_lvid(sb);
607 else
608 udf_open_lvid(sb);
609
610 return 0;
611}
612
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200616 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int sectorsize;
618 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700619 int iso9660 = 0;
620 int nsr02 = 0;
621 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800622 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Block size must be a multiple of 512 */
625 if (sb->s_blocksize & 511)
626 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800627 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (sb->s_blocksize < sizeof(struct volStructDesc))
630 sectorsize = sizeof(struct volStructDesc);
631 else
632 sectorsize = sb->s_blocksize;
633
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800634 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200637 (unsigned int)(sector >> sb->s_blocksize_bits),
638 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* Read a block */
642 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
643 if (!bh)
644 break;
645
646 /* Look for ISO descriptors */
647 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800648 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700651 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800653 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
654 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700656 switch (vsd->structType) {
657 case 0:
658 udf_debug("ISO9660 Boot Record found\n");
659 break;
660 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 udf_debug("ISO9660 Primary Volume Descriptor "
662 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 break;
664 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 udf_debug("ISO9660 Supplementary Volume "
666 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700667 break;
668 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800669 udf_debug("ISO9660 Volume Partition Descriptor "
670 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700671 break;
672 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800673 udf_debug("ISO9660 Volume Descriptor Set "
674 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700675 break;
676 default:
677 udf_debug("ISO9660 VRS (%u) found\n",
678 vsd->structType);
679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800681 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
682 VSD_STD_ID_LEN))
683 ; /* nothing */
684 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
685 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700686 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800688 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
689 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800691 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
692 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700694 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 if (nsr03)
698 return nsr03;
699 else if (nsr02)
700 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800701 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -1;
703 else
704 return 0;
705}
706
707/*
Jan Kara423cf6d2008-04-07 15:59:23 +0200708 * Check whether there is an anchor block in the given block
709 */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200710static int udf_check_anchor_block(struct super_block *sb, sector_t block)
Jan Kara423cf6d2008-04-07 15:59:23 +0200711{
Tomas Janouseke8183c22008-06-23 15:12:35 +0200712 struct buffer_head *bh;
Jan Kara423cf6d2008-04-07 15:59:23 +0200713 uint16_t ident;
Jan Kara423cf6d2008-04-07 15:59:23 +0200714
Tomas Janouseke8183c22008-06-23 15:12:35 +0200715 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
716 udf_fixed_to_variable(block) >=
717 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
718 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200719
Tomas Janouseke8183c22008-06-23 15:12:35 +0200720 bh = udf_read_tagged(sb, block, block, &ident);
Jan Kara423cf6d2008-04-07 15:59:23 +0200721 if (!bh)
722 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200723 brelse(bh);
Tomas Janouseke8183c22008-06-23 15:12:35 +0200724
725 return ident == TAG_IDENT_AVDP;
Jan Kara423cf6d2008-04-07 15:59:23 +0200726}
727
728/* Search for an anchor volume descriptor pointer */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200729static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200730{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200731 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200732 int i;
733 struct udf_sb_info *sbi = UDF_SB(sb);
734
735 last[0] = lastblock;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200736 last[1] = last[0] - 1;
737 last[2] = last[0] + 1;
738 last[3] = last[0] - 2;
739 last[4] = last[0] - 150;
740 last[5] = last[0] - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200741
742 /* according to spec, anchor is in either:
743 * block 256
744 * lastblock-256
745 * lastblock
746 * however, if the disc isn't closed, it could be 512 */
747
748 for (i = 0; i < ARRAY_SIZE(last); i++) {
Jan Kara5fb28aa2008-04-07 16:15:04 +0200749 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
750 sb->s_blocksize_bits)
751 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200752
Tomas Janouseke8183c22008-06-23 15:12:35 +0200753 if (udf_check_anchor_block(sb, last[i])) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200754 sbi->s_anchor[0] = last[i];
755 sbi->s_anchor[1] = last[i] - 256;
756 return last[i];
757 }
758
759 if (last[i] < 256)
760 continue;
761
Tomas Janouseke8183c22008-06-23 15:12:35 +0200762 if (udf_check_anchor_block(sb, last[i] - 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200763 sbi->s_anchor[1] = last[i] - 256;
764 return last[i];
765 }
766 }
767
Tomas Janouseke8183c22008-06-23 15:12:35 +0200768 if (udf_check_anchor_block(sb, sbi->s_session + 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200769 sbi->s_anchor[0] = sbi->s_session + 256;
770 return last[0];
771 }
Tomas Janouseke8183c22008-06-23 15:12:35 +0200772 if (udf_check_anchor_block(sb, sbi->s_session + 512)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200773 sbi->s_anchor[0] = sbi->s_session + 512;
774 return last[0];
775 }
776 return 0;
777}
778
779/*
780 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
781 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200783 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Jan Kara423cf6d2008-04-07 15:59:23 +0200788 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct buffer_head *bh = NULL;
790 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 int i;
Jan Kara423cf6d2008-04-07 15:59:23 +0200792 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800793
Tomas Janouseke8183c22008-06-23 15:12:35 +0200794 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
Jan Kara423cf6d2008-04-07 15:59:23 +0200795 if (lastblock)
796 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jan Kara423cf6d2008-04-07 15:59:23 +0200798 /* No anchor found? Try VARCONV conversion of block numbers */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200799 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara423cf6d2008-04-07 15:59:23 +0200800 /* Firstly, we try to not convert number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200801 lastblock = udf_scan_anchors(sb,
Jan Kara423cf6d2008-04-07 15:59:23 +0200802 udf_variable_to_fixed(sbi->s_last_block));
Tomas Janouseke8183c22008-06-23 15:12:35 +0200803 if (lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200804 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Jan Kara423cf6d2008-04-07 15:59:23 +0200806 /* Secondly, we try with converted number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200807 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
808 if (!lastblock) {
809 /* VARCONV didn't help. Clear it. */
810 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Jan Kara423cf6d2008-04-07 15:59:23 +0200813check_anchor:
814 /*
815 * Check located anchors and the anchor block supplied via
816 * mount options
817 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800818 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100819 if (!sbi->s_anchor[i])
820 continue;
821 bh = udf_read_tagged(sb, sbi->s_anchor[i],
822 sbi->s_anchor[i], &ident);
823 if (!bh)
824 sbi->s_anchor[i] = 0;
825 else {
826 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200827 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800828 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
831
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800832 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800835static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200836 struct kernel_lb_addr *fileset,
837 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct buffer_head *bh = NULL;
840 long lastblock;
841 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700845 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200846 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700848 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700850 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700851 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 1;
853 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800857 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800858 if (!bh) {
859 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200860 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700862/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700864
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800865 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 (newfileset.partitionReferenceNum != 0xFFFF &&
867 fileset->logicalBlockNum == 0xFFFFFFFF &&
868 fileset->partitionReferenceNum == 0xFFFF);
869 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800870 lastblock = sbi->s_partmaps
871 [newfileset.partitionReferenceNum]
872 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 newfileset.logicalBlockNum = 0;
874
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200876 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800877 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700878 if (!bh) {
879 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 continue;
881 }
882
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700883 switch (ident) {
884 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700885 {
886 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800887 sp = (struct spaceBitmapDesc *)
888 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700889 newfileset.logicalBlockNum += 1 +
890 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800891 sizeof(struct spaceBitmapDesc)
892 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700893 brelse(bh);
894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700896 case TAG_IDENT_FSD:
897 *fileset = newfileset;
898 break;
899 default:
900 newfileset.logicalBlockNum++;
901 brelse(bh);
902 bh = NULL;
903 break;
904 }
905 } while (newfileset.logicalBlockNum < lastblock &&
906 fileset->logicalBlockNum == 0xFFFFFFFF &&
907 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
909 }
910
911 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700912 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700914 fileset->logicalBlockNum,
915 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800917 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700919 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
922 return 1;
923}
924
Jan Karac0eb31e2008-04-01 16:50:35 +0200925static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
927 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100928 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200929 struct buffer_head *bh;
930 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100931 int ret = 1;
932
933 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
934 if (!instr)
935 return 1;
936
937 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
938 if (!outstr)
939 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200940
941 bh = udf_read_tagged(sb, block, block, &ident);
942 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100943 goto out2;
944
Jan Karac0eb31e2008-04-01 16:50:35 +0200945 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 pvoldesc = (struct primaryVolDesc *)bh->b_data;
948
Marcin Slusarz56774802008-02-10 11:25:31 +0100949 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
950 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100951#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200952 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100953 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800954 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100955 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
956 ts->minute, le16_to_cpu(ts->typeAndTimezone));
957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100960 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
961 if (udf_CS0toUTF8(outstr, instr)) {
962 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
963 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800964 udf_debug("volIdent[] = '%s'\n",
965 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100968 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
969 if (udf_CS0toUTF8(outstr, instr))
970 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200971
972 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100973 ret = 0;
974out2:
975 kfree(outstr);
976out1:
977 kfree(instr);
978 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Jan Karabfb257a2008-04-08 20:37:21 +0200981static int udf_load_metadata_files(struct super_block *sb, int partition)
982{
983 struct udf_sb_info *sbi = UDF_SB(sb);
984 struct udf_part_map *map;
985 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200986 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200987 int fe_error = 0;
988
989 map = &sbi->s_partmaps[partition];
990 mdata = &map->s_type_specific.s_metadata;
991
992 /* metadata address */
993 addr.logicalBlockNum = mdata->s_meta_file_loc;
994 addr.partitionReferenceNum = map->s_partition_num;
995
996 udf_debug("Metadata file location: block = %d part = %d\n",
997 addr.logicalBlockNum, addr.partitionReferenceNum);
998
Pekka Enberg97e961f2008-10-15 12:29:03 +0200999 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001000
1001 if (mdata->s_metadata_fe == NULL) {
1002 udf_warning(sb, __func__, "metadata inode efe not found, "
1003 "will try mirror inode.");
1004 fe_error = 1;
1005 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
1006 ICBTAG_FLAG_AD_SHORT) {
1007 udf_warning(sb, __func__, "metadata inode efe does not have "
1008 "short allocation descriptors!");
1009 fe_error = 1;
1010 iput(mdata->s_metadata_fe);
1011 mdata->s_metadata_fe = NULL;
1012 }
1013
1014 /* mirror file entry */
1015 addr.logicalBlockNum = mdata->s_mirror_file_loc;
1016 addr.partitionReferenceNum = map->s_partition_num;
1017
1018 udf_debug("Mirror metadata file location: block = %d part = %d\n",
1019 addr.logicalBlockNum, addr.partitionReferenceNum);
1020
Pekka Enberg97e961f2008-10-15 12:29:03 +02001021 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001022
1023 if (mdata->s_mirror_fe == NULL) {
1024 if (fe_error) {
1025 udf_error(sb, __func__, "mirror inode efe not found "
1026 "and metadata inode is missing too, exiting...");
1027 goto error_exit;
1028 } else
1029 udf_warning(sb, __func__, "mirror inode efe not found,"
1030 " but metadata inode is OK");
1031 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1032 ICBTAG_FLAG_AD_SHORT) {
1033 udf_warning(sb, __func__, "mirror inode efe does not have "
1034 "short allocation descriptors!");
1035 iput(mdata->s_mirror_fe);
1036 mdata->s_mirror_fe = NULL;
1037 if (fe_error)
1038 goto error_exit;
1039 }
1040
1041 /*
1042 * bitmap file entry
1043 * Note:
1044 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1045 */
1046 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1047 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1048 addr.partitionReferenceNum = map->s_partition_num;
1049
1050 udf_debug("Bitmap file location: block = %d part = %d\n",
1051 addr.logicalBlockNum, addr.partitionReferenceNum);
1052
Pekka Enberg97e961f2008-10-15 12:29:03 +02001053 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001054
1055 if (mdata->s_bitmap_fe == NULL) {
1056 if (sb->s_flags & MS_RDONLY)
1057 udf_warning(sb, __func__, "bitmap inode efe "
1058 "not found but it's ok since the disc"
1059 " is mounted read-only");
1060 else {
1061 udf_error(sb, __func__, "bitmap inode efe not "
1062 "found and attempted read-write mount");
1063 goto error_exit;
1064 }
1065 }
1066 }
1067
1068 udf_debug("udf_load_metadata_files Ok\n");
1069
1070 return 0;
1071
1072error_exit:
1073 return 1;
1074}
1075
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001076static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001077 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct fileSetDesc *fset;
1080
1081 fset = (struct fileSetDesc *)bh->b_data;
1082
1083 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1084
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001085 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087 udf_debug("Rootdir at block=%d, partition=%d\n",
1088 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001091int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1092{
1093 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001094 return DIV_ROUND_UP(map->s_partition_len +
1095 (sizeof(struct spaceBitmapDesc) << 3),
1096 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001097}
1098
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001099static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1100{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001101 struct udf_bitmap *bitmap;
1102 int nr_groups;
1103 int size;
1104
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001105 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001106 size = sizeof(struct udf_bitmap) +
1107 (sizeof(struct buffer_head *) * nr_groups);
1108
1109 if (size <= PAGE_SIZE)
1110 bitmap = kmalloc(size, GFP_KERNEL);
1111 else
1112 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1113
1114 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001115 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001116 "Unable to allocate space for bitmap "
1117 "and %d buffer_head pointers", nr_groups);
1118 return NULL;
1119 }
1120
1121 memset(bitmap, 0x00, size);
1122 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1123 bitmap->s_nr_groups = nr_groups;
1124 return bitmap;
1125}
1126
Jan Kara3fb38df2008-04-02 12:26:36 +02001127static int udf_fill_partdesc_info(struct super_block *sb,
1128 struct partitionDesc *p, int p_index)
1129{
1130 struct udf_part_map *map;
1131 struct udf_sb_info *sbi = UDF_SB(sb);
1132 struct partitionHeaderDesc *phd;
1133
1134 map = &sbi->s_partmaps[p_index];
1135
1136 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1137 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1138
1139 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1140 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1141 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1142 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1143 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1144 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1145 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1146 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1147
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001148 udf_debug("Partition (%d type %x) starts at physical %d, "
1149 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001150 map->s_partition_type, map->s_partition_root,
1151 map->s_partition_len);
1152
1153 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1154 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1155 return 0;
1156
1157 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1158 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001159 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001160 .logicalBlockNum = le32_to_cpu(
1161 phd->unallocSpaceTable.extPosition),
1162 .partitionReferenceNum = p_index,
1163 };
1164
Pekka Enberg97e961f2008-10-15 12:29:03 +02001165 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001166 if (!map->s_uspace.s_table) {
1167 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1168 p_index);
1169 return 1;
1170 }
1171 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1172 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1173 p_index, map->s_uspace.s_table->i_ino);
1174 }
1175
1176 if (phd->unallocSpaceBitmap.extLength) {
1177 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1178 if (!bitmap)
1179 return 1;
1180 map->s_uspace.s_bitmap = bitmap;
1181 bitmap->s_extLength = le32_to_cpu(
1182 phd->unallocSpaceBitmap.extLength);
1183 bitmap->s_extPosition = le32_to_cpu(
1184 phd->unallocSpaceBitmap.extPosition);
1185 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1186 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1187 bitmap->s_extPosition);
1188 }
1189
1190 if (phd->partitionIntegrityTable.extLength)
1191 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1192
1193 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001194 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001195 .logicalBlockNum = le32_to_cpu(
1196 phd->freedSpaceTable.extPosition),
1197 .partitionReferenceNum = p_index,
1198 };
1199
Pekka Enberg97e961f2008-10-15 12:29:03 +02001200 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001201 if (!map->s_fspace.s_table) {
1202 udf_debug("cannot load freedSpaceTable (part %d)\n",
1203 p_index);
1204 return 1;
1205 }
1206
1207 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1208 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1209 p_index, map->s_fspace.s_table->i_ino);
1210 }
1211
1212 if (phd->freedSpaceBitmap.extLength) {
1213 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1214 if (!bitmap)
1215 return 1;
1216 map->s_fspace.s_bitmap = bitmap;
1217 bitmap->s_extLength = le32_to_cpu(
1218 phd->freedSpaceBitmap.extLength);
1219 bitmap->s_extPosition = le32_to_cpu(
1220 phd->freedSpaceBitmap.extPosition);
1221 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1222 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1223 bitmap->s_extPosition);
1224 }
1225 return 0;
1226}
1227
Jan Kara38b74a52008-04-02 16:01:35 +02001228static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1229{
1230 struct udf_sb_info *sbi = UDF_SB(sb);
1231 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001232 struct kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001233 struct buffer_head *bh = NULL;
1234 struct udf_inode_info *vati;
1235 uint32_t pos;
1236 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001237
1238 /* VAT file entry is in the last recorded block */
1239 ino.partitionReferenceNum = type1_index;
1240 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
Pekka Enberg97e961f2008-10-15 12:29:03 +02001241 sbi->s_vat_inode = udf_iget(sb, &ino);
Jan Kara38b74a52008-04-02 16:01:35 +02001242 if (!sbi->s_vat_inode)
1243 return 1;
1244
1245 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001246 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001247 map->s_type_specific.s_virtual.s_num_entries =
1248 (sbi->s_vat_inode->i_size - 36) >> 2;
1249 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001250 vati = UDF_I(sbi->s_vat_inode);
1251 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1252 pos = udf_block_map(sbi->s_vat_inode, 0);
1253 bh = sb_bread(sb, pos);
1254 if (!bh)
1255 return 1;
1256 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1257 } else {
1258 vat20 = (struct virtualAllocationTable20 *)
1259 vati->i_ext.i_data;
1260 }
Jan Kara38b74a52008-04-02 16:01:35 +02001261
Jan Kara38b74a52008-04-02 16:01:35 +02001262 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001263 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001264 map->s_type_specific.s_virtual.s_num_entries =
1265 (sbi->s_vat_inode->i_size -
1266 map->s_type_specific.s_virtual.
1267 s_start_offset) >> 2;
1268 brelse(bh);
1269 }
1270 return 0;
1271}
1272
Jan Karac0eb31e2008-04-01 16:50:35 +02001273static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Jan Karac0eb31e2008-04-01 16:50:35 +02001275 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001276 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001277 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001278 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001279 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001280 uint16_t partitionNumber;
1281 uint16_t ident;
1282 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jan Karac0eb31e2008-04-01 16:50:35 +02001284 bh = udf_read_tagged(sb, block, block, &ident);
1285 if (!bh)
1286 return 1;
1287 if (ident != TAG_IDENT_PD)
1288 goto out_bh;
1289
1290 p = (struct partitionDesc *)bh->b_data;
1291 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001292
Jan Karabfb257a2008-04-08 20:37:21 +02001293 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001294 for (i = 0; i < sbi->s_partitions; i++) {
1295 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001296 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001297 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001298 if (map->s_partition_num == partitionNumber &&
1299 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1300 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001302 }
1303
Jan Kara38b74a52008-04-02 16:01:35 +02001304 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001305 udf_debug("Partition (%d) not found in partition map\n",
1306 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001307 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001308 }
1309
Jan Kara3fb38df2008-04-02 12:26:36 +02001310 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001311
1312 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001313 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1314 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001315 */
1316 type1_idx = i;
1317 for (i = 0; i < sbi->s_partitions; i++) {
1318 map = &sbi->s_partmaps[i];
1319
1320 if (map->s_partition_num == partitionNumber &&
1321 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001322 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1323 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001324 break;
1325 }
1326
1327 if (i >= sbi->s_partitions)
1328 goto out_bh;
1329
1330 ret = udf_fill_partdesc_info(sb, p, i);
1331 if (ret)
1332 goto out_bh;
1333
Jan Karabfb257a2008-04-08 20:37:21 +02001334 if (map->s_partition_type == UDF_METADATA_MAP25) {
1335 ret = udf_load_metadata_files(sb, i);
1336 if (ret) {
1337 printk(KERN_ERR "UDF-fs: error loading MetaData "
1338 "partition map %d\n", i);
1339 goto out_bh;
1340 }
1341 } else {
1342 ret = udf_load_vat(sb, i, type1_idx);
1343 if (ret)
1344 goto out_bh;
1345 /*
1346 * Mark filesystem read-only if we have a partition with
1347 * virtual map since we don't handle writing to it (we
1348 * overwrite blocks instead of relocating them).
1349 */
1350 sb->s_flags |= MS_RDONLY;
1351 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1352 "because writing to pseudooverwrite partition is "
1353 "not implemented.\n");
1354 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001355out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001356 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001357 brelse(bh);
1358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Jan Karac0eb31e2008-04-01 16:50:35 +02001361static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001362 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
1364 struct logicalVolDesc *lvd;
1365 int i, j, offset;
1366 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001367 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001368 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001369 uint16_t ident;
1370 struct buffer_head *bh;
1371 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Jan Karac0eb31e2008-04-01 16:50:35 +02001373 bh = udf_read_tagged(sb, block, block, &ident);
1374 if (!bh)
1375 return 1;
1376 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 lvd = (struct logicalVolDesc *)bh->b_data;
1378
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001379 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001380 if (i != 0) {
1381 ret = i;
1382 goto out_bh;
1383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001385 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001386 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001387 i++, offset += gpm->partitionMapLength) {
1388 struct udf_part_map *map = &sbi->s_partmaps[i];
1389 gpm = (struct genericPartitionMap *)
1390 &(lvd->partitionMaps[offset]);
1391 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001392 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001393 struct genericPartitionMap1 *gpm1 =
1394 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001395 map->s_partition_type = UDF_TYPE1_MAP15;
1396 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1397 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1398 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001399 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001400 struct udfPartitionMap2 *upm2 =
1401 (struct udfPartitionMap2 *)gpm;
1402 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1403 strlen(UDF_ID_VIRTUAL))) {
1404 u16 suf =
1405 le16_to_cpu(((__le16 *)upm2->partIdent.
1406 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001407 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001408 map->s_partition_type =
1409 UDF_VIRTUAL_MAP15;
1410 map->s_partition_func =
1411 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001412 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001413 map->s_partition_type =
1414 UDF_VIRTUAL_MAP20;
1415 map->s_partition_func =
1416 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001418 } else if (!strncmp(upm2->partIdent.ident,
1419 UDF_ID_SPARABLE,
1420 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001423 struct sparablePartitionMap *spm =
1424 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001426 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001427 map->s_type_specific.s_sparing.s_packet_len =
1428 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001429 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001430 struct buffer_head *bh2;
1431
1432 loc = le32_to_cpu(
1433 spm->locSparingTable[j]);
1434 bh2 = udf_read_tagged(sb, loc, loc,
1435 &ident);
1436 map->s_type_specific.s_sparing.
1437 s_spar_map[j] = bh2;
1438
Marcin Slusarz165923f2008-02-10 11:33:08 +01001439 if (bh2 == NULL)
1440 continue;
1441
1442 st = (struct sparingTable *)bh2->b_data;
1443 if (ident != 0 || strncmp(
1444 st->sparingIdent.ident,
1445 UDF_ID_SPARING,
1446 strlen(UDF_ID_SPARING))) {
1447 brelse(bh2);
1448 map->s_type_specific.s_sparing.
1449 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001452 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001453 } else if (!strncmp(upm2->partIdent.ident,
1454 UDF_ID_METADATA,
1455 strlen(UDF_ID_METADATA))) {
1456 struct udf_meta_data *mdata =
1457 &map->s_type_specific.s_metadata;
1458 struct metadataPartitionMap *mdm =
1459 (struct metadataPartitionMap *)
1460 &(lvd->partitionMaps[offset]);
1461 udf_debug("Parsing Logical vol part %d "
1462 "type %d id=%s\n", i, type,
1463 UDF_ID_METADATA);
1464
1465 map->s_partition_type = UDF_METADATA_MAP25;
1466 map->s_partition_func = udf_get_pblock_meta25;
1467
1468 mdata->s_meta_file_loc =
1469 le32_to_cpu(mdm->metadataFileLoc);
1470 mdata->s_mirror_file_loc =
1471 le32_to_cpu(mdm->metadataMirrorFileLoc);
1472 mdata->s_bitmap_file_loc =
1473 le32_to_cpu(mdm->metadataBitmapFileLoc);
1474 mdata->s_alloc_unit_size =
1475 le32_to_cpu(mdm->allocUnitSize);
1476 mdata->s_align_unit_size =
1477 le16_to_cpu(mdm->alignUnitSize);
1478 mdata->s_dup_md_flag =
1479 mdm->flags & 0x01;
1480
1481 udf_debug("Metadata Ident suffix=0x%x\n",
1482 (le16_to_cpu(
1483 ((__le16 *)
1484 mdm->partIdent.identSuffix)[0])));
1485 udf_debug("Metadata part num=%d\n",
1486 le16_to_cpu(mdm->partitionNum));
1487 udf_debug("Metadata part alloc unit size=%d\n",
1488 le32_to_cpu(mdm->allocUnitSize));
1489 udf_debug("Metadata file loc=%d\n",
1490 le32_to_cpu(mdm->metadataFileLoc));
1491 udf_debug("Mirror file loc=%d\n",
1492 le32_to_cpu(mdm->metadataMirrorFileLoc));
1493 udf_debug("Bitmap file loc=%d\n",
1494 le32_to_cpu(mdm->metadataBitmapFileLoc));
1495 udf_debug("Duplicate Flag: %d %d\n",
1496 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001497 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001498 udf_debug("Unknown ident: %s\n",
1499 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 continue;
1501 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001502 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1503 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001506 i, map->s_partition_num, type,
1507 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001510 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001511 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001514 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1515 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001516 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 if (lvd->integritySeqExt.extLength)
1519 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001520
Jan Karac0eb31e2008-04-01 16:50:35 +02001521out_bh:
1522 brelse(bh);
1523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
1527 * udf_load_logicalvolint
1528 *
1529 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001530static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct buffer_head *bh = NULL;
1533 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001534 struct udf_sb_info *sbi = UDF_SB(sb);
1535 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001538 (bh = udf_read_tagged(sb, loc.extLocation,
1539 loc.extLocation, &ident)) &&
1540 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001541 sbi->s_lvid_bh = bh;
1542 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001543
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001544 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001545 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001546 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001547
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001548 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001549 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001551 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001554 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557/*
1558 * udf_process_sequence
1559 *
1560 * PURPOSE
1561 * Process a main/reserve volume descriptor sequence.
1562 *
1563 * PRE-CONDITIONS
1564 * sb Pointer to _locked_ superblock.
1565 * block First block of first extent of the sequence.
1566 * lastblock Lastblock of first extent of the sequence.
1567 *
1568 * HISTORY
1569 * July 1, 1997 - Andrew E. Mileski
1570 * Written, tested, and released.
1571 */
Jan Kara200a3592008-03-04 13:03:09 +01001572static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001573 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 struct buffer_head *bh = NULL;
1576 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001577 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct generic_desc *gd;
1579 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001580 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 uint32_t vdsn;
1582 uint16_t ident;
1583 long next_s = 0, next_e = 0;
1584
1585 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1586
Jan Karac0eb31e2008-04-01 16:50:35 +02001587 /*
1588 * Read the main descriptor sequence and find which descriptors
1589 * are in it.
1590 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001591 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001594 if (!bh) {
1595 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1596 "sequence is corrupted or we could not read "
1597 "it.\n", (unsigned long long)block);
1598 return 1;
1599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1602 gd = (struct generic_desc *)bh->b_data;
1603 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001604 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001605 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001606 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1607 if (vdsn >= curr->volDescSeqNum) {
1608 curr->volDescSeqNum = vdsn;
1609 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001610 }
1611 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001612 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001613 curr = &vds[VDS_POS_VOL_DESC_PTR];
1614 if (vdsn >= curr->volDescSeqNum) {
1615 curr->volDescSeqNum = vdsn;
1616 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001618 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001619 next_s = le32_to_cpu(
1620 vdp->nextVolDescSeqExt.extLocation);
1621 next_e = le32_to_cpu(
1622 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001623 next_e = next_e >> sb->s_blocksize_bits;
1624 next_e += next_s;
1625 }
1626 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001627 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001628 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1629 if (vdsn >= curr->volDescSeqNum) {
1630 curr->volDescSeqNum = vdsn;
1631 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001632 }
1633 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001634 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001635 curr = &vds[VDS_POS_PARTITION_DESC];
1636 if (!curr->block)
1637 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001638 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001639 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001640 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1641 if (vdsn >= curr->volDescSeqNum) {
1642 curr->volDescSeqNum = vdsn;
1643 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001644 }
1645 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001646 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001647 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1648 if (vdsn >= curr->volDescSeqNum) {
1649 curr->volDescSeqNum = vdsn;
1650 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001651 }
1652 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001653 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001654 vds[VDS_POS_TERMINATING_DESC].block = block;
1655 if (next_e) {
1656 block = next_s;
1657 lastblock = next_e;
1658 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001659 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001660 done = 1;
1661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001663 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001665 /*
1666 * Now read interesting descriptors again and process them
1667 * in a suitable order
1668 */
1669 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1670 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1671 return 1;
1672 }
1673 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1674 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jan Karac0eb31e2008-04-01 16:50:35 +02001676 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1677 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1678 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001679
Jan Karac0eb31e2008-04-01 16:50:35 +02001680 if (vds[VDS_POS_PARTITION_DESC].block) {
1681 /*
1682 * We rescan the whole descriptor sequence to find
1683 * partition descriptor blocks and process them.
1684 */
1685 for (block = vds[VDS_POS_PARTITION_DESC].block;
1686 block < vds[VDS_POS_TERMINATING_DESC].block;
1687 block++)
1688 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001689 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
1692 return 0;
1693}
1694
1695/*
1696 * udf_check_valid()
1697 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001698static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001701 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001703 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 udf_debug("Validity check skipped because of novrs option\n");
1705 return 0;
1706 }
1707 /* Check that it is NSR02 compliant */
1708 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001709 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001710 if (block == -1)
1711 udf_debug("Failed to read byte 32768. Assuming open "
1712 "disc. Skipping validity check\n");
1713 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001714 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001715 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001718static int udf_load_sequence(struct super_block *sb, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 struct anchorVolDescPtr *anchor;
1721 uint16_t ident;
1722 struct buffer_head *bh;
1723 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001724 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001725 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 if (!sb)
1728 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001729 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001731 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001732 if (!sbi->s_anchor[i])
1733 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001734
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001735 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1736 &ident);
1737 if (!bh)
1738 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001740 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001741
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001742 /* Locate the main sequence */
1743 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1744 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1745 main_e = main_e >> sb->s_blocksize_bits;
1746 main_e += main_s;
1747
1748 /* Locate the reserve sequence */
1749 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001750 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001751 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001752 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001753 reserve_e = reserve_e >> sb->s_blocksize_bits;
1754 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001756 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001758 /* Process the main & reserve sequences */
1759 /* responsible for finding the PartitionDesc(s) */
1760 if (!(udf_process_sequence(sb, main_s, main_e,
1761 fileset) &&
1762 udf_process_sequence(sb, reserve_s, reserve_e,
1763 fileset)))
1764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001767 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 udf_debug("No Anchor block found\n");
1769 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001770 }
1771 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return 0;
1774}
1775
1776static void udf_open_lvid(struct super_block *sb)
1777{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001778 struct udf_sb_info *sbi = UDF_SB(sb);
1779 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001780 struct logicalVolIntegrityDesc *lvid;
1781 struct logicalVolIntegrityDescImpUse *lvidiu;
1782 if (!bh)
1783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Marcin Slusarz165923f2008-02-10 11:33:08 +01001785 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1786 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Marcin Slusarz165923f2008-02-10 11:33:08 +01001788 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1789 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1790 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1791 CURRENT_TIME);
1792 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Marcin Slusarz165923f2008-02-10 11:33:08 +01001794 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001795 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001796 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001797
1798 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1799 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802static void udf_close_lvid(struct super_block *sb)
1803{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001804 struct udf_sb_info *sbi = UDF_SB(sb);
1805 struct buffer_head *bh = sbi->s_lvid_bh;
1806 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001807 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001808
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001809 if (!bh)
1810 return;
1811
1812 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1813
Marcin Slusarz165923f2008-02-10 11:33:08 +01001814 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1815 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Marcin Slusarz165923f2008-02-10 11:33:08 +01001817 lvidiu = udf_sb_lvidiu(sbi);
1818 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1819 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1820 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1821 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1822 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1823 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1824 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1825 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1826 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1827 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Marcin Slusarz165923f2008-02-10 11:33:08 +01001829 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001830 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001831 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001832
1833 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1834 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001837static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1838{
1839 int i;
1840 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001841 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1842 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001843
1844 for (i = 0; i < nr_groups; i++)
1845 if (bitmap->s_block_bitmap[i])
1846 brelse(bitmap->s_block_bitmap[i]);
1847
1848 if (size <= PAGE_SIZE)
1849 kfree(bitmap);
1850 else
1851 vfree(bitmap);
1852}
1853
Jan Kara2e0838f2008-04-01 18:08:51 +02001854static void udf_free_partition(struct udf_part_map *map)
1855{
1856 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001857 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001858
1859 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1860 iput(map->s_uspace.s_table);
1861 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1862 iput(map->s_fspace.s_table);
1863 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1864 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1865 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1866 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1867 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1868 for (i = 0; i < 4; i++)
1869 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001870 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1871 mdata = &map->s_type_specific.s_metadata;
1872 iput(mdata->s_metadata_fe);
1873 mdata->s_metadata_fe = NULL;
1874
1875 iput(mdata->s_mirror_fe);
1876 mdata->s_mirror_fe = NULL;
1877
1878 iput(mdata->s_bitmap_fe);
1879 mdata->s_bitmap_fe = NULL;
1880 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883static int udf_fill_super(struct super_block *sb, void *options, int silent)
1884{
1885 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001886 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001888 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct udf_sb_info *sbi;
1890
1891 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1892 uopt.uid = -1;
1893 uopt.gid = -1;
1894 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001895 uopt.fmode = UDF_INVALID_MODE;
1896 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001898 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (!sbi)
1900 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001904 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Miklos Szeredi6da80892008-02-08 04:21:50 -08001906 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto error_out;
1908
1909 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001910 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001912 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto error_out;
1914 }
1915#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001916 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 uopt.nls_map = load_nls_default();
1918 if (!uopt.nls_map)
1919 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1920 else
1921 udf_debug("Using default NLS map\n");
1922 }
1923#endif
1924 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1925 uopt.flags |= (1 << UDF_FLAG_UTF8);
1926
1927 fileset.logicalBlockNum = 0xFFFFFFFF;
1928 fileset.partitionReferenceNum = 0xFFFF;
1929
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001930 sbi->s_flags = uopt.flags;
1931 sbi->s_uid = uopt.uid;
1932 sbi->s_gid = uopt.gid;
1933 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01001934 sbi->s_fmode = uopt.fmode;
1935 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001936 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001939 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1940 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1941 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001945 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001946 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001948 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001950 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001952 sbi->s_last_block = uopt.lastblock;
1953 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1954 sbi->s_anchor[2] = uopt.anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001956 if (udf_check_valid(sb, uopt.novrs, silent)) {
1957 /* read volume recognition sequences */
1958 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001959 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961
1962 udf_find_anchor(sb);
1963
1964 /* Fill in the rest of the superblock */
1965 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02001966 sb->s_export_op = &udf_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 sb->dq_op = NULL;
1968 sb->s_dirt = 0;
1969 sb->s_magic = UDF_SUPER_MAGIC;
1970 sb->s_time_gran = 1000;
1971
Jan Kara38b74a52008-04-02 16:01:35 +02001972 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001973 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto error_out;
1975 }
1976
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001977 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001979 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 struct logicalVolIntegrityDescImpUse *lvidiu =
1981 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001982 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1983 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001984 /* uint16_t maxUDFWriteRev =
1985 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001987 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1989 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001990 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001991 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001996 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1999 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2000 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2001 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2002 }
2003
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002004 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002005 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto error_out;
2007 }
2008
Marcin Slusarz4b111112008-02-08 04:20:36 -08002009 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2010 UDF_PART_FLAG_READ_ONLY) {
2011 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
2012 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002013 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002014 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002015
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002016 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002017 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto error_out;
2019 }
2020
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002022 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002023 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08002024 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002025 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01002026 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2027 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029 if (!(sb->s_flags & MS_RDONLY))
2030 udf_open_lvid(sb);
2031
2032 /* Assign the root inode */
2033 /* assign inodes by physical block number */
2034 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002035 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002036 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002037 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2038 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002039 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto error_out;
2041 }
2042
2043 /* Allocate a dentry for the root inode */
2044 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002045 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002046 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 iput(inode);
2048 goto error_out;
2049 }
Jan Kara31170b62007-05-08 00:35:21 -07002050 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
2052
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002053error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002054 if (sbi->s_vat_inode)
2055 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002056 if (sbi->s_partitions)
2057 for (i = 0; i < sbi->s_partitions; i++)
2058 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#ifdef CONFIG_UDF_NLS
2060 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002061 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#endif
2063 if (!(sb->s_flags & MS_RDONLY))
2064 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002065 brelse(sbi->s_lvid_bh);
2066
2067 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 kfree(sbi);
2069 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return -EINVAL;
2072}
2073
Adrian Bunkb8145a72008-02-17 10:19:55 +02002074static void udf_error(struct super_block *sb, const char *function,
2075 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 va_list args;
2078
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002079 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* mark sb error */
2081 sb->s_dirt = 1;
2082 }
2083 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002084 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002086 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002087 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002091 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 va_list args;
2094
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002095 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002096 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 va_end(args);
2098 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002099 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002102static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002105 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002107 sbi = UDF_SB(sb);
2108 if (sbi->s_vat_inode)
2109 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002110 if (sbi->s_partitions)
2111 for (i = 0; i < sbi->s_partitions; i++)
2112 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#ifdef CONFIG_UDF_NLS
2114 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002115 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#endif
2117 if (!(sb->s_flags & MS_RDONLY))
2118 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002119 brelse(sbi->s_lvid_bh);
2120 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 kfree(sb->s_fs_info);
2122 sb->s_fs_info = NULL;
2123}
2124
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002125static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
David Howells726c3342006-06-23 02:02:58 -07002127 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002128 struct udf_sb_info *sbi = UDF_SB(sb);
2129 struct logicalVolIntegrityDescImpUse *lvidiu;
2130
2131 if (sbi->s_lvid_bh != NULL)
2132 lvidiu = udf_sb_lvidiu(sbi);
2133 else
2134 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 buf->f_type = UDF_SUPER_MAGIC;
2137 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002138 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 buf->f_bfree = udf_count_free(sb);
2140 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002141 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2142 le32_to_cpu(lvidiu->numDirs)) : 0)
2143 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 buf->f_ffree = buf->f_bfree;
2145 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002146 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 return 0;
2149}
2150
Marcin Slusarz4b111112008-02-08 04:20:36 -08002151static unsigned int udf_count_free_bitmap(struct super_block *sb,
2152 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct buffer_head *bh = NULL;
2155 unsigned int accum = 0;
2156 int index;
2157 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002158 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 uint8_t *ptr;
2161 uint16_t ident;
2162 struct spaceBitmapDesc *bm;
2163
2164 lock_kernel();
2165
2166 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002167 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002168 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002170 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 printk(KERN_ERR "udf: udf_count_free failed\n");
2172 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002173 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002174 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 printk(KERN_ERR "udf: udf_count_free failed\n");
2176 goto out;
2177 }
2178
2179 bm = (struct spaceBitmapDesc *)bh->b_data;
2180 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002181 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2182 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002184 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002185 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2186 accum += bitmap_weight((const unsigned long *)(ptr + index),
2187 cur_bytes * 8);
2188 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002189 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002190 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002191 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002193 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 udf_debug("read failed\n");
2195 goto out;
2196 }
2197 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002198 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002201 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002203out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 unlock_kernel();
2205
2206 return accum;
2207}
2208
Marcin Slusarz4b111112008-02-08 04:20:36 -08002209static unsigned int udf_count_free_table(struct super_block *sb,
2210 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002213 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002214 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002216 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 lock_kernel();
2219
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002220 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002221 epos.offset = sizeof(struct unallocSpaceEntry);
2222 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002224 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002226
Jan Kara3bf25cb2007-05-08 00:35:16 -07002227 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 unlock_kernel();
2230
2231 return accum;
2232}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002233
2234static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002237 struct udf_sb_info *sbi;
2238 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002240 sbi = UDF_SB(sb);
2241 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002242 struct logicalVolIntegrityDesc *lvid =
2243 (struct logicalVolIntegrityDesc *)
2244 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002245 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002246 accum = le32_to_cpu(
2247 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (accum == 0xFFFFFFFF)
2249 accum = 0;
2250 }
2251 }
2252
2253 if (accum)
2254 return accum;
2255
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002256 map = &sbi->s_partmaps[sbi->s_partition];
2257 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002258 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002259 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002261 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002262 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002263 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 if (accum)
2266 return accum;
2267
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002268 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002269 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002270 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002272 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002273 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002274 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276
2277 return accum;
2278}