blob: 9ab4e259404bbc1a7f383ccbf8ced5888bc99786 [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>
Christoph Hellwigc79d9672010-05-19 07:16:40 -040057#include <linux/quotaops.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080058#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010059#include <linux/bitmap.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020060#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/byteorder.h>
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "udf_sb.h"
64#include "udf_i.h"
65
66#include <linux/init.h>
67#include <asm/uaccess.h>
68
69#define VDS_POS_PRIMARY_VOL_DESC 0
70#define VDS_POS_UNALLOC_SPACE_DESC 1
71#define VDS_POS_LOGICAL_VOL_DESC 2
72#define VDS_POS_PARTITION_DESC 3
73#define VDS_POS_IMP_USE_VOL_DESC 4
74#define VDS_POS_VOL_DESC_PTR 5
75#define VDS_POS_TERMINATING_DESC 6
76#define VDS_POS_LENGTH 7
77
Miklos Szeredi6da80892008-02-08 04:21:50 -080078#define UDF_DEFAULT_BLOCKSIZE 2048
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static char error_buf[1024];
81
82/* These are the "meat" - everything else is stuffing */
83static int udf_fill_super(struct super_block *, void *, int);
84static void udf_put_super(struct super_block *);
Jan Kara146bca72009-03-16 18:27:37 +010085static int udf_sync_fs(struct super_block *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static int udf_remount_fs(struct super_block *, int *, char *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020087static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020088static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
89 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020091 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void udf_open_lvid(struct super_block *);
93static void udf_close_lvid(struct super_block *);
94static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070095static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080096static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020097static void udf_error(struct super_block *sb, const char *function,
98 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800100struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
101{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800102 struct logicalVolIntegrityDesc *lvid =
103 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800104 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800105 __u32 offset = number_of_partitions * 2 *
106 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800107 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700111static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700112 int flags, const char *dev_name, void *data,
113 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
David Howells454e2392006-06-23 02:02:57 -0700115 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
118static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700119 .owner = THIS_MODULE,
120 .name = "udf",
121 .get_sb = udf_get_sb,
122 .kill_sb = kill_block_super,
123 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128static struct inode *udf_alloc_inode(struct super_block *sb)
129{
130 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800131 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (!ei)
133 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700134
135 ei->i_unique = 0;
136 ei->i_lenExtents = 0;
137 ei->i_next_alloc_block = 0;
138 ei->i_next_alloc_goal = 0;
139 ei->i_strat4096 = 0;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return &ei->vfs_inode;
142}
143
144static void udf_destroy_inode(struct inode *inode)
145{
146 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
147}
148
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700149static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700151 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Christoph Lametera35afb82007-05-16 22:10:57 -0700153 ei->i_ext.i_data = NULL;
154 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static int init_inodecache(void)
158{
159 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
160 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700161 0, (SLAB_RECLAIM_ACCOUNT |
162 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900163 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700164 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -ENOMEM;
166 return 0;
167}
168
169static void destroy_inodecache(void)
170{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700171 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800175static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700176 .alloc_inode = udf_alloc_inode,
177 .destroy_inode = udf_destroy_inode,
178 .write_inode = udf_write_inode,
179 .delete_inode = udf_delete_inode,
180 .clear_inode = udf_clear_inode,
181 .put_super = udf_put_super,
Jan Kara146bca72009-03-16 18:27:37 +0100182 .sync_fs = udf_sync_fs,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700183 .statfs = udf_statfs,
184 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800185 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700188struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned char novrs;
190 unsigned int blocksize;
191 unsigned int session;
192 unsigned int lastblock;
193 unsigned int anchor;
194 unsigned int volume;
195 unsigned short partition;
196 unsigned int fileset;
197 unsigned int rootdir;
198 unsigned int flags;
199 mode_t umask;
200 gid_t gid;
201 uid_t uid;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100202 mode_t fmode;
203 mode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct nls_table *nls_map;
205};
206
207static int __init init_udf_fs(void)
208{
209 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 err = init_inodecache();
212 if (err)
213 goto out1;
214 err = register_filesystem(&udf_fstype);
215 if (err)
216 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219
220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222
223out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return err;
225}
226
227static void __exit exit_udf_fs(void)
228{
229 unregister_filesystem(&udf_fstype);
230 destroy_inodecache();
231}
232
233module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800236static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
237{
238 struct udf_sb_info *sbi = UDF_SB(sb);
239
240 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
241 GFP_KERNEL);
242 if (!sbi->s_partmaps) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700243 udf_error(sb, __func__,
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800244 "Unable to allocate space for %d partition maps",
245 count);
246 sbi->s_partitions = 0;
247 return -ENOMEM;
248 }
249
250 sbi->s_partitions = count;
251 return 0;
252}
253
Miklos Szeredi6da80892008-02-08 04:21:50 -0800254static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
255{
256 struct super_block *sb = mnt->mnt_sb;
257 struct udf_sb_info *sbi = UDF_SB(sb);
258
259 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
260 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100261 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800262 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
263 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
264 seq_puts(seq, ",unhide");
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
266 seq_puts(seq, ",undelete");
267 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
268 seq_puts(seq, ",noadinicb");
269 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
270 seq_puts(seq, ",shortad");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
272 seq_puts(seq, ",uid=forget");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
274 seq_puts(seq, ",uid=ignore");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
276 seq_puts(seq, ",gid=forget");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
278 seq_puts(seq, ",gid=ignore");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
280 seq_printf(seq, ",uid=%u", sbi->s_uid);
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
282 seq_printf(seq, ",gid=%u", sbi->s_gid);
283 if (sbi->s_umask != 0)
284 seq_printf(seq, ",umask=%o", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100285 if (sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100286 seq_printf(seq, ",mode=%o", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100287 if (sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100288 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
290 seq_printf(seq, ",session=%u", sbi->s_session);
291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
292 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara40346002009-03-19 16:21:38 +0100293 if (sbi->s_anchor != 0)
294 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800295 /*
296 * volume, partition, fileset and rootdir seem to be ignored
297 * currently
298 */
299 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
300 seq_puts(seq, ",utf8");
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
302 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
303
304 return 0;
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * udf_parse_options
309 *
310 * PURPOSE
311 * Parse mount options.
312 *
313 * DESCRIPTION
314 * The following mount options are supported:
315 *
316 * gid= Set the default group.
317 * umask= Set the default umask.
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100318 * mode= Set the default file permissions.
319 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * uid= Set the default user.
321 * bs= Set the block size.
322 * unhide Show otherwise hidden files.
323 * undelete Show deleted files in lists.
324 * adinicb Embed data in the inode (default)
325 * noadinicb Don't embed data in the inode
326 * shortad Use short ad's
327 * longad Use long ad's (default)
328 * nostrict Unset strict conformance
329 * iocharset= Set the NLS character set
330 *
331 * The remaining are for debugging and disaster recovery:
332 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700333 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
335 * The following expect a offset from 0.
336 *
337 * session= Set the CDROM session (default= last session)
338 * anchor= Override standard anchor location. (default= 256)
339 * volume= Override the VolumeDesc location. (unused)
340 * partition= Override the PartitionDesc location. (unused)
341 * lastblock= Set the last block of the filesystem/
342 *
343 * The following expect a offset from the partition root.
344 *
345 * fileset= Override the fileset block location. (unused)
346 * rootdir= Override the root directory location. (unused)
347 * WARNING: overriding the rootdir to a non-directory may
348 * yield highly unpredictable results.
349 *
350 * PRE-CONDITIONS
351 * options Pointer to mount options string.
352 * uopts Pointer to mount options variable.
353 *
354 * POST-CONDITIONS
355 * <return> 1 Mount options parsed okay.
356 * <return> 0 Error parsing mount options.
357 *
358 * HISTORY
359 * July 1, 1997 - Andrew E. Mileski
360 * Written, tested, and released.
361 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363enum {
364 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
365 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
366 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
367 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
368 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100369 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
370 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
Steven Whitehousea447c092008-10-13 10:46:57 +0100373static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374 {Opt_novrs, "novrs"},
375 {Opt_nostrict, "nostrict"},
376 {Opt_bs, "bs=%u"},
377 {Opt_unhide, "unhide"},
378 {Opt_undelete, "undelete"},
379 {Opt_noadinicb, "noadinicb"},
380 {Opt_adinicb, "adinicb"},
381 {Opt_shortad, "shortad"},
382 {Opt_longad, "longad"},
383 {Opt_uforget, "uid=forget"},
384 {Opt_uignore, "uid=ignore"},
385 {Opt_gforget, "gid=forget"},
386 {Opt_gignore, "gid=ignore"},
387 {Opt_gid, "gid=%u"},
388 {Opt_uid, "uid=%u"},
389 {Opt_umask, "umask=%o"},
390 {Opt_session, "session=%u"},
391 {Opt_lastblock, "lastblock=%u"},
392 {Opt_anchor, "anchor=%u"},
393 {Opt_volume, "volume=%u"},
394 {Opt_partition, "partition=%u"},
395 {Opt_fileset, "fileset=%u"},
396 {Opt_rootdir, "rootdir=%u"},
397 {Opt_utf8, "utf8"},
398 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100399 {Opt_fmode, "mode=%o"},
400 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700401 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Miklos Szeredi6da80892008-02-08 04:21:50 -0800404static int udf_parse_options(char *options, struct udf_options *uopt,
405 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 char *p;
408 int option;
409
410 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 uopt->partition = 0xFFFF;
412 uopt->session = 0xFFFFFFFF;
413 uopt->lastblock = 0;
414 uopt->anchor = 0;
415 uopt->volume = 0xFFFFFFFF;
416 uopt->rootdir = 0xFFFFFFFF;
417 uopt->fileset = 0xFFFFFFFF;
418 uopt->nls_map = NULL;
419
420 if (!options)
421 return 1;
422
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700423 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 substring_t args[MAX_OPT_ARGS];
425 int token;
426 if (!*p)
427 continue;
428
429 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 switch (token) {
431 case Opt_novrs:
432 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100433 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700434 case Opt_bs:
435 if (match_int(&args[0], &option))
436 return 0;
437 uopt->blocksize = option;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100438 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700439 break;
440 case Opt_unhide:
441 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
442 break;
443 case Opt_undelete:
444 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
445 break;
446 case Opt_noadinicb:
447 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
448 break;
449 case Opt_adinicb:
450 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
451 break;
452 case Opt_shortad:
453 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
454 break;
455 case Opt_longad:
456 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
457 break;
458 case Opt_gid:
459 if (match_int(args, &option))
460 return 0;
461 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700462 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700463 break;
464 case Opt_uid:
465 if (match_int(args, &option))
466 return 0;
467 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700468 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469 break;
470 case Opt_umask:
471 if (match_octal(args, &option))
472 return 0;
473 uopt->umask = option;
474 break;
475 case Opt_nostrict:
476 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
477 break;
478 case Opt_session:
479 if (match_int(args, &option))
480 return 0;
481 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800482 if (!remount)
483 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700484 break;
485 case Opt_lastblock:
486 if (match_int(args, &option))
487 return 0;
488 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800489 if (!remount)
490 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491 break;
492 case Opt_anchor:
493 if (match_int(args, &option))
494 return 0;
495 uopt->anchor = option;
496 break;
497 case Opt_volume:
498 if (match_int(args, &option))
499 return 0;
500 uopt->volume = option;
501 break;
502 case Opt_partition:
503 if (match_int(args, &option))
504 return 0;
505 uopt->partition = option;
506 break;
507 case Opt_fileset:
508 if (match_int(args, &option))
509 return 0;
510 uopt->fileset = option;
511 break;
512 case Opt_rootdir:
513 if (match_int(args, &option))
514 return 0;
515 uopt->rootdir = option;
516 break;
517 case Opt_utf8:
518 uopt->flags |= (1 << UDF_FLAG_UTF8);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 case Opt_iocharset:
522 uopt->nls_map = load_nls(args[0].from);
523 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700526 case Opt_uignore:
527 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
528 break;
529 case Opt_uforget:
530 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
531 break;
532 case Opt_gignore:
533 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
534 break;
535 case Opt_gforget:
536 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
537 break;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100538 case Opt_fmode:
539 if (match_octal(args, &option))
540 return 0;
541 uopt->fmode = option & 0777;
542 break;
543 case Opt_dmode:
544 if (match_octal(args, &option))
545 return 0;
546 uopt->dmode = option & 0777;
547 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700548 default:
549 printk(KERN_ERR "udf: bad mount option \"%s\" "
550 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552 }
553 }
554 return 1;
555}
556
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800560 struct udf_sb_info *sbi = UDF_SB(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400561 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800563 uopt.flags = sbi->s_flags;
564 uopt.uid = sbi->s_uid;
565 uopt.gid = sbi->s_gid;
566 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100567 uopt.fmode = sbi->s_fmode;
568 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Miklos Szeredi6da80892008-02-08 04:21:50 -0800570 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return -EINVAL;
572
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200573 lock_kernel();
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800574 sbi->s_flags = uopt.flags;
575 sbi->s_uid = uopt.uid;
576 sbi->s_gid = uopt.gid;
577 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100578 sbi->s_fmode = uopt.fmode;
579 sbi->s_dmode = uopt.dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800581 if (sbi->s_lvid_bh) {
582 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (write_rev > UDF_MAX_WRITE_VERSION)
584 *flags |= MS_RDONLY;
585 }
586
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400587 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
588 goto out_unlock;
589
590 if (*flags & MS_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 udf_close_lvid(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400592
593 error = vfs_dq_off(sb, 1);
594 if (error < 0 && error != -ENOSYS)
595 error = -EBUSY;
596 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 udf_open_lvid(sb);
598
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400599 /* mark the fs r/w for quota activity */
600 sb->s_flags &= ~MS_RDONLY;
601 vfs_dq_quota_on_remount(sb);
602 }
603
604out_unlock:
Alessio Igor Bogani337eb002009-05-12 15:10:54 +0200605 unlock_kernel();
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400606 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Jan Kara40346002009-03-19 16:21:38 +0100609/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
610/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
611static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200614 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int sectorsize;
616 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617 int nsr02 = 0;
618 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800619 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800621 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (sb->s_blocksize < sizeof(struct volStructDesc))
623 sectorsize = sizeof(struct volStructDesc);
624 else
625 sectorsize = sb->s_blocksize;
626
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800627 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200630 (unsigned int)(sector >> sb->s_blocksize_bits),
631 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700633 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Read a block */
635 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
636 if (!bh)
637 break;
638
639 /* Look for ISO descriptors */
640 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800641 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700644 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800646 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
647 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 switch (vsd->structType) {
649 case 0:
650 udf_debug("ISO9660 Boot Record found\n");
651 break;
652 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800653 udf_debug("ISO9660 Primary Volume Descriptor "
654 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700655 break;
656 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800657 udf_debug("ISO9660 Supplementary Volume "
658 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700659 break;
660 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 udf_debug("ISO9660 Volume Partition Descriptor "
662 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700663 break;
664 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 udf_debug("ISO9660 Volume Descriptor Set "
666 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700667 break;
668 default:
669 udf_debug("ISO9660 VRS (%u) found\n",
670 vsd->structType);
671 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800673 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
674 VSD_STD_ID_LEN))
675 ; /* nothing */
676 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
677 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700678 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800680 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
681 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800683 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
684 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700686 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
689 if (nsr03)
690 return nsr03;
691 else if (nsr02)
692 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800693 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -1;
695 else
696 return 0;
697}
698
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800699static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200700 struct kernel_lb_addr *fileset,
701 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 struct buffer_head *bh = NULL;
704 long lastblock;
705 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800706 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200710 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700712 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700715 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 1;
717 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800721 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800722 if (!bh) {
723 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200724 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700726/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700728
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800729 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700730 (newfileset.partitionReferenceNum != 0xFFFF &&
731 fileset->logicalBlockNum == 0xFFFFFFFF &&
732 fileset->partitionReferenceNum == 0xFFFF);
733 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800734 lastblock = sbi->s_partmaps
735 [newfileset.partitionReferenceNum]
736 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 newfileset.logicalBlockNum = 0;
738
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200740 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800741 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 if (!bh) {
743 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 continue;
745 }
746
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700747 switch (ident) {
748 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700749 {
750 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800751 sp = (struct spaceBitmapDesc *)
752 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700753 newfileset.logicalBlockNum += 1 +
754 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800755 sizeof(struct spaceBitmapDesc)
756 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700757 brelse(bh);
758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700760 case TAG_IDENT_FSD:
761 *fileset = newfileset;
762 break;
763 default:
764 newfileset.logicalBlockNum++;
765 brelse(bh);
766 bh = NULL;
767 break;
768 }
769 } while (newfileset.logicalBlockNum < lastblock &&
770 fileset->logicalBlockNum == 0xFFFFFFFF &&
771 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 }
774
775 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778 fileset->logicalBlockNum,
779 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800781 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700783 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return 0;
785 }
786 return 1;
787}
788
Jan Karac0eb31e2008-04-01 16:50:35 +0200789static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
791 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100792 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200793 struct buffer_head *bh;
794 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100795 int ret = 1;
796
797 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
798 if (!instr)
799 return 1;
800
801 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
802 if (!outstr)
803 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200804
805 bh = udf_read_tagged(sb, block, block, &ident);
806 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100807 goto out2;
808
Jan Karac0eb31e2008-04-01 16:50:35 +0200809 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 pvoldesc = (struct primaryVolDesc *)bh->b_data;
812
Marcin Slusarz56774802008-02-10 11:25:31 +0100813 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
814 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100815#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200816 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100817 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800818 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100819 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
820 ts->minute, le16_to_cpu(ts->typeAndTimezone));
821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100824 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
825 if (udf_CS0toUTF8(outstr, instr)) {
826 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
827 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800828 udf_debug("volIdent[] = '%s'\n",
829 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100832 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
833 if (udf_CS0toUTF8(outstr, instr))
834 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200835
836 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100837 ret = 0;
838out2:
839 kfree(outstr);
840out1:
841 kfree(instr);
842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Jan Karabfb257a2008-04-08 20:37:21 +0200845static int udf_load_metadata_files(struct super_block *sb, int partition)
846{
847 struct udf_sb_info *sbi = UDF_SB(sb);
848 struct udf_part_map *map;
849 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200850 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200851 int fe_error = 0;
852
853 map = &sbi->s_partmaps[partition];
854 mdata = &map->s_type_specific.s_metadata;
855
856 /* metadata address */
857 addr.logicalBlockNum = mdata->s_meta_file_loc;
858 addr.partitionReferenceNum = map->s_partition_num;
859
860 udf_debug("Metadata file location: block = %d part = %d\n",
861 addr.logicalBlockNum, addr.partitionReferenceNum);
862
Pekka Enberg97e961f2008-10-15 12:29:03 +0200863 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200864
865 if (mdata->s_metadata_fe == NULL) {
866 udf_warning(sb, __func__, "metadata inode efe not found, "
867 "will try mirror inode.");
868 fe_error = 1;
869 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
870 ICBTAG_FLAG_AD_SHORT) {
871 udf_warning(sb, __func__, "metadata inode efe does not have "
872 "short allocation descriptors!");
873 fe_error = 1;
874 iput(mdata->s_metadata_fe);
875 mdata->s_metadata_fe = NULL;
876 }
877
878 /* mirror file entry */
879 addr.logicalBlockNum = mdata->s_mirror_file_loc;
880 addr.partitionReferenceNum = map->s_partition_num;
881
882 udf_debug("Mirror metadata file location: block = %d part = %d\n",
883 addr.logicalBlockNum, addr.partitionReferenceNum);
884
Pekka Enberg97e961f2008-10-15 12:29:03 +0200885 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200886
887 if (mdata->s_mirror_fe == NULL) {
888 if (fe_error) {
889 udf_error(sb, __func__, "mirror inode efe not found "
890 "and metadata inode is missing too, exiting...");
891 goto error_exit;
892 } else
893 udf_warning(sb, __func__, "mirror inode efe not found,"
894 " but metadata inode is OK");
895 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
896 ICBTAG_FLAG_AD_SHORT) {
897 udf_warning(sb, __func__, "mirror inode efe does not have "
898 "short allocation descriptors!");
899 iput(mdata->s_mirror_fe);
900 mdata->s_mirror_fe = NULL;
901 if (fe_error)
902 goto error_exit;
903 }
904
905 /*
906 * bitmap file entry
907 * Note:
908 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
909 */
910 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
911 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
912 addr.partitionReferenceNum = map->s_partition_num;
913
914 udf_debug("Bitmap file location: block = %d part = %d\n",
915 addr.logicalBlockNum, addr.partitionReferenceNum);
916
Pekka Enberg97e961f2008-10-15 12:29:03 +0200917 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200918
919 if (mdata->s_bitmap_fe == NULL) {
920 if (sb->s_flags & MS_RDONLY)
921 udf_warning(sb, __func__, "bitmap inode efe "
922 "not found but it's ok since the disc"
923 " is mounted read-only");
924 else {
925 udf_error(sb, __func__, "bitmap inode efe not "
926 "found and attempted read-write mount");
927 goto error_exit;
928 }
929 }
930 }
931
932 udf_debug("udf_load_metadata_files Ok\n");
933
934 return 0;
935
936error_exit:
937 return 1;
938}
939
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700940static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200941 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
943 struct fileSetDesc *fset;
944
945 fset = (struct fileSetDesc *)bh->b_data;
946
947 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
948
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800949 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700951 udf_debug("Rootdir at block=%d, partition=%d\n",
952 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800955int udf_compute_nr_groups(struct super_block *sb, u32 partition)
956{
957 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100958 return DIV_ROUND_UP(map->s_partition_len +
959 (sizeof(struct spaceBitmapDesc) << 3),
960 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800961}
962
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800963static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
964{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800965 struct udf_bitmap *bitmap;
966 int nr_groups;
967 int size;
968
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800969 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800970 size = sizeof(struct udf_bitmap) +
971 (sizeof(struct buffer_head *) * nr_groups);
972
973 if (size <= PAGE_SIZE)
974 bitmap = kmalloc(size, GFP_KERNEL);
975 else
976 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
977
978 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700979 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800980 "Unable to allocate space for bitmap "
981 "and %d buffer_head pointers", nr_groups);
982 return NULL;
983 }
984
985 memset(bitmap, 0x00, size);
986 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
987 bitmap->s_nr_groups = nr_groups;
988 return bitmap;
989}
990
Jan Kara3fb38df2008-04-02 12:26:36 +0200991static int udf_fill_partdesc_info(struct super_block *sb,
992 struct partitionDesc *p, int p_index)
993{
994 struct udf_part_map *map;
995 struct udf_sb_info *sbi = UDF_SB(sb);
996 struct partitionHeaderDesc *phd;
997
998 map = &sbi->s_partmaps[p_index];
999
1000 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1001 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1002
1003 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1004 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1005 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1006 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1007 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1008 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1009 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1010 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1011
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001012 udf_debug("Partition (%d type %x) starts at physical %d, "
1013 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001014 map->s_partition_type, map->s_partition_root,
1015 map->s_partition_len);
1016
1017 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1018 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1019 return 0;
1020
1021 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1022 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001023 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001024 .logicalBlockNum = le32_to_cpu(
1025 phd->unallocSpaceTable.extPosition),
1026 .partitionReferenceNum = p_index,
1027 };
1028
Pekka Enberg97e961f2008-10-15 12:29:03 +02001029 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001030 if (!map->s_uspace.s_table) {
1031 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1032 p_index);
1033 return 1;
1034 }
1035 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1036 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1037 p_index, map->s_uspace.s_table->i_ino);
1038 }
1039
1040 if (phd->unallocSpaceBitmap.extLength) {
1041 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1042 if (!bitmap)
1043 return 1;
1044 map->s_uspace.s_bitmap = bitmap;
1045 bitmap->s_extLength = le32_to_cpu(
1046 phd->unallocSpaceBitmap.extLength);
1047 bitmap->s_extPosition = le32_to_cpu(
1048 phd->unallocSpaceBitmap.extPosition);
1049 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1050 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1051 bitmap->s_extPosition);
1052 }
1053
1054 if (phd->partitionIntegrityTable.extLength)
1055 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1056
1057 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001058 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001059 .logicalBlockNum = le32_to_cpu(
1060 phd->freedSpaceTable.extPosition),
1061 .partitionReferenceNum = p_index,
1062 };
1063
Pekka Enberg97e961f2008-10-15 12:29:03 +02001064 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001065 if (!map->s_fspace.s_table) {
1066 udf_debug("cannot load freedSpaceTable (part %d)\n",
1067 p_index);
1068 return 1;
1069 }
1070
1071 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1072 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1073 p_index, map->s_fspace.s_table->i_ino);
1074 }
1075
1076 if (phd->freedSpaceBitmap.extLength) {
1077 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1078 if (!bitmap)
1079 return 1;
1080 map->s_fspace.s_bitmap = bitmap;
1081 bitmap->s_extLength = le32_to_cpu(
1082 phd->freedSpaceBitmap.extLength);
1083 bitmap->s_extPosition = le32_to_cpu(
1084 phd->freedSpaceBitmap.extPosition);
1085 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1086 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1087 bitmap->s_extPosition);
1088 }
1089 return 0;
1090}
1091
Jan Karae971b0b2009-11-30 19:47:55 +01001092static void udf_find_vat_block(struct super_block *sb, int p_index,
1093 int type1_index, sector_t start_block)
1094{
1095 struct udf_sb_info *sbi = UDF_SB(sb);
1096 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1097 sector_t vat_block;
1098 struct kernel_lb_addr ino;
1099
1100 /*
1101 * VAT file entry is in the last recorded block. Some broken disks have
1102 * it a few blocks before so try a bit harder...
1103 */
1104 ino.partitionReferenceNum = type1_index;
1105 for (vat_block = start_block;
1106 vat_block >= map->s_partition_root &&
1107 vat_block >= start_block - 3 &&
1108 !sbi->s_vat_inode; vat_block--) {
1109 ino.logicalBlockNum = vat_block - map->s_partition_root;
1110 sbi->s_vat_inode = udf_iget(sb, &ino);
1111 }
1112}
1113
Jan Kara38b74a52008-04-02 16:01:35 +02001114static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1115{
1116 struct udf_sb_info *sbi = UDF_SB(sb);
1117 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Jan Karafa5e0812008-04-08 02:08:53 +02001118 struct buffer_head *bh = NULL;
1119 struct udf_inode_info *vati;
1120 uint32_t pos;
1121 struct virtualAllocationTable20 *vat20;
Jan Kara4bf17af2009-07-14 19:30:23 +02001122 sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
Jan Kara38b74a52008-04-02 16:01:35 +02001123
Jan Karae971b0b2009-11-30 19:47:55 +01001124 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
Jan Kara4bf17af2009-07-14 19:30:23 +02001125 if (!sbi->s_vat_inode &&
1126 sbi->s_last_block != blocks - 1) {
1127 printk(KERN_NOTICE "UDF-fs: Failed to read VAT inode from the"
1128 " last recorded block (%lu), retrying with the last "
1129 "block of the device (%lu).\n",
1130 (unsigned long)sbi->s_last_block,
1131 (unsigned long)blocks - 1);
Jan Karae971b0b2009-11-30 19:47:55 +01001132 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
Jan Kara4bf17af2009-07-14 19:30:23 +02001133 }
Jan Kara38b74a52008-04-02 16:01:35 +02001134 if (!sbi->s_vat_inode)
1135 return 1;
1136
1137 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001138 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001139 map->s_type_specific.s_virtual.s_num_entries =
1140 (sbi->s_vat_inode->i_size - 36) >> 2;
1141 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001142 vati = UDF_I(sbi->s_vat_inode);
1143 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1144 pos = udf_block_map(sbi->s_vat_inode, 0);
1145 bh = sb_bread(sb, pos);
1146 if (!bh)
1147 return 1;
1148 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1149 } else {
1150 vat20 = (struct virtualAllocationTable20 *)
1151 vati->i_ext.i_data;
1152 }
Jan Kara38b74a52008-04-02 16:01:35 +02001153
Jan Kara38b74a52008-04-02 16:01:35 +02001154 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001155 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001156 map->s_type_specific.s_virtual.s_num_entries =
1157 (sbi->s_vat_inode->i_size -
1158 map->s_type_specific.s_virtual.
1159 s_start_offset) >> 2;
1160 brelse(bh);
1161 }
1162 return 0;
1163}
1164
Jan Karac0eb31e2008-04-01 16:50:35 +02001165static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Jan Karac0eb31e2008-04-01 16:50:35 +02001167 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001168 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001169 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001170 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001171 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001172 uint16_t partitionNumber;
1173 uint16_t ident;
1174 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Jan Karac0eb31e2008-04-01 16:50:35 +02001176 bh = udf_read_tagged(sb, block, block, &ident);
1177 if (!bh)
1178 return 1;
1179 if (ident != TAG_IDENT_PD)
1180 goto out_bh;
1181
1182 p = (struct partitionDesc *)bh->b_data;
1183 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001184
Jan Karabfb257a2008-04-08 20:37:21 +02001185 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001186 for (i = 0; i < sbi->s_partitions; i++) {
1187 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001188 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001189 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001190 if (map->s_partition_num == partitionNumber &&
1191 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1192 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001194 }
1195
Jan Kara38b74a52008-04-02 16:01:35 +02001196 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001197 udf_debug("Partition (%d) not found in partition map\n",
1198 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001199 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001200 }
1201
Jan Kara3fb38df2008-04-02 12:26:36 +02001202 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001203
1204 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001205 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1206 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001207 */
1208 type1_idx = i;
1209 for (i = 0; i < sbi->s_partitions; i++) {
1210 map = &sbi->s_partmaps[i];
1211
1212 if (map->s_partition_num == partitionNumber &&
1213 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001214 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1215 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001216 break;
1217 }
1218
1219 if (i >= sbi->s_partitions)
1220 goto out_bh;
1221
1222 ret = udf_fill_partdesc_info(sb, p, i);
1223 if (ret)
1224 goto out_bh;
1225
Jan Karabfb257a2008-04-08 20:37:21 +02001226 if (map->s_partition_type == UDF_METADATA_MAP25) {
1227 ret = udf_load_metadata_files(sb, i);
1228 if (ret) {
1229 printk(KERN_ERR "UDF-fs: error loading MetaData "
1230 "partition map %d\n", i);
1231 goto out_bh;
1232 }
1233 } else {
1234 ret = udf_load_vat(sb, i, type1_idx);
1235 if (ret)
1236 goto out_bh;
1237 /*
1238 * Mark filesystem read-only if we have a partition with
1239 * virtual map since we don't handle writing to it (we
1240 * overwrite blocks instead of relocating them).
1241 */
1242 sb->s_flags |= MS_RDONLY;
1243 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1244 "because writing to pseudooverwrite partition is "
1245 "not implemented.\n");
1246 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001247out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001248 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001249 brelse(bh);
1250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Jan Karac0eb31e2008-04-01 16:50:35 +02001253static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001254 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct logicalVolDesc *lvd;
1257 int i, j, offset;
1258 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001259 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001260 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001261 uint16_t ident;
1262 struct buffer_head *bh;
1263 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Jan Karac0eb31e2008-04-01 16:50:35 +02001265 bh = udf_read_tagged(sb, block, block, &ident);
1266 if (!bh)
1267 return 1;
1268 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 lvd = (struct logicalVolDesc *)bh->b_data;
1270
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001271 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001272 if (i != 0) {
1273 ret = i;
1274 goto out_bh;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001278 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001279 i++, offset += gpm->partitionMapLength) {
1280 struct udf_part_map *map = &sbi->s_partmaps[i];
1281 gpm = (struct genericPartitionMap *)
1282 &(lvd->partitionMaps[offset]);
1283 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001284 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001285 struct genericPartitionMap1 *gpm1 =
1286 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001287 map->s_partition_type = UDF_TYPE1_MAP15;
1288 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1289 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1290 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001291 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001292 struct udfPartitionMap2 *upm2 =
1293 (struct udfPartitionMap2 *)gpm;
1294 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1295 strlen(UDF_ID_VIRTUAL))) {
1296 u16 suf =
1297 le16_to_cpu(((__le16 *)upm2->partIdent.
1298 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001299 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001300 map->s_partition_type =
1301 UDF_VIRTUAL_MAP15;
1302 map->s_partition_func =
1303 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001304 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001305 map->s_partition_type =
1306 UDF_VIRTUAL_MAP20;
1307 map->s_partition_func =
1308 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001310 } else if (!strncmp(upm2->partIdent.ident,
1311 UDF_ID_SPARABLE,
1312 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001315 struct sparablePartitionMap *spm =
1316 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001318 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001319 map->s_type_specific.s_sparing.s_packet_len =
1320 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001321 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001322 struct buffer_head *bh2;
1323
1324 loc = le32_to_cpu(
1325 spm->locSparingTable[j]);
1326 bh2 = udf_read_tagged(sb, loc, loc,
1327 &ident);
1328 map->s_type_specific.s_sparing.
1329 s_spar_map[j] = bh2;
1330
Marcin Slusarz165923f2008-02-10 11:33:08 +01001331 if (bh2 == NULL)
1332 continue;
1333
1334 st = (struct sparingTable *)bh2->b_data;
1335 if (ident != 0 || strncmp(
1336 st->sparingIdent.ident,
1337 UDF_ID_SPARING,
1338 strlen(UDF_ID_SPARING))) {
1339 brelse(bh2);
1340 map->s_type_specific.s_sparing.
1341 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001344 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001345 } else if (!strncmp(upm2->partIdent.ident,
1346 UDF_ID_METADATA,
1347 strlen(UDF_ID_METADATA))) {
1348 struct udf_meta_data *mdata =
1349 &map->s_type_specific.s_metadata;
1350 struct metadataPartitionMap *mdm =
1351 (struct metadataPartitionMap *)
1352 &(lvd->partitionMaps[offset]);
1353 udf_debug("Parsing Logical vol part %d "
1354 "type %d id=%s\n", i, type,
1355 UDF_ID_METADATA);
1356
1357 map->s_partition_type = UDF_METADATA_MAP25;
1358 map->s_partition_func = udf_get_pblock_meta25;
1359
1360 mdata->s_meta_file_loc =
1361 le32_to_cpu(mdm->metadataFileLoc);
1362 mdata->s_mirror_file_loc =
1363 le32_to_cpu(mdm->metadataMirrorFileLoc);
1364 mdata->s_bitmap_file_loc =
1365 le32_to_cpu(mdm->metadataBitmapFileLoc);
1366 mdata->s_alloc_unit_size =
1367 le32_to_cpu(mdm->allocUnitSize);
1368 mdata->s_align_unit_size =
1369 le16_to_cpu(mdm->alignUnitSize);
1370 mdata->s_dup_md_flag =
1371 mdm->flags & 0x01;
1372
1373 udf_debug("Metadata Ident suffix=0x%x\n",
1374 (le16_to_cpu(
1375 ((__le16 *)
1376 mdm->partIdent.identSuffix)[0])));
1377 udf_debug("Metadata part num=%d\n",
1378 le16_to_cpu(mdm->partitionNum));
1379 udf_debug("Metadata part alloc unit size=%d\n",
1380 le32_to_cpu(mdm->allocUnitSize));
1381 udf_debug("Metadata file loc=%d\n",
1382 le32_to_cpu(mdm->metadataFileLoc));
1383 udf_debug("Mirror file loc=%d\n",
1384 le32_to_cpu(mdm->metadataMirrorFileLoc));
1385 udf_debug("Bitmap file loc=%d\n",
1386 le32_to_cpu(mdm->metadataBitmapFileLoc));
1387 udf_debug("Duplicate Flag: %d %d\n",
1388 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001389 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001390 udf_debug("Unknown ident: %s\n",
1391 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 continue;
1393 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001394 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1395 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001398 i, map->s_partition_num, type,
1399 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001402 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001403 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001406 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1407 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001408 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 if (lvd->integritySeqExt.extLength)
1411 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001412
Jan Karac0eb31e2008-04-01 16:50:35 +02001413out_bh:
1414 brelse(bh);
1415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
1418/*
1419 * udf_load_logicalvolint
1420 *
1421 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001422static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct buffer_head *bh = NULL;
1425 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001426 struct udf_sb_info *sbi = UDF_SB(sb);
1427 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001430 (bh = udf_read_tagged(sb, loc.extLocation,
1431 loc.extLocation, &ident)) &&
1432 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001433 sbi->s_lvid_bh = bh;
1434 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001435
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001436 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001437 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001438 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001439
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001440 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001441 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001443 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001445 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001446 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449/*
1450 * udf_process_sequence
1451 *
1452 * PURPOSE
1453 * Process a main/reserve volume descriptor sequence.
1454 *
1455 * PRE-CONDITIONS
1456 * sb Pointer to _locked_ superblock.
1457 * block First block of first extent of the sequence.
1458 * lastblock Lastblock of first extent of the sequence.
1459 *
1460 * HISTORY
1461 * July 1, 1997 - Andrew E. Mileski
1462 * Written, tested, and released.
1463 */
Jan Kara200a3592008-03-04 13:03:09 +01001464static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001465 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct buffer_head *bh = NULL;
1468 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001469 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct generic_desc *gd;
1471 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001472 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 uint32_t vdsn;
1474 uint16_t ident;
1475 long next_s = 0, next_e = 0;
1476
1477 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1478
Jan Karac0eb31e2008-04-01 16:50:35 +02001479 /*
1480 * Read the main descriptor sequence and find which descriptors
1481 * are in it.
1482 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001483 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001486 if (!bh) {
1487 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1488 "sequence is corrupted or we could not read "
1489 "it.\n", (unsigned long long)block);
1490 return 1;
1491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1494 gd = (struct generic_desc *)bh->b_data;
1495 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001496 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001497 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001498 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1499 if (vdsn >= curr->volDescSeqNum) {
1500 curr->volDescSeqNum = vdsn;
1501 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502 }
1503 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001504 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001505 curr = &vds[VDS_POS_VOL_DESC_PTR];
1506 if (vdsn >= curr->volDescSeqNum) {
1507 curr->volDescSeqNum = vdsn;
1508 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001510 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001511 next_s = le32_to_cpu(
1512 vdp->nextVolDescSeqExt.extLocation);
1513 next_e = le32_to_cpu(
1514 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001515 next_e = next_e >> sb->s_blocksize_bits;
1516 next_e += next_s;
1517 }
1518 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001519 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001520 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1521 if (vdsn >= curr->volDescSeqNum) {
1522 curr->volDescSeqNum = vdsn;
1523 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001524 }
1525 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001526 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001527 curr = &vds[VDS_POS_PARTITION_DESC];
1528 if (!curr->block)
1529 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001530 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001531 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001532 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1533 if (vdsn >= curr->volDescSeqNum) {
1534 curr->volDescSeqNum = vdsn;
1535 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001536 }
1537 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001538 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001539 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1540 if (vdsn >= curr->volDescSeqNum) {
1541 curr->volDescSeqNum = vdsn;
1542 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001543 }
1544 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001545 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001546 vds[VDS_POS_TERMINATING_DESC].block = block;
1547 if (next_e) {
1548 block = next_s;
1549 lastblock = next_e;
1550 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001551 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001552 done = 1;
1553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001555 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001557 /*
1558 * Now read interesting descriptors again and process them
1559 * in a suitable order
1560 */
1561 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1562 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1563 return 1;
1564 }
1565 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Jan Karac0eb31e2008-04-01 16:50:35 +02001568 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1569 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1570 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001571
Jan Karac0eb31e2008-04-01 16:50:35 +02001572 if (vds[VDS_POS_PARTITION_DESC].block) {
1573 /*
1574 * We rescan the whole descriptor sequence to find
1575 * partition descriptor blocks and process them.
1576 */
1577 for (block = vds[VDS_POS_PARTITION_DESC].block;
1578 block < vds[VDS_POS_TERMINATING_DESC].block;
1579 block++)
1580 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001581 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 return 0;
1585}
1586
Jan Kara40346002009-03-19 16:21:38 +01001587static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1588 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Jan Kara40346002009-03-19 16:21:38 +01001590 struct anchorVolDescPtr *anchor;
1591 long main_s, main_e, reserve_s, reserve_e;
1592 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Jan Kara40346002009-03-19 16:21:38 +01001594 sbi = UDF_SB(sb);
1595 anchor = (struct anchorVolDescPtr *)bh->b_data;
1596
1597 /* Locate the main sequence */
1598 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1599 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1600 main_e = main_e >> sb->s_blocksize_bits;
1601 main_e += main_s;
1602
1603 /* Locate the reserve sequence */
1604 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1605 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1606 reserve_e = reserve_e >> sb->s_blocksize_bits;
1607 reserve_e += reserve_s;
1608
1609 /* Process the main & reserve sequences */
1610 /* responsible for finding the PartitionDesc(s) */
1611 if (!udf_process_sequence(sb, main_s, main_e, fileset))
1612 return 1;
1613 return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Jan Kara40346002009-03-19 16:21:38 +01001616/*
1617 * Check whether there is an anchor block in the given block and
1618 * load Volume Descriptor Sequence if so.
1619 */
1620static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1621 struct kernel_lb_addr *fileset)
1622{
1623 struct buffer_head *bh;
1624 uint16_t ident;
1625 int ret;
1626
1627 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1628 udf_fixed_to_variable(block) >=
1629 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
1630 return 0;
1631
1632 bh = udf_read_tagged(sb, block, block, &ident);
1633 if (!bh)
1634 return 0;
1635 if (ident != TAG_IDENT_AVDP) {
1636 brelse(bh);
1637 return 0;
1638 }
1639 ret = udf_load_sequence(sb, bh, fileset);
1640 brelse(bh);
1641 return ret;
1642}
1643
1644/* Search for an anchor volume descriptor pointer */
1645static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
1646 struct kernel_lb_addr *fileset)
1647{
1648 sector_t last[6];
1649 int i;
1650 struct udf_sb_info *sbi = UDF_SB(sb);
1651 int last_count = 0;
1652
1653 /* First try user provided anchor */
1654 if (sbi->s_anchor) {
1655 if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
1656 return lastblock;
1657 }
1658 /*
1659 * according to spec, anchor is in either:
1660 * block 256
1661 * lastblock-256
1662 * lastblock
1663 * however, if the disc isn't closed, it could be 512.
1664 */
1665 if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
1666 return lastblock;
1667 /*
1668 * The trouble is which block is the last one. Drives often misreport
1669 * this so we try various possibilities.
1670 */
1671 last[last_count++] = lastblock;
1672 if (lastblock >= 1)
1673 last[last_count++] = lastblock - 1;
1674 last[last_count++] = lastblock + 1;
1675 if (lastblock >= 2)
1676 last[last_count++] = lastblock - 2;
1677 if (lastblock >= 150)
1678 last[last_count++] = lastblock - 150;
1679 if (lastblock >= 152)
1680 last[last_count++] = lastblock - 152;
1681
1682 for (i = 0; i < last_count; i++) {
1683 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
1684 sb->s_blocksize_bits)
1685 continue;
1686 if (udf_check_anchor_block(sb, last[i], fileset))
1687 return last[i];
1688 if (last[i] < 256)
1689 continue;
1690 if (udf_check_anchor_block(sb, last[i] - 256, fileset))
1691 return last[i];
1692 }
1693
1694 /* Finally try block 512 in case media is open */
1695 if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
1696 return last[0];
1697 return 0;
1698}
1699
1700/*
1701 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1702 * area specified by it. The function expects sbi->s_lastblock to be the last
1703 * block on the media.
1704 *
1705 * Return 1 if ok, 0 if not found.
1706 *
1707 */
1708static int udf_find_anchor(struct super_block *sb,
1709 struct kernel_lb_addr *fileset)
1710{
1711 sector_t lastblock;
1712 struct udf_sb_info *sbi = UDF_SB(sb);
1713
1714 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1715 if (lastblock)
1716 goto out;
1717
1718 /* No anchor found? Try VARCONV conversion of block numbers */
1719 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1720 /* Firstly, we try to not convert number of the last block */
1721 lastblock = udf_scan_anchors(sb,
1722 udf_variable_to_fixed(sbi->s_last_block),
1723 fileset);
1724 if (lastblock)
1725 goto out;
1726
1727 /* Secondly, we try with converted number of the last block */
1728 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1729 if (!lastblock) {
1730 /* VARCONV didn't help. Clear it. */
1731 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1732 return 0;
1733 }
1734out:
1735 sbi->s_last_block = lastblock;
1736 return 1;
1737}
1738
1739/*
1740 * Check Volume Structure Descriptor, find Anchor block and load Volume
1741 * Descriptor Sequence
1742 */
1743static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1744 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001745{
1746 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara40346002009-03-19 16:21:38 +01001747 loff_t nsr_off;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001748
1749 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1750 if (!silent)
1751 printk(KERN_WARNING "UDF-fs: Bad block size\n");
1752 return 0;
1753 }
1754 sbi->s_last_block = uopt->lastblock;
Jan Kara40346002009-03-19 16:21:38 +01001755 if (!uopt->novrs) {
1756 /* Check that it is NSR02 compliant */
1757 nsr_off = udf_check_vsd(sb);
1758 if (!nsr_off) {
1759 if (!silent)
1760 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1761 return 0;
1762 }
1763 if (nsr_off == -1)
1764 udf_debug("Failed to read byte 32768. Assuming open "
1765 "disc. Skipping validity check\n");
1766 if (!sbi->s_last_block)
1767 sbi->s_last_block = udf_get_last_block(sb);
1768 } else {
1769 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001770 }
Jan Kara40346002009-03-19 16:21:38 +01001771
1772 /* Look for anchor block and load Volume Descriptor Sequence */
1773 sbi->s_anchor = uopt->anchor;
1774 if (!udf_find_anchor(sb, fileset)) {
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001775 if (!silent)
1776 printk(KERN_WARNING "UDF-fs: No anchor found\n");
1777 return 0;
1778 }
1779 return 1;
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782static void udf_open_lvid(struct super_block *sb)
1783{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001784 struct udf_sb_info *sbi = UDF_SB(sb);
1785 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001786 struct logicalVolIntegrityDesc *lvid;
1787 struct logicalVolIntegrityDescImpUse *lvidiu;
Jan Kara146bca72009-03-16 18:27:37 +01001788
Marcin Slusarz165923f2008-02-10 11:33:08 +01001789 if (!bh)
1790 return;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001791 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1792 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Marcin Slusarz165923f2008-02-10 11:33:08 +01001794 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1795 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1796 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1797 CURRENT_TIME);
Jan Kara146bca72009-03-16 18:27:37 +01001798 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Marcin Slusarz165923f2008-02-10 11:33:08 +01001800 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001801 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001802 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001803
1804 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1805 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001806 sbi->s_lvid_dirty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
1809static void udf_close_lvid(struct super_block *sb)
1810{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001811 struct udf_sb_info *sbi = UDF_SB(sb);
1812 struct buffer_head *bh = sbi->s_lvid_bh;
1813 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001814 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001815
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001816 if (!bh)
1817 return;
1818
1819 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001820 lvidiu = udf_sb_lvidiu(sbi);
1821 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1822 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1823 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1824 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1825 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1826 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1827 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1828 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1829 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1830 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Marcin Slusarz165923f2008-02-10 11:33:08 +01001832 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001833 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001834 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001835
1836 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1837 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001838 sbi->s_lvid_dirty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001841static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1842{
1843 int i;
1844 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001845 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1846 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001847
1848 for (i = 0; i < nr_groups; i++)
1849 if (bitmap->s_block_bitmap[i])
1850 brelse(bitmap->s_block_bitmap[i]);
1851
1852 if (size <= PAGE_SIZE)
1853 kfree(bitmap);
1854 else
1855 vfree(bitmap);
1856}
1857
Jan Kara2e0838f2008-04-01 18:08:51 +02001858static void udf_free_partition(struct udf_part_map *map)
1859{
1860 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001861 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001862
1863 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1864 iput(map->s_uspace.s_table);
1865 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1866 iput(map->s_fspace.s_table);
1867 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1868 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1869 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1870 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1871 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1872 for (i = 0; i < 4; i++)
1873 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001874 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1875 mdata = &map->s_type_specific.s_metadata;
1876 iput(mdata->s_metadata_fe);
1877 mdata->s_metadata_fe = NULL;
1878
1879 iput(mdata->s_mirror_fe);
1880 mdata->s_mirror_fe = NULL;
1881
1882 iput(mdata->s_bitmap_fe);
1883 mdata->s_bitmap_fe = NULL;
1884 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887static int udf_fill_super(struct super_block *sb, void *options, int silent)
1888{
1889 int i;
Jan Kara40346002009-03-19 16:21:38 +01001890 int ret;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001891 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001893 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct udf_sb_info *sbi;
1895
1896 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1897 uopt.uid = -1;
1898 uopt.gid = -1;
1899 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001900 uopt.fmode = UDF_INVALID_MODE;
1901 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001903 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (!sbi)
1905 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001909 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Miklos Szeredi6da80892008-02-08 04:21:50 -08001911 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto error_out;
1913
1914 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001915 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001917 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 goto error_out;
1919 }
1920#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001921 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 uopt.nls_map = load_nls_default();
1923 if (!uopt.nls_map)
1924 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1925 else
1926 udf_debug("Using default NLS map\n");
1927 }
1928#endif
1929 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1930 uopt.flags |= (1 << UDF_FLAG_UTF8);
1931
1932 fileset.logicalBlockNum = 0xFFFFFFFF;
1933 fileset.partitionReferenceNum = 0xFFFF;
1934
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001935 sbi->s_flags = uopt.flags;
1936 sbi->s_uid = uopt.uid;
1937 sbi->s_gid = uopt.gid;
1938 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001939 sbi->s_fmode = uopt.fmode;
1940 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001941 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001943 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001944 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001946 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001948 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /* Fill in the rest of the superblock */
1951 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02001952 sb->s_export_op = &udf_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 sb->dq_op = NULL;
1954 sb->s_dirt = 0;
1955 sb->s_magic = UDF_SUPER_MAGIC;
1956 sb->s_time_gran = 1000;
1957
Jan Kara40346002009-03-19 16:21:38 +01001958 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
1959 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1960 } else {
Martin K. Petersene1defc42009-05-22 17:17:49 -04001961 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
Jan Kara40346002009-03-19 16:21:38 +01001962 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1963 if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
1964 if (!silent)
1965 printk(KERN_NOTICE
1966 "UDF-fs: Rescanning with blocksize "
1967 "%d\n", UDF_DEFAULT_BLOCKSIZE);
1968 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
1969 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1970 }
1971 }
1972 if (!ret) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001973 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto error_out;
1975 }
1976
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001977 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001979 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001980 struct logicalVolIntegrityDescImpUse *lvidiu =
1981 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001982 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1983 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001984 /* uint16_t maxUDFWriteRev =
1985 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001987 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001988 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1989 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001990 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001991 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001993 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001996 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1999 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2000 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2001 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2002 }
2003
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002004 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002005 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto error_out;
2007 }
2008
Marcin Slusarz4b111112008-02-08 04:20:36 -08002009 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2010 UDF_PART_FLAG_READ_ONLY) {
2011 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
2012 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002013 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002014 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002015
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002016 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002017 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto error_out;
2019 }
2020
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002021 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002022 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002023 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08002024 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002025 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01002026 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2027 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
2029 if (!(sb->s_flags & MS_RDONLY))
2030 udf_open_lvid(sb);
2031
2032 /* Assign the root inode */
2033 /* assign inodes by physical block number */
2034 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002035 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002036 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002037 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2038 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002039 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto error_out;
2041 }
2042
2043 /* Allocate a dentry for the root inode */
2044 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002045 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002046 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 iput(inode);
2048 goto error_out;
2049 }
Jan Kara31170b62007-05-08 00:35:21 -07002050 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
2052
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002053error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002054 if (sbi->s_vat_inode)
2055 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002056 if (sbi->s_partitions)
2057 for (i = 0; i < sbi->s_partitions; i++)
2058 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#ifdef CONFIG_UDF_NLS
2060 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002061 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062#endif
2063 if (!(sb->s_flags & MS_RDONLY))
2064 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002065 brelse(sbi->s_lvid_bh);
2066
2067 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 kfree(sbi);
2069 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return -EINVAL;
2072}
2073
Adrian Bunkb8145a72008-02-17 10:19:55 +02002074static void udf_error(struct super_block *sb, const char *function,
2075 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 va_list args;
2078
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002079 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* mark sb error */
2081 sb->s_dirt = 1;
2082 }
2083 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002084 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002086 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002087 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088}
2089
2090void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002091 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 va_list args;
2094
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002095 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002096 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 va_end(args);
2098 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002099 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002102static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002105 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002107 sbi = UDF_SB(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002108
2109 lock_kernel();
2110
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002111 if (sbi->s_vat_inode)
2112 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002113 if (sbi->s_partitions)
2114 for (i = 0; i < sbi->s_partitions; i++)
2115 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#ifdef CONFIG_UDF_NLS
2117 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002118 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119#endif
2120 if (!(sb->s_flags & MS_RDONLY))
2121 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002122 brelse(sbi->s_lvid_bh);
2123 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 kfree(sb->s_fs_info);
2125 sb->s_fs_info = NULL;
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002126
2127 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Jan Kara146bca72009-03-16 18:27:37 +01002130static int udf_sync_fs(struct super_block *sb, int wait)
2131{
2132 struct udf_sb_info *sbi = UDF_SB(sb);
2133
2134 mutex_lock(&sbi->s_alloc_mutex);
2135 if (sbi->s_lvid_dirty) {
2136 /*
2137 * Blockdevice will be synced later so we don't have to submit
2138 * the buffer for IO
2139 */
2140 mark_buffer_dirty(sbi->s_lvid_bh);
2141 sb->s_dirt = 0;
2142 sbi->s_lvid_dirty = 0;
2143 }
2144 mutex_unlock(&sbi->s_alloc_mutex);
2145
2146 return 0;
2147}
2148
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002149static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
David Howells726c3342006-06-23 02:02:58 -07002151 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002152 struct udf_sb_info *sbi = UDF_SB(sb);
2153 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002154 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002155
2156 if (sbi->s_lvid_bh != NULL)
2157 lvidiu = udf_sb_lvidiu(sbi);
2158 else
2159 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 buf->f_type = UDF_SUPER_MAGIC;
2162 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002163 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 buf->f_bfree = udf_count_free(sb);
2165 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002166 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2167 le32_to_cpu(lvidiu->numDirs)) : 0)
2168 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 buf->f_ffree = buf->f_bfree;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002170 buf->f_namelen = UDF_NAME_LEN - 2;
Coly Li557f5a12009-01-20 01:36:55 +08002171 buf->f_fsid.val[0] = (u32)id;
2172 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 return 0;
2175}
2176
Marcin Slusarz4b111112008-02-08 04:20:36 -08002177static unsigned int udf_count_free_bitmap(struct super_block *sb,
2178 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct buffer_head *bh = NULL;
2181 unsigned int accum = 0;
2182 int index;
2183 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002184 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 uint8_t *ptr;
2187 uint16_t ident;
2188 struct spaceBitmapDesc *bm;
2189
2190 lock_kernel();
2191
2192 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002193 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002194 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002196 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 printk(KERN_ERR "udf: udf_count_free failed\n");
2198 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002199 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002200 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 printk(KERN_ERR "udf: udf_count_free failed\n");
2202 goto out;
2203 }
2204
2205 bm = (struct spaceBitmapDesc *)bh->b_data;
2206 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002207 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2208 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002210 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002211 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2212 accum += bitmap_weight((const unsigned long *)(ptr + index),
2213 cur_bytes * 8);
2214 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002215 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002216 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002217 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002219 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 udf_debug("read failed\n");
2221 goto out;
2222 }
2223 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002224 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002227 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002229out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unlock_kernel();
2231
2232 return accum;
2233}
2234
Marcin Slusarz4b111112008-02-08 04:20:36 -08002235static unsigned int udf_count_free_table(struct super_block *sb,
2236 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002239 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002240 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002242 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 lock_kernel();
2245
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002246 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002247 epos.offset = sizeof(struct unallocSpaceEntry);
2248 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002250 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002252
Jan Kara3bf25cb2007-05-08 00:35:16 -07002253 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 unlock_kernel();
2256
2257 return accum;
2258}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002259
2260static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
2262 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002263 struct udf_sb_info *sbi;
2264 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002266 sbi = UDF_SB(sb);
2267 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002268 struct logicalVolIntegrityDesc *lvid =
2269 (struct logicalVolIntegrityDesc *)
2270 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002271 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002272 accum = le32_to_cpu(
2273 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (accum == 0xFFFFFFFF)
2275 accum = 0;
2276 }
2277 }
2278
2279 if (accum)
2280 return accum;
2281
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002282 map = &sbi->s_partmaps[sbi->s_partition];
2283 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002284 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002285 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002287 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002288 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002289 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291 if (accum)
2292 return accum;
2293
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002294 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002295 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002296 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002298 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002299 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002300 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302
2303 return accum;
2304}