blob: be0aa424b8f18ee0e014d7ec19a42b5c42619748 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010058#include <linux/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
87static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
89 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
91static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
93 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095static void udf_load_fileset(struct super_block *, struct buffer_head *,
96 kernel_lb_addr *);
Jan Karabcec4472007-08-30 23:56:22 -070097static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void udf_open_lvid(struct super_block *);
99static void udf_close_lvid(struct super_block *);
100static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -0700101static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800102static int udf_show_options(struct seq_file *, struct vfsmount *);
Adrian Bunkb8145a72008-02-17 10:19:55 +0200103static void udf_error(struct super_block *sb, const char *function,
104 const char *fmt, ...);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
107{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800108 struct logicalVolIntegrityDesc *lvid =
109 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800110 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800111 __u32 offset = number_of_partitions * 2 *
112 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800113 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700117static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700118 int flags, const char *dev_name, void *data,
119 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
David Howells454e2392006-06-23 02:02:57 -0700121 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700125 .owner = THIS_MODULE,
126 .name = "udf",
127 .get_sb = udf_get_sb,
128 .kill_sb = kill_block_super,
129 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130};
131
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700132static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134static struct inode *udf_alloc_inode(struct super_block *sb)
135{
136 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800137 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (!ei)
139 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700140
141 ei->i_unique = 0;
142 ei->i_lenExtents = 0;
143 ei->i_next_alloc_block = 0;
144 ei->i_next_alloc_goal = 0;
145 ei->i_strat4096 = 0;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return &ei->vfs_inode;
148}
149
150static void udf_destroy_inode(struct inode *inode)
151{
152 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
153}
154
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700155static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700157 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Christoph Lametera35afb82007-05-16 22:10:57 -0700159 ei->i_ext.i_data = NULL;
160 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static int init_inodecache(void)
164{
165 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
166 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700167 0, (SLAB_RECLAIM_ACCOUNT |
168 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900169 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700170 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -ENOMEM;
172 return 0;
173}
174
175static void destroy_inodecache(void)
176{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700177 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800181static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700182 .alloc_inode = udf_alloc_inode,
183 .destroy_inode = udf_destroy_inode,
184 .write_inode = udf_write_inode,
185 .delete_inode = udf_delete_inode,
186 .clear_inode = udf_clear_inode,
187 .put_super = udf_put_super,
188 .write_super = udf_write_super,
189 .statfs = udf_statfs,
190 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800191 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned char novrs;
196 unsigned int blocksize;
197 unsigned int session;
198 unsigned int lastblock;
199 unsigned int anchor;
200 unsigned int volume;
201 unsigned short partition;
202 unsigned int fileset;
203 unsigned int rootdir;
204 unsigned int flags;
205 mode_t umask;
206 gid_t gid;
207 uid_t uid;
208 struct nls_table *nls_map;
209};
210
211static int __init init_udf_fs(void)
212{
213 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 err = init_inodecache();
216 if (err)
217 goto out1;
218 err = register_filesystem(&udf_fstype);
219 if (err)
220 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700223
224out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700226
227out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return err;
229}
230
231static void __exit exit_udf_fs(void)
232{
233 unregister_filesystem(&udf_fstype);
234 destroy_inodecache();
235}
236
237module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700238module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800240static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
241{
242 struct udf_sb_info *sbi = UDF_SB(sb);
243
244 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
245 GFP_KERNEL);
246 if (!sbi->s_partmaps) {
247 udf_error(sb, __FUNCTION__,
248 "Unable to allocate space for %d partition maps",
249 count);
250 sbi->s_partitions = 0;
251 return -ENOMEM;
252 }
253
254 sbi->s_partitions = count;
255 return 0;
256}
257
Miklos Szeredi6da80892008-02-08 04:21:50 -0800258static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
259{
260 struct super_block *sb = mnt->mnt_sb;
261 struct udf_sb_info *sbi = UDF_SB(sb);
262
263 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
264 seq_puts(seq, ",nostrict");
265 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
266 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
268 seq_puts(seq, ",unhide");
269 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
270 seq_puts(seq, ",undelete");
271 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
272 seq_puts(seq, ",noadinicb");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
274 seq_puts(seq, ",shortad");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
276 seq_puts(seq, ",uid=forget");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
278 seq_puts(seq, ",uid=ignore");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
280 seq_puts(seq, ",gid=forget");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
282 seq_puts(seq, ",gid=ignore");
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
284 seq_printf(seq, ",uid=%u", sbi->s_uid);
285 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
286 seq_printf(seq, ",gid=%u", sbi->s_gid);
287 if (sbi->s_umask != 0)
288 seq_printf(seq, ",umask=%o", sbi->s_umask);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
290 seq_printf(seq, ",session=%u", sbi->s_session);
291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
292 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
293 /*
294 * s_anchor[2] could be zeroed out in case there is no anchor
295 * in the specified block, but then the "anchor=N" option
296 * originally given by the user wasn't effective, so it's OK
297 * if we don't show it.
298 */
299 if (sbi->s_anchor[2] != 0)
300 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
301 /*
302 * volume, partition, fileset and rootdir seem to be ignored
303 * currently
304 */
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
306 seq_puts(seq, ",utf8");
307 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
308 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
309
310 return 0;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
314 * udf_parse_options
315 *
316 * PURPOSE
317 * Parse mount options.
318 *
319 * DESCRIPTION
320 * The following mount options are supported:
321 *
322 * gid= Set the default group.
323 * umask= Set the default umask.
324 * uid= Set the default user.
325 * bs= Set the block size.
326 * unhide Show otherwise hidden files.
327 * undelete Show deleted files in lists.
328 * adinicb Embed data in the inode (default)
329 * noadinicb Don't embed data in the inode
330 * shortad Use short ad's
331 * longad Use long ad's (default)
332 * nostrict Unset strict conformance
333 * iocharset= Set the NLS character set
334 *
335 * The remaining are for debugging and disaster recovery:
336 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700337 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
339 * The following expect a offset from 0.
340 *
341 * session= Set the CDROM session (default= last session)
342 * anchor= Override standard anchor location. (default= 256)
343 * volume= Override the VolumeDesc location. (unused)
344 * partition= Override the PartitionDesc location. (unused)
345 * lastblock= Set the last block of the filesystem/
346 *
347 * The following expect a offset from the partition root.
348 *
349 * fileset= Override the fileset block location. (unused)
350 * rootdir= Override the root directory location. (unused)
351 * WARNING: overriding the rootdir to a non-directory may
352 * yield highly unpredictable results.
353 *
354 * PRE-CONDITIONS
355 * options Pointer to mount options string.
356 * uopts Pointer to mount options variable.
357 *
358 * POST-CONDITIONS
359 * <return> 1 Mount options parsed okay.
360 * <return> 0 Error parsing mount options.
361 *
362 * HISTORY
363 * July 1, 1997 - Andrew E. Mileski
364 * Written, tested, and released.
365 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367enum {
368 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
369 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
370 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
371 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
372 Opt_rootdir, Opt_utf8, Opt_iocharset,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800373 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
376static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700377 {Opt_novrs, "novrs"},
378 {Opt_nostrict, "nostrict"},
379 {Opt_bs, "bs=%u"},
380 {Opt_unhide, "unhide"},
381 {Opt_undelete, "undelete"},
382 {Opt_noadinicb, "noadinicb"},
383 {Opt_adinicb, "adinicb"},
384 {Opt_shortad, "shortad"},
385 {Opt_longad, "longad"},
386 {Opt_uforget, "uid=forget"},
387 {Opt_uignore, "uid=ignore"},
388 {Opt_gforget, "gid=forget"},
389 {Opt_gignore, "gid=ignore"},
390 {Opt_gid, "gid=%u"},
391 {Opt_uid, "uid=%u"},
392 {Opt_umask, "umask=%o"},
393 {Opt_session, "session=%u"},
394 {Opt_lastblock, "lastblock=%u"},
395 {Opt_anchor, "anchor=%u"},
396 {Opt_volume, "volume=%u"},
397 {Opt_partition, "partition=%u"},
398 {Opt_fileset, "fileset=%u"},
399 {Opt_rootdir, "rootdir=%u"},
400 {Opt_utf8, "utf8"},
401 {Opt_iocharset, "iocharset=%s"},
402 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404
Miklos Szeredi6da80892008-02-08 04:21:50 -0800405static int udf_parse_options(char *options, struct udf_options *uopt,
406 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 char *p;
409 int option;
410
411 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800412 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 uopt->partition = 0xFFFF;
414 uopt->session = 0xFFFFFFFF;
415 uopt->lastblock = 0;
416 uopt->anchor = 0;
417 uopt->volume = 0xFFFFFFFF;
418 uopt->rootdir = 0xFFFFFFFF;
419 uopt->fileset = 0xFFFFFFFF;
420 uopt->nls_map = NULL;
421
422 if (!options)
423 return 1;
424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700425 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 substring_t args[MAX_OPT_ARGS];
427 int token;
428 if (!*p)
429 continue;
430
431 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 switch (token) {
433 case Opt_novrs:
434 uopt->novrs = 1;
435 case Opt_bs:
436 if (match_int(&args[0], &option))
437 return 0;
438 uopt->blocksize = option;
439 break;
440 case Opt_unhide:
441 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
442 break;
443 case Opt_undelete:
444 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
445 break;
446 case Opt_noadinicb:
447 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
448 break;
449 case Opt_adinicb:
450 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
451 break;
452 case Opt_shortad:
453 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
454 break;
455 case Opt_longad:
456 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
457 break;
458 case Opt_gid:
459 if (match_int(args, &option))
460 return 0;
461 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700462 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700463 break;
464 case Opt_uid:
465 if (match_int(args, &option))
466 return 0;
467 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700468 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700469 break;
470 case Opt_umask:
471 if (match_octal(args, &option))
472 return 0;
473 uopt->umask = option;
474 break;
475 case Opt_nostrict:
476 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
477 break;
478 case Opt_session:
479 if (match_int(args, &option))
480 return 0;
481 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800482 if (!remount)
483 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700484 break;
485 case Opt_lastblock:
486 if (match_int(args, &option))
487 return 0;
488 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800489 if (!remount)
490 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700491 break;
492 case Opt_anchor:
493 if (match_int(args, &option))
494 return 0;
495 uopt->anchor = option;
496 break;
497 case Opt_volume:
498 if (match_int(args, &option))
499 return 0;
500 uopt->volume = option;
501 break;
502 case Opt_partition:
503 if (match_int(args, &option))
504 return 0;
505 uopt->partition = option;
506 break;
507 case Opt_fileset:
508 if (match_int(args, &option))
509 return 0;
510 uopt->fileset = option;
511 break;
512 case Opt_rootdir:
513 if (match_int(args, &option))
514 return 0;
515 uopt->rootdir = option;
516 break;
517 case Opt_utf8:
518 uopt->flags |= (1 << UDF_FLAG_UTF8);
519 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 case Opt_iocharset:
522 uopt->nls_map = load_nls(args[0].from);
523 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700526 case Opt_uignore:
527 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
528 break;
529 case Opt_uforget:
530 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
531 break;
532 case Opt_gignore:
533 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
534 break;
535 case Opt_gforget:
536 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
537 break;
538 default:
539 printk(KERN_ERR "udf: bad mount option \"%s\" "
540 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542 }
543 }
544 return 1;
545}
546
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800547static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (!(sb->s_flags & MS_RDONLY))
552 udf_open_lvid(sb);
553 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 unlock_kernel();
556}
557
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700558static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800561 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800563 uopt.flags = sbi->s_flags;
564 uopt.uid = sbi->s_uid;
565 uopt.gid = sbi->s_gid;
566 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Miklos Szeredi6da80892008-02-08 04:21:50 -0800568 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800571 sbi->s_flags = uopt.flags;
572 sbi->s_uid = uopt.uid;
573 sbi->s_gid = uopt.gid;
574 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800576 if (sbi->s_lvid_bh) {
577 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (write_rev > UDF_MAX_WRITE_VERSION)
579 *flags |= MS_RDONLY;
580 }
581
582 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
583 return 0;
584 if (*flags & MS_RDONLY)
585 udf_close_lvid(sb);
586 else
587 udf_open_lvid(sb);
588
589 return 0;
590}
591
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700592static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct volStructDesc *vsd = NULL;
595 int sector = 32768;
596 int sectorsize;
597 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700598 int iso9660 = 0;
599 int nsr02 = 0;
600 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800601 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Block size must be a multiple of 512 */
604 if (sb->s_blocksize & 511)
605 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800606 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 if (sb->s_blocksize < sizeof(struct volStructDesc))
609 sectorsize = sizeof(struct volStructDesc);
610 else
611 sectorsize = sb->s_blocksize;
612
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800613 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700616 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700618 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Read a block */
620 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
621 if (!bh)
622 break;
623
624 /* Look for ISO descriptors */
625 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800626 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700628 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700629 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800631 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
632 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700634 switch (vsd->structType) {
635 case 0:
636 udf_debug("ISO9660 Boot Record found\n");
637 break;
638 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800639 udf_debug("ISO9660 Primary Volume Descriptor "
640 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 break;
642 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800643 udf_debug("ISO9660 Supplementary Volume "
644 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700645 break;
646 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800647 udf_debug("ISO9660 Volume Partition Descriptor "
648 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700649 break;
650 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800651 udf_debug("ISO9660 Volume Descriptor Set "
652 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700653 break;
654 default:
655 udf_debug("ISO9660 VRS (%u) found\n",
656 vsd->structType);
657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800659 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
660 VSD_STD_ID_LEN))
661 ; /* nothing */
662 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
663 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700664 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800666 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
667 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800669 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
670 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700672 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674
675 if (nsr03)
676 return nsr03;
677 else if (nsr02)
678 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800679 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -1;
681 else
682 return 0;
683}
684
685/*
686 * udf_find_anchor
687 *
688 * PURPOSE
689 * Find an anchor volume descriptor.
690 *
691 * PRE-CONDITIONS
692 * sb Pointer to _locked_ superblock.
693 * lastblock Last block on media.
694 *
695 * POST-CONDITIONS
696 * <return> 1 if not found, 0 if ok
697 *
698 * HISTORY
699 * July 1, 1997 - Andrew E. Mileski
700 * Written, tested, and released.
701 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700702static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800704 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct buffer_head *bh = NULL;
706 uint16_t ident;
707 uint32_t location;
708 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800709 struct udf_sb_info *sbi;
710
711 sbi = UDF_SB(sb);
712 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700714 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700716 int last[] = { lastblock, lastblock - 2,
717 lastblock - 150, lastblock - 152,
718 varlastblock, varlastblock - 2,
719 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 lastblock = 0;
722
723 /* Search for an anchor volume descriptor pointer */
724
725 /* according to spec, anchor is in either:
726 * block 256
727 * lastblock-256
728 * lastblock
729 * however, if the disc isn't closed, it could be 512 */
730
Tobias Klausere8c96f82006-03-24 03:15:34 -0800731 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800732 ident = location = 0;
733 if (last[i] >= 0) {
734 bh = sb_bread(sb, last[i]);
735 if (bh) {
736 tag *t = (tag *)bh->b_data;
737 ident = le16_to_cpu(t->tagIdent);
738 location = le32_to_cpu(t->tagLocation);
739 brelse(bh);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800742
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700743 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800744 if (location == last[i] - sbi->s_session) {
745 lastblock = last[i] - sbi->s_session;
746 sbi->s_anchor[0] = lastblock;
747 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800748 } else if (location ==
749 udf_variable_to_fixed(last[i]) -
750 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800752 lastblock =
753 udf_variable_to_fixed(last[i]) -
754 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800755 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800756 sbi->s_anchor[1] = lastblock - 256 -
757 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700758 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800759 udf_debug("Anchor found at block %d, "
760 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700761 last[i], location);
762 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800763 } else if (ident == TAG_IDENT_FE ||
764 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800766 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700767 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800768 ident = location = 0;
769 if (last[i] >= 256) {
770 bh = sb_bread(sb, last[i] - 256);
771 if (bh) {
772 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800773 ident = le16_to_cpu(
774 t->tagIdent);
775 location = le32_to_cpu(
776 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800777 brelse(bh);
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800782 location == last[i] - 256 -
783 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800785 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700786 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800787 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800788 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800789 bh = sb_bread(sb,
790 last[i] - 312 -
791 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800792 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800793 tag *t = (tag *)
794 bh->b_data;
795 ident = le16_to_cpu(
796 t->tagIdent);
797 location = le32_to_cpu(
798 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800799 brelse(bh);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700804 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800805 UDF_SET_FLAG(sb,
806 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800808 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810 }
811 }
812 }
813 }
814
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700815 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800816 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800817 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800818 if (bh) {
819 tag *t = (tag *)bh->b_data;
820 ident = le16_to_cpu(t->tagIdent);
821 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700822 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (ident == TAG_IDENT_AVDP && location == 256)
825 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
826 }
827 }
828
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800829 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
830 if (sbi->s_anchor[i]) {
831 bh = udf_read_tagged(sb, sbi->s_anchor[i],
832 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800833 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800834 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800835 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700836 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700837 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 (i || (ident != TAG_IDENT_FE &&
839 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800840 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842 }
843 }
844
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800845 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800848static int udf_find_fileset(struct super_block *sb,
849 kernel_lb_addr *fileset,
850 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct buffer_head *bh = NULL;
853 long lastblock;
854 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800855 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700858 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
860
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700861 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700863 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700864 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 1;
866 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800870 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800871 if (!bh) {
872 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 kernel_lb_addr newfileset;
874
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700875/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700877
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800878 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700879 (newfileset.partitionReferenceNum != 0xFFFF &&
880 fileset->logicalBlockNum == 0xFFFFFFFF &&
881 fileset->partitionReferenceNum == 0xFFFF);
882 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800883 lastblock = sbi->s_partmaps
884 [newfileset.partitionReferenceNum]
885 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 newfileset.logicalBlockNum = 0;
887
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700888 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800889 bh = udf_read_ptagged(sb, newfileset, 0,
890 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700891 if (!bh) {
892 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 continue;
894 }
895
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700896 switch (ident) {
897 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700898 {
899 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800900 sp = (struct spaceBitmapDesc *)
901 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700902 newfileset.logicalBlockNum += 1 +
903 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800904 sizeof(struct spaceBitmapDesc)
905 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700906 brelse(bh);
907 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700909 case TAG_IDENT_FSD:
910 *fileset = newfileset;
911 break;
912 default:
913 newfileset.logicalBlockNum++;
914 brelse(bh);
915 bh = NULL;
916 break;
917 }
918 } while (newfileset.logicalBlockNum < lastblock &&
919 fileset->logicalBlockNum == 0xFFFFFFFF &&
920 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 }
923
924 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700927 fileset->logicalBlockNum,
928 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800930 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700932 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return 0;
934 }
935 return 1;
936}
937
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700938static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct primaryVolDesc *pvoldesc;
941 time_t recording;
942 long recording_usec;
943 struct ustr instr;
944 struct ustr outstr;
945
946 pvoldesc = (struct primaryVolDesc *)bh->b_data;
947
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700948 if (udf_stamp_to_time(&recording, &recording_usec,
949 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 kernel_timestamp ts;
951 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800952 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
953 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700954 recording, recording_usec,
955 ts.year, ts.month, ts.day, ts.hour,
956 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800957 UDF_SB(sb)->s_record_time.tv_sec = recording;
958 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Marcin Slusarz4b111112008-02-08 04:20:36 -0800961 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700962 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800963 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800965 udf_debug("volIdent[] = '%s'\n",
966 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Marcin Slusarz4b111112008-02-08 04:20:36 -0800969 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 if (udf_CS0toUTF8(&outstr, &instr))
971 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700974static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
975 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct fileSetDesc *fset;
978
979 fset = (struct fileSetDesc *)bh->b_data;
980
981 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
982
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800983 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700985 udf_debug("Rootdir at block=%d, partition=%d\n",
986 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800989int udf_compute_nr_groups(struct super_block *sb, u32 partition)
990{
991 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100992 return DIV_ROUND_UP(map->s_partition_len +
993 (sizeof(struct spaceBitmapDesc) << 3),
994 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800995}
996
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800997static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
998{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800999 struct udf_bitmap *bitmap;
1000 int nr_groups;
1001 int size;
1002
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001003 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001004 size = sizeof(struct udf_bitmap) +
1005 (sizeof(struct buffer_head *) * nr_groups);
1006
1007 if (size <= PAGE_SIZE)
1008 bitmap = kmalloc(size, GFP_KERNEL);
1009 else
1010 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1011
1012 if (bitmap == NULL) {
1013 udf_error(sb, __FUNCTION__,
1014 "Unable to allocate space for bitmap "
1015 "and %d buffer_head pointers", nr_groups);
1016 return NULL;
1017 }
1018
1019 memset(bitmap, 0x00, size);
1020 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1021 bitmap->s_nr_groups = nr_groups;
1022 return bitmap;
1023}
1024
Jan Karabcec4472007-08-30 23:56:22 -07001025static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct partitionDesc *p;
1028 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001029 struct udf_part_map *map;
1030 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001033 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001035 for (i = 0; i < sbi->s_partitions; i++) {
1036 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001037 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001038 map->s_partition_num,
1039 le16_to_cpu(p->partitionNumber));
1040 if (map->s_partition_num ==
1041 le16_to_cpu(p->partitionNumber)) {
1042 map->s_partition_len =
1043 le32_to_cpu(p->partitionLength); /* blocks */
1044 map->s_partition_root =
1045 le32_to_cpu(p->partitionStartingLocation);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001046 if (p->accessType ==
1047 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001048 map->s_partition_flags |=
1049 UDF_PART_FLAG_READ_ONLY;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001050 if (p->accessType ==
1051 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001052 map->s_partition_flags |=
1053 UDF_PART_FLAG_WRITE_ONCE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001054 if (p->accessType ==
1055 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001056 map->s_partition_flags |=
1057 UDF_PART_FLAG_REWRITABLE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001058 if (p->accessType ==
1059 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001060 map->s_partition_flags |=
1061 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001063 if (!strcmp(p->partitionContents.ident,
1064 PD_PARTITION_CONTENTS_NSR02) ||
1065 !strcmp(p->partitionContents.ident,
1066 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct partitionHeaderDesc *phd;
1068
Marcin Slusarz4b111112008-02-08 04:20:36 -08001069 phd = (struct partitionHeaderDesc *)
1070 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001071 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001072 kernel_lb_addr loc = {
1073 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1074 .partitionReferenceNum = i,
1075 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001077 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001078 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001079 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001080 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001081 return 1;
1082 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001083 map->s_partition_flags |=
1084 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001085 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001086 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001088 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001089 struct udf_bitmap *bitmap =
1090 udf_sb_alloc_bitmap(sb, i);
1091 map->s_uspace.s_bitmap = bitmap;
1092 if (bitmap != NULL) {
1093 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001094 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001095 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001096 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001097 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001098 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001099 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101 }
1102 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001103 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001104 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001105 kernel_lb_addr loc = {
1106 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1107 .partitionReferenceNum = i,
1108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001110 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001111 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001112 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001113 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001114 return 1;
1115 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001116 map->s_partition_flags |=
1117 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001118 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001119 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001121 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001122 struct udf_bitmap *bitmap =
1123 udf_sb_alloc_bitmap(sb, i);
1124 map->s_fspace.s_bitmap = bitmap;
1125 if (bitmap != NULL) {
1126 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001127 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001128 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001129 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001130 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001131 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001132 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 }
1135 }
1136 break;
1137 }
1138 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001139 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001140 udf_debug("Partition (%d) not found in partition map\n",
1141 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001142 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001143 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1144 "block length %d\n",
1145 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001146 map->s_partition_type,
1147 map->s_partition_root,
1148 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001152static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1153 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct logicalVolDesc *lvd;
1156 int i, j, offset;
1157 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001158 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001159 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 lvd = (struct logicalVolDesc *)bh->b_data;
1162
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001163 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1164 if (i != 0)
1165 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001167 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001168 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001169 i++, offset += gpm->partitionMapLength) {
1170 struct udf_part_map *map = &sbi->s_partmaps[i];
1171 gpm = (struct genericPartitionMap *)
1172 &(lvd->partitionMaps[offset]);
1173 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001174 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001175 struct genericPartitionMap1 *gpm1 =
1176 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001177 map->s_partition_type = UDF_TYPE1_MAP15;
1178 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1179 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1180 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001181 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001182 struct udfPartitionMap2 *upm2 =
1183 (struct udfPartitionMap2 *)gpm;
1184 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1185 strlen(UDF_ID_VIRTUAL))) {
1186 u16 suf =
1187 le16_to_cpu(((__le16 *)upm2->partIdent.
1188 identSuffix)[0]);
1189 if (suf == 0x0150) {
1190 map->s_partition_type =
1191 UDF_VIRTUAL_MAP15;
1192 map->s_partition_func =
1193 udf_get_pblock_virt15;
1194 } else if (suf == 0x0200) {
1195 map->s_partition_type =
1196 UDF_VIRTUAL_MAP20;
1197 map->s_partition_func =
1198 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001200 } else if (!strncmp(upm2->partIdent.ident,
1201 UDF_ID_SPARABLE,
1202 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 uint32_t loc;
1204 uint16_t ident;
1205 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001206 struct sparablePartitionMap *spm =
1207 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001209 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001210 map->s_type_specific.s_sparing.s_packet_len =
1211 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001212 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001213 struct buffer_head *bh2;
1214
1215 loc = le32_to_cpu(
1216 spm->locSparingTable[j]);
1217 bh2 = udf_read_tagged(sb, loc, loc,
1218 &ident);
1219 map->s_type_specific.s_sparing.
1220 s_spar_map[j] = bh2;
1221
1222 if (bh2 != NULL) {
1223 st = (struct sparingTable *)
1224 bh2->b_data;
1225 if (ident != 0 || strncmp(
1226 st->sparingIdent.ident,
1227 UDF_ID_SPARING,
1228 strlen(UDF_ID_SPARING))) {
1229 brelse(bh2);
1230 map->s_type_specific.
1231 s_sparing.
1232 s_spar_map[j] =
1233 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 }
1236 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001237 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001238 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001239 udf_debug("Unknown ident: %s\n",
1240 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 continue;
1242 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001243 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1244 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
1246 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001247 i, map->s_partition_num, type,
1248 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001251 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001252 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001255 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1256 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001257 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259 if (lvd->integritySeqExt.extLength)
1260 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return 0;
1263}
1264
1265/*
1266 * udf_load_logicalvolint
1267 *
1268 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001269static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct buffer_head *bh = NULL;
1272 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001273 struct udf_sb_info *sbi = UDF_SB(sb);
1274 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001277 (bh = udf_read_tagged(sb, loc.extLocation,
1278 loc.extLocation, &ident)) &&
1279 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001280 sbi->s_lvid_bh = bh;
1281 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001282
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001283 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001284 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001285 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001286
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001287 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001288 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001290 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001292 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001293 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
1296/*
1297 * udf_process_sequence
1298 *
1299 * PURPOSE
1300 * Process a main/reserve volume descriptor sequence.
1301 *
1302 * PRE-CONDITIONS
1303 * sb Pointer to _locked_ superblock.
1304 * block First block of first extent of the sequence.
1305 * lastblock Lastblock of first extent of the sequence.
1306 *
1307 * HISTORY
1308 * July 1, 1997 - Andrew E. Mileski
1309 * Written, tested, and released.
1310 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001311static int udf_process_sequence(struct super_block *sb, long block,
1312 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct buffer_head *bh = NULL;
1315 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001316 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct generic_desc *gd;
1318 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001319 int done = 0;
1320 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 uint32_t vdsn;
1322 uint16_t ident;
1323 long next_s = 0, next_e = 0;
1324
1325 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1326
1327 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001328 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001331 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
1333
1334 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1335 gd = (struct generic_desc *)bh->b_data;
1336 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001337 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001338 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001339 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1340 if (vdsn >= curr->volDescSeqNum) {
1341 curr->volDescSeqNum = vdsn;
1342 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001343 }
1344 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001345 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001346 curr = &vds[VDS_POS_VOL_DESC_PTR];
1347 if (vdsn >= curr->volDescSeqNum) {
1348 curr->volDescSeqNum = vdsn;
1349 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001351 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001352 next_s = le32_to_cpu(
1353 vdp->nextVolDescSeqExt.extLocation);
1354 next_e = le32_to_cpu(
1355 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001356 next_e = next_e >> sb->s_blocksize_bits;
1357 next_e += next_s;
1358 }
1359 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001360 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001361 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1362 if (vdsn >= curr->volDescSeqNum) {
1363 curr->volDescSeqNum = vdsn;
1364 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001365 }
1366 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001367 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001368 curr = &vds[VDS_POS_PARTITION_DESC];
1369 if (!curr->block)
1370 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001371 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001372 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001373 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1374 if (vdsn >= curr->volDescSeqNum) {
1375 curr->volDescSeqNum = vdsn;
1376 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001377 }
1378 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001379 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001380 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1381 if (vdsn >= curr->volDescSeqNum) {
1382 curr->volDescSeqNum = vdsn;
1383 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001384 }
1385 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001386 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001387 vds[VDS_POS_TERMINATING_DESC].block = block;
1388 if (next_e) {
1389 block = next_s;
1390 lastblock = next_e;
1391 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001392 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001393 done = 1;
1394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001396 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001398 for (i = 0; i < VDS_POS_LENGTH; i++) {
1399 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001400 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1401 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001403 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001405 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001406 if (udf_load_logicalvol(sb, bh, fileset)) {
1407 brelse(bh);
1408 return 1;
1409 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001410 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001412 if (udf_load_partdesc(sb, bh)) {
1413 brelse(bh);
1414 return 1;
1415 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001416 for (j = vds[i].block + 1;
1417 j < vds[VDS_POS_TERMINATING_DESC].block;
1418 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 bh2 = udf_read_tagged(sb, j, j, &ident);
1420 gd = (struct generic_desc *)bh2->b_data;
1421 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001422 if (udf_load_partdesc(sb,
1423 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001424 brelse(bh);
1425 brelse(bh2);
1426 return 1;
1427 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001428 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001431 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
1434
1435 return 0;
1436}
1437
1438/*
1439 * udf_check_valid()
1440 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001441static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 long block;
1444
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001445 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 udf_debug("Validity check skipped because of novrs option\n");
1447 return 0;
1448 }
1449 /* Check that it is NSR02 compliant */
1450 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001451 else {
1452 block = udf_vrs(sb, silent);
1453 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001454 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001455 udf_debug("Failed to read byte 32768. Assuming open "
1456 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001457 if (!sbi->s_last_block)
1458 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001459 return 0;
1460 } else
1461 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001465static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct anchorVolDescPtr *anchor;
1468 uint16_t ident;
1469 struct buffer_head *bh;
1470 long main_s, main_e, reserve_s, reserve_e;
1471 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001472 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (!sb)
1475 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001476 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001478 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001479 if (!sbi->s_anchor[i])
1480 continue;
1481 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1482 &ident);
1483 if (!bh)
1484 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001486 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001487
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001488 /* Locate the main sequence */
1489 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1490 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1491 main_e = main_e >> sb->s_blocksize_bits;
1492 main_e += main_s;
1493
1494 /* Locate the reserve sequence */
1495 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001496 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001497 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001498 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001499 reserve_e = reserve_e >> sb->s_blocksize_bits;
1500 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001502 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001504 /* Process the main & reserve sequences */
1505 /* responsible for finding the PartitionDesc(s) */
1506 if (!(udf_process_sequence(sb, main_s, main_e,
1507 fileset) &&
1508 udf_process_sequence(sb, reserve_s, reserve_e,
1509 fileset)))
1510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001513 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 udf_debug("No Anchor block found\n");
1515 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001516 }
1517 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001519 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001520 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001521 struct udf_part_map *map = &sbi->s_partmaps[i];
1522 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001523 case UDF_VIRTUAL_MAP15:
1524 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001525 if (!sbi->s_last_block) {
1526 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001527 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001529
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001530 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001531 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001532 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001533 return 1;
1534 }
1535
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001536 for (j = 0; j < sbi->s_partitions; j++) {
1537 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b12007-10-16 23:30:08 -07001538 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001539 map->s_volumeseqnum ==
1540 map2->s_volumeseqnum &&
1541 map->s_partition_num ==
1542 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001543 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001544 ino.logicalBlockNum =
1545 sbi->s_last_block -
1546 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001547 break;
1548 }
1549 }
1550
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001551 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001552 return 1;
1553
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001554 sbi->s_vat_inode = udf_iget(sb, ino);
1555 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001556 return 1;
1557
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001558 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1559 map->s_type_specific.s_virtual.s_start_offset =
1560 udf_ext0_offset(sbi->s_vat_inode);
1561 map->s_type_specific.s_virtual.s_num_entries =
1562 (sbi->s_vat_inode->i_size - 36) >> 2;
1563 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001564 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001565 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001566
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001567 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001568 bh = sb_bread(sb, pos);
1569 if (!bh)
1570 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001571 vat20 = (struct virtualAllocationTable20 *)
1572 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001573 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001574 map->s_type_specific.s_virtual.s_start_offset =
1575 le16_to_cpu(vat20->lengthHeader) +
1576 udf_ext0_offset(sbi->s_vat_inode);
1577 map->s_type_specific.s_virtual.s_num_entries =
1578 (sbi->s_vat_inode->i_size -
1579 map->s_type_specific.s_virtual.
1580 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001581 brelse(bh);
1582 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001583 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1584 map->s_partition_len =
1585 sbi->s_partmaps[ino.partitionReferenceNum].
1586 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 }
1589 return 0;
1590}
1591
1592static void udf_open_lvid(struct super_block *sb)
1593{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001594 struct udf_sb_info *sbi = UDF_SB(sb);
1595 struct buffer_head *bh = sbi->s_lvid_bh;
1596 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001598 struct logicalVolIntegrityDesc *lvid =
1599 (struct logicalVolIntegrityDesc *)bh->b_data;
1600 struct logicalVolIntegrityDescImpUse *lvidiu =
1601 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001603 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1604 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001606 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1607 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Marcin Slusarz4b111112008-02-08 04:20:36 -08001609 lvid->descTag.descCRC = cpu_to_le16(
1610 udf_crc((char *)lvid + sizeof(tag),
1611 le16_to_cpu(lvid->descTag.descCRCLength),
1612 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001614 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001615 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617}
1618
1619static void udf_close_lvid(struct super_block *sb)
1620{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001621 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001622 struct udf_sb_info *sbi = UDF_SB(sb);
1623 struct buffer_head *bh = sbi->s_lvid_bh;
1624 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001625
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001626 if (!bh)
1627 return;
1628
1629 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1630
1631 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001632 struct logicalVolIntegrityDescImpUse *lvidiu =
1633 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001634 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1635 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001637 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1638 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001639 lvidiu->maxUDFWriteRev =
1640 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001641 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1642 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1643 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1644 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1645 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Marcin Slusarz4b111112008-02-08 04:20:36 -08001647 lvid->descTag.descCRC = cpu_to_le16(
1648 udf_crc((char *)lvid + sizeof(tag),
1649 le16_to_cpu(lvid->descTag.descCRCLength),
1650 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001652 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001653 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655}
1656
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001657static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1658{
1659 int i;
1660 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001661 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1662 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001663
1664 for (i = 0; i < nr_groups; i++)
1665 if (bitmap->s_block_bitmap[i])
1666 brelse(bitmap->s_block_bitmap[i]);
1667
1668 if (size <= PAGE_SIZE)
1669 kfree(bitmap);
1670 else
1671 vfree(bitmap);
1672}
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674static int udf_fill_super(struct super_block *sb, void *options, int silent)
1675{
1676 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001677 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct udf_options uopt;
1679 kernel_lb_addr rootdir, fileset;
1680 struct udf_sb_info *sbi;
1681
1682 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1683 uopt.uid = -1;
1684 uopt.gid = -1;
1685 uopt.umask = 0;
1686
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001687 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!sbi)
1689 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001693 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Miklos Szeredi6da80892008-02-08 04:21:50 -08001695 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto error_out;
1697
1698 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001699 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001701 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 goto error_out;
1703 }
1704#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001705 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 uopt.nls_map = load_nls_default();
1707 if (!uopt.nls_map)
1708 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1709 else
1710 udf_debug("Using default NLS map\n");
1711 }
1712#endif
1713 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1714 uopt.flags |= (1 << UDF_FLAG_UTF8);
1715
1716 fileset.logicalBlockNum = 0xFFFFFFFF;
1717 fileset.partitionReferenceNum = 0xFFFF;
1718
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001719 sbi->s_flags = uopt.flags;
1720 sbi->s_uid = uopt.uid;
1721 sbi->s_gid = uopt.gid;
1722 sbi->s_umask = uopt.umask;
1723 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001726 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1727 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1728 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001732 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001733 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001735 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001737 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001739 sbi->s_last_block = uopt.lastblock;
1740 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1741 sbi->s_anchor[2] = uopt.anchor;
1742 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001744 if (udf_check_valid(sb, uopt.novrs, silent)) {
1745 /* read volume recognition sequences */
1746 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001747 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750 udf_find_anchor(sb);
1751
1752 /* Fill in the rest of the superblock */
1753 sb->s_op = &udf_sb_ops;
1754 sb->dq_op = NULL;
1755 sb->s_dirt = 0;
1756 sb->s_magic = UDF_SUPER_MAGIC;
1757 sb->s_time_gran = 1000;
1758
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001759 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001760 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto error_out;
1762 }
1763
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001764 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001766 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001767 struct logicalVolIntegrityDescImpUse *lvidiu =
1768 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001769 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1770 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001771 /* uint16_t maxUDFWriteRev =
1772 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001774 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001775 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1776 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001777 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001778 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001780 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001783 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1786 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1787 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1788 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1789 }
1790
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001791 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001792 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 goto error_out;
1794 }
1795
Marcin Slusarz4b111112008-02-08 04:20:36 -08001796 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1797 UDF_PART_FLAG_READ_ONLY) {
1798 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1799 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001800 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001801 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001802
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001803 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001804 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto error_out;
1806 }
1807
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001808 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001810 udf_time_to_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001811 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001812 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001813 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1814 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816 if (!(sb->s_flags & MS_RDONLY))
1817 udf_open_lvid(sb);
1818
1819 /* Assign the root inode */
1820 /* assign inodes by physical block number */
1821 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001822 inode = udf_iget(sb, rootdir);
1823 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001824 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1825 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001826 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto error_out;
1828 }
1829
1830 /* Allocate a dentry for the root inode */
1831 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001832 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001833 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 iput(inode);
1835 goto error_out;
1836 }
Jan Kara31170b62007-05-08 00:35:21 -07001837 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return 0;
1839
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001840error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001841 if (sbi->s_vat_inode)
1842 iput(sbi->s_vat_inode);
1843 if (sbi->s_partitions) {
1844 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1845 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1846 iput(map->s_uspace.s_table);
1847 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1848 iput(map->s_fspace.s_table);
1849 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001850 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001851 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001852 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001853 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001854 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001855 brelse(map->s_type_specific.s_sparing.
1856 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 }
1858#ifdef CONFIG_UDF_NLS
1859 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001860 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861#endif
1862 if (!(sb->s_flags & MS_RDONLY))
1863 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001864 brelse(sbi->s_lvid_bh);
1865
1866 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 kfree(sbi);
1868 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 return -EINVAL;
1871}
1872
Adrian Bunkb8145a72008-02-17 10:19:55 +02001873static void udf_error(struct super_block *sb, const char *function,
1874 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 va_list args;
1877
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001878 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 /* mark sb error */
1880 sb->s_dirt = 1;
1881 }
1882 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001883 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001885 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001886 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001890 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 va_list args;
1893
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001894 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001895 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 va_end(args);
1897 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001898 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001901static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001904 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001906 sbi = UDF_SB(sb);
1907 if (sbi->s_vat_inode)
1908 iput(sbi->s_vat_inode);
1909 if (sbi->s_partitions) {
1910 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1911 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1912 iput(map->s_uspace.s_table);
1913 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1914 iput(map->s_fspace.s_table);
1915 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001916 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001917 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001918 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001919 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001920 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001921 brelse(map->s_type_specific.s_sparing.
1922 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924#ifdef CONFIG_UDF_NLS
1925 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001926 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927#endif
1928 if (!(sb->s_flags & MS_RDONLY))
1929 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001930 brelse(sbi->s_lvid_bh);
1931 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 kfree(sb->s_fs_info);
1933 sb->s_fs_info = NULL;
1934}
1935
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001936static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
David Howells726c3342006-06-23 02:02:58 -07001938 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001939 struct udf_sb_info *sbi = UDF_SB(sb);
1940 struct logicalVolIntegrityDescImpUse *lvidiu;
1941
1942 if (sbi->s_lvid_bh != NULL)
1943 lvidiu = udf_sb_lvidiu(sbi);
1944 else
1945 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 buf->f_type = UDF_SUPER_MAGIC;
1948 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001949 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 buf->f_bfree = udf_count_free(sb);
1951 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001952 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1953 le32_to_cpu(lvidiu->numDirs)) : 0)
1954 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 buf->f_ffree = buf->f_bfree;
1956 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001957 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 return 0;
1960}
1961
Marcin Slusarz4b111112008-02-08 04:20:36 -08001962static unsigned int udf_count_free_bitmap(struct super_block *sb,
1963 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct buffer_head *bh = NULL;
1966 unsigned int accum = 0;
1967 int index;
1968 int block = 0, newblock;
1969 kernel_lb_addr loc;
1970 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 uint8_t *ptr;
1972 uint16_t ident;
1973 struct spaceBitmapDesc *bm;
1974
1975 lock_kernel();
1976
1977 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001978 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 bh = udf_read_ptagged(sb, loc, 0, &ident);
1980
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001981 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 printk(KERN_ERR "udf: udf_count_free failed\n");
1983 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001984 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07001985 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 printk(KERN_ERR "udf: udf_count_free failed\n");
1987 goto out;
1988 }
1989
1990 bm = (struct spaceBitmapDesc *)bh->b_data;
1991 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001992 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
1993 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001995 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01001996 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
1997 accum += bitmap_weight((const unsigned long *)(ptr + index),
1998 cur_bytes * 8);
1999 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002000 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002001 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 newblock = udf_get_lb_pblock(sb, loc, ++block);
2003 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002004 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 udf_debug("read failed\n");
2006 goto out;
2007 }
2008 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002009 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002012 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002014out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 unlock_kernel();
2016
2017 return accum;
2018}
2019
Marcin Slusarz4b111112008-02-08 04:20:36 -08002020static unsigned int udf_count_free_table(struct super_block *sb,
2021 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002024 uint32_t elen;
2025 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002027 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 lock_kernel();
2030
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002031 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002032 epos.offset = sizeof(struct unallocSpaceEntry);
2033 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002035 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002037
Jan Kara3bf25cb2007-05-08 00:35:16 -07002038 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 unlock_kernel();
2041
2042 return accum;
2043}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002044
2045static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002048 struct udf_sb_info *sbi;
2049 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002051 sbi = UDF_SB(sb);
2052 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002053 struct logicalVolIntegrityDesc *lvid =
2054 (struct logicalVolIntegrityDesc *)
2055 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002056 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002057 accum = le32_to_cpu(
2058 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (accum == 0xFFFFFFFF)
2060 accum = 0;
2061 }
2062 }
2063
2064 if (accum)
2065 return accum;
2066
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002067 map = &sbi->s_partmaps[sbi->s_partition];
2068 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002069 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002070 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002072 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002073 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002074 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 if (accum)
2077 return accum;
2078
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002079 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002080 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002081 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002083 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002084 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002085 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087
2088 return accum;
2089}