blob: f8fece43f6c67e0b50fa3d17233a741173abc86f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020059#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/byteorder.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "udf_sb.h"
63#include "udf_i.h"
64
65#include <linux/init.h>
66#include <asm/uaccess.h>
67
68#define VDS_POS_PRIMARY_VOL_DESC 0
69#define VDS_POS_UNALLOC_SPACE_DESC 1
70#define VDS_POS_LOGICAL_VOL_DESC 2
71#define VDS_POS_PARTITION_DESC 3
72#define VDS_POS_IMP_USE_VOL_DESC 4
73#define VDS_POS_VOL_DESC_PTR 5
74#define VDS_POS_TERMINATING_DESC 6
75#define VDS_POS_LENGTH 7
76
Miklos Szeredi6da80892008-02-08 04:21:50 -080077#define UDF_DEFAULT_BLOCKSIZE 2048
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static char error_buf[1024];
80
81/* These are the "meat" - everything else is stuffing */
82static int udf_fill_super(struct super_block *, void *, int);
83static void udf_put_super(struct super_block *);
84static void udf_write_super(struct super_block *);
85static int udf_remount_fs(struct super_block *, int *, char *);
86static int udf_check_valid(struct super_block *, int, int);
87static int udf_vrs(struct super_block *sb, int silent);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020088static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020089static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
90 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020092 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void udf_open_lvid(struct super_block *);
94static void udf_close_lvid(struct super_block *);
95static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070096static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080097static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020098static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800101struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
102{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700112static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells454e2392006-06-23 02:02:57 -0700116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700120 .owner = THIS_MODULE,
121 .name = "udf",
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct inode *udf_alloc_inode(struct super_block *sb)
130{
131 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!ei)
134 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700135
136 ei->i_unique = 0;
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
140 ei->i_strat4096 = 0;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return &ei->vfs_inode;
143}
144
145static void udf_destroy_inode(struct inode *inode)
146{
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
148}
149
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700150static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Lametera35afb82007-05-16 22:10:57 -0700154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int init_inodecache(void)
159{
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 0, (SLAB_RECLAIM_ACCOUNT |
163 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900164 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENOMEM;
167 return 0;
168}
169
170static void destroy_inodecache(void)
171{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700172 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800176static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800186 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char novrs;
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
194 unsigned int anchor;
195 unsigned int volume;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
199 unsigned int flags;
200 mode_t umask;
201 gid_t gid;
202 uid_t uid;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100203 mode_t fmode;
204 mode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct nls_table *nls_map;
206};
207
208static int __init init_udf_fs(void)
209{
210 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 err = init_inodecache();
213 if (err)
214 goto out1;
215 err = register_filesystem(&udf_fstype);
216 if (err)
217 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220
221out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700223
224out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
226}
227
228static void __exit exit_udf_fs(void)
229{
230 unregister_filesystem(&udf_fstype);
231 destroy_inodecache();
232}
233
234module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700235module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800237static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
238{
239 struct udf_sb_info *sbi = UDF_SB(sb);
240
241 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
242 GFP_KERNEL);
243 if (!sbi->s_partmaps) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700244 udf_error(sb, __func__,
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800245 "Unable to allocate space for %d partition maps",
246 count);
247 sbi->s_partitions = 0;
248 return -ENOMEM;
249 }
250
251 sbi->s_partitions = count;
252 return 0;
253}
254
Miklos Szeredi6da80892008-02-08 04:21:50 -0800255static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
256{
257 struct super_block *sb = mnt->mnt_sb;
258 struct udf_sb_info *sbi = UDF_SB(sb);
259
260 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
261 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800263 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
265 seq_puts(seq, ",unhide");
266 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
267 seq_puts(seq, ",undelete");
268 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
269 seq_puts(seq, ",noadinicb");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
271 seq_puts(seq, ",shortad");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
273 seq_puts(seq, ",uid=forget");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
275 seq_puts(seq, ",uid=ignore");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
277 seq_puts(seq, ",gid=forget");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
279 seq_puts(seq, ",gid=ignore");
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
281 seq_printf(seq, ",uid=%u", sbi->s_uid);
282 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
283 seq_printf(seq, ",gid=%u", sbi->s_gid);
284 if (sbi->s_umask != 0)
285 seq_printf(seq, ",umask=%o", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100286 if (sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100287 seq_printf(seq, ",mode=%o", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100288 if (sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100289 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800290 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
291 seq_printf(seq, ",session=%u", sbi->s_session);
292 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
293 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
294 /*
295 * s_anchor[2] could be zeroed out in case there is no anchor
296 * in the specified block, but then the "anchor=N" option
297 * originally given by the user wasn't effective, so it's OK
298 * if we don't show it.
299 */
300 if (sbi->s_anchor[2] != 0)
301 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
302 /*
303 * volume, partition, fileset and rootdir seem to be ignored
304 * currently
305 */
306 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
307 seq_puts(seq, ",utf8");
308 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
309 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
310
311 return 0;
312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*
315 * udf_parse_options
316 *
317 * PURPOSE
318 * Parse mount options.
319 *
320 * DESCRIPTION
321 * The following mount options are supported:
322 *
323 * gid= Set the default group.
324 * umask= Set the default umask.
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100325 * mode= Set the default file permissions.
326 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * uid= Set the default user.
328 * bs= Set the block size.
329 * unhide Show otherwise hidden files.
330 * undelete Show deleted files in lists.
331 * adinicb Embed data in the inode (default)
332 * noadinicb Don't embed data in the inode
333 * shortad Use short ad's
334 * longad Use long ad's (default)
335 * nostrict Unset strict conformance
336 * iocharset= Set the NLS character set
337 *
338 * The remaining are for debugging and disaster recovery:
339 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700340 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 *
342 * The following expect a offset from 0.
343 *
344 * session= Set the CDROM session (default= last session)
345 * anchor= Override standard anchor location. (default= 256)
346 * volume= Override the VolumeDesc location. (unused)
347 * partition= Override the PartitionDesc location. (unused)
348 * lastblock= Set the last block of the filesystem/
349 *
350 * The following expect a offset from the partition root.
351 *
352 * fileset= Override the fileset block location. (unused)
353 * rootdir= Override the root directory location. (unused)
354 * WARNING: overriding the rootdir to a non-directory may
355 * yield highly unpredictable results.
356 *
357 * PRE-CONDITIONS
358 * options Pointer to mount options string.
359 * uopts Pointer to mount options variable.
360 *
361 * POST-CONDITIONS
362 * <return> 1 Mount options parsed okay.
363 * <return> 0 Error parsing mount options.
364 *
365 * HISTORY
366 * July 1, 1997 - Andrew E. Mileski
367 * Written, tested, and released.
368 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370enum {
371 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
372 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
373 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
374 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
375 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100376 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
377 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
Steven Whitehousea447c092008-10-13 10:46:57 +0100380static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700381 {Opt_novrs, "novrs"},
382 {Opt_nostrict, "nostrict"},
383 {Opt_bs, "bs=%u"},
384 {Opt_unhide, "unhide"},
385 {Opt_undelete, "undelete"},
386 {Opt_noadinicb, "noadinicb"},
387 {Opt_adinicb, "adinicb"},
388 {Opt_shortad, "shortad"},
389 {Opt_longad, "longad"},
390 {Opt_uforget, "uid=forget"},
391 {Opt_uignore, "uid=ignore"},
392 {Opt_gforget, "gid=forget"},
393 {Opt_gignore, "gid=ignore"},
394 {Opt_gid, "gid=%u"},
395 {Opt_uid, "uid=%u"},
396 {Opt_umask, "umask=%o"},
397 {Opt_session, "session=%u"},
398 {Opt_lastblock, "lastblock=%u"},
399 {Opt_anchor, "anchor=%u"},
400 {Opt_volume, "volume=%u"},
401 {Opt_partition, "partition=%u"},
402 {Opt_fileset, "fileset=%u"},
403 {Opt_rootdir, "rootdir=%u"},
404 {Opt_utf8, "utf8"},
405 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100406 {Opt_fmode, "mode=%o"},
407 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700408 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
Miklos Szeredi6da80892008-02-08 04:21:50 -0800411static int udf_parse_options(char *options, struct udf_options *uopt,
412 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 char *p;
415 int option;
416
417 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 uopt->partition = 0xFFFF;
419 uopt->session = 0xFFFFFFFF;
420 uopt->lastblock = 0;
421 uopt->anchor = 0;
422 uopt->volume = 0xFFFFFFFF;
423 uopt->rootdir = 0xFFFFFFFF;
424 uopt->fileset = 0xFFFFFFFF;
425 uopt->nls_map = NULL;
426
427 if (!options)
428 return 1;
429
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 substring_t args[MAX_OPT_ARGS];
432 int token;
433 if (!*p)
434 continue;
435
436 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700437 switch (token) {
438 case Opt_novrs:
439 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100440 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 case Opt_bs:
442 if (match_int(&args[0], &option))
443 return 0;
444 uopt->blocksize = option;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100445 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700446 break;
447 case Opt_unhide:
448 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
449 break;
450 case Opt_undelete:
451 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
452 break;
453 case Opt_noadinicb:
454 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
455 break;
456 case Opt_adinicb:
457 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
458 break;
459 case Opt_shortad:
460 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
461 break;
462 case Opt_longad:
463 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
464 break;
465 case Opt_gid:
466 if (match_int(args, &option))
467 return 0;
468 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700469 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700470 break;
471 case Opt_uid:
472 if (match_int(args, &option))
473 return 0;
474 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700475 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700476 break;
477 case Opt_umask:
478 if (match_octal(args, &option))
479 return 0;
480 uopt->umask = option;
481 break;
482 case Opt_nostrict:
483 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
484 break;
485 case Opt_session:
486 if (match_int(args, &option))
487 return 0;
488 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800489 if (!remount)
490 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491 break;
492 case Opt_lastblock:
493 if (match_int(args, &option))
494 return 0;
495 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800496 if (!remount)
497 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700498 break;
499 case Opt_anchor:
500 if (match_int(args, &option))
501 return 0;
502 uopt->anchor = option;
503 break;
504 case Opt_volume:
505 if (match_int(args, &option))
506 return 0;
507 uopt->volume = option;
508 break;
509 case Opt_partition:
510 if (match_int(args, &option))
511 return 0;
512 uopt->partition = option;
513 break;
514 case Opt_fileset:
515 if (match_int(args, &option))
516 return 0;
517 uopt->fileset = option;
518 break;
519 case Opt_rootdir:
520 if (match_int(args, &option))
521 return 0;
522 uopt->rootdir = option;
523 break;
524 case Opt_utf8:
525 uopt->flags |= (1 << UDF_FLAG_UTF8);
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700528 case Opt_iocharset:
529 uopt->nls_map = load_nls(args[0].from);
530 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700533 case Opt_uignore:
534 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
535 break;
536 case Opt_uforget:
537 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
538 break;
539 case Opt_gignore:
540 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
541 break;
542 case Opt_gforget:
543 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
544 break;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100545 case Opt_fmode:
546 if (match_octal(args, &option))
547 return 0;
548 uopt->fmode = option & 0777;
549 break;
550 case Opt_dmode:
551 if (match_octal(args, &option))
552 return 0;
553 uopt->dmode = option & 0777;
554 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 default:
556 printk(KERN_ERR "udf: bad mount option \"%s\" "
557 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559 }
560 }
561 return 1;
562}
563
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800564static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!(sb->s_flags & MS_RDONLY))
569 udf_open_lvid(sb);
570 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unlock_kernel();
573}
574
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700575static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800578 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800580 uopt.flags = sbi->s_flags;
581 uopt.uid = sbi->s_uid;
582 uopt.gid = sbi->s_gid;
583 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100584 uopt.fmode = sbi->s_fmode;
585 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Miklos Szeredi6da80892008-02-08 04:21:50 -0800587 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return -EINVAL;
589
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800590 sbi->s_flags = uopt.flags;
591 sbi->s_uid = uopt.uid;
592 sbi->s_gid = uopt.gid;
593 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100594 sbi->s_fmode = uopt.fmode;
595 sbi->s_dmode = uopt.dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800597 if (sbi->s_lvid_bh) {
598 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (write_rev > UDF_MAX_WRITE_VERSION)
600 *flags |= MS_RDONLY;
601 }
602
603 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
604 return 0;
605 if (*flags & MS_RDONLY)
606 udf_close_lvid(sb);
607 else
608 udf_open_lvid(sb);
609
610 return 0;
611}
612
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200616 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 int sectorsize;
618 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700619 int iso9660 = 0;
620 int nsr02 = 0;
621 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800622 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Block size must be a multiple of 512 */
625 if (sb->s_blocksize & 511)
626 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800627 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (sb->s_blocksize < sizeof(struct volStructDesc))
630 sectorsize = sizeof(struct volStructDesc);
631 else
632 sectorsize = sb->s_blocksize;
633
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800634 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200637 (unsigned int)(sector >> sb->s_blocksize_bits),
638 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /* Read a block */
642 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
643 if (!bh)
644 break;
645
646 /* Look for ISO descriptors */
647 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800648 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700651 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800653 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
654 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700656 switch (vsd->structType) {
657 case 0:
658 udf_debug("ISO9660 Boot Record found\n");
659 break;
660 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 udf_debug("ISO9660 Primary Volume Descriptor "
662 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 break;
664 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 udf_debug("ISO9660 Supplementary Volume "
666 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700667 break;
668 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800669 udf_debug("ISO9660 Volume Partition Descriptor "
670 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700671 break;
672 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800673 udf_debug("ISO9660 Volume Descriptor Set "
674 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700675 break;
676 default:
677 udf_debug("ISO9660 VRS (%u) found\n",
678 vsd->structType);
679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800681 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
682 VSD_STD_ID_LEN))
683 ; /* nothing */
684 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
685 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700686 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800688 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
689 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800691 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
692 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700694 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 if (nsr03)
698 return nsr03;
699 else if (nsr02)
700 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800701 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -1;
703 else
704 return 0;
705}
706
707/*
Jan Kara423cf6d2008-04-07 15:59:23 +0200708 * Check whether there is an anchor block in the given block
709 */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200710static int udf_check_anchor_block(struct super_block *sb, sector_t block)
Jan Kara423cf6d2008-04-07 15:59:23 +0200711{
Tomas Janouseke8183c22008-06-23 15:12:35 +0200712 struct buffer_head *bh;
Jan Kara423cf6d2008-04-07 15:59:23 +0200713 uint16_t ident;
Jan Kara423cf6d2008-04-07 15:59:23 +0200714
Tomas Janouseke8183c22008-06-23 15:12:35 +0200715 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
716 udf_fixed_to_variable(block) >=
717 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
718 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200719
Tomas Janouseke8183c22008-06-23 15:12:35 +0200720 bh = udf_read_tagged(sb, block, block, &ident);
Jan Kara423cf6d2008-04-07 15:59:23 +0200721 if (!bh)
722 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200723 brelse(bh);
Tomas Janouseke8183c22008-06-23 15:12:35 +0200724
725 return ident == TAG_IDENT_AVDP;
Jan Kara423cf6d2008-04-07 15:59:23 +0200726}
727
728/* Search for an anchor volume descriptor pointer */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200729static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200730{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200731 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200732 int i;
733 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Karaf90981f2008-12-03 17:31:39 +0100734 int last_count = 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200735
Jan Karaf90981f2008-12-03 17:31:39 +0100736 last[last_count++] = lastblock;
737 if (lastblock >= 1)
738 last[last_count++] = lastblock - 1;
739 last[last_count++] = lastblock + 1;
740 if (lastblock >= 2)
741 last[last_count++] = lastblock - 2;
742 if (lastblock >= 150)
743 last[last_count++] = lastblock - 150;
744 if (lastblock >= 152)
745 last[last_count++] = lastblock - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200746
747 /* according to spec, anchor is in either:
748 * block 256
749 * lastblock-256
750 * lastblock
751 * however, if the disc isn't closed, it could be 512 */
752
Jan Karaf90981f2008-12-03 17:31:39 +0100753 for (i = 0; i < last_count; i++) {
Jan Kara5fb28aa2008-04-07 16:15:04 +0200754 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
755 sb->s_blocksize_bits)
756 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200757
Tomas Janouseke8183c22008-06-23 15:12:35 +0200758 if (udf_check_anchor_block(sb, last[i])) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200759 sbi->s_anchor[0] = last[i];
760 sbi->s_anchor[1] = last[i] - 256;
761 return last[i];
762 }
763
764 if (last[i] < 256)
765 continue;
766
Tomas Janouseke8183c22008-06-23 15:12:35 +0200767 if (udf_check_anchor_block(sb, last[i] - 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200768 sbi->s_anchor[1] = last[i] - 256;
769 return last[i];
770 }
771 }
772
Tomas Janouseke8183c22008-06-23 15:12:35 +0200773 if (udf_check_anchor_block(sb, sbi->s_session + 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200774 sbi->s_anchor[0] = sbi->s_session + 256;
775 return last[0];
776 }
Tomas Janouseke8183c22008-06-23 15:12:35 +0200777 if (udf_check_anchor_block(sb, sbi->s_session + 512)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200778 sbi->s_anchor[0] = sbi->s_session + 512;
779 return last[0];
780 }
781 return 0;
782}
783
784/*
785 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
786 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200788 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 */
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100791static int udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Jan Kara423cf6d2008-04-07 15:59:23 +0200793 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct buffer_head *bh = NULL;
795 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 int i;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100797 int anchor_found = 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200798 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800799
Tomas Janouseke8183c22008-06-23 15:12:35 +0200800 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
Jan Kara423cf6d2008-04-07 15:59:23 +0200801 if (lastblock)
802 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jan Kara423cf6d2008-04-07 15:59:23 +0200804 /* No anchor found? Try VARCONV conversion of block numbers */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200805 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara423cf6d2008-04-07 15:59:23 +0200806 /* Firstly, we try to not convert number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200807 lastblock = udf_scan_anchors(sb,
Jan Kara423cf6d2008-04-07 15:59:23 +0200808 udf_variable_to_fixed(sbi->s_last_block));
Tomas Janouseke8183c22008-06-23 15:12:35 +0200809 if (lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200810 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Jan Kara423cf6d2008-04-07 15:59:23 +0200812 /* Secondly, we try with converted number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200813 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
814 if (!lastblock) {
815 /* VARCONV didn't help. Clear it. */
816 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jan Kara423cf6d2008-04-07 15:59:23 +0200819check_anchor:
820 /*
821 * Check located anchors and the anchor block supplied via
822 * mount options
823 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800824 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100825 if (!sbi->s_anchor[i])
826 continue;
827 bh = udf_read_tagged(sb, sbi->s_anchor[i],
828 sbi->s_anchor[i], &ident);
829 if (!bh)
830 sbi->s_anchor[i] = 0;
831 else {
832 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200833 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 sbi->s_anchor[i] = 0;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100835 else
836 anchor_found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 }
839
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 sbi->s_last_block = lastblock;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100841 return anchor_found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800844static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200845 struct kernel_lb_addr *fileset,
846 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct buffer_head *bh = NULL;
849 long lastblock;
850 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800851 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200855 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700857 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700859 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700860 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 1;
862 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800866 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800867 if (!bh) {
868 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200869 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700871/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700873
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800874 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875 (newfileset.partitionReferenceNum != 0xFFFF &&
876 fileset->logicalBlockNum == 0xFFFFFFFF &&
877 fileset->partitionReferenceNum == 0xFFFF);
878 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800879 lastblock = sbi->s_partmaps
880 [newfileset.partitionReferenceNum]
881 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 newfileset.logicalBlockNum = 0;
883
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200885 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800886 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 if (!bh) {
888 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 continue;
890 }
891
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700892 switch (ident) {
893 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700894 {
895 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800896 sp = (struct spaceBitmapDesc *)
897 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700898 newfileset.logicalBlockNum += 1 +
899 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800900 sizeof(struct spaceBitmapDesc)
901 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700902 brelse(bh);
903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700905 case TAG_IDENT_FSD:
906 *fileset = newfileset;
907 break;
908 default:
909 newfileset.logicalBlockNum++;
910 brelse(bh);
911 bh = NULL;
912 break;
913 }
914 } while (newfileset.logicalBlockNum < lastblock &&
915 fileset->logicalBlockNum == 0xFFFFFFFF &&
916 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919
920 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700921 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700923 fileset->logicalBlockNum,
924 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800926 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700928 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930 }
931 return 1;
932}
933
Jan Karac0eb31e2008-04-01 16:50:35 +0200934static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100937 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200938 struct buffer_head *bh;
939 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100940 int ret = 1;
941
942 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
943 if (!instr)
944 return 1;
945
946 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
947 if (!outstr)
948 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200949
950 bh = udf_read_tagged(sb, block, block, &ident);
951 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100952 goto out2;
953
Jan Karac0eb31e2008-04-01 16:50:35 +0200954 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 pvoldesc = (struct primaryVolDesc *)bh->b_data;
957
Marcin Slusarz56774802008-02-10 11:25:31 +0100958 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
959 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100960#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200961 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100962 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800963 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100964 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
965 ts->minute, le16_to_cpu(ts->typeAndTimezone));
966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100969 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
970 if (udf_CS0toUTF8(outstr, instr)) {
971 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
972 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800973 udf_debug("volIdent[] = '%s'\n",
974 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100977 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
978 if (udf_CS0toUTF8(outstr, instr))
979 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200980
981 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100982 ret = 0;
983out2:
984 kfree(outstr);
985out1:
986 kfree(instr);
987 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Jan Karabfb257a2008-04-08 20:37:21 +0200990static int udf_load_metadata_files(struct super_block *sb, int partition)
991{
992 struct udf_sb_info *sbi = UDF_SB(sb);
993 struct udf_part_map *map;
994 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200995 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200996 int fe_error = 0;
997
998 map = &sbi->s_partmaps[partition];
999 mdata = &map->s_type_specific.s_metadata;
1000
1001 /* metadata address */
1002 addr.logicalBlockNum = mdata->s_meta_file_loc;
1003 addr.partitionReferenceNum = map->s_partition_num;
1004
1005 udf_debug("Metadata file location: block = %d part = %d\n",
1006 addr.logicalBlockNum, addr.partitionReferenceNum);
1007
Pekka Enberg97e961f2008-10-15 12:29:03 +02001008 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001009
1010 if (mdata->s_metadata_fe == NULL) {
1011 udf_warning(sb, __func__, "metadata inode efe not found, "
1012 "will try mirror inode.");
1013 fe_error = 1;
1014 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
1015 ICBTAG_FLAG_AD_SHORT) {
1016 udf_warning(sb, __func__, "metadata inode efe does not have "
1017 "short allocation descriptors!");
1018 fe_error = 1;
1019 iput(mdata->s_metadata_fe);
1020 mdata->s_metadata_fe = NULL;
1021 }
1022
1023 /* mirror file entry */
1024 addr.logicalBlockNum = mdata->s_mirror_file_loc;
1025 addr.partitionReferenceNum = map->s_partition_num;
1026
1027 udf_debug("Mirror metadata file location: block = %d part = %d\n",
1028 addr.logicalBlockNum, addr.partitionReferenceNum);
1029
Pekka Enberg97e961f2008-10-15 12:29:03 +02001030 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001031
1032 if (mdata->s_mirror_fe == NULL) {
1033 if (fe_error) {
1034 udf_error(sb, __func__, "mirror inode efe not found "
1035 "and metadata inode is missing too, exiting...");
1036 goto error_exit;
1037 } else
1038 udf_warning(sb, __func__, "mirror inode efe not found,"
1039 " but metadata inode is OK");
1040 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1041 ICBTAG_FLAG_AD_SHORT) {
1042 udf_warning(sb, __func__, "mirror inode efe does not have "
1043 "short allocation descriptors!");
1044 iput(mdata->s_mirror_fe);
1045 mdata->s_mirror_fe = NULL;
1046 if (fe_error)
1047 goto error_exit;
1048 }
1049
1050 /*
1051 * bitmap file entry
1052 * Note:
1053 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1054 */
1055 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1056 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1057 addr.partitionReferenceNum = map->s_partition_num;
1058
1059 udf_debug("Bitmap file location: block = %d part = %d\n",
1060 addr.logicalBlockNum, addr.partitionReferenceNum);
1061
Pekka Enberg97e961f2008-10-15 12:29:03 +02001062 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001063
1064 if (mdata->s_bitmap_fe == NULL) {
1065 if (sb->s_flags & MS_RDONLY)
1066 udf_warning(sb, __func__, "bitmap inode efe "
1067 "not found but it's ok since the disc"
1068 " is mounted read-only");
1069 else {
1070 udf_error(sb, __func__, "bitmap inode efe not "
1071 "found and attempted read-write mount");
1072 goto error_exit;
1073 }
1074 }
1075 }
1076
1077 udf_debug("udf_load_metadata_files Ok\n");
1078
1079 return 0;
1080
1081error_exit:
1082 return 1;
1083}
1084
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001085static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001086 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088 struct fileSetDesc *fset;
1089
1090 fset = (struct fileSetDesc *)bh->b_data;
1091
1092 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1093
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001094 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001096 udf_debug("Rootdir at block=%d, partition=%d\n",
1097 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001100int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1101{
1102 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001103 return DIV_ROUND_UP(map->s_partition_len +
1104 (sizeof(struct spaceBitmapDesc) << 3),
1105 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001106}
1107
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001108static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1109{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001110 struct udf_bitmap *bitmap;
1111 int nr_groups;
1112 int size;
1113
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001114 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001115 size = sizeof(struct udf_bitmap) +
1116 (sizeof(struct buffer_head *) * nr_groups);
1117
1118 if (size <= PAGE_SIZE)
1119 bitmap = kmalloc(size, GFP_KERNEL);
1120 else
1121 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1122
1123 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001124 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001125 "Unable to allocate space for bitmap "
1126 "and %d buffer_head pointers", nr_groups);
1127 return NULL;
1128 }
1129
1130 memset(bitmap, 0x00, size);
1131 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1132 bitmap->s_nr_groups = nr_groups;
1133 return bitmap;
1134}
1135
Jan Kara3fb38df2008-04-02 12:26:36 +02001136static int udf_fill_partdesc_info(struct super_block *sb,
1137 struct partitionDesc *p, int p_index)
1138{
1139 struct udf_part_map *map;
1140 struct udf_sb_info *sbi = UDF_SB(sb);
1141 struct partitionHeaderDesc *phd;
1142
1143 map = &sbi->s_partmaps[p_index];
1144
1145 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1146 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1147
1148 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1149 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1150 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1151 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1152 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1153 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1154 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1155 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1156
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001157 udf_debug("Partition (%d type %x) starts at physical %d, "
1158 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001159 map->s_partition_type, map->s_partition_root,
1160 map->s_partition_len);
1161
1162 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1163 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1164 return 0;
1165
1166 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1167 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001168 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001169 .logicalBlockNum = le32_to_cpu(
1170 phd->unallocSpaceTable.extPosition),
1171 .partitionReferenceNum = p_index,
1172 };
1173
Pekka Enberg97e961f2008-10-15 12:29:03 +02001174 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001175 if (!map->s_uspace.s_table) {
1176 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1177 p_index);
1178 return 1;
1179 }
1180 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1181 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1182 p_index, map->s_uspace.s_table->i_ino);
1183 }
1184
1185 if (phd->unallocSpaceBitmap.extLength) {
1186 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1187 if (!bitmap)
1188 return 1;
1189 map->s_uspace.s_bitmap = bitmap;
1190 bitmap->s_extLength = le32_to_cpu(
1191 phd->unallocSpaceBitmap.extLength);
1192 bitmap->s_extPosition = le32_to_cpu(
1193 phd->unallocSpaceBitmap.extPosition);
1194 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1195 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1196 bitmap->s_extPosition);
1197 }
1198
1199 if (phd->partitionIntegrityTable.extLength)
1200 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1201
1202 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001203 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001204 .logicalBlockNum = le32_to_cpu(
1205 phd->freedSpaceTable.extPosition),
1206 .partitionReferenceNum = p_index,
1207 };
1208
Pekka Enberg97e961f2008-10-15 12:29:03 +02001209 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001210 if (!map->s_fspace.s_table) {
1211 udf_debug("cannot load freedSpaceTable (part %d)\n",
1212 p_index);
1213 return 1;
1214 }
1215
1216 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1217 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1218 p_index, map->s_fspace.s_table->i_ino);
1219 }
1220
1221 if (phd->freedSpaceBitmap.extLength) {
1222 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1223 if (!bitmap)
1224 return 1;
1225 map->s_fspace.s_bitmap = bitmap;
1226 bitmap->s_extLength = le32_to_cpu(
1227 phd->freedSpaceBitmap.extLength);
1228 bitmap->s_extPosition = le32_to_cpu(
1229 phd->freedSpaceBitmap.extPosition);
1230 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1231 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1232 bitmap->s_extPosition);
1233 }
1234 return 0;
1235}
1236
Jan Kara38b74a52008-04-02 16:01:35 +02001237static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1238{
1239 struct udf_sb_info *sbi = UDF_SB(sb);
1240 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001241 struct kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001242 struct buffer_head *bh = NULL;
1243 struct udf_inode_info *vati;
1244 uint32_t pos;
1245 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001246
1247 /* VAT file entry is in the last recorded block */
1248 ino.partitionReferenceNum = type1_index;
1249 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
Pekka Enberg97e961f2008-10-15 12:29:03 +02001250 sbi->s_vat_inode = udf_iget(sb, &ino);
Jan Kara38b74a52008-04-02 16:01:35 +02001251 if (!sbi->s_vat_inode)
1252 return 1;
1253
1254 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001255 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001256 map->s_type_specific.s_virtual.s_num_entries =
1257 (sbi->s_vat_inode->i_size - 36) >> 2;
1258 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001259 vati = UDF_I(sbi->s_vat_inode);
1260 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1261 pos = udf_block_map(sbi->s_vat_inode, 0);
1262 bh = sb_bread(sb, pos);
1263 if (!bh)
1264 return 1;
1265 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1266 } else {
1267 vat20 = (struct virtualAllocationTable20 *)
1268 vati->i_ext.i_data;
1269 }
Jan Kara38b74a52008-04-02 16:01:35 +02001270
Jan Kara38b74a52008-04-02 16:01:35 +02001271 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001272 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001273 map->s_type_specific.s_virtual.s_num_entries =
1274 (sbi->s_vat_inode->i_size -
1275 map->s_type_specific.s_virtual.
1276 s_start_offset) >> 2;
1277 brelse(bh);
1278 }
1279 return 0;
1280}
1281
Jan Karac0eb31e2008-04-01 16:50:35 +02001282static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Jan Karac0eb31e2008-04-01 16:50:35 +02001284 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001285 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001286 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001287 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001288 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001289 uint16_t partitionNumber;
1290 uint16_t ident;
1291 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Jan Karac0eb31e2008-04-01 16:50:35 +02001293 bh = udf_read_tagged(sb, block, block, &ident);
1294 if (!bh)
1295 return 1;
1296 if (ident != TAG_IDENT_PD)
1297 goto out_bh;
1298
1299 p = (struct partitionDesc *)bh->b_data;
1300 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001301
Jan Karabfb257a2008-04-08 20:37:21 +02001302 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001303 for (i = 0; i < sbi->s_partitions; i++) {
1304 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001305 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001306 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001307 if (map->s_partition_num == partitionNumber &&
1308 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1309 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001311 }
1312
Jan Kara38b74a52008-04-02 16:01:35 +02001313 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001314 udf_debug("Partition (%d) not found in partition map\n",
1315 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001316 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001317 }
1318
Jan Kara3fb38df2008-04-02 12:26:36 +02001319 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001320
1321 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001322 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1323 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001324 */
1325 type1_idx = i;
1326 for (i = 0; i < sbi->s_partitions; i++) {
1327 map = &sbi->s_partmaps[i];
1328
1329 if (map->s_partition_num == partitionNumber &&
1330 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001331 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1332 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001333 break;
1334 }
1335
1336 if (i >= sbi->s_partitions)
1337 goto out_bh;
1338
1339 ret = udf_fill_partdesc_info(sb, p, i);
1340 if (ret)
1341 goto out_bh;
1342
Jan Karabfb257a2008-04-08 20:37:21 +02001343 if (map->s_partition_type == UDF_METADATA_MAP25) {
1344 ret = udf_load_metadata_files(sb, i);
1345 if (ret) {
1346 printk(KERN_ERR "UDF-fs: error loading MetaData "
1347 "partition map %d\n", i);
1348 goto out_bh;
1349 }
1350 } else {
1351 ret = udf_load_vat(sb, i, type1_idx);
1352 if (ret)
1353 goto out_bh;
1354 /*
1355 * Mark filesystem read-only if we have a partition with
1356 * virtual map since we don't handle writing to it (we
1357 * overwrite blocks instead of relocating them).
1358 */
1359 sb->s_flags |= MS_RDONLY;
1360 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1361 "because writing to pseudooverwrite partition is "
1362 "not implemented.\n");
1363 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001364out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001365 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001366 brelse(bh);
1367 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Jan Karac0eb31e2008-04-01 16:50:35 +02001370static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001371 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct logicalVolDesc *lvd;
1374 int i, j, offset;
1375 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001376 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001377 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001378 uint16_t ident;
1379 struct buffer_head *bh;
1380 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jan Karac0eb31e2008-04-01 16:50:35 +02001382 bh = udf_read_tagged(sb, block, block, &ident);
1383 if (!bh)
1384 return 1;
1385 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 lvd = (struct logicalVolDesc *)bh->b_data;
1387
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001388 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001389 if (i != 0) {
1390 ret = i;
1391 goto out_bh;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001394 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001395 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001396 i++, offset += gpm->partitionMapLength) {
1397 struct udf_part_map *map = &sbi->s_partmaps[i];
1398 gpm = (struct genericPartitionMap *)
1399 &(lvd->partitionMaps[offset]);
1400 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001401 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001402 struct genericPartitionMap1 *gpm1 =
1403 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001404 map->s_partition_type = UDF_TYPE1_MAP15;
1405 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1406 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1407 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001408 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001409 struct udfPartitionMap2 *upm2 =
1410 (struct udfPartitionMap2 *)gpm;
1411 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1412 strlen(UDF_ID_VIRTUAL))) {
1413 u16 suf =
1414 le16_to_cpu(((__le16 *)upm2->partIdent.
1415 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001416 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001417 map->s_partition_type =
1418 UDF_VIRTUAL_MAP15;
1419 map->s_partition_func =
1420 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001421 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001422 map->s_partition_type =
1423 UDF_VIRTUAL_MAP20;
1424 map->s_partition_func =
1425 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001427 } else if (!strncmp(upm2->partIdent.ident,
1428 UDF_ID_SPARABLE,
1429 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001432 struct sparablePartitionMap *spm =
1433 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001435 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001436 map->s_type_specific.s_sparing.s_packet_len =
1437 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001438 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001439 struct buffer_head *bh2;
1440
1441 loc = le32_to_cpu(
1442 spm->locSparingTable[j]);
1443 bh2 = udf_read_tagged(sb, loc, loc,
1444 &ident);
1445 map->s_type_specific.s_sparing.
1446 s_spar_map[j] = bh2;
1447
Marcin Slusarz165923f2008-02-10 11:33:08 +01001448 if (bh2 == NULL)
1449 continue;
1450
1451 st = (struct sparingTable *)bh2->b_data;
1452 if (ident != 0 || strncmp(
1453 st->sparingIdent.ident,
1454 UDF_ID_SPARING,
1455 strlen(UDF_ID_SPARING))) {
1456 brelse(bh2);
1457 map->s_type_specific.s_sparing.
1458 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001461 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001462 } else if (!strncmp(upm2->partIdent.ident,
1463 UDF_ID_METADATA,
1464 strlen(UDF_ID_METADATA))) {
1465 struct udf_meta_data *mdata =
1466 &map->s_type_specific.s_metadata;
1467 struct metadataPartitionMap *mdm =
1468 (struct metadataPartitionMap *)
1469 &(lvd->partitionMaps[offset]);
1470 udf_debug("Parsing Logical vol part %d "
1471 "type %d id=%s\n", i, type,
1472 UDF_ID_METADATA);
1473
1474 map->s_partition_type = UDF_METADATA_MAP25;
1475 map->s_partition_func = udf_get_pblock_meta25;
1476
1477 mdata->s_meta_file_loc =
1478 le32_to_cpu(mdm->metadataFileLoc);
1479 mdata->s_mirror_file_loc =
1480 le32_to_cpu(mdm->metadataMirrorFileLoc);
1481 mdata->s_bitmap_file_loc =
1482 le32_to_cpu(mdm->metadataBitmapFileLoc);
1483 mdata->s_alloc_unit_size =
1484 le32_to_cpu(mdm->allocUnitSize);
1485 mdata->s_align_unit_size =
1486 le16_to_cpu(mdm->alignUnitSize);
1487 mdata->s_dup_md_flag =
1488 mdm->flags & 0x01;
1489
1490 udf_debug("Metadata Ident suffix=0x%x\n",
1491 (le16_to_cpu(
1492 ((__le16 *)
1493 mdm->partIdent.identSuffix)[0])));
1494 udf_debug("Metadata part num=%d\n",
1495 le16_to_cpu(mdm->partitionNum));
1496 udf_debug("Metadata part alloc unit size=%d\n",
1497 le32_to_cpu(mdm->allocUnitSize));
1498 udf_debug("Metadata file loc=%d\n",
1499 le32_to_cpu(mdm->metadataFileLoc));
1500 udf_debug("Mirror file loc=%d\n",
1501 le32_to_cpu(mdm->metadataMirrorFileLoc));
1502 udf_debug("Bitmap file loc=%d\n",
1503 le32_to_cpu(mdm->metadataBitmapFileLoc));
1504 udf_debug("Duplicate Flag: %d %d\n",
1505 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001506 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001507 udf_debug("Unknown ident: %s\n",
1508 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 continue;
1510 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001511 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1512 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001515 i, map->s_partition_num, type,
1516 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001519 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001520 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001523 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1524 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001525 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 if (lvd->integritySeqExt.extLength)
1528 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001529
Jan Karac0eb31e2008-04-01 16:50:35 +02001530out_bh:
1531 brelse(bh);
1532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
1536 * udf_load_logicalvolint
1537 *
1538 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001539static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct buffer_head *bh = NULL;
1542 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001543 struct udf_sb_info *sbi = UDF_SB(sb);
1544 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001547 (bh = udf_read_tagged(sb, loc.extLocation,
1548 loc.extLocation, &ident)) &&
1549 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001550 sbi->s_lvid_bh = bh;
1551 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001552
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001554 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001555 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001557 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001558 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001562 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001563 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * udf_process_sequence
1568 *
1569 * PURPOSE
1570 * Process a main/reserve volume descriptor sequence.
1571 *
1572 * PRE-CONDITIONS
1573 * sb Pointer to _locked_ superblock.
1574 * block First block of first extent of the sequence.
1575 * lastblock Lastblock of first extent of the sequence.
1576 *
1577 * HISTORY
1578 * July 1, 1997 - Andrew E. Mileski
1579 * Written, tested, and released.
1580 */
Jan Kara200a3592008-03-04 13:03:09 +01001581static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001582 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct buffer_head *bh = NULL;
1585 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001586 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 struct generic_desc *gd;
1588 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001589 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 uint32_t vdsn;
1591 uint16_t ident;
1592 long next_s = 0, next_e = 0;
1593
1594 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1595
Jan Karac0eb31e2008-04-01 16:50:35 +02001596 /*
1597 * Read the main descriptor sequence and find which descriptors
1598 * are in it.
1599 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001603 if (!bh) {
1604 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1605 "sequence is corrupted or we could not read "
1606 "it.\n", (unsigned long long)block);
1607 return 1;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1611 gd = (struct generic_desc *)bh->b_data;
1612 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001613 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001614 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001615 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1616 if (vdsn >= curr->volDescSeqNum) {
1617 curr->volDescSeqNum = vdsn;
1618 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001619 }
1620 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001621 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001622 curr = &vds[VDS_POS_VOL_DESC_PTR];
1623 if (vdsn >= curr->volDescSeqNum) {
1624 curr->volDescSeqNum = vdsn;
1625 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001627 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001628 next_s = le32_to_cpu(
1629 vdp->nextVolDescSeqExt.extLocation);
1630 next_e = le32_to_cpu(
1631 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001632 next_e = next_e >> sb->s_blocksize_bits;
1633 next_e += next_s;
1634 }
1635 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001636 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001637 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1638 if (vdsn >= curr->volDescSeqNum) {
1639 curr->volDescSeqNum = vdsn;
1640 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001641 }
1642 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001643 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001644 curr = &vds[VDS_POS_PARTITION_DESC];
1645 if (!curr->block)
1646 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001647 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001648 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001649 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1650 if (vdsn >= curr->volDescSeqNum) {
1651 curr->volDescSeqNum = vdsn;
1652 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001653 }
1654 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001655 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001656 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1657 if (vdsn >= curr->volDescSeqNum) {
1658 curr->volDescSeqNum = vdsn;
1659 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001660 }
1661 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001662 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001663 vds[VDS_POS_TERMINATING_DESC].block = block;
1664 if (next_e) {
1665 block = next_s;
1666 lastblock = next_e;
1667 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001668 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001669 done = 1;
1670 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001672 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001674 /*
1675 * Now read interesting descriptors again and process them
1676 * in a suitable order
1677 */
1678 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1679 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1680 return 1;
1681 }
1682 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1683 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Jan Karac0eb31e2008-04-01 16:50:35 +02001685 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1686 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1687 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001688
Jan Karac0eb31e2008-04-01 16:50:35 +02001689 if (vds[VDS_POS_PARTITION_DESC].block) {
1690 /*
1691 * We rescan the whole descriptor sequence to find
1692 * partition descriptor blocks and process them.
1693 */
1694 for (block = vds[VDS_POS_PARTITION_DESC].block;
1695 block < vds[VDS_POS_TERMINATING_DESC].block;
1696 block++)
1697 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001698 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
1701 return 0;
1702}
1703
1704/*
1705 * udf_check_valid()
1706 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001710 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001712 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 udf_debug("Validity check skipped because of novrs option\n");
1714 return 0;
1715 }
1716 /* Check that it is NSR02 compliant */
1717 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001718 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001719 if (block == -1)
1720 udf_debug("Failed to read byte 32768. Assuming open "
1721 "disc. Skipping validity check\n");
1722 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001723 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001724 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001727static int udf_check_volume(struct super_block *sb,
1728 struct udf_options *uopt, int silent)
1729{
1730 struct udf_sb_info *sbi = UDF_SB(sb);
1731
1732 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1733 if (!silent)
1734 printk(KERN_WARNING "UDF-fs: Bad block size\n");
1735 return 0;
1736 }
1737 sbi->s_last_block = uopt->lastblock;
1738 if (udf_check_valid(sb, uopt->novrs, silent)) {
1739 if (!silent)
1740 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1741 return 0;
1742 }
1743 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1744 sbi->s_anchor[2] = uopt->anchor;
1745 if (!udf_find_anchor(sb)) {
1746 if (!silent)
1747 printk(KERN_WARNING "UDF-fs: No anchor found\n");
1748 return 0;
1749 }
1750 return 1;
1751}
1752
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001753static int udf_load_sequence(struct super_block *sb, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
1755 struct anchorVolDescPtr *anchor;
1756 uint16_t ident;
1757 struct buffer_head *bh;
1758 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001759 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001760 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 if (!sb)
1763 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001764 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001766 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001767 if (!sbi->s_anchor[i])
1768 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001769
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001770 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1771 &ident);
1772 if (!bh)
1773 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001775 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001776
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001777 /* Locate the main sequence */
1778 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1779 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1780 main_e = main_e >> sb->s_blocksize_bits;
1781 main_e += main_s;
1782
1783 /* Locate the reserve sequence */
1784 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001785 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001786 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001787 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001788 reserve_e = reserve_e >> sb->s_blocksize_bits;
1789 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001791 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001793 /* Process the main & reserve sequences */
1794 /* responsible for finding the PartitionDesc(s) */
1795 if (!(udf_process_sequence(sb, main_s, main_e,
1796 fileset) &&
1797 udf_process_sequence(sb, reserve_s, reserve_e,
1798 fileset)))
1799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
1801
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001802 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 udf_debug("No Anchor block found\n");
1804 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001805 }
1806 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return 0;
1809}
1810
1811static void udf_open_lvid(struct super_block *sb)
1812{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001813 struct udf_sb_info *sbi = UDF_SB(sb);
1814 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001815 struct logicalVolIntegrityDesc *lvid;
1816 struct logicalVolIntegrityDescImpUse *lvidiu;
1817 if (!bh)
1818 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Marcin Slusarz165923f2008-02-10 11:33:08 +01001820 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1821 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Marcin Slusarz165923f2008-02-10 11:33:08 +01001823 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1824 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1825 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1826 CURRENT_TIME);
1827 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Marcin Slusarz165923f2008-02-10 11:33:08 +01001829 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001830 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001831 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001832
1833 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1834 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837static void udf_close_lvid(struct super_block *sb)
1838{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001839 struct udf_sb_info *sbi = UDF_SB(sb);
1840 struct buffer_head *bh = sbi->s_lvid_bh;
1841 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001842 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001843
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001844 if (!bh)
1845 return;
1846
1847 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1848
Marcin Slusarz165923f2008-02-10 11:33:08 +01001849 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1850 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Marcin Slusarz165923f2008-02-10 11:33:08 +01001852 lvidiu = udf_sb_lvidiu(sbi);
1853 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1854 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1855 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1856 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1857 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1858 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1859 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1860 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1861 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1862 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Marcin Slusarz165923f2008-02-10 11:33:08 +01001864 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001865 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001866 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001867
1868 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1869 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870}
1871
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001872static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1873{
1874 int i;
1875 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001876 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1877 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001878
1879 for (i = 0; i < nr_groups; i++)
1880 if (bitmap->s_block_bitmap[i])
1881 brelse(bitmap->s_block_bitmap[i]);
1882
1883 if (size <= PAGE_SIZE)
1884 kfree(bitmap);
1885 else
1886 vfree(bitmap);
1887}
1888
Jan Kara2e0838f2008-04-01 18:08:51 +02001889static void udf_free_partition(struct udf_part_map *map)
1890{
1891 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001892 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001893
1894 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1895 iput(map->s_uspace.s_table);
1896 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1897 iput(map->s_fspace.s_table);
1898 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1899 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1900 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1901 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1902 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1903 for (i = 0; i < 4; i++)
1904 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001905 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1906 mdata = &map->s_type_specific.s_metadata;
1907 iput(mdata->s_metadata_fe);
1908 mdata->s_metadata_fe = NULL;
1909
1910 iput(mdata->s_mirror_fe);
1911 mdata->s_mirror_fe = NULL;
1912
1913 iput(mdata->s_bitmap_fe);
1914 mdata->s_bitmap_fe = NULL;
1915 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001916}
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static int udf_fill_super(struct super_block *sb, void *options, int silent)
1919{
1920 int i;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001921 int found_anchor;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001922 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001924 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 struct udf_sb_info *sbi;
1926
1927 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1928 uopt.uid = -1;
1929 uopt.gid = -1;
1930 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001931 uopt.fmode = UDF_INVALID_MODE;
1932 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001934 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (!sbi)
1936 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001940 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Miklos Szeredi6da80892008-02-08 04:21:50 -08001942 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 goto error_out;
1944
1945 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001946 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001948 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto error_out;
1950 }
1951#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001952 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 uopt.nls_map = load_nls_default();
1954 if (!uopt.nls_map)
1955 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1956 else
1957 udf_debug("Using default NLS map\n");
1958 }
1959#endif
1960 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1961 uopt.flags |= (1 << UDF_FLAG_UTF8);
1962
1963 fileset.logicalBlockNum = 0xFFFFFFFF;
1964 fileset.partitionReferenceNum = 0xFFFF;
1965
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001966 sbi->s_flags = uopt.flags;
1967 sbi->s_uid = uopt.uid;
1968 sbi->s_gid = uopt.gid;
1969 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001970 sbi->s_fmode = uopt.fmode;
1971 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001972 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001974 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001975 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001977 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001979 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001981 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
1982 found_anchor = udf_check_volume(sb, &uopt, silent);
1983 } else {
1984 uopt.blocksize = bdev_hardsect_size(sb->s_bdev);
1985 found_anchor = udf_check_volume(sb, &uopt, silent);
1986 if (!found_anchor && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
1987 if (!silent)
1988 printk(KERN_NOTICE
1989 "UDF-fs: Rescanning with blocksize "
1990 "%d\n", UDF_DEFAULT_BLOCKSIZE);
1991 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
1992 found_anchor = udf_check_volume(sb, &uopt, silent);
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001995 if (!found_anchor)
1996 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Fill in the rest of the superblock */
1999 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02002000 sb->s_export_op = &udf_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 sb->dq_op = NULL;
2002 sb->s_dirt = 0;
2003 sb->s_magic = UDF_SUPER_MAGIC;
2004 sb->s_time_gran = 1000;
2005
Jan Kara38b74a52008-04-02 16:01:35 +02002006 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002007 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto error_out;
2009 }
2010
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002011 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002013 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002014 struct logicalVolIntegrityDescImpUse *lvidiu =
2015 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002016 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2017 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002018 /* uint16_t maxUDFWriteRev =
2019 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002022 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
2023 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002024 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002025 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002027 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002030 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2033 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2034 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2035 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2036 }
2037
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002038 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002039 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto error_out;
2041 }
2042
Marcin Slusarz4b111112008-02-08 04:20:36 -08002043 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2044 UDF_PART_FLAG_READ_ONLY) {
2045 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
2046 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002047 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002048 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002049
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002050 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002051 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 goto error_out;
2053 }
2054
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002055 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002056 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002057 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08002058 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002059 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01002060 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2061 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 if (!(sb->s_flags & MS_RDONLY))
2064 udf_open_lvid(sb);
2065
2066 /* Assign the root inode */
2067 /* assign inodes by physical block number */
2068 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002069 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002070 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002071 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2072 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002073 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 goto error_out;
2075 }
2076
2077 /* Allocate a dentry for the root inode */
2078 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002079 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002080 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 iput(inode);
2082 goto error_out;
2083 }
Jan Kara31170b62007-05-08 00:35:21 -07002084 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return 0;
2086
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002087error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 if (sbi->s_vat_inode)
2089 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002090 if (sbi->s_partitions)
2091 for (i = 0; i < sbi->s_partitions; i++)
2092 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093#ifdef CONFIG_UDF_NLS
2094 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002095 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096#endif
2097 if (!(sb->s_flags & MS_RDONLY))
2098 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002099 brelse(sbi->s_lvid_bh);
2100
2101 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 kfree(sbi);
2103 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return -EINVAL;
2106}
2107
Adrian Bunkb8145a72008-02-17 10:19:55 +02002108static void udf_error(struct super_block *sb, const char *function,
2109 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
2111 va_list args;
2112
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002113 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /* mark sb error */
2115 sb->s_dirt = 1;
2116 }
2117 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002118 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002120 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002121 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002125 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 va_list args;
2128
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002129 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002130 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 va_end(args);
2132 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002133 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002136static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002139 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002141 sbi = UDF_SB(sb);
2142 if (sbi->s_vat_inode)
2143 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002144 if (sbi->s_partitions)
2145 for (i = 0; i < sbi->s_partitions; i++)
2146 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147#ifdef CONFIG_UDF_NLS
2148 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002149 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150#endif
2151 if (!(sb->s_flags & MS_RDONLY))
2152 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002153 brelse(sbi->s_lvid_bh);
2154 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 kfree(sb->s_fs_info);
2156 sb->s_fs_info = NULL;
2157}
2158
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002159static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
David Howells726c3342006-06-23 02:02:58 -07002161 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002162 struct udf_sb_info *sbi = UDF_SB(sb);
2163 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002164 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002165
2166 if (sbi->s_lvid_bh != NULL)
2167 lvidiu = udf_sb_lvidiu(sbi);
2168 else
2169 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 buf->f_type = UDF_SUPER_MAGIC;
2172 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002173 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 buf->f_bfree = udf_count_free(sb);
2175 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002176 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2177 le32_to_cpu(lvidiu->numDirs)) : 0)
2178 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 buf->f_ffree = buf->f_bfree;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002180 buf->f_namelen = UDF_NAME_LEN - 2;
Coly Li557f5a12009-01-20 01:36:55 +08002181 buf->f_fsid.val[0] = (u32)id;
2182 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 return 0;
2185}
2186
Marcin Slusarz4b111112008-02-08 04:20:36 -08002187static unsigned int udf_count_free_bitmap(struct super_block *sb,
2188 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct buffer_head *bh = NULL;
2191 unsigned int accum = 0;
2192 int index;
2193 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002194 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 uint8_t *ptr;
2197 uint16_t ident;
2198 struct spaceBitmapDesc *bm;
2199
2200 lock_kernel();
2201
2202 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002203 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002204 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002206 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 printk(KERN_ERR "udf: udf_count_free failed\n");
2208 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002209 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002210 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 printk(KERN_ERR "udf: udf_count_free failed\n");
2212 goto out;
2213 }
2214
2215 bm = (struct spaceBitmapDesc *)bh->b_data;
2216 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002217 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2218 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002220 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002221 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2222 accum += bitmap_weight((const unsigned long *)(ptr + index),
2223 cur_bytes * 8);
2224 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002225 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002226 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002227 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002229 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 udf_debug("read failed\n");
2231 goto out;
2232 }
2233 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002234 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002237 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002239out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 unlock_kernel();
2241
2242 return accum;
2243}
2244
Marcin Slusarz4b111112008-02-08 04:20:36 -08002245static unsigned int udf_count_free_table(struct super_block *sb,
2246 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002249 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002250 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002252 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 lock_kernel();
2255
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002256 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002257 epos.offset = sizeof(struct unallocSpaceEntry);
2258 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002260 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002262
Jan Kara3bf25cb2007-05-08 00:35:16 -07002263 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 unlock_kernel();
2266
2267 return accum;
2268}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002269
2270static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002273 struct udf_sb_info *sbi;
2274 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002276 sbi = UDF_SB(sb);
2277 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002278 struct logicalVolIntegrityDesc *lvid =
2279 (struct logicalVolIntegrityDesc *)
2280 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002281 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002282 accum = le32_to_cpu(
2283 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (accum == 0xFFFFFFFF)
2285 accum = 0;
2286 }
2287 }
2288
2289 if (accum)
2290 return accum;
2291
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002292 map = &sbi->s_partmaps[sbi->s_partition];
2293 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002294 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002295 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002297 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002298 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002299 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301 if (accum)
2302 return accum;
2303
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002304 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002305 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002306 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002308 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002309 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002310 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
2313 return accum;
2314}