blob: c5fef85a9c15241bd42169a0635a4baec0add1aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
87static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
89static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
91 kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static void udf_load_fileset(struct super_block *, struct buffer_head *,
93 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_open_lvid(struct super_block *);
95static void udf_close_lvid(struct super_block *);
96static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070097static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080098static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020099static void udf_error(struct super_block *sb, const char *function,
100 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800102struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
103{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800104 struct logicalVolIntegrityDesc *lvid =
105 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 __u32 offset = number_of_partitions * 2 *
108 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700113static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700114 int flags, const char *dev_name, void *data,
115 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David Howells454e2392006-06-23 02:02:57 -0700117 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700121 .owner = THIS_MODULE,
122 .name = "udf",
123 .get_sb = udf_get_sb,
124 .kill_sb = kill_block_super,
125 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130static struct inode *udf_alloc_inode(struct super_block *sb)
131{
132 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800133 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!ei)
135 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700136
137 ei->i_unique = 0;
138 ei->i_lenExtents = 0;
139 ei->i_next_alloc_block = 0;
140 ei->i_next_alloc_goal = 0;
141 ei->i_strat4096 = 0;
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return &ei->vfs_inode;
144}
145
146static void udf_destroy_inode(struct inode *inode)
147{
148 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
149}
150
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700151static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700153 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Christoph Lametera35afb82007-05-16 22:10:57 -0700155 ei->i_ext.i_data = NULL;
156 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static int init_inodecache(void)
160{
161 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
162 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 0, (SLAB_RECLAIM_ACCOUNT |
164 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900165 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700166 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return -ENOMEM;
168 return 0;
169}
170
171static void destroy_inodecache(void)
172{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700173 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800177static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700178 .alloc_inode = udf_alloc_inode,
179 .destroy_inode = udf_destroy_inode,
180 .write_inode = udf_write_inode,
181 .delete_inode = udf_delete_inode,
182 .clear_inode = udf_clear_inode,
183 .put_super = udf_put_super,
184 .write_super = udf_write_super,
185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800187 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned char novrs;
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
195 unsigned int anchor;
196 unsigned int volume;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
200 unsigned int flags;
201 mode_t umask;
202 gid_t gid;
203 uid_t uid;
204 struct nls_table *nls_map;
205};
206
207static int __init init_udf_fs(void)
208{
209 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 err = init_inodecache();
212 if (err)
213 goto out1;
214 err = register_filesystem(&udf_fstype);
215 if (err)
216 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219
220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700222
223out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return err;
225}
226
227static void __exit exit_udf_fs(void)
228{
229 unregister_filesystem(&udf_fstype);
230 destroy_inodecache();
231}
232
233module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700234module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800236static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
237{
238 struct udf_sb_info *sbi = UDF_SB(sb);
239
240 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
241 GFP_KERNEL);
242 if (!sbi->s_partmaps) {
243 udf_error(sb, __FUNCTION__,
244 "Unable to allocate space for %d partition maps",
245 count);
246 sbi->s_partitions = 0;
247 return -ENOMEM;
248 }
249
250 sbi->s_partitions = count;
251 return 0;
252}
253
Miklos Szeredi6da80892008-02-08 04:21:50 -0800254static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
255{
256 struct super_block *sb = mnt->mnt_sb;
257 struct udf_sb_info *sbi = UDF_SB(sb);
258
259 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
260 seq_puts(seq, ",nostrict");
261 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
262 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
263 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
264 seq_puts(seq, ",unhide");
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
266 seq_puts(seq, ",undelete");
267 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
268 seq_puts(seq, ",noadinicb");
269 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
270 seq_puts(seq, ",shortad");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
272 seq_puts(seq, ",uid=forget");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
274 seq_puts(seq, ",uid=ignore");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
276 seq_puts(seq, ",gid=forget");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
278 seq_puts(seq, ",gid=ignore");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
280 seq_printf(seq, ",uid=%u", sbi->s_uid);
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
282 seq_printf(seq, ",gid=%u", sbi->s_gid);
283 if (sbi->s_umask != 0)
284 seq_printf(seq, ",umask=%o", sbi->s_umask);
285 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
286 seq_printf(seq, ",session=%u", sbi->s_session);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
288 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
289 /*
290 * s_anchor[2] could be zeroed out in case there is no anchor
291 * in the specified block, but then the "anchor=N" option
292 * originally given by the user wasn't effective, so it's OK
293 * if we don't show it.
294 */
295 if (sbi->s_anchor[2] != 0)
296 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
297 /*
298 * volume, partition, fileset and rootdir seem to be ignored
299 * currently
300 */
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
302 seq_puts(seq, ",utf8");
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
304 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
305
306 return 0;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * udf_parse_options
311 *
312 * PURPOSE
313 * Parse mount options.
314 *
315 * DESCRIPTION
316 * The following mount options are supported:
317 *
318 * gid= Set the default group.
319 * umask= Set the default umask.
320 * uid= Set the default user.
321 * bs= Set the block size.
322 * unhide Show otherwise hidden files.
323 * undelete Show deleted files in lists.
324 * adinicb Embed data in the inode (default)
325 * noadinicb Don't embed data in the inode
326 * shortad Use short ad's
327 * longad Use long ad's (default)
328 * nostrict Unset strict conformance
329 * iocharset= Set the NLS character set
330 *
331 * The remaining are for debugging and disaster recovery:
332 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700333 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
335 * The following expect a offset from 0.
336 *
337 * session= Set the CDROM session (default= last session)
338 * anchor= Override standard anchor location. (default= 256)
339 * volume= Override the VolumeDesc location. (unused)
340 * partition= Override the PartitionDesc location. (unused)
341 * lastblock= Set the last block of the filesystem/
342 *
343 * The following expect a offset from the partition root.
344 *
345 * fileset= Override the fileset block location. (unused)
346 * rootdir= Override the root directory location. (unused)
347 * WARNING: overriding the rootdir to a non-directory may
348 * yield highly unpredictable results.
349 *
350 * PRE-CONDITIONS
351 * options Pointer to mount options string.
352 * uopts Pointer to mount options variable.
353 *
354 * POST-CONDITIONS
355 * <return> 1 Mount options parsed okay.
356 * <return> 0 Error parsing mount options.
357 *
358 * HISTORY
359 * July 1, 1997 - Andrew E. Mileski
360 * Written, tested, and released.
361 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363enum {
364 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
365 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
366 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
367 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
368 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800369 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
372static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700373 {Opt_novrs, "novrs"},
374 {Opt_nostrict, "nostrict"},
375 {Opt_bs, "bs=%u"},
376 {Opt_unhide, "unhide"},
377 {Opt_undelete, "undelete"},
378 {Opt_noadinicb, "noadinicb"},
379 {Opt_adinicb, "adinicb"},
380 {Opt_shortad, "shortad"},
381 {Opt_longad, "longad"},
382 {Opt_uforget, "uid=forget"},
383 {Opt_uignore, "uid=ignore"},
384 {Opt_gforget, "gid=forget"},
385 {Opt_gignore, "gid=ignore"},
386 {Opt_gid, "gid=%u"},
387 {Opt_uid, "uid=%u"},
388 {Opt_umask, "umask=%o"},
389 {Opt_session, "session=%u"},
390 {Opt_lastblock, "lastblock=%u"},
391 {Opt_anchor, "anchor=%u"},
392 {Opt_volume, "volume=%u"},
393 {Opt_partition, "partition=%u"},
394 {Opt_fileset, "fileset=%u"},
395 {Opt_rootdir, "rootdir=%u"},
396 {Opt_utf8, "utf8"},
397 {Opt_iocharset, "iocharset=%s"},
398 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399};
400
Miklos Szeredi6da80892008-02-08 04:21:50 -0800401static int udf_parse_options(char *options, struct udf_options *uopt,
402 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 char *p;
405 int option;
406
407 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800408 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 uopt->partition = 0xFFFF;
410 uopt->session = 0xFFFFFFFF;
411 uopt->lastblock = 0;
412 uopt->anchor = 0;
413 uopt->volume = 0xFFFFFFFF;
414 uopt->rootdir = 0xFFFFFFFF;
415 uopt->fileset = 0xFFFFFFFF;
416 uopt->nls_map = NULL;
417
418 if (!options)
419 return 1;
420
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700421 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 substring_t args[MAX_OPT_ARGS];
423 int token;
424 if (!*p)
425 continue;
426
427 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700428 switch (token) {
429 case Opt_novrs:
430 uopt->novrs = 1;
431 case Opt_bs:
432 if (match_int(&args[0], &option))
433 return 0;
434 uopt->blocksize = option;
435 break;
436 case Opt_unhide:
437 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
438 break;
439 case Opt_undelete:
440 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
441 break;
442 case Opt_noadinicb:
443 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
444 break;
445 case Opt_adinicb:
446 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
447 break;
448 case Opt_shortad:
449 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
450 break;
451 case Opt_longad:
452 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
453 break;
454 case Opt_gid:
455 if (match_int(args, &option))
456 return 0;
457 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700458 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700459 break;
460 case Opt_uid:
461 if (match_int(args, &option))
462 return 0;
463 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700464 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 break;
466 case Opt_umask:
467 if (match_octal(args, &option))
468 return 0;
469 uopt->umask = option;
470 break;
471 case Opt_nostrict:
472 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
473 break;
474 case Opt_session:
475 if (match_int(args, &option))
476 return 0;
477 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800478 if (!remount)
479 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700480 break;
481 case Opt_lastblock:
482 if (match_int(args, &option))
483 return 0;
484 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800485 if (!remount)
486 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700487 break;
488 case Opt_anchor:
489 if (match_int(args, &option))
490 return 0;
491 uopt->anchor = option;
492 break;
493 case Opt_volume:
494 if (match_int(args, &option))
495 return 0;
496 uopt->volume = option;
497 break;
498 case Opt_partition:
499 if (match_int(args, &option))
500 return 0;
501 uopt->partition = option;
502 break;
503 case Opt_fileset:
504 if (match_int(args, &option))
505 return 0;
506 uopt->fileset = option;
507 break;
508 case Opt_rootdir:
509 if (match_int(args, &option))
510 return 0;
511 uopt->rootdir = option;
512 break;
513 case Opt_utf8:
514 uopt->flags |= (1 << UDF_FLAG_UTF8);
515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700517 case Opt_iocharset:
518 uopt->nls_map = load_nls(args[0].from);
519 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
520 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700522 case Opt_uignore:
523 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
524 break;
525 case Opt_uforget:
526 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
527 break;
528 case Opt_gignore:
529 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
530 break;
531 case Opt_gforget:
532 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
533 break;
534 default:
535 printk(KERN_ERR "udf: bad mount option \"%s\" "
536 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return 0;
538 }
539 }
540 return 1;
541}
542
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800543static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!(sb->s_flags & MS_RDONLY))
548 udf_open_lvid(sb);
549 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 unlock_kernel();
552}
553
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700554static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800557 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800559 uopt.flags = sbi->s_flags;
560 uopt.uid = sbi->s_uid;
561 uopt.gid = sbi->s_gid;
562 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Miklos Szeredi6da80892008-02-08 04:21:50 -0800564 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EINVAL;
566
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800567 sbi->s_flags = uopt.flags;
568 sbi->s_uid = uopt.uid;
569 sbi->s_gid = uopt.gid;
570 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800572 if (sbi->s_lvid_bh) {
573 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (write_rev > UDF_MAX_WRITE_VERSION)
575 *flags |= MS_RDONLY;
576 }
577
578 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
579 return 0;
580 if (*flags & MS_RDONLY)
581 udf_close_lvid(sb);
582 else
583 udf_open_lvid(sb);
584
585 return 0;
586}
587
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700588static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct volStructDesc *vsd = NULL;
591 int sector = 32768;
592 int sectorsize;
593 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700594 int iso9660 = 0;
595 int nsr02 = 0;
596 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800597 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* Block size must be a multiple of 512 */
600 if (sb->s_blocksize & 511)
601 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800602 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 if (sb->s_blocksize < sizeof(struct volStructDesc))
605 sectorsize = sizeof(struct volStructDesc);
606 else
607 sectorsize = sb->s_blocksize;
608
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800609 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700612 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Read a block */
616 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
617 if (!bh)
618 break;
619
620 /* Look for ISO descriptors */
621 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800622 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700624 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700625 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800627 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
628 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700630 switch (vsd->structType) {
631 case 0:
632 udf_debug("ISO9660 Boot Record found\n");
633 break;
634 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800635 udf_debug("ISO9660 Primary Volume Descriptor "
636 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700637 break;
638 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800639 udf_debug("ISO9660 Supplementary Volume "
640 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 break;
642 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800643 udf_debug("ISO9660 Volume Partition Descriptor "
644 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645 break;
646 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800647 udf_debug("ISO9660 Volume Descriptor Set "
648 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 break;
650 default:
651 udf_debug("ISO9660 VRS (%u) found\n",
652 vsd->structType);
653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800655 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
656 VSD_STD_ID_LEN))
657 ; /* nothing */
658 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
659 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700660 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800662 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
663 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800665 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
666 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700668 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 if (nsr03)
672 return nsr03;
673 else if (nsr02)
674 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800675 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -1;
677 else
678 return 0;
679}
680
681/*
682 * udf_find_anchor
683 *
684 * PURPOSE
685 * Find an anchor volume descriptor.
686 *
687 * PRE-CONDITIONS
688 * sb Pointer to _locked_ superblock.
689 * lastblock Last block on media.
690 *
691 * POST-CONDITIONS
692 * <return> 1 if not found, 0 if ok
693 *
694 * HISTORY
695 * July 1, 1997 - Andrew E. Mileski
696 * Written, tested, and released.
697 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700698static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800700 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct buffer_head *bh = NULL;
702 uint16_t ident;
703 uint32_t location;
704 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800705 struct udf_sb_info *sbi;
706
707 sbi = UDF_SB(sb);
708 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700710 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700712 int last[] = { lastblock, lastblock - 2,
713 lastblock - 150, lastblock - 152,
714 varlastblock, varlastblock - 2,
715 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 lastblock = 0;
718
719 /* Search for an anchor volume descriptor pointer */
720
721 /* according to spec, anchor is in either:
722 * block 256
723 * lastblock-256
724 * lastblock
725 * however, if the disc isn't closed, it could be 512 */
726
Tobias Klausere8c96f82006-03-24 03:15:34 -0800727 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800728 ident = location = 0;
729 if (last[i] >= 0) {
730 bh = sb_bread(sb, last[i]);
731 if (bh) {
732 tag *t = (tag *)bh->b_data;
733 ident = le16_to_cpu(t->tagIdent);
734 location = le32_to_cpu(t->tagLocation);
735 brelse(bh);
736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800738
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800740 if (location == last[i] - sbi->s_session) {
741 lastblock = last[i] - sbi->s_session;
742 sbi->s_anchor[0] = lastblock;
743 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800744 } else if (location ==
745 udf_variable_to_fixed(last[i]) -
746 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800748 lastblock =
749 udf_variable_to_fixed(last[i]) -
750 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800751 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 sbi->s_anchor[1] = lastblock - 256 -
753 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700754 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800755 udf_debug("Anchor found at block %d, "
756 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700757 last[i], location);
758 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 } else if (ident == TAG_IDENT_FE ||
760 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800762 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800764 ident = location = 0;
765 if (last[i] >= 256) {
766 bh = sb_bread(sb, last[i] - 256);
767 if (bh) {
768 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800769 ident = le16_to_cpu(
770 t->tagIdent);
771 location = le32_to_cpu(
772 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800773 brelse(bh);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800778 location == last[i] - 256 -
779 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800781 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700782 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800783 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800784 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800785 bh = sb_bread(sb,
786 last[i] - 312 -
787 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800788 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800789 tag *t = (tag *)
790 bh->b_data;
791 ident = le16_to_cpu(
792 t->tagIdent);
793 location = le32_to_cpu(
794 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800795 brelse(bh);
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700800 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800801 UDF_SET_FLAG(sb,
802 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700803 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800804 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 }
807 }
808 }
809 }
810
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700811 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800812 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800813 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800814 if (bh) {
815 tag *t = (tag *)bh->b_data;
816 ident = le16_to_cpu(t->tagIdent);
817 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700818 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (ident == TAG_IDENT_AVDP && location == 256)
821 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
822 }
823 }
824
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800825 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100826 if (!sbi->s_anchor[i])
827 continue;
828 bh = udf_read_tagged(sb, sbi->s_anchor[i],
829 sbi->s_anchor[i], &ident);
830 if (!bh)
831 sbi->s_anchor[i] = 0;
832 else {
833 brelse(bh);
834 if ((ident != TAG_IDENT_AVDP) &&
835 (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
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800841 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800844static int udf_find_fileset(struct super_block *sb,
845 kernel_lb_addr *fileset,
846 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct buffer_head *bh = NULL;
849 long lastblock;
850 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800851 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700854 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
856
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700857 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700859 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700860 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 1;
862 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800866 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800867 if (!bh) {
868 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 kernel_lb_addr newfileset;
870
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700871/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700873
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800874 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875 (newfileset.partitionReferenceNum != 0xFFFF &&
876 fileset->logicalBlockNum == 0xFFFFFFFF &&
877 fileset->partitionReferenceNum == 0xFFFF);
878 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800879 lastblock = sbi->s_partmaps
880 [newfileset.partitionReferenceNum]
881 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 newfileset.logicalBlockNum = 0;
883
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700884 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800885 bh = udf_read_ptagged(sb, newfileset, 0,
886 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700887 if (!bh) {
888 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 continue;
890 }
891
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700892 switch (ident) {
893 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700894 {
895 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800896 sp = (struct spaceBitmapDesc *)
897 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700898 newfileset.logicalBlockNum += 1 +
899 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800900 sizeof(struct spaceBitmapDesc)
901 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700902 brelse(bh);
903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700905 case TAG_IDENT_FSD:
906 *fileset = newfileset;
907 break;
908 default:
909 newfileset.logicalBlockNum++;
910 brelse(bh);
911 bh = NULL;
912 break;
913 }
914 } while (newfileset.logicalBlockNum < lastblock &&
915 fileset->logicalBlockNum == 0xFFFFFFFF &&
916 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919
920 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700921 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700923 fileset->logicalBlockNum,
924 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800926 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700928 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return 0;
930 }
931 return 1;
932}
933
Jan Karac0eb31e2008-04-01 16:50:35 +0200934static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct primaryVolDesc *pvoldesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct ustr instr;
938 struct ustr outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200939 struct buffer_head *bh;
940 uint16_t ident;
941
942 bh = udf_read_tagged(sb, block, block, &ident);
943 if (!bh)
944 return 1;
945 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 pvoldesc = (struct primaryVolDesc *)bh->b_data;
948
Marcin Slusarz56774802008-02-10 11:25:31 +0100949 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
950 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100951#ifdef UDFFS_DEBUG
952 timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100953 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800954 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100955 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
956 ts->minute, le16_to_cpu(ts->typeAndTimezone));
957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
Marcin Slusarz4b111112008-02-08 04:20:36 -0800960 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700961 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800962 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800964 udf_debug("volIdent[] = '%s'\n",
965 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Marcin Slusarz4b111112008-02-08 04:20:36 -0800968 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (udf_CS0toUTF8(&outstr, &instr))
970 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200971
972 brelse(bh);
973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700976static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
977 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct fileSetDesc *fset;
980
981 fset = (struct fileSetDesc *)bh->b_data;
982
983 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
984
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800985 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700987 udf_debug("Rootdir at block=%d, partition=%d\n",
988 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800991int udf_compute_nr_groups(struct super_block *sb, u32 partition)
992{
993 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100994 return DIV_ROUND_UP(map->s_partition_len +
995 (sizeof(struct spaceBitmapDesc) << 3),
996 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800997}
998
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800999static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1000{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001001 struct udf_bitmap *bitmap;
1002 int nr_groups;
1003 int size;
1004
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001005 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001006 size = sizeof(struct udf_bitmap) +
1007 (sizeof(struct buffer_head *) * nr_groups);
1008
1009 if (size <= PAGE_SIZE)
1010 bitmap = kmalloc(size, GFP_KERNEL);
1011 else
1012 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1013
1014 if (bitmap == NULL) {
1015 udf_error(sb, __FUNCTION__,
1016 "Unable to allocate space for bitmap "
1017 "and %d buffer_head pointers", nr_groups);
1018 return NULL;
1019 }
1020
1021 memset(bitmap, 0x00, size);
1022 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1023 bitmap->s_nr_groups = nr_groups;
1024 return bitmap;
1025}
1026
Jan Kara3fb38df2008-04-02 12:26:36 +02001027static int udf_fill_partdesc_info(struct super_block *sb,
1028 struct partitionDesc *p, int p_index)
1029{
1030 struct udf_part_map *map;
1031 struct udf_sb_info *sbi = UDF_SB(sb);
1032 struct partitionHeaderDesc *phd;
1033
1034 map = &sbi->s_partmaps[p_index];
1035
1036 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1037 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1038
1039 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1040 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1041 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1042 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1043 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1044 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1045 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1046 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1047
1048 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1049 "block length %d\n", partitionNumber, p_index,
1050 map->s_partition_type, map->s_partition_root,
1051 map->s_partition_len);
1052
1053 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1054 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1055 return 0;
1056
1057 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1058 if (phd->unallocSpaceTable.extLength) {
1059 kernel_lb_addr loc = {
1060 .logicalBlockNum = le32_to_cpu(
1061 phd->unallocSpaceTable.extPosition),
1062 .partitionReferenceNum = p_index,
1063 };
1064
1065 map->s_uspace.s_table = udf_iget(sb, loc);
1066 if (!map->s_uspace.s_table) {
1067 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1068 p_index);
1069 return 1;
1070 }
1071 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1072 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1073 p_index, map->s_uspace.s_table->i_ino);
1074 }
1075
1076 if (phd->unallocSpaceBitmap.extLength) {
1077 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1078 if (!bitmap)
1079 return 1;
1080 map->s_uspace.s_bitmap = bitmap;
1081 bitmap->s_extLength = le32_to_cpu(
1082 phd->unallocSpaceBitmap.extLength);
1083 bitmap->s_extPosition = le32_to_cpu(
1084 phd->unallocSpaceBitmap.extPosition);
1085 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1086 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1087 bitmap->s_extPosition);
1088 }
1089
1090 if (phd->partitionIntegrityTable.extLength)
1091 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1092
1093 if (phd->freedSpaceTable.extLength) {
1094 kernel_lb_addr loc = {
1095 .logicalBlockNum = le32_to_cpu(
1096 phd->freedSpaceTable.extPosition),
1097 .partitionReferenceNum = p_index,
1098 };
1099
1100 map->s_fspace.s_table = udf_iget(sb, loc);
1101 if (!map->s_fspace.s_table) {
1102 udf_debug("cannot load freedSpaceTable (part %d)\n",
1103 p_index);
1104 return 1;
1105 }
1106
1107 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1108 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1109 p_index, map->s_fspace.s_table->i_ino);
1110 }
1111
1112 if (phd->freedSpaceBitmap.extLength) {
1113 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1114 if (!bitmap)
1115 return 1;
1116 map->s_fspace.s_bitmap = bitmap;
1117 bitmap->s_extLength = le32_to_cpu(
1118 phd->freedSpaceBitmap.extLength);
1119 bitmap->s_extPosition = le32_to_cpu(
1120 phd->freedSpaceBitmap.extPosition);
1121 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1122 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1123 bitmap->s_extPosition);
1124 }
1125 return 0;
1126}
1127
Jan Karac0eb31e2008-04-01 16:50:35 +02001128static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Jan Karac0eb31e2008-04-01 16:50:35 +02001130 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001131 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001132 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001133 struct udf_sb_info *sbi = UDF_SB(sb);
1134 bool found = false;
1135 int i;
Jan Karac0eb31e2008-04-01 16:50:35 +02001136 uint16_t partitionNumber;
1137 uint16_t ident;
1138 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Jan Karac0eb31e2008-04-01 16:50:35 +02001140 bh = udf_read_tagged(sb, block, block, &ident);
1141 if (!bh)
1142 return 1;
1143 if (ident != TAG_IDENT_PD)
1144 goto out_bh;
1145
1146 p = (struct partitionDesc *)bh->b_data;
1147 partitionNumber = le16_to_cpu(p->partitionNumber);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001148 for (i = 0; i < sbi->s_partitions; i++) {
1149 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001150 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001151 map->s_partition_num, partitionNumber);
1152 found = map->s_partition_num == partitionNumber;
1153 if (found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001155 }
1156
1157 if (!found) {
1158 udf_debug("Partition (%d) not found in partition map\n",
1159 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001160 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001161 }
1162
Jan Kara3fb38df2008-04-02 12:26:36 +02001163 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karac0eb31e2008-04-01 16:50:35 +02001164out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001165 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001166 brelse(bh);
1167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Jan Karac0eb31e2008-04-01 16:50:35 +02001170static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001171 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct logicalVolDesc *lvd;
1174 int i, j, offset;
1175 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001176 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001177 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001178 uint16_t ident;
1179 struct buffer_head *bh;
1180 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Jan Karac0eb31e2008-04-01 16:50:35 +02001182 bh = udf_read_tagged(sb, block, block, &ident);
1183 if (!bh)
1184 return 1;
1185 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 lvd = (struct logicalVolDesc *)bh->b_data;
1187
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001188 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001189 if (i != 0) {
1190 ret = i;
1191 goto out_bh;
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001194 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001195 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001196 i++, offset += gpm->partitionMapLength) {
1197 struct udf_part_map *map = &sbi->s_partmaps[i];
1198 gpm = (struct genericPartitionMap *)
1199 &(lvd->partitionMaps[offset]);
1200 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001201 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001202 struct genericPartitionMap1 *gpm1 =
1203 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001204 map->s_partition_type = UDF_TYPE1_MAP15;
1205 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1206 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1207 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001208 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001209 struct udfPartitionMap2 *upm2 =
1210 (struct udfPartitionMap2 *)gpm;
1211 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1212 strlen(UDF_ID_VIRTUAL))) {
1213 u16 suf =
1214 le16_to_cpu(((__le16 *)upm2->partIdent.
1215 identSuffix)[0]);
1216 if (suf == 0x0150) {
1217 map->s_partition_type =
1218 UDF_VIRTUAL_MAP15;
1219 map->s_partition_func =
1220 udf_get_pblock_virt15;
1221 } else if (suf == 0x0200) {
1222 map->s_partition_type =
1223 UDF_VIRTUAL_MAP20;
1224 map->s_partition_func =
1225 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001227 } else if (!strncmp(upm2->partIdent.ident,
1228 UDF_ID_SPARABLE,
1229 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001232 struct sparablePartitionMap *spm =
1233 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001235 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001236 map->s_type_specific.s_sparing.s_packet_len =
1237 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001238 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001239 struct buffer_head *bh2;
1240
1241 loc = le32_to_cpu(
1242 spm->locSparingTable[j]);
1243 bh2 = udf_read_tagged(sb, loc, loc,
1244 &ident);
1245 map->s_type_specific.s_sparing.
1246 s_spar_map[j] = bh2;
1247
Marcin Slusarz165923f2008-02-10 11:33:08 +01001248 if (bh2 == NULL)
1249 continue;
1250
1251 st = (struct sparingTable *)bh2->b_data;
1252 if (ident != 0 || strncmp(
1253 st->sparingIdent.ident,
1254 UDF_ID_SPARING,
1255 strlen(UDF_ID_SPARING))) {
1256 brelse(bh2);
1257 map->s_type_specific.s_sparing.
1258 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001261 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001262 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001263 udf_debug("Unknown ident: %s\n",
1264 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 continue;
1266 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001267 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1268 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001271 i, map->s_partition_num, type,
1272 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001275 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001276 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001279 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1280 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001281 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283 if (lvd->integritySeqExt.extLength)
1284 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001285
Jan Karac0eb31e2008-04-01 16:50:35 +02001286out_bh:
1287 brelse(bh);
1288 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
1291/*
1292 * udf_load_logicalvolint
1293 *
1294 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001295static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 struct buffer_head *bh = NULL;
1298 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001299 struct udf_sb_info *sbi = UDF_SB(sb);
1300 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001303 (bh = udf_read_tagged(sb, loc.extLocation,
1304 loc.extLocation, &ident)) &&
1305 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001306 sbi->s_lvid_bh = bh;
1307 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001308
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001309 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001310 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001311 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001312
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001313 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001314 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001316 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001318 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001319 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322/*
1323 * udf_process_sequence
1324 *
1325 * PURPOSE
1326 * Process a main/reserve volume descriptor sequence.
1327 *
1328 * PRE-CONDITIONS
1329 * sb Pointer to _locked_ superblock.
1330 * block First block of first extent of the sequence.
1331 * lastblock Lastblock of first extent of the sequence.
1332 *
1333 * HISTORY
1334 * July 1, 1997 - Andrew E. Mileski
1335 * Written, tested, and released.
1336 */
Jan Kara200a3592008-03-04 13:03:09 +01001337static noinline int udf_process_sequence(struct super_block *sb, long block,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001338 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct buffer_head *bh = NULL;
1341 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001342 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 struct generic_desc *gd;
1344 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001345 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 uint32_t vdsn;
1347 uint16_t ident;
1348 long next_s = 0, next_e = 0;
1349
1350 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1351
Jan Karac0eb31e2008-04-01 16:50:35 +02001352 /*
1353 * Read the main descriptor sequence and find which descriptors
1354 * are in it.
1355 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001356 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001359 if (!bh) {
1360 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1361 "sequence is corrupted or we could not read "
1362 "it.\n", (unsigned long long)block);
1363 return 1;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1367 gd = (struct generic_desc *)bh->b_data;
1368 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001369 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001370 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001371 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1372 if (vdsn >= curr->volDescSeqNum) {
1373 curr->volDescSeqNum = vdsn;
1374 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001375 }
1376 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001377 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001378 curr = &vds[VDS_POS_VOL_DESC_PTR];
1379 if (vdsn >= curr->volDescSeqNum) {
1380 curr->volDescSeqNum = vdsn;
1381 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001383 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001384 next_s = le32_to_cpu(
1385 vdp->nextVolDescSeqExt.extLocation);
1386 next_e = le32_to_cpu(
1387 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001388 next_e = next_e >> sb->s_blocksize_bits;
1389 next_e += next_s;
1390 }
1391 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001392 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001393 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1394 if (vdsn >= curr->volDescSeqNum) {
1395 curr->volDescSeqNum = vdsn;
1396 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001397 }
1398 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001399 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001400 curr = &vds[VDS_POS_PARTITION_DESC];
1401 if (!curr->block)
1402 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001403 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001405 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1406 if (vdsn >= curr->volDescSeqNum) {
1407 curr->volDescSeqNum = vdsn;
1408 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001409 }
1410 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001411 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001412 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1413 if (vdsn >= curr->volDescSeqNum) {
1414 curr->volDescSeqNum = vdsn;
1415 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001416 }
1417 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001418 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001419 vds[VDS_POS_TERMINATING_DESC].block = block;
1420 if (next_e) {
1421 block = next_s;
1422 lastblock = next_e;
1423 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001424 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001425 done = 1;
1426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001428 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001430 /*
1431 * Now read interesting descriptors again and process them
1432 * in a suitable order
1433 */
1434 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1435 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1436 return 1;
1437 }
1438 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1439 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jan Karac0eb31e2008-04-01 16:50:35 +02001441 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1442 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1443 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001444
Jan Karac0eb31e2008-04-01 16:50:35 +02001445 if (vds[VDS_POS_PARTITION_DESC].block) {
1446 /*
1447 * We rescan the whole descriptor sequence to find
1448 * partition descriptor blocks and process them.
1449 */
1450 for (block = vds[VDS_POS_PARTITION_DESC].block;
1451 block < vds[VDS_POS_TERMINATING_DESC].block;
1452 block++)
1453 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001454 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
1457 return 0;
1458}
1459
1460/*
1461 * udf_check_valid()
1462 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001463static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001466 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001468 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 udf_debug("Validity check skipped because of novrs option\n");
1470 return 0;
1471 }
1472 /* Check that it is NSR02 compliant */
1473 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001474 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001475 if (block == -1)
1476 udf_debug("Failed to read byte 32768. Assuming open "
1477 "disc. Skipping validity check\n");
1478 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001479 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001480 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001483static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct anchorVolDescPtr *anchor;
1486 uint16_t ident;
1487 struct buffer_head *bh;
1488 long main_s, main_e, reserve_s, reserve_e;
1489 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001490 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (!sb)
1493 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001494 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001496 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001497 if (!sbi->s_anchor[i])
1498 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001499
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001500 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1501 &ident);
1502 if (!bh)
1503 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001505 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001506
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001507 /* Locate the main sequence */
1508 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1509 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1510 main_e = main_e >> sb->s_blocksize_bits;
1511 main_e += main_s;
1512
1513 /* Locate the reserve sequence */
1514 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001515 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001516 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001517 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001518 reserve_e = reserve_e >> sb->s_blocksize_bits;
1519 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001521 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001523 /* Process the main & reserve sequences */
1524 /* responsible for finding the PartitionDesc(s) */
1525 if (!(udf_process_sequence(sb, main_s, main_e,
1526 fileset) &&
1527 udf_process_sequence(sb, reserve_s, reserve_e,
1528 fileset)))
1529 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001532 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 udf_debug("No Anchor block found\n");
1534 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001535 }
1536 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001538 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001539 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001540 struct udf_part_map *map = &sbi->s_partmaps[i];
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001541
Marcin Slusarz165923f2008-02-10 11:33:08 +01001542 if (map->s_partition_type != UDF_VIRTUAL_MAP15 &&
1543 map->s_partition_type != UDF_VIRTUAL_MAP20)
1544 continue;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001545
Marcin Slusarz165923f2008-02-10 11:33:08 +01001546 if (!sbi->s_last_block) {
1547 sbi->s_last_block = udf_get_last_block(sb);
1548 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Marcin Slusarz165923f2008-02-10 11:33:08 +01001550
1551 if (!sbi->s_last_block) {
1552 udf_debug("Unable to determine Lastblock (For "
1553 "Virtual Partition)\n");
1554 return 1;
1555 }
1556
1557 for (j = 0; j < sbi->s_partitions; j++) {
1558 struct udf_part_map *map2 = &sbi->s_partmaps[j];
1559 if (j != i &&
1560 map->s_volumeseqnum ==
1561 map2->s_volumeseqnum &&
1562 map->s_partition_num ==
1563 map2->s_partition_num) {
1564 ino.partitionReferenceNum = j;
1565 ino.logicalBlockNum =
1566 sbi->s_last_block -
1567 map2->s_partition_root;
1568 break;
1569 }
1570 }
1571
1572 if (j == sbi->s_partitions)
1573 return 1;
1574
1575 sbi->s_vat_inode = udf_iget(sb, ino);
1576 if (!sbi->s_vat_inode)
1577 return 1;
1578
1579 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1580 map->s_type_specific.s_virtual.s_start_offset =
1581 udf_ext0_offset(sbi->s_vat_inode);
1582 map->s_type_specific.s_virtual.s_num_entries =
1583 (sbi->s_vat_inode->i_size - 36) >> 2;
1584 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1585 uint32_t pos;
1586 struct virtualAllocationTable20 *vat20;
1587
1588 pos = udf_block_map(sbi->s_vat_inode, 0);
1589 bh = sb_bread(sb, pos);
1590 if (!bh)
1591 return 1;
1592 vat20 = (struct virtualAllocationTable20 *)
1593 bh->b_data +
1594 udf_ext0_offset(sbi->s_vat_inode);
1595 map->s_type_specific.s_virtual.s_start_offset =
1596 le16_to_cpu(vat20->lengthHeader) +
1597 udf_ext0_offset(sbi->s_vat_inode);
1598 map->s_type_specific.s_virtual.s_num_entries =
1599 (sbi->s_vat_inode->i_size -
1600 map->s_type_specific.s_virtual.
1601 s_start_offset) >> 2;
1602 brelse(bh);
1603 }
1604 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1605 map->s_partition_len =
1606 sbi->s_partmaps[ino.partitionReferenceNum].
1607 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
1609 return 0;
1610}
1611
1612static void udf_open_lvid(struct super_block *sb)
1613{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001614 struct udf_sb_info *sbi = UDF_SB(sb);
1615 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001616 struct logicalVolIntegrityDesc *lvid;
1617 struct logicalVolIntegrityDescImpUse *lvidiu;
1618 if (!bh)
1619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Marcin Slusarz165923f2008-02-10 11:33:08 +01001621 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1622 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Marcin Slusarz165923f2008-02-10 11:33:08 +01001624 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1625 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1626 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1627 CURRENT_TIME);
1628 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Marcin Slusarz165923f2008-02-10 11:33:08 +01001630 lvid->descTag.descCRC = cpu_to_le16(
1631 udf_crc((char *)lvid + sizeof(tag),
1632 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1633
1634 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1635 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638static void udf_close_lvid(struct super_block *sb)
1639{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001640 struct udf_sb_info *sbi = UDF_SB(sb);
1641 struct buffer_head *bh = sbi->s_lvid_bh;
1642 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001643 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001644
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001645 if (!bh)
1646 return;
1647
1648 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1649
Marcin Slusarz165923f2008-02-10 11:33:08 +01001650 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1651 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Marcin Slusarz165923f2008-02-10 11:33:08 +01001653 lvidiu = udf_sb_lvidiu(sbi);
1654 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1655 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1656 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1657 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1658 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1659 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1660 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1661 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1662 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1663 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Marcin Slusarz165923f2008-02-10 11:33:08 +01001665 lvid->descTag.descCRC = cpu_to_le16(
1666 udf_crc((char *)lvid + sizeof(tag),
1667 le16_to_cpu(lvid->descTag.descCRCLength),
1668 0));
1669
1670 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1671 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001674static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1675{
1676 int i;
1677 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001678 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1679 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001680
1681 for (i = 0; i < nr_groups; i++)
1682 if (bitmap->s_block_bitmap[i])
1683 brelse(bitmap->s_block_bitmap[i]);
1684
1685 if (size <= PAGE_SIZE)
1686 kfree(bitmap);
1687 else
1688 vfree(bitmap);
1689}
1690
Jan Kara2e0838f2008-04-01 18:08:51 +02001691static void udf_free_partition(struct udf_part_map *map)
1692{
1693 int i;
1694
1695 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1696 iput(map->s_uspace.s_table);
1697 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1698 iput(map->s_fspace.s_table);
1699 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1700 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1701 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1702 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1703 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1704 for (i = 0; i < 4; i++)
1705 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708static int udf_fill_super(struct super_block *sb, void *options, int silent)
1709{
1710 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001711 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 struct udf_options uopt;
1713 kernel_lb_addr rootdir, fileset;
1714 struct udf_sb_info *sbi;
1715
1716 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1717 uopt.uid = -1;
1718 uopt.gid = -1;
1719 uopt.umask = 0;
1720
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001721 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (!sbi)
1723 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001727 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Miklos Szeredi6da80892008-02-08 04:21:50 -08001729 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto error_out;
1731
1732 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001733 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001735 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto error_out;
1737 }
1738#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001739 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 uopt.nls_map = load_nls_default();
1741 if (!uopt.nls_map)
1742 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1743 else
1744 udf_debug("Using default NLS map\n");
1745 }
1746#endif
1747 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1748 uopt.flags |= (1 << UDF_FLAG_UTF8);
1749
1750 fileset.logicalBlockNum = 0xFFFFFFFF;
1751 fileset.partitionReferenceNum = 0xFFFF;
1752
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001753 sbi->s_flags = uopt.flags;
1754 sbi->s_uid = uopt.uid;
1755 sbi->s_gid = uopt.gid;
1756 sbi->s_umask = uopt.umask;
1757 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001760 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1761 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1762 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001766 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001767 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001769 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001771 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001773 sbi->s_last_block = uopt.lastblock;
1774 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1775 sbi->s_anchor[2] = uopt.anchor;
1776 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001778 if (udf_check_valid(sb, uopt.novrs, silent)) {
1779 /* read volume recognition sequences */
1780 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001781 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
1784 udf_find_anchor(sb);
1785
1786 /* Fill in the rest of the superblock */
1787 sb->s_op = &udf_sb_ops;
1788 sb->dq_op = NULL;
1789 sb->s_dirt = 0;
1790 sb->s_magic = UDF_SUPER_MAGIC;
1791 sb->s_time_gran = 1000;
1792
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001793 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001794 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto error_out;
1796 }
1797
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001798 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001800 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001801 struct logicalVolIntegrityDescImpUse *lvidiu =
1802 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001803 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1804 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001805 /* uint16_t maxUDFWriteRev =
1806 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001808 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001809 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1810 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001811 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001812 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001814 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001817 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1820 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1821 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1822 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1823 }
1824
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001825 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001826 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto error_out;
1828 }
1829
Marcin Slusarz4b111112008-02-08 04:20:36 -08001830 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1831 UDF_PART_FLAG_READ_ONLY) {
1832 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1833 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001834 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001835 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001836
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001837 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001838 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto error_out;
1840 }
1841
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001842 if (!silent) {
Marcin Slusarz56774802008-02-10 11:25:31 +01001843 timestamp ts;
1844 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001845 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001846 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001847 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1848 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 }
1850 if (!(sb->s_flags & MS_RDONLY))
1851 udf_open_lvid(sb);
1852
1853 /* Assign the root inode */
1854 /* assign inodes by physical block number */
1855 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001856 inode = udf_iget(sb, rootdir);
1857 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001858 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1859 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001860 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 goto error_out;
1862 }
1863
1864 /* Allocate a dentry for the root inode */
1865 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001866 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001867 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 iput(inode);
1869 goto error_out;
1870 }
Jan Kara31170b62007-05-08 00:35:21 -07001871 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return 0;
1873
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001874error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001875 if (sbi->s_vat_inode)
1876 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001877 if (sbi->s_partitions)
1878 for (i = 0; i < sbi->s_partitions; i++)
1879 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880#ifdef CONFIG_UDF_NLS
1881 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001882 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883#endif
1884 if (!(sb->s_flags & MS_RDONLY))
1885 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001886 brelse(sbi->s_lvid_bh);
1887
1888 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 kfree(sbi);
1890 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return -EINVAL;
1893}
1894
Adrian Bunkb8145a72008-02-17 10:19:55 +02001895static void udf_error(struct super_block *sb, const char *function,
1896 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 va_list args;
1899
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001900 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* mark sb error */
1902 sb->s_dirt = 1;
1903 }
1904 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001905 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001907 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001908 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001912 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 va_list args;
1915
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001916 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001917 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 va_end(args);
1919 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001923static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001926 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001928 sbi = UDF_SB(sb);
1929 if (sbi->s_vat_inode)
1930 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001931 if (sbi->s_partitions)
1932 for (i = 0; i < sbi->s_partitions; i++)
1933 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934#ifdef CONFIG_UDF_NLS
1935 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001936 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937#endif
1938 if (!(sb->s_flags & MS_RDONLY))
1939 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001940 brelse(sbi->s_lvid_bh);
1941 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 kfree(sb->s_fs_info);
1943 sb->s_fs_info = NULL;
1944}
1945
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001946static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
David Howells726c3342006-06-23 02:02:58 -07001948 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001949 struct udf_sb_info *sbi = UDF_SB(sb);
1950 struct logicalVolIntegrityDescImpUse *lvidiu;
1951
1952 if (sbi->s_lvid_bh != NULL)
1953 lvidiu = udf_sb_lvidiu(sbi);
1954 else
1955 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 buf->f_type = UDF_SUPER_MAGIC;
1958 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001959 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 buf->f_bfree = udf_count_free(sb);
1961 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001962 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1963 le32_to_cpu(lvidiu->numDirs)) : 0)
1964 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 buf->f_ffree = buf->f_bfree;
1966 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001967 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 return 0;
1970}
1971
Marcin Slusarz4b111112008-02-08 04:20:36 -08001972static unsigned int udf_count_free_bitmap(struct super_block *sb,
1973 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 struct buffer_head *bh = NULL;
1976 unsigned int accum = 0;
1977 int index;
1978 int block = 0, newblock;
1979 kernel_lb_addr loc;
1980 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 uint8_t *ptr;
1982 uint16_t ident;
1983 struct spaceBitmapDesc *bm;
1984
1985 lock_kernel();
1986
1987 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001988 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 bh = udf_read_ptagged(sb, loc, 0, &ident);
1990
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001991 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 printk(KERN_ERR "udf: udf_count_free failed\n");
1993 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001994 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001995 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 printk(KERN_ERR "udf: udf_count_free failed\n");
1997 goto out;
1998 }
1999
2000 bm = (struct spaceBitmapDesc *)bh->b_data;
2001 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002002 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2003 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002005 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002006 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2007 accum += bitmap_weight((const unsigned long *)(ptr + index),
2008 cur_bytes * 8);
2009 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002010 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002011 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 newblock = udf_get_lb_pblock(sb, loc, ++block);
2013 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002014 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 udf_debug("read failed\n");
2016 goto out;
2017 }
2018 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002019 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002022 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002024out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 unlock_kernel();
2026
2027 return accum;
2028}
2029
Marcin Slusarz4b111112008-02-08 04:20:36 -08002030static unsigned int udf_count_free_table(struct super_block *sb,
2031 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002034 uint32_t elen;
2035 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002037 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 lock_kernel();
2040
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002041 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002042 epos.offset = sizeof(struct unallocSpaceEntry);
2043 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002045 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002047
Jan Kara3bf25cb2007-05-08 00:35:16 -07002048 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 unlock_kernel();
2051
2052 return accum;
2053}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002054
2055static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002058 struct udf_sb_info *sbi;
2059 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002061 sbi = UDF_SB(sb);
2062 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002063 struct logicalVolIntegrityDesc *lvid =
2064 (struct logicalVolIntegrityDesc *)
2065 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002066 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002067 accum = le32_to_cpu(
2068 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (accum == 0xFFFFFFFF)
2070 accum = 0;
2071 }
2072 }
2073
2074 if (accum)
2075 return accum;
2076
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002077 map = &sbi->s_partmaps[sbi->s_partition];
2078 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002079 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002080 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002082 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002083 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002084 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086 if (accum)
2087 return accum;
2088
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002089 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002090 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002091 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002093 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002094 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002095 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097
2098 return accum;
2099}