blob: 6823733c0121be622b7b0a656a82ebfc1b2ca9eb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
88static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
90 kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void udf_open_lvid(struct super_block *);
94static void udf_close_lvid(struct super_block *);
95static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070096static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080097static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020098static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800101struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
102{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700112static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells454e2392006-06-23 02:02:57 -0700116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700120 .owner = THIS_MODULE,
121 .name = "udf",
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct inode *udf_alloc_inode(struct super_block *sb)
130{
131 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!ei)
134 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700135
136 ei->i_unique = 0;
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
140 ei->i_strat4096 = 0;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return &ei->vfs_inode;
143}
144
145static void udf_destroy_inode(struct inode *inode)
146{
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
148}
149
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700150static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Lametera35afb82007-05-16 22:10:57 -0700154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int init_inodecache(void)
159{
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 0, (SLAB_RECLAIM_ACCOUNT |
163 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900164 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENOMEM;
167 return 0;
168}
169
170static void destroy_inodecache(void)
171{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700172 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800176static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800186 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char novrs;
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
194 unsigned int anchor;
195 unsigned int volume;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
199 unsigned int flags;
200 mode_t umask;
201 gid_t gid;
202 uid_t uid;
203 struct nls_table *nls_map;
204};
205
206static int __init init_udf_fs(void)
207{
208 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = init_inodecache();
211 if (err)
212 goto out1;
213 err = register_filesystem(&udf_fstype);
214 if (err)
215 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return err;
224}
225
226static void __exit exit_udf_fs(void)
227{
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
230}
231
232module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700233module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800235static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
236{
237 struct udf_sb_info *sbi = UDF_SB(sb);
238
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
240 GFP_KERNEL);
241 if (!sbi->s_partmaps) {
242 udf_error(sb, __FUNCTION__,
243 "Unable to allocate space for %d partition maps",
244 count);
245 sbi->s_partitions = 0;
246 return -ENOMEM;
247 }
248
249 sbi->s_partitions = count;
250 return 0;
251}
252
Miklos Szeredi6da80892008-02-08 04:21:50 -0800253static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
254{
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
257
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
260 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
285 seq_printf(seq, ",session=%u", sbi->s_session);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
287 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
288 /*
289 * s_anchor[2] could be zeroed out in case there is no anchor
290 * in the specified block, but then the "anchor=N" option
291 * originally given by the user wasn't effective, so it's OK
292 * if we don't show it.
293 */
294 if (sbi->s_anchor[2] != 0)
295 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
296 /*
297 * volume, partition, fileset and rootdir seem to be ignored
298 * currently
299 */
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
301 seq_puts(seq, ",utf8");
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
303 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
304
305 return 0;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * udf_parse_options
310 *
311 * PURPOSE
312 * Parse mount options.
313 *
314 * DESCRIPTION
315 * The following mount options are supported:
316 *
317 * gid= Set the default group.
318 * umask= Set the default umask.
319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
329 *
330 * The remaining are for debugging and disaster recovery:
331 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * The following expect a offset from 0.
335 *
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
341 *
342 * The following expect a offset from the partition root.
343 *
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
348 *
349 * PRE-CONDITIONS
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
352 *
353 * POST-CONDITIONS
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
356 *
357 * HISTORY
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
360 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362enum {
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 {Opt_novrs, "novrs"},
373 {Opt_nostrict, "nostrict"},
374 {Opt_bs, "bs=%u"},
375 {Opt_unhide, "unhide"},
376 {Opt_undelete, "undelete"},
377 {Opt_noadinicb, "noadinicb"},
378 {Opt_adinicb, "adinicb"},
379 {Opt_shortad, "shortad"},
380 {Opt_longad, "longad"},
381 {Opt_uforget, "uid=forget"},
382 {Opt_uignore, "uid=ignore"},
383 {Opt_gforget, "gid=forget"},
384 {Opt_gignore, "gid=ignore"},
385 {Opt_gid, "gid=%u"},
386 {Opt_uid, "uid=%u"},
387 {Opt_umask, "umask=%o"},
388 {Opt_session, "session=%u"},
389 {Opt_lastblock, "lastblock=%u"},
390 {Opt_anchor, "anchor=%u"},
391 {Opt_volume, "volume=%u"},
392 {Opt_partition, "partition=%u"},
393 {Opt_fileset, "fileset=%u"},
394 {Opt_rootdir, "rootdir=%u"},
395 {Opt_utf8, "utf8"},
396 {Opt_iocharset, "iocharset=%s"},
397 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Miklos Szeredi6da80892008-02-08 04:21:50 -0800400static int udf_parse_options(char *options, struct udf_options *uopt,
401 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 char *p;
404 int option;
405
406 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800407 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 uopt->partition = 0xFFFF;
409 uopt->session = 0xFFFFFFFF;
410 uopt->lastblock = 0;
411 uopt->anchor = 0;
412 uopt->volume = 0xFFFFFFFF;
413 uopt->rootdir = 0xFFFFFFFF;
414 uopt->fileset = 0xFFFFFFFF;
415 uopt->nls_map = NULL;
416
417 if (!options)
418 return 1;
419
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 substring_t args[MAX_OPT_ARGS];
422 int token;
423 if (!*p)
424 continue;
425
426 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 switch (token) {
428 case Opt_novrs:
429 uopt->novrs = 1;
430 case Opt_bs:
431 if (match_int(&args[0], &option))
432 return 0;
433 uopt->blocksize = option;
434 break;
435 case Opt_unhide:
436 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
437 break;
438 case Opt_undelete:
439 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
440 break;
441 case Opt_noadinicb:
442 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
443 break;
444 case Opt_adinicb:
445 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_shortad:
448 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
449 break;
450 case Opt_longad:
451 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_gid:
454 if (match_int(args, &option))
455 return 0;
456 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700457 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458 break;
459 case Opt_uid:
460 if (match_int(args, &option))
461 return 0;
462 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700463 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700464 break;
465 case Opt_umask:
466 if (match_octal(args, &option))
467 return 0;
468 uopt->umask = option;
469 break;
470 case Opt_nostrict:
471 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
472 break;
473 case Opt_session:
474 if (match_int(args, &option))
475 return 0;
476 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800477 if (!remount)
478 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479 break;
480 case Opt_lastblock:
481 if (match_int(args, &option))
482 return 0;
483 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 break;
487 case Opt_anchor:
488 if (match_int(args, &option))
489 return 0;
490 uopt->anchor = option;
491 break;
492 case Opt_volume:
493 if (match_int(args, &option))
494 return 0;
495 uopt->volume = option;
496 break;
497 case Opt_partition:
498 if (match_int(args, &option))
499 return 0;
500 uopt->partition = option;
501 break;
502 case Opt_fileset:
503 if (match_int(args, &option))
504 return 0;
505 uopt->fileset = option;
506 break;
507 case Opt_rootdir:
508 if (match_int(args, &option))
509 return 0;
510 uopt->rootdir = option;
511 break;
512 case Opt_utf8:
513 uopt->flags |= (1 << UDF_FLAG_UTF8);
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 case Opt_iocharset:
517 uopt->nls_map = load_nls(args[0].from);
518 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 case Opt_uignore:
522 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
523 break;
524 case Opt_uforget:
525 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
526 break;
527 case Opt_gignore:
528 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
529 break;
530 case Opt_gforget:
531 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
532 break;
533 default:
534 printk(KERN_ERR "udf: bad mount option \"%s\" "
535 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537 }
538 }
539 return 1;
540}
541
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800542static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!(sb->s_flags & MS_RDONLY))
547 udf_open_lvid(sb);
548 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unlock_kernel();
551}
552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800556 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800558 uopt.flags = sbi->s_flags;
559 uopt.uid = sbi->s_uid;
560 uopt.gid = sbi->s_gid;
561 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Miklos Szeredi6da80892008-02-08 04:21:50 -0800563 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EINVAL;
565
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800566 sbi->s_flags = uopt.flags;
567 sbi->s_uid = uopt.uid;
568 sbi->s_gid = uopt.gid;
569 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800571 if (sbi->s_lvid_bh) {
572 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (write_rev > UDF_MAX_WRITE_VERSION)
574 *flags |= MS_RDONLY;
575 }
576
577 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
578 return 0;
579 if (*flags & MS_RDONLY)
580 udf_close_lvid(sb);
581 else
582 udf_open_lvid(sb);
583
584 return 0;
585}
586
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200590 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int sectorsize;
592 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 int iso9660 = 0;
594 int nsr02 = 0;
595 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800596 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Block size must be a multiple of 512 */
599 if (sb->s_blocksize & 511)
600 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800601 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (sb->s_blocksize < sizeof(struct volStructDesc))
604 sectorsize = sizeof(struct volStructDesc);
605 else
606 sectorsize = sb->s_blocksize;
607
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800608 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200611 (unsigned int)(sector >> sb->s_blocksize_bits),
612 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Read a block */
616 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
617 if (!bh)
618 break;
619
620 /* Look for ISO descriptors */
621 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800622 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700624 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700625 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800627 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
628 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700630 switch (vsd->structType) {
631 case 0:
632 udf_debug("ISO9660 Boot Record found\n");
633 break;
634 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800635 udf_debug("ISO9660 Primary Volume Descriptor "
636 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637 break;
638 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800639 udf_debug("ISO9660 Supplementary Volume "
640 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 break;
642 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800643 udf_debug("ISO9660 Volume Partition Descriptor "
644 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645 break;
646 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800647 udf_debug("ISO9660 Volume Descriptor Set "
648 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 break;
650 default:
651 udf_debug("ISO9660 VRS (%u) found\n",
652 vsd->structType);
653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800655 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
656 VSD_STD_ID_LEN))
657 ; /* nothing */
658 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
659 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700660 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800662 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
663 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
666 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700668 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 if (nsr03)
672 return nsr03;
673 else if (nsr02)
674 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800675 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -1;
677 else
678 return 0;
679}
680
681/*
Jan Kara423cf6d2008-04-07 15:59:23 +0200682 * Check whether there is an anchor block in the given block
683 */
684static int udf_check_anchor_block(struct super_block *sb, sector_t block,
685 bool varconv)
686{
687 struct buffer_head *bh = NULL;
688 tag *t;
689 uint16_t ident;
690 uint32_t location;
691
Jan Kara4f7874c2008-04-07 23:16:38 +0200692 if (varconv) {
693 if (udf_fixed_to_variable(block) >=
694 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
695 return 0;
Jan Kara423cf6d2008-04-07 15:59:23 +0200696 bh = sb_bread(sb, udf_fixed_to_variable(block));
Jan Kara4f7874c2008-04-07 23:16:38 +0200697 }
Jan Kara423cf6d2008-04-07 15:59:23 +0200698 else
699 bh = sb_bread(sb, block);
700
701 if (!bh)
702 return 0;
703
704 t = (tag *)bh->b_data;
705 ident = le16_to_cpu(t->tagIdent);
706 location = le32_to_cpu(t->tagLocation);
707 brelse(bh);
708 if (ident != TAG_IDENT_AVDP)
709 return 0;
710 return location == block;
711}
712
713/* Search for an anchor volume descriptor pointer */
714static sector_t udf_scan_anchors(struct super_block *sb, bool varconv,
715 sector_t lastblock)
716{
Jan Kara5fb28aa2008-04-07 16:15:04 +0200717 sector_t last[6];
Jan Kara423cf6d2008-04-07 15:59:23 +0200718 int i;
719 struct udf_sb_info *sbi = UDF_SB(sb);
720
721 last[0] = lastblock;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200722 last[1] = last[0] - 1;
723 last[2] = last[0] + 1;
724 last[3] = last[0] - 2;
725 last[4] = last[0] - 150;
726 last[5] = last[0] - 152;
Jan Kara423cf6d2008-04-07 15:59:23 +0200727
728 /* according to spec, anchor is in either:
729 * block 256
730 * lastblock-256
731 * lastblock
732 * however, if the disc isn't closed, it could be 512 */
733
734 for (i = 0; i < ARRAY_SIZE(last); i++) {
735 if (last[i] < 0)
736 continue;
Jan Kara5fb28aa2008-04-07 16:15:04 +0200737 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
738 sb->s_blocksize_bits)
739 continue;
Jan Kara423cf6d2008-04-07 15:59:23 +0200740
741 if (udf_check_anchor_block(sb, last[i], varconv)) {
742 sbi->s_anchor[0] = last[i];
743 sbi->s_anchor[1] = last[i] - 256;
744 return last[i];
745 }
746
747 if (last[i] < 256)
748 continue;
749
750 if (udf_check_anchor_block(sb, last[i] - 256, varconv)) {
751 sbi->s_anchor[1] = last[i] - 256;
752 return last[i];
753 }
754 }
755
756 if (udf_check_anchor_block(sb, sbi->s_session + 256, varconv)) {
757 sbi->s_anchor[0] = sbi->s_session + 256;
758 return last[0];
759 }
760 if (udf_check_anchor_block(sb, sbi->s_session + 512, varconv)) {
761 sbi->s_anchor[0] = sbi->s_session + 512;
762 return last[0];
763 }
764 return 0;
765}
766
767/*
768 * Find an anchor volume descriptor. The function expects sbi->s_lastblock to
769 * be the last block on the media.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 *
Jan Kara423cf6d2008-04-07 15:59:23 +0200771 * Return 1 if not found, 0 if ok
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Jan Kara423cf6d2008-04-07 15:59:23 +0200776 sector_t lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct buffer_head *bh = NULL;
778 uint16_t ident;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int i;
Jan Kara423cf6d2008-04-07 15:59:23 +0200780 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800781
Jan Kara423cf6d2008-04-07 15:59:23 +0200782 lastblock = udf_scan_anchors(sb, 0, sbi->s_last_block);
783 if (lastblock)
784 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jan Kara423cf6d2008-04-07 15:59:23 +0200786 /* No anchor found? Try VARCONV conversion of block numbers */
787 /* Firstly, we try to not convert number of the last block */
788 lastblock = udf_scan_anchors(sb, 1,
789 udf_variable_to_fixed(sbi->s_last_block));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700790 if (lastblock) {
Jan Kara423cf6d2008-04-07 15:59:23 +0200791 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
792 goto check_anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Jan Kara423cf6d2008-04-07 15:59:23 +0200795 /* Secondly, we try with converted number of the last block */
796 lastblock = udf_scan_anchors(sb, 1, sbi->s_last_block);
797 if (lastblock)
798 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Jan Kara423cf6d2008-04-07 15:59:23 +0200800check_anchor:
801 /*
802 * Check located anchors and the anchor block supplied via
803 * mount options
804 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800805 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100806 if (!sbi->s_anchor[i])
807 continue;
808 bh = udf_read_tagged(sb, sbi->s_anchor[i],
809 sbi->s_anchor[i], &ident);
810 if (!bh)
811 sbi->s_anchor[i] = 0;
812 else {
813 brelse(bh);
Jan Kara423cf6d2008-04-07 15:59:23 +0200814 if (ident != TAG_IDENT_AVDP)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800815 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 }
818
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800819 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800822static int udf_find_fileset(struct super_block *sb,
823 kernel_lb_addr *fileset,
824 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 struct buffer_head *bh = NULL;
827 long lastblock;
828 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800829 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700832 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
834
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700835 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700837 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700838 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 1;
840 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800844 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800845 if (!bh) {
846 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 kernel_lb_addr newfileset;
848
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700849/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800852 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 (newfileset.partitionReferenceNum != 0xFFFF &&
854 fileset->logicalBlockNum == 0xFFFFFFFF &&
855 fileset->partitionReferenceNum == 0xFFFF);
856 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800857 lastblock = sbi->s_partmaps
858 [newfileset.partitionReferenceNum]
859 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 newfileset.logicalBlockNum = 0;
861
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800863 bh = udf_read_ptagged(sb, newfileset, 0,
864 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 if (!bh) {
866 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 continue;
868 }
869
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700870 switch (ident) {
871 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700872 {
873 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800874 sp = (struct spaceBitmapDesc *)
875 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700876 newfileset.logicalBlockNum += 1 +
877 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800878 sizeof(struct spaceBitmapDesc)
879 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700880 brelse(bh);
881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700883 case TAG_IDENT_FSD:
884 *fileset = newfileset;
885 break;
886 default:
887 newfileset.logicalBlockNum++;
888 brelse(bh);
889 bh = NULL;
890 break;
891 }
892 } while (newfileset.logicalBlockNum < lastblock &&
893 fileset->logicalBlockNum == 0xFFFFFFFF &&
894 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 }
897
898 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700899 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700901 fileset->logicalBlockNum,
902 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800904 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700906 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908 }
909 return 1;
910}
911
Jan Karac0eb31e2008-04-01 16:50:35 +0200912static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct primaryVolDesc *pvoldesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct ustr instr;
916 struct ustr outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200917 struct buffer_head *bh;
918 uint16_t ident;
919
920 bh = udf_read_tagged(sb, block, block, &ident);
921 if (!bh)
922 return 1;
923 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 pvoldesc = (struct primaryVolDesc *)bh->b_data;
926
Marcin Slusarz56774802008-02-10 11:25:31 +0100927 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
928 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100929#ifdef UDFFS_DEBUG
930 timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100931 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800932 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100933 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
934 ts->minute, le16_to_cpu(ts->typeAndTimezone));
935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Marcin Slusarz4b111112008-02-08 04:20:36 -0800938 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700939 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800940 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800942 udf_debug("volIdent[] = '%s'\n",
943 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Marcin Slusarz4b111112008-02-08 04:20:36 -0800946 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (udf_CS0toUTF8(&outstr, &instr))
948 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200949
950 brelse(bh);
951 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Jan Karabfb257a2008-04-08 20:37:21 +0200954static int udf_load_metadata_files(struct super_block *sb, int partition)
955{
956 struct udf_sb_info *sbi = UDF_SB(sb);
957 struct udf_part_map *map;
958 struct udf_meta_data *mdata;
959 kernel_lb_addr addr;
960 int fe_error = 0;
961
962 map = &sbi->s_partmaps[partition];
963 mdata = &map->s_type_specific.s_metadata;
964
965 /* metadata address */
966 addr.logicalBlockNum = mdata->s_meta_file_loc;
967 addr.partitionReferenceNum = map->s_partition_num;
968
969 udf_debug("Metadata file location: block = %d part = %d\n",
970 addr.logicalBlockNum, addr.partitionReferenceNum);
971
972 mdata->s_metadata_fe = udf_iget(sb, addr);
973
974 if (mdata->s_metadata_fe == NULL) {
975 udf_warning(sb, __func__, "metadata inode efe not found, "
976 "will try mirror inode.");
977 fe_error = 1;
978 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
979 ICBTAG_FLAG_AD_SHORT) {
980 udf_warning(sb, __func__, "metadata inode efe does not have "
981 "short allocation descriptors!");
982 fe_error = 1;
983 iput(mdata->s_metadata_fe);
984 mdata->s_metadata_fe = NULL;
985 }
986
987 /* mirror file entry */
988 addr.logicalBlockNum = mdata->s_mirror_file_loc;
989 addr.partitionReferenceNum = map->s_partition_num;
990
991 udf_debug("Mirror metadata file location: block = %d part = %d\n",
992 addr.logicalBlockNum, addr.partitionReferenceNum);
993
994 mdata->s_mirror_fe = udf_iget(sb, addr);
995
996 if (mdata->s_mirror_fe == NULL) {
997 if (fe_error) {
998 udf_error(sb, __func__, "mirror inode efe not found "
999 "and metadata inode is missing too, exiting...");
1000 goto error_exit;
1001 } else
1002 udf_warning(sb, __func__, "mirror inode efe not found,"
1003 " but metadata inode is OK");
1004 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
1005 ICBTAG_FLAG_AD_SHORT) {
1006 udf_warning(sb, __func__, "mirror inode efe does not have "
1007 "short allocation descriptors!");
1008 iput(mdata->s_mirror_fe);
1009 mdata->s_mirror_fe = NULL;
1010 if (fe_error)
1011 goto error_exit;
1012 }
1013
1014 /*
1015 * bitmap file entry
1016 * Note:
1017 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1018 */
1019 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1020 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1021 addr.partitionReferenceNum = map->s_partition_num;
1022
1023 udf_debug("Bitmap file location: block = %d part = %d\n",
1024 addr.logicalBlockNum, addr.partitionReferenceNum);
1025
1026 mdata->s_bitmap_fe = udf_iget(sb, addr);
1027
1028 if (mdata->s_bitmap_fe == NULL) {
1029 if (sb->s_flags & MS_RDONLY)
1030 udf_warning(sb, __func__, "bitmap inode efe "
1031 "not found but it's ok since the disc"
1032 " is mounted read-only");
1033 else {
1034 udf_error(sb, __func__, "bitmap inode efe not "
1035 "found and attempted read-write mount");
1036 goto error_exit;
1037 }
1038 }
1039 }
1040
1041 udf_debug("udf_load_metadata_files Ok\n");
1042
1043 return 0;
1044
1045error_exit:
1046 return 1;
1047}
1048
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001049static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1050 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
1052 struct fileSetDesc *fset;
1053
1054 fset = (struct fileSetDesc *)bh->b_data;
1055
1056 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1057
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001058 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001060 udf_debug("Rootdir at block=%d, partition=%d\n",
1061 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001064int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1065{
1066 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001067 return DIV_ROUND_UP(map->s_partition_len +
1068 (sizeof(struct spaceBitmapDesc) << 3),
1069 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001070}
1071
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001072static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1073{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001074 struct udf_bitmap *bitmap;
1075 int nr_groups;
1076 int size;
1077
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001078 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001079 size = sizeof(struct udf_bitmap) +
1080 (sizeof(struct buffer_head *) * nr_groups);
1081
1082 if (size <= PAGE_SIZE)
1083 bitmap = kmalloc(size, GFP_KERNEL);
1084 else
1085 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1086
1087 if (bitmap == NULL) {
1088 udf_error(sb, __FUNCTION__,
1089 "Unable to allocate space for bitmap "
1090 "and %d buffer_head pointers", nr_groups);
1091 return NULL;
1092 }
1093
1094 memset(bitmap, 0x00, size);
1095 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1096 bitmap->s_nr_groups = nr_groups;
1097 return bitmap;
1098}
1099
Jan Kara3fb38df2008-04-02 12:26:36 +02001100static int udf_fill_partdesc_info(struct super_block *sb,
1101 struct partitionDesc *p, int p_index)
1102{
1103 struct udf_part_map *map;
1104 struct udf_sb_info *sbi = UDF_SB(sb);
1105 struct partitionHeaderDesc *phd;
1106
1107 map = &sbi->s_partmaps[p_index];
1108
1109 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1110 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1111
1112 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1113 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1114 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1115 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1116 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1117 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1118 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1119 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1120
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001121 udf_debug("Partition (%d type %x) starts at physical %d, "
1122 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001123 map->s_partition_type, map->s_partition_root,
1124 map->s_partition_len);
1125
1126 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1127 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1128 return 0;
1129
1130 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1131 if (phd->unallocSpaceTable.extLength) {
1132 kernel_lb_addr loc = {
1133 .logicalBlockNum = le32_to_cpu(
1134 phd->unallocSpaceTable.extPosition),
1135 .partitionReferenceNum = p_index,
1136 };
1137
1138 map->s_uspace.s_table = udf_iget(sb, loc);
1139 if (!map->s_uspace.s_table) {
1140 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1141 p_index);
1142 return 1;
1143 }
1144 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1145 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1146 p_index, map->s_uspace.s_table->i_ino);
1147 }
1148
1149 if (phd->unallocSpaceBitmap.extLength) {
1150 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1151 if (!bitmap)
1152 return 1;
1153 map->s_uspace.s_bitmap = bitmap;
1154 bitmap->s_extLength = le32_to_cpu(
1155 phd->unallocSpaceBitmap.extLength);
1156 bitmap->s_extPosition = le32_to_cpu(
1157 phd->unallocSpaceBitmap.extPosition);
1158 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1159 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1160 bitmap->s_extPosition);
1161 }
1162
1163 if (phd->partitionIntegrityTable.extLength)
1164 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1165
1166 if (phd->freedSpaceTable.extLength) {
1167 kernel_lb_addr loc = {
1168 .logicalBlockNum = le32_to_cpu(
1169 phd->freedSpaceTable.extPosition),
1170 .partitionReferenceNum = p_index,
1171 };
1172
1173 map->s_fspace.s_table = udf_iget(sb, loc);
1174 if (!map->s_fspace.s_table) {
1175 udf_debug("cannot load freedSpaceTable (part %d)\n",
1176 p_index);
1177 return 1;
1178 }
1179
1180 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1181 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1182 p_index, map->s_fspace.s_table->i_ino);
1183 }
1184
1185 if (phd->freedSpaceBitmap.extLength) {
1186 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1187 if (!bitmap)
1188 return 1;
1189 map->s_fspace.s_bitmap = bitmap;
1190 bitmap->s_extLength = le32_to_cpu(
1191 phd->freedSpaceBitmap.extLength);
1192 bitmap->s_extPosition = le32_to_cpu(
1193 phd->freedSpaceBitmap.extPosition);
1194 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1195 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1196 bitmap->s_extPosition);
1197 }
1198 return 0;
1199}
1200
Jan Kara38b74a52008-04-02 16:01:35 +02001201static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1202{
1203 struct udf_sb_info *sbi = UDF_SB(sb);
1204 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1205 kernel_lb_addr ino;
Jan Karafa5e0812008-04-08 02:08:53 +02001206 struct buffer_head *bh = NULL;
1207 struct udf_inode_info *vati;
1208 uint32_t pos;
1209 struct virtualAllocationTable20 *vat20;
Jan Kara38b74a52008-04-02 16:01:35 +02001210
1211 /* VAT file entry is in the last recorded block */
1212 ino.partitionReferenceNum = type1_index;
1213 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1214 sbi->s_vat_inode = udf_iget(sb, ino);
1215 if (!sbi->s_vat_inode)
1216 return 1;
1217
1218 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001219 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001220 map->s_type_specific.s_virtual.s_num_entries =
1221 (sbi->s_vat_inode->i_size - 36) >> 2;
1222 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001223 vati = UDF_I(sbi->s_vat_inode);
1224 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1225 pos = udf_block_map(sbi->s_vat_inode, 0);
1226 bh = sb_bread(sb, pos);
1227 if (!bh)
1228 return 1;
1229 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1230 } else {
1231 vat20 = (struct virtualAllocationTable20 *)
1232 vati->i_ext.i_data;
1233 }
Jan Kara38b74a52008-04-02 16:01:35 +02001234
Jan Kara38b74a52008-04-02 16:01:35 +02001235 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001236 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001237 map->s_type_specific.s_virtual.s_num_entries =
1238 (sbi->s_vat_inode->i_size -
1239 map->s_type_specific.s_virtual.
1240 s_start_offset) >> 2;
1241 brelse(bh);
1242 }
1243 return 0;
1244}
1245
Jan Karac0eb31e2008-04-01 16:50:35 +02001246static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Jan Karac0eb31e2008-04-01 16:50:35 +02001248 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001249 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001250 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001251 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001252 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001253 uint16_t partitionNumber;
1254 uint16_t ident;
1255 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Jan Karac0eb31e2008-04-01 16:50:35 +02001257 bh = udf_read_tagged(sb, block, block, &ident);
1258 if (!bh)
1259 return 1;
1260 if (ident != TAG_IDENT_PD)
1261 goto out_bh;
1262
1263 p = (struct partitionDesc *)bh->b_data;
1264 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001265
Jan Karabfb257a2008-04-08 20:37:21 +02001266 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001267 for (i = 0; i < sbi->s_partitions; i++) {
1268 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001269 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001270 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001271 if (map->s_partition_num == partitionNumber &&
1272 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1273 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001275 }
1276
Jan Kara38b74a52008-04-02 16:01:35 +02001277 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001278 udf_debug("Partition (%d) not found in partition map\n",
1279 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001280 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001281 }
1282
Jan Kara3fb38df2008-04-02 12:26:36 +02001283 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001284
1285 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001286 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1287 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001288 */
1289 type1_idx = i;
1290 for (i = 0; i < sbi->s_partitions; i++) {
1291 map = &sbi->s_partmaps[i];
1292
1293 if (map->s_partition_num == partitionNumber &&
1294 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001295 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1296 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001297 break;
1298 }
1299
1300 if (i >= sbi->s_partitions)
1301 goto out_bh;
1302
1303 ret = udf_fill_partdesc_info(sb, p, i);
1304 if (ret)
1305 goto out_bh;
1306
Jan Karabfb257a2008-04-08 20:37:21 +02001307 if (map->s_partition_type == UDF_METADATA_MAP25) {
1308 ret = udf_load_metadata_files(sb, i);
1309 if (ret) {
1310 printk(KERN_ERR "UDF-fs: error loading MetaData "
1311 "partition map %d\n", i);
1312 goto out_bh;
1313 }
1314 } else {
1315 ret = udf_load_vat(sb, i, type1_idx);
1316 if (ret)
1317 goto out_bh;
1318 /*
1319 * Mark filesystem read-only if we have a partition with
1320 * virtual map since we don't handle writing to it (we
1321 * overwrite blocks instead of relocating them).
1322 */
1323 sb->s_flags |= MS_RDONLY;
1324 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1325 "because writing to pseudooverwrite partition is "
1326 "not implemented.\n");
1327 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001328out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001329 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001330 brelse(bh);
1331 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Jan Karac0eb31e2008-04-01 16:50:35 +02001334static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001335 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct logicalVolDesc *lvd;
1338 int i, j, offset;
1339 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001340 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001341 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001342 uint16_t ident;
1343 struct buffer_head *bh;
1344 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Jan Karac0eb31e2008-04-01 16:50:35 +02001346 bh = udf_read_tagged(sb, block, block, &ident);
1347 if (!bh)
1348 return 1;
1349 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 lvd = (struct logicalVolDesc *)bh->b_data;
1351
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001352 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001353 if (i != 0) {
1354 ret = i;
1355 goto out_bh;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001358 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001359 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001360 i++, offset += gpm->partitionMapLength) {
1361 struct udf_part_map *map = &sbi->s_partmaps[i];
1362 gpm = (struct genericPartitionMap *)
1363 &(lvd->partitionMaps[offset]);
1364 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001365 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001366 struct genericPartitionMap1 *gpm1 =
1367 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001368 map->s_partition_type = UDF_TYPE1_MAP15;
1369 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1370 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1371 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001372 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001373 struct udfPartitionMap2 *upm2 =
1374 (struct udfPartitionMap2 *)gpm;
1375 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1376 strlen(UDF_ID_VIRTUAL))) {
1377 u16 suf =
1378 le16_to_cpu(((__le16 *)upm2->partIdent.
1379 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001380 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001381 map->s_partition_type =
1382 UDF_VIRTUAL_MAP15;
1383 map->s_partition_func =
1384 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001385 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001386 map->s_partition_type =
1387 UDF_VIRTUAL_MAP20;
1388 map->s_partition_func =
1389 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001391 } else if (!strncmp(upm2->partIdent.ident,
1392 UDF_ID_SPARABLE,
1393 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001396 struct sparablePartitionMap *spm =
1397 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001399 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001400 map->s_type_specific.s_sparing.s_packet_len =
1401 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001402 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001403 struct buffer_head *bh2;
1404
1405 loc = le32_to_cpu(
1406 spm->locSparingTable[j]);
1407 bh2 = udf_read_tagged(sb, loc, loc,
1408 &ident);
1409 map->s_type_specific.s_sparing.
1410 s_spar_map[j] = bh2;
1411
Marcin Slusarz165923f2008-02-10 11:33:08 +01001412 if (bh2 == NULL)
1413 continue;
1414
1415 st = (struct sparingTable *)bh2->b_data;
1416 if (ident != 0 || strncmp(
1417 st->sparingIdent.ident,
1418 UDF_ID_SPARING,
1419 strlen(UDF_ID_SPARING))) {
1420 brelse(bh2);
1421 map->s_type_specific.s_sparing.
1422 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001425 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001426 } else if (!strncmp(upm2->partIdent.ident,
1427 UDF_ID_METADATA,
1428 strlen(UDF_ID_METADATA))) {
1429 struct udf_meta_data *mdata =
1430 &map->s_type_specific.s_metadata;
1431 struct metadataPartitionMap *mdm =
1432 (struct metadataPartitionMap *)
1433 &(lvd->partitionMaps[offset]);
1434 udf_debug("Parsing Logical vol part %d "
1435 "type %d id=%s\n", i, type,
1436 UDF_ID_METADATA);
1437
1438 map->s_partition_type = UDF_METADATA_MAP25;
1439 map->s_partition_func = udf_get_pblock_meta25;
1440
1441 mdata->s_meta_file_loc =
1442 le32_to_cpu(mdm->metadataFileLoc);
1443 mdata->s_mirror_file_loc =
1444 le32_to_cpu(mdm->metadataMirrorFileLoc);
1445 mdata->s_bitmap_file_loc =
1446 le32_to_cpu(mdm->metadataBitmapFileLoc);
1447 mdata->s_alloc_unit_size =
1448 le32_to_cpu(mdm->allocUnitSize);
1449 mdata->s_align_unit_size =
1450 le16_to_cpu(mdm->alignUnitSize);
1451 mdata->s_dup_md_flag =
1452 mdm->flags & 0x01;
1453
1454 udf_debug("Metadata Ident suffix=0x%x\n",
1455 (le16_to_cpu(
1456 ((__le16 *)
1457 mdm->partIdent.identSuffix)[0])));
1458 udf_debug("Metadata part num=%d\n",
1459 le16_to_cpu(mdm->partitionNum));
1460 udf_debug("Metadata part alloc unit size=%d\n",
1461 le32_to_cpu(mdm->allocUnitSize));
1462 udf_debug("Metadata file loc=%d\n",
1463 le32_to_cpu(mdm->metadataFileLoc));
1464 udf_debug("Mirror file loc=%d\n",
1465 le32_to_cpu(mdm->metadataMirrorFileLoc));
1466 udf_debug("Bitmap file loc=%d\n",
1467 le32_to_cpu(mdm->metadataBitmapFileLoc));
1468 udf_debug("Duplicate Flag: %d %d\n",
1469 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001470 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001471 udf_debug("Unknown ident: %s\n",
1472 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 continue;
1474 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001475 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1476 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001479 i, map->s_partition_num, type,
1480 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001483 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001484 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001487 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1488 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001489 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 if (lvd->integritySeqExt.extLength)
1492 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001493
Jan Karac0eb31e2008-04-01 16:50:35 +02001494out_bh:
1495 brelse(bh);
1496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
1499/*
1500 * udf_load_logicalvolint
1501 *
1502 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001503static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct buffer_head *bh = NULL;
1506 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001507 struct udf_sb_info *sbi = UDF_SB(sb);
1508 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001511 (bh = udf_read_tagged(sb, loc.extLocation,
1512 loc.extLocation, &ident)) &&
1513 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001514 sbi->s_lvid_bh = bh;
1515 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001516
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001517 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001518 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001519 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001521 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001522 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001524 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001526 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001527 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530/*
1531 * udf_process_sequence
1532 *
1533 * PURPOSE
1534 * Process a main/reserve volume descriptor sequence.
1535 *
1536 * PRE-CONDITIONS
1537 * sb Pointer to _locked_ superblock.
1538 * block First block of first extent of the sequence.
1539 * lastblock Lastblock of first extent of the sequence.
1540 *
1541 * HISTORY
1542 * July 1, 1997 - Andrew E. Mileski
1543 * Written, tested, and released.
1544 */
Jan Kara200a3592008-03-04 13:03:09 +01001545static noinline int udf_process_sequence(struct super_block *sb, long block,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001546 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 struct buffer_head *bh = NULL;
1549 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001550 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct generic_desc *gd;
1552 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001553 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 uint32_t vdsn;
1555 uint16_t ident;
1556 long next_s = 0, next_e = 0;
1557
1558 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1559
Jan Karac0eb31e2008-04-01 16:50:35 +02001560 /*
1561 * Read the main descriptor sequence and find which descriptors
1562 * are in it.
1563 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001567 if (!bh) {
1568 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1569 "sequence is corrupted or we could not read "
1570 "it.\n", (unsigned long long)block);
1571 return 1;
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1575 gd = (struct generic_desc *)bh->b_data;
1576 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001577 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001578 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001579 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1580 if (vdsn >= curr->volDescSeqNum) {
1581 curr->volDescSeqNum = vdsn;
1582 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001583 }
1584 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001585 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001586 curr = &vds[VDS_POS_VOL_DESC_PTR];
1587 if (vdsn >= curr->volDescSeqNum) {
1588 curr->volDescSeqNum = vdsn;
1589 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001591 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001592 next_s = le32_to_cpu(
1593 vdp->nextVolDescSeqExt.extLocation);
1594 next_e = le32_to_cpu(
1595 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001596 next_e = next_e >> sb->s_blocksize_bits;
1597 next_e += next_s;
1598 }
1599 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001600 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001601 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1602 if (vdsn >= curr->volDescSeqNum) {
1603 curr->volDescSeqNum = vdsn;
1604 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001605 }
1606 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001607 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 curr = &vds[VDS_POS_PARTITION_DESC];
1609 if (!curr->block)
1610 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001611 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001612 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001613 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1614 if (vdsn >= curr->volDescSeqNum) {
1615 curr->volDescSeqNum = vdsn;
1616 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001617 }
1618 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001619 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001620 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1621 if (vdsn >= curr->volDescSeqNum) {
1622 curr->volDescSeqNum = vdsn;
1623 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001624 }
1625 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001626 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001627 vds[VDS_POS_TERMINATING_DESC].block = block;
1628 if (next_e) {
1629 block = next_s;
1630 lastblock = next_e;
1631 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001632 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001633 done = 1;
1634 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001636 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001638 /*
1639 * Now read interesting descriptors again and process them
1640 * in a suitable order
1641 */
1642 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1643 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1644 return 1;
1645 }
1646 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1647 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Jan Karac0eb31e2008-04-01 16:50:35 +02001649 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1650 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1651 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001652
Jan Karac0eb31e2008-04-01 16:50:35 +02001653 if (vds[VDS_POS_PARTITION_DESC].block) {
1654 /*
1655 * We rescan the whole descriptor sequence to find
1656 * partition descriptor blocks and process them.
1657 */
1658 for (block = vds[VDS_POS_PARTITION_DESC].block;
1659 block < vds[VDS_POS_TERMINATING_DESC].block;
1660 block++)
1661 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001662 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
1665 return 0;
1666}
1667
1668/*
1669 * udf_check_valid()
1670 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001671static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001674 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 udf_debug("Validity check skipped because of novrs option\n");
1678 return 0;
1679 }
1680 /* Check that it is NSR02 compliant */
1681 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001682 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001683 if (block == -1)
1684 udf_debug("Failed to read byte 32768. Assuming open "
1685 "disc. Skipping validity check\n");
1686 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001687 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001688 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689}
1690
Jan Kara38b74a52008-04-02 16:01:35 +02001691static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct anchorVolDescPtr *anchor;
1694 uint16_t ident;
1695 struct buffer_head *bh;
1696 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001697 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001698 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 if (!sb)
1701 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001702 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001704 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001705 if (!sbi->s_anchor[i])
1706 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001707
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001708 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1709 &ident);
1710 if (!bh)
1711 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001713 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001714
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001715 /* Locate the main sequence */
1716 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1717 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1718 main_e = main_e >> sb->s_blocksize_bits;
1719 main_e += main_s;
1720
1721 /* Locate the reserve sequence */
1722 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001723 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001724 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001725 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001726 reserve_e = reserve_e >> sb->s_blocksize_bits;
1727 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001729 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001731 /* Process the main & reserve sequences */
1732 /* responsible for finding the PartitionDesc(s) */
1733 if (!(udf_process_sequence(sb, main_s, main_e,
1734 fileset) &&
1735 udf_process_sequence(sb, reserve_s, reserve_e,
1736 fileset)))
1737 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001740 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 udf_debug("No Anchor block found\n");
1742 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001743 }
1744 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 return 0;
1747}
1748
1749static void udf_open_lvid(struct super_block *sb)
1750{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001751 struct udf_sb_info *sbi = UDF_SB(sb);
1752 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001753 struct logicalVolIntegrityDesc *lvid;
1754 struct logicalVolIntegrityDescImpUse *lvidiu;
1755 if (!bh)
1756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Marcin Slusarz165923f2008-02-10 11:33:08 +01001758 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1759 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Marcin Slusarz165923f2008-02-10 11:33:08 +01001761 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1762 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1763 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1764 CURRENT_TIME);
1765 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Marcin Slusarz165923f2008-02-10 11:33:08 +01001767 lvid->descTag.descCRC = cpu_to_le16(
1768 udf_crc((char *)lvid + sizeof(tag),
1769 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1770
1771 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1772 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
1775static void udf_close_lvid(struct super_block *sb)
1776{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001777 struct udf_sb_info *sbi = UDF_SB(sb);
1778 struct buffer_head *bh = sbi->s_lvid_bh;
1779 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001780 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001781
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001782 if (!bh)
1783 return;
1784
1785 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1786
Marcin Slusarz165923f2008-02-10 11:33:08 +01001787 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1788 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Marcin Slusarz165923f2008-02-10 11:33:08 +01001790 lvidiu = udf_sb_lvidiu(sbi);
1791 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1792 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1793 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1794 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1795 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1796 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1797 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1798 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1799 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1800 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Marcin Slusarz165923f2008-02-10 11:33:08 +01001802 lvid->descTag.descCRC = cpu_to_le16(
1803 udf_crc((char *)lvid + sizeof(tag),
1804 le16_to_cpu(lvid->descTag.descCRCLength),
1805 0));
1806
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}