blob: 2666e5bc69ff96a8d72e8dd95607132dc6df620f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "udf_sb.h"
61#include "udf_i.h"
62
63#include <linux/init.h>
64#include <asm/uaccess.h>
65
66#define VDS_POS_PRIMARY_VOL_DESC 0
67#define VDS_POS_UNALLOC_SPACE_DESC 1
68#define VDS_POS_LOGICAL_VOL_DESC 2
69#define VDS_POS_PARTITION_DESC 3
70#define VDS_POS_IMP_USE_VOL_DESC 4
71#define VDS_POS_VOL_DESC_PTR 5
72#define VDS_POS_TERMINATING_DESC 6
73#define VDS_POS_LENGTH 7
74
Miklos Szeredi6da80892008-02-08 04:21:50 -080075#define UDF_DEFAULT_BLOCKSIZE 2048
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static char error_buf[1024];
78
79/* These are the "meat" - everything else is stuffing */
80static int udf_fill_super(struct super_block *, void *, int);
81static void udf_put_super(struct super_block *);
82static void udf_write_super(struct super_block *);
83static int udf_remount_fs(struct super_block *, int *, char *);
84static int udf_check_valid(struct super_block *, int, int);
85static int udf_vrs(struct super_block *sb, int silent);
86static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070087static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
88 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
90static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
92 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070094static void udf_load_fileset(struct super_block *, struct buffer_head *,
95 kernel_lb_addr *);
Jan Karabcec4472007-08-30 23:56:22 -070096static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void udf_open_lvid(struct super_block *);
98static void udf_close_lvid(struct super_block *);
99static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -0700100static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800101static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +0200102static void udf_error(struct super_block *sb, const char *function,
103 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800105struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
106{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 struct logicalVolIntegrityDesc *lvid =
108 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800110 __u32 offset = number_of_partitions * 2 *
111 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800112 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
113}
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700116static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700117 int flags, const char *dev_name, void *data,
118 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
David Howells454e2392006-06-23 02:02:57 -0700120 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700124 .owner = THIS_MODULE,
125 .name = "udf",
126 .get_sb = udf_get_sb,
127 .kill_sb = kill_block_super,
128 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129};
130
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700131static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct inode *udf_alloc_inode(struct super_block *sb)
134{
135 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800136 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (!ei)
138 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700139
140 ei->i_unique = 0;
141 ei->i_lenExtents = 0;
142 ei->i_next_alloc_block = 0;
143 ei->i_next_alloc_goal = 0;
144 ei->i_strat4096 = 0;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return &ei->vfs_inode;
147}
148
149static void udf_destroy_inode(struct inode *inode)
150{
151 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
152}
153
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700154static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Christoph Lametera35afb82007-05-16 22:10:57 -0700158 ei->i_ext.i_data = NULL;
159 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static int init_inodecache(void)
163{
164 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
165 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700166 0, (SLAB_RECLAIM_ACCOUNT |
167 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900168 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700169 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -ENOMEM;
171 return 0;
172}
173
174static void destroy_inodecache(void)
175{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700176 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800180static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700181 .alloc_inode = udf_alloc_inode,
182 .destroy_inode = udf_destroy_inode,
183 .write_inode = udf_write_inode,
184 .delete_inode = udf_delete_inode,
185 .clear_inode = udf_clear_inode,
186 .put_super = udf_put_super,
187 .write_super = udf_write_super,
188 .statfs = udf_statfs,
189 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800190 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191};
192
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned char novrs;
195 unsigned int blocksize;
196 unsigned int session;
197 unsigned int lastblock;
198 unsigned int anchor;
199 unsigned int volume;
200 unsigned short partition;
201 unsigned int fileset;
202 unsigned int rootdir;
203 unsigned int flags;
204 mode_t umask;
205 gid_t gid;
206 uid_t uid;
207 struct nls_table *nls_map;
208};
209
210static int __init init_udf_fs(void)
211{
212 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = init_inodecache();
215 if (err)
216 goto out1;
217 err = register_filesystem(&udf_fstype);
218 if (err)
219 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222
223out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700225
226out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return err;
228}
229
230static void __exit exit_udf_fs(void)
231{
232 unregister_filesystem(&udf_fstype);
233 destroy_inodecache();
234}
235
236module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800239static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
240{
241 struct udf_sb_info *sbi = UDF_SB(sb);
242
243 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
244 GFP_KERNEL);
245 if (!sbi->s_partmaps) {
246 udf_error(sb, __FUNCTION__,
247 "Unable to allocate space for %d partition maps",
248 count);
249 sbi->s_partitions = 0;
250 return -ENOMEM;
251 }
252
253 sbi->s_partitions = count;
254 return 0;
255}
256
Miklos Szeredi6da80892008-02-08 04:21:50 -0800257static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
258{
259 struct super_block *sb = mnt->mnt_sb;
260 struct udf_sb_info *sbi = UDF_SB(sb);
261
262 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
263 seq_puts(seq, ",nostrict");
264 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
265 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
266 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
267 seq_puts(seq, ",unhide");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
269 seq_puts(seq, ",undelete");
270 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
271 seq_puts(seq, ",noadinicb");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
273 seq_puts(seq, ",shortad");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
275 seq_puts(seq, ",uid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
277 seq_puts(seq, ",uid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
279 seq_puts(seq, ",gid=forget");
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
281 seq_puts(seq, ",gid=ignore");
282 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
283 seq_printf(seq, ",uid=%u", sbi->s_uid);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
285 seq_printf(seq, ",gid=%u", sbi->s_gid);
286 if (sbi->s_umask != 0)
287 seq_printf(seq, ",umask=%o", sbi->s_umask);
288 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
289 seq_printf(seq, ",session=%u", sbi->s_session);
290 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
291 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
292 /*
293 * s_anchor[2] could be zeroed out in case there is no anchor
294 * in the specified block, but then the "anchor=N" option
295 * originally given by the user wasn't effective, so it's OK
296 * if we don't show it.
297 */
298 if (sbi->s_anchor[2] != 0)
299 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
300 /*
301 * volume, partition, fileset and rootdir seem to be ignored
302 * currently
303 */
304 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
305 seq_puts(seq, ",utf8");
306 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
307 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
308
309 return 0;
310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/*
313 * udf_parse_options
314 *
315 * PURPOSE
316 * Parse mount options.
317 *
318 * DESCRIPTION
319 * The following mount options are supported:
320 *
321 * gid= Set the default group.
322 * umask= Set the default umask.
323 * uid= Set the default user.
324 * bs= Set the block size.
325 * unhide Show otherwise hidden files.
326 * undelete Show deleted files in lists.
327 * adinicb Embed data in the inode (default)
328 * noadinicb Don't embed data in the inode
329 * shortad Use short ad's
330 * longad Use long ad's (default)
331 * nostrict Unset strict conformance
332 * iocharset= Set the NLS character set
333 *
334 * The remaining are for debugging and disaster recovery:
335 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700336 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *
338 * The following expect a offset from 0.
339 *
340 * session= Set the CDROM session (default= last session)
341 * anchor= Override standard anchor location. (default= 256)
342 * volume= Override the VolumeDesc location. (unused)
343 * partition= Override the PartitionDesc location. (unused)
344 * lastblock= Set the last block of the filesystem/
345 *
346 * The following expect a offset from the partition root.
347 *
348 * fileset= Override the fileset block location. (unused)
349 * rootdir= Override the root directory location. (unused)
350 * WARNING: overriding the rootdir to a non-directory may
351 * yield highly unpredictable results.
352 *
353 * PRE-CONDITIONS
354 * options Pointer to mount options string.
355 * uopts Pointer to mount options variable.
356 *
357 * POST-CONDITIONS
358 * <return> 1 Mount options parsed okay.
359 * <return> 0 Error parsing mount options.
360 *
361 * HISTORY
362 * July 1, 1997 - Andrew E. Mileski
363 * Written, tested, and released.
364 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366enum {
367 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
368 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
369 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
370 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
371 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800372 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
375static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376 {Opt_novrs, "novrs"},
377 {Opt_nostrict, "nostrict"},
378 {Opt_bs, "bs=%u"},
379 {Opt_unhide, "unhide"},
380 {Opt_undelete, "undelete"},
381 {Opt_noadinicb, "noadinicb"},
382 {Opt_adinicb, "adinicb"},
383 {Opt_shortad, "shortad"},
384 {Opt_longad, "longad"},
385 {Opt_uforget, "uid=forget"},
386 {Opt_uignore, "uid=ignore"},
387 {Opt_gforget, "gid=forget"},
388 {Opt_gignore, "gid=ignore"},
389 {Opt_gid, "gid=%u"},
390 {Opt_uid, "uid=%u"},
391 {Opt_umask, "umask=%o"},
392 {Opt_session, "session=%u"},
393 {Opt_lastblock, "lastblock=%u"},
394 {Opt_anchor, "anchor=%u"},
395 {Opt_volume, "volume=%u"},
396 {Opt_partition, "partition=%u"},
397 {Opt_fileset, "fileset=%u"},
398 {Opt_rootdir, "rootdir=%u"},
399 {Opt_utf8, "utf8"},
400 {Opt_iocharset, "iocharset=%s"},
401 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Miklos Szeredi6da80892008-02-08 04:21:50 -0800404static int udf_parse_options(char *options, struct udf_options *uopt,
405 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 char *p;
408 int option;
409
410 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800411 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 uopt->partition = 0xFFFF;
413 uopt->session = 0xFFFFFFFF;
414 uopt->lastblock = 0;
415 uopt->anchor = 0;
416 uopt->volume = 0xFFFFFFFF;
417 uopt->rootdir = 0xFFFFFFFF;
418 uopt->fileset = 0xFFFFFFFF;
419 uopt->nls_map = NULL;
420
421 if (!options)
422 return 1;
423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700424 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 substring_t args[MAX_OPT_ARGS];
426 int token;
427 if (!*p)
428 continue;
429
430 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700431 switch (token) {
432 case Opt_novrs:
433 uopt->novrs = 1;
434 case Opt_bs:
435 if (match_int(&args[0], &option))
436 return 0;
437 uopt->blocksize = option;
438 break;
439 case Opt_unhide:
440 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
441 break;
442 case Opt_undelete:
443 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
444 break;
445 case Opt_noadinicb:
446 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
447 break;
448 case Opt_adinicb:
449 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
450 break;
451 case Opt_shortad:
452 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
453 break;
454 case Opt_longad:
455 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
456 break;
457 case Opt_gid:
458 if (match_int(args, &option))
459 return 0;
460 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700461 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700462 break;
463 case Opt_uid:
464 if (match_int(args, &option))
465 return 0;
466 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700467 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700468 break;
469 case Opt_umask:
470 if (match_octal(args, &option))
471 return 0;
472 uopt->umask = option;
473 break;
474 case Opt_nostrict:
475 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
476 break;
477 case Opt_session:
478 if (match_int(args, &option))
479 return 0;
480 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800481 if (!remount)
482 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700483 break;
484 case Opt_lastblock:
485 if (match_int(args, &option))
486 return 0;
487 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800488 if (!remount)
489 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700490 break;
491 case Opt_anchor:
492 if (match_int(args, &option))
493 return 0;
494 uopt->anchor = option;
495 break;
496 case Opt_volume:
497 if (match_int(args, &option))
498 return 0;
499 uopt->volume = option;
500 break;
501 case Opt_partition:
502 if (match_int(args, &option))
503 return 0;
504 uopt->partition = option;
505 break;
506 case Opt_fileset:
507 if (match_int(args, &option))
508 return 0;
509 uopt->fileset = option;
510 break;
511 case Opt_rootdir:
512 if (match_int(args, &option))
513 return 0;
514 uopt->rootdir = option;
515 break;
516 case Opt_utf8:
517 uopt->flags |= (1 << UDF_FLAG_UTF8);
518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700520 case Opt_iocharset:
521 uopt->nls_map = load_nls(args[0].from);
522 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700525 case Opt_uignore:
526 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
527 break;
528 case Opt_uforget:
529 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
530 break;
531 case Opt_gignore:
532 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
533 break;
534 case Opt_gforget:
535 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
536 break;
537 default:
538 printk(KERN_ERR "udf: bad mount option \"%s\" "
539 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
541 }
542 }
543 return 1;
544}
545
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800546static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!(sb->s_flags & MS_RDONLY))
551 udf_open_lvid(sb);
552 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 unlock_kernel();
555}
556
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700557static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800560 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800562 uopt.flags = sbi->s_flags;
563 uopt.uid = sbi->s_uid;
564 uopt.gid = sbi->s_gid;
565 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Miklos Szeredi6da80892008-02-08 04:21:50 -0800567 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return -EINVAL;
569
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800570 sbi->s_flags = uopt.flags;
571 sbi->s_uid = uopt.uid;
572 sbi->s_gid = uopt.gid;
573 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800575 if (sbi->s_lvid_bh) {
576 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (write_rev > UDF_MAX_WRITE_VERSION)
578 *flags |= MS_RDONLY;
579 }
580
581 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
582 return 0;
583 if (*flags & MS_RDONLY)
584 udf_close_lvid(sb);
585 else
586 udf_open_lvid(sb);
587
588 return 0;
589}
590
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700591static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct volStructDesc *vsd = NULL;
594 int sector = 32768;
595 int sectorsize;
596 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700597 int iso9660 = 0;
598 int nsr02 = 0;
599 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800600 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Block size must be a multiple of 512 */
603 if (sb->s_blocksize & 511)
604 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800605 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 if (sb->s_blocksize < sizeof(struct volStructDesc))
608 sectorsize = sizeof(struct volStructDesc);
609 else
610 sectorsize = sb->s_blocksize;
611
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800612 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700617 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Read a block */
619 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
620 if (!bh)
621 break;
622
623 /* Look for ISO descriptors */
624 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800625 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700627 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700628 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800630 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
631 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700633 switch (vsd->structType) {
634 case 0:
635 udf_debug("ISO9660 Boot Record found\n");
636 break;
637 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800638 udf_debug("ISO9660 Primary Volume Descriptor "
639 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 break;
641 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800642 udf_debug("ISO9660 Supplementary Volume "
643 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 break;
645 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800646 udf_debug("ISO9660 Volume Partition Descriptor "
647 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 break;
649 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800650 udf_debug("ISO9660 Volume Descriptor Set "
651 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700652 break;
653 default:
654 udf_debug("ISO9660 VRS (%u) found\n",
655 vsd->structType);
656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800658 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
659 VSD_STD_ID_LEN))
660 ; /* nothing */
661 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
662 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700663 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
666 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800668 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
669 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700671 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
674 if (nsr03)
675 return nsr03;
676 else if (nsr02)
677 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800678 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return -1;
680 else
681 return 0;
682}
683
684/*
685 * udf_find_anchor
686 *
687 * PURPOSE
688 * Find an anchor volume descriptor.
689 *
690 * PRE-CONDITIONS
691 * sb Pointer to _locked_ superblock.
692 * lastblock Last block on media.
693 *
694 * POST-CONDITIONS
695 * <return> 1 if not found, 0 if ok
696 *
697 * HISTORY
698 * July 1, 1997 - Andrew E. Mileski
699 * Written, tested, and released.
700 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700701static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800703 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct buffer_head *bh = NULL;
705 uint16_t ident;
706 uint32_t location;
707 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800708 struct udf_sb_info *sbi;
709
710 sbi = UDF_SB(sb);
711 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700713 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700715 int last[] = { lastblock, lastblock - 2,
716 lastblock - 150, lastblock - 152,
717 varlastblock, varlastblock - 2,
718 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 lastblock = 0;
721
722 /* Search for an anchor volume descriptor pointer */
723
724 /* according to spec, anchor is in either:
725 * block 256
726 * lastblock-256
727 * lastblock
728 * however, if the disc isn't closed, it could be 512 */
729
Tobias Klausere8c96f82006-03-24 03:15:34 -0800730 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800731 ident = location = 0;
732 if (last[i] >= 0) {
733 bh = sb_bread(sb, last[i]);
734 if (bh) {
735 tag *t = (tag *)bh->b_data;
736 ident = le16_to_cpu(t->tagIdent);
737 location = le32_to_cpu(t->tagLocation);
738 brelse(bh);
739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800741
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800743 if (location == last[i] - sbi->s_session) {
744 lastblock = last[i] - sbi->s_session;
745 sbi->s_anchor[0] = lastblock;
746 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800747 } else if (location ==
748 udf_variable_to_fixed(last[i]) -
749 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800751 lastblock =
752 udf_variable_to_fixed(last[i]) -
753 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800754 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800755 sbi->s_anchor[1] = lastblock - 256 -
756 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700757 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800758 udf_debug("Anchor found at block %d, "
759 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700760 last[i], location);
761 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800762 } else if (ident == TAG_IDENT_FE ||
763 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800765 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700766 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800767 ident = location = 0;
768 if (last[i] >= 256) {
769 bh = sb_bread(sb, last[i] - 256);
770 if (bh) {
771 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800772 ident = le16_to_cpu(
773 t->tagIdent);
774 location = le32_to_cpu(
775 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800776 brelse(bh);
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800781 location == last[i] - 256 -
782 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800784 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700785 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800786 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800787 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800788 bh = sb_bread(sb,
789 last[i] - 312 -
790 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800791 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800792 tag *t = (tag *)
793 bh->b_data;
794 ident = le16_to_cpu(
795 t->tagIdent);
796 location = le32_to_cpu(
797 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800798 brelse(bh);
799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700803 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800804 UDF_SET_FLAG(sb,
805 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700806 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800807 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809 }
810 }
811 }
812 }
813
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700814 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800815 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800816 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800817 if (bh) {
818 tag *t = (tag *)bh->b_data;
819 ident = le16_to_cpu(t->tagIdent);
820 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700821 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (ident == TAG_IDENT_AVDP && location == 256)
824 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
825 }
826 }
827
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800828 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
829 if (sbi->s_anchor[i]) {
830 bh = udf_read_tagged(sb, sbi->s_anchor[i],
831 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800832 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800833 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800834 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700835 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700836 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800837 (i || (ident != TAG_IDENT_FE &&
838 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800839 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 }
842 }
843
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800844 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800847static int udf_find_fileset(struct super_block *sb,
848 kernel_lb_addr *fileset,
849 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct buffer_head *bh = NULL;
852 long lastblock;
853 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800854 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700857 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
859
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700860 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700862 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700863 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 1;
865 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800869 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800870 if (!bh) {
871 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 kernel_lb_addr newfileset;
873
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700874/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700876
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800877 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700878 (newfileset.partitionReferenceNum != 0xFFFF &&
879 fileset->logicalBlockNum == 0xFFFFFFFF &&
880 fileset->partitionReferenceNum == 0xFFFF);
881 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800882 lastblock = sbi->s_partmaps
883 [newfileset.partitionReferenceNum]
884 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 newfileset.logicalBlockNum = 0;
886
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800888 bh = udf_read_ptagged(sb, newfileset, 0,
889 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700890 if (!bh) {
891 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 continue;
893 }
894
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700895 switch (ident) {
896 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700897 {
898 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800899 sp = (struct spaceBitmapDesc *)
900 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700901 newfileset.logicalBlockNum += 1 +
902 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800903 sizeof(struct spaceBitmapDesc)
904 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700905 brelse(bh);
906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700908 case TAG_IDENT_FSD:
909 *fileset = newfileset;
910 break;
911 default:
912 newfileset.logicalBlockNum++;
913 brelse(bh);
914 bh = NULL;
915 break;
916 }
917 } while (newfileset.logicalBlockNum < lastblock &&
918 fileset->logicalBlockNum == 0xFFFFFFFF &&
919 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 }
922
923 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700924 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700926 fileset->logicalBlockNum,
927 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800929 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700931 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 0;
933 }
934 return 1;
935}
936
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700937static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 struct primaryVolDesc *pvoldesc;
940 time_t recording;
941 long recording_usec;
942 struct ustr instr;
943 struct ustr outstr;
944
945 pvoldesc = (struct primaryVolDesc *)bh->b_data;
946
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700947 if (udf_stamp_to_time(&recording, &recording_usec,
948 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 kernel_timestamp ts;
950 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800951 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
952 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700953 recording, recording_usec,
954 ts.year, ts.month, ts.day, ts.hour,
955 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800956 UDF_SB(sb)->s_record_time.tv_sec = recording;
957 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Marcin Slusarz4b111112008-02-08 04:20:36 -0800960 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800962 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800964 udf_debug("volIdent[] = '%s'\n",
965 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Marcin Slusarz4b111112008-02-08 04:20:36 -0800968 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (udf_CS0toUTF8(&outstr, &instr))
970 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700973static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
974 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 struct fileSetDesc *fset;
977
978 fset = (struct fileSetDesc *)bh->b_data;
979
980 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
981
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800982 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700984 udf_debug("Rootdir at block=%d, partition=%d\n",
985 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800988int udf_compute_nr_groups(struct super_block *sb, u32 partition)
989{
990 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100991 return DIV_ROUND_UP(map->s_partition_len +
992 (sizeof(struct spaceBitmapDesc) << 3),
993 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800994}
995
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800996static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
997{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800998 struct udf_bitmap *bitmap;
999 int nr_groups;
1000 int size;
1001
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001002 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001003 size = sizeof(struct udf_bitmap) +
1004 (sizeof(struct buffer_head *) * nr_groups);
1005
1006 if (size <= PAGE_SIZE)
1007 bitmap = kmalloc(size, GFP_KERNEL);
1008 else
1009 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1010
1011 if (bitmap == NULL) {
1012 udf_error(sb, __FUNCTION__,
1013 "Unable to allocate space for bitmap "
1014 "and %d buffer_head pointers", nr_groups);
1015 return NULL;
1016 }
1017
1018 memset(bitmap, 0x00, size);
1019 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1020 bitmap->s_nr_groups = nr_groups;
1021 return bitmap;
1022}
1023
Jan Karabcec4472007-08-30 23:56:22 -07001024static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct partitionDesc *p;
1027 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001028 struct udf_part_map *map;
1029 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001032 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001034 for (i = 0; i < sbi->s_partitions; i++) {
1035 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001037 map->s_partition_num,
1038 le16_to_cpu(p->partitionNumber));
1039 if (map->s_partition_num ==
1040 le16_to_cpu(p->partitionNumber)) {
1041 map->s_partition_len =
1042 le32_to_cpu(p->partitionLength); /* blocks */
1043 map->s_partition_root =
1044 le32_to_cpu(p->partitionStartingLocation);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001045 if (p->accessType ==
1046 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001047 map->s_partition_flags |=
1048 UDF_PART_FLAG_READ_ONLY;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001049 if (p->accessType ==
1050 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001051 map->s_partition_flags |=
1052 UDF_PART_FLAG_WRITE_ONCE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001053 if (p->accessType ==
1054 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001055 map->s_partition_flags |=
1056 UDF_PART_FLAG_REWRITABLE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001057 if (p->accessType ==
1058 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001059 map->s_partition_flags |=
1060 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001062 if (!strcmp(p->partitionContents.ident,
1063 PD_PARTITION_CONTENTS_NSR02) ||
1064 !strcmp(p->partitionContents.ident,
1065 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct partitionHeaderDesc *phd;
1067
Marcin Slusarz4b111112008-02-08 04:20:36 -08001068 phd = (struct partitionHeaderDesc *)
1069 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001070 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001071 kernel_lb_addr loc = {
1072 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1073 .partitionReferenceNum = i,
1074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001076 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001077 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001078 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001079 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001080 return 1;
1081 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001082 map->s_partition_flags |=
1083 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001084 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001085 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001088 struct udf_bitmap *bitmap =
1089 udf_sb_alloc_bitmap(sb, i);
1090 map->s_uspace.s_bitmap = bitmap;
1091 if (bitmap != NULL) {
1092 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001093 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001094 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001095 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001096 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001097 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001098 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 }
1101 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001103 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001104 kernel_lb_addr loc = {
1105 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1106 .partitionReferenceNum = i,
1107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001109 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001110 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001111 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001112 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001113 return 1;
1114 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001115 map->s_partition_flags |=
1116 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001117 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001118 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001120 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001121 struct udf_bitmap *bitmap =
1122 udf_sb_alloc_bitmap(sb, i);
1123 map->s_fspace.s_bitmap = bitmap;
1124 if (bitmap != NULL) {
1125 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001126 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001127 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001128 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001129 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001131 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 }
1134 }
1135 break;
1136 }
1137 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001138 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001139 udf_debug("Partition (%d) not found in partition map\n",
1140 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001141 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001142 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1143 "block length %d\n",
1144 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001145 map->s_partition_type,
1146 map->s_partition_root,
1147 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001151static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1152 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 struct logicalVolDesc *lvd;
1155 int i, j, offset;
1156 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001157 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001158 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 lvd = (struct logicalVolDesc *)bh->b_data;
1161
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001162 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1163 if (i != 0)
1164 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001166 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001167 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001168 i++, offset += gpm->partitionMapLength) {
1169 struct udf_part_map *map = &sbi->s_partmaps[i];
1170 gpm = (struct genericPartitionMap *)
1171 &(lvd->partitionMaps[offset]);
1172 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001173 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001174 struct genericPartitionMap1 *gpm1 =
1175 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001176 map->s_partition_type = UDF_TYPE1_MAP15;
1177 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1178 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1179 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001180 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001181 struct udfPartitionMap2 *upm2 =
1182 (struct udfPartitionMap2 *)gpm;
1183 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1184 strlen(UDF_ID_VIRTUAL))) {
1185 u16 suf =
1186 le16_to_cpu(((__le16 *)upm2->partIdent.
1187 identSuffix)[0]);
1188 if (suf == 0x0150) {
1189 map->s_partition_type =
1190 UDF_VIRTUAL_MAP15;
1191 map->s_partition_func =
1192 udf_get_pblock_virt15;
1193 } else if (suf == 0x0200) {
1194 map->s_partition_type =
1195 UDF_VIRTUAL_MAP20;
1196 map->s_partition_func =
1197 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001199 } else if (!strncmp(upm2->partIdent.ident,
1200 UDF_ID_SPARABLE,
1201 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 uint32_t loc;
1203 uint16_t ident;
1204 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001205 struct sparablePartitionMap *spm =
1206 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001208 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001209 map->s_type_specific.s_sparing.s_packet_len =
1210 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001211 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001212 struct buffer_head *bh2;
1213
1214 loc = le32_to_cpu(
1215 spm->locSparingTable[j]);
1216 bh2 = udf_read_tagged(sb, loc, loc,
1217 &ident);
1218 map->s_type_specific.s_sparing.
1219 s_spar_map[j] = bh2;
1220
1221 if (bh2 != NULL) {
1222 st = (struct sparingTable *)
1223 bh2->b_data;
1224 if (ident != 0 || strncmp(
1225 st->sparingIdent.ident,
1226 UDF_ID_SPARING,
1227 strlen(UDF_ID_SPARING))) {
1228 brelse(bh2);
1229 map->s_type_specific.
1230 s_sparing.
1231 s_spar_map[j] =
1232 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 }
1235 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001236 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001237 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001238 udf_debug("Unknown ident: %s\n",
1239 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 continue;
1241 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001242 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1243 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001246 i, map->s_partition_num, type,
1247 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001250 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001251 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001254 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1255 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001256 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 if (lvd->integritySeqExt.extLength)
1259 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262}
1263
1264/*
1265 * udf_load_logicalvolint
1266 *
1267 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001268static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct buffer_head *bh = NULL;
1271 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001272 struct udf_sb_info *sbi = UDF_SB(sb);
1273 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001276 (bh = udf_read_tagged(sb, loc.extLocation,
1277 loc.extLocation, &ident)) &&
1278 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001279 sbi->s_lvid_bh = bh;
1280 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001281
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001282 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001283 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001285
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001286 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001287 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001291 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001292 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*
1296 * udf_process_sequence
1297 *
1298 * PURPOSE
1299 * Process a main/reserve volume descriptor sequence.
1300 *
1301 * PRE-CONDITIONS
1302 * sb Pointer to _locked_ superblock.
1303 * block First block of first extent of the sequence.
1304 * lastblock Lastblock of first extent of the sequence.
1305 *
1306 * HISTORY
1307 * July 1, 1997 - Andrew E. Mileski
1308 * Written, tested, and released.
1309 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001310static int udf_process_sequence(struct super_block *sb, long block,
1311 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct buffer_head *bh = NULL;
1314 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001315 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct generic_desc *gd;
1317 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001318 int done = 0;
1319 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 uint32_t vdsn;
1321 uint16_t ident;
1322 long next_s = 0, next_e = 0;
1323
1324 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1325
1326 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001327 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001330 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332
1333 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1334 gd = (struct generic_desc *)bh->b_data;
1335 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001336 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001337 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001338 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1339 if (vdsn >= curr->volDescSeqNum) {
1340 curr->volDescSeqNum = vdsn;
1341 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 }
1343 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001344 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001345 curr = &vds[VDS_POS_VOL_DESC_PTR];
1346 if (vdsn >= curr->volDescSeqNum) {
1347 curr->volDescSeqNum = vdsn;
1348 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001350 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001351 next_s = le32_to_cpu(
1352 vdp->nextVolDescSeqExt.extLocation);
1353 next_e = le32_to_cpu(
1354 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 next_e = next_e >> sb->s_blocksize_bits;
1356 next_e += next_s;
1357 }
1358 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001359 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001360 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1361 if (vdsn >= curr->volDescSeqNum) {
1362 curr->volDescSeqNum = vdsn;
1363 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001364 }
1365 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001366 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001367 curr = &vds[VDS_POS_PARTITION_DESC];
1368 if (!curr->block)
1369 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001371 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001372 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1373 if (vdsn >= curr->volDescSeqNum) {
1374 curr->volDescSeqNum = vdsn;
1375 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001376 }
1377 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001378 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001379 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1380 if (vdsn >= curr->volDescSeqNum) {
1381 curr->volDescSeqNum = vdsn;
1382 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001383 }
1384 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001386 vds[VDS_POS_TERMINATING_DESC].block = block;
1387 if (next_e) {
1388 block = next_s;
1389 lastblock = next_e;
1390 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001391 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001392 done = 1;
1393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001395 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001397 for (i = 0; i < VDS_POS_LENGTH; i++) {
1398 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001399 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1400 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001402 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001405 if (udf_load_logicalvol(sb, bh, fileset)) {
1406 brelse(bh);
1407 return 1;
1408 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001409 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001411 if (udf_load_partdesc(sb, bh)) {
1412 brelse(bh);
1413 return 1;
1414 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001415 for (j = vds[i].block + 1;
1416 j < vds[VDS_POS_TERMINATING_DESC].block;
1417 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 bh2 = udf_read_tagged(sb, j, j, &ident);
1419 gd = (struct generic_desc *)bh2->b_data;
1420 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001421 if (udf_load_partdesc(sb,
1422 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001423 brelse(bh);
1424 brelse(bh2);
1425 return 1;
1426 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001427 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001430 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 }
1433
1434 return 0;
1435}
1436
1437/*
1438 * udf_check_valid()
1439 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001440static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 long block;
1443
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001444 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 udf_debug("Validity check skipped because of novrs option\n");
1446 return 0;
1447 }
1448 /* Check that it is NSR02 compliant */
1449 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001450 else {
1451 block = udf_vrs(sb, silent);
1452 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001453 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001454 udf_debug("Failed to read byte 32768. Assuming open "
1455 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001456 if (!sbi->s_last_block)
1457 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001458 return 0;
1459 } else
1460 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001464static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct anchorVolDescPtr *anchor;
1467 uint16_t ident;
1468 struct buffer_head *bh;
1469 long main_s, main_e, reserve_s, reserve_e;
1470 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001471 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (!sb)
1474 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001475 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001477 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001478 if (!sbi->s_anchor[i])
1479 continue;
1480 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1481 &ident);
1482 if (!bh)
1483 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001485 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001486
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001487 /* Locate the main sequence */
1488 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1489 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1490 main_e = main_e >> sb->s_blocksize_bits;
1491 main_e += main_s;
1492
1493 /* Locate the reserve sequence */
1494 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001495 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001496 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001497 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001498 reserve_e = reserve_e >> sb->s_blocksize_bits;
1499 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001501 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001503 /* Process the main & reserve sequences */
1504 /* responsible for finding the PartitionDesc(s) */
1505 if (!(udf_process_sequence(sb, main_s, main_e,
1506 fileset) &&
1507 udf_process_sequence(sb, reserve_s, reserve_e,
1508 fileset)))
1509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001512 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 udf_debug("No Anchor block found\n");
1514 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001515 }
1516 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001519 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001520 struct udf_part_map *map = &sbi->s_partmaps[i];
1521 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001522 case UDF_VIRTUAL_MAP15:
1523 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001524 if (!sbi->s_last_block) {
1525 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001526 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001528
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001529 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001531 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001532 return 1;
1533 }
1534
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001535 for (j = 0; j < sbi->s_partitions; j++) {
1536 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001537 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001538 map->s_volumeseqnum ==
1539 map2->s_volumeseqnum &&
1540 map->s_partition_num ==
1541 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001542 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001543 ino.logicalBlockNum =
1544 sbi->s_last_block -
1545 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001546 break;
1547 }
1548 }
1549
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001550 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001551 return 1;
1552
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 sbi->s_vat_inode = udf_iget(sb, ino);
1554 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001555 return 1;
1556
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001557 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1558 map->s_type_specific.s_virtual.s_start_offset =
1559 udf_ext0_offset(sbi->s_vat_inode);
1560 map->s_type_specific.s_virtual.s_num_entries =
1561 (sbi->s_vat_inode->i_size - 36) >> 2;
1562 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001563 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001564 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001565
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001566 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001567 bh = sb_bread(sb, pos);
1568 if (!bh)
1569 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001570 vat20 = (struct virtualAllocationTable20 *)
1571 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001572 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001573 map->s_type_specific.s_virtual.s_start_offset =
1574 le16_to_cpu(vat20->lengthHeader) +
1575 udf_ext0_offset(sbi->s_vat_inode);
1576 map->s_type_specific.s_virtual.s_num_entries =
1577 (sbi->s_vat_inode->i_size -
1578 map->s_type_specific.s_virtual.
1579 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001580 brelse(bh);
1581 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001582 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1583 map->s_partition_len =
1584 sbi->s_partmaps[ino.partitionReferenceNum].
1585 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 }
1588 return 0;
1589}
1590
1591static void udf_open_lvid(struct super_block *sb)
1592{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001593 struct udf_sb_info *sbi = UDF_SB(sb);
1594 struct buffer_head *bh = sbi->s_lvid_bh;
1595 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001597 struct logicalVolIntegrityDesc *lvid =
1598 (struct logicalVolIntegrityDesc *)bh->b_data;
1599 struct logicalVolIntegrityDescImpUse *lvidiu =
1600 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001602 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1603 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001605 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1606 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 lvid->descTag.descCRC = cpu_to_le16(
1609 udf_crc((char *)lvid + sizeof(tag),
1610 le16_to_cpu(lvid->descTag.descCRCLength),
1611 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001613 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001614 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616}
1617
1618static void udf_close_lvid(struct super_block *sb)
1619{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001620 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001621 struct udf_sb_info *sbi = UDF_SB(sb);
1622 struct buffer_head *bh = sbi->s_lvid_bh;
1623 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001624
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001625 if (!bh)
1626 return;
1627
1628 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1629
1630 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001631 struct logicalVolIntegrityDescImpUse *lvidiu =
1632 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001633 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1634 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001636 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1637 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001638 lvidiu->maxUDFWriteRev =
1639 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001640 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1641 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1642 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1643 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1644 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
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
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001656static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1657{
1658 int i;
1659 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001660 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1661 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001662
1663 for (i = 0; i < nr_groups; i++)
1664 if (bitmap->s_block_bitmap[i])
1665 brelse(bitmap->s_block_bitmap[i]);
1666
1667 if (size <= PAGE_SIZE)
1668 kfree(bitmap);
1669 else
1670 vfree(bitmap);
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static int udf_fill_super(struct super_block *sb, void *options, int silent)
1674{
1675 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct udf_options uopt;
1678 kernel_lb_addr rootdir, fileset;
1679 struct udf_sb_info *sbi;
1680
1681 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1682 uopt.uid = -1;
1683 uopt.gid = -1;
1684 uopt.umask = 0;
1685
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001686 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (!sbi)
1688 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001692 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Miklos Szeredi6da80892008-02-08 04:21:50 -08001694 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto error_out;
1696
1697 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001698 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001700 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto error_out;
1702 }
1703#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001704 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 uopt.nls_map = load_nls_default();
1706 if (!uopt.nls_map)
1707 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1708 else
1709 udf_debug("Using default NLS map\n");
1710 }
1711#endif
1712 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1713 uopt.flags |= (1 << UDF_FLAG_UTF8);
1714
1715 fileset.logicalBlockNum = 0xFFFFFFFF;
1716 fileset.partitionReferenceNum = 0xFFFF;
1717
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001718 sbi->s_flags = uopt.flags;
1719 sbi->s_uid = uopt.uid;
1720 sbi->s_gid = uopt.gid;
1721 sbi->s_umask = uopt.umask;
1722 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001725 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1726 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1727 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001731 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001732 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001734 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001736 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001738 sbi->s_last_block = uopt.lastblock;
1739 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1740 sbi->s_anchor[2] = uopt.anchor;
1741 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001743 if (udf_check_valid(sb, uopt.novrs, silent)) {
1744 /* read volume recognition sequences */
1745 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001746 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748
1749 udf_find_anchor(sb);
1750
1751 /* Fill in the rest of the superblock */
1752 sb->s_op = &udf_sb_ops;
1753 sb->dq_op = NULL;
1754 sb->s_dirt = 0;
1755 sb->s_magic = UDF_SUPER_MAGIC;
1756 sb->s_time_gran = 1000;
1757
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001758 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001759 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto error_out;
1761 }
1762
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001763 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001765 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001766 struct logicalVolIntegrityDescImpUse *lvidiu =
1767 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001768 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1769 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001770 /* uint16_t maxUDFWriteRev =
1771 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001773 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001774 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1775 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001776 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001777 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001779 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001782 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1785 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1786 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1787 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1788 }
1789
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001790 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001791 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto error_out;
1793 }
1794
Marcin Slusarz4b111112008-02-08 04:20:36 -08001795 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1796 UDF_PART_FLAG_READ_ONLY) {
1797 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1798 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001799 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001800 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001801
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001802 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001803 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 goto error_out;
1805 }
1806
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001807 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001809 udf_time_to_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001810 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001811 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001812 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1813 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815 if (!(sb->s_flags & MS_RDONLY))
1816 udf_open_lvid(sb);
1817
1818 /* Assign the root inode */
1819 /* assign inodes by physical block number */
1820 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001821 inode = udf_iget(sb, rootdir);
1822 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001823 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1824 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001825 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto error_out;
1827 }
1828
1829 /* Allocate a dentry for the root inode */
1830 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001831 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001832 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 iput(inode);
1834 goto error_out;
1835 }
Jan Kara31170b62007-05-08 00:35:21 -07001836 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return 0;
1838
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001839error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001840 if (sbi->s_vat_inode)
1841 iput(sbi->s_vat_inode);
1842 if (sbi->s_partitions) {
1843 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1844 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1845 iput(map->s_uspace.s_table);
1846 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1847 iput(map->s_fspace.s_table);
1848 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001849 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001850 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001851 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001852 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001853 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001854 brelse(map->s_type_specific.s_sparing.
1855 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857#ifdef CONFIG_UDF_NLS
1858 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001859 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860#endif
1861 if (!(sb->s_flags & MS_RDONLY))
1862 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001863 brelse(sbi->s_lvid_bh);
1864
1865 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 kfree(sbi);
1867 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return -EINVAL;
1870}
1871
Adrian Bunkb8145a72008-02-17 10:19:55 +02001872static void udf_error(struct super_block *sb, const char *function,
1873 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 va_list args;
1876
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001877 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /* mark sb error */
1879 sb->s_dirt = 1;
1880 }
1881 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001882 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001884 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001885 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
1888void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 va_list args;
1892
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001893 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001894 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 va_end(args);
1896 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001897 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001900static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001903 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001905 sbi = UDF_SB(sb);
1906 if (sbi->s_vat_inode)
1907 iput(sbi->s_vat_inode);
1908 if (sbi->s_partitions) {
1909 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1910 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1911 iput(map->s_uspace.s_table);
1912 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1913 iput(map->s_fspace.s_table);
1914 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001915 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001916 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001917 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001918 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001919 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001920 brelse(map->s_type_specific.s_sparing.
1921 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923#ifdef CONFIG_UDF_NLS
1924 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001925 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926#endif
1927 if (!(sb->s_flags & MS_RDONLY))
1928 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001929 brelse(sbi->s_lvid_bh);
1930 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 kfree(sb->s_fs_info);
1932 sb->s_fs_info = NULL;
1933}
1934
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001935static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
David Howells726c3342006-06-23 02:02:58 -07001937 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001938 struct udf_sb_info *sbi = UDF_SB(sb);
1939 struct logicalVolIntegrityDescImpUse *lvidiu;
1940
1941 if (sbi->s_lvid_bh != NULL)
1942 lvidiu = udf_sb_lvidiu(sbi);
1943 else
1944 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 buf->f_type = UDF_SUPER_MAGIC;
1947 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001948 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 buf->f_bfree = udf_count_free(sb);
1950 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001951 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1952 le32_to_cpu(lvidiu->numDirs)) : 0)
1953 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 buf->f_ffree = buf->f_bfree;
1955 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001956 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 return 0;
1959}
1960
1961static unsigned char udf_bitmap_lookup[16] = {
1962 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1963};
1964
Marcin Slusarz4b111112008-02-08 04:20:36 -08001965static unsigned int udf_count_free_bitmap(struct super_block *sb,
1966 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct buffer_head *bh = NULL;
1969 unsigned int accum = 0;
1970 int index;
1971 int block = 0, newblock;
1972 kernel_lb_addr loc;
1973 uint32_t bytes;
1974 uint8_t value;
1975 uint8_t *ptr;
1976 uint16_t ident;
1977 struct spaceBitmapDesc *bm;
1978
1979 lock_kernel();
1980
1981 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001982 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 bh = udf_read_ptagged(sb, loc, 0, &ident);
1984
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001985 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 printk(KERN_ERR "udf: udf_count_free failed\n");
1987 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001988 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001989 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 printk(KERN_ERR "udf: udf_count_free failed\n");
1991 goto out;
1992 }
1993
1994 bm = (struct spaceBitmapDesc *)bh->b_data;
1995 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001996 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1997 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001999 while (bytes > 0) {
2000 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002002 accum += udf_bitmap_lookup[value & 0x0f];
2003 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 index++;
2005 bytes--;
2006 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002007 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002008 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 newblock = udf_get_lb_pblock(sb, loc, ++block);
2010 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002011 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 udf_debug("read failed\n");
2013 goto out;
2014 }
2015 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002016 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002019 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002021out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 unlock_kernel();
2023
2024 return accum;
2025}
2026
Marcin Slusarz4b111112008-02-08 04:20:36 -08002027static unsigned int udf_count_free_table(struct super_block *sb,
2028 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002031 uint32_t elen;
2032 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002034 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 lock_kernel();
2037
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002038 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002039 epos.offset = sizeof(struct unallocSpaceEntry);
2040 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002042 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002044
Jan Kara3bf25cb2007-05-08 00:35:16 -07002045 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 unlock_kernel();
2048
2049 return accum;
2050}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002051
2052static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002055 struct udf_sb_info *sbi;
2056 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002058 sbi = UDF_SB(sb);
2059 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002060 struct logicalVolIntegrityDesc *lvid =
2061 (struct logicalVolIntegrityDesc *)
2062 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002063 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002064 accum = le32_to_cpu(
2065 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (accum == 0xFFFFFFFF)
2067 accum = 0;
2068 }
2069 }
2070
2071 if (accum)
2072 return accum;
2073
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002074 map = &sbi->s_partmaps[sbi->s_partition];
2075 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002076 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002077 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002079 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002080 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002081 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083 if (accum)
2084 return accum;
2085
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002086 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002087 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002090 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002091 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002092 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 return accum;
2096}