blob: 5f0d400385e0fd05bb8b0c3c78201a32b0674abf [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800103struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
104{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800105 struct logicalVolIntegrityDesc *lvid =
106 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800107 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800108 __u32 offset = number_of_partitions * 2 *
109 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800110 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700114static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 int flags, const char *dev_name, void *data,
116 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
David Howells454e2392006-06-23 02:02:57 -0700118 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700122 .owner = THIS_MODULE,
123 .name = "udf",
124 .get_sb = udf_get_sb,
125 .kill_sb = kill_block_super,
126 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700129static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static struct inode *udf_alloc_inode(struct super_block *sb)
132{
133 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800134 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (!ei)
136 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700137
138 ei->i_unique = 0;
139 ei->i_lenExtents = 0;
140 ei->i_next_alloc_block = 0;
141 ei->i_next_alloc_goal = 0;
142 ei->i_strat4096 = 0;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return &ei->vfs_inode;
145}
146
147static void udf_destroy_inode(struct inode *inode)
148{
149 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
150}
151
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700152static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700154 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Christoph Lametera35afb82007-05-16 22:10:57 -0700156 ei->i_ext.i_data = NULL;
157 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static int init_inodecache(void)
161{
162 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
163 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 0, (SLAB_RECLAIM_ACCOUNT |
165 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900166 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700167 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -ENOMEM;
169 return 0;
170}
171
172static void destroy_inodecache(void)
173{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700174 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800178static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700179 .alloc_inode = udf_alloc_inode,
180 .destroy_inode = udf_destroy_inode,
181 .write_inode = udf_write_inode,
182 .delete_inode = udf_delete_inode,
183 .clear_inode = udf_clear_inode,
184 .put_super = udf_put_super,
185 .write_super = udf_write_super,
186 .statfs = udf_statfs,
187 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800188 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700191struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 unsigned char novrs;
193 unsigned int blocksize;
194 unsigned int session;
195 unsigned int lastblock;
196 unsigned int anchor;
197 unsigned int volume;
198 unsigned short partition;
199 unsigned int fileset;
200 unsigned int rootdir;
201 unsigned int flags;
202 mode_t umask;
203 gid_t gid;
204 uid_t uid;
205 struct nls_table *nls_map;
206};
207
208static int __init init_udf_fs(void)
209{
210 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 err = init_inodecache();
213 if (err)
214 goto out1;
215 err = register_filesystem(&udf_fstype);
216 if (err)
217 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700220
221out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700223
224out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return err;
226}
227
228static void __exit exit_udf_fs(void)
229{
230 unregister_filesystem(&udf_fstype);
231 destroy_inodecache();
232}
233
234module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700235module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800237static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
238{
239 struct udf_sb_info *sbi = UDF_SB(sb);
240
241 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
242 GFP_KERNEL);
243 if (!sbi->s_partmaps) {
244 udf_error(sb, __FUNCTION__,
245 "Unable to allocate space for %d partition maps",
246 count);
247 sbi->s_partitions = 0;
248 return -ENOMEM;
249 }
250
251 sbi->s_partitions = count;
252 return 0;
253}
254
Miklos Szeredi6da80892008-02-08 04:21:50 -0800255static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
256{
257 struct super_block *sb = mnt->mnt_sb;
258 struct udf_sb_info *sbi = UDF_SB(sb);
259
260 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
261 seq_puts(seq, ",nostrict");
262 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
263 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
265 seq_puts(seq, ",unhide");
266 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
267 seq_puts(seq, ",undelete");
268 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
269 seq_puts(seq, ",noadinicb");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
271 seq_puts(seq, ",shortad");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
273 seq_puts(seq, ",uid=forget");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
275 seq_puts(seq, ",uid=ignore");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
277 seq_puts(seq, ",gid=forget");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
279 seq_puts(seq, ",gid=ignore");
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
281 seq_printf(seq, ",uid=%u", sbi->s_uid);
282 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
283 seq_printf(seq, ",gid=%u", sbi->s_gid);
284 if (sbi->s_umask != 0)
285 seq_printf(seq, ",umask=%o", sbi->s_umask);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
287 seq_printf(seq, ",session=%u", sbi->s_session);
288 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
289 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
290 /*
291 * s_anchor[2] could be zeroed out in case there is no anchor
292 * in the specified block, but then the "anchor=N" option
293 * originally given by the user wasn't effective, so it's OK
294 * if we don't show it.
295 */
296 if (sbi->s_anchor[2] != 0)
297 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
298 /*
299 * volume, partition, fileset and rootdir seem to be ignored
300 * currently
301 */
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
303 seq_puts(seq, ",utf8");
304 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
305 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
306
307 return 0;
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * udf_parse_options
312 *
313 * PURPOSE
314 * Parse mount options.
315 *
316 * DESCRIPTION
317 * The following mount options are supported:
318 *
319 * gid= Set the default group.
320 * umask= Set the default umask.
321 * uid= Set the default user.
322 * bs= Set the block size.
323 * unhide Show otherwise hidden files.
324 * undelete Show deleted files in lists.
325 * adinicb Embed data in the inode (default)
326 * noadinicb Don't embed data in the inode
327 * shortad Use short ad's
328 * longad Use long ad's (default)
329 * nostrict Unset strict conformance
330 * iocharset= Set the NLS character set
331 *
332 * The remaining are for debugging and disaster recovery:
333 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700334 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *
336 * The following expect a offset from 0.
337 *
338 * session= Set the CDROM session (default= last session)
339 * anchor= Override standard anchor location. (default= 256)
340 * volume= Override the VolumeDesc location. (unused)
341 * partition= Override the PartitionDesc location. (unused)
342 * lastblock= Set the last block of the filesystem/
343 *
344 * The following expect a offset from the partition root.
345 *
346 * fileset= Override the fileset block location. (unused)
347 * rootdir= Override the root directory location. (unused)
348 * WARNING: overriding the rootdir to a non-directory may
349 * yield highly unpredictable results.
350 *
351 * PRE-CONDITIONS
352 * options Pointer to mount options string.
353 * uopts Pointer to mount options variable.
354 *
355 * POST-CONDITIONS
356 * <return> 1 Mount options parsed okay.
357 * <return> 0 Error parsing mount options.
358 *
359 * HISTORY
360 * July 1, 1997 - Andrew E. Mileski
361 * Written, tested, and released.
362 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364enum {
365 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
366 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
367 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
368 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
369 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800370 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
373static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700374 {Opt_novrs, "novrs"},
375 {Opt_nostrict, "nostrict"},
376 {Opt_bs, "bs=%u"},
377 {Opt_unhide, "unhide"},
378 {Opt_undelete, "undelete"},
379 {Opt_noadinicb, "noadinicb"},
380 {Opt_adinicb, "adinicb"},
381 {Opt_shortad, "shortad"},
382 {Opt_longad, "longad"},
383 {Opt_uforget, "uid=forget"},
384 {Opt_uignore, "uid=ignore"},
385 {Opt_gforget, "gid=forget"},
386 {Opt_gignore, "gid=ignore"},
387 {Opt_gid, "gid=%u"},
388 {Opt_uid, "uid=%u"},
389 {Opt_umask, "umask=%o"},
390 {Opt_session, "session=%u"},
391 {Opt_lastblock, "lastblock=%u"},
392 {Opt_anchor, "anchor=%u"},
393 {Opt_volume, "volume=%u"},
394 {Opt_partition, "partition=%u"},
395 {Opt_fileset, "fileset=%u"},
396 {Opt_rootdir, "rootdir=%u"},
397 {Opt_utf8, "utf8"},
398 {Opt_iocharset, "iocharset=%s"},
399 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
401
Miklos Szeredi6da80892008-02-08 04:21:50 -0800402static int udf_parse_options(char *options, struct udf_options *uopt,
403 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 char *p;
406 int option;
407
408 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800409 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 uopt->partition = 0xFFFF;
411 uopt->session = 0xFFFFFFFF;
412 uopt->lastblock = 0;
413 uopt->anchor = 0;
414 uopt->volume = 0xFFFFFFFF;
415 uopt->rootdir = 0xFFFFFFFF;
416 uopt->fileset = 0xFFFFFFFF;
417 uopt->nls_map = NULL;
418
419 if (!options)
420 return 1;
421
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700422 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 substring_t args[MAX_OPT_ARGS];
424 int token;
425 if (!*p)
426 continue;
427
428 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700429 switch (token) {
430 case Opt_novrs:
431 uopt->novrs = 1;
432 case Opt_bs:
433 if (match_int(&args[0], &option))
434 return 0;
435 uopt->blocksize = option;
436 break;
437 case Opt_unhide:
438 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
439 break;
440 case Opt_undelete:
441 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
442 break;
443 case Opt_noadinicb:
444 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
445 break;
446 case Opt_adinicb:
447 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
448 break;
449 case Opt_shortad:
450 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
451 break;
452 case Opt_longad:
453 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
454 break;
455 case Opt_gid:
456 if (match_int(args, &option))
457 return 0;
458 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700459 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700460 break;
461 case Opt_uid:
462 if (match_int(args, &option))
463 return 0;
464 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700465 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700466 break;
467 case Opt_umask:
468 if (match_octal(args, &option))
469 return 0;
470 uopt->umask = option;
471 break;
472 case Opt_nostrict:
473 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
474 break;
475 case Opt_session:
476 if (match_int(args, &option))
477 return 0;
478 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800479 if (!remount)
480 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700481 break;
482 case Opt_lastblock:
483 if (match_int(args, &option))
484 return 0;
485 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800486 if (!remount)
487 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700488 break;
489 case Opt_anchor:
490 if (match_int(args, &option))
491 return 0;
492 uopt->anchor = option;
493 break;
494 case Opt_volume:
495 if (match_int(args, &option))
496 return 0;
497 uopt->volume = option;
498 break;
499 case Opt_partition:
500 if (match_int(args, &option))
501 return 0;
502 uopt->partition = option;
503 break;
504 case Opt_fileset:
505 if (match_int(args, &option))
506 return 0;
507 uopt->fileset = option;
508 break;
509 case Opt_rootdir:
510 if (match_int(args, &option))
511 return 0;
512 uopt->rootdir = option;
513 break;
514 case Opt_utf8:
515 uopt->flags |= (1 << UDF_FLAG_UTF8);
516 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700518 case Opt_iocharset:
519 uopt->nls_map = load_nls(args[0].from);
520 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700523 case Opt_uignore:
524 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
525 break;
526 case Opt_uforget:
527 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
528 break;
529 case Opt_gignore:
530 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
531 break;
532 case Opt_gforget:
533 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
534 break;
535 default:
536 printk(KERN_ERR "udf: bad mount option \"%s\" "
537 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539 }
540 }
541 return 1;
542}
543
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800544static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!(sb->s_flags & MS_RDONLY))
549 udf_open_lvid(sb);
550 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 unlock_kernel();
553}
554
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800558 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800560 uopt.flags = sbi->s_flags;
561 uopt.uid = sbi->s_uid;
562 uopt.gid = sbi->s_gid;
563 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Miklos Szeredi6da80892008-02-08 04:21:50 -0800565 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return -EINVAL;
567
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800568 sbi->s_flags = uopt.flags;
569 sbi->s_uid = uopt.uid;
570 sbi->s_gid = uopt.gid;
571 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800573 if (sbi->s_lvid_bh) {
574 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (write_rev > UDF_MAX_WRITE_VERSION)
576 *flags |= MS_RDONLY;
577 }
578
579 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
580 return 0;
581 if (*flags & MS_RDONLY)
582 udf_close_lvid(sb);
583 else
584 udf_open_lvid(sb);
585
586 return 0;
587}
588
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700589static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct volStructDesc *vsd = NULL;
592 int sector = 32768;
593 int sectorsize;
594 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700595 int iso9660 = 0;
596 int nsr02 = 0;
597 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800598 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Block size must be a multiple of 512 */
601 if (sb->s_blocksize & 511)
602 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800603 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (sb->s_blocksize < sizeof(struct volStructDesc))
606 sectorsize = sizeof(struct volStructDesc);
607 else
608 sectorsize = sb->s_blocksize;
609
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800610 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700615 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Read a block */
617 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
618 if (!bh)
619 break;
620
621 /* Look for ISO descriptors */
622 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800623 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700625 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700626 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800628 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
629 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700631 switch (vsd->structType) {
632 case 0:
633 udf_debug("ISO9660 Boot Record found\n");
634 break;
635 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800636 udf_debug("ISO9660 Primary Volume Descriptor "
637 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700638 break;
639 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800640 udf_debug("ISO9660 Supplementary Volume "
641 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642 break;
643 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800644 udf_debug("ISO9660 Volume Partition Descriptor "
645 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700646 break;
647 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800648 udf_debug("ISO9660 Volume Descriptor Set "
649 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700650 break;
651 default:
652 udf_debug("ISO9660 VRS (%u) found\n",
653 vsd->structType);
654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800656 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
657 VSD_STD_ID_LEN))
658 ; /* nothing */
659 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
660 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700661 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800663 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
664 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800666 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
667 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700669 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
672 if (nsr03)
673 return nsr03;
674 else if (nsr02)
675 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800676 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -1;
678 else
679 return 0;
680}
681
682/*
683 * udf_find_anchor
684 *
685 * PURPOSE
686 * Find an anchor volume descriptor.
687 *
688 * PRE-CONDITIONS
689 * sb Pointer to _locked_ superblock.
690 * lastblock Last block on media.
691 *
692 * POST-CONDITIONS
693 * <return> 1 if not found, 0 if ok
694 *
695 * HISTORY
696 * July 1, 1997 - Andrew E. Mileski
697 * Written, tested, and released.
698 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700699static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800701 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct buffer_head *bh = NULL;
703 uint16_t ident;
704 uint32_t location;
705 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800706 struct udf_sb_info *sbi;
707
708 sbi = UDF_SB(sb);
709 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700711 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700713 int last[] = { lastblock, lastblock - 2,
714 lastblock - 150, lastblock - 152,
715 varlastblock, varlastblock - 2,
716 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 lastblock = 0;
719
720 /* Search for an anchor volume descriptor pointer */
721
722 /* according to spec, anchor is in either:
723 * block 256
724 * lastblock-256
725 * lastblock
726 * however, if the disc isn't closed, it could be 512 */
727
Tobias Klausere8c96f82006-03-24 03:15:34 -0800728 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800729 ident = location = 0;
730 if (last[i] >= 0) {
731 bh = sb_bread(sb, last[i]);
732 if (bh) {
733 tag *t = (tag *)bh->b_data;
734 ident = le16_to_cpu(t->tagIdent);
735 location = le32_to_cpu(t->tagLocation);
736 brelse(bh);
737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800739
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700740 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800741 if (location == last[i] - sbi->s_session) {
742 lastblock = last[i] - sbi->s_session;
743 sbi->s_anchor[0] = lastblock;
744 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800745 } else if (location ==
746 udf_variable_to_fixed(last[i]) -
747 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800749 lastblock =
750 udf_variable_to_fixed(last[i]) -
751 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800752 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800753 sbi->s_anchor[1] = lastblock - 256 -
754 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700755 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800756 udf_debug("Anchor found at block %d, "
757 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700758 last[i], location);
759 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800760 } else if (ident == TAG_IDENT_FE ||
761 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800763 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700764 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800765 ident = location = 0;
766 if (last[i] >= 256) {
767 bh = sb_bread(sb, last[i] - 256);
768 if (bh) {
769 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800770 ident = le16_to_cpu(
771 t->tagIdent);
772 location = le32_to_cpu(
773 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800774 brelse(bh);
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800779 location == last[i] - 256 -
780 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800782 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700783 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800784 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800785 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800786 bh = sb_bread(sb,
787 last[i] - 312 -
788 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800789 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800790 tag *t = (tag *)
791 bh->b_data;
792 ident = le16_to_cpu(
793 t->tagIdent);
794 location = le32_to_cpu(
795 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800796 brelse(bh);
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700801 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800802 UDF_SET_FLAG(sb,
803 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700804 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800805 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807 }
808 }
809 }
810 }
811
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700812 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800813 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800814 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800815 if (bh) {
816 tag *t = (tag *)bh->b_data;
817 ident = le16_to_cpu(t->tagIdent);
818 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700819 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (ident == TAG_IDENT_AVDP && location == 256)
822 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
823 }
824 }
825
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800826 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
827 if (sbi->s_anchor[i]) {
828 bh = udf_read_tagged(sb, sbi->s_anchor[i],
829 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800830 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800831 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800832 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700833 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700834 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800835 (i || (ident != TAG_IDENT_FE &&
836 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800837 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 }
840 }
841
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800842 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800845static int udf_find_fileset(struct super_block *sb,
846 kernel_lb_addr *fileset,
847 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct buffer_head *bh = NULL;
850 long lastblock;
851 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800852 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700855 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
857
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700858 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700860 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700861 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 1;
863 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800867 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800868 if (!bh) {
869 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 kernel_lb_addr newfileset;
871
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700872/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800875 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700876 (newfileset.partitionReferenceNum != 0xFFFF &&
877 fileset->logicalBlockNum == 0xFFFFFFFF &&
878 fileset->partitionReferenceNum == 0xFFFF);
879 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800880 lastblock = sbi->s_partmaps
881 [newfileset.partitionReferenceNum]
882 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 newfileset.logicalBlockNum = 0;
884
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700885 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800886 bh = udf_read_ptagged(sb, newfileset, 0,
887 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700888 if (!bh) {
889 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 continue;
891 }
892
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700893 switch (ident) {
894 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700895 {
896 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800897 sp = (struct spaceBitmapDesc *)
898 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700899 newfileset.logicalBlockNum += 1 +
900 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800901 sizeof(struct spaceBitmapDesc)
902 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700903 brelse(bh);
904 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700906 case TAG_IDENT_FSD:
907 *fileset = newfileset;
908 break;
909 default:
910 newfileset.logicalBlockNum++;
911 brelse(bh);
912 bh = NULL;
913 break;
914 }
915 } while (newfileset.logicalBlockNum < lastblock &&
916 fileset->logicalBlockNum == 0xFFFFFFFF &&
917 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 }
920
921 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700922 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700924 fileset->logicalBlockNum,
925 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800927 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700929 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931 }
932 return 1;
933}
934
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700935static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct primaryVolDesc *pvoldesc;
938 time_t recording;
939 long recording_usec;
940 struct ustr instr;
941 struct ustr outstr;
942
943 pvoldesc = (struct primaryVolDesc *)bh->b_data;
944
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700945 if (udf_stamp_to_time(&recording, &recording_usec,
946 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 kernel_timestamp ts;
948 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800949 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
950 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700951 recording, recording_usec,
952 ts.year, ts.month, ts.day, ts.hour,
953 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800954 UDF_SB(sb)->s_record_time.tv_sec = recording;
955 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957
Marcin Slusarz4b111112008-02-08 04:20:36 -0800958 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700959 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800960 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800962 udf_debug("volIdent[] = '%s'\n",
963 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Marcin Slusarz4b111112008-02-08 04:20:36 -0800966 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (udf_CS0toUTF8(&outstr, &instr))
968 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700971static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
972 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 struct fileSetDesc *fset;
975
976 fset = (struct fileSetDesc *)bh->b_data;
977
978 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
979
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800980 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700982 udf_debug("Rootdir at block=%d, partition=%d\n",
983 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800986int udf_compute_nr_groups(struct super_block *sb, u32 partition)
987{
988 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100989 return DIV_ROUND_UP(map->s_partition_len +
990 (sizeof(struct spaceBitmapDesc) << 3),
991 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800992}
993
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800994static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
995{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800996 struct udf_bitmap *bitmap;
997 int nr_groups;
998 int size;
999
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001000 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001001 size = sizeof(struct udf_bitmap) +
1002 (sizeof(struct buffer_head *) * nr_groups);
1003
1004 if (size <= PAGE_SIZE)
1005 bitmap = kmalloc(size, GFP_KERNEL);
1006 else
1007 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1008
1009 if (bitmap == NULL) {
1010 udf_error(sb, __FUNCTION__,
1011 "Unable to allocate space for bitmap "
1012 "and %d buffer_head pointers", nr_groups);
1013 return NULL;
1014 }
1015
1016 memset(bitmap, 0x00, size);
1017 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1018 bitmap->s_nr_groups = nr_groups;
1019 return bitmap;
1020}
1021
Jan Karabcec4472007-08-30 23:56:22 -07001022static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct partitionDesc *p;
1025 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001026 struct udf_part_map *map;
1027 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001030 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001032 for (i = 0; i < sbi->s_partitions; i++) {
1033 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001034 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001035 map->s_partition_num,
1036 le16_to_cpu(p->partitionNumber));
1037 if (map->s_partition_num ==
1038 le16_to_cpu(p->partitionNumber)) {
1039 map->s_partition_len =
1040 le32_to_cpu(p->partitionLength); /* blocks */
1041 map->s_partition_root =
1042 le32_to_cpu(p->partitionStartingLocation);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001043 if (p->accessType ==
1044 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001045 map->s_partition_flags |=
1046 UDF_PART_FLAG_READ_ONLY;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001047 if (p->accessType ==
1048 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001049 map->s_partition_flags |=
1050 UDF_PART_FLAG_WRITE_ONCE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001051 if (p->accessType ==
1052 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001053 map->s_partition_flags |=
1054 UDF_PART_FLAG_REWRITABLE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001055 if (p->accessType ==
1056 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001057 map->s_partition_flags |=
1058 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001060 if (!strcmp(p->partitionContents.ident,
1061 PD_PARTITION_CONTENTS_NSR02) ||
1062 !strcmp(p->partitionContents.ident,
1063 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct partitionHeaderDesc *phd;
1065
Marcin Slusarz4b111112008-02-08 04:20:36 -08001066 phd = (struct partitionHeaderDesc *)
1067 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001068 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001069 kernel_lb_addr loc = {
1070 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1071 .partitionReferenceNum = i,
1072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001074 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001075 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001076 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001077 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001078 return 1;
1079 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001080 map->s_partition_flags |=
1081 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001082 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001083 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001085 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001086 struct udf_bitmap *bitmap =
1087 udf_sb_alloc_bitmap(sb, i);
1088 map->s_uspace.s_bitmap = bitmap;
1089 if (bitmap != NULL) {
1090 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001091 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001092 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001093 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001094 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001095 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001096 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098 }
1099 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001100 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001101 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102 kernel_lb_addr loc = {
1103 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1104 .partitionReferenceNum = i,
1105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001107 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001108 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001109 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001110 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001111 return 1;
1112 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001113 map->s_partition_flags |=
1114 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001115 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001116 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001118 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001119 struct udf_bitmap *bitmap =
1120 udf_sb_alloc_bitmap(sb, i);
1121 map->s_fspace.s_bitmap = bitmap;
1122 if (bitmap != NULL) {
1123 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001124 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001125 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001126 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001127 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001128 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001129 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 }
1132 }
1133 break;
1134 }
1135 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001136 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001137 udf_debug("Partition (%d) not found in partition map\n",
1138 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001139 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001140 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1141 "block length %d\n",
1142 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001143 map->s_partition_type,
1144 map->s_partition_root,
1145 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001149static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1150 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct logicalVolDesc *lvd;
1153 int i, j, offset;
1154 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001155 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001156 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 lvd = (struct logicalVolDesc *)bh->b_data;
1159
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001160 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1161 if (i != 0)
1162 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001164 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001165 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001166 i++, offset += gpm->partitionMapLength) {
1167 struct udf_part_map *map = &sbi->s_partmaps[i];
1168 gpm = (struct genericPartitionMap *)
1169 &(lvd->partitionMaps[offset]);
1170 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001171 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001172 struct genericPartitionMap1 *gpm1 =
1173 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001174 map->s_partition_type = UDF_TYPE1_MAP15;
1175 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1176 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1177 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001178 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001179 struct udfPartitionMap2 *upm2 =
1180 (struct udfPartitionMap2 *)gpm;
1181 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1182 strlen(UDF_ID_VIRTUAL))) {
1183 u16 suf =
1184 le16_to_cpu(((__le16 *)upm2->partIdent.
1185 identSuffix)[0]);
1186 if (suf == 0x0150) {
1187 map->s_partition_type =
1188 UDF_VIRTUAL_MAP15;
1189 map->s_partition_func =
1190 udf_get_pblock_virt15;
1191 } else if (suf == 0x0200) {
1192 map->s_partition_type =
1193 UDF_VIRTUAL_MAP20;
1194 map->s_partition_func =
1195 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001197 } else if (!strncmp(upm2->partIdent.ident,
1198 UDF_ID_SPARABLE,
1199 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 uint32_t loc;
1201 uint16_t ident;
1202 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001203 struct sparablePartitionMap *spm =
1204 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001206 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001207 map->s_type_specific.s_sparing.s_packet_len =
1208 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001209 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001210 struct buffer_head *bh2;
1211
1212 loc = le32_to_cpu(
1213 spm->locSparingTable[j]);
1214 bh2 = udf_read_tagged(sb, loc, loc,
1215 &ident);
1216 map->s_type_specific.s_sparing.
1217 s_spar_map[j] = bh2;
1218
1219 if (bh2 != NULL) {
1220 st = (struct sparingTable *)
1221 bh2->b_data;
1222 if (ident != 0 || strncmp(
1223 st->sparingIdent.ident,
1224 UDF_ID_SPARING,
1225 strlen(UDF_ID_SPARING))) {
1226 brelse(bh2);
1227 map->s_type_specific.
1228 s_sparing.
1229 s_spar_map[j] =
1230 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232 }
1233 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001234 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001235 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001236 udf_debug("Unknown ident: %s\n",
1237 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 continue;
1239 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001240 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1241 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001244 i, map->s_partition_num, type,
1245 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001248 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001249 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001252 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1253 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001254 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256 if (lvd->integritySeqExt.extLength)
1257 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260}
1261
1262/*
1263 * udf_load_logicalvolint
1264 *
1265 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001266static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct buffer_head *bh = NULL;
1269 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001270 struct udf_sb_info *sbi = UDF_SB(sb);
1271 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001274 (bh = udf_read_tagged(sb, loc.extLocation,
1275 loc.extLocation, &ident)) &&
1276 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001277 sbi->s_lvid_bh = bh;
1278 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001279
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001280 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001281 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001282 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001283
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001285 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001287 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001289 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001290 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293/*
1294 * udf_process_sequence
1295 *
1296 * PURPOSE
1297 * Process a main/reserve volume descriptor sequence.
1298 *
1299 * PRE-CONDITIONS
1300 * sb Pointer to _locked_ superblock.
1301 * block First block of first extent of the sequence.
1302 * lastblock Lastblock of first extent of the sequence.
1303 *
1304 * HISTORY
1305 * July 1, 1997 - Andrew E. Mileski
1306 * Written, tested, and released.
1307 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001308static int udf_process_sequence(struct super_block *sb, long block,
1309 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct buffer_head *bh = NULL;
1312 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001313 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 struct generic_desc *gd;
1315 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001316 int done = 0;
1317 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 uint32_t vdsn;
1319 uint16_t ident;
1320 long next_s = 0, next_e = 0;
1321
1322 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1323
1324 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001325 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001328 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
1330
1331 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1332 gd = (struct generic_desc *)bh->b_data;
1333 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001334 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001335 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001336 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1337 if (vdsn >= curr->volDescSeqNum) {
1338 curr->volDescSeqNum = vdsn;
1339 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001340 }
1341 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001342 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001343 curr = &vds[VDS_POS_VOL_DESC_PTR];
1344 if (vdsn >= curr->volDescSeqNum) {
1345 curr->volDescSeqNum = vdsn;
1346 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001348 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001349 next_s = le32_to_cpu(
1350 vdp->nextVolDescSeqExt.extLocation);
1351 next_e = le32_to_cpu(
1352 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001353 next_e = next_e >> sb->s_blocksize_bits;
1354 next_e += next_s;
1355 }
1356 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001357 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001358 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1359 if (vdsn >= curr->volDescSeqNum) {
1360 curr->volDescSeqNum = vdsn;
1361 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001362 }
1363 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001364 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001365 curr = &vds[VDS_POS_PARTITION_DESC];
1366 if (!curr->block)
1367 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001368 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001369 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001370 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1371 if (vdsn >= curr->volDescSeqNum) {
1372 curr->volDescSeqNum = vdsn;
1373 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001374 }
1375 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001376 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001377 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1378 if (vdsn >= curr->volDescSeqNum) {
1379 curr->volDescSeqNum = vdsn;
1380 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001381 }
1382 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001383 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001384 vds[VDS_POS_TERMINATING_DESC].block = block;
1385 if (next_e) {
1386 block = next_s;
1387 lastblock = next_e;
1388 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001389 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001390 done = 1;
1391 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001393 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001395 for (i = 0; i < VDS_POS_LENGTH; i++) {
1396 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001397 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1398 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001400 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001402 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001403 if (udf_load_logicalvol(sb, bh, fileset)) {
1404 brelse(bh);
1405 return 1;
1406 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001407 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001409 if (udf_load_partdesc(sb, bh)) {
1410 brelse(bh);
1411 return 1;
1412 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001413 for (j = vds[i].block + 1;
1414 j < vds[VDS_POS_TERMINATING_DESC].block;
1415 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 bh2 = udf_read_tagged(sb, j, j, &ident);
1417 gd = (struct generic_desc *)bh2->b_data;
1418 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001419 if (udf_load_partdesc(sb,
1420 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001421 brelse(bh);
1422 brelse(bh2);
1423 return 1;
1424 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001425 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001428 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 }
1431
1432 return 0;
1433}
1434
1435/*
1436 * udf_check_valid()
1437 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001438static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 long block;
1441
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001442 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 udf_debug("Validity check skipped because of novrs option\n");
1444 return 0;
1445 }
1446 /* Check that it is NSR02 compliant */
1447 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001448 else {
1449 block = udf_vrs(sb, silent);
1450 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001451 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001452 udf_debug("Failed to read byte 32768. Assuming open "
1453 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001454 if (!sbi->s_last_block)
1455 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001456 return 0;
1457 } else
1458 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001462static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct anchorVolDescPtr *anchor;
1465 uint16_t ident;
1466 struct buffer_head *bh;
1467 long main_s, main_e, reserve_s, reserve_e;
1468 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001469 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 if (!sb)
1472 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001473 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001475 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001476 if (!sbi->s_anchor[i])
1477 continue;
1478 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1479 &ident);
1480 if (!bh)
1481 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001483 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001484
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001485 /* Locate the main sequence */
1486 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1487 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1488 main_e = main_e >> sb->s_blocksize_bits;
1489 main_e += main_s;
1490
1491 /* Locate the reserve sequence */
1492 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001493 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001494 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001495 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001496 reserve_e = reserve_e >> sb->s_blocksize_bits;
1497 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001499 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001501 /* Process the main & reserve sequences */
1502 /* responsible for finding the PartitionDesc(s) */
1503 if (!(udf_process_sequence(sb, main_s, main_e,
1504 fileset) &&
1505 udf_process_sequence(sb, reserve_s, reserve_e,
1506 fileset)))
1507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001510 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 udf_debug("No Anchor block found\n");
1512 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001513 }
1514 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001516 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001517 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 struct udf_part_map *map = &sbi->s_partmaps[i];
1519 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001520 case UDF_VIRTUAL_MAP15:
1521 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001522 if (!sbi->s_last_block) {
1523 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001524 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001526
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001527 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001528 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001529 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 return 1;
1531 }
1532
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001533 for (j = 0; j < sbi->s_partitions; j++) {
1534 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001535 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001536 map->s_volumeseqnum ==
1537 map2->s_volumeseqnum &&
1538 map->s_partition_num ==
1539 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001540 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001541 ino.logicalBlockNum =
1542 sbi->s_last_block -
1543 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001544 break;
1545 }
1546 }
1547
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001548 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001549 return 1;
1550
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001551 sbi->s_vat_inode = udf_iget(sb, ino);
1552 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001553 return 1;
1554
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001555 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1556 map->s_type_specific.s_virtual.s_start_offset =
1557 udf_ext0_offset(sbi->s_vat_inode);
1558 map->s_type_specific.s_virtual.s_num_entries =
1559 (sbi->s_vat_inode->i_size - 36) >> 2;
1560 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001561 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001562 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001563
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001564 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001565 bh = sb_bread(sb, pos);
1566 if (!bh)
1567 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001568 vat20 = (struct virtualAllocationTable20 *)
1569 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001570 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001571 map->s_type_specific.s_virtual.s_start_offset =
1572 le16_to_cpu(vat20->lengthHeader) +
1573 udf_ext0_offset(sbi->s_vat_inode);
1574 map->s_type_specific.s_virtual.s_num_entries =
1575 (sbi->s_vat_inode->i_size -
1576 map->s_type_specific.s_virtual.
1577 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001578 brelse(bh);
1579 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001580 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1581 map->s_partition_len =
1582 sbi->s_partmaps[ino.partitionReferenceNum].
1583 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585 }
1586 return 0;
1587}
1588
1589static void udf_open_lvid(struct super_block *sb)
1590{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001591 struct udf_sb_info *sbi = UDF_SB(sb);
1592 struct buffer_head *bh = sbi->s_lvid_bh;
1593 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001595 struct logicalVolIntegrityDesc *lvid =
1596 (struct logicalVolIntegrityDesc *)bh->b_data;
1597 struct logicalVolIntegrityDescImpUse *lvidiu =
1598 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001600 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1601 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001603 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1604 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Marcin Slusarz4b111112008-02-08 04:20:36 -08001606 lvid->descTag.descCRC = cpu_to_le16(
1607 udf_crc((char *)lvid + sizeof(tag),
1608 le16_to_cpu(lvid->descTag.descCRCLength),
1609 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001611 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001612 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614}
1615
1616static void udf_close_lvid(struct super_block *sb)
1617{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001618 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001619 struct udf_sb_info *sbi = UDF_SB(sb);
1620 struct buffer_head *bh = sbi->s_lvid_bh;
1621 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001622
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001623 if (!bh)
1624 return;
1625
1626 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1627
1628 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001629 struct logicalVolIntegrityDescImpUse *lvidiu =
1630 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001631 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1632 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001634 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1635 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001636 lvidiu->maxUDFWriteRev =
1637 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001638 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1639 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1640 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1641 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1642 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Marcin Slusarz4b111112008-02-08 04:20:36 -08001644 lvid->descTag.descCRC = cpu_to_le16(
1645 udf_crc((char *)lvid + sizeof(tag),
1646 le16_to_cpu(lvid->descTag.descCRCLength),
1647 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001649 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001650 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652}
1653
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001654static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1655{
1656 int i;
1657 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001658 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1659 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001660
1661 for (i = 0; i < nr_groups; i++)
1662 if (bitmap->s_block_bitmap[i])
1663 brelse(bitmap->s_block_bitmap[i]);
1664
1665 if (size <= PAGE_SIZE)
1666 kfree(bitmap);
1667 else
1668 vfree(bitmap);
1669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static int udf_fill_super(struct super_block *sb, void *options, int silent)
1672{
1673 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001674 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct udf_options uopt;
1676 kernel_lb_addr rootdir, fileset;
1677 struct udf_sb_info *sbi;
1678
1679 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1680 uopt.uid = -1;
1681 uopt.gid = -1;
1682 uopt.umask = 0;
1683
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001684 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (!sbi)
1686 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001690 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Miklos Szeredi6da80892008-02-08 04:21:50 -08001692 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 goto error_out;
1694
1695 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001696 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001698 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 goto error_out;
1700 }
1701#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001702 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 uopt.nls_map = load_nls_default();
1704 if (!uopt.nls_map)
1705 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1706 else
1707 udf_debug("Using default NLS map\n");
1708 }
1709#endif
1710 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1711 uopt.flags |= (1 << UDF_FLAG_UTF8);
1712
1713 fileset.logicalBlockNum = 0xFFFFFFFF;
1714 fileset.partitionReferenceNum = 0xFFFF;
1715
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001716 sbi->s_flags = uopt.flags;
1717 sbi->s_uid = uopt.uid;
1718 sbi->s_gid = uopt.gid;
1719 sbi->s_umask = uopt.umask;
1720 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001723 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1724 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1725 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001729 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001730 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001732 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001734 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001736 sbi->s_last_block = uopt.lastblock;
1737 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1738 sbi->s_anchor[2] = uopt.anchor;
1739 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001741 if (udf_check_valid(sb, uopt.novrs, silent)) {
1742 /* read volume recognition sequences */
1743 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001744 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
1747 udf_find_anchor(sb);
1748
1749 /* Fill in the rest of the superblock */
1750 sb->s_op = &udf_sb_ops;
1751 sb->dq_op = NULL;
1752 sb->s_dirt = 0;
1753 sb->s_magic = UDF_SUPER_MAGIC;
1754 sb->s_time_gran = 1000;
1755
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001756 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001757 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto error_out;
1759 }
1760
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001761 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001763 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001764 struct logicalVolIntegrityDescImpUse *lvidiu =
1765 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001766 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1767 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001768 /* uint16_t maxUDFWriteRev =
1769 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001772 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1773 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001774 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001775 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001777 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001780 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1783 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1784 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1785 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1786 }
1787
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001788 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001789 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto error_out;
1791 }
1792
Marcin Slusarz4b111112008-02-08 04:20:36 -08001793 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1794 UDF_PART_FLAG_READ_ONLY) {
1795 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1796 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001797 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001798 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001799
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001800 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001801 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto error_out;
1803 }
1804
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001805 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001807 udf_time_to_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001808 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001809 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001810 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1811 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813 if (!(sb->s_flags & MS_RDONLY))
1814 udf_open_lvid(sb);
1815
1816 /* Assign the root inode */
1817 /* assign inodes by physical block number */
1818 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001819 inode = udf_iget(sb, rootdir);
1820 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001821 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1822 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001823 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto error_out;
1825 }
1826
1827 /* Allocate a dentry for the root inode */
1828 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001829 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001830 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 iput(inode);
1832 goto error_out;
1833 }
Jan Kara31170b62007-05-08 00:35:21 -07001834 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return 0;
1836
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001837error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001838 if (sbi->s_vat_inode)
1839 iput(sbi->s_vat_inode);
1840 if (sbi->s_partitions) {
1841 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1842 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1843 iput(map->s_uspace.s_table);
1844 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1845 iput(map->s_fspace.s_table);
1846 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001847 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001848 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001849 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001850 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001851 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001852 brelse(map->s_type_specific.s_sparing.
1853 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855#ifdef CONFIG_UDF_NLS
1856 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001857 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858#endif
1859 if (!(sb->s_flags & MS_RDONLY))
1860 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001861 brelse(sbi->s_lvid_bh);
1862
1863 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 kfree(sbi);
1865 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return -EINVAL;
1868}
1869
1870void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001871 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 va_list args;
1874
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001875 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* mark sb error */
1877 sb->s_dirt = 1;
1878 }
1879 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001880 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001882 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001883 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
1886void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001887 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 va_list args;
1890
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001891 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001892 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 va_end(args);
1894 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001895 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001898static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
1900 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001901 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001903 sbi = UDF_SB(sb);
1904 if (sbi->s_vat_inode)
1905 iput(sbi->s_vat_inode);
1906 if (sbi->s_partitions) {
1907 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1908 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1909 iput(map->s_uspace.s_table);
1910 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1911 iput(map->s_fspace.s_table);
1912 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001913 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001914 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001915 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001916 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001917 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001918 brelse(map->s_type_specific.s_sparing.
1919 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921#ifdef CONFIG_UDF_NLS
1922 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001923 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924#endif
1925 if (!(sb->s_flags & MS_RDONLY))
1926 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001927 brelse(sbi->s_lvid_bh);
1928 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 kfree(sb->s_fs_info);
1930 sb->s_fs_info = NULL;
1931}
1932
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001933static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
David Howells726c3342006-06-23 02:02:58 -07001935 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001936 struct udf_sb_info *sbi = UDF_SB(sb);
1937 struct logicalVolIntegrityDescImpUse *lvidiu;
1938
1939 if (sbi->s_lvid_bh != NULL)
1940 lvidiu = udf_sb_lvidiu(sbi);
1941 else
1942 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 buf->f_type = UDF_SUPER_MAGIC;
1945 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001946 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 buf->f_bfree = udf_count_free(sb);
1948 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001949 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1950 le32_to_cpu(lvidiu->numDirs)) : 0)
1951 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 buf->f_ffree = buf->f_bfree;
1953 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001954 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 return 0;
1957}
1958
1959static unsigned char udf_bitmap_lookup[16] = {
1960 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
1961};
1962
Marcin Slusarz4b111112008-02-08 04:20:36 -08001963static unsigned int udf_count_free_bitmap(struct super_block *sb,
1964 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct buffer_head *bh = NULL;
1967 unsigned int accum = 0;
1968 int index;
1969 int block = 0, newblock;
1970 kernel_lb_addr loc;
1971 uint32_t bytes;
1972 uint8_t value;
1973 uint8_t *ptr;
1974 uint16_t ident;
1975 struct spaceBitmapDesc *bm;
1976
1977 lock_kernel();
1978
1979 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001980 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 bh = udf_read_ptagged(sb, loc, 0, &ident);
1982
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001983 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 printk(KERN_ERR "udf: udf_count_free failed\n");
1985 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001986 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001987 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 printk(KERN_ERR "udf: udf_count_free failed\n");
1989 goto out;
1990 }
1991
1992 bm = (struct spaceBitmapDesc *)bh->b_data;
1993 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001994 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1995 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001997 while (bytes > 0) {
1998 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002000 accum += udf_bitmap_lookup[value & 0x0f];
2001 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 index++;
2003 bytes--;
2004 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002005 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002006 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 newblock = udf_get_lb_pblock(sb, loc, ++block);
2008 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002009 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 udf_debug("read failed\n");
2011 goto out;
2012 }
2013 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002014 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002017 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 unlock_kernel();
2021
2022 return accum;
2023}
2024
Marcin Slusarz4b111112008-02-08 04:20:36 -08002025static unsigned int udf_count_free_table(struct super_block *sb,
2026 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002029 uint32_t elen;
2030 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002032 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 lock_kernel();
2035
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002036 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002037 epos.offset = sizeof(struct unallocSpaceEntry);
2038 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002040 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002042
Jan Kara3bf25cb2007-05-08 00:35:16 -07002043 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 unlock_kernel();
2046
2047 return accum;
2048}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049
2050static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002053 struct udf_sb_info *sbi;
2054 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002056 sbi = UDF_SB(sb);
2057 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002058 struct logicalVolIntegrityDesc *lvid =
2059 (struct logicalVolIntegrityDesc *)
2060 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002061 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002062 accum = le32_to_cpu(
2063 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (accum == 0xFFFFFFFF)
2065 accum = 0;
2066 }
2067 }
2068
2069 if (accum)
2070 return accum;
2071
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 map = &sbi->s_partmaps[sbi->s_partition];
2073 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002074 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002075 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002077 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002078 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002079 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 if (accum)
2082 return accum;
2083
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002084 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002085 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002086 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002089 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002090 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
2092
2093 return accum;
2094}