blob: f3ac4abfc9467a093ac54ebdef4283e9eaec166c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/byteorder.h>
59
60#include <linux/udf_fs.h>
61#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);
87static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
89 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
91static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
93 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095static void udf_load_fileset(struct super_block *, struct buffer_head *,
96 kernel_lb_addr *);
Jan Karabcec4472007-08-30 23:56:22 -070097static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void udf_open_lvid(struct super_block *);
99static void udf_close_lvid(struct super_block *);
100static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -0700101static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800102static int udf_show_options(struct seq_file *, struct vfsmount *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800104struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
105{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 struct logicalVolIntegrityDesc *lvid =
107 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800109 __u32 offset = number_of_partitions * 2 *
110 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800111 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700115static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 int flags, const char *dev_name, void *data,
117 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David Howells454e2392006-06-23 02:02:57 -0700119 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700123 .owner = THIS_MODULE,
124 .name = "udf",
125 .get_sb = udf_get_sb,
126 .kill_sb = kill_block_super,
127 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700130static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132static struct inode *udf_alloc_inode(struct super_block *sb)
133{
134 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800135 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!ei)
137 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700138
139 ei->i_unique = 0;
140 ei->i_lenExtents = 0;
141 ei->i_next_alloc_block = 0;
142 ei->i_next_alloc_goal = 0;
143 ei->i_strat4096 = 0;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return &ei->vfs_inode;
146}
147
148static void udf_destroy_inode(struct inode *inode)
149{
150 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
151}
152
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700153static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Christoph Lametera35afb82007-05-16 22:10:57 -0700157 ei->i_ext.i_data = NULL;
158 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static int init_inodecache(void)
162{
163 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
164 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 0, (SLAB_RECLAIM_ACCOUNT |
166 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900167 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700168 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -ENOMEM;
170 return 0;
171}
172
173static void destroy_inodecache(void)
174{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700175 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800179static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700180 .alloc_inode = udf_alloc_inode,
181 .destroy_inode = udf_destroy_inode,
182 .write_inode = udf_write_inode,
183 .delete_inode = udf_delete_inode,
184 .clear_inode = udf_clear_inode,
185 .put_super = udf_put_super,
186 .write_super = udf_write_super,
187 .statfs = udf_statfs,
188 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800189 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned char novrs;
194 unsigned int blocksize;
195 unsigned int session;
196 unsigned int lastblock;
197 unsigned int anchor;
198 unsigned int volume;
199 unsigned short partition;
200 unsigned int fileset;
201 unsigned int rootdir;
202 unsigned int flags;
203 mode_t umask;
204 gid_t gid;
205 uid_t uid;
206 struct nls_table *nls_map;
207};
208
209static int __init init_udf_fs(void)
210{
211 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = init_inodecache();
214 if (err)
215 goto out1;
216 err = register_filesystem(&udf_fstype);
217 if (err)
218 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700224
225out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return err;
227}
228
229static void __exit exit_udf_fs(void)
230{
231 unregister_filesystem(&udf_fstype);
232 destroy_inodecache();
233}
234
235module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800238static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
239{
240 struct udf_sb_info *sbi = UDF_SB(sb);
241
242 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
243 GFP_KERNEL);
244 if (!sbi->s_partmaps) {
245 udf_error(sb, __FUNCTION__,
246 "Unable to allocate space for %d partition maps",
247 count);
248 sbi->s_partitions = 0;
249 return -ENOMEM;
250 }
251
252 sbi->s_partitions = count;
253 return 0;
254}
255
Miklos Szeredi6da80892008-02-08 04:21:50 -0800256static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
257{
258 struct super_block *sb = mnt->mnt_sb;
259 struct udf_sb_info *sbi = UDF_SB(sb);
260
261 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
262 seq_puts(seq, ",nostrict");
263 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
264 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
266 seq_puts(seq, ",unhide");
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
268 seq_puts(seq, ",undelete");
269 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
270 seq_puts(seq, ",noadinicb");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
272 seq_puts(seq, ",shortad");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
274 seq_puts(seq, ",uid=forget");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
276 seq_puts(seq, ",uid=ignore");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
278 seq_puts(seq, ",gid=forget");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
280 seq_puts(seq, ",gid=ignore");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
282 seq_printf(seq, ",uid=%u", sbi->s_uid);
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
284 seq_printf(seq, ",gid=%u", sbi->s_gid);
285 if (sbi->s_umask != 0)
286 seq_printf(seq, ",umask=%o", sbi->s_umask);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
288 seq_printf(seq, ",session=%u", sbi->s_session);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
290 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
291 /*
292 * s_anchor[2] could be zeroed out in case there is no anchor
293 * in the specified block, but then the "anchor=N" option
294 * originally given by the user wasn't effective, so it's OK
295 * if we don't show it.
296 */
297 if (sbi->s_anchor[2] != 0)
298 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
299 /*
300 * volume, partition, fileset and rootdir seem to be ignored
301 * currently
302 */
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
304 seq_puts(seq, ",utf8");
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
306 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
307
308 return 0;
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * udf_parse_options
313 *
314 * PURPOSE
315 * Parse mount options.
316 *
317 * DESCRIPTION
318 * The following mount options are supported:
319 *
320 * gid= Set the default group.
321 * umask= Set the default umask.
322 * uid= Set the default user.
323 * bs= Set the block size.
324 * unhide Show otherwise hidden files.
325 * undelete Show deleted files in lists.
326 * adinicb Embed data in the inode (default)
327 * noadinicb Don't embed data in the inode
328 * shortad Use short ad's
329 * longad Use long ad's (default)
330 * nostrict Unset strict conformance
331 * iocharset= Set the NLS character set
332 *
333 * The remaining are for debugging and disaster recovery:
334 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700335 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
337 * The following expect a offset from 0.
338 *
339 * session= Set the CDROM session (default= last session)
340 * anchor= Override standard anchor location. (default= 256)
341 * volume= Override the VolumeDesc location. (unused)
342 * partition= Override the PartitionDesc location. (unused)
343 * lastblock= Set the last block of the filesystem/
344 *
345 * The following expect a offset from the partition root.
346 *
347 * fileset= Override the fileset block location. (unused)
348 * rootdir= Override the root directory location. (unused)
349 * WARNING: overriding the rootdir to a non-directory may
350 * yield highly unpredictable results.
351 *
352 * PRE-CONDITIONS
353 * options Pointer to mount options string.
354 * uopts Pointer to mount options variable.
355 *
356 * POST-CONDITIONS
357 * <return> 1 Mount options parsed okay.
358 * <return> 0 Error parsing mount options.
359 *
360 * HISTORY
361 * July 1, 1997 - Andrew E. Mileski
362 * Written, tested, and released.
363 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365enum {
366 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
367 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
368 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
369 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
370 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700375 {Opt_novrs, "novrs"},
376 {Opt_nostrict, "nostrict"},
377 {Opt_bs, "bs=%u"},
378 {Opt_unhide, "unhide"},
379 {Opt_undelete, "undelete"},
380 {Opt_noadinicb, "noadinicb"},
381 {Opt_adinicb, "adinicb"},
382 {Opt_shortad, "shortad"},
383 {Opt_longad, "longad"},
384 {Opt_uforget, "uid=forget"},
385 {Opt_uignore, "uid=ignore"},
386 {Opt_gforget, "gid=forget"},
387 {Opt_gignore, "gid=ignore"},
388 {Opt_gid, "gid=%u"},
389 {Opt_uid, "uid=%u"},
390 {Opt_umask, "umask=%o"},
391 {Opt_session, "session=%u"},
392 {Opt_lastblock, "lastblock=%u"},
393 {Opt_anchor, "anchor=%u"},
394 {Opt_volume, "volume=%u"},
395 {Opt_partition, "partition=%u"},
396 {Opt_fileset, "fileset=%u"},
397 {Opt_rootdir, "rootdir=%u"},
398 {Opt_utf8, "utf8"},
399 {Opt_iocharset, "iocharset=%s"},
400 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Miklos Szeredi6da80892008-02-08 04:21:50 -0800403static int udf_parse_options(char *options, struct udf_options *uopt,
404 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 char *p;
407 int option;
408
409 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800410 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 uopt->partition = 0xFFFF;
412 uopt->session = 0xFFFFFFFF;
413 uopt->lastblock = 0;
414 uopt->anchor = 0;
415 uopt->volume = 0xFFFFFFFF;
416 uopt->rootdir = 0xFFFFFFFF;
417 uopt->fileset = 0xFFFFFFFF;
418 uopt->nls_map = NULL;
419
420 if (!options)
421 return 1;
422
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700423 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 substring_t args[MAX_OPT_ARGS];
425 int token;
426 if (!*p)
427 continue;
428
429 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 switch (token) {
431 case Opt_novrs:
432 uopt->novrs = 1;
433 case Opt_bs:
434 if (match_int(&args[0], &option))
435 return 0;
436 uopt->blocksize = option;
437 break;
438 case Opt_unhide:
439 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
440 break;
441 case Opt_undelete:
442 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
443 break;
444 case Opt_noadinicb:
445 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_adinicb:
448 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
449 break;
450 case Opt_shortad:
451 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_longad:
454 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
455 break;
456 case Opt_gid:
457 if (match_int(args, &option))
458 return 0;
459 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700460 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700461 break;
462 case Opt_uid:
463 if (match_int(args, &option))
464 return 0;
465 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700466 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 break;
468 case Opt_umask:
469 if (match_octal(args, &option))
470 return 0;
471 uopt->umask = option;
472 break;
473 case Opt_nostrict:
474 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
475 break;
476 case Opt_session:
477 if (match_int(args, &option))
478 return 0;
479 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800480 if (!remount)
481 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700482 break;
483 case Opt_lastblock:
484 if (match_int(args, &option))
485 return 0;
486 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800487 if (!remount)
488 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489 break;
490 case Opt_anchor:
491 if (match_int(args, &option))
492 return 0;
493 uopt->anchor = option;
494 break;
495 case Opt_volume:
496 if (match_int(args, &option))
497 return 0;
498 uopt->volume = option;
499 break;
500 case Opt_partition:
501 if (match_int(args, &option))
502 return 0;
503 uopt->partition = option;
504 break;
505 case Opt_fileset:
506 if (match_int(args, &option))
507 return 0;
508 uopt->fileset = option;
509 break;
510 case Opt_rootdir:
511 if (match_int(args, &option))
512 return 0;
513 uopt->rootdir = option;
514 break;
515 case Opt_utf8:
516 uopt->flags |= (1 << UDF_FLAG_UTF8);
517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700519 case Opt_iocharset:
520 uopt->nls_map = load_nls(args[0].from);
521 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700524 case Opt_uignore:
525 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
526 break;
527 case Opt_uforget:
528 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
529 break;
530 case Opt_gignore:
531 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
532 break;
533 case Opt_gforget:
534 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
535 break;
536 default:
537 printk(KERN_ERR "udf: bad mount option \"%s\" "
538 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540 }
541 }
542 return 1;
543}
544
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800545static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!(sb->s_flags & MS_RDONLY))
550 udf_open_lvid(sb);
551 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unlock_kernel();
554}
555
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800559 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800561 uopt.flags = sbi->s_flags;
562 uopt.uid = sbi->s_uid;
563 uopt.gid = sbi->s_gid;
564 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Miklos Szeredi6da80892008-02-08 04:21:50 -0800566 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -EINVAL;
568
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800569 sbi->s_flags = uopt.flags;
570 sbi->s_uid = uopt.uid;
571 sbi->s_gid = uopt.gid;
572 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800574 if (sbi->s_lvid_bh) {
575 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (write_rev > UDF_MAX_WRITE_VERSION)
577 *flags |= MS_RDONLY;
578 }
579
580 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
581 return 0;
582 if (*flags & MS_RDONLY)
583 udf_close_lvid(sb);
584 else
585 udf_open_lvid(sb);
586
587 return 0;
588}
589
590/*
591 * udf_set_blocksize
592 *
593 * PURPOSE
594 * Set the block size to be used in all transfers.
595 *
596 * DESCRIPTION
597 * To allow room for a DMA transfer, it is best to guess big when unsure.
598 * This routine picks 2048 bytes as the blocksize when guessing. This
599 * should be adequate until devices with larger block sizes become common.
600 *
601 * Note that the Linux kernel can currently only deal with blocksizes of
602 * 512, 1024, 2048, 4096, and 8192 bytes.
603 *
604 * PRE-CONDITIONS
605 * sb Pointer to _locked_ superblock.
606 *
607 * POST-CONDITIONS
608 * sb->s_blocksize Blocksize.
609 * sb->s_blocksize_bits log2 of blocksize.
610 * <return> 0 Blocksize is valid.
611 * <return> 1 Blocksize is invalid.
612 *
613 * HISTORY
614 * July 1, 1997 - Andrew E. Mileski
615 * Written, tested, and released.
616 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617static int udf_set_blocksize(struct super_block *sb, int bsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 if (!sb_min_blocksize(sb, bsize)) {
620 udf_debug("Bad block size (%d)\n", bsize);
621 printk(KERN_ERR "udf: bad block size (%d)\n", bsize);
622 return 0;
623 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return sb->s_blocksize;
626}
627
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700628static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct volStructDesc *vsd = NULL;
631 int sector = 32768;
632 int sectorsize;
633 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700634 int iso9660 = 0;
635 int nsr02 = 0;
636 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800637 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 /* Block size must be a multiple of 512 */
640 if (sb->s_blocksize & 511)
641 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800642 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (sb->s_blocksize < sizeof(struct volStructDesc))
645 sectorsize = sizeof(struct volStructDesc);
646 else
647 sectorsize = sb->s_blocksize;
648
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800649 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700652 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700654 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* Read a block */
656 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
657 if (!bh)
658 break;
659
660 /* Look for ISO descriptors */
661 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800662 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700664 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700665 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800667 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
668 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700670 switch (vsd->structType) {
671 case 0:
672 udf_debug("ISO9660 Boot Record found\n");
673 break;
674 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800675 udf_debug("ISO9660 Primary Volume Descriptor "
676 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700677 break;
678 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800679 udf_debug("ISO9660 Supplementary Volume "
680 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700681 break;
682 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800683 udf_debug("ISO9660 Volume Partition Descriptor "
684 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700685 break;
686 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800687 udf_debug("ISO9660 Volume Descriptor Set "
688 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700689 break;
690 default:
691 udf_debug("ISO9660 VRS (%u) found\n",
692 vsd->structType);
693 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800695 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
696 VSD_STD_ID_LEN))
697 ; /* nothing */
698 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
699 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700700 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800702 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
703 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800705 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
706 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700708 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 if (nsr03)
712 return nsr03;
713 else if (nsr02)
714 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800715 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return -1;
717 else
718 return 0;
719}
720
721/*
722 * udf_find_anchor
723 *
724 * PURPOSE
725 * Find an anchor volume descriptor.
726 *
727 * PRE-CONDITIONS
728 * sb Pointer to _locked_ superblock.
729 * lastblock Last block on media.
730 *
731 * POST-CONDITIONS
732 * <return> 1 if not found, 0 if ok
733 *
734 * HISTORY
735 * July 1, 1997 - Andrew E. Mileski
736 * Written, tested, and released.
737 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800740 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct buffer_head *bh = NULL;
742 uint16_t ident;
743 uint32_t location;
744 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800745 struct udf_sb_info *sbi;
746
747 sbi = UDF_SB(sb);
748 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700750 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700752 int last[] = { lastblock, lastblock - 2,
753 lastblock - 150, lastblock - 152,
754 varlastblock, varlastblock - 2,
755 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 lastblock = 0;
758
759 /* Search for an anchor volume descriptor pointer */
760
761 /* according to spec, anchor is in either:
762 * block 256
763 * lastblock-256
764 * lastblock
765 * however, if the disc isn't closed, it could be 512 */
766
Tobias Klausere8c96f82006-03-24 03:15:34 -0800767 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800768 ident = location = 0;
769 if (last[i] >= 0) {
770 bh = sb_bread(sb, last[i]);
771 if (bh) {
772 tag *t = (tag *)bh->b_data;
773 ident = le16_to_cpu(t->tagIdent);
774 location = le32_to_cpu(t->tagLocation);
775 brelse(bh);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800778
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700779 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800780 if (location == last[i] - sbi->s_session) {
781 lastblock = last[i] - sbi->s_session;
782 sbi->s_anchor[0] = lastblock;
783 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800784 } else if (location ==
785 udf_variable_to_fixed(last[i]) -
786 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800788 lastblock =
789 udf_variable_to_fixed(last[i]) -
790 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800791 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800792 sbi->s_anchor[1] = lastblock - 256 -
793 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700794 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800795 udf_debug("Anchor found at block %d, "
796 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700797 last[i], location);
798 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800799 } else if (ident == TAG_IDENT_FE ||
800 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800802 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700803 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800804 ident = location = 0;
805 if (last[i] >= 256) {
806 bh = sb_bread(sb, last[i] - 256);
807 if (bh) {
808 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800809 ident = le16_to_cpu(
810 t->tagIdent);
811 location = le32_to_cpu(
812 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800813 brelse(bh);
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800818 location == last[i] - 256 -
819 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800821 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700822 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800823 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800824 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800825 bh = sb_bread(sb,
826 last[i] - 312 -
827 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800828 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800829 tag *t = (tag *)
830 bh->b_data;
831 ident = le16_to_cpu(
832 t->tagIdent);
833 location = le32_to_cpu(
834 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800835 brelse(bh);
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700840 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800841 UDF_SET_FLAG(sb,
842 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700843 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800844 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 }
847 }
848 }
849 }
850
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700851 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800852 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800853 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800854 if (bh) {
855 tag *t = (tag *)bh->b_data;
856 ident = le16_to_cpu(t->tagIdent);
857 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700858 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (ident == TAG_IDENT_AVDP && location == 256)
861 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
862 }
863 }
864
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800865 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
866 if (sbi->s_anchor[i]) {
867 bh = udf_read_tagged(sb, sbi->s_anchor[i],
868 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800869 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800870 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800871 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700872 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700873 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800874 (i || (ident != TAG_IDENT_FE &&
875 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800876 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
879 }
880
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800881 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800884static int udf_find_fileset(struct super_block *sb,
885 kernel_lb_addr *fileset,
886 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 struct buffer_head *bh = NULL;
889 long lastblock;
890 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800891 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700894 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
896
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700897 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700899 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700900 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 1;
902 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800906 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800907 if (!bh) {
908 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 kernel_lb_addr newfileset;
910
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700911/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700913
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800914 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700915 (newfileset.partitionReferenceNum != 0xFFFF &&
916 fileset->logicalBlockNum == 0xFFFFFFFF &&
917 fileset->partitionReferenceNum == 0xFFFF);
918 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800919 lastblock = sbi->s_partmaps
920 [newfileset.partitionReferenceNum]
921 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 newfileset.logicalBlockNum = 0;
923
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700924 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800925 bh = udf_read_ptagged(sb, newfileset, 0,
926 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700927 if (!bh) {
928 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 continue;
930 }
931
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700932 switch (ident) {
933 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700934 {
935 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800936 sp = (struct spaceBitmapDesc *)
937 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700938 newfileset.logicalBlockNum += 1 +
939 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800940 sizeof(struct spaceBitmapDesc)
941 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700942 brelse(bh);
943 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700945 case TAG_IDENT_FSD:
946 *fileset = newfileset;
947 break;
948 default:
949 newfileset.logicalBlockNum++;
950 brelse(bh);
951 bh = NULL;
952 break;
953 }
954 } while (newfileset.logicalBlockNum < lastblock &&
955 fileset->logicalBlockNum == 0xFFFFFFFF &&
956 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 }
959
960 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700963 fileset->logicalBlockNum,
964 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800966 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700968 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return 0;
970 }
971 return 1;
972}
973
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700974static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct primaryVolDesc *pvoldesc;
977 time_t recording;
978 long recording_usec;
979 struct ustr instr;
980 struct ustr outstr;
981
982 pvoldesc = (struct primaryVolDesc *)bh->b_data;
983
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 if (udf_stamp_to_time(&recording, &recording_usec,
985 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 kernel_timestamp ts;
987 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800988 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
989 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700990 recording, recording_usec,
991 ts.year, ts.month, ts.day, ts.hour,
992 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800993 UDF_SB(sb)->s_record_time.tv_sec = recording;
994 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Marcin Slusarz4b111112008-02-08 04:20:36 -0800997 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700998 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800999 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001001 udf_debug("volIdent[] = '%s'\n",
1002 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Marcin Slusarz4b111112008-02-08 04:20:36 -08001005 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (udf_CS0toUTF8(&outstr, &instr))
1007 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001010static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
1011 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 struct fileSetDesc *fset;
1014
1015 fset = (struct fileSetDesc *)bh->b_data;
1016
1017 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1018
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001019 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001021 udf_debug("Rootdir at block=%d, partition=%d\n",
1022 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001025int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1026{
1027 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
1028 return (map->s_partition_len +
1029 (sizeof(struct spaceBitmapDesc) << 3) +
1030 (sb->s_blocksize * 8) - 1) /
1031 (sb->s_blocksize * 8);
1032}
1033
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001034static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1035{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001036 struct udf_bitmap *bitmap;
1037 int nr_groups;
1038 int size;
1039
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001040 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001041 size = sizeof(struct udf_bitmap) +
1042 (sizeof(struct buffer_head *) * nr_groups);
1043
1044 if (size <= PAGE_SIZE)
1045 bitmap = kmalloc(size, GFP_KERNEL);
1046 else
1047 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1048
1049 if (bitmap == NULL) {
1050 udf_error(sb, __FUNCTION__,
1051 "Unable to allocate space for bitmap "
1052 "and %d buffer_head pointers", nr_groups);
1053 return NULL;
1054 }
1055
1056 memset(bitmap, 0x00, size);
1057 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1058 bitmap->s_nr_groups = nr_groups;
1059 return bitmap;
1060}
1061
Jan Karabcec4472007-08-30 23:56:22 -07001062static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct partitionDesc *p;
1065 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001066 struct udf_part_map *map;
1067 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001070 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001072 for (i = 0; i < sbi->s_partitions; i++) {
1073 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001074 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001075 map->s_partition_num,
1076 le16_to_cpu(p->partitionNumber));
1077 if (map->s_partition_num ==
1078 le16_to_cpu(p->partitionNumber)) {
1079 map->s_partition_len =
1080 le32_to_cpu(p->partitionLength); /* blocks */
1081 map->s_partition_root =
1082 le32_to_cpu(p->partitionStartingLocation);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001083 if (p->accessType ==
1084 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001085 map->s_partition_flags |=
1086 UDF_PART_FLAG_READ_ONLY;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001087 if (p->accessType ==
1088 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001089 map->s_partition_flags |=
1090 UDF_PART_FLAG_WRITE_ONCE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001091 if (p->accessType ==
1092 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001093 map->s_partition_flags |=
1094 UDF_PART_FLAG_REWRITABLE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001095 if (p->accessType ==
1096 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001097 map->s_partition_flags |=
1098 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001100 if (!strcmp(p->partitionContents.ident,
1101 PD_PARTITION_CONTENTS_NSR02) ||
1102 !strcmp(p->partitionContents.ident,
1103 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct partitionHeaderDesc *phd;
1105
Marcin Slusarz4b111112008-02-08 04:20:36 -08001106 phd = (struct partitionHeaderDesc *)
1107 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001108 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001109 kernel_lb_addr loc = {
1110 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1111 .partitionReferenceNum = i,
1112 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001114 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001115 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001116 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001117 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001118 return 1;
1119 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001120 map->s_partition_flags |=
1121 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001122 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001123 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001125 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001126 struct udf_bitmap *bitmap =
1127 udf_sb_alloc_bitmap(sb, i);
1128 map->s_uspace.s_bitmap = bitmap;
1129 if (bitmap != NULL) {
1130 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001131 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001132 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001133 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001134 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001135 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001136 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138 }
1139 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001140 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001141 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001142 kernel_lb_addr loc = {
1143 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1144 .partitionReferenceNum = i,
1145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001147 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001148 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001149 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001150 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001151 return 1;
1152 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001153 map->s_partition_flags |=
1154 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001155 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001156 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001158 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001159 struct udf_bitmap *bitmap =
1160 udf_sb_alloc_bitmap(sb, i);
1161 map->s_fspace.s_bitmap = bitmap;
1162 if (bitmap != NULL) {
1163 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001164 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001165 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001166 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001167 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001168 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001169 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171 }
1172 }
1173 break;
1174 }
1175 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001176 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001177 udf_debug("Partition (%d) not found in partition map\n",
1178 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001179 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001180 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1181 "block length %d\n",
1182 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001183 map->s_partition_type,
1184 map->s_partition_root,
1185 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001189static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1190 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 struct logicalVolDesc *lvd;
1193 int i, j, offset;
1194 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001195 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001196 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 lvd = (struct logicalVolDesc *)bh->b_data;
1199
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001200 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1201 if (i != 0)
1202 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001204 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001205 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001206 i++, offset += gpm->partitionMapLength) {
1207 struct udf_part_map *map = &sbi->s_partmaps[i];
1208 gpm = (struct genericPartitionMap *)
1209 &(lvd->partitionMaps[offset]);
1210 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001211 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001212 struct genericPartitionMap1 *gpm1 =
1213 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001214 map->s_partition_type = UDF_TYPE1_MAP15;
1215 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1216 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1217 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001218 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001219 struct udfPartitionMap2 *upm2 =
1220 (struct udfPartitionMap2 *)gpm;
1221 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1222 strlen(UDF_ID_VIRTUAL))) {
1223 u16 suf =
1224 le16_to_cpu(((__le16 *)upm2->partIdent.
1225 identSuffix)[0]);
1226 if (suf == 0x0150) {
1227 map->s_partition_type =
1228 UDF_VIRTUAL_MAP15;
1229 map->s_partition_func =
1230 udf_get_pblock_virt15;
1231 } else if (suf == 0x0200) {
1232 map->s_partition_type =
1233 UDF_VIRTUAL_MAP20;
1234 map->s_partition_func =
1235 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001237 } else if (!strncmp(upm2->partIdent.ident,
1238 UDF_ID_SPARABLE,
1239 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 uint32_t loc;
1241 uint16_t ident;
1242 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001243 struct sparablePartitionMap *spm =
1244 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001246 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001247 map->s_type_specific.s_sparing.s_packet_len =
1248 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001249 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001250 struct buffer_head *bh2;
1251
1252 loc = le32_to_cpu(
1253 spm->locSparingTable[j]);
1254 bh2 = udf_read_tagged(sb, loc, loc,
1255 &ident);
1256 map->s_type_specific.s_sparing.
1257 s_spar_map[j] = bh2;
1258
1259 if (bh2 != NULL) {
1260 st = (struct sparingTable *)
1261 bh2->b_data;
1262 if (ident != 0 || strncmp(
1263 st->sparingIdent.ident,
1264 UDF_ID_SPARING,
1265 strlen(UDF_ID_SPARING))) {
1266 brelse(bh2);
1267 map->s_type_specific.
1268 s_sparing.
1269 s_spar_map[j] =
1270 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 }
1273 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001274 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001276 udf_debug("Unknown ident: %s\n",
1277 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 continue;
1279 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001280 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1281 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 i, map->s_partition_num, type,
1285 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001288 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001289 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001292 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1293 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001294 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 if (lvd->integritySeqExt.extLength)
1297 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0;
1300}
1301
1302/*
1303 * udf_load_logicalvolint
1304 *
1305 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001306static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct buffer_head *bh = NULL;
1309 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001310 struct udf_sb_info *sbi = UDF_SB(sb);
1311 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 (bh = udf_read_tagged(sb, loc.extLocation,
1315 loc.extLocation, &ident)) &&
1316 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001317 sbi->s_lvid_bh = bh;
1318 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001319
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001320 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001321 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001322 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001323
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001324 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001325 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001327 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001329 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001330 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331}
1332
1333/*
1334 * udf_process_sequence
1335 *
1336 * PURPOSE
1337 * Process a main/reserve volume descriptor sequence.
1338 *
1339 * PRE-CONDITIONS
1340 * sb Pointer to _locked_ superblock.
1341 * block First block of first extent of the sequence.
1342 * lastblock Lastblock of first extent of the sequence.
1343 *
1344 * HISTORY
1345 * July 1, 1997 - Andrew E. Mileski
1346 * Written, tested, and released.
1347 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001348static int udf_process_sequence(struct super_block *sb, long block,
1349 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 struct buffer_head *bh = NULL;
1352 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001353 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 struct generic_desc *gd;
1355 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001356 int done = 0;
1357 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 uint32_t vdsn;
1359 uint16_t ident;
1360 long next_s = 0, next_e = 0;
1361
1362 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1363
1364 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001365 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001368 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
1370
1371 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1372 gd = (struct generic_desc *)bh->b_data;
1373 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001374 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001375 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001376 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1377 if (vdsn >= curr->volDescSeqNum) {
1378 curr->volDescSeqNum = vdsn;
1379 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001380 }
1381 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001382 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001383 curr = &vds[VDS_POS_VOL_DESC_PTR];
1384 if (vdsn >= curr->volDescSeqNum) {
1385 curr->volDescSeqNum = vdsn;
1386 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001388 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001389 next_s = le32_to_cpu(
1390 vdp->nextVolDescSeqExt.extLocation);
1391 next_e = le32_to_cpu(
1392 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001393 next_e = next_e >> sb->s_blocksize_bits;
1394 next_e += next_s;
1395 }
1396 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001397 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001398 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1399 if (vdsn >= curr->volDescSeqNum) {
1400 curr->volDescSeqNum = vdsn;
1401 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001402 }
1403 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001405 curr = &vds[VDS_POS_PARTITION_DESC];
1406 if (!curr->block)
1407 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001408 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001409 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001410 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1411 if (vdsn >= curr->volDescSeqNum) {
1412 curr->volDescSeqNum = vdsn;
1413 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001414 }
1415 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001416 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001417 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1418 if (vdsn >= curr->volDescSeqNum) {
1419 curr->volDescSeqNum = vdsn;
1420 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001421 }
1422 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001423 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001424 vds[VDS_POS_TERMINATING_DESC].block = block;
1425 if (next_e) {
1426 block = next_s;
1427 lastblock = next_e;
1428 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001429 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001430 done = 1;
1431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001433 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001435 for (i = 0; i < VDS_POS_LENGTH; i++) {
1436 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001437 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1438 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001440 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001442 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001443 if (udf_load_logicalvol(sb, bh, fileset)) {
1444 brelse(bh);
1445 return 1;
1446 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001447 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001449 if (udf_load_partdesc(sb, bh)) {
1450 brelse(bh);
1451 return 1;
1452 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001453 for (j = vds[i].block + 1;
1454 j < vds[VDS_POS_TERMINATING_DESC].block;
1455 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 bh2 = udf_read_tagged(sb, j, j, &ident);
1457 gd = (struct generic_desc *)bh2->b_data;
1458 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001459 if (udf_load_partdesc(sb,
1460 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001461 brelse(bh);
1462 brelse(bh2);
1463 return 1;
1464 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001465 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001468 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 }
1471
1472 return 0;
1473}
1474
1475/*
1476 * udf_check_valid()
1477 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001478static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 long block;
1481
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001482 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 udf_debug("Validity check skipped because of novrs option\n");
1484 return 0;
1485 }
1486 /* Check that it is NSR02 compliant */
1487 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001488 else {
1489 block = udf_vrs(sb, silent);
1490 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001491 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001492 udf_debug("Failed to read byte 32768. Assuming open "
1493 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001494 if (!sbi->s_last_block)
1495 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001496 return 0;
1497 } else
1498 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001502static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct anchorVolDescPtr *anchor;
1505 uint16_t ident;
1506 struct buffer_head *bh;
1507 long main_s, main_e, reserve_s, reserve_e;
1508 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001509 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (!sb)
1512 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001513 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001515 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001516 if (!sbi->s_anchor[i])
1517 continue;
1518 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1519 &ident);
1520 if (!bh)
1521 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001523 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001524
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001525 /* Locate the main sequence */
1526 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1527 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1528 main_e = main_e >> sb->s_blocksize_bits;
1529 main_e += main_s;
1530
1531 /* Locate the reserve sequence */
1532 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001533 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001534 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001535 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001536 reserve_e = reserve_e >> sb->s_blocksize_bits;
1537 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001539 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001541 /* Process the main & reserve sequences */
1542 /* responsible for finding the PartitionDesc(s) */
1543 if (!(udf_process_sequence(sb, main_s, main_e,
1544 fileset) &&
1545 udf_process_sequence(sb, reserve_s, reserve_e,
1546 fileset)))
1547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001550 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 udf_debug("No Anchor block found\n");
1552 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001553 }
1554 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001556 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001557 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001558 struct udf_part_map *map = &sbi->s_partmaps[i];
1559 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 case UDF_VIRTUAL_MAP15:
1561 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001562 if (!sbi->s_last_block) {
1563 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001564 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001566
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001567 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001568 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001569 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001570 return 1;
1571 }
1572
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001573 for (j = 0; j < sbi->s_partitions; j++) {
1574 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001575 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001576 map->s_volumeseqnum ==
1577 map2->s_volumeseqnum &&
1578 map->s_partition_num ==
1579 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001580 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001581 ino.logicalBlockNum =
1582 sbi->s_last_block -
1583 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001584 break;
1585 }
1586 }
1587
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001588 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001589 return 1;
1590
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001591 sbi->s_vat_inode = udf_iget(sb, ino);
1592 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001593 return 1;
1594
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001595 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1596 map->s_type_specific.s_virtual.s_start_offset =
1597 udf_ext0_offset(sbi->s_vat_inode);
1598 map->s_type_specific.s_virtual.s_num_entries =
1599 (sbi->s_vat_inode->i_size - 36) >> 2;
1600 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001601 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001602 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001603
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001604 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001605 bh = sb_bread(sb, pos);
1606 if (!bh)
1607 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 vat20 = (struct virtualAllocationTable20 *)
1609 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001610 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001611 map->s_type_specific.s_virtual.s_start_offset =
1612 le16_to_cpu(vat20->lengthHeader) +
1613 udf_ext0_offset(sbi->s_vat_inode);
1614 map->s_type_specific.s_virtual.s_num_entries =
1615 (sbi->s_vat_inode->i_size -
1616 map->s_type_specific.s_virtual.
1617 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001618 brelse(bh);
1619 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001620 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1621 map->s_partition_len =
1622 sbi->s_partmaps[ino.partitionReferenceNum].
1623 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 }
1626 return 0;
1627}
1628
1629static void udf_open_lvid(struct super_block *sb)
1630{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001631 struct udf_sb_info *sbi = UDF_SB(sb);
1632 struct buffer_head *bh = sbi->s_lvid_bh;
1633 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001635 struct logicalVolIntegrityDesc *lvid =
1636 (struct logicalVolIntegrityDesc *)bh->b_data;
1637 struct logicalVolIntegrityDescImpUse *lvidiu =
1638 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001640 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1641 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001643 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1644 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Marcin Slusarz4b111112008-02-08 04:20:36 -08001646 lvid->descTag.descCRC = cpu_to_le16(
1647 udf_crc((char *)lvid + sizeof(tag),
1648 le16_to_cpu(lvid->descTag.descCRCLength),
1649 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001651 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001652 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654}
1655
1656static void udf_close_lvid(struct super_block *sb)
1657{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001658 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001659 struct udf_sb_info *sbi = UDF_SB(sb);
1660 struct buffer_head *bh = sbi->s_lvid_bh;
1661 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001662
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001663 if (!bh)
1664 return;
1665
1666 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1667
1668 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001669 struct logicalVolIntegrityDescImpUse *lvidiu =
1670 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001671 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1672 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001674 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1675 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001676 lvidiu->maxUDFWriteRev =
1677 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001678 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1679 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1680 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1681 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1682 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Marcin Slusarz4b111112008-02-08 04:20:36 -08001684 lvid->descTag.descCRC = cpu_to_le16(
1685 udf_crc((char *)lvid + sizeof(tag),
1686 le16_to_cpu(lvid->descTag.descCRCLength),
1687 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001689 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001690 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692}
1693
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001694static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1695{
1696 int i;
1697 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001698 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1699 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001700
1701 for (i = 0; i < nr_groups; i++)
1702 if (bitmap->s_block_bitmap[i])
1703 brelse(bitmap->s_block_bitmap[i]);
1704
1705 if (size <= PAGE_SIZE)
1706 kfree(bitmap);
1707 else
1708 vfree(bitmap);
1709}
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711/*
1712 * udf_read_super
1713 *
1714 * PURPOSE
1715 * Complete the specified super block.
1716 *
1717 * PRE-CONDITIONS
1718 * sb Pointer to superblock to complete - never NULL.
1719 * sb->s_dev Device to read suberblock from.
1720 * options Pointer to mount options.
1721 * silent Silent flag.
1722 *
1723 * HISTORY
1724 * July 1, 1997 - Andrew E. Mileski
1725 * Written, tested, and released.
1726 */
1727static int udf_fill_super(struct super_block *sb, void *options, int silent)
1728{
1729 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001730 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct udf_options uopt;
1732 kernel_lb_addr rootdir, fileset;
1733 struct udf_sb_info *sbi;
1734
1735 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1736 uopt.uid = -1;
1737 uopt.gid = -1;
1738 uopt.umask = 0;
1739
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001740 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (!sbi)
1742 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001746 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
Miklos Szeredi6da80892008-02-08 04:21:50 -08001748 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto error_out;
1750
1751 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001752 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001754 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto error_out;
1756 }
1757#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 uopt.nls_map = load_nls_default();
1760 if (!uopt.nls_map)
1761 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1762 else
1763 udf_debug("Using default NLS map\n");
1764 }
1765#endif
1766 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1767 uopt.flags |= (1 << UDF_FLAG_UTF8);
1768
1769 fileset.logicalBlockNum = 0xFFFFFFFF;
1770 fileset.partitionReferenceNum = 0xFFFF;
1771
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001772 sbi->s_flags = uopt.flags;
1773 sbi->s_uid = uopt.uid;
1774 sbi->s_gid = uopt.gid;
1775 sbi->s_umask = uopt.umask;
1776 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 /* Set the block size for all transfers */
1779 if (!udf_set_blocksize(sb, uopt.blocksize))
1780 goto error_out;
1781
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001782 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001783 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001785 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001787 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001789 sbi->s_last_block = uopt.lastblock;
1790 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1791 sbi->s_anchor[2] = uopt.anchor;
1792 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001794 if (udf_check_valid(sb, uopt.novrs, silent)) {
1795 /* read volume recognition sequences */
1796 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001797 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
1800 udf_find_anchor(sb);
1801
1802 /* Fill in the rest of the superblock */
1803 sb->s_op = &udf_sb_ops;
1804 sb->dq_op = NULL;
1805 sb->s_dirt = 0;
1806 sb->s_magic = UDF_SUPER_MAGIC;
1807 sb->s_time_gran = 1000;
1808
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001809 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001810 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto error_out;
1812 }
1813
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001814 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001816 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001817 struct logicalVolIntegrityDescImpUse *lvidiu =
1818 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001819 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1820 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001821 /* uint16_t maxUDFWriteRev =
1822 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001824 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001825 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1826 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001827 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001828 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001830 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001833 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1836 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1837 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1838 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1839 }
1840
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001841 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001842 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto error_out;
1844 }
1845
Marcin Slusarz4b111112008-02-08 04:20:36 -08001846 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1847 UDF_PART_FLAG_READ_ONLY) {
1848 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1849 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001850 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001851 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001852
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001854 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto error_out;
1856 }
1857
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001858 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001860 udf_time_to_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001861 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001862 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001863 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1864 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866 if (!(sb->s_flags & MS_RDONLY))
1867 udf_open_lvid(sb);
1868
1869 /* Assign the root inode */
1870 /* assign inodes by physical block number */
1871 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001872 inode = udf_iget(sb, rootdir);
1873 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001874 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1875 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001876 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto error_out;
1878 }
1879
1880 /* Allocate a dentry for the root inode */
1881 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001882 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001883 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 iput(inode);
1885 goto error_out;
1886 }
Jan Kara31170b62007-05-08 00:35:21 -07001887 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001890error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001891 if (sbi->s_vat_inode)
1892 iput(sbi->s_vat_inode);
1893 if (sbi->s_partitions) {
1894 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1895 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1896 iput(map->s_uspace.s_table);
1897 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1898 iput(map->s_fspace.s_table);
1899 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001900 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001901 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001902 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001903 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001904 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001905 brelse(map->s_type_specific.s_sparing.
1906 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908#ifdef CONFIG_UDF_NLS
1909 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001910 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911#endif
1912 if (!(sb->s_flags & MS_RDONLY))
1913 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001914 brelse(sbi->s_lvid_bh);
1915
1916 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 kfree(sbi);
1918 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return -EINVAL;
1921}
1922
1923void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001924 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 va_list args;
1927
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001928 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* mark sb error */
1930 sb->s_dirt = 1;
1931 }
1932 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001933 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001935 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001936 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001940 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 va_list args;
1943
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001944 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001945 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 va_end(args);
1947 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001948 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951/*
1952 * udf_put_super
1953 *
1954 * PURPOSE
1955 * Prepare for destruction of the superblock.
1956 *
1957 * DESCRIPTION
1958 * Called before the filesystem is unmounted.
1959 *
1960 * HISTORY
1961 * July 1, 1997 - Andrew E. Mileski
1962 * Written, tested, and released.
1963 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001964static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001967 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001969 sbi = UDF_SB(sb);
1970 if (sbi->s_vat_inode)
1971 iput(sbi->s_vat_inode);
1972 if (sbi->s_partitions) {
1973 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1974 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1975 iput(map->s_uspace.s_table);
1976 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1977 iput(map->s_fspace.s_table);
1978 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001979 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001980 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001981 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001982 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001983 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001984 brelse(map->s_type_specific.s_sparing.
1985 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987#ifdef CONFIG_UDF_NLS
1988 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001989 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#endif
1991 if (!(sb->s_flags & MS_RDONLY))
1992 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001993 brelse(sbi->s_lvid_bh);
1994 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 kfree(sb->s_fs_info);
1996 sb->s_fs_info = NULL;
1997}
1998
1999/*
2000 * udf_stat_fs
2001 *
2002 * PURPOSE
2003 * Return info about the filesystem.
2004 *
2005 * DESCRIPTION
2006 * Called by sys_statfs()
2007 *
2008 * HISTORY
2009 * July 1, 1997 - Andrew E. Mileski
2010 * Written, tested, and released.
2011 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002012static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
David Howells726c3342006-06-23 02:02:58 -07002014 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002015 struct udf_sb_info *sbi = UDF_SB(sb);
2016 struct logicalVolIntegrityDescImpUse *lvidiu;
2017
2018 if (sbi->s_lvid_bh != NULL)
2019 lvidiu = udf_sb_lvidiu(sbi);
2020 else
2021 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 buf->f_type = UDF_SUPER_MAGIC;
2024 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002025 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 buf->f_bfree = udf_count_free(sb);
2027 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002028 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2029 le32_to_cpu(lvidiu->numDirs)) : 0)
2030 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 buf->f_ffree = buf->f_bfree;
2032 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002033 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 return 0;
2036}
2037
2038static unsigned char udf_bitmap_lookup[16] = {
2039 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
2040};
2041
Marcin Slusarz4b111112008-02-08 04:20:36 -08002042static unsigned int udf_count_free_bitmap(struct super_block *sb,
2043 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct buffer_head *bh = NULL;
2046 unsigned int accum = 0;
2047 int index;
2048 int block = 0, newblock;
2049 kernel_lb_addr loc;
2050 uint32_t bytes;
2051 uint8_t value;
2052 uint8_t *ptr;
2053 uint16_t ident;
2054 struct spaceBitmapDesc *bm;
2055
2056 lock_kernel();
2057
2058 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002059 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 bh = udf_read_ptagged(sb, loc, 0, &ident);
2061
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002062 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 printk(KERN_ERR "udf: udf_count_free failed\n");
2064 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002065 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002066 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 printk(KERN_ERR "udf: udf_count_free failed\n");
2068 goto out;
2069 }
2070
2071 bm = (struct spaceBitmapDesc *)bh->b_data;
2072 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002073 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2074 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002076 while (bytes > 0) {
2077 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002079 accum += udf_bitmap_lookup[value & 0x0f];
2080 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 index++;
2082 bytes--;
2083 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002084 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002085 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 newblock = udf_get_lb_pblock(sb, loc, ++block);
2087 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002088 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 udf_debug("read failed\n");
2090 goto out;
2091 }
2092 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002093 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002096 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002098out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 unlock_kernel();
2100
2101 return accum;
2102}
2103
Marcin Slusarz4b111112008-02-08 04:20:36 -08002104static unsigned int udf_count_free_table(struct super_block *sb,
2105 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002108 uint32_t elen;
2109 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002111 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 lock_kernel();
2114
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002115 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002116 epos.offset = sizeof(struct unallocSpaceEntry);
2117 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002119 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002121
Jan Kara3bf25cb2007-05-08 00:35:16 -07002122 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 unlock_kernel();
2125
2126 return accum;
2127}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002128
2129static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002132 struct udf_sb_info *sbi;
2133 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002135 sbi = UDF_SB(sb);
2136 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002137 struct logicalVolIntegrityDesc *lvid =
2138 (struct logicalVolIntegrityDesc *)
2139 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002140 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002141 accum = le32_to_cpu(
2142 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (accum == 0xFFFFFFFF)
2144 accum = 0;
2145 }
2146 }
2147
2148 if (accum)
2149 return accum;
2150
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002151 map = &sbi->s_partmaps[sbi->s_partition];
2152 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002153 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002154 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002156 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002157 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002158 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160 if (accum)
2161 return accum;
2162
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002163 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002164 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002165 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002167 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002168 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002169 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
2172 return accum;
2173}