blob: cae079eb5dd908ae941671bfe029a5a4da9f926b [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 *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020086static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020087static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
88 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020090 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static void udf_open_lvid(struct super_block *);
92static void udf_close_lvid(struct super_block *);
93static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070094static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080095static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020096static void udf_error(struct super_block *sb, const char *function,
97 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Marcin Slusarz6c79e982008-02-08 04:20:30 -080099struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
100{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800101 struct logicalVolIntegrityDesc *lvid =
102 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800103 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800104 __u32 offset = number_of_partitions * 2 *
105 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700110static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 int flags, const char *dev_name, void *data,
112 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
David Howells454e2392006-06-23 02:02:57 -0700114 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700118 .owner = THIS_MODULE,
119 .name = "udf",
120 .get_sb = udf_get_sb,
121 .kill_sb = kill_block_super,
122 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700125static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127static struct inode *udf_alloc_inode(struct super_block *sb)
128{
129 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800130 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (!ei)
132 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700133
134 ei->i_unique = 0;
135 ei->i_lenExtents = 0;
136 ei->i_next_alloc_block = 0;
137 ei->i_next_alloc_goal = 0;
138 ei->i_strat4096 = 0;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return &ei->vfs_inode;
141}
142
143static void udf_destroy_inode(struct inode *inode)
144{
145 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
146}
147
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700148static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Christoph Lametera35afb82007-05-16 22:10:57 -0700152 ei->i_ext.i_data = NULL;
153 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156static int init_inodecache(void)
157{
158 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
159 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160 0, (SLAB_RECLAIM_ACCOUNT |
161 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900162 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700163 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ENOMEM;
165 return 0;
166}
167
168static void destroy_inodecache(void)
169{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700170 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800174static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175 .alloc_inode = udf_alloc_inode,
176 .destroy_inode = udf_destroy_inode,
177 .write_inode = udf_write_inode,
178 .delete_inode = udf_delete_inode,
179 .clear_inode = udf_clear_inode,
180 .put_super = udf_put_super,
181 .write_super = udf_write_super,
182 .statfs = udf_statfs,
183 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800184 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned char novrs;
189 unsigned int blocksize;
190 unsigned int session;
191 unsigned int lastblock;
192 unsigned int anchor;
193 unsigned int volume;
194 unsigned short partition;
195 unsigned int fileset;
196 unsigned int rootdir;
197 unsigned int flags;
198 mode_t umask;
199 gid_t gid;
200 uid_t uid;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100201 mode_t fmode;
202 mode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct nls_table *nls_map;
204};
205
206static int __init init_udf_fs(void)
207{
208 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = init_inodecache();
211 if (err)
212 goto out1;
213 err = register_filesystem(&udf_fstype);
214 if (err)
215 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return err;
224}
225
226static void __exit exit_udf_fs(void)
227{
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
230}
231
232module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700233module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800235static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
236{
237 struct udf_sb_info *sbi = UDF_SB(sb);
238
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
240 GFP_KERNEL);
241 if (!sbi->s_partmaps) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700242 udf_error(sb, __func__,
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800243 "Unable to allocate space for %d partition maps",
244 count);
245 sbi->s_partitions = 0;
246 return -ENOMEM;
247 }
248
249 sbi->s_partitions = count;
250 return 0;
251}
252
Miklos Szeredi6da80892008-02-08 04:21:50 -0800253static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
254{
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
257
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100260 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100284 if (sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100285 seq_printf(seq, ",mode=%o", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100286 if (sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100287 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800288 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
289 seq_printf(seq, ",session=%u", sbi->s_session);
290 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
291 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara40346002009-03-19 16:21:38 +0100292 if (sbi->s_anchor != 0)
293 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800294 /*
295 * volume, partition, fileset and rootdir seem to be ignored
296 * currently
297 */
298 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
299 seq_puts(seq, ",utf8");
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
301 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
302
303 return 0;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * udf_parse_options
308 *
309 * PURPOSE
310 * Parse mount options.
311 *
312 * DESCRIPTION
313 * The following mount options are supported:
314 *
315 * gid= Set the default group.
316 * umask= Set the default umask.
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100317 * mode= Set the default file permissions.
318 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
329 *
330 * The remaining are for debugging and disaster recovery:
331 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * The following expect a offset from 0.
335 *
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
341 *
342 * The following expect a offset from the partition root.
343 *
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
348 *
349 * PRE-CONDITIONS
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
352 *
353 * POST-CONDITIONS
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
356 *
357 * HISTORY
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
360 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362enum {
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
369 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
Steven Whitehousea447c092008-10-13 10:46:57 +0100372static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373 {Opt_novrs, "novrs"},
374 {Opt_nostrict, "nostrict"},
375 {Opt_bs, "bs=%u"},
376 {Opt_unhide, "unhide"},
377 {Opt_undelete, "undelete"},
378 {Opt_noadinicb, "noadinicb"},
379 {Opt_adinicb, "adinicb"},
380 {Opt_shortad, "shortad"},
381 {Opt_longad, "longad"},
382 {Opt_uforget, "uid=forget"},
383 {Opt_uignore, "uid=ignore"},
384 {Opt_gforget, "gid=forget"},
385 {Opt_gignore, "gid=ignore"},
386 {Opt_gid, "gid=%u"},
387 {Opt_uid, "uid=%u"},
388 {Opt_umask, "umask=%o"},
389 {Opt_session, "session=%u"},
390 {Opt_lastblock, "lastblock=%u"},
391 {Opt_anchor, "anchor=%u"},
392 {Opt_volume, "volume=%u"},
393 {Opt_partition, "partition=%u"},
394 {Opt_fileset, "fileset=%u"},
395 {Opt_rootdir, "rootdir=%u"},
396 {Opt_utf8, "utf8"},
397 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100398 {Opt_fmode, "mode=%o"},
399 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700400 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Miklos Szeredi6da80892008-02-08 04:21:50 -0800403static int udf_parse_options(char *options, struct udf_options *uopt,
404 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 char *p;
407 int option;
408
409 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 uopt->partition = 0xFFFF;
411 uopt->session = 0xFFFFFFFF;
412 uopt->lastblock = 0;
413 uopt->anchor = 0;
414 uopt->volume = 0xFFFFFFFF;
415 uopt->rootdir = 0xFFFFFFFF;
416 uopt->fileset = 0xFFFFFFFF;
417 uopt->nls_map = NULL;
418
419 if (!options)
420 return 1;
421
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700422 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 substring_t args[MAX_OPT_ARGS];
424 int token;
425 if (!*p)
426 continue;
427
428 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700429 switch (token) {
430 case Opt_novrs:
431 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100432 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700433 case Opt_bs:
434 if (match_int(&args[0], &option))
435 return 0;
436 uopt->blocksize = option;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100437 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700438 break;
439 case Opt_unhide:
440 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
441 break;
442 case Opt_undelete:
443 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
444 break;
445 case Opt_noadinicb:
446 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
447 break;
448 case Opt_adinicb:
449 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
450 break;
451 case Opt_shortad:
452 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
453 break;
454 case Opt_longad:
455 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
456 break;
457 case Opt_gid:
458 if (match_int(args, &option))
459 return 0;
460 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700461 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 break;
463 case Opt_uid:
464 if (match_int(args, &option))
465 return 0;
466 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700467 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 break;
469 case Opt_umask:
470 if (match_octal(args, &option))
471 return 0;
472 uopt->umask = option;
473 break;
474 case Opt_nostrict:
475 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
476 break;
477 case Opt_session:
478 if (match_int(args, &option))
479 return 0;
480 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800481 if (!remount)
482 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483 break;
484 case Opt_lastblock:
485 if (match_int(args, &option))
486 return 0;
487 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800488 if (!remount)
489 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700490 break;
491 case Opt_anchor:
492 if (match_int(args, &option))
493 return 0;
494 uopt->anchor = option;
495 break;
496 case Opt_volume:
497 if (match_int(args, &option))
498 return 0;
499 uopt->volume = option;
500 break;
501 case Opt_partition:
502 if (match_int(args, &option))
503 return 0;
504 uopt->partition = option;
505 break;
506 case Opt_fileset:
507 if (match_int(args, &option))
508 return 0;
509 uopt->fileset = option;
510 break;
511 case Opt_rootdir:
512 if (match_int(args, &option))
513 return 0;
514 uopt->rootdir = option;
515 break;
516 case Opt_utf8:
517 uopt->flags |= (1 << UDF_FLAG_UTF8);
518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700520 case Opt_iocharset:
521 uopt->nls_map = load_nls(args[0].from);
522 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 case Opt_uignore:
526 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
527 break;
528 case Opt_uforget:
529 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
530 break;
531 case Opt_gignore:
532 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
533 break;
534 case Opt_gforget:
535 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
536 break;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100537 case Opt_fmode:
538 if (match_octal(args, &option))
539 return 0;
540 uopt->fmode = option & 0777;
541 break;
542 case Opt_dmode:
543 if (match_octal(args, &option))
544 return 0;
545 uopt->dmode = option & 0777;
546 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 default:
548 printk(KERN_ERR "udf: bad mount option \"%s\" "
549 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 0;
551 }
552 }
553 return 1;
554}
555
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800556static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (!(sb->s_flags & MS_RDONLY))
561 udf_open_lvid(sb);
562 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 unlock_kernel();
565}
566
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700567static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800570 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800572 uopt.flags = sbi->s_flags;
573 uopt.uid = sbi->s_uid;
574 uopt.gid = sbi->s_gid;
575 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100576 uopt.fmode = sbi->s_fmode;
577 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Miklos Szeredi6da80892008-02-08 04:21:50 -0800579 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EINVAL;
581
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800582 sbi->s_flags = uopt.flags;
583 sbi->s_uid = uopt.uid;
584 sbi->s_gid = uopt.gid;
585 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100586 sbi->s_fmode = uopt.fmode;
587 sbi->s_dmode = uopt.dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800589 if (sbi->s_lvid_bh) {
590 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (write_rev > UDF_MAX_WRITE_VERSION)
592 *flags |= MS_RDONLY;
593 }
594
595 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
596 return 0;
597 if (*flags & MS_RDONLY)
598 udf_close_lvid(sb);
599 else
600 udf_open_lvid(sb);
601
602 return 0;
603}
604
Jan Kara40346002009-03-19 16:21:38 +0100605/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
606/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
607static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200610 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int sectorsize;
612 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 int nsr02 = 0;
614 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800615 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800617 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (sb->s_blocksize < sizeof(struct volStructDesc))
619 sectorsize = sizeof(struct volStructDesc);
620 else
621 sectorsize = sb->s_blocksize;
622
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800623 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200626 (unsigned int)(sector >> sb->s_blocksize_bits),
627 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Read a block */
631 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
632 if (!bh)
633 break;
634
635 /* Look for ISO descriptors */
636 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800637 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700639 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700640 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800642 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
643 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 switch (vsd->structType) {
645 case 0:
646 udf_debug("ISO9660 Boot Record found\n");
647 break;
648 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800649 udf_debug("ISO9660 Primary Volume Descriptor "
650 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700651 break;
652 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800653 udf_debug("ISO9660 Supplementary Volume "
654 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700655 break;
656 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800657 udf_debug("ISO9660 Volume Partition Descriptor "
658 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700659 break;
660 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 udf_debug("ISO9660 Volume Descriptor Set "
662 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 break;
664 default:
665 udf_debug("ISO9660 VRS (%u) found\n",
666 vsd->structType);
667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800669 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
670 VSD_STD_ID_LEN))
671 ; /* nothing */
672 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
673 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700674 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800676 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
677 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800679 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
680 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700682 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (nsr03)
686 return nsr03;
687 else if (nsr02)
688 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800689 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -1;
691 else
692 return 0;
693}
694
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800695static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200696 struct kernel_lb_addr *fileset,
697 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 struct buffer_head *bh = NULL;
700 long lastblock;
701 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800702 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200706 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700708 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700710 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700711 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 1;
713 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800717 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800718 if (!bh) {
719 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200720 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700722/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700724
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800725 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700726 (newfileset.partitionReferenceNum != 0xFFFF &&
727 fileset->logicalBlockNum == 0xFFFFFFFF &&
728 fileset->partitionReferenceNum == 0xFFFF);
729 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800730 lastblock = sbi->s_partmaps
731 [newfileset.partitionReferenceNum]
732 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 newfileset.logicalBlockNum = 0;
734
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200736 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800737 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738 if (!bh) {
739 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 continue;
741 }
742
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700743 switch (ident) {
744 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700745 {
746 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800747 sp = (struct spaceBitmapDesc *)
748 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700749 newfileset.logicalBlockNum += 1 +
750 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800751 sizeof(struct spaceBitmapDesc)
752 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700753 brelse(bh);
754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700756 case TAG_IDENT_FSD:
757 *fileset = newfileset;
758 break;
759 default:
760 newfileset.logicalBlockNum++;
761 brelse(bh);
762 bh = NULL;
763 break;
764 }
765 } while (newfileset.logicalBlockNum < lastblock &&
766 fileset->logicalBlockNum == 0xFFFFFFFF &&
767 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
770
771 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700772 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 fileset->logicalBlockNum,
775 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800777 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700779 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781 }
782 return 1;
783}
784
Jan Karac0eb31e2008-04-01 16:50:35 +0200785static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100788 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200789 struct buffer_head *bh;
790 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100791 int ret = 1;
792
793 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
794 if (!instr)
795 return 1;
796
797 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
798 if (!outstr)
799 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200800
801 bh = udf_read_tagged(sb, block, block, &ident);
802 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100803 goto out2;
804
Jan Karac0eb31e2008-04-01 16:50:35 +0200805 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 pvoldesc = (struct primaryVolDesc *)bh->b_data;
808
Marcin Slusarz56774802008-02-10 11:25:31 +0100809 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
810 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100811#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200812 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100813 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800814 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100815 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
816 ts->minute, le16_to_cpu(ts->typeAndTimezone));
817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100820 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
821 if (udf_CS0toUTF8(outstr, instr)) {
822 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
823 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800824 udf_debug("volIdent[] = '%s'\n",
825 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100828 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
829 if (udf_CS0toUTF8(outstr, instr))
830 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200831
832 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100833 ret = 0;
834out2:
835 kfree(outstr);
836out1:
837 kfree(instr);
838 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Jan Karabfb257a2008-04-08 20:37:21 +0200841static int udf_load_metadata_files(struct super_block *sb, int partition)
842{
843 struct udf_sb_info *sbi = UDF_SB(sb);
844 struct udf_part_map *map;
845 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200846 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200847 int fe_error = 0;
848
849 map = &sbi->s_partmaps[partition];
850 mdata = &map->s_type_specific.s_metadata;
851
852 /* metadata address */
853 addr.logicalBlockNum = mdata->s_meta_file_loc;
854 addr.partitionReferenceNum = map->s_partition_num;
855
856 udf_debug("Metadata file location: block = %d part = %d\n",
857 addr.logicalBlockNum, addr.partitionReferenceNum);
858
Pekka Enberg97e961f2008-10-15 12:29:03 +0200859 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200860
861 if (mdata->s_metadata_fe == NULL) {
862 udf_warning(sb, __func__, "metadata inode efe not found, "
863 "will try mirror inode.");
864 fe_error = 1;
865 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
866 ICBTAG_FLAG_AD_SHORT) {
867 udf_warning(sb, __func__, "metadata inode efe does not have "
868 "short allocation descriptors!");
869 fe_error = 1;
870 iput(mdata->s_metadata_fe);
871 mdata->s_metadata_fe = NULL;
872 }
873
874 /* mirror file entry */
875 addr.logicalBlockNum = mdata->s_mirror_file_loc;
876 addr.partitionReferenceNum = map->s_partition_num;
877
878 udf_debug("Mirror metadata file location: block = %d part = %d\n",
879 addr.logicalBlockNum, addr.partitionReferenceNum);
880
Pekka Enberg97e961f2008-10-15 12:29:03 +0200881 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200882
883 if (mdata->s_mirror_fe == NULL) {
884 if (fe_error) {
885 udf_error(sb, __func__, "mirror inode efe not found "
886 "and metadata inode is missing too, exiting...");
887 goto error_exit;
888 } else
889 udf_warning(sb, __func__, "mirror inode efe not found,"
890 " but metadata inode is OK");
891 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
892 ICBTAG_FLAG_AD_SHORT) {
893 udf_warning(sb, __func__, "mirror inode efe does not have "
894 "short allocation descriptors!");
895 iput(mdata->s_mirror_fe);
896 mdata->s_mirror_fe = NULL;
897 if (fe_error)
898 goto error_exit;
899 }
900
901 /*
902 * bitmap file entry
903 * Note:
904 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
905 */
906 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
907 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
908 addr.partitionReferenceNum = map->s_partition_num;
909
910 udf_debug("Bitmap file location: block = %d part = %d\n",
911 addr.logicalBlockNum, addr.partitionReferenceNum);
912
Pekka Enberg97e961f2008-10-15 12:29:03 +0200913 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200914
915 if (mdata->s_bitmap_fe == NULL) {
916 if (sb->s_flags & MS_RDONLY)
917 udf_warning(sb, __func__, "bitmap inode efe "
918 "not found but it's ok since the disc"
919 " is mounted read-only");
920 else {
921 udf_error(sb, __func__, "bitmap inode efe not "
922 "found and attempted read-write mount");
923 goto error_exit;
924 }
925 }
926 }
927
928 udf_debug("udf_load_metadata_files Ok\n");
929
930 return 0;
931
932error_exit:
933 return 1;
934}
935
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700936static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200937 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct fileSetDesc *fset;
940
941 fset = (struct fileSetDesc *)bh->b_data;
942
943 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
944
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800945 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700947 udf_debug("Rootdir at block=%d, partition=%d\n",
948 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800951int udf_compute_nr_groups(struct super_block *sb, u32 partition)
952{
953 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100954 return DIV_ROUND_UP(map->s_partition_len +
955 (sizeof(struct spaceBitmapDesc) << 3),
956 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800957}
958
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800959static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
960{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800961 struct udf_bitmap *bitmap;
962 int nr_groups;
963 int size;
964
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800965 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800966 size = sizeof(struct udf_bitmap) +
967 (sizeof(struct buffer_head *) * nr_groups);
968
969 if (size <= PAGE_SIZE)
970 bitmap = kmalloc(size, GFP_KERNEL);
971 else
972 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
973
974 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700975 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800976 "Unable to allocate space for bitmap "
977 "and %d buffer_head pointers", nr_groups);
978 return NULL;
979 }
980
981 memset(bitmap, 0x00, size);
982 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
983 bitmap->s_nr_groups = nr_groups;
984 return bitmap;
985}
986
Jan Kara3fb38df2008-04-02 12:26:36 +0200987static int udf_fill_partdesc_info(struct super_block *sb,
988 struct partitionDesc *p, int p_index)
989{
990 struct udf_part_map *map;
991 struct udf_sb_info *sbi = UDF_SB(sb);
992 struct partitionHeaderDesc *phd;
993
994 map = &sbi->s_partmaps[p_index];
995
996 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
997 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
998
999 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1000 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1001 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1002 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1003 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1004 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1005 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1006 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1007
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001008 udf_debug("Partition (%d type %x) starts at physical %d, "
1009 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001010 map->s_partition_type, map->s_partition_root,
1011 map->s_partition_len);
1012
1013 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1014 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1015 return 0;
1016
1017 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1018 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001019 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001020 .logicalBlockNum = le32_to_cpu(
1021 phd->unallocSpaceTable.extPosition),
1022 .partitionReferenceNum = p_index,
1023 };
1024
Pekka Enberg97e961f2008-10-15 12:29:03 +02001025 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001026 if (!map->s_uspace.s_table) {
1027 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1028 p_index);
1029 return 1;
1030 }
1031 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1032 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1033 p_index, map->s_uspace.s_table->i_ino);
1034 }
1035
1036 if (phd->unallocSpaceBitmap.extLength) {
1037 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1038 if (!bitmap)
1039 return 1;
1040 map->s_uspace.s_bitmap = bitmap;
1041 bitmap->s_extLength = le32_to_cpu(
1042 phd->unallocSpaceBitmap.extLength);
1043 bitmap->s_extPosition = le32_to_cpu(
1044 phd->unallocSpaceBitmap.extPosition);
1045 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1046 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1047 bitmap->s_extPosition);
1048 }
1049
1050 if (phd->partitionIntegrityTable.extLength)
1051 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1052
1053 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001054 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001055 .logicalBlockNum = le32_to_cpu(
1056 phd->freedSpaceTable.extPosition),
1057 .partitionReferenceNum = p_index,
1058 };
1059
Pekka Enberg97e961f2008-10-15 12:29:03 +02001060 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001061 if (!map->s_fspace.s_table) {
1062 udf_debug("cannot load freedSpaceTable (part %d)\n",
1063 p_index);
1064 return 1;
1065 }
1066
1067 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1068 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1069 p_index, map->s_fspace.s_table->i_ino);
1070 }
1071
1072 if (phd->freedSpaceBitmap.extLength) {
1073 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1074 if (!bitmap)
1075 return 1;
1076 map->s_fspace.s_bitmap = bitmap;
1077 bitmap->s_extLength = le32_to_cpu(
1078 phd->freedSpaceBitmap.extLength);
1079 bitmap->s_extPosition = le32_to_cpu(
1080 phd->freedSpaceBitmap.extPosition);
1081 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1082 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1083 bitmap->s_extPosition);
1084 }
1085 return 0;
1086}
1087
Jan Kara38b74a52008-04-02 16:01:35 +02001088static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1089{
1090 struct udf_sb_info *sbi = UDF_SB(sb);
1091 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001092 struct kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001093 struct buffer_head *bh = NULL;
1094 struct udf_inode_info *vati;
1095 uint32_t pos;
1096 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001097
1098 /* VAT file entry is in the last recorded block */
1099 ino.partitionReferenceNum = type1_index;
1100 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
Pekka Enberg97e961f2008-10-15 12:29:03 +02001101 sbi->s_vat_inode = udf_iget(sb, &ino);
Jan Kara38b74a52008-04-02 16:01:35 +02001102 if (!sbi->s_vat_inode)
1103 return 1;
1104
1105 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001106 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001107 map->s_type_specific.s_virtual.s_num_entries =
1108 (sbi->s_vat_inode->i_size - 36) >> 2;
1109 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001110 vati = UDF_I(sbi->s_vat_inode);
1111 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1112 pos = udf_block_map(sbi->s_vat_inode, 0);
1113 bh = sb_bread(sb, pos);
1114 if (!bh)
1115 return 1;
1116 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1117 } else {
1118 vat20 = (struct virtualAllocationTable20 *)
1119 vati->i_ext.i_data;
1120 }
Jan Kara38b74a52008-04-02 16:01:35 +02001121
Jan Kara38b74a52008-04-02 16:01:35 +02001122 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001123 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001124 map->s_type_specific.s_virtual.s_num_entries =
1125 (sbi->s_vat_inode->i_size -
1126 map->s_type_specific.s_virtual.
1127 s_start_offset) >> 2;
1128 brelse(bh);
1129 }
1130 return 0;
1131}
1132
Jan Karac0eb31e2008-04-01 16:50:35 +02001133static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Jan Karac0eb31e2008-04-01 16:50:35 +02001135 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001136 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001137 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001138 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001139 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001140 uint16_t partitionNumber;
1141 uint16_t ident;
1142 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Jan Karac0eb31e2008-04-01 16:50:35 +02001144 bh = udf_read_tagged(sb, block, block, &ident);
1145 if (!bh)
1146 return 1;
1147 if (ident != TAG_IDENT_PD)
1148 goto out_bh;
1149
1150 p = (struct partitionDesc *)bh->b_data;
1151 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001152
Jan Karabfb257a2008-04-08 20:37:21 +02001153 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001154 for (i = 0; i < sbi->s_partitions; i++) {
1155 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001156 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001157 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001158 if (map->s_partition_num == partitionNumber &&
1159 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1160 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001162 }
1163
Jan Kara38b74a52008-04-02 16:01:35 +02001164 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001165 udf_debug("Partition (%d) not found in partition map\n",
1166 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001167 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001168 }
1169
Jan Kara3fb38df2008-04-02 12:26:36 +02001170 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001171
1172 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001173 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1174 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001175 */
1176 type1_idx = i;
1177 for (i = 0; i < sbi->s_partitions; i++) {
1178 map = &sbi->s_partmaps[i];
1179
1180 if (map->s_partition_num == partitionNumber &&
1181 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001182 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1183 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001184 break;
1185 }
1186
1187 if (i >= sbi->s_partitions)
1188 goto out_bh;
1189
1190 ret = udf_fill_partdesc_info(sb, p, i);
1191 if (ret)
1192 goto out_bh;
1193
Jan Karabfb257a2008-04-08 20:37:21 +02001194 if (map->s_partition_type == UDF_METADATA_MAP25) {
1195 ret = udf_load_metadata_files(sb, i);
1196 if (ret) {
1197 printk(KERN_ERR "UDF-fs: error loading MetaData "
1198 "partition map %d\n", i);
1199 goto out_bh;
1200 }
1201 } else {
1202 ret = udf_load_vat(sb, i, type1_idx);
1203 if (ret)
1204 goto out_bh;
1205 /*
1206 * Mark filesystem read-only if we have a partition with
1207 * virtual map since we don't handle writing to it (we
1208 * overwrite blocks instead of relocating them).
1209 */
1210 sb->s_flags |= MS_RDONLY;
1211 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1212 "because writing to pseudooverwrite partition is "
1213 "not implemented.\n");
1214 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001215out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001216 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001217 brelse(bh);
1218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Jan Karac0eb31e2008-04-01 16:50:35 +02001221static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001222 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 struct logicalVolDesc *lvd;
1225 int i, j, offset;
1226 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001227 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001228 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001229 uint16_t ident;
1230 struct buffer_head *bh;
1231 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Jan Karac0eb31e2008-04-01 16:50:35 +02001233 bh = udf_read_tagged(sb, block, block, &ident);
1234 if (!bh)
1235 return 1;
1236 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 lvd = (struct logicalVolDesc *)bh->b_data;
1238
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001239 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001240 if (i != 0) {
1241 ret = i;
1242 goto out_bh;
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001245 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001246 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001247 i++, offset += gpm->partitionMapLength) {
1248 struct udf_part_map *map = &sbi->s_partmaps[i];
1249 gpm = (struct genericPartitionMap *)
1250 &(lvd->partitionMaps[offset]);
1251 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001252 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001253 struct genericPartitionMap1 *gpm1 =
1254 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001255 map->s_partition_type = UDF_TYPE1_MAP15;
1256 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1257 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1258 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001259 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001260 struct udfPartitionMap2 *upm2 =
1261 (struct udfPartitionMap2 *)gpm;
1262 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1263 strlen(UDF_ID_VIRTUAL))) {
1264 u16 suf =
1265 le16_to_cpu(((__le16 *)upm2->partIdent.
1266 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001267 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001268 map->s_partition_type =
1269 UDF_VIRTUAL_MAP15;
1270 map->s_partition_func =
1271 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001272 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001273 map->s_partition_type =
1274 UDF_VIRTUAL_MAP20;
1275 map->s_partition_func =
1276 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001278 } else if (!strncmp(upm2->partIdent.ident,
1279 UDF_ID_SPARABLE,
1280 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001283 struct sparablePartitionMap *spm =
1284 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001286 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001287 map->s_type_specific.s_sparing.s_packet_len =
1288 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001290 struct buffer_head *bh2;
1291
1292 loc = le32_to_cpu(
1293 spm->locSparingTable[j]);
1294 bh2 = udf_read_tagged(sb, loc, loc,
1295 &ident);
1296 map->s_type_specific.s_sparing.
1297 s_spar_map[j] = bh2;
1298
Marcin Slusarz165923f2008-02-10 11:33:08 +01001299 if (bh2 == NULL)
1300 continue;
1301
1302 st = (struct sparingTable *)bh2->b_data;
1303 if (ident != 0 || strncmp(
1304 st->sparingIdent.ident,
1305 UDF_ID_SPARING,
1306 strlen(UDF_ID_SPARING))) {
1307 brelse(bh2);
1308 map->s_type_specific.s_sparing.
1309 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001312 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001313 } else if (!strncmp(upm2->partIdent.ident,
1314 UDF_ID_METADATA,
1315 strlen(UDF_ID_METADATA))) {
1316 struct udf_meta_data *mdata =
1317 &map->s_type_specific.s_metadata;
1318 struct metadataPartitionMap *mdm =
1319 (struct metadataPartitionMap *)
1320 &(lvd->partitionMaps[offset]);
1321 udf_debug("Parsing Logical vol part %d "
1322 "type %d id=%s\n", i, type,
1323 UDF_ID_METADATA);
1324
1325 map->s_partition_type = UDF_METADATA_MAP25;
1326 map->s_partition_func = udf_get_pblock_meta25;
1327
1328 mdata->s_meta_file_loc =
1329 le32_to_cpu(mdm->metadataFileLoc);
1330 mdata->s_mirror_file_loc =
1331 le32_to_cpu(mdm->metadataMirrorFileLoc);
1332 mdata->s_bitmap_file_loc =
1333 le32_to_cpu(mdm->metadataBitmapFileLoc);
1334 mdata->s_alloc_unit_size =
1335 le32_to_cpu(mdm->allocUnitSize);
1336 mdata->s_align_unit_size =
1337 le16_to_cpu(mdm->alignUnitSize);
1338 mdata->s_dup_md_flag =
1339 mdm->flags & 0x01;
1340
1341 udf_debug("Metadata Ident suffix=0x%x\n",
1342 (le16_to_cpu(
1343 ((__le16 *)
1344 mdm->partIdent.identSuffix)[0])));
1345 udf_debug("Metadata part num=%d\n",
1346 le16_to_cpu(mdm->partitionNum));
1347 udf_debug("Metadata part alloc unit size=%d\n",
1348 le32_to_cpu(mdm->allocUnitSize));
1349 udf_debug("Metadata file loc=%d\n",
1350 le32_to_cpu(mdm->metadataFileLoc));
1351 udf_debug("Mirror file loc=%d\n",
1352 le32_to_cpu(mdm->metadataMirrorFileLoc));
1353 udf_debug("Bitmap file loc=%d\n",
1354 le32_to_cpu(mdm->metadataBitmapFileLoc));
1355 udf_debug("Duplicate Flag: %d %d\n",
1356 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001357 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001358 udf_debug("Unknown ident: %s\n",
1359 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 continue;
1361 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001362 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1363 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001366 i, map->s_partition_num, type,
1367 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001371 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001374 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1375 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001376 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378 if (lvd->integritySeqExt.extLength)
1379 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001380
Jan Karac0eb31e2008-04-01 16:50:35 +02001381out_bh:
1382 brelse(bh);
1383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/*
1387 * udf_load_logicalvolint
1388 *
1389 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001390static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 struct buffer_head *bh = NULL;
1393 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001394 struct udf_sb_info *sbi = UDF_SB(sb);
1395 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001398 (bh = udf_read_tagged(sb, loc.extLocation,
1399 loc.extLocation, &ident)) &&
1400 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001401 sbi->s_lvid_bh = bh;
1402 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001403
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001404 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001405 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001406 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001407
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001408 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001409 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001411 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001413 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001414 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417/*
1418 * udf_process_sequence
1419 *
1420 * PURPOSE
1421 * Process a main/reserve volume descriptor sequence.
1422 *
1423 * PRE-CONDITIONS
1424 * sb Pointer to _locked_ superblock.
1425 * block First block of first extent of the sequence.
1426 * lastblock Lastblock of first extent of the sequence.
1427 *
1428 * HISTORY
1429 * July 1, 1997 - Andrew E. Mileski
1430 * Written, tested, and released.
1431 */
Jan Kara200a3592008-03-04 13:03:09 +01001432static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001433 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 struct buffer_head *bh = NULL;
1436 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001437 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct generic_desc *gd;
1439 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001440 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 uint32_t vdsn;
1442 uint16_t ident;
1443 long next_s = 0, next_e = 0;
1444
1445 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1446
Jan Karac0eb31e2008-04-01 16:50:35 +02001447 /*
1448 * Read the main descriptor sequence and find which descriptors
1449 * are in it.
1450 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001451 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001454 if (!bh) {
1455 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1456 "sequence is corrupted or we could not read "
1457 "it.\n", (unsigned long long)block);
1458 return 1;
1459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1462 gd = (struct generic_desc *)bh->b_data;
1463 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001464 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001465 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001466 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1467 if (vdsn >= curr->volDescSeqNum) {
1468 curr->volDescSeqNum = vdsn;
1469 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001470 }
1471 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001472 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001473 curr = &vds[VDS_POS_VOL_DESC_PTR];
1474 if (vdsn >= curr->volDescSeqNum) {
1475 curr->volDescSeqNum = vdsn;
1476 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001478 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001479 next_s = le32_to_cpu(
1480 vdp->nextVolDescSeqExt.extLocation);
1481 next_e = le32_to_cpu(
1482 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001483 next_e = next_e >> sb->s_blocksize_bits;
1484 next_e += next_s;
1485 }
1486 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001487 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001488 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1489 if (vdsn >= curr->volDescSeqNum) {
1490 curr->volDescSeqNum = vdsn;
1491 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001492 }
1493 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001494 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001495 curr = &vds[VDS_POS_PARTITION_DESC];
1496 if (!curr->block)
1497 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001498 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001499 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001500 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1501 if (vdsn >= curr->volDescSeqNum) {
1502 curr->volDescSeqNum = vdsn;
1503 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001504 }
1505 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001506 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001507 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1508 if (vdsn >= curr->volDescSeqNum) {
1509 curr->volDescSeqNum = vdsn;
1510 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001511 }
1512 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001513 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001514 vds[VDS_POS_TERMINATING_DESC].block = block;
1515 if (next_e) {
1516 block = next_s;
1517 lastblock = next_e;
1518 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001519 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520 done = 1;
1521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001523 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001525 /*
1526 * Now read interesting descriptors again and process them
1527 * in a suitable order
1528 */
1529 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1530 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1531 return 1;
1532 }
1533 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1534 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Jan Karac0eb31e2008-04-01 16:50:35 +02001536 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1537 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1538 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001539
Jan Karac0eb31e2008-04-01 16:50:35 +02001540 if (vds[VDS_POS_PARTITION_DESC].block) {
1541 /*
1542 * We rescan the whole descriptor sequence to find
1543 * partition descriptor blocks and process them.
1544 */
1545 for (block = vds[VDS_POS_PARTITION_DESC].block;
1546 block < vds[VDS_POS_TERMINATING_DESC].block;
1547 block++)
1548 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001549 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551
1552 return 0;
1553}
1554
Jan Kara40346002009-03-19 16:21:38 +01001555static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1556 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Jan Kara40346002009-03-19 16:21:38 +01001558 struct anchorVolDescPtr *anchor;
1559 long main_s, main_e, reserve_s, reserve_e;
1560 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Jan Kara40346002009-03-19 16:21:38 +01001562 sbi = UDF_SB(sb);
1563 anchor = (struct anchorVolDescPtr *)bh->b_data;
1564
1565 /* Locate the main sequence */
1566 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1567 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1568 main_e = main_e >> sb->s_blocksize_bits;
1569 main_e += main_s;
1570
1571 /* Locate the reserve sequence */
1572 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1573 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1574 reserve_e = reserve_e >> sb->s_blocksize_bits;
1575 reserve_e += reserve_s;
1576
1577 /* Process the main & reserve sequences */
1578 /* responsible for finding the PartitionDesc(s) */
1579 if (!udf_process_sequence(sb, main_s, main_e, fileset))
1580 return 1;
1581 return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Jan Kara40346002009-03-19 16:21:38 +01001584/*
1585 * Check whether there is an anchor block in the given block and
1586 * load Volume Descriptor Sequence if so.
1587 */
1588static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1589 struct kernel_lb_addr *fileset)
1590{
1591 struct buffer_head *bh;
1592 uint16_t ident;
1593 int ret;
1594
1595 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1596 udf_fixed_to_variable(block) >=
1597 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
1598 return 0;
1599
1600 bh = udf_read_tagged(sb, block, block, &ident);
1601 if (!bh)
1602 return 0;
1603 if (ident != TAG_IDENT_AVDP) {
1604 brelse(bh);
1605 return 0;
1606 }
1607 ret = udf_load_sequence(sb, bh, fileset);
1608 brelse(bh);
1609 return ret;
1610}
1611
1612/* Search for an anchor volume descriptor pointer */
1613static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
1614 struct kernel_lb_addr *fileset)
1615{
1616 sector_t last[6];
1617 int i;
1618 struct udf_sb_info *sbi = UDF_SB(sb);
1619 int last_count = 0;
1620
1621 /* First try user provided anchor */
1622 if (sbi->s_anchor) {
1623 if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
1624 return lastblock;
1625 }
1626 /*
1627 * according to spec, anchor is in either:
1628 * block 256
1629 * lastblock-256
1630 * lastblock
1631 * however, if the disc isn't closed, it could be 512.
1632 */
1633 if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
1634 return lastblock;
1635 /*
1636 * The trouble is which block is the last one. Drives often misreport
1637 * this so we try various possibilities.
1638 */
1639 last[last_count++] = lastblock;
1640 if (lastblock >= 1)
1641 last[last_count++] = lastblock - 1;
1642 last[last_count++] = lastblock + 1;
1643 if (lastblock >= 2)
1644 last[last_count++] = lastblock - 2;
1645 if (lastblock >= 150)
1646 last[last_count++] = lastblock - 150;
1647 if (lastblock >= 152)
1648 last[last_count++] = lastblock - 152;
1649
1650 for (i = 0; i < last_count; i++) {
1651 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
1652 sb->s_blocksize_bits)
1653 continue;
1654 if (udf_check_anchor_block(sb, last[i], fileset))
1655 return last[i];
1656 if (last[i] < 256)
1657 continue;
1658 if (udf_check_anchor_block(sb, last[i] - 256, fileset))
1659 return last[i];
1660 }
1661
1662 /* Finally try block 512 in case media is open */
1663 if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
1664 return last[0];
1665 return 0;
1666}
1667
1668/*
1669 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1670 * area specified by it. The function expects sbi->s_lastblock to be the last
1671 * block on the media.
1672 *
1673 * Return 1 if ok, 0 if not found.
1674 *
1675 */
1676static int udf_find_anchor(struct super_block *sb,
1677 struct kernel_lb_addr *fileset)
1678{
1679 sector_t lastblock;
1680 struct udf_sb_info *sbi = UDF_SB(sb);
1681
1682 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1683 if (lastblock)
1684 goto out;
1685
1686 /* No anchor found? Try VARCONV conversion of block numbers */
1687 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1688 /* Firstly, we try to not convert number of the last block */
1689 lastblock = udf_scan_anchors(sb,
1690 udf_variable_to_fixed(sbi->s_last_block),
1691 fileset);
1692 if (lastblock)
1693 goto out;
1694
1695 /* Secondly, we try with converted number of the last block */
1696 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1697 if (!lastblock) {
1698 /* VARCONV didn't help. Clear it. */
1699 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1700 return 0;
1701 }
1702out:
1703 sbi->s_last_block = lastblock;
1704 return 1;
1705}
1706
1707/*
1708 * Check Volume Structure Descriptor, find Anchor block and load Volume
1709 * Descriptor Sequence
1710 */
1711static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1712 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001713{
1714 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara40346002009-03-19 16:21:38 +01001715 loff_t nsr_off;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001716
1717 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1718 if (!silent)
1719 printk(KERN_WARNING "UDF-fs: Bad block size\n");
1720 return 0;
1721 }
1722 sbi->s_last_block = uopt->lastblock;
Jan Kara40346002009-03-19 16:21:38 +01001723 if (!uopt->novrs) {
1724 /* Check that it is NSR02 compliant */
1725 nsr_off = udf_check_vsd(sb);
1726 if (!nsr_off) {
1727 if (!silent)
1728 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1729 return 0;
1730 }
1731 if (nsr_off == -1)
1732 udf_debug("Failed to read byte 32768. Assuming open "
1733 "disc. Skipping validity check\n");
1734 if (!sbi->s_last_block)
1735 sbi->s_last_block = udf_get_last_block(sb);
1736 } else {
1737 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001738 }
Jan Kara40346002009-03-19 16:21:38 +01001739
1740 /* Look for anchor block and load Volume Descriptor Sequence */
1741 sbi->s_anchor = uopt->anchor;
1742 if (!udf_find_anchor(sb, fileset)) {
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001743 if (!silent)
1744 printk(KERN_WARNING "UDF-fs: No anchor found\n");
1745 return 0;
1746 }
1747 return 1;
1748}
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750static void udf_open_lvid(struct super_block *sb)
1751{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001752 struct udf_sb_info *sbi = UDF_SB(sb);
1753 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001754 struct logicalVolIntegrityDesc *lvid;
1755 struct logicalVolIntegrityDescImpUse *lvidiu;
1756 if (!bh)
1757 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Marcin Slusarz165923f2008-02-10 11:33:08 +01001759 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1760 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Marcin Slusarz165923f2008-02-10 11:33:08 +01001762 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1763 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1764 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1765 CURRENT_TIME);
1766 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Marcin Slusarz165923f2008-02-10 11:33:08 +01001768 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001769 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001770 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001771
1772 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1773 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776static void udf_close_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;
1780 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001781 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001782
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001783 if (!bh)
1784 return;
1785
1786 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1787
Marcin Slusarz165923f2008-02-10 11:33:08 +01001788 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1789 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Marcin Slusarz165923f2008-02-10 11:33:08 +01001791 lvidiu = udf_sb_lvidiu(sbi);
1792 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1793 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1794 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1795 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1796 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1797 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1798 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1799 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1800 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1801 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Marcin Slusarz165923f2008-02-10 11:33:08 +01001803 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001804 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001805 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001806
1807 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1808 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001811static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1812{
1813 int i;
1814 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001815 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1816 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001817
1818 for (i = 0; i < nr_groups; i++)
1819 if (bitmap->s_block_bitmap[i])
1820 brelse(bitmap->s_block_bitmap[i]);
1821
1822 if (size <= PAGE_SIZE)
1823 kfree(bitmap);
1824 else
1825 vfree(bitmap);
1826}
1827
Jan Kara2e0838f2008-04-01 18:08:51 +02001828static void udf_free_partition(struct udf_part_map *map)
1829{
1830 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001831 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001832
1833 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1834 iput(map->s_uspace.s_table);
1835 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1836 iput(map->s_fspace.s_table);
1837 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1838 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1839 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1840 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1841 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1842 for (i = 0; i < 4; i++)
1843 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001844 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1845 mdata = &map->s_type_specific.s_metadata;
1846 iput(mdata->s_metadata_fe);
1847 mdata->s_metadata_fe = NULL;
1848
1849 iput(mdata->s_mirror_fe);
1850 mdata->s_mirror_fe = NULL;
1851
1852 iput(mdata->s_bitmap_fe);
1853 mdata->s_bitmap_fe = NULL;
1854 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int udf_fill_super(struct super_block *sb, void *options, int silent)
1858{
1859 int i;
Jan Kara40346002009-03-19 16:21:38 +01001860 int ret;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001861 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001863 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct udf_sb_info *sbi;
1865
1866 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1867 uopt.uid = -1;
1868 uopt.gid = -1;
1869 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001870 uopt.fmode = UDF_INVALID_MODE;
1871 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001873 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!sbi)
1875 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001879 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Miklos Szeredi6da80892008-02-08 04:21:50 -08001881 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto error_out;
1883
1884 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001885 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001887 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto error_out;
1889 }
1890#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001891 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 uopt.nls_map = load_nls_default();
1893 if (!uopt.nls_map)
1894 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1895 else
1896 udf_debug("Using default NLS map\n");
1897 }
1898#endif
1899 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1900 uopt.flags |= (1 << UDF_FLAG_UTF8);
1901
1902 fileset.logicalBlockNum = 0xFFFFFFFF;
1903 fileset.partitionReferenceNum = 0xFFFF;
1904
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001905 sbi->s_flags = uopt.flags;
1906 sbi->s_uid = uopt.uid;
1907 sbi->s_gid = uopt.gid;
1908 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001909 sbi->s_fmode = uopt.fmode;
1910 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001911 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001914 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001916 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001918 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /* Fill in the rest of the superblock */
1921 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02001922 sb->s_export_op = &udf_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 sb->dq_op = NULL;
1924 sb->s_dirt = 0;
1925 sb->s_magic = UDF_SUPER_MAGIC;
1926 sb->s_time_gran = 1000;
1927
Jan Kara40346002009-03-19 16:21:38 +01001928 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
1929 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1930 } else {
1931 uopt.blocksize = bdev_hardsect_size(sb->s_bdev);
1932 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1933 if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
1934 if (!silent)
1935 printk(KERN_NOTICE
1936 "UDF-fs: Rescanning with blocksize "
1937 "%d\n", UDF_DEFAULT_BLOCKSIZE);
1938 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
1939 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1940 }
1941 }
1942 if (!ret) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001943 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 goto error_out;
1945 }
1946
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001947 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001949 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001950 struct logicalVolIntegrityDescImpUse *lvidiu =
1951 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001952 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1953 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001954 /* uint16_t maxUDFWriteRev =
1955 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001957 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001958 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1959 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001960 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001961 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001963 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001966 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1969 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1970 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1971 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1972 }
1973
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001974 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001975 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto error_out;
1977 }
1978
Marcin Slusarz4b111112008-02-08 04:20:36 -08001979 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1980 UDF_PART_FLAG_READ_ONLY) {
1981 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1982 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001983 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001984 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001985
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001986 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001987 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 goto error_out;
1989 }
1990
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001991 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001992 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01001993 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001994 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001995 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001996 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1997 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 if (!(sb->s_flags & MS_RDONLY))
2000 udf_open_lvid(sb);
2001
2002 /* Assign the root inode */
2003 /* assign inodes by physical block number */
2004 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002005 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002006 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002007 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2008 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002009 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto error_out;
2011 }
2012
2013 /* Allocate a dentry for the root inode */
2014 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002015 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002016 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 iput(inode);
2018 goto error_out;
2019 }
Jan Kara31170b62007-05-08 00:35:21 -07002020 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return 0;
2022
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002023error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002024 if (sbi->s_vat_inode)
2025 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002026 if (sbi->s_partitions)
2027 for (i = 0; i < sbi->s_partitions; i++)
2028 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029#ifdef CONFIG_UDF_NLS
2030 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002031 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#endif
2033 if (!(sb->s_flags & MS_RDONLY))
2034 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002035 brelse(sbi->s_lvid_bh);
2036
2037 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 kfree(sbi);
2039 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return -EINVAL;
2042}
2043
Adrian Bunkb8145a72008-02-17 10:19:55 +02002044static void udf_error(struct super_block *sb, const char *function,
2045 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 va_list args;
2048
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /* mark sb error */
2051 sb->s_dirt = 1;
2052 }
2053 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002054 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002056 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002057 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058}
2059
2060void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002061 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 va_list args;
2064
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002065 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002066 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 va_end(args);
2068 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002069 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002072static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002075 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002077 sbi = UDF_SB(sb);
2078 if (sbi->s_vat_inode)
2079 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002080 if (sbi->s_partitions)
2081 for (i = 0; i < sbi->s_partitions; i++)
2082 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083#ifdef CONFIG_UDF_NLS
2084 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002085 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#endif
2087 if (!(sb->s_flags & MS_RDONLY))
2088 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002089 brelse(sbi->s_lvid_bh);
2090 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 kfree(sb->s_fs_info);
2092 sb->s_fs_info = NULL;
2093}
2094
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002095static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
David Howells726c3342006-06-23 02:02:58 -07002097 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002098 struct udf_sb_info *sbi = UDF_SB(sb);
2099 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002100 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002101
2102 if (sbi->s_lvid_bh != NULL)
2103 lvidiu = udf_sb_lvidiu(sbi);
2104 else
2105 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 buf->f_type = UDF_SUPER_MAGIC;
2108 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002109 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 buf->f_bfree = udf_count_free(sb);
2111 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002112 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2113 le32_to_cpu(lvidiu->numDirs)) : 0)
2114 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 buf->f_ffree = buf->f_bfree;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002116 buf->f_namelen = UDF_NAME_LEN - 2;
Coly Li557f5a12009-01-20 01:36:55 +08002117 buf->f_fsid.val[0] = (u32)id;
2118 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 return 0;
2121}
2122
Marcin Slusarz4b111112008-02-08 04:20:36 -08002123static unsigned int udf_count_free_bitmap(struct super_block *sb,
2124 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 struct buffer_head *bh = NULL;
2127 unsigned int accum = 0;
2128 int index;
2129 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002130 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 uint8_t *ptr;
2133 uint16_t ident;
2134 struct spaceBitmapDesc *bm;
2135
2136 lock_kernel();
2137
2138 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002139 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002140 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002142 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 printk(KERN_ERR "udf: udf_count_free failed\n");
2144 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002145 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002146 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 printk(KERN_ERR "udf: udf_count_free failed\n");
2148 goto out;
2149 }
2150
2151 bm = (struct spaceBitmapDesc *)bh->b_data;
2152 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002153 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2154 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002156 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002157 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2158 accum += bitmap_weight((const unsigned long *)(ptr + index),
2159 cur_bytes * 8);
2160 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002161 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002162 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002163 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002165 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 udf_debug("read failed\n");
2167 goto out;
2168 }
2169 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002170 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
2172 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002173 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002175out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 unlock_kernel();
2177
2178 return accum;
2179}
2180
Marcin Slusarz4b111112008-02-08 04:20:36 -08002181static unsigned int udf_count_free_table(struct super_block *sb,
2182 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002185 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002186 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002188 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 lock_kernel();
2191
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002192 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002193 epos.offset = sizeof(struct unallocSpaceEntry);
2194 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002196 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002198
Jan Kara3bf25cb2007-05-08 00:35:16 -07002199 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 unlock_kernel();
2202
2203 return accum;
2204}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002205
2206static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002209 struct udf_sb_info *sbi;
2210 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002212 sbi = UDF_SB(sb);
2213 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002214 struct logicalVolIntegrityDesc *lvid =
2215 (struct logicalVolIntegrityDesc *)
2216 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002217 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002218 accum = le32_to_cpu(
2219 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (accum == 0xFFFFFFFF)
2221 accum = 0;
2222 }
2223 }
2224
2225 if (accum)
2226 return accum;
2227
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002228 map = &sbi->s_partmaps[sbi->s_partition];
2229 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002230 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002231 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002233 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002234 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002235 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237 if (accum)
2238 return accum;
2239
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002240 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002241 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002242 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002244 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002245 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002246 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
2249 return accum;
2250}