blob: 8364b1719158dfeb3bdbf5364d72efd182e8dae7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020059#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/byteorder.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "udf_sb.h"
63#include "udf_i.h"
64
65#include <linux/init.h>
66#include <asm/uaccess.h>
67
68#define VDS_POS_PRIMARY_VOL_DESC 0
69#define VDS_POS_UNALLOC_SPACE_DESC 1
70#define VDS_POS_LOGICAL_VOL_DESC 2
71#define VDS_POS_PARTITION_DESC 3
72#define VDS_POS_IMP_USE_VOL_DESC 4
73#define VDS_POS_VOL_DESC_PTR 5
74#define VDS_POS_TERMINATING_DESC 6
75#define VDS_POS_LENGTH 7
76
Miklos Szeredi6da80892008-02-08 04:21:50 -080077#define UDF_DEFAULT_BLOCKSIZE 2048
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static char error_buf[1024];
80
81/* These are the "meat" - everything else is stuffing */
82static int udf_fill_super(struct super_block *, void *, int);
83static void udf_put_super(struct super_block *);
84static void udf_write_super(struct super_block *);
85static int udf_remount_fs(struct super_block *, int *, char *);
86static int udf_check_valid(struct super_block *, int, int);
87static int udf_vrs(struct super_block *sb, int silent);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020088static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void udf_find_anchor(struct super_block *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020090static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
91 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020093 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_open_lvid(struct super_block *);
95static void udf_close_lvid(struct super_block *);
96static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070097static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080098static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020099static void udf_error(struct super_block *sb, const char *function,
100 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800102struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
103{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800104 struct logicalVolIntegrityDesc *lvid =
105 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 __u32 offset = number_of_partitions * 2 *
108 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700113static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 int flags, const char *dev_name, void *data,
115 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David Howells454e2392006-06-23 02:02:57 -0700117 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700121 .owner = THIS_MODULE,
122 .name = "udf",
123 .get_sb = udf_get_sb,
124 .kill_sb = kill_block_super,
125 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static struct inode *udf_alloc_inode(struct super_block *sb)
131{
132 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800133 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!ei)
135 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700136
137 ei->i_unique = 0;
138 ei->i_lenExtents = 0;
139 ei->i_next_alloc_block = 0;
140 ei->i_next_alloc_goal = 0;
141 ei->i_strat4096 = 0;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return &ei->vfs_inode;
144}
145
146static void udf_destroy_inode(struct inode *inode)
147{
148 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
149}
150
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700151static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700153 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Lametera35afb82007-05-16 22:10:57 -0700155 ei->i_ext.i_data = NULL;
156 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static int init_inodecache(void)
160{
161 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
162 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 0, (SLAB_RECLAIM_ACCOUNT |
164 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900165 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700166 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -ENOMEM;
168 return 0;
169}
170
171static void destroy_inodecache(void)
172{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700173 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800177static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700178 .alloc_inode = udf_alloc_inode,
179 .destroy_inode = udf_destroy_inode,
180 .write_inode = udf_write_inode,
181 .delete_inode = udf_delete_inode,
182 .clear_inode = udf_clear_inode,
183 .put_super = udf_put_super,
184 .write_super = udf_write_super,
185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800187 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned char novrs;
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
195 unsigned int anchor;
196 unsigned int volume;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
200 unsigned int flags;
201 mode_t umask;
202 gid_t gid;
203 uid_t uid;
204 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");
261 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
262 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);
285 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
286 seq_printf(seq, ",session=%u", sbi->s_session);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
288 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
289 /*
290 * s_anchor[2] could be zeroed out in case there is no anchor
291 * in the specified block, but then the "anchor=N" option
292 * originally given by the user wasn't effective, so it's OK
293 * if we don't show it.
294 */
295 if (sbi->s_anchor[2] != 0)
296 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
297 /*
298 * volume, partition, fileset and rootdir seem to be ignored
299 * currently
300 */
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
302 seq_puts(seq, ",utf8");
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
304 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
305
306 return 0;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * udf_parse_options
311 *
312 * PURPOSE
313 * Parse mount options.
314 *
315 * DESCRIPTION
316 * The following mount options are supported:
317 *
318 * gid= Set the default group.
319 * umask= Set the default umask.
320 * 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,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800369 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
Steven Whitehousea447c092008-10-13 10:46:57 +0100372static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373 {Opt_novrs, "novrs"},
374 {Opt_nostrict, "nostrict"},
375 {Opt_bs, "bs=%u"},
376 {Opt_unhide, "unhide"},
377 {Opt_undelete, "undelete"},
378 {Opt_noadinicb, "noadinicb"},
379 {Opt_adinicb, "adinicb"},
380 {Opt_shortad, "shortad"},
381 {Opt_longad, "longad"},
382 {Opt_uforget, "uid=forget"},
383 {Opt_uignore, "uid=ignore"},
384 {Opt_gforget, "gid=forget"},
385 {Opt_gignore, "gid=ignore"},
386 {Opt_gid, "gid=%u"},
387 {Opt_uid, "uid=%u"},
388 {Opt_umask, "umask=%o"},
389 {Opt_session, "session=%u"},
390 {Opt_lastblock, "lastblock=%u"},
391 {Opt_anchor, "anchor=%u"},
392 {Opt_volume, "volume=%u"},
393 {Opt_partition, "partition=%u"},
394 {Opt_fileset, "fileset=%u"},
395 {Opt_rootdir, "rootdir=%u"},
396 {Opt_utf8, "utf8"},
397 {Opt_iocharset, "iocharset=%s"},
398 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
Miklos Szeredi6da80892008-02-08 04:21:50 -0800401static int udf_parse_options(char *options, struct udf_options *uopt,
402 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 char *p;
405 int option;
406
407 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800408 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 uopt->partition = 0xFFFF;
410 uopt->session = 0xFFFFFFFF;
411 uopt->lastblock = 0;
412 uopt->anchor = 0;
413 uopt->volume = 0xFFFFFFFF;
414 uopt->rootdir = 0xFFFFFFFF;
415 uopt->fileset = 0xFFFFFFFF;
416 uopt->nls_map = NULL;
417
418 if (!options)
419 return 1;
420
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700421 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 substring_t args[MAX_OPT_ARGS];
423 int token;
424 if (!*p)
425 continue;
426
427 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700428 switch (token) {
429 case Opt_novrs:
430 uopt->novrs = 1;
431 case Opt_bs:
432 if (match_int(&args[0], &option))
433 return 0;
434 uopt->blocksize = option;
435 break;
436 case Opt_unhide:
437 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
438 break;
439 case Opt_undelete:
440 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
441 break;
442 case Opt_noadinicb:
443 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
444 break;
445 case Opt_adinicb:
446 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
447 break;
448 case Opt_shortad:
449 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
450 break;
451 case Opt_longad:
452 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
453 break;
454 case Opt_gid:
455 if (match_int(args, &option))
456 return 0;
457 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700458 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700459 break;
460 case Opt_uid:
461 if (match_int(args, &option))
462 return 0;
463 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700464 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 break;
466 case Opt_umask:
467 if (match_octal(args, &option))
468 return 0;
469 uopt->umask = option;
470 break;
471 case Opt_nostrict:
472 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
473 break;
474 case Opt_session:
475 if (match_int(args, &option))
476 return 0;
477 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800478 if (!remount)
479 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700480 break;
481 case Opt_lastblock:
482 if (match_int(args, &option))
483 return 0;
484 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800485 if (!remount)
486 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700487 break;
488 case Opt_anchor:
489 if (match_int(args, &option))
490 return 0;
491 uopt->anchor = option;
492 break;
493 case Opt_volume:
494 if (match_int(args, &option))
495 return 0;
496 uopt->volume = option;
497 break;
498 case Opt_partition:
499 if (match_int(args, &option))
500 return 0;
501 uopt->partition = option;
502 break;
503 case Opt_fileset:
504 if (match_int(args, &option))
505 return 0;
506 uopt->fileset = option;
507 break;
508 case Opt_rootdir:
509 if (match_int(args, &option))
510 return 0;
511 uopt->rootdir = option;
512 break;
513 case Opt_utf8:
514 uopt->flags |= (1 << UDF_FLAG_UTF8);
515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700517 case Opt_iocharset:
518 uopt->nls_map = load_nls(args[0].from);
519 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700522 case Opt_uignore:
523 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
524 break;
525 case Opt_uforget:
526 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
527 break;
528 case Opt_gignore:
529 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
530 break;
531 case Opt_gforget:
532 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
533 break;
534 default:
535 printk(KERN_ERR "udf: bad mount option \"%s\" "
536 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538 }
539 }
540 return 1;
541}
542
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800543static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!(sb->s_flags & MS_RDONLY))
548 udf_open_lvid(sb);
549 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 unlock_kernel();
552}
553
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700554static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800557 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800559 uopt.flags = sbi->s_flags;
560 uopt.uid = sbi->s_uid;
561 uopt.gid = sbi->s_gid;
562 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Miklos Szeredi6da80892008-02-08 04:21:50 -0800564 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
566
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800567 sbi->s_flags = uopt.flags;
568 sbi->s_uid = uopt.uid;
569 sbi->s_gid = uopt.gid;
570 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800572 if (sbi->s_lvid_bh) {
573 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (write_rev > UDF_MAX_WRITE_VERSION)
575 *flags |= MS_RDONLY;
576 }
577
578 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
579 return 0;
580 if (*flags & MS_RDONLY)
581 udf_close_lvid(sb);
582 else
583 udf_open_lvid(sb);
584
585 return 0;
586}
587
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700588static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200591 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int sectorsize;
593 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 int iso9660 = 0;
595 int nsr02 = 0;
596 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800597 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* Block size must be a multiple of 512 */
600 if (sb->s_blocksize & 511)
601 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800602 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (sb->s_blocksize < sizeof(struct volStructDesc))
605 sectorsize = sizeof(struct volStructDesc);
606 else
607 sectorsize = sb->s_blocksize;
608
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800609 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200612 (unsigned int)(sector >> sb->s_blocksize_bits),
613 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Read a block */
617 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
618 if (!bh)
619 break;
620
621 /* Look for ISO descriptors */
622 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800623 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700625 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700626 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800628 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
629 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700631 switch (vsd->structType) {
632 case 0:
633 udf_debug("ISO9660 Boot Record found\n");
634 break;
635 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800636 udf_debug("ISO9660 Primary Volume Descriptor "
637 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700638 break;
639 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800640 udf_debug("ISO9660 Supplementary Volume "
641 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642 break;
643 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800644 udf_debug("ISO9660 Volume Partition Descriptor "
645 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700646 break;
647 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800648 udf_debug("ISO9660 Volume Descriptor Set "
649 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 break;
651 default:
652 udf_debug("ISO9660 VRS (%u) found\n",
653 vsd->structType);
654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800656 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
657 VSD_STD_ID_LEN))
658 ; /* nothing */
659 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
660 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700661 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800663 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
664 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800666 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
667 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700669 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 if (nsr03)
673 return nsr03;
674 else if (nsr02)
675 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800676 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -1;
678 else
679 return 0;
680}
681
682/*
Jan Kara423cf6d2008-04-07 15:59:23 +0200683 * Check whether there is an anchor block in the given block
684 */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200685static int udf_check_anchor_block(struct super_block *sb, sector_t block)
Jan Kara423cf6d2008-04-07 15:59:23 +0200686{
Tomas Janouseke8183c22008-06-23 15:12:35 +0200687 struct buffer_head *bh;
Jan Kara423cf6d2008-04-07 15:59:23 +0200688 uint16_t ident;
Jan Kara423cf6d2008-04-07 15:59:23 +0200689
Tomas Janouseke8183c22008-06-23 15:12:35 +0200690 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
691 udf_fixed_to_variable(block) >=
692 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
693 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200694
Tomas Janouseke8183c22008-06-23 15:12:35 +0200695 bh = udf_read_tagged(sb, block, block, &ident);
Jan Kara423cf6d2008-04-07 15:59:23 +0200696 if (!bh)
697 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200698 brelse(bh);
Tomas Janouseke8183c22008-06-23 15:12:35 +0200699
700 return ident == TAG_IDENT_AVDP;
Jan Kara423cf6d2008-04-07 15:59:23 +0200701}
702
703/* Search for an anchor volume descriptor pointer */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200704static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200705{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200706 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200707 int i;
708 struct udf_sb_info *sbi = UDF_SB(sb);
709
710 last[0] = lastblock;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200711 last[1] = last[0] - 1;
712 last[2] = last[0] + 1;
713 last[3] = last[0] - 2;
714 last[4] = last[0] - 150;
715 last[5] = last[0] - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200716
717 /* according to spec, anchor is in either:
718 * block 256
719 * lastblock-256
720 * lastblock
721 * however, if the disc isn't closed, it could be 512 */
722
723 for (i = 0; i < ARRAY_SIZE(last); i++) {
724 if (last[i] < 0)
725 continue;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200726 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
727 sb->s_blocksize_bits)
728 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200729
Tomas Janouseke8183c22008-06-23 15:12:35 +0200730 if (udf_check_anchor_block(sb, last[i])) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200731 sbi->s_anchor[0] = last[i];
732 sbi->s_anchor[1] = last[i] - 256;
733 return last[i];
734 }
735
736 if (last[i] < 256)
737 continue;
738
Tomas Janouseke8183c22008-06-23 15:12:35 +0200739 if (udf_check_anchor_block(sb, last[i] - 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200740 sbi->s_anchor[1] = last[i] - 256;
741 return last[i];
742 }
743 }
744
Tomas Janouseke8183c22008-06-23 15:12:35 +0200745 if (udf_check_anchor_block(sb, sbi->s_session + 256)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200746 sbi->s_anchor[0] = sbi->s_session + 256;
747 return last[0];
748 }
Tomas Janouseke8183c22008-06-23 15:12:35 +0200749 if (udf_check_anchor_block(sb, sbi->s_session + 512)) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200750 sbi->s_anchor[0] = sbi->s_session + 512;
751 return last[0];
752 }
753 return 0;
754}
755
756/*
757 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
758 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200760 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Jan Kara423cf6d2008-04-07 15:59:23 +0200765 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct buffer_head *bh = NULL;
767 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 int i;
Jan Kara423cf6d2008-04-07 15:59:23 +0200769 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800770
Tomas Janouseke8183c22008-06-23 15:12:35 +0200771 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
Jan Kara423cf6d2008-04-07 15:59:23 +0200772 if (lastblock)
773 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Jan Kara423cf6d2008-04-07 15:59:23 +0200775 /* No anchor found? Try VARCONV conversion of block numbers */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200776 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara423cf6d2008-04-07 15:59:23 +0200777 /* Firstly, we try to not convert number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200778 lastblock = udf_scan_anchors(sb,
Jan Kara423cf6d2008-04-07 15:59:23 +0200779 udf_variable_to_fixed(sbi->s_last_block));
Tomas Janouseke8183c22008-06-23 15:12:35 +0200780 if (lastblock)
Jan Kara423cf6d2008-04-07 15:59:23 +0200781 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Jan Kara423cf6d2008-04-07 15:59:23 +0200783 /* Secondly, we try with converted number of the last block */
Tomas Janouseke8183c22008-06-23 15:12:35 +0200784 lastblock = udf_scan_anchors(sb, sbi->s_last_block);
785 if (!lastblock) {
786 /* VARCONV didn't help. Clear it. */
787 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Jan Kara423cf6d2008-04-07 15:59:23 +0200790check_anchor:
791 /*
792 * Check located anchors and the anchor block supplied via
793 * mount options
794 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800795 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100796 if (!sbi->s_anchor[i])
797 continue;
798 bh = udf_read_tagged(sb, sbi->s_anchor[i],
799 sbi->s_anchor[i], &ident);
800 if (!bh)
801 sbi->s_anchor[i] = 0;
802 else {
803 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200804 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800805 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807 }
808
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800809 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800812static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200813 struct kernel_lb_addr *fileset,
814 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct buffer_head *bh = NULL;
817 long lastblock;
818 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800819 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700822 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200823 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700825 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700827 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700828 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 1;
830 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800835 if (!bh) {
836 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200837 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700839/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700843 (newfileset.partitionReferenceNum != 0xFFFF &&
844 fileset->logicalBlockNum == 0xFFFFFFFF &&
845 fileset->partitionReferenceNum == 0xFFFF);
846 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800847 lastblock = sbi->s_partmaps
848 [newfileset.partitionReferenceNum]
849 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 newfileset.logicalBlockNum = 0;
851
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700852 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200853 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800854 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700855 if (!bh) {
856 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 continue;
858 }
859
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700860 switch (ident) {
861 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700862 {
863 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800864 sp = (struct spaceBitmapDesc *)
865 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700866 newfileset.logicalBlockNum += 1 +
867 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800868 sizeof(struct spaceBitmapDesc)
869 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700870 brelse(bh);
871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700873 case TAG_IDENT_FSD:
874 *fileset = newfileset;
875 break;
876 default:
877 newfileset.logicalBlockNum++;
878 brelse(bh);
879 bh = NULL;
880 break;
881 }
882 } while (newfileset.logicalBlockNum < lastblock &&
883 fileset->logicalBlockNum == 0xFFFFFFFF &&
884 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886 }
887
888 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700889 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700891 fileset->logicalBlockNum,
892 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800894 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700896 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898 }
899 return 1;
900}
901
Jan Karac0eb31e2008-04-01 16:50:35 +0200902static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100905 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200906 struct buffer_head *bh;
907 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100908 int ret = 1;
909
910 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
911 if (!instr)
912 return 1;
913
914 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
915 if (!outstr)
916 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200917
918 bh = udf_read_tagged(sb, block, block, &ident);
919 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100920 goto out2;
921
Jan Karac0eb31e2008-04-01 16:50:35 +0200922 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 pvoldesc = (struct primaryVolDesc *)bh->b_data;
925
Marcin Slusarz56774802008-02-10 11:25:31 +0100926 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
927 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100928#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200929 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100930 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800931 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100932 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
933 ts->minute, le16_to_cpu(ts->typeAndTimezone));
934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100937 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
938 if (udf_CS0toUTF8(outstr, instr)) {
939 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
940 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800941 udf_debug("volIdent[] = '%s'\n",
942 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100945 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
946 if (udf_CS0toUTF8(outstr, instr))
947 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200948
949 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100950 ret = 0;
951out2:
952 kfree(outstr);
953out1:
954 kfree(instr);
955 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Jan Karabfb257a2008-04-08 20:37:21 +0200958static int udf_load_metadata_files(struct super_block *sb, int partition)
959{
960 struct udf_sb_info *sbi = UDF_SB(sb);
961 struct udf_part_map *map;
962 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200963 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200964 int fe_error = 0;
965
966 map = &sbi->s_partmaps[partition];
967 mdata = &map->s_type_specific.s_metadata;
968
969 /* metadata address */
970 addr.logicalBlockNum = mdata->s_meta_file_loc;
971 addr.partitionReferenceNum = map->s_partition_num;
972
973 udf_debug("Metadata file location: block = %d part = %d\n",
974 addr.logicalBlockNum, addr.partitionReferenceNum);
975
Pekka Enberg97e961f2008-10-15 12:29:03 +0200976 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200977
978 if (mdata->s_metadata_fe == NULL) {
979 udf_warning(sb, __func__, "metadata inode efe not found, "
980 "will try mirror inode.");
981 fe_error = 1;
982 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
983 ICBTAG_FLAG_AD_SHORT) {
984 udf_warning(sb, __func__, "metadata inode efe does not have "
985 "short allocation descriptors!");
986 fe_error = 1;
987 iput(mdata->s_metadata_fe);
988 mdata->s_metadata_fe = NULL;
989 }
990
991 /* mirror file entry */
992 addr.logicalBlockNum = mdata->s_mirror_file_loc;
993 addr.partitionReferenceNum = map->s_partition_num;
994
995 udf_debug("Mirror metadata file location: block = %d part = %d\n",
996 addr.logicalBlockNum, addr.partitionReferenceNum);
997
Pekka Enberg97e961f2008-10-15 12:29:03 +0200998 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200999
1000 if (mdata->s_mirror_fe == NULL) {
1001 if (fe_error) {
1002 udf_error(sb, __func__, "mirror inode efe not found "
1003 "and metadata inode is missing too, exiting...");
1004 goto error_exit;
1005 } else
1006 udf_warning(sb, __func__, "mirror inode efe not found,"
1007 " but metadata inode is OK");
1008 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1009 ICBTAG_FLAG_AD_SHORT) {
1010 udf_warning(sb, __func__, "mirror inode efe does not have "
1011 "short allocation descriptors!");
1012 iput(mdata->s_mirror_fe);
1013 mdata->s_mirror_fe = NULL;
1014 if (fe_error)
1015 goto error_exit;
1016 }
1017
1018 /*
1019 * bitmap file entry
1020 * Note:
1021 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1022 */
1023 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1024 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1025 addr.partitionReferenceNum = map->s_partition_num;
1026
1027 udf_debug("Bitmap file location: block = %d part = %d\n",
1028 addr.logicalBlockNum, addr.partitionReferenceNum);
1029
Pekka Enberg97e961f2008-10-15 12:29:03 +02001030 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001031
1032 if (mdata->s_bitmap_fe == NULL) {
1033 if (sb->s_flags & MS_RDONLY)
1034 udf_warning(sb, __func__, "bitmap inode efe "
1035 "not found but it's ok since the disc"
1036 " is mounted read-only");
1037 else {
1038 udf_error(sb, __func__, "bitmap inode efe not "
1039 "found and attempted read-write mount");
1040 goto error_exit;
1041 }
1042 }
1043 }
1044
1045 udf_debug("udf_load_metadata_files Ok\n");
1046
1047 return 0;
1048
1049error_exit:
1050 return 1;
1051}
1052
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001053static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001054 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct fileSetDesc *fset;
1057
1058 fset = (struct fileSetDesc *)bh->b_data;
1059
1060 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1061
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001062 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001064 udf_debug("Rootdir at block=%d, partition=%d\n",
1065 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001068int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1069{
1070 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001071 return DIV_ROUND_UP(map->s_partition_len +
1072 (sizeof(struct spaceBitmapDesc) << 3),
1073 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001074}
1075
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001076static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1077{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001078 struct udf_bitmap *bitmap;
1079 int nr_groups;
1080 int size;
1081
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001082 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001083 size = sizeof(struct udf_bitmap) +
1084 (sizeof(struct buffer_head *) * nr_groups);
1085
1086 if (size <= PAGE_SIZE)
1087 bitmap = kmalloc(size, GFP_KERNEL);
1088 else
1089 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1090
1091 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -07001092 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001093 "Unable to allocate space for bitmap "
1094 "and %d buffer_head pointers", nr_groups);
1095 return NULL;
1096 }
1097
1098 memset(bitmap, 0x00, size);
1099 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1100 bitmap->s_nr_groups = nr_groups;
1101 return bitmap;
1102}
1103
Jan Kara3fb38df2008-04-02 12:26:36 +02001104static int udf_fill_partdesc_info(struct super_block *sb,
1105 struct partitionDesc *p, int p_index)
1106{
1107 struct udf_part_map *map;
1108 struct udf_sb_info *sbi = UDF_SB(sb);
1109 struct partitionHeaderDesc *phd;
1110
1111 map = &sbi->s_partmaps[p_index];
1112
1113 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1114 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1115
1116 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1117 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1118 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1119 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1120 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1121 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1122 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1123 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1124
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001125 udf_debug("Partition (%d type %x) starts at physical %d, "
1126 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001127 map->s_partition_type, map->s_partition_root,
1128 map->s_partition_len);
1129
1130 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1131 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1132 return 0;
1133
1134 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1135 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001136 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001137 .logicalBlockNum = le32_to_cpu(
1138 phd->unallocSpaceTable.extPosition),
1139 .partitionReferenceNum = p_index,
1140 };
1141
Pekka Enberg97e961f2008-10-15 12:29:03 +02001142 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001143 if (!map->s_uspace.s_table) {
1144 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1145 p_index);
1146 return 1;
1147 }
1148 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1149 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1150 p_index, map->s_uspace.s_table->i_ino);
1151 }
1152
1153 if (phd->unallocSpaceBitmap.extLength) {
1154 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1155 if (!bitmap)
1156 return 1;
1157 map->s_uspace.s_bitmap = bitmap;
1158 bitmap->s_extLength = le32_to_cpu(
1159 phd->unallocSpaceBitmap.extLength);
1160 bitmap->s_extPosition = le32_to_cpu(
1161 phd->unallocSpaceBitmap.extPosition);
1162 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1163 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1164 bitmap->s_extPosition);
1165 }
1166
1167 if (phd->partitionIntegrityTable.extLength)
1168 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1169
1170 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001171 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001172 .logicalBlockNum = le32_to_cpu(
1173 phd->freedSpaceTable.extPosition),
1174 .partitionReferenceNum = p_index,
1175 };
1176
Pekka Enberg97e961f2008-10-15 12:29:03 +02001177 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001178 if (!map->s_fspace.s_table) {
1179 udf_debug("cannot load freedSpaceTable (part %d)\n",
1180 p_index);
1181 return 1;
1182 }
1183
1184 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1185 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1186 p_index, map->s_fspace.s_table->i_ino);
1187 }
1188
1189 if (phd->freedSpaceBitmap.extLength) {
1190 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1191 if (!bitmap)
1192 return 1;
1193 map->s_fspace.s_bitmap = bitmap;
1194 bitmap->s_extLength = le32_to_cpu(
1195 phd->freedSpaceBitmap.extLength);
1196 bitmap->s_extPosition = le32_to_cpu(
1197 phd->freedSpaceBitmap.extPosition);
1198 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1199 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1200 bitmap->s_extPosition);
1201 }
1202 return 0;
1203}
1204
Jan Kara38b74a52008-04-02 16:01:35 +02001205static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1206{
1207 struct udf_sb_info *sbi = UDF_SB(sb);
1208 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001209 struct kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001210 struct buffer_head *bh = NULL;
1211 struct udf_inode_info *vati;
1212 uint32_t pos;
1213 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001214
1215 /* VAT file entry is in the last recorded block */
1216 ino.partitionReferenceNum = type1_index;
1217 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
Pekka Enberg97e961f2008-10-15 12:29:03 +02001218 sbi->s_vat_inode = udf_iget(sb, &ino);
Jan Kara38b74a52008-04-02 16:01:35 +02001219 if (!sbi->s_vat_inode)
1220 return 1;
1221
1222 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001223 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001224 map->s_type_specific.s_virtual.s_num_entries =
1225 (sbi->s_vat_inode->i_size - 36) >> 2;
1226 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001227 vati = UDF_I(sbi->s_vat_inode);
1228 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1229 pos = udf_block_map(sbi->s_vat_inode, 0);
1230 bh = sb_bread(sb, pos);
1231 if (!bh)
1232 return 1;
1233 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1234 } else {
1235 vat20 = (struct virtualAllocationTable20 *)
1236 vati->i_ext.i_data;
1237 }
Jan Kara38b74a52008-04-02 16:01:35 +02001238
Jan Kara38b74a52008-04-02 16:01:35 +02001239 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001240 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001241 map->s_type_specific.s_virtual.s_num_entries =
1242 (sbi->s_vat_inode->i_size -
1243 map->s_type_specific.s_virtual.
1244 s_start_offset) >> 2;
1245 brelse(bh);
1246 }
1247 return 0;
1248}
1249
Jan Karac0eb31e2008-04-01 16:50:35 +02001250static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Jan Karac0eb31e2008-04-01 16:50:35 +02001252 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001253 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001254 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001255 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001256 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001257 uint16_t partitionNumber;
1258 uint16_t ident;
1259 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jan Karac0eb31e2008-04-01 16:50:35 +02001261 bh = udf_read_tagged(sb, block, block, &ident);
1262 if (!bh)
1263 return 1;
1264 if (ident != TAG_IDENT_PD)
1265 goto out_bh;
1266
1267 p = (struct partitionDesc *)bh->b_data;
1268 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001269
Jan Karabfb257a2008-04-08 20:37:21 +02001270 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001271 for (i = 0; i < sbi->s_partitions; i++) {
1272 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001273 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001274 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001275 if (map->s_partition_num == partitionNumber &&
1276 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1277 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001279 }
1280
Jan Kara38b74a52008-04-02 16:01:35 +02001281 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001282 udf_debug("Partition (%d) not found in partition map\n",
1283 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001284 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001285 }
1286
Jan Kara3fb38df2008-04-02 12:26:36 +02001287 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001288
1289 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001290 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1291 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001292 */
1293 type1_idx = i;
1294 for (i = 0; i < sbi->s_partitions; i++) {
1295 map = &sbi->s_partmaps[i];
1296
1297 if (map->s_partition_num == partitionNumber &&
1298 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001299 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1300 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001301 break;
1302 }
1303
1304 if (i >= sbi->s_partitions)
1305 goto out_bh;
1306
1307 ret = udf_fill_partdesc_info(sb, p, i);
1308 if (ret)
1309 goto out_bh;
1310
Jan Karabfb257a2008-04-08 20:37:21 +02001311 if (map->s_partition_type == UDF_METADATA_MAP25) {
1312 ret = udf_load_metadata_files(sb, i);
1313 if (ret) {
1314 printk(KERN_ERR "UDF-fs: error loading MetaData "
1315 "partition map %d\n", i);
1316 goto out_bh;
1317 }
1318 } else {
1319 ret = udf_load_vat(sb, i, type1_idx);
1320 if (ret)
1321 goto out_bh;
1322 /*
1323 * Mark filesystem read-only if we have a partition with
1324 * virtual map since we don't handle writing to it (we
1325 * overwrite blocks instead of relocating them).
1326 */
1327 sb->s_flags |= MS_RDONLY;
1328 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1329 "because writing to pseudooverwrite partition is "
1330 "not implemented.\n");
1331 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001332out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001333 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001334 brelse(bh);
1335 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
Jan Karac0eb31e2008-04-01 16:50:35 +02001338static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001339 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 struct logicalVolDesc *lvd;
1342 int i, j, offset;
1343 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001344 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001345 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001346 uint16_t ident;
1347 struct buffer_head *bh;
1348 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jan Karac0eb31e2008-04-01 16:50:35 +02001350 bh = udf_read_tagged(sb, block, block, &ident);
1351 if (!bh)
1352 return 1;
1353 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 lvd = (struct logicalVolDesc *)bh->b_data;
1355
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001356 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001357 if (i != 0) {
1358 ret = i;
1359 goto out_bh;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001362 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001363 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001364 i++, offset += gpm->partitionMapLength) {
1365 struct udf_part_map *map = &sbi->s_partmaps[i];
1366 gpm = (struct genericPartitionMap *)
1367 &(lvd->partitionMaps[offset]);
1368 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001369 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001370 struct genericPartitionMap1 *gpm1 =
1371 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001372 map->s_partition_type = UDF_TYPE1_MAP15;
1373 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1374 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1375 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001376 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001377 struct udfPartitionMap2 *upm2 =
1378 (struct udfPartitionMap2 *)gpm;
1379 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1380 strlen(UDF_ID_VIRTUAL))) {
1381 u16 suf =
1382 le16_to_cpu(((__le16 *)upm2->partIdent.
1383 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001384 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001385 map->s_partition_type =
1386 UDF_VIRTUAL_MAP15;
1387 map->s_partition_func =
1388 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001389 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001390 map->s_partition_type =
1391 UDF_VIRTUAL_MAP20;
1392 map->s_partition_func =
1393 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001395 } else if (!strncmp(upm2->partIdent.ident,
1396 UDF_ID_SPARABLE,
1397 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001400 struct sparablePartitionMap *spm =
1401 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001403 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001404 map->s_type_specific.s_sparing.s_packet_len =
1405 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001406 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001407 struct buffer_head *bh2;
1408
1409 loc = le32_to_cpu(
1410 spm->locSparingTable[j]);
1411 bh2 = udf_read_tagged(sb, loc, loc,
1412 &ident);
1413 map->s_type_specific.s_sparing.
1414 s_spar_map[j] = bh2;
1415
Marcin Slusarz165923f2008-02-10 11:33:08 +01001416 if (bh2 == NULL)
1417 continue;
1418
1419 st = (struct sparingTable *)bh2->b_data;
1420 if (ident != 0 || strncmp(
1421 st->sparingIdent.ident,
1422 UDF_ID_SPARING,
1423 strlen(UDF_ID_SPARING))) {
1424 brelse(bh2);
1425 map->s_type_specific.s_sparing.
1426 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001429 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001430 } else if (!strncmp(upm2->partIdent.ident,
1431 UDF_ID_METADATA,
1432 strlen(UDF_ID_METADATA))) {
1433 struct udf_meta_data *mdata =
1434 &map->s_type_specific.s_metadata;
1435 struct metadataPartitionMap *mdm =
1436 (struct metadataPartitionMap *)
1437 &(lvd->partitionMaps[offset]);
1438 udf_debug("Parsing Logical vol part %d "
1439 "type %d id=%s\n", i, type,
1440 UDF_ID_METADATA);
1441
1442 map->s_partition_type = UDF_METADATA_MAP25;
1443 map->s_partition_func = udf_get_pblock_meta25;
1444
1445 mdata->s_meta_file_loc =
1446 le32_to_cpu(mdm->metadataFileLoc);
1447 mdata->s_mirror_file_loc =
1448 le32_to_cpu(mdm->metadataMirrorFileLoc);
1449 mdata->s_bitmap_file_loc =
1450 le32_to_cpu(mdm->metadataBitmapFileLoc);
1451 mdata->s_alloc_unit_size =
1452 le32_to_cpu(mdm->allocUnitSize);
1453 mdata->s_align_unit_size =
1454 le16_to_cpu(mdm->alignUnitSize);
1455 mdata->s_dup_md_flag =
1456 mdm->flags & 0x01;
1457
1458 udf_debug("Metadata Ident suffix=0x%x\n",
1459 (le16_to_cpu(
1460 ((__le16 *)
1461 mdm->partIdent.identSuffix)[0])));
1462 udf_debug("Metadata part num=%d\n",
1463 le16_to_cpu(mdm->partitionNum));
1464 udf_debug("Metadata part alloc unit size=%d\n",
1465 le32_to_cpu(mdm->allocUnitSize));
1466 udf_debug("Metadata file loc=%d\n",
1467 le32_to_cpu(mdm->metadataFileLoc));
1468 udf_debug("Mirror file loc=%d\n",
1469 le32_to_cpu(mdm->metadataMirrorFileLoc));
1470 udf_debug("Bitmap file loc=%d\n",
1471 le32_to_cpu(mdm->metadataBitmapFileLoc));
1472 udf_debug("Duplicate Flag: %d %d\n",
1473 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001474 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001475 udf_debug("Unknown ident: %s\n",
1476 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 continue;
1478 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001479 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1480 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001483 i, map->s_partition_num, type,
1484 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001487 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001488 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001491 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1492 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001493 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 if (lvd->integritySeqExt.extLength)
1496 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001497
Jan Karac0eb31e2008-04-01 16:50:35 +02001498out_bh:
1499 brelse(bh);
1500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503/*
1504 * udf_load_logicalvolint
1505 *
1506 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001507static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct buffer_head *bh = NULL;
1510 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001511 struct udf_sb_info *sbi = UDF_SB(sb);
1512 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001515 (bh = udf_read_tagged(sb, loc.extLocation,
1516 loc.extLocation, &ident)) &&
1517 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 sbi->s_lvid_bh = bh;
1519 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001521 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001522 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001523 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001524
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001525 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001526 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001528 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001530 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001531 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/*
1535 * udf_process_sequence
1536 *
1537 * PURPOSE
1538 * Process a main/reserve volume descriptor sequence.
1539 *
1540 * PRE-CONDITIONS
1541 * sb Pointer to _locked_ superblock.
1542 * block First block of first extent of the sequence.
1543 * lastblock Lastblock of first extent of the sequence.
1544 *
1545 * HISTORY
1546 * July 1, 1997 - Andrew E. Mileski
1547 * Written, tested, and released.
1548 */
Jan Kara200a3592008-03-04 13:03:09 +01001549static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001550 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct buffer_head *bh = NULL;
1553 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001554 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct generic_desc *gd;
1556 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001557 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 uint32_t vdsn;
1559 uint16_t ident;
1560 long next_s = 0, next_e = 0;
1561
1562 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1563
Jan Karac0eb31e2008-04-01 16:50:35 +02001564 /*
1565 * Read the main descriptor sequence and find which descriptors
1566 * are in it.
1567 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001568 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001571 if (!bh) {
1572 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1573 "sequence is corrupted or we could not read "
1574 "it.\n", (unsigned long long)block);
1575 return 1;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1579 gd = (struct generic_desc *)bh->b_data;
1580 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001581 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001582 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001583 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1584 if (vdsn >= curr->volDescSeqNum) {
1585 curr->volDescSeqNum = vdsn;
1586 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001587 }
1588 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001589 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001590 curr = &vds[VDS_POS_VOL_DESC_PTR];
1591 if (vdsn >= curr->volDescSeqNum) {
1592 curr->volDescSeqNum = vdsn;
1593 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001595 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001596 next_s = le32_to_cpu(
1597 vdp->nextVolDescSeqExt.extLocation);
1598 next_e = le32_to_cpu(
1599 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001600 next_e = next_e >> sb->s_blocksize_bits;
1601 next_e += next_s;
1602 }
1603 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001604 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001605 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1606 if (vdsn >= curr->volDescSeqNum) {
1607 curr->volDescSeqNum = vdsn;
1608 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001609 }
1610 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001611 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001612 curr = &vds[VDS_POS_PARTITION_DESC];
1613 if (!curr->block)
1614 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001615 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001616 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001617 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1618 if (vdsn >= curr->volDescSeqNum) {
1619 curr->volDescSeqNum = vdsn;
1620 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001621 }
1622 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001623 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001624 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1625 if (vdsn >= curr->volDescSeqNum) {
1626 curr->volDescSeqNum = vdsn;
1627 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001628 }
1629 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001630 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001631 vds[VDS_POS_TERMINATING_DESC].block = block;
1632 if (next_e) {
1633 block = next_s;
1634 lastblock = next_e;
1635 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001636 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001637 done = 1;
1638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001640 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001642 /*
1643 * Now read interesting descriptors again and process them
1644 * in a suitable order
1645 */
1646 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1647 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1648 return 1;
1649 }
1650 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1651 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Jan Karac0eb31e2008-04-01 16:50:35 +02001653 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1654 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1655 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001656
Jan Karac0eb31e2008-04-01 16:50:35 +02001657 if (vds[VDS_POS_PARTITION_DESC].block) {
1658 /*
1659 * We rescan the whole descriptor sequence to find
1660 * partition descriptor blocks and process them.
1661 */
1662 for (block = vds[VDS_POS_PARTITION_DESC].block;
1663 block < vds[VDS_POS_TERMINATING_DESC].block;
1664 block++)
1665 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001666 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
1669 return 0;
1670}
1671
1672/*
1673 * udf_check_valid()
1674 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001675static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
1677 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001678 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001680 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 udf_debug("Validity check skipped because of novrs option\n");
1682 return 0;
1683 }
1684 /* Check that it is NSR02 compliant */
1685 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001686 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001687 if (block == -1)
1688 udf_debug("Failed to read byte 32768. Assuming open "
1689 "disc. Skipping validity check\n");
1690 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001691 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001692 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001695static int udf_load_sequence(struct super_block *sb, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct anchorVolDescPtr *anchor;
1698 uint16_t ident;
1699 struct buffer_head *bh;
1700 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001701 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001702 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 if (!sb)
1705 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001706 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001708 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001709 if (!sbi->s_anchor[i])
1710 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001711
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001712 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1713 &ident);
1714 if (!bh)
1715 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001717 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001718
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001719 /* Locate the main sequence */
1720 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1721 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1722 main_e = main_e >> sb->s_blocksize_bits;
1723 main_e += main_s;
1724
1725 /* Locate the reserve sequence */
1726 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001727 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001728 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001729 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001730 reserve_e = reserve_e >> sb->s_blocksize_bits;
1731 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001733 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001735 /* Process the main & reserve sequences */
1736 /* responsible for finding the PartitionDesc(s) */
1737 if (!(udf_process_sequence(sb, main_s, main_e,
1738 fileset) &&
1739 udf_process_sequence(sb, reserve_s, reserve_e,
1740 fileset)))
1741 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001744 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 udf_debug("No Anchor block found\n");
1746 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001747 }
1748 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
1752
1753static void udf_open_lvid(struct super_block *sb)
1754{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001755 struct udf_sb_info *sbi = UDF_SB(sb);
1756 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001757 struct logicalVolIntegrityDesc *lvid;
1758 struct logicalVolIntegrityDescImpUse *lvidiu;
1759 if (!bh)
1760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Marcin Slusarz165923f2008-02-10 11:33:08 +01001762 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1763 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Marcin Slusarz165923f2008-02-10 11:33:08 +01001765 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1766 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1767 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1768 CURRENT_TIME);
1769 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Marcin Slusarz165923f2008-02-10 11:33:08 +01001771 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001772 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001773 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001774
1775 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1776 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779static void udf_close_lvid(struct super_block *sb)
1780{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001781 struct udf_sb_info *sbi = UDF_SB(sb);
1782 struct buffer_head *bh = sbi->s_lvid_bh;
1783 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001784 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001785
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001786 if (!bh)
1787 return;
1788
1789 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1790
Marcin Slusarz165923f2008-02-10 11:33:08 +01001791 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1792 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Marcin Slusarz165923f2008-02-10 11:33:08 +01001794 lvidiu = udf_sb_lvidiu(sbi);
1795 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1796 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1797 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1798 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1799 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1800 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1801 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1802 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1803 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1804 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Marcin Slusarz165923f2008-02-10 11:33:08 +01001806 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001807 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001808 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001809
1810 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1811 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001814static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1815{
1816 int i;
1817 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001818 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1819 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001820
1821 for (i = 0; i < nr_groups; i++)
1822 if (bitmap->s_block_bitmap[i])
1823 brelse(bitmap->s_block_bitmap[i]);
1824
1825 if (size <= PAGE_SIZE)
1826 kfree(bitmap);
1827 else
1828 vfree(bitmap);
1829}
1830
Jan Kara2e0838f2008-04-01 18:08:51 +02001831static void udf_free_partition(struct udf_part_map *map)
1832{
1833 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001834 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001835
1836 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1837 iput(map->s_uspace.s_table);
1838 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1839 iput(map->s_fspace.s_table);
1840 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1841 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1842 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1843 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1844 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1845 for (i = 0; i < 4; i++)
1846 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001847 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1848 mdata = &map->s_type_specific.s_metadata;
1849 iput(mdata->s_metadata_fe);
1850 mdata->s_metadata_fe = NULL;
1851
1852 iput(mdata->s_mirror_fe);
1853 mdata->s_mirror_fe = NULL;
1854
1855 iput(mdata->s_bitmap_fe);
1856 mdata->s_bitmap_fe = NULL;
1857 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001858}
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860static int udf_fill_super(struct super_block *sb, void *options, int silent)
1861{
1862 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001863 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001865 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 struct udf_sb_info *sbi;
1867
1868 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1869 uopt.uid = -1;
1870 uopt.gid = -1;
1871 uopt.umask = 0;
1872
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001873 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!sbi)
1875 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001879 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Miklos Szeredi6da80892008-02-08 04:21:50 -08001881 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto error_out;
1883
1884 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001885 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001887 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto error_out;
1889 }
1890#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001891 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 uopt.nls_map = load_nls_default();
1893 if (!uopt.nls_map)
1894 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1895 else
1896 udf_debug("Using default NLS map\n");
1897 }
1898#endif
1899 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1900 uopt.flags |= (1 << UDF_FLAG_UTF8);
1901
1902 fileset.logicalBlockNum = 0xFFFFFFFF;
1903 fileset.partitionReferenceNum = 0xFFFF;
1904
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001905 sbi->s_flags = uopt.flags;
1906 sbi->s_uid = uopt.uid;
1907 sbi->s_gid = uopt.gid;
1908 sbi->s_umask = uopt.umask;
1909 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001912 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1913 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1914 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001918 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001919 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001921 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001923 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001925 sbi->s_last_block = uopt.lastblock;
1926 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1927 sbi->s_anchor[2] = uopt.anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001929 if (udf_check_valid(sb, uopt.novrs, silent)) {
1930 /* read volume recognition sequences */
1931 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001932 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 udf_find_anchor(sb);
1936
1937 /* Fill in the rest of the superblock */
1938 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02001939 sb->s_export_op = &udf_export_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 sb->dq_op = NULL;
1941 sb->s_dirt = 0;
1942 sb->s_magic = UDF_SUPER_MAGIC;
1943 sb->s_time_gran = 1000;
1944
Jan Kara38b74a52008-04-02 16:01:35 +02001945 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001946 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto error_out;
1948 }
1949
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001950 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001952 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001953 struct logicalVolIntegrityDescImpUse *lvidiu =
1954 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001955 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1956 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001957 /* uint16_t maxUDFWriteRev =
1958 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001960 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001961 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1962 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001963 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001964 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001966 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001969 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1972 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1973 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1974 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1975 }
1976
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001977 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001978 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto error_out;
1980 }
1981
Marcin Slusarz4b111112008-02-08 04:20:36 -08001982 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1983 UDF_PART_FLAG_READ_ONLY) {
1984 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1985 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001986 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001987 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001988
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001989 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001990 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto error_out;
1992 }
1993
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001994 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001995 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01001996 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001997 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001998 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001999 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2000 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002 if (!(sb->s_flags & MS_RDONLY))
2003 udf_open_lvid(sb);
2004
2005 /* Assign the root inode */
2006 /* assign inodes by physical block number */
2007 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002008 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002009 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002010 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2011 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002012 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto error_out;
2014 }
2015
2016 /* Allocate a dentry for the root inode */
2017 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002018 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002019 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 iput(inode);
2021 goto error_out;
2022 }
Jan Kara31170b62007-05-08 00:35:21 -07002023 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return 0;
2025
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002026error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002027 if (sbi->s_vat_inode)
2028 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002029 if (sbi->s_partitions)
2030 for (i = 0; i < sbi->s_partitions; i++)
2031 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#ifdef CONFIG_UDF_NLS
2033 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002034 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035#endif
2036 if (!(sb->s_flags & MS_RDONLY))
2037 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002038 brelse(sbi->s_lvid_bh);
2039
2040 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 kfree(sbi);
2042 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return -EINVAL;
2045}
2046
Adrian Bunkb8145a72008-02-17 10:19:55 +02002047static void udf_error(struct super_block *sb, const char *function,
2048 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
2050 va_list args;
2051
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002052 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /* mark sb error */
2054 sb->s_dirt = 1;
2055 }
2056 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002057 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002059 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002060 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
2063void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002064 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 va_list args;
2067
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002068 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002069 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 va_end(args);
2071 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002072 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002075static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002078 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002080 sbi = UDF_SB(sb);
2081 if (sbi->s_vat_inode)
2082 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002083 if (sbi->s_partitions)
2084 for (i = 0; i < sbi->s_partitions; i++)
2085 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#ifdef CONFIG_UDF_NLS
2087 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089#endif
2090 if (!(sb->s_flags & MS_RDONLY))
2091 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002092 brelse(sbi->s_lvid_bh);
2093 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 kfree(sb->s_fs_info);
2095 sb->s_fs_info = NULL;
2096}
2097
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002098static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
David Howells726c3342006-06-23 02:02:58 -07002100 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002101 struct udf_sb_info *sbi = UDF_SB(sb);
2102 struct logicalVolIntegrityDescImpUse *lvidiu;
2103
2104 if (sbi->s_lvid_bh != NULL)
2105 lvidiu = udf_sb_lvidiu(sbi);
2106 else
2107 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 buf->f_type = UDF_SUPER_MAGIC;
2110 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002111 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 buf->f_bfree = udf_count_free(sb);
2113 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002114 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2115 le32_to_cpu(lvidiu->numDirs)) : 0)
2116 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 buf->f_ffree = buf->f_bfree;
2118 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002119 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 return 0;
2122}
2123
Marcin Slusarz4b111112008-02-08 04:20:36 -08002124static unsigned int udf_count_free_bitmap(struct super_block *sb,
2125 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 struct buffer_head *bh = NULL;
2128 unsigned int accum = 0;
2129 int index;
2130 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002131 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 uint8_t *ptr;
2134 uint16_t ident;
2135 struct spaceBitmapDesc *bm;
2136
2137 lock_kernel();
2138
2139 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002140 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002141 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002143 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 printk(KERN_ERR "udf: udf_count_free failed\n");
2145 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002146 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002147 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 printk(KERN_ERR "udf: udf_count_free failed\n");
2149 goto out;
2150 }
2151
2152 bm = (struct spaceBitmapDesc *)bh->b_data;
2153 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002154 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2155 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002157 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002158 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2159 accum += bitmap_weight((const unsigned long *)(ptr + index),
2160 cur_bytes * 8);
2161 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002162 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002163 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002164 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002166 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 udf_debug("read failed\n");
2168 goto out;
2169 }
2170 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002171 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002174 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002176out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 unlock_kernel();
2178
2179 return accum;
2180}
2181
Marcin Slusarz4b111112008-02-08 04:20:36 -08002182static unsigned int udf_count_free_table(struct super_block *sb,
2183 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002186 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002187 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002189 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 lock_kernel();
2192
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002193 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002194 epos.offset = sizeof(struct unallocSpaceEntry);
2195 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002197 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002199
Jan Kara3bf25cb2007-05-08 00:35:16 -07002200 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 unlock_kernel();
2203
2204 return accum;
2205}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002206
2207static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
2209 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002210 struct udf_sb_info *sbi;
2211 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002213 sbi = UDF_SB(sb);
2214 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002215 struct logicalVolIntegrityDesc *lvid =
2216 (struct logicalVolIntegrityDesc *)
2217 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002218 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002219 accum = le32_to_cpu(
2220 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 if (accum == 0xFFFFFFFF)
2222 accum = 0;
2223 }
2224 }
2225
2226 if (accum)
2227 return accum;
2228
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002229 map = &sbi->s_partmaps[sbi->s_partition];
2230 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002231 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002232 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002234 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002235 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002236 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238 if (accum)
2239 return accum;
2240
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002241 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002242 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002243 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002245 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002246 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002247 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249
2250 return accum;
2251}