blob: 7ec828566df2fb53f772a6c058647f7503102b42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
88static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
90 kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091static void udf_load_fileset(struct super_block *, struct buffer_head *,
92 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static void udf_open_lvid(struct super_block *);
94static void udf_close_lvid(struct super_block *);
95static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070096static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080097static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +020098static void udf_error(struct super_block *sb, const char *function,
99 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800101struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
102{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800103 struct logicalVolIntegrityDesc *lvid =
104 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800105 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 __u32 offset = number_of_partitions * 2 *
107 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700112static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700113 int flags, const char *dev_name, void *data,
114 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
David Howells454e2392006-06-23 02:02:57 -0700116 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700120 .owner = THIS_MODULE,
121 .name = "udf",
122 .get_sb = udf_get_sb,
123 .kill_sb = kill_block_super,
124 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700127static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static struct inode *udf_alloc_inode(struct super_block *sb)
130{
131 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800132 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!ei)
134 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700135
136 ei->i_unique = 0;
137 ei->i_lenExtents = 0;
138 ei->i_next_alloc_block = 0;
139 ei->i_next_alloc_goal = 0;
140 ei->i_strat4096 = 0;
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return &ei->vfs_inode;
143}
144
145static void udf_destroy_inode(struct inode *inode)
146{
147 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
148}
149
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700150static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700152 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Christoph Lametera35afb82007-05-16 22:10:57 -0700154 ei->i_ext.i_data = NULL;
155 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int init_inodecache(void)
159{
160 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
161 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 0, (SLAB_RECLAIM_ACCOUNT |
163 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900164 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENOMEM;
167 return 0;
168}
169
170static void destroy_inodecache(void)
171{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700172 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800176static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700177 .alloc_inode = udf_alloc_inode,
178 .destroy_inode = udf_destroy_inode,
179 .write_inode = udf_write_inode,
180 .delete_inode = udf_delete_inode,
181 .clear_inode = udf_clear_inode,
182 .put_super = udf_put_super,
183 .write_super = udf_write_super,
184 .statfs = udf_statfs,
185 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800186 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char novrs;
191 unsigned int blocksize;
192 unsigned int session;
193 unsigned int lastblock;
194 unsigned int anchor;
195 unsigned int volume;
196 unsigned short partition;
197 unsigned int fileset;
198 unsigned int rootdir;
199 unsigned int flags;
200 mode_t umask;
201 gid_t gid;
202 uid_t uid;
203 struct nls_table *nls_map;
204};
205
206static int __init init_udf_fs(void)
207{
208 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = init_inodecache();
211 if (err)
212 goto out1;
213 err = register_filesystem(&udf_fstype);
214 if (err)
215 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700218
219out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return err;
224}
225
226static void __exit exit_udf_fs(void)
227{
228 unregister_filesystem(&udf_fstype);
229 destroy_inodecache();
230}
231
232module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700233module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800235static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
236{
237 struct udf_sb_info *sbi = UDF_SB(sb);
238
239 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
240 GFP_KERNEL);
241 if (!sbi->s_partmaps) {
242 udf_error(sb, __FUNCTION__,
243 "Unable to allocate space for %d partition maps",
244 count);
245 sbi->s_partitions = 0;
246 return -ENOMEM;
247 }
248
249 sbi->s_partitions = count;
250 return 0;
251}
252
Miklos Szeredi6da80892008-02-08 04:21:50 -0800253static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
254{
255 struct super_block *sb = mnt->mnt_sb;
256 struct udf_sb_info *sbi = UDF_SB(sb);
257
258 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
259 seq_puts(seq, ",nostrict");
260 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
261 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
262 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
263 seq_puts(seq, ",unhide");
264 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
265 seq_puts(seq, ",undelete");
266 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
267 seq_puts(seq, ",noadinicb");
268 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
269 seq_puts(seq, ",shortad");
270 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
271 seq_puts(seq, ",uid=forget");
272 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
273 seq_puts(seq, ",uid=ignore");
274 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
275 seq_puts(seq, ",gid=forget");
276 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
277 seq_puts(seq, ",gid=ignore");
278 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
279 seq_printf(seq, ",uid=%u", sbi->s_uid);
280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
281 seq_printf(seq, ",gid=%u", sbi->s_gid);
282 if (sbi->s_umask != 0)
283 seq_printf(seq, ",umask=%o", sbi->s_umask);
284 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
285 seq_printf(seq, ",session=%u", sbi->s_session);
286 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
287 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
288 /*
289 * s_anchor[2] could be zeroed out in case there is no anchor
290 * in the specified block, but then the "anchor=N" option
291 * originally given by the user wasn't effective, so it's OK
292 * if we don't show it.
293 */
294 if (sbi->s_anchor[2] != 0)
295 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
296 /*
297 * volume, partition, fileset and rootdir seem to be ignored
298 * currently
299 */
300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
301 seq_puts(seq, ",utf8");
302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
303 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
304
305 return 0;
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
309 * udf_parse_options
310 *
311 * PURPOSE
312 * Parse mount options.
313 *
314 * DESCRIPTION
315 * The following mount options are supported:
316 *
317 * gid= Set the default group.
318 * umask= Set the default umask.
319 * uid= Set the default user.
320 * bs= Set the block size.
321 * unhide Show otherwise hidden files.
322 * undelete Show deleted files in lists.
323 * adinicb Embed data in the inode (default)
324 * noadinicb Don't embed data in the inode
325 * shortad Use short ad's
326 * longad Use long ad's (default)
327 * nostrict Unset strict conformance
328 * iocharset= Set the NLS character set
329 *
330 * The remaining are for debugging and disaster recovery:
331 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700332 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 *
334 * The following expect a offset from 0.
335 *
336 * session= Set the CDROM session (default= last session)
337 * anchor= Override standard anchor location. (default= 256)
338 * volume= Override the VolumeDesc location. (unused)
339 * partition= Override the PartitionDesc location. (unused)
340 * lastblock= Set the last block of the filesystem/
341 *
342 * The following expect a offset from the partition root.
343 *
344 * fileset= Override the fileset block location. (unused)
345 * rootdir= Override the root directory location. (unused)
346 * WARNING: overriding the rootdir to a non-directory may
347 * yield highly unpredictable results.
348 *
349 * PRE-CONDITIONS
350 * options Pointer to mount options string.
351 * uopts Pointer to mount options variable.
352 *
353 * POST-CONDITIONS
354 * <return> 1 Mount options parsed okay.
355 * <return> 0 Error parsing mount options.
356 *
357 * HISTORY
358 * July 1, 1997 - Andrew E. Mileski
359 * Written, tested, and released.
360 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362enum {
363 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
364 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
365 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
366 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
367 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800368 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369};
370
371static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700372 {Opt_novrs, "novrs"},
373 {Opt_nostrict, "nostrict"},
374 {Opt_bs, "bs=%u"},
375 {Opt_unhide, "unhide"},
376 {Opt_undelete, "undelete"},
377 {Opt_noadinicb, "noadinicb"},
378 {Opt_adinicb, "adinicb"},
379 {Opt_shortad, "shortad"},
380 {Opt_longad, "longad"},
381 {Opt_uforget, "uid=forget"},
382 {Opt_uignore, "uid=ignore"},
383 {Opt_gforget, "gid=forget"},
384 {Opt_gignore, "gid=ignore"},
385 {Opt_gid, "gid=%u"},
386 {Opt_uid, "uid=%u"},
387 {Opt_umask, "umask=%o"},
388 {Opt_session, "session=%u"},
389 {Opt_lastblock, "lastblock=%u"},
390 {Opt_anchor, "anchor=%u"},
391 {Opt_volume, "volume=%u"},
392 {Opt_partition, "partition=%u"},
393 {Opt_fileset, "fileset=%u"},
394 {Opt_rootdir, "rootdir=%u"},
395 {Opt_utf8, "utf8"},
396 {Opt_iocharset, "iocharset=%s"},
397 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398};
399
Miklos Szeredi6da80892008-02-08 04:21:50 -0800400static int udf_parse_options(char *options, struct udf_options *uopt,
401 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 char *p;
404 int option;
405
406 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800407 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 uopt->partition = 0xFFFF;
409 uopt->session = 0xFFFFFFFF;
410 uopt->lastblock = 0;
411 uopt->anchor = 0;
412 uopt->volume = 0xFFFFFFFF;
413 uopt->rootdir = 0xFFFFFFFF;
414 uopt->fileset = 0xFFFFFFFF;
415 uopt->nls_map = NULL;
416
417 if (!options)
418 return 1;
419
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700420 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 substring_t args[MAX_OPT_ARGS];
422 int token;
423 if (!*p)
424 continue;
425
426 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700427 switch (token) {
428 case Opt_novrs:
429 uopt->novrs = 1;
430 case Opt_bs:
431 if (match_int(&args[0], &option))
432 return 0;
433 uopt->blocksize = option;
434 break;
435 case Opt_unhide:
436 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
437 break;
438 case Opt_undelete:
439 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
440 break;
441 case Opt_noadinicb:
442 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
443 break;
444 case Opt_adinicb:
445 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_shortad:
448 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
449 break;
450 case Opt_longad:
451 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_gid:
454 if (match_int(args, &option))
455 return 0;
456 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700457 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700458 break;
459 case Opt_uid:
460 if (match_int(args, &option))
461 return 0;
462 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700463 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700464 break;
465 case Opt_umask:
466 if (match_octal(args, &option))
467 return 0;
468 uopt->umask = option;
469 break;
470 case Opt_nostrict:
471 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
472 break;
473 case Opt_session:
474 if (match_int(args, &option))
475 return 0;
476 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800477 if (!remount)
478 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700479 break;
480 case Opt_lastblock:
481 if (match_int(args, &option))
482 return 0;
483 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 break;
487 case Opt_anchor:
488 if (match_int(args, &option))
489 return 0;
490 uopt->anchor = option;
491 break;
492 case Opt_volume:
493 if (match_int(args, &option))
494 return 0;
495 uopt->volume = option;
496 break;
497 case Opt_partition:
498 if (match_int(args, &option))
499 return 0;
500 uopt->partition = option;
501 break;
502 case Opt_fileset:
503 if (match_int(args, &option))
504 return 0;
505 uopt->fileset = option;
506 break;
507 case Opt_rootdir:
508 if (match_int(args, &option))
509 return 0;
510 uopt->rootdir = option;
511 break;
512 case Opt_utf8:
513 uopt->flags |= (1 << UDF_FLAG_UTF8);
514 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 case Opt_iocharset:
517 uopt->nls_map = load_nls(args[0].from);
518 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 case Opt_uignore:
522 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
523 break;
524 case Opt_uforget:
525 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
526 break;
527 case Opt_gignore:
528 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
529 break;
530 case Opt_gforget:
531 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
532 break;
533 default:
534 printk(KERN_ERR "udf: bad mount option \"%s\" "
535 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return 0;
537 }
538 }
539 return 1;
540}
541
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800542static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!(sb->s_flags & MS_RDONLY))
547 udf_open_lvid(sb);
548 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 unlock_kernel();
551}
552
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700553static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800556 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800558 uopt.flags = sbi->s_flags;
559 uopt.uid = sbi->s_uid;
560 uopt.gid = sbi->s_gid;
561 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Miklos Szeredi6da80892008-02-08 04:21:50 -0800563 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EINVAL;
565
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800566 sbi->s_flags = uopt.flags;
567 sbi->s_uid = uopt.uid;
568 sbi->s_gid = uopt.gid;
569 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800571 if (sbi->s_lvid_bh) {
572 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (write_rev > UDF_MAX_WRITE_VERSION)
574 *flags |= MS_RDONLY;
575 }
576
577 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
578 return 0;
579 if (*flags & MS_RDONLY)
580 udf_close_lvid(sb);
581 else
582 udf_open_lvid(sb);
583
584 return 0;
585}
586
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700587static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct volStructDesc *vsd = NULL;
590 int sector = 32768;
591 int sectorsize;
592 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700593 int iso9660 = 0;
594 int nsr02 = 0;
595 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800596 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Block size must be a multiple of 512 */
599 if (sb->s_blocksize & 511)
600 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800601 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 if (sb->s_blocksize < sizeof(struct volStructDesc))
604 sectorsize = sizeof(struct volStructDesc);
605 else
606 sectorsize = sb->s_blocksize;
607
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800608 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700611 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700613 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Read a block */
615 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
616 if (!bh)
617 break;
618
619 /* Look for ISO descriptors */
620 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800621 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700623 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700624 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800626 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
627 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700629 switch (vsd->structType) {
630 case 0:
631 udf_debug("ISO9660 Boot Record found\n");
632 break;
633 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800634 udf_debug("ISO9660 Primary Volume Descriptor "
635 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636 break;
637 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800638 udf_debug("ISO9660 Supplementary Volume "
639 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700640 break;
641 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800642 udf_debug("ISO9660 Volume Partition Descriptor "
643 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700644 break;
645 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800646 udf_debug("ISO9660 Volume Descriptor Set "
647 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 break;
649 default:
650 udf_debug("ISO9660 VRS (%u) found\n",
651 vsd->structType);
652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800654 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
655 VSD_STD_ID_LEN))
656 ; /* nothing */
657 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
658 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700659 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800661 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
662 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800664 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
665 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700667 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
670 if (nsr03)
671 return nsr03;
672 else if (nsr02)
673 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800674 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return -1;
676 else
677 return 0;
678}
679
680/*
681 * udf_find_anchor
682 *
683 * PURPOSE
684 * Find an anchor volume descriptor.
685 *
686 * PRE-CONDITIONS
687 * sb Pointer to _locked_ superblock.
688 * lastblock Last block on media.
689 *
690 * POST-CONDITIONS
691 * <return> 1 if not found, 0 if ok
692 *
693 * HISTORY
694 * July 1, 1997 - Andrew E. Mileski
695 * Written, tested, and released.
696 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700697static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800699 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct buffer_head *bh = NULL;
701 uint16_t ident;
702 uint32_t location;
703 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800704 struct udf_sb_info *sbi;
705
706 sbi = UDF_SB(sb);
707 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700709 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700711 int last[] = { lastblock, lastblock - 2,
712 lastblock - 150, lastblock - 152,
713 varlastblock, varlastblock - 2,
714 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 lastblock = 0;
717
718 /* Search for an anchor volume descriptor pointer */
719
720 /* according to spec, anchor is in either:
721 * block 256
722 * lastblock-256
723 * lastblock
724 * however, if the disc isn't closed, it could be 512 */
725
Tobias Klausere8c96f82006-03-24 03:15:34 -0800726 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800727 ident = location = 0;
728 if (last[i] >= 0) {
729 bh = sb_bread(sb, last[i]);
730 if (bh) {
731 tag *t = (tag *)bh->b_data;
732 ident = le16_to_cpu(t->tagIdent);
733 location = le32_to_cpu(t->tagLocation);
734 brelse(bh);
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800737
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700738 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800739 if (location == last[i] - sbi->s_session) {
740 lastblock = last[i] - sbi->s_session;
741 sbi->s_anchor[0] = lastblock;
742 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800743 } else if (location ==
744 udf_variable_to_fixed(last[i]) -
745 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800747 lastblock =
748 udf_variable_to_fixed(last[i]) -
749 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800750 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800751 sbi->s_anchor[1] = lastblock - 256 -
752 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700753 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800754 udf_debug("Anchor found at block %d, "
755 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700756 last[i], location);
757 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800758 } else if (ident == TAG_IDENT_FE ||
759 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800761 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700762 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800763 ident = location = 0;
764 if (last[i] >= 256) {
765 bh = sb_bread(sb, last[i] - 256);
766 if (bh) {
767 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800768 ident = le16_to_cpu(
769 t->tagIdent);
770 location = le32_to_cpu(
771 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800772 brelse(bh);
773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800777 location == last[i] - 256 -
778 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800780 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700781 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800782 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800783 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800784 bh = sb_bread(sb,
785 last[i] - 312 -
786 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800787 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800788 tag *t = (tag *)
789 bh->b_data;
790 ident = le16_to_cpu(
791 t->tagIdent);
792 location = le32_to_cpu(
793 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800794 brelse(bh);
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700799 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800800 UDF_SET_FLAG(sb,
801 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700802 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800803 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 }
806 }
807 }
808 }
809
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700810 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800811 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800812 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800813 if (bh) {
814 tag *t = (tag *)bh->b_data;
815 ident = le16_to_cpu(t->tagIdent);
816 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700817 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (ident == TAG_IDENT_AVDP && location == 256)
820 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
821 }
822 }
823
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800824 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz165923f2008-02-10 11:33:08 +0100825 if (!sbi->s_anchor[i])
826 continue;
827 bh = udf_read_tagged(sb, sbi->s_anchor[i],
828 sbi->s_anchor[i], &ident);
829 if (!bh)
830 sbi->s_anchor[i] = 0;
831 else {
832 brelse(bh);
833 if ((ident != TAG_IDENT_AVDP) &&
834 (i || (ident != TAG_IDENT_FE &&
835 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800836 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838 }
839
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800843static int udf_find_fileset(struct super_block *sb,
844 kernel_lb_addr *fileset,
845 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct buffer_head *bh = NULL;
848 long lastblock;
849 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800850 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700853 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
855
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700856 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700858 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700859 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 1;
861 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800865 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800866 if (!bh) {
867 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 kernel_lb_addr newfileset;
869
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700870/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700872
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800873 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700874 (newfileset.partitionReferenceNum != 0xFFFF &&
875 fileset->logicalBlockNum == 0xFFFFFFFF &&
876 fileset->partitionReferenceNum == 0xFFFF);
877 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800878 lastblock = sbi->s_partmaps
879 [newfileset.partitionReferenceNum]
880 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 newfileset.logicalBlockNum = 0;
882
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700883 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800884 bh = udf_read_ptagged(sb, newfileset, 0,
885 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700886 if (!bh) {
887 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 continue;
889 }
890
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700891 switch (ident) {
892 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700893 {
894 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800895 sp = (struct spaceBitmapDesc *)
896 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700897 newfileset.logicalBlockNum += 1 +
898 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800899 sizeof(struct spaceBitmapDesc)
900 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700901 brelse(bh);
902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700904 case TAG_IDENT_FSD:
905 *fileset = newfileset;
906 break;
907 default:
908 newfileset.logicalBlockNum++;
909 brelse(bh);
910 bh = NULL;
911 break;
912 }
913 } while (newfileset.logicalBlockNum < lastblock &&
914 fileset->logicalBlockNum == 0xFFFFFFFF &&
915 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 }
918
919 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700920 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700922 fileset->logicalBlockNum,
923 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800925 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700927 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929 }
930 return 1;
931}
932
Jan Karac0eb31e2008-04-01 16:50:35 +0200933static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct primaryVolDesc *pvoldesc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct ustr instr;
937 struct ustr outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200938 struct buffer_head *bh;
939 uint16_t ident;
940
941 bh = udf_read_tagged(sb, block, block, &ident);
942 if (!bh)
943 return 1;
944 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 pvoldesc = (struct primaryVolDesc *)bh->b_data;
947
Marcin Slusarz56774802008-02-10 11:25:31 +0100948 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
949 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100950#ifdef UDFFS_DEBUG
951 timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100952 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800953 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100954 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
955 ts->minute, le16_to_cpu(ts->typeAndTimezone));
956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
Marcin Slusarz4b111112008-02-08 04:20:36 -0800959 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700960 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800961 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800963 udf_debug("volIdent[] = '%s'\n",
964 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Marcin Slusarz4b111112008-02-08 04:20:36 -0800967 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (udf_CS0toUTF8(&outstr, &instr))
969 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200970
971 brelse(bh);
972 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700975static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
976 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct fileSetDesc *fset;
979
980 fset = (struct fileSetDesc *)bh->b_data;
981
982 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
983
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800984 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700986 udf_debug("Rootdir at block=%d, partition=%d\n",
987 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800990int udf_compute_nr_groups(struct super_block *sb, u32 partition)
991{
992 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100993 return DIV_ROUND_UP(map->s_partition_len +
994 (sizeof(struct spaceBitmapDesc) << 3),
995 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800996}
997
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800998static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
999{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001000 struct udf_bitmap *bitmap;
1001 int nr_groups;
1002 int size;
1003
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001004 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001005 size = sizeof(struct udf_bitmap) +
1006 (sizeof(struct buffer_head *) * nr_groups);
1007
1008 if (size <= PAGE_SIZE)
1009 bitmap = kmalloc(size, GFP_KERNEL);
1010 else
1011 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1012
1013 if (bitmap == NULL) {
1014 udf_error(sb, __FUNCTION__,
1015 "Unable to allocate space for bitmap "
1016 "and %d buffer_head pointers", nr_groups);
1017 return NULL;
1018 }
1019
1020 memset(bitmap, 0x00, size);
1021 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1022 bitmap->s_nr_groups = nr_groups;
1023 return bitmap;
1024}
1025
Jan Kara3fb38df2008-04-02 12:26:36 +02001026static int udf_fill_partdesc_info(struct super_block *sb,
1027 struct partitionDesc *p, int p_index)
1028{
1029 struct udf_part_map *map;
1030 struct udf_sb_info *sbi = UDF_SB(sb);
1031 struct partitionHeaderDesc *phd;
1032
1033 map = &sbi->s_partmaps[p_index];
1034
1035 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1036 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1037
1038 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1039 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1040 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1041 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1042 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1043 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1044 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1045 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1046
1047 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1048 "block length %d\n", partitionNumber, p_index,
1049 map->s_partition_type, map->s_partition_root,
1050 map->s_partition_len);
1051
1052 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1053 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1054 return 0;
1055
1056 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1057 if (phd->unallocSpaceTable.extLength) {
1058 kernel_lb_addr loc = {
1059 .logicalBlockNum = le32_to_cpu(
1060 phd->unallocSpaceTable.extPosition),
1061 .partitionReferenceNum = p_index,
1062 };
1063
1064 map->s_uspace.s_table = udf_iget(sb, loc);
1065 if (!map->s_uspace.s_table) {
1066 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1067 p_index);
1068 return 1;
1069 }
1070 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1071 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1072 p_index, map->s_uspace.s_table->i_ino);
1073 }
1074
1075 if (phd->unallocSpaceBitmap.extLength) {
1076 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1077 if (!bitmap)
1078 return 1;
1079 map->s_uspace.s_bitmap = bitmap;
1080 bitmap->s_extLength = le32_to_cpu(
1081 phd->unallocSpaceBitmap.extLength);
1082 bitmap->s_extPosition = le32_to_cpu(
1083 phd->unallocSpaceBitmap.extPosition);
1084 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1085 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1086 bitmap->s_extPosition);
1087 }
1088
1089 if (phd->partitionIntegrityTable.extLength)
1090 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1091
1092 if (phd->freedSpaceTable.extLength) {
1093 kernel_lb_addr loc = {
1094 .logicalBlockNum = le32_to_cpu(
1095 phd->freedSpaceTable.extPosition),
1096 .partitionReferenceNum = p_index,
1097 };
1098
1099 map->s_fspace.s_table = udf_iget(sb, loc);
1100 if (!map->s_fspace.s_table) {
1101 udf_debug("cannot load freedSpaceTable (part %d)\n",
1102 p_index);
1103 return 1;
1104 }
1105
1106 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1107 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1108 p_index, map->s_fspace.s_table->i_ino);
1109 }
1110
1111 if (phd->freedSpaceBitmap.extLength) {
1112 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1113 if (!bitmap)
1114 return 1;
1115 map->s_fspace.s_bitmap = bitmap;
1116 bitmap->s_extLength = le32_to_cpu(
1117 phd->freedSpaceBitmap.extLength);
1118 bitmap->s_extPosition = le32_to_cpu(
1119 phd->freedSpaceBitmap.extPosition);
1120 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1121 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1122 bitmap->s_extPosition);
1123 }
1124 return 0;
1125}
1126
Jan Kara38b74a52008-04-02 16:01:35 +02001127static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1128{
1129 struct udf_sb_info *sbi = UDF_SB(sb);
1130 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1131 kernel_lb_addr ino;
1132 struct buffer_head *bh;
1133
1134 /* VAT file entry is in the last recorded block */
1135 ino.partitionReferenceNum = type1_index;
1136 ino.logicalBlockNum = sbi->s_last_block - map->s_partition_root;
1137 sbi->s_vat_inode = udf_iget(sb, ino);
1138 if (!sbi->s_vat_inode)
1139 return 1;
1140
1141 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1142 map->s_type_specific.s_virtual.s_start_offset =
1143 udf_ext0_offset(sbi->s_vat_inode);
1144 map->s_type_specific.s_virtual.s_num_entries =
1145 (sbi->s_vat_inode->i_size - 36) >> 2;
1146 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
1147 uint32_t pos;
1148 struct virtualAllocationTable20 *vat20;
1149
1150 pos = udf_block_map(sbi->s_vat_inode, 0);
1151 bh = sb_bread(sb, pos);
1152 if (!bh)
1153 return 1;
1154 vat20 = (struct virtualAllocationTable20 *)bh->b_data +
1155 udf_ext0_offset(sbi->s_vat_inode);
1156 map->s_type_specific.s_virtual.s_start_offset =
1157 le16_to_cpu(vat20->lengthHeader) +
1158 udf_ext0_offset(sbi->s_vat_inode);
1159 map->s_type_specific.s_virtual.s_num_entries =
1160 (sbi->s_vat_inode->i_size -
1161 map->s_type_specific.s_virtual.
1162 s_start_offset) >> 2;
1163 brelse(bh);
1164 }
1165 return 0;
1166}
1167
Jan Karac0eb31e2008-04-01 16:50:35 +02001168static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Jan Karac0eb31e2008-04-01 16:50:35 +02001170 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001171 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001172 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001173 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001174 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001175 uint16_t partitionNumber;
1176 uint16_t ident;
1177 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Jan Karac0eb31e2008-04-01 16:50:35 +02001179 bh = udf_read_tagged(sb, block, block, &ident);
1180 if (!bh)
1181 return 1;
1182 if (ident != TAG_IDENT_PD)
1183 goto out_bh;
1184
1185 p = (struct partitionDesc *)bh->b_data;
1186 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001187
1188 /* First scan for TYPE1 and SPARABLE partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001189 for (i = 0; i < sbi->s_partitions; i++) {
1190 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001191 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001192 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001193 if (map->s_partition_num == partitionNumber &&
1194 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1195 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001197 }
1198
Jan Kara38b74a52008-04-02 16:01:35 +02001199 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001200 udf_debug("Partition (%d) not found in partition map\n",
1201 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001202 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001203 }
1204
Jan Kara3fb38df2008-04-02 12:26:36 +02001205 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001206
1207 /*
1208 * Now rescan for VIRTUAL partitions when TYPE1 partitions are
1209 * already set up
1210 */
1211 type1_idx = i;
1212 for (i = 0; i < sbi->s_partitions; i++) {
1213 map = &sbi->s_partmaps[i];
1214
1215 if (map->s_partition_num == partitionNumber &&
1216 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1217 map->s_partition_type == UDF_VIRTUAL_MAP20))
1218 break;
1219 }
1220
1221 if (i >= sbi->s_partitions)
1222 goto out_bh;
1223
1224 ret = udf_fill_partdesc_info(sb, p, i);
1225 if (ret)
1226 goto out_bh;
1227
1228 if (!sbi->s_last_block) {
1229 sbi->s_last_block = udf_get_last_block(sb);
1230 udf_find_anchor(sb);
1231 if (!sbi->s_last_block) {
1232 udf_debug("Unable to determine Lastblock (For "
1233 "Virtual Partition)\n");
1234 ret = 1;
1235 goto out_bh;
1236 }
1237 }
1238
1239 ret = udf_load_vat(sb, i, type1_idx);
Jan Karac0eb31e2008-04-01 16:50:35 +02001240out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001241 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001242 brelse(bh);
1243 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Jan Karac0eb31e2008-04-01 16:50:35 +02001246static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001247 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct logicalVolDesc *lvd;
1250 int i, j, offset;
1251 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001252 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001253 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001254 uint16_t ident;
1255 struct buffer_head *bh;
1256 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jan Karac0eb31e2008-04-01 16:50:35 +02001258 bh = udf_read_tagged(sb, block, block, &ident);
1259 if (!bh)
1260 return 1;
1261 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 lvd = (struct logicalVolDesc *)bh->b_data;
1263
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001264 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001265 if (i != 0) {
1266 ret = i;
1267 goto out_bh;
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001270 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001271 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001272 i++, offset += gpm->partitionMapLength) {
1273 struct udf_part_map *map = &sbi->s_partmaps[i];
1274 gpm = (struct genericPartitionMap *)
1275 &(lvd->partitionMaps[offset]);
1276 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001278 struct genericPartitionMap1 *gpm1 =
1279 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001280 map->s_partition_type = UDF_TYPE1_MAP15;
1281 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1282 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1283 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001284 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001285 struct udfPartitionMap2 *upm2 =
1286 (struct udfPartitionMap2 *)gpm;
1287 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1288 strlen(UDF_ID_VIRTUAL))) {
1289 u16 suf =
1290 le16_to_cpu(((__le16 *)upm2->partIdent.
1291 identSuffix)[0]);
1292 if (suf == 0x0150) {
1293 map->s_partition_type =
1294 UDF_VIRTUAL_MAP15;
1295 map->s_partition_func =
1296 udf_get_pblock_virt15;
1297 } else if (suf == 0x0200) {
1298 map->s_partition_type =
1299 UDF_VIRTUAL_MAP20;
1300 map->s_partition_func =
1301 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001303 } else if (!strncmp(upm2->partIdent.ident,
1304 UDF_ID_SPARABLE,
1305 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001308 struct sparablePartitionMap *spm =
1309 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001311 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001312 map->s_type_specific.s_sparing.s_packet_len =
1313 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001314 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001315 struct buffer_head *bh2;
1316
1317 loc = le32_to_cpu(
1318 spm->locSparingTable[j]);
1319 bh2 = udf_read_tagged(sb, loc, loc,
1320 &ident);
1321 map->s_type_specific.s_sparing.
1322 s_spar_map[j] = bh2;
1323
Marcin Slusarz165923f2008-02-10 11:33:08 +01001324 if (bh2 == NULL)
1325 continue;
1326
1327 st = (struct sparingTable *)bh2->b_data;
1328 if (ident != 0 || strncmp(
1329 st->sparingIdent.ident,
1330 UDF_ID_SPARING,
1331 strlen(UDF_ID_SPARING))) {
1332 brelse(bh2);
1333 map->s_type_specific.s_sparing.
1334 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 }
1336 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001337 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001338 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001339 udf_debug("Unknown ident: %s\n",
1340 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 continue;
1342 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001343 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1344 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001347 i, map->s_partition_num, type,
1348 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001351 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001352 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001355 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1356 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001357 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 if (lvd->integritySeqExt.extLength)
1360 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001361
Jan Karac0eb31e2008-04-01 16:50:35 +02001362out_bh:
1363 brelse(bh);
1364 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367/*
1368 * udf_load_logicalvolint
1369 *
1370 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001371static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
1373 struct buffer_head *bh = NULL;
1374 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001375 struct udf_sb_info *sbi = UDF_SB(sb);
1376 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001379 (bh = udf_read_tagged(sb, loc.extLocation,
1380 loc.extLocation, &ident)) &&
1381 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001382 sbi->s_lvid_bh = bh;
1383 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001384
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001385 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001386 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001387 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001388
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001389 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001390 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001392 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001394 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001395 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
1399 * udf_process_sequence
1400 *
1401 * PURPOSE
1402 * Process a main/reserve volume descriptor sequence.
1403 *
1404 * PRE-CONDITIONS
1405 * sb Pointer to _locked_ superblock.
1406 * block First block of first extent of the sequence.
1407 * lastblock Lastblock of first extent of the sequence.
1408 *
1409 * HISTORY
1410 * July 1, 1997 - Andrew E. Mileski
1411 * Written, tested, and released.
1412 */
Jan Kara200a3592008-03-04 13:03:09 +01001413static noinline int udf_process_sequence(struct super_block *sb, long block,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001414 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct buffer_head *bh = NULL;
1417 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001418 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct generic_desc *gd;
1420 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001421 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 uint32_t vdsn;
1423 uint16_t ident;
1424 long next_s = 0, next_e = 0;
1425
1426 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1427
Jan Karac0eb31e2008-04-01 16:50:35 +02001428 /*
1429 * Read the main descriptor sequence and find which descriptors
1430 * are in it.
1431 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001432 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001435 if (!bh) {
1436 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1437 "sequence is corrupted or we could not read "
1438 "it.\n", (unsigned long long)block);
1439 return 1;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1443 gd = (struct generic_desc *)bh->b_data;
1444 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001445 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001446 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001447 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1448 if (vdsn >= curr->volDescSeqNum) {
1449 curr->volDescSeqNum = vdsn;
1450 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001451 }
1452 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001453 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001454 curr = &vds[VDS_POS_VOL_DESC_PTR];
1455 if (vdsn >= curr->volDescSeqNum) {
1456 curr->volDescSeqNum = vdsn;
1457 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001459 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001460 next_s = le32_to_cpu(
1461 vdp->nextVolDescSeqExt.extLocation);
1462 next_e = le32_to_cpu(
1463 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001464 next_e = next_e >> sb->s_blocksize_bits;
1465 next_e += next_s;
1466 }
1467 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001468 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001469 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1470 if (vdsn >= curr->volDescSeqNum) {
1471 curr->volDescSeqNum = vdsn;
1472 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001473 }
1474 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001475 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001476 curr = &vds[VDS_POS_PARTITION_DESC];
1477 if (!curr->block)
1478 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001479 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001480 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001481 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1482 if (vdsn >= curr->volDescSeqNum) {
1483 curr->volDescSeqNum = vdsn;
1484 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001485 }
1486 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001487 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001488 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1489 if (vdsn >= curr->volDescSeqNum) {
1490 curr->volDescSeqNum = vdsn;
1491 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001492 }
1493 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001494 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001495 vds[VDS_POS_TERMINATING_DESC].block = block;
1496 if (next_e) {
1497 block = next_s;
1498 lastblock = next_e;
1499 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001500 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501 done = 1;
1502 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001504 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001506 /*
1507 * Now read interesting descriptors again and process them
1508 * in a suitable order
1509 */
1510 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1511 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1512 return 1;
1513 }
1514 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1515 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Jan Karac0eb31e2008-04-01 16:50:35 +02001517 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1518 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1519 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001520
Jan Karac0eb31e2008-04-01 16:50:35 +02001521 if (vds[VDS_POS_PARTITION_DESC].block) {
1522 /*
1523 * We rescan the whole descriptor sequence to find
1524 * partition descriptor blocks and process them.
1525 */
1526 for (block = vds[VDS_POS_PARTITION_DESC].block;
1527 block < vds[VDS_POS_TERMINATING_DESC].block;
1528 block++)
1529 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001530 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
1533 return 0;
1534}
1535
1536/*
1537 * udf_check_valid()
1538 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001539static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 long block;
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001542 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001544 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 udf_debug("Validity check skipped because of novrs option\n");
1546 return 0;
1547 }
1548 /* Check that it is NSR02 compliant */
1549 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz165923f2008-02-10 11:33:08 +01001550 block = udf_vrs(sb, silent);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001551 if (block == -1)
1552 udf_debug("Failed to read byte 32768. Assuming open "
1553 "disc. Skipping validity check\n");
1554 if (block && !sbi->s_last_block)
Marcin Slusarz165923f2008-02-10 11:33:08 +01001555 sbi->s_last_block = udf_get_last_block(sb);
Pavel Emelyanovd0db1812008-03-05 00:03:36 +01001556 return !block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
Jan Kara38b74a52008-04-02 16:01:35 +02001559static int udf_load_sequence(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct anchorVolDescPtr *anchor;
1562 uint16_t ident;
1563 struct buffer_head *bh;
1564 long main_s, main_e, reserve_s, reserve_e;
Jan Kara38b74a52008-04-02 16:01:35 +02001565 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001566 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
1568 if (!sb)
1569 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001570 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001572 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001573 if (!sbi->s_anchor[i])
1574 continue;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001575
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001576 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1577 &ident);
1578 if (!bh)
1579 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001581 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001582
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001583 /* Locate the main sequence */
1584 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1585 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1586 main_e = main_e >> sb->s_blocksize_bits;
1587 main_e += main_s;
1588
1589 /* Locate the reserve sequence */
1590 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001591 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001592 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001593 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001594 reserve_e = reserve_e >> sb->s_blocksize_bits;
1595 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001597 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001599 /* Process the main & reserve sequences */
1600 /* responsible for finding the PartitionDesc(s) */
1601 if (!(udf_process_sequence(sb, main_s, main_e,
1602 fileset) &&
1603 udf_process_sequence(sb, reserve_s, reserve_e,
1604 fileset)))
1605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
1607
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001608 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 udf_debug("No Anchor block found\n");
1610 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001611 }
1612 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return 0;
1615}
1616
1617static void udf_open_lvid(struct super_block *sb)
1618{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001619 struct udf_sb_info *sbi = UDF_SB(sb);
1620 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001621 struct logicalVolIntegrityDesc *lvid;
1622 struct logicalVolIntegrityDescImpUse *lvidiu;
1623 if (!bh)
1624 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Marcin Slusarz165923f2008-02-10 11:33:08 +01001626 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1627 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Marcin Slusarz165923f2008-02-10 11:33:08 +01001629 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1630 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1631 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1632 CURRENT_TIME);
1633 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Marcin Slusarz165923f2008-02-10 11:33:08 +01001635 lvid->descTag.descCRC = cpu_to_le16(
1636 udf_crc((char *)lvid + sizeof(tag),
1637 le16_to_cpu(lvid->descTag.descCRCLength), 0));
1638
1639 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1640 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643static void udf_close_lvid(struct super_block *sb)
1644{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001645 struct udf_sb_info *sbi = UDF_SB(sb);
1646 struct buffer_head *bh = sbi->s_lvid_bh;
1647 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001648 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001649
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001650 if (!bh)
1651 return;
1652
1653 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1654
Marcin Slusarz165923f2008-02-10 11:33:08 +01001655 if (lvid->integrityType != LVID_INTEGRITY_TYPE_OPEN)
1656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Marcin Slusarz165923f2008-02-10 11:33:08 +01001658 lvidiu = udf_sb_lvidiu(sbi);
1659 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1660 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1661 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1662 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1663 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1664 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1665 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1666 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1667 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1668 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Marcin Slusarz165923f2008-02-10 11:33:08 +01001670 lvid->descTag.descCRC = cpu_to_le16(
1671 udf_crc((char *)lvid + sizeof(tag),
1672 le16_to_cpu(lvid->descTag.descCRCLength),
1673 0));
1674
1675 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1676 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001679static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1680{
1681 int i;
1682 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001683 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1684 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001685
1686 for (i = 0; i < nr_groups; i++)
1687 if (bitmap->s_block_bitmap[i])
1688 brelse(bitmap->s_block_bitmap[i]);
1689
1690 if (size <= PAGE_SIZE)
1691 kfree(bitmap);
1692 else
1693 vfree(bitmap);
1694}
1695
Jan Kara2e0838f2008-04-01 18:08:51 +02001696static void udf_free_partition(struct udf_part_map *map)
1697{
1698 int i;
1699
1700 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1701 iput(map->s_uspace.s_table);
1702 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1703 iput(map->s_fspace.s_table);
1704 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1705 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1706 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1707 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1708 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1709 for (i = 0; i < 4; i++)
1710 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
1711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713static int udf_fill_super(struct super_block *sb, void *options, int silent)
1714{
1715 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001716 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct udf_options uopt;
1718 kernel_lb_addr rootdir, fileset;
1719 struct udf_sb_info *sbi;
1720
1721 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1722 uopt.uid = -1;
1723 uopt.gid = -1;
1724 uopt.umask = 0;
1725
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001726 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (!sbi)
1728 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001732 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Miklos Szeredi6da80892008-02-08 04:21:50 -08001734 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto error_out;
1736
1737 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001738 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001740 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 goto error_out;
1742 }
1743#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001744 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 uopt.nls_map = load_nls_default();
1746 if (!uopt.nls_map)
1747 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1748 else
1749 udf_debug("Using default NLS map\n");
1750 }
1751#endif
1752 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1753 uopt.flags |= (1 << UDF_FLAG_UTF8);
1754
1755 fileset.logicalBlockNum = 0xFFFFFFFF;
1756 fileset.partitionReferenceNum = 0xFFFF;
1757
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001758 sbi->s_flags = uopt.flags;
1759 sbi->s_uid = uopt.uid;
1760 sbi->s_gid = uopt.gid;
1761 sbi->s_umask = uopt.umask;
1762 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001765 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1766 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1767 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001771 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001772 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001774 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001776 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001778 sbi->s_last_block = uopt.lastblock;
1779 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1780 sbi->s_anchor[2] = uopt.anchor;
1781 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001783 if (udf_check_valid(sb, uopt.novrs, silent)) {
1784 /* read volume recognition sequences */
1785 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001786 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788
1789 udf_find_anchor(sb);
1790
1791 /* Fill in the rest of the superblock */
1792 sb->s_op = &udf_sb_ops;
1793 sb->dq_op = NULL;
1794 sb->s_dirt = 0;
1795 sb->s_magic = UDF_SUPER_MAGIC;
1796 sb->s_time_gran = 1000;
1797
Jan Kara38b74a52008-04-02 16:01:35 +02001798 if (udf_load_sequence(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001799 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 goto error_out;
1801 }
1802
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001803 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001805 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001806 struct logicalVolIntegrityDescImpUse *lvidiu =
1807 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001808 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1809 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001810 /* uint16_t maxUDFWriteRev =
1811 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001813 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001814 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1815 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001816 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001817 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001819 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001822 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1825 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1826 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1827 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1828 }
1829
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001830 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001831 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto error_out;
1833 }
1834
Marcin Slusarz4b111112008-02-08 04:20:36 -08001835 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1836 UDF_PART_FLAG_READ_ONLY) {
1837 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1838 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001839 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001840 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001841
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001842 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001843 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto error_out;
1845 }
1846
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001847 if (!silent) {
Marcin Slusarz56774802008-02-10 11:25:31 +01001848 timestamp ts;
1849 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001850 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001851 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01001852 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
1853 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
1855 if (!(sb->s_flags & MS_RDONLY))
1856 udf_open_lvid(sb);
1857
1858 /* Assign the root inode */
1859 /* assign inodes by physical block number */
1860 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001861 inode = udf_iget(sb, rootdir);
1862 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001863 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1864 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001865 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto error_out;
1867 }
1868
1869 /* Allocate a dentry for the root inode */
1870 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001871 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001872 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 iput(inode);
1874 goto error_out;
1875 }
Jan Kara31170b62007-05-08 00:35:21 -07001876 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return 0;
1878
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001879error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001880 if (sbi->s_vat_inode)
1881 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001882 if (sbi->s_partitions)
1883 for (i = 0; i < sbi->s_partitions; i++)
1884 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885#ifdef CONFIG_UDF_NLS
1886 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001887 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888#endif
1889 if (!(sb->s_flags & MS_RDONLY))
1890 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001891 brelse(sbi->s_lvid_bh);
1892
1893 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 kfree(sbi);
1895 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return -EINVAL;
1898}
1899
Adrian Bunkb8145a72008-02-17 10:19:55 +02001900static void udf_error(struct super_block *sb, const char *function,
1901 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 va_list args;
1904
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001905 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* mark sb error */
1907 sb->s_dirt = 1;
1908 }
1909 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001910 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001912 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001913 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001917 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 va_list args;
1920
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001921 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001922 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 va_end(args);
1924 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001925 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001928static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001931 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001933 sbi = UDF_SB(sb);
1934 if (sbi->s_vat_inode)
1935 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02001936 if (sbi->s_partitions)
1937 for (i = 0; i < sbi->s_partitions; i++)
1938 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939#ifdef CONFIG_UDF_NLS
1940 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001941 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942#endif
1943 if (!(sb->s_flags & MS_RDONLY))
1944 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001945 brelse(sbi->s_lvid_bh);
1946 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 kfree(sb->s_fs_info);
1948 sb->s_fs_info = NULL;
1949}
1950
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001951static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
David Howells726c3342006-06-23 02:02:58 -07001953 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001954 struct udf_sb_info *sbi = UDF_SB(sb);
1955 struct logicalVolIntegrityDescImpUse *lvidiu;
1956
1957 if (sbi->s_lvid_bh != NULL)
1958 lvidiu = udf_sb_lvidiu(sbi);
1959 else
1960 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 buf->f_type = UDF_SUPER_MAGIC;
1963 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001964 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 buf->f_bfree = udf_count_free(sb);
1966 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001967 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1968 le32_to_cpu(lvidiu->numDirs)) : 0)
1969 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 buf->f_ffree = buf->f_bfree;
1971 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001972 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 return 0;
1975}
1976
Marcin Slusarz4b111112008-02-08 04:20:36 -08001977static unsigned int udf_count_free_bitmap(struct super_block *sb,
1978 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 struct buffer_head *bh = NULL;
1981 unsigned int accum = 0;
1982 int index;
1983 int block = 0, newblock;
1984 kernel_lb_addr loc;
1985 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 uint8_t *ptr;
1987 uint16_t ident;
1988 struct spaceBitmapDesc *bm;
1989
1990 lock_kernel();
1991
1992 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001993 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 bh = udf_read_ptagged(sb, loc, 0, &ident);
1995
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001996 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 printk(KERN_ERR "udf: udf_count_free failed\n");
1998 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001999 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002000 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 printk(KERN_ERR "udf: udf_count_free failed\n");
2002 goto out;
2003 }
2004
2005 bm = (struct spaceBitmapDesc *)bh->b_data;
2006 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002007 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2008 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002010 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002011 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2012 accum += bitmap_weight((const unsigned long *)(ptr + index),
2013 cur_bytes * 8);
2014 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002015 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002016 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 newblock = udf_get_lb_pblock(sb, loc, ++block);
2018 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002019 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 udf_debug("read failed\n");
2021 goto out;
2022 }
2023 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002024 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002027 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 unlock_kernel();
2031
2032 return accum;
2033}
2034
Marcin Slusarz4b111112008-02-08 04:20:36 -08002035static unsigned int udf_count_free_table(struct super_block *sb,
2036 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002039 uint32_t elen;
2040 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002042 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 lock_kernel();
2045
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002046 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002047 epos.offset = sizeof(struct unallocSpaceEntry);
2048 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002050 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002052
Jan Kara3bf25cb2007-05-08 00:35:16 -07002053 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 unlock_kernel();
2056
2057 return accum;
2058}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002059
2060static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002063 struct udf_sb_info *sbi;
2064 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002066 sbi = UDF_SB(sb);
2067 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002068 struct logicalVolIntegrityDesc *lvid =
2069 (struct logicalVolIntegrityDesc *)
2070 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002071 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002072 accum = le32_to_cpu(
2073 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (accum == 0xFFFFFFFF)
2075 accum = 0;
2076 }
2077 }
2078
2079 if (accum)
2080 return accum;
2081
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002082 map = &sbi->s_partmaps[sbi->s_partition];
2083 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002084 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002085 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002087 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002088 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002089 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091 if (accum)
2092 return accum;
2093
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002094 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002095 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002096 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002098 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002099 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002100 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102
2103 return accum;
2104}