blob: b564fc140fe4e90efc24b7b7f231a5676aa979b6 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
89static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
91 kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static void udf_load_fileset(struct super_block *, struct buffer_head *,
93 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
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700151static void init_once(struct kmem_cache *cachep, 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) {
243 udf_error(sb, __FUNCTION__,
244 "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
372static 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 */
685static int udf_check_anchor_block(struct super_block *sb, sector_t block,
686 bool varconv)
687{
688 struct buffer_head *bh = NULL;
689 tag *t;
690 uint16_t ident;
691 uint32_t location;
692
Jan Kara4f7874c2008-04-07 23:16:38 +0200693 if (varconv) {
694 if (udf_fixed_to_variable(block) >=
695 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
696 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200697 bh = sb_bread(sb, udf_fixed_to_variable(block));
Jan Kara4f7874c2008-04-07 23:16:38 +0200698 }
Jan Kara423cf6d2008-04-07 15:59:23 +0200699 else
700 bh = sb_bread(sb, block);
701
702 if (!bh)
703 return 0;
704
705 t = (tag *)bh->b_data;
706 ident = le16_to_cpu(t->tagIdent);
707 location = le32_to_cpu(t->tagLocation);
708 brelse(bh);
709 if (ident != TAG_IDENT_AVDP)
710 return 0;
711 return location == block;
712}
713
714/* Search for an anchor volume descriptor pointer */
715static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
716 sector_t lastblock)
717{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200718 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200719 int i;
720 struct udf_sb_info *sbi = UDF_SB(sb);
721
722 last[0] = lastblock;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200723 last[1] = last[0] - 1;
724 last[2] = last[0] + 1;
725 last[3] = last[0] - 2;
726 last[4] = last[0] - 150;
727 last[5] = last[0] - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200728
729 /* according to spec, anchor is in either:
730 * block 256
731 * lastblock-256
732 * lastblock
733 * however, if the disc isn't closed, it could be 512 */
734
735 for (i = 0; i < ARRAY_SIZE(last); i++) {
736 if (last[i] < 0)
737 continue;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200738 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
739 sb->s_blocksize_bits)
740 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200741
742 if (udf_check_anchor_block(sb, last[i], varconv)) {
743 sbi->s_anchor[0] = last[i];
744 sbi->s_anchor[1] = last[i] - 256;
745 return last[i];
746 }
747
748 if (last[i] < 256)
749 continue;
750
751 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
752 sbi->s_anchor[1] = last[i] - 256;
753 return last[i];
754 }
755 }
756
757 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
758 sbi->s_anchor[0] = sbi->s_session + 256;
759 return last[0];
760 }
761 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
762 sbi->s_anchor[0] = sbi->s_session + 512;
763 return last[0];
764 }
765 return 0;
766}
767
768/*
769 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
770 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200772 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700775static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Jan Kara423cf6d2008-04-07 15:59:23 +0200777 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct buffer_head *bh = NULL;
779 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int i;
Jan Kara423cf6d2008-04-07 15:59:23 +0200781 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800782
Jan Kara423cf6d2008-04-07 15:59:23 +0200783 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
784 if (lastblock)
785 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Jan Kara423cf6d2008-04-07 15:59:23 +0200787 /* No anchor found? Try VARCONV conversion of block numbers */
788 /* Firstly, we try to not convert number of the last block */
789 lastblock = udf_scan_anchors(sb, 1,
790 udf_variable_to_fixed(sbi->s_last_block));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700791 if (lastblock) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200792 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
793 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Jan Kara423cf6d2008-04-07 15:59:23 +0200796 /* Secondly, we try with converted number of the last block */
797 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
798 if (lastblock)
799 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Jan Kara423cf6d2008-04-07 15:59:23 +0200801check_anchor:
802 /*
803 * Check located anchors and the anchor block supplied via
804 * mount options
805 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800806 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100807 if (!sbi->s_anchor[i])
808 continue;
809 bh = udf_read_tagged(sb, sbi->s_anchor[i],
810 sbi->s_anchor[i], &ident);
811 if (!bh)
812 sbi->s_anchor[i] = 0;
813 else {
814 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200815 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800816 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
819
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800820 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800823static int udf_find_fileset(struct super_block *sb,
824 kernel_lb_addr *fileset,
825 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct buffer_head *bh = NULL;
828 long lastblock;
829 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800830 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700833 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
835
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700836 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700838 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700839 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 1;
841 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800845 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800846 if (!bh) {
847 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 kernel_lb_addr newfileset;
849
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700850/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700852
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800853 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 (newfileset.partitionReferenceNum != 0xFFFF &&
855 fileset->logicalBlockNum == 0xFFFFFFFF &&
856 fileset->partitionReferenceNum == 0xFFFF);
857 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800858 lastblock = sbi->s_partmaps
859 [newfileset.partitionReferenceNum]
860 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 newfileset.logicalBlockNum = 0;
862
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800864 bh = udf_read_ptagged(sb, newfileset, 0,
865 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866 if (!bh) {
867 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 continue;
869 }
870
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700871 switch (ident) {
872 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700873 {
874 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800875 sp = (struct spaceBitmapDesc *)
876 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700877 newfileset.logicalBlockNum += 1 +
878 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800879 sizeof(struct spaceBitmapDesc)
880 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700881 brelse(bh);
882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700884 case TAG_IDENT_FSD:
885 *fileset = newfileset;
886 break;
887 default:
888 newfileset.logicalBlockNum++;
889 brelse(bh);
890 bh = NULL;
891 break;
892 }
893 } while (newfileset.logicalBlockNum < lastblock &&
894 fileset->logicalBlockNum == 0xFFFFFFFF &&
895 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 }
898
899 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700900 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700902 fileset->logicalBlockNum,
903 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800905 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700907 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
909 }
910 return 1;
911}
912
Jan Karac0eb31e2008-04-01 16:50:35 +0200913static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 struct primaryVolDesc *pvoldesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct ustr instr;
917 struct ustr outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200918 struct buffer_head *bh;
919 uint16_t ident;
920
921 bh = udf_read_tagged(sb, block, block, &ident);
922 if (!bh)
923 return 1;
924 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 pvoldesc = (struct primaryVolDesc *)bh->b_data;
927
Marcin Slusarz56774802008-02-10 11:25:31 +0100928 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
929 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100930#ifdef UDFFS_DEBUG
931 timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100932 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800933 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100934 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
935 ts->minute, le16_to_cpu(ts->typeAndTimezone));
936#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Marcin Slusarz4b111112008-02-08 04:20:36 -0800939 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700940 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800941 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800943 udf_debug("volIdent[] = '%s'\n",
944 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Marcin Slusarz4b111112008-02-08 04:20:36 -0800947 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (udf_CS0toUTF8(&outstr, &instr))
949 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200950
951 brelse(bh);
952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Jan Karabfb257a2008-04-08 20:37:21 +0200955static int udf_load_metadata_files(struct super_block *sb, int partition)
956{
957 struct udf_sb_info *sbi = UDF_SB(sb);
958 struct udf_part_map *map;
959 struct udf_meta_data *mdata;
960 kernel_lb_addr addr;
961 int fe_error = 0;
962
963 map = &sbi->s_partmaps[partition];
964 mdata = &map->s_type_specific.s_metadata;
965
966 /* metadata address */
967 addr.logicalBlockNum = mdata->s_meta_file_loc;
968 addr.partitionReferenceNum = map->s_partition_num;
969
970 udf_debug("Metadata file location: block = %d part = %d\n",
971 addr.logicalBlockNum, addr.partitionReferenceNum);
972
973 mdata->s_metadata_fe = udf_iget(sb, addr);
974
975 if (mdata->s_metadata_fe == NULL) {
976 udf_warning(sb, __func__, "metadata inode efe not found, "
977 "will try mirror inode.");
978 fe_error = 1;
979 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
980 ICBTAG_FLAG_AD_SHORT) {
981 udf_warning(sb, __func__, "metadata inode efe does not have "
982 "short allocation descriptors!");
983 fe_error = 1;
984 iput(mdata->s_metadata_fe);
985 mdata->s_metadata_fe = NULL;
986 }
987
988 /* mirror file entry */
989 addr.logicalBlockNum = mdata->s_mirror_file_loc;
990 addr.partitionReferenceNum = map->s_partition_num;
991
992 udf_debug("Mirror metadata file location: block = %d part = %d\n",
993 addr.logicalBlockNum, addr.partitionReferenceNum);
994
995 mdata->s_mirror_fe = udf_iget(sb, addr);
996
997 if (mdata->s_mirror_fe == NULL) {
998 if (fe_error) {
999 udf_error(sb, __func__, "mirror inode efe not found "
1000 "and metadata inode is missing too, exiting...");
1001 goto error_exit;
1002 } else
1003 udf_warning(sb, __func__, "mirror inode efe not found,"
1004 " but metadata inode is OK");
1005 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1006 ICBTAG_FLAG_AD_SHORT) {
1007 udf_warning(sb, __func__, "mirror inode efe does not have "
1008 "short allocation descriptors!");
1009 iput(mdata->s_mirror_fe);
1010 mdata->s_mirror_fe = NULL;
1011 if (fe_error)
1012 goto error_exit;
1013 }
1014
1015 /*
1016 * bitmap file entry
1017 * Note:
1018 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1019 */
1020 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1021 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1022 addr.partitionReferenceNum = map->s_partition_num;
1023
1024 udf_debug("Bitmap file location: block = %d part = %d\n",
1025 addr.logicalBlockNum, addr.partitionReferenceNum);
1026
1027 mdata->s_bitmap_fe = udf_iget(sb, addr);
1028
1029 if (mdata->s_bitmap_fe == NULL) {
1030 if (sb->s_flags & MS_RDONLY)
1031 udf_warning(sb, __func__, "bitmap inode efe "
1032 "not found but it's ok since the disc"
1033 " is mounted read-only");
1034 else {
1035 udf_error(sb, __func__, "bitmap inode efe not "
1036 "found and attempted read-write mount");
1037 goto error_exit;
1038 }
1039 }
1040 }
1041
1042 udf_debug("udf_load_metadata_files Ok\n");
1043
1044 return 0;
1045
1046error_exit:
1047 return 1;
1048}
1049
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001050static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1051 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 struct fileSetDesc *fset;
1054
1055 fset = (struct fileSetDesc *)bh->b_data;
1056
1057 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1058
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001059 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001061 udf_debug("Rootdir at block=%d, partition=%d\n",
1062 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001065int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1066{
1067 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001068 return DIV_ROUND_UP(map->s_partition_len +
1069 (sizeof(struct spaceBitmapDesc) << 3),
1070 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001071}
1072
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001073static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1074{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001075 struct udf_bitmap *bitmap;
1076 int nr_groups;
1077 int size;
1078
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001079 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001080 size = sizeof(struct udf_bitmap) +
1081 (sizeof(struct buffer_head *) * nr_groups);
1082
1083 if (size <= PAGE_SIZE)
1084 bitmap = kmalloc(size, GFP_KERNEL);
1085 else
1086 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1087
1088 if (bitmap == NULL) {
1089 udf_error(sb, __FUNCTION__,
1090 "Unable to allocate space for bitmap "
1091 "and %d buffer_head pointers", nr_groups);
1092 return NULL;
1093 }
1094
1095 memset(bitmap, 0x00, size);
1096 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1097 bitmap->s_nr_groups = nr_groups;
1098 return bitmap;
1099}
1100
Jan Kara3fb38df2008-04-02 12:26:36 +02001101static int udf_fill_partdesc_info(struct super_block *sb,
1102 struct partitionDesc *p, int p_index)
1103{
1104 struct udf_part_map *map;
1105 struct udf_sb_info *sbi = UDF_SB(sb);
1106 struct partitionHeaderDesc *phd;
1107
1108 map = &sbi->s_partmaps[p_index];
1109
1110 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1111 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1112
1113 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1114 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1115 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1116 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1117 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1118 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1119 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1120 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1121
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001122 udf_debug("Partition (%d type %x) starts at physical %d, "
1123 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001124 map->s_partition_type, map->s_partition_root,
1125 map->s_partition_len);
1126
1127 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1128 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1129 return 0;
1130
1131 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1132 if (phd->unallocSpaceTable.extLength) {
1133 kernel_lb_addr loc = {
1134 .logicalBlockNum = le32_to_cpu(
1135 phd->unallocSpaceTable.extPosition),
1136 .partitionReferenceNum = p_index,
1137 };
1138
1139 map->s_uspace.s_table = udf_iget(sb, loc);
1140 if (!map->s_uspace.s_table) {
1141 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1142 p_index);
1143 return 1;
1144 }
1145 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1146 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1147 p_index, map->s_uspace.s_table->i_ino);
1148 }
1149
1150 if (phd->unallocSpaceBitmap.extLength) {
1151 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1152 if (!bitmap)
1153 return 1;
1154 map->s_uspace.s_bitmap = bitmap;
1155 bitmap->s_extLength = le32_to_cpu(
1156 phd->unallocSpaceBitmap.extLength);
1157 bitmap->s_extPosition = le32_to_cpu(
1158 phd->unallocSpaceBitmap.extPosition);
1159 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1160 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1161 bitmap->s_extPosition);
1162 }
1163
1164 if (phd->partitionIntegrityTable.extLength)
1165 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1166
1167 if (phd->freedSpaceTable.extLength) {
1168 kernel_lb_addr loc = {
1169 .logicalBlockNum = le32_to_cpu(
1170 phd->freedSpaceTable.extPosition),
1171 .partitionReferenceNum = p_index,
1172 };
1173
1174 map->s_fspace.s_table = udf_iget(sb, loc);
1175 if (!map->s_fspace.s_table) {
1176 udf_debug("cannot load freedSpaceTable (part %d)\n",
1177 p_index);
1178 return 1;
1179 }
1180
1181 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1182 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1183 p_index, map->s_fspace.s_table->i_ino);
1184 }
1185
1186 if (phd->freedSpaceBitmap.extLength) {
1187 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1188 if (!bitmap)
1189 return 1;
1190 map->s_fspace.s_bitmap = bitmap;
1191 bitmap->s_extLength = le32_to_cpu(
1192 phd->freedSpaceBitmap.extLength);
1193 bitmap->s_extPosition = le32_to_cpu(
1194 phd->freedSpaceBitmap.extPosition);
1195 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1196 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1197 bitmap->s_extPosition);
1198 }
1199 return 0;
1200}
1201
Jan Kara38b74a52008-04-02 16:01:35 +02001202static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1203{
1204 struct udf_sb_info *sbi = UDF_SB(sb);
1205 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1206 kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001207 struct buffer_head *bh = NULL;
1208 struct udf_inode_info *vati;
1209 uint32_t pos;
1210 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001211
1212 /* VAT file entry is in the last recorded block */
1213 ino.partitionReferenceNum = type1_index;
1214 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1215 sbi->s_vat_inode = udf_iget(sb, ino);
1216 if (!sbi->s_vat_inode)
1217 return 1;
1218
1219 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001220 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001221 map->s_type_specific.s_virtual.s_num_entries =
1222 (sbi->s_vat_inode->i_size - 36) >> 2;
1223 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001224 vati = UDF_I(sbi->s_vat_inode);
1225 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1226 pos = udf_block_map(sbi->s_vat_inode, 0);
1227 bh = sb_bread(sb, pos);
1228 if (!bh)
1229 return 1;
1230 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1231 } else {
1232 vat20 = (struct virtualAllocationTable20 *)
1233 vati->i_ext.i_data;
1234 }
Jan Kara38b74a52008-04-02 16:01:35 +02001235
Jan Kara38b74a52008-04-02 16:01:35 +02001236 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001237 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001238 map->s_type_specific.s_virtual.s_num_entries =
1239 (sbi->s_vat_inode->i_size -
1240 map->s_type_specific.s_virtual.
1241 s_start_offset) >> 2;
1242 brelse(bh);
1243 }
1244 return 0;
1245}
1246
Jan Karac0eb31e2008-04-01 16:50:35 +02001247static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Jan Karac0eb31e2008-04-01 16:50:35 +02001249 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001250 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001251 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001252 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001253 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001254 uint16_t partitionNumber;
1255 uint16_t ident;
1256 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jan Karac0eb31e2008-04-01 16:50:35 +02001258 bh = udf_read_tagged(sb, block, block, &ident);
1259 if (!bh)
1260 return 1;
1261 if (ident != TAG_IDENT_PD)
1262 goto out_bh;
1263
1264 p = (struct partitionDesc *)bh->b_data;
1265 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001266
Jan Karabfb257a2008-04-08 20:37:21 +02001267 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001268 for (i = 0; i < sbi->s_partitions; i++) {
1269 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001271 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001272 if (map->s_partition_num == partitionNumber &&
1273 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1274 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001276 }
1277
Jan Kara38b74a52008-04-02 16:01:35 +02001278 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001279 udf_debug("Partition (%d) not found in partition map\n",
1280 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001281 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001282 }
1283
Jan Kara3fb38df2008-04-02 12:26:36 +02001284 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001285
1286 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001287 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1288 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001289 */
1290 type1_idx = i;
1291 for (i = 0; i < sbi->s_partitions; i++) {
1292 map = &sbi->s_partmaps[i];
1293
1294 if (map->s_partition_num == partitionNumber &&
1295 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001296 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1297 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001298 break;
1299 }
1300
1301 if (i >= sbi->s_partitions)
1302 goto out_bh;
1303
1304 ret = udf_fill_partdesc_info(sb, p, i);
1305 if (ret)
1306 goto out_bh;
1307
Jan Karabfb257a2008-04-08 20:37:21 +02001308 if (map->s_partition_type == UDF_METADATA_MAP25) {
1309 ret = udf_load_metadata_files(sb, i);
1310 if (ret) {
1311 printk(KERN_ERR "UDF-fs: error loading MetaData "
1312 "partition map %d\n", i);
1313 goto out_bh;
1314 }
1315 } else {
1316 ret = udf_load_vat(sb, i, type1_idx);
1317 if (ret)
1318 goto out_bh;
1319 /*
1320 * Mark filesystem read-only if we have a partition with
1321 * virtual map since we don't handle writing to it (we
1322 * overwrite blocks instead of relocating them).
1323 */
1324 sb->s_flags |= MS_RDONLY;
1325 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1326 "because writing to pseudooverwrite partition is "
1327 "not implemented.\n");
1328 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001329out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001330 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001331 brelse(bh);
1332 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Jan Karac0eb31e2008-04-01 16:50:35 +02001335static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001336 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct logicalVolDesc *lvd;
1339 int i, j, offset;
1340 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001341 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001342 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001343 uint16_t ident;
1344 struct buffer_head *bh;
1345 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Jan Karac0eb31e2008-04-01 16:50:35 +02001347 bh = udf_read_tagged(sb, block, block, &ident);
1348 if (!bh)
1349 return 1;
1350 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 lvd = (struct logicalVolDesc *)bh->b_data;
1352
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001353 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001354 if (i != 0) {
1355 ret = i;
1356 goto out_bh;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001359 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001360 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001361 i++, offset += gpm->partitionMapLength) {
1362 struct udf_part_map *map = &sbi->s_partmaps[i];
1363 gpm = (struct genericPartitionMap *)
1364 &(lvd->partitionMaps[offset]);
1365 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001366 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001367 struct genericPartitionMap1 *gpm1 =
1368 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001369 map->s_partition_type = UDF_TYPE1_MAP15;
1370 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1371 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1372 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001373 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001374 struct udfPartitionMap2 *upm2 =
1375 (struct udfPartitionMap2 *)gpm;
1376 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1377 strlen(UDF_ID_VIRTUAL))) {
1378 u16 suf =
1379 le16_to_cpu(((__le16 *)upm2->partIdent.
1380 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001381 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001382 map->s_partition_type =
1383 UDF_VIRTUAL_MAP15;
1384 map->s_partition_func =
1385 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001386 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001387 map->s_partition_type =
1388 UDF_VIRTUAL_MAP20;
1389 map->s_partition_func =
1390 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001392 } else if (!strncmp(upm2->partIdent.ident,
1393 UDF_ID_SPARABLE,
1394 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001397 struct sparablePartitionMap *spm =
1398 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001400 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001401 map->s_type_specific.s_sparing.s_packet_len =
1402 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001403 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001404 struct buffer_head *bh2;
1405
1406 loc = le32_to_cpu(
1407 spm->locSparingTable[j]);
1408 bh2 = udf_read_tagged(sb, loc, loc,
1409 &ident);
1410 map->s_type_specific.s_sparing.
1411 s_spar_map[j] = bh2;
1412
Marcin Slusarz165923f2008-02-10 11:33:08 +01001413 if (bh2 == NULL)
1414 continue;
1415
1416 st = (struct sparingTable *)bh2->b_data;
1417 if (ident != 0 || strncmp(
1418 st->sparingIdent.ident,
1419 UDF_ID_SPARING,
1420 strlen(UDF_ID_SPARING))) {
1421 brelse(bh2);
1422 map->s_type_specific.s_sparing.
1423 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001426 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001427 } else if (!strncmp(upm2->partIdent.ident,
1428 UDF_ID_METADATA,
1429 strlen(UDF_ID_METADATA))) {
1430 struct udf_meta_data *mdata =
1431 &map->s_type_specific.s_metadata;
1432 struct metadataPartitionMap *mdm =
1433 (struct metadataPartitionMap *)
1434 &(lvd->partitionMaps[offset]);
1435 udf_debug("Parsing Logical vol part %d "
1436 "type %d id=%s\n", i, type,
1437 UDF_ID_METADATA);
1438
1439 map->s_partition_type = UDF_METADATA_MAP25;
1440 map->s_partition_func = udf_get_pblock_meta25;
1441
1442 mdata->s_meta_file_loc =
1443 le32_to_cpu(mdm->metadataFileLoc);
1444 mdata->s_mirror_file_loc =
1445 le32_to_cpu(mdm->metadataMirrorFileLoc);
1446 mdata->s_bitmap_file_loc =
1447 le32_to_cpu(mdm->metadataBitmapFileLoc);
1448 mdata->s_alloc_unit_size =
1449 le32_to_cpu(mdm->allocUnitSize);
1450 mdata->s_align_unit_size =
1451 le16_to_cpu(mdm->alignUnitSize);
1452 mdata->s_dup_md_flag =
1453 mdm->flags & 0x01;
1454
1455 udf_debug("Metadata Ident suffix=0x%x\n",
1456 (le16_to_cpu(
1457 ((__le16 *)
1458 mdm->partIdent.identSuffix)[0])));
1459 udf_debug("Metadata part num=%d\n",
1460 le16_to_cpu(mdm->partitionNum));
1461 udf_debug("Metadata part alloc unit size=%d\n",
1462 le32_to_cpu(mdm->allocUnitSize));
1463 udf_debug("Metadata file loc=%d\n",
1464 le32_to_cpu(mdm->metadataFileLoc));
1465 udf_debug("Mirror file loc=%d\n",
1466 le32_to_cpu(mdm->metadataMirrorFileLoc));
1467 udf_debug("Bitmap file loc=%d\n",
1468 le32_to_cpu(mdm->metadataBitmapFileLoc));
1469 udf_debug("Duplicate Flag: %d %d\n",
1470 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001471 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001472 udf_debug("Unknown ident: %s\n",
1473 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 continue;
1475 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001476 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1477 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001480 i, map->s_partition_num, type,
1481 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001484 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001485 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001488 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1489 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001490 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 if (lvd->integritySeqExt.extLength)
1493 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001494
Jan Karac0eb31e2008-04-01 16:50:35 +02001495out_bh:
1496 brelse(bh);
1497 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/*
1501 * udf_load_logicalvolint
1502 *
1503 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001504static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
1506 struct buffer_head *bh = NULL;
1507 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001508 struct udf_sb_info *sbi = UDF_SB(sb);
1509 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001512 (bh = udf_read_tagged(sb, loc.extLocation,
1513 loc.extLocation, &ident)) &&
1514 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001515 sbi->s_lvid_bh = bh;
1516 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001517
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001519 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001520 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001521
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001522 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001523 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001525 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001527 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001528 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
1532 * udf_process_sequence
1533 *
1534 * PURPOSE
1535 * Process a main/reserve volume descriptor sequence.
1536 *
1537 * PRE-CONDITIONS
1538 * sb Pointer to _locked_ superblock.
1539 * block First block of first extent of the sequence.
1540 * lastblock Lastblock of first extent of the sequence.
1541 *
1542 * HISTORY
1543 * July 1, 1997 - Andrew E. Mileski
1544 * Written, tested, and released.
1545 */
Jan Kara200a3592008-03-04 13:03:09 +01001546static noinline int udf_process_sequence(struct super_block *sb, long block,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001547 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct buffer_head *bh = NULL;
1550 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001551 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 struct generic_desc *gd;
1553 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001554 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 uint32_t vdsn;
1556 uint16_t ident;
1557 long next_s = 0, next_e = 0;
1558
1559 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1560
Jan Karac0eb31e2008-04-01 16:50:35 +02001561 /*
1562 * Read the main descriptor sequence and find which descriptors
1563 * are in it.
1564 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001565 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001568 if (!bh) {
1569 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1570 "sequence is corrupted or we could not read "
1571 "it.\n", (unsigned long long)block);
1572 return 1;
1573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1576 gd = (struct generic_desc *)bh->b_data;
1577 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001578 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001579 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001580 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1581 if (vdsn >= curr->volDescSeqNum) {
1582 curr->volDescSeqNum = vdsn;
1583 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001584 }
1585 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001586 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001587 curr = &vds[VDS_POS_VOL_DESC_PTR];
1588 if (vdsn >= curr->volDescSeqNum) {
1589 curr->volDescSeqNum = vdsn;
1590 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001592 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001593 next_s = le32_to_cpu(
1594 vdp->nextVolDescSeqExt.extLocation);
1595 next_e = le32_to_cpu(
1596 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001597 next_e = next_e >> sb->s_blocksize_bits;
1598 next_e += next_s;
1599 }
1600 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001601 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001602 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1603 if (vdsn >= curr->volDescSeqNum) {
1604 curr->volDescSeqNum = vdsn;
1605 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001606 }
1607 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001608 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001609 curr = &vds[VDS_POS_PARTITION_DESC];
1610 if (!curr->block)
1611 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001612 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001613 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001614 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1615 if (vdsn >= curr->volDescSeqNum) {
1616 curr->volDescSeqNum = vdsn;
1617 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001618 }
1619 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001620 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001621 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1622 if (vdsn >= curr->volDescSeqNum) {
1623 curr->volDescSeqNum = vdsn;
1624 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001625 }
1626 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001627 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001628 vds[VDS_POS_TERMINATING_DESC].block = block;
1629 if (next_e) {
1630 block = next_s;
1631 lastblock = next_e;
1632 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001633 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001634 done = 1;
1635 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001637 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001639 /*
1640 * Now read interesting descriptors again and process them
1641 * in a suitable order
1642 */
1643 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1644 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1645 return 1;
1646 }
1647 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1648 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Jan Karac0eb31e2008-04-01 16:50:35 +02001650 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1651 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1652 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001653
Jan Karac0eb31e2008-04-01 16:50:35 +02001654 if (vds[VDS_POS_PARTITION_DESC].block) {
1655 /*
1656 * We rescan the whole descriptor sequence to find
1657 * partition descriptor blocks and process them.
1658 */
1659 for (block = vds[VDS_POS_PARTITION_DESC].block;
1660 block < vds[VDS_POS_TERMINATING_DESC].block;
1661 block++)
1662 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001663 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 return 0;
1667}
1668
1669/*
1670 * udf_check_valid()
1671 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001672static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001675 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001677 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 udf_debug("Validity check skipped because of novrs option\n");
1679 return 0;
1680 }
1681 /* Check that it is NSR02 compliant */
1682 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001683 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001684 if (block == -1)
1685 udf_debug("Failed to read byte 32768. Assuming open "
1686 "disc. Skipping validity check\n");
1687 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001688 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001689 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Jan Kara38b74a52008-04-02 16:01:35 +02001692static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct anchorVolDescPtr *anchor;
1695 uint16_t ident;
1696 struct buffer_head *bh;
1697 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001698 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001699 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 if (!sb)
1702 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001703 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001705 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001706 if (!sbi->s_anchor[i])
1707 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001708
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001709 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1710 &ident);
1711 if (!bh)
1712 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001714 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001715
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001716 /* Locate the main sequence */
1717 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1718 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1719 main_e = main_e >> sb->s_blocksize_bits;
1720 main_e += main_s;
1721
1722 /* Locate the reserve sequence */
1723 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001724 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001725 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001726 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001727 reserve_e = reserve_e >> sb->s_blocksize_bits;
1728 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001730 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001732 /* Process the main & reserve sequences */
1733 /* responsible for finding the PartitionDesc(s) */
1734 if (!(udf_process_sequence(sb, main_s, main_e,
1735 fileset) &&
1736 udf_process_sequence(sb, reserve_s, reserve_e,
1737 fileset)))
1738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 }
1740
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001741 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 udf_debug("No Anchor block found\n");
1743 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001744 }
1745 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return 0;
1748}
1749
1750static void udf_open_lvid(struct super_block *sb)
1751{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001752 struct udf_sb_info *sbi = UDF_SB(sb);
1753 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001754 struct logicalVolIntegrityDesc *lvid;
1755 struct logicalVolIntegrityDescImpUse *lvidiu;
1756 if (!bh)
1757 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Marcin Slusarz165923f2008-02-10 11:33:08 +01001759 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1760 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Marcin Slusarz165923f2008-02-10 11:33:08 +01001762 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1763 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1764 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1765 CURRENT_TIME);
1766 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Marcin Slusarz165923f2008-02-10 11:33:08 +01001768 lvid->descTag.descCRC = cpu_to_le16(
Bob Copelandf845fce2008-04-17 09:47:48 +02001769 crc_itu_t(0, (char *)lvid + sizeof(tag),
1770 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001771
1772 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1773 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776static void udf_close_lvid(struct super_block *sb)
1777{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001778 struct udf_sb_info *sbi = UDF_SB(sb);
1779 struct buffer_head *bh = sbi->s_lvid_bh;
1780 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001781 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001782
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001783 if (!bh)
1784 return;
1785
1786 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1787
Marcin Slusarz165923f2008-02-10 11:33:08 +01001788 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1789 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Marcin Slusarz165923f2008-02-10 11:33:08 +01001791 lvidiu = udf_sb_lvidiu(sbi);
1792 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1793 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1794 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1795 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1796 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1797 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1798 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1799 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1800 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1801 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Marcin Slusarz165923f2008-02-10 11:33:08 +01001803 lvid->descTag.descCRC = cpu_to_le16(
Bob Copelandf845fce2008-04-17 09:47:48 +02001804 crc_itu_t(0, (char *)lvid + sizeof(tag),
1805 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001806
1807 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1808 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001811static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1812{
1813 int i;
1814 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001815 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1816 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001817
1818 for (i = 0; i < nr_groups; i++)
1819 if (bitmap->s_block_bitmap[i])
1820 brelse(bitmap->s_block_bitmap[i]);
1821
1822 if (size <= PAGE_SIZE)
1823 kfree(bitmap);
1824 else
1825 vfree(bitmap);
1826}
1827
Jan Kara2e0838f2008-04-01 18:08:51 +02001828static void udf_free_partition(struct udf_part_map *map)
1829{
1830 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001831 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001832
1833 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1834 iput(map->s_uspace.s_table);
1835 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1836 iput(map->s_fspace.s_table);
1837 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1838 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1839 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1840 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1841 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1842 for (i = 0; i < 4; i++)
1843 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001844 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1845 mdata = &map->s_type_specific.s_metadata;
1846 iput(mdata->s_metadata_fe);
1847 mdata->s_metadata_fe = NULL;
1848
1849 iput(mdata->s_mirror_fe);
1850 mdata->s_mirror_fe = NULL;
1851
1852 iput(mdata->s_bitmap_fe);
1853 mdata->s_bitmap_fe = NULL;
1854 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001855}
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int udf_fill_super(struct super_block *sb, void *options, int silent)
1858{
1859 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001860 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct udf_options uopt;
1862 kernel_lb_addr rootdir, fileset;
1863 struct udf_sb_info *sbi;
1864
1865 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1866 uopt.uid = -1;
1867 uopt.gid = -1;
1868 uopt.umask = 0;
1869
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001870 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (!sbi)
1872 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001876 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Miklos Szeredi6da80892008-02-08 04:21:50 -08001878 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto error_out;
1880
1881 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001882 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001884 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto error_out;
1886 }
1887#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001888 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 uopt.nls_map = load_nls_default();
1890 if (!uopt.nls_map)
1891 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1892 else
1893 udf_debug("Using default NLS map\n");
1894 }
1895#endif
1896 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1897 uopt.flags |= (1 << UDF_FLAG_UTF8);
1898
1899 fileset.logicalBlockNum = 0xFFFFFFFF;
1900 fileset.partitionReferenceNum = 0xFFFF;
1901
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001902 sbi->s_flags = uopt.flags;
1903 sbi->s_uid = uopt.uid;
1904 sbi->s_gid = uopt.gid;
1905 sbi->s_umask = uopt.umask;
1906 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001909 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1910 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1911 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001915 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001916 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001918 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001920 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001922 sbi->s_last_block = uopt.lastblock;
1923 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1924 sbi->s_anchor[2] = uopt.anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001926 if (udf_check_valid(sb, uopt.novrs, silent)) {
1927 /* read volume recognition sequences */
1928 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001929 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931
1932 udf_find_anchor(sb);
1933
1934 /* Fill in the rest of the superblock */
1935 sb->s_op = &udf_sb_ops;
1936 sb->dq_op = NULL;
1937 sb->s_dirt = 0;
1938 sb->s_magic = UDF_SUPER_MAGIC;
1939 sb->s_time_gran = 1000;
1940
Jan Kara38b74a52008-04-02 16:01:35 +02001941 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001942 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 goto error_out;
1944 }
1945
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001946 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001948 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001949 struct logicalVolIntegrityDescImpUse *lvidiu =
1950 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001951 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1952 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001953 /* uint16_t maxUDFWriteRev =
1954 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001956 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001957 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1958 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001959 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001960 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001962 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001965 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1968 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1969 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1970 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1971 }
1972
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001973 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001974 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto error_out;
1976 }
1977
Marcin Slusarz4b111112008-02-08 04:20:36 -08001978 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1979 UDF_PART_FLAG_READ_ONLY) {
1980 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1981 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001982 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001983 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001984
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001985 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001986 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto error_out;
1988 }
1989
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001990 if (!silent) {
Marcin Slusarz56774802008-02-10 11:25:31 +01001991 timestamp ts;
1992 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001993 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001994 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001995 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1996 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
1998 if (!(sb->s_flags & MS_RDONLY))
1999 udf_open_lvid(sb);
2000
2001 /* Assign the root inode */
2002 /* assign inodes by physical block number */
2003 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002004 inode = udf_iget(sb, rootdir);
2005 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002006 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2007 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002008 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 goto error_out;
2010 }
2011
2012 /* Allocate a dentry for the root inode */
2013 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002014 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002015 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 iput(inode);
2017 goto error_out;
2018 }
Jan Kara31170b62007-05-08 00:35:21 -07002019 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 return 0;
2021
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002022error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002023 if (sbi->s_vat_inode)
2024 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002025 if (sbi->s_partitions)
2026 for (i = 0; i < sbi->s_partitions; i++)
2027 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028#ifdef CONFIG_UDF_NLS
2029 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002030 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031#endif
2032 if (!(sb->s_flags & MS_RDONLY))
2033 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002034 brelse(sbi->s_lvid_bh);
2035
2036 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 kfree(sbi);
2038 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return -EINVAL;
2041}
2042
Adrian Bunkb8145a72008-02-17 10:19:55 +02002043static void udf_error(struct super_block *sb, const char *function,
2044 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 va_list args;
2047
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002048 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /* mark sb error */
2050 sb->s_dirt = 1;
2051 }
2052 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002053 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002055 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002056 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
2059void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002060 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 va_list args;
2063
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002064 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002065 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 va_end(args);
2067 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002068 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002071static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002074 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002076 sbi = UDF_SB(sb);
2077 if (sbi->s_vat_inode)
2078 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002079 if (sbi->s_partitions)
2080 for (i = 0; i < sbi->s_partitions; i++)
2081 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#ifdef CONFIG_UDF_NLS
2083 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002084 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085#endif
2086 if (!(sb->s_flags & MS_RDONLY))
2087 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 brelse(sbi->s_lvid_bh);
2089 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 kfree(sb->s_fs_info);
2091 sb->s_fs_info = NULL;
2092}
2093
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002094static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
David Howells726c3342006-06-23 02:02:58 -07002096 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002097 struct udf_sb_info *sbi = UDF_SB(sb);
2098 struct logicalVolIntegrityDescImpUse *lvidiu;
2099
2100 if (sbi->s_lvid_bh != NULL)
2101 lvidiu = udf_sb_lvidiu(sbi);
2102 else
2103 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 buf->f_type = UDF_SUPER_MAGIC;
2106 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002107 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 buf->f_bfree = udf_count_free(sb);
2109 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002110 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2111 le32_to_cpu(lvidiu->numDirs)) : 0)
2112 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 buf->f_ffree = buf->f_bfree;
2114 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002115 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 return 0;
2118}
2119
Marcin Slusarz4b111112008-02-08 04:20:36 -08002120static unsigned int udf_count_free_bitmap(struct super_block *sb,
2121 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 struct buffer_head *bh = NULL;
2124 unsigned int accum = 0;
2125 int index;
2126 int block = 0, newblock;
2127 kernel_lb_addr loc;
2128 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 uint8_t *ptr;
2130 uint16_t ident;
2131 struct spaceBitmapDesc *bm;
2132
2133 lock_kernel();
2134
2135 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002136 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 bh = udf_read_ptagged(sb, loc, 0, &ident);
2138
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002139 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 printk(KERN_ERR "udf: udf_count_free failed\n");
2141 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002142 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002143 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 printk(KERN_ERR "udf: udf_count_free failed\n");
2145 goto out;
2146 }
2147
2148 bm = (struct spaceBitmapDesc *)bh->b_data;
2149 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002150 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2151 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002153 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002154 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2155 accum += bitmap_weight((const unsigned long *)(ptr + index),
2156 cur_bytes * 8);
2157 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002158 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002159 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 newblock = udf_get_lb_pblock(sb, loc, ++block);
2161 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002162 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 udf_debug("read failed\n");
2164 goto out;
2165 }
2166 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002167 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002170 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002172out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 unlock_kernel();
2174
2175 return accum;
2176}
2177
Marcin Slusarz4b111112008-02-08 04:20:36 -08002178static unsigned int udf_count_free_table(struct super_block *sb,
2179 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002182 uint32_t elen;
2183 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002185 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 lock_kernel();
2188
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002189 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002190 epos.offset = sizeof(struct unallocSpaceEntry);
2191 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002193 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002195
Jan Kara3bf25cb2007-05-08 00:35:16 -07002196 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 unlock_kernel();
2199
2200 return accum;
2201}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002202
2203static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002206 struct udf_sb_info *sbi;
2207 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002209 sbi = UDF_SB(sb);
2210 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002211 struct logicalVolIntegrityDesc *lvid =
2212 (struct logicalVolIntegrityDesc *)
2213 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002214 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002215 accum = le32_to_cpu(
2216 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (accum == 0xFFFFFFFF)
2218 accum = 0;
2219 }
2220 }
2221
2222 if (accum)
2223 return accum;
2224
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002225 map = &sbi->s_partmaps[sbi->s_partition];
2226 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002227 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002228 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002230 if (map->s_partition_flags & UDF_PART_FLAG_FREED_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_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234 if (accum)
2235 return accum;
2236
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002237 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002238 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002239 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002241 if (map->s_partition_flags & UDF_PART_FLAG_FREED_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_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
2245
2246 return accum;
2247}