]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/udf/super.c
udf: Promote some debugging messages to udf_error
[mirror_ubuntu-artful-kernel.git] / fs / udf / super.c
CommitLineData
1da177e4
LT
1/*
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 *
1da177e4
LT
17 * 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
3a71fc5d
MS
36 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
1da177e4
LT
38 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
cb00ea35 41#include "udfdecl.h"
1da177e4 42
1da177e4
LT
43#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>
1da177e4
LT
51#include <linux/buffer_head.h>
52#include <linux/vfs.h>
53#include <linux/vmalloc.h>
dc5d39be 54#include <linux/errno.h>
6da80894
MS
55#include <linux/mount.h>
56#include <linux/seq_file.h>
01b954a3 57#include <linux/bitmap.h>
f845fced 58#include <linux/crc-itu-t.h>
1da177e4
LT
59#include <asm/byteorder.h>
60
1da177e4
LT
61#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
6da80894
MS
76#define UDF_DEFAULT_BLOCKSIZE 2048
77
1da177e4
LT
78static 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 *);
146bca72 83static int udf_sync_fs(struct super_block *, int);
1da177e4 84static int udf_remount_fs(struct super_block *, int *, char *);
5ca4e4be 85static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
5ca4e4be
PE
86static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
87 struct kernel_lb_addr *);
cb00ea35 88static void udf_load_fileset(struct super_block *, struct buffer_head *,
5ca4e4be 89 struct kernel_lb_addr *);
1da177e4
LT
90static void udf_open_lvid(struct super_block *);
91static void udf_close_lvid(struct super_block *);
92static unsigned int udf_count_free(struct super_block *);
726c3342 93static int udf_statfs(struct dentry *, struct kstatfs *);
6da80894 94static int udf_show_options(struct seq_file *, struct vfsmount *);
1da177e4 95
6c79e987
MS
96struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
97{
4b11111a
MS
98 struct logicalVolIntegrityDesc *lvid =
99 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
6c79e987 100 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
4b11111a
MS
101 __u32 offset = number_of_partitions * 2 *
102 sizeof(uint32_t)/sizeof(uint8_t);
6c79e987
MS
103 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
104}
105
1da177e4 106/* UDF filesystem type */
152a0836
AV
107static struct dentry *udf_mount(struct file_system_type *fs_type,
108 int flags, const char *dev_name, void *data)
1da177e4 109{
152a0836 110 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
1da177e4
LT
111}
112
113static struct file_system_type udf_fstype = {
28de7948
CG
114 .owner = THIS_MODULE,
115 .name = "udf",
152a0836 116 .mount = udf_mount,
28de7948
CG
117 .kill_sb = kill_block_super,
118 .fs_flags = FS_REQUIRES_DEV,
1da177e4
LT
119};
120
cb00ea35 121static struct kmem_cache *udf_inode_cachep;
1da177e4
LT
122
123static struct inode *udf_alloc_inode(struct super_block *sb)
124{
125 struct udf_inode_info *ei;
3a71fc5d 126 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
1da177e4
LT
127 if (!ei)
128 return NULL;
95f8797f
DB
129
130 ei->i_unique = 0;
131 ei->i_lenExtents = 0;
132 ei->i_next_alloc_block = 0;
133 ei->i_next_alloc_goal = 0;
134 ei->i_strat4096 = 0;
4d0fb621 135 init_rwsem(&ei->i_data_sem);
95f8797f 136
1da177e4
LT
137 return &ei->vfs_inode;
138}
139
fa0d7e3d 140static void udf_i_callback(struct rcu_head *head)
1da177e4 141{
fa0d7e3d
NP
142 struct inode *inode = container_of(head, struct inode, i_rcu);
143 INIT_LIST_HEAD(&inode->i_dentry);
1da177e4
LT
144 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
145}
146
fa0d7e3d
NP
147static void udf_destroy_inode(struct inode *inode)
148{
149 call_rcu(&inode->i_rcu, udf_i_callback);
150}
151
51cc5068 152static void init_once(void *foo)
1da177e4 153{
cb00ea35 154 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
1da177e4 155
a35afb83
CL
156 ei->i_ext.i_data = NULL;
157 inode_init_once(&ei->vfs_inode);
1da177e4
LT
158}
159
160static int init_inodecache(void)
161{
162 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
163 sizeof(struct udf_inode_info),
cb00ea35
CG
164 0, (SLAB_RECLAIM_ACCOUNT |
165 SLAB_MEM_SPREAD),
20c2df83 166 init_once);
28de7948 167 if (!udf_inode_cachep)
1da177e4
LT
168 return -ENOMEM;
169 return 0;
170}
171
172static void destroy_inodecache(void)
173{
1a1d92c1 174 kmem_cache_destroy(udf_inode_cachep);
1da177e4
LT
175}
176
177/* Superblock operations */
ee9b6d61 178static const struct super_operations udf_sb_ops = {
28de7948
CG
179 .alloc_inode = udf_alloc_inode,
180 .destroy_inode = udf_destroy_inode,
181 .write_inode = udf_write_inode,
3aac2b62 182 .evict_inode = udf_evict_inode,
28de7948 183 .put_super = udf_put_super,
146bca72 184 .sync_fs = udf_sync_fs,
28de7948
CG
185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
6da80894 187 .show_options = udf_show_options,
1da177e4
LT
188};
189
cb00ea35 190struct udf_options {
1da177e4
LT
191 unsigned char novrs;
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
195 unsigned int anchor;
196 unsigned int volume;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
200 unsigned int flags;
201 mode_t umask;
202 gid_t gid;
203 uid_t uid;
7ac9bcd5
MS
204 mode_t fmode;
205 mode_t dmode;
1da177e4
LT
206 struct nls_table *nls_map;
207};
208
209static int __init init_udf_fs(void)
210{
211 int err;
28de7948 212
1da177e4
LT
213 err = init_inodecache();
214 if (err)
215 goto out1;
216 err = register_filesystem(&udf_fstype);
217 if (err)
218 goto out;
28de7948 219
1da177e4 220 return 0;
28de7948
CG
221
222out:
1da177e4 223 destroy_inodecache();
28de7948
CG
224
225out1:
1da177e4
LT
226 return err;
227}
228
229static void __exit exit_udf_fs(void)
230{
231 unregister_filesystem(&udf_fstype);
232 destroy_inodecache();
233}
234
235module_init(init_udf_fs)
28de7948 236module_exit(exit_udf_fs)
1da177e4 237
dc5d39be
MS
238static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
239{
240 struct udf_sb_info *sbi = UDF_SB(sb);
241
242 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
243 GFP_KERNEL);
244 if (!sbi->s_partmaps) {
8e24eea7 245 udf_error(sb, __func__,
dc5d39be
MS
246 "Unable to allocate space for %d partition maps",
247 count);
248 sbi->s_partitions = 0;
249 return -ENOMEM;
250 }
251
252 sbi->s_partitions = count;
253 return 0;
254}
255
6da80894
MS
256static int udf_show_options(struct seq_file *seq, struct vfsmount *mnt)
257{
258 struct super_block *sb = mnt->mnt_sb;
259 struct udf_sb_info *sbi = UDF_SB(sb);
260
261 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
262 seq_puts(seq, ",nostrict");
1197e4df 263 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
6da80894
MS
264 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
265 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
266 seq_puts(seq, ",unhide");
267 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
268 seq_puts(seq, ",undelete");
269 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
270 seq_puts(seq, ",noadinicb");
271 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
272 seq_puts(seq, ",shortad");
273 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
274 seq_puts(seq, ",uid=forget");
275 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
276 seq_puts(seq, ",uid=ignore");
277 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
278 seq_puts(seq, ",gid=forget");
279 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
280 seq_puts(seq, ",gid=ignore");
281 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
282 seq_printf(seq, ",uid=%u", sbi->s_uid);
283 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
284 seq_printf(seq, ",gid=%u", sbi->s_gid);
285 if (sbi->s_umask != 0)
286 seq_printf(seq, ",umask=%o", sbi->s_umask);
87bc730c 287 if (sbi->s_fmode != UDF_INVALID_MODE)
7ac9bcd5 288 seq_printf(seq, ",mode=%o", sbi->s_fmode);
87bc730c 289 if (sbi->s_dmode != UDF_INVALID_MODE)
7ac9bcd5 290 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
6da80894
MS
291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
292 seq_printf(seq, ",session=%u", sbi->s_session);
293 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
294 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
40346005
JK
295 if (sbi->s_anchor != 0)
296 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
6da80894
MS
297 /*
298 * volume, partition, fileset and rootdir seem to be ignored
299 * currently
300 */
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
302 seq_puts(seq, ",utf8");
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
304 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
305
306 return 0;
307}
308
1da177e4
LT
309/*
310 * udf_parse_options
311 *
312 * PURPOSE
313 * Parse mount options.
314 *
315 * DESCRIPTION
316 * The following mount options are supported:
317 *
318 * gid= Set the default group.
319 * umask= Set the default umask.
7ac9bcd5
MS
320 * mode= Set the default file permissions.
321 * dmode= Set the default directory permissions.
1da177e4
LT
322 * uid= Set the default user.
323 * bs= Set the block size.
324 * unhide Show otherwise hidden files.
325 * undelete Show deleted files in lists.
326 * adinicb Embed data in the inode (default)
327 * noadinicb Don't embed data in the inode
328 * shortad Use short ad's
329 * longad Use long ad's (default)
330 * nostrict Unset strict conformance
331 * iocharset= Set the NLS character set
332 *
333 * The remaining are for debugging and disaster recovery:
334 *
28de7948 335 * novrs Skip volume sequence recognition
1da177e4
LT
336 *
337 * The following expect a offset from 0.
338 *
339 * session= Set the CDROM session (default= last session)
340 * anchor= Override standard anchor location. (default= 256)
341 * volume= Override the VolumeDesc location. (unused)
342 * partition= Override the PartitionDesc location. (unused)
343 * lastblock= Set the last block of the filesystem/
344 *
345 * The following expect a offset from the partition root.
346 *
347 * fileset= Override the fileset block location. (unused)
348 * rootdir= Override the root directory location. (unused)
349 * WARNING: overriding the rootdir to a non-directory may
350 * yield highly unpredictable results.
351 *
352 * PRE-CONDITIONS
353 * options Pointer to mount options string.
354 * uopts Pointer to mount options variable.
355 *
356 * POST-CONDITIONS
357 * <return> 1 Mount options parsed okay.
358 * <return> 0 Error parsing mount options.
359 *
360 * HISTORY
361 * July 1, 1997 - Andrew E. Mileski
362 * Written, tested, and released.
363 */
28de7948 364
1da177e4
LT
365enum {
366 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
367 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
368 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
369 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
370 Opt_rootdir, Opt_utf8, Opt_iocharset,
7ac9bcd5
MS
371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
372 Opt_fmode, Opt_dmode
1da177e4
LT
373};
374
a447c093 375static const match_table_t tokens = {
28de7948
CG
376 {Opt_novrs, "novrs"},
377 {Opt_nostrict, "nostrict"},
378 {Opt_bs, "bs=%u"},
379 {Opt_unhide, "unhide"},
380 {Opt_undelete, "undelete"},
381 {Opt_noadinicb, "noadinicb"},
382 {Opt_adinicb, "adinicb"},
383 {Opt_shortad, "shortad"},
384 {Opt_longad, "longad"},
385 {Opt_uforget, "uid=forget"},
386 {Opt_uignore, "uid=ignore"},
387 {Opt_gforget, "gid=forget"},
388 {Opt_gignore, "gid=ignore"},
389 {Opt_gid, "gid=%u"},
390 {Opt_uid, "uid=%u"},
391 {Opt_umask, "umask=%o"},
392 {Opt_session, "session=%u"},
393 {Opt_lastblock, "lastblock=%u"},
394 {Opt_anchor, "anchor=%u"},
395 {Opt_volume, "volume=%u"},
396 {Opt_partition, "partition=%u"},
397 {Opt_fileset, "fileset=%u"},
398 {Opt_rootdir, "rootdir=%u"},
399 {Opt_utf8, "utf8"},
400 {Opt_iocharset, "iocharset=%s"},
7ac9bcd5
MS
401 {Opt_fmode, "mode=%o"},
402 {Opt_dmode, "dmode=%o"},
28de7948 403 {Opt_err, NULL}
1da177e4
LT
404};
405
6da80894
MS
406static int udf_parse_options(char *options, struct udf_options *uopt,
407 bool remount)
1da177e4
LT
408{
409 char *p;
410 int option;
411
412 uopt->novrs = 0;
1da177e4
LT
413 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
cb00ea35 425 while ((p = strsep(&options, ",")) != NULL) {
1da177e4
LT
426 substring_t args[MAX_OPT_ARGS];
427 int token;
428 if (!*p)
429 continue;
430
431 token = match_token(p, tokens, args);
cb00ea35
CG
432 switch (token) {
433 case Opt_novrs:
434 uopt->novrs = 1;
4136801a 435 break;
cb00ea35
CG
436 case Opt_bs:
437 if (match_int(&args[0], &option))
438 return 0;
439 uopt->blocksize = option;
1197e4df 440 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
cb00ea35
CG
441 break;
442 case Opt_unhide:
443 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
444 break;
445 case Opt_undelete:
446 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
447 break;
448 case Opt_noadinicb:
449 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
450 break;
451 case Opt_adinicb:
452 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
453 break;
454 case Opt_shortad:
455 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
456 break;
457 case Opt_longad:
458 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
459 break;
460 case Opt_gid:
461 if (match_int(args, &option))
462 return 0;
463 uopt->gid = option;
ca76d2d8 464 uopt->flags |= (1 << UDF_FLAG_GID_SET);
cb00ea35
CG
465 break;
466 case Opt_uid:
467 if (match_int(args, &option))
468 return 0;
469 uopt->uid = option;
ca76d2d8 470 uopt->flags |= (1 << UDF_FLAG_UID_SET);
cb00ea35
CG
471 break;
472 case Opt_umask:
473 if (match_octal(args, &option))
474 return 0;
475 uopt->umask = option;
476 break;
477 case Opt_nostrict:
478 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
479 break;
480 case Opt_session:
481 if (match_int(args, &option))
482 return 0;
483 uopt->session = option;
6da80894
MS
484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
cb00ea35
CG
486 break;
487 case Opt_lastblock:
488 if (match_int(args, &option))
489 return 0;
490 uopt->lastblock = option;
6da80894
MS
491 if (!remount)
492 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
cb00ea35
CG
493 break;
494 case Opt_anchor:
495 if (match_int(args, &option))
496 return 0;
497 uopt->anchor = option;
498 break;
499 case Opt_volume:
500 if (match_int(args, &option))
501 return 0;
502 uopt->volume = option;
503 break;
504 case Opt_partition:
505 if (match_int(args, &option))
506 return 0;
507 uopt->partition = option;
508 break;
509 case Opt_fileset:
510 if (match_int(args, &option))
511 return 0;
512 uopt->fileset = option;
513 break;
514 case Opt_rootdir:
515 if (match_int(args, &option))
516 return 0;
517 uopt->rootdir = option;
518 break;
519 case Opt_utf8:
520 uopt->flags |= (1 << UDF_FLAG_UTF8);
521 break;
1da177e4 522#ifdef CONFIG_UDF_NLS
cb00ea35
CG
523 case Opt_iocharset:
524 uopt->nls_map = load_nls(args[0].from);
525 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
526 break;
1da177e4 527#endif
cb00ea35
CG
528 case Opt_uignore:
529 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
530 break;
531 case Opt_uforget:
532 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
533 break;
534 case Opt_gignore:
535 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
536 break;
537 case Opt_gforget:
538 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
539 break;
7ac9bcd5
MS
540 case Opt_fmode:
541 if (match_octal(args, &option))
542 return 0;
543 uopt->fmode = option & 0777;
544 break;
545 case Opt_dmode:
546 if (match_octal(args, &option))
547 return 0;
548 uopt->dmode = option & 0777;
549 break;
cb00ea35
CG
550 default:
551 printk(KERN_ERR "udf: bad mount option \"%s\" "
552 "or missing value\n", p);
1da177e4
LT
553 return 0;
554 }
555 }
556 return 1;
557}
558
cb00ea35 559static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
1da177e4
LT
560{
561 struct udf_options uopt;
6c79e987 562 struct udf_sb_info *sbi = UDF_SB(sb);
c79d967d 563 int error = 0;
1da177e4 564
6c79e987
MS
565 uopt.flags = sbi->s_flags;
566 uopt.uid = sbi->s_uid;
567 uopt.gid = sbi->s_gid;
568 uopt.umask = sbi->s_umask;
7ac9bcd5
MS
569 uopt.fmode = sbi->s_fmode;
570 uopt.dmode = sbi->s_dmode;
1da177e4 571
6da80894 572 if (!udf_parse_options(options, &uopt, true))
1da177e4
LT
573 return -EINVAL;
574
c03cad24 575 write_lock(&sbi->s_cred_lock);
6c79e987
MS
576 sbi->s_flags = uopt.flags;
577 sbi->s_uid = uopt.uid;
578 sbi->s_gid = uopt.gid;
579 sbi->s_umask = uopt.umask;
7ac9bcd5
MS
580 sbi->s_fmode = uopt.fmode;
581 sbi->s_dmode = uopt.dmode;
c03cad24 582 write_unlock(&sbi->s_cred_lock);
1da177e4 583
6c79e987
MS
584 if (sbi->s_lvid_bh) {
585 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
1da177e4
LT
586 if (write_rev > UDF_MAX_WRITE_VERSION)
587 *flags |= MS_RDONLY;
588 }
589
c79d967d
CH
590 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
591 goto out_unlock;
592
36350462 593 if (*flags & MS_RDONLY)
1da177e4 594 udf_close_lvid(sb);
36350462 595 else
1da177e4
LT
596 udf_open_lvid(sb);
597
c79d967d 598out_unlock:
c79d967d 599 return error;
1da177e4
LT
600}
601
40346005
JK
602/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
603/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
604static loff_t udf_check_vsd(struct super_block *sb)
1da177e4
LT
605{
606 struct volStructDesc *vsd = NULL;
f4bcbbd9 607 loff_t sector = 32768;
1da177e4
LT
608 int sectorsize;
609 struct buffer_head *bh = NULL;
cb00ea35
CG
610 int nsr02 = 0;
611 int nsr03 = 0;
6c79e987 612 struct udf_sb_info *sbi;
1da177e4 613
6c79e987 614 sbi = UDF_SB(sb);
1da177e4
LT
615 if (sb->s_blocksize < sizeof(struct volStructDesc))
616 sectorsize = sizeof(struct volStructDesc);
617 else
618 sectorsize = sb->s_blocksize;
619
6c79e987 620 sector += (sbi->s_session << sb->s_blocksize_bits);
1da177e4
LT
621
622 udf_debug("Starting at sector %u (%ld byte sectors)\n",
706047a7
SM
623 (unsigned int)(sector >> sb->s_blocksize_bits),
624 sb->s_blocksize);
1da177e4 625 /* Process the sequence (if applicable) */
cb00ea35 626 for (; !nsr02 && !nsr03; sector += sectorsize) {
1da177e4
LT
627 /* Read a block */
628 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
629 if (!bh)
630 break;
631
632 /* Look for ISO descriptors */
633 vsd = (struct volStructDesc *)(bh->b_data +
3a71fc5d 634 (sector & (sb->s_blocksize - 1)));
1da177e4 635
cb00ea35 636 if (vsd->stdIdent[0] == 0) {
3bf25cb4 637 brelse(bh);
1da177e4 638 break;
3a71fc5d
MS
639 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
640 VSD_STD_ID_LEN)) {
cb00ea35
CG
641 switch (vsd->structType) {
642 case 0:
643 udf_debug("ISO9660 Boot Record found\n");
644 break;
645 case 1:
3a71fc5d
MS
646 udf_debug("ISO9660 Primary Volume Descriptor "
647 "found\n");
cb00ea35
CG
648 break;
649 case 2:
3a71fc5d
MS
650 udf_debug("ISO9660 Supplementary Volume "
651 "Descriptor found\n");
cb00ea35
CG
652 break;
653 case 3:
3a71fc5d
MS
654 udf_debug("ISO9660 Volume Partition Descriptor "
655 "found\n");
cb00ea35
CG
656 break;
657 case 255:
3a71fc5d
MS
658 udf_debug("ISO9660 Volume Descriptor Set "
659 "Terminator found\n");
cb00ea35
CG
660 break;
661 default:
662 udf_debug("ISO9660 VRS (%u) found\n",
663 vsd->structType);
664 break;
1da177e4 665 }
3a71fc5d
MS
666 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
667 VSD_STD_ID_LEN))
668 ; /* nothing */
669 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
670 VSD_STD_ID_LEN)) {
3bf25cb4 671 brelse(bh);
1da177e4 672 break;
3a71fc5d
MS
673 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
674 VSD_STD_ID_LEN))
1da177e4 675 nsr02 = sector;
3a71fc5d
MS
676 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
677 VSD_STD_ID_LEN))
1da177e4 678 nsr03 = sector;
3bf25cb4 679 brelse(bh);
1da177e4
LT
680 }
681
682 if (nsr03)
683 return nsr03;
684 else if (nsr02)
685 return nsr02;
6c79e987 686 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
1da177e4
LT
687 return -1;
688 else
689 return 0;
690}
691
3a71fc5d 692static int udf_find_fileset(struct super_block *sb,
5ca4e4be
PE
693 struct kernel_lb_addr *fileset,
694 struct kernel_lb_addr *root)
1da177e4
LT
695{
696 struct buffer_head *bh = NULL;
697 long lastblock;
698 uint16_t ident;
6c79e987 699 struct udf_sb_info *sbi;
1da177e4
LT
700
701 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
cb00ea35 702 fileset->partitionReferenceNum != 0xFFFF) {
97e961fd 703 bh = udf_read_ptagged(sb, fileset, 0, &ident);
1da177e4 704
28de7948 705 if (!bh) {
1da177e4 706 return 1;
28de7948 707 } else if (ident != TAG_IDENT_FSD) {
3bf25cb4 708 brelse(bh);
1da177e4
LT
709 return 1;
710 }
cb00ea35 711
1da177e4
LT
712 }
713
6c79e987 714 sbi = UDF_SB(sb);
3a71fc5d
MS
715 if (!bh) {
716 /* Search backwards through the partitions */
5ca4e4be 717 struct kernel_lb_addr newfileset;
1da177e4 718
28de7948 719/* --> cvg: FIXME - is it reasonable? */
1da177e4 720 return 1;
cb00ea35 721
6c79e987 722 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
cb00ea35
CG
723 (newfileset.partitionReferenceNum != 0xFFFF &&
724 fileset->logicalBlockNum == 0xFFFFFFFF &&
725 fileset->partitionReferenceNum == 0xFFFF);
726 newfileset.partitionReferenceNum--) {
6c79e987
MS
727 lastblock = sbi->s_partmaps
728 [newfileset.partitionReferenceNum]
729 .s_partition_len;
1da177e4
LT
730 newfileset.logicalBlockNum = 0;
731
cb00ea35 732 do {
97e961fd 733 bh = udf_read_ptagged(sb, &newfileset, 0,
3a71fc5d 734 &ident);
cb00ea35
CG
735 if (!bh) {
736 newfileset.logicalBlockNum++;
1da177e4
LT
737 continue;
738 }
739
cb00ea35
CG
740 switch (ident) {
741 case TAG_IDENT_SBD:
28de7948
CG
742 {
743 struct spaceBitmapDesc *sp;
4b11111a
MS
744 sp = (struct spaceBitmapDesc *)
745 bh->b_data;
28de7948
CG
746 newfileset.logicalBlockNum += 1 +
747 ((le32_to_cpu(sp->numOfBytes) +
4b11111a
MS
748 sizeof(struct spaceBitmapDesc)
749 - 1) >> sb->s_blocksize_bits);
28de7948
CG
750 brelse(bh);
751 break;
752 }
cb00ea35 753 case TAG_IDENT_FSD:
28de7948
CG
754 *fileset = newfileset;
755 break;
cb00ea35 756 default:
28de7948
CG
757 newfileset.logicalBlockNum++;
758 brelse(bh);
759 bh = NULL;
760 break;
1da177e4 761 }
28de7948
CG
762 } while (newfileset.logicalBlockNum < lastblock &&
763 fileset->logicalBlockNum == 0xFFFFFFFF &&
764 fileset->partitionReferenceNum == 0xFFFF);
1da177e4
LT
765 }
766 }
767
768 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
cb00ea35 769 fileset->partitionReferenceNum != 0xFFFF) && bh) {
1da177e4 770 udf_debug("Fileset at block=%d, partition=%d\n",
cb00ea35
CG
771 fileset->logicalBlockNum,
772 fileset->partitionReferenceNum);
1da177e4 773
6c79e987 774 sbi->s_partition = fileset->partitionReferenceNum;
1da177e4 775 udf_load_fileset(sb, bh, root);
3bf25cb4 776 brelse(bh);
1da177e4
LT
777 return 0;
778 }
779 return 1;
780}
781
c0eb31ed 782static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
1da177e4
LT
783{
784 struct primaryVolDesc *pvoldesc;
ba9aadd8 785 struct ustr *instr, *outstr;
c0eb31ed
JK
786 struct buffer_head *bh;
787 uint16_t ident;
ba9aadd8
MS
788 int ret = 1;
789
790 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
791 if (!instr)
792 return 1;
793
794 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
795 if (!outstr)
796 goto out1;
c0eb31ed
JK
797
798 bh = udf_read_tagged(sb, block, block, &ident);
799 if (!bh)
ba9aadd8
MS
800 goto out2;
801
c0eb31ed 802 BUG_ON(ident != TAG_IDENT_PVD);
1da177e4
LT
803
804 pvoldesc = (struct primaryVolDesc *)bh->b_data;
805
56774805
MS
806 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
807 pvoldesc->recordingDateAndTime)) {
af15a298 808#ifdef UDFFS_DEBUG
5ca4e4be 809 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
cbf5676a 810 udf_debug("recording time %04u/%02u/%02u"
3a71fc5d 811 " %02u:%02u (%x)\n",
af15a298
MS
812 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
813 ts->minute, le16_to_cpu(ts->typeAndTimezone));
814#endif
1da177e4
LT
815 }
816
ba9aadd8
MS
817 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
818 if (udf_CS0toUTF8(outstr, instr)) {
819 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
820 outstr->u_len > 31 ? 31 : outstr->u_len);
4b11111a
MS
821 udf_debug("volIdent[] = '%s'\n",
822 UDF_SB(sb)->s_volume_ident);
1da177e4 823 }
1da177e4 824
ba9aadd8
MS
825 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
826 if (udf_CS0toUTF8(outstr, instr))
827 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
c0eb31ed
JK
828
829 brelse(bh);
ba9aadd8
MS
830 ret = 0;
831out2:
832 kfree(outstr);
833out1:
834 kfree(instr);
835 return ret;
1da177e4
LT
836}
837
bfb257a5
JK
838static int udf_load_metadata_files(struct super_block *sb, int partition)
839{
840 struct udf_sb_info *sbi = UDF_SB(sb);
841 struct udf_part_map *map;
842 struct udf_meta_data *mdata;
5ca4e4be 843 struct kernel_lb_addr addr;
bfb257a5
JK
844 int fe_error = 0;
845
846 map = &sbi->s_partmaps[partition];
847 mdata = &map->s_type_specific.s_metadata;
848
849 /* metadata address */
850 addr.logicalBlockNum = mdata->s_meta_file_loc;
851 addr.partitionReferenceNum = map->s_partition_num;
852
853 udf_debug("Metadata file location: block = %d part = %d\n",
854 addr.logicalBlockNum, addr.partitionReferenceNum);
855
97e961fd 856 mdata->s_metadata_fe = udf_iget(sb, &addr);
bfb257a5
JK
857
858 if (mdata->s_metadata_fe == NULL) {
859 udf_warning(sb, __func__, "metadata inode efe not found, "
860 "will try mirror inode.");
861 fe_error = 1;
862 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
863 ICBTAG_FLAG_AD_SHORT) {
864 udf_warning(sb, __func__, "metadata inode efe does not have "
865 "short allocation descriptors!");
866 fe_error = 1;
867 iput(mdata->s_metadata_fe);
868 mdata->s_metadata_fe = NULL;
869 }
870
871 /* mirror file entry */
872 addr.logicalBlockNum = mdata->s_mirror_file_loc;
873 addr.partitionReferenceNum = map->s_partition_num;
874
875 udf_debug("Mirror metadata file location: block = %d part = %d\n",
876 addr.logicalBlockNum, addr.partitionReferenceNum);
877
97e961fd 878 mdata->s_mirror_fe = udf_iget(sb, &addr);
bfb257a5
JK
879
880 if (mdata->s_mirror_fe == NULL) {
881 if (fe_error) {
882 udf_error(sb, __func__, "mirror inode efe not found "
883 "and metadata inode is missing too, exiting...");
884 goto error_exit;
885 } else
886 udf_warning(sb, __func__, "mirror inode efe not found,"
887 " but metadata inode is OK");
888 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
889 ICBTAG_FLAG_AD_SHORT) {
890 udf_warning(sb, __func__, "mirror inode efe does not have "
891 "short allocation descriptors!");
892 iput(mdata->s_mirror_fe);
893 mdata->s_mirror_fe = NULL;
894 if (fe_error)
895 goto error_exit;
896 }
897
898 /*
899 * bitmap file entry
900 * Note:
901 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
902 */
903 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
904 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
905 addr.partitionReferenceNum = map->s_partition_num;
906
907 udf_debug("Bitmap file location: block = %d part = %d\n",
908 addr.logicalBlockNum, addr.partitionReferenceNum);
909
97e961fd 910 mdata->s_bitmap_fe = udf_iget(sb, &addr);
bfb257a5
JK
911
912 if (mdata->s_bitmap_fe == NULL) {
913 if (sb->s_flags & MS_RDONLY)
914 udf_warning(sb, __func__, "bitmap inode efe "
915 "not found but it's ok since the disc"
916 " is mounted read-only");
917 else {
918 udf_error(sb, __func__, "bitmap inode efe not "
919 "found and attempted read-write mount");
920 goto error_exit;
921 }
922 }
923 }
924
925 udf_debug("udf_load_metadata_files Ok\n");
926
927 return 0;
928
929error_exit:
930 return 1;
931}
932
28de7948 933static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
5ca4e4be 934 struct kernel_lb_addr *root)
1da177e4
LT
935{
936 struct fileSetDesc *fset;
937
938 fset = (struct fileSetDesc *)bh->b_data;
939
940 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
941
6c79e987 942 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
1da177e4 943
cb00ea35
CG
944 udf_debug("Rootdir at block=%d, partition=%d\n",
945 root->logicalBlockNum, root->partitionReferenceNum);
1da177e4
LT
946}
947
883cb9d1
MS
948int udf_compute_nr_groups(struct super_block *sb, u32 partition)
949{
950 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
8dee00bb
JL
951 return DIV_ROUND_UP(map->s_partition_len +
952 (sizeof(struct spaceBitmapDesc) << 3),
953 sb->s_blocksize * 8);
883cb9d1
MS
954}
955
66e1da3f
MS
956static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
957{
66e1da3f
MS
958 struct udf_bitmap *bitmap;
959 int nr_groups;
960 int size;
961
883cb9d1 962 nr_groups = udf_compute_nr_groups(sb, index);
66e1da3f
MS
963 size = sizeof(struct udf_bitmap) +
964 (sizeof(struct buffer_head *) * nr_groups);
965
966 if (size <= PAGE_SIZE)
ed2ae6f6 967 bitmap = kzalloc(size, GFP_KERNEL);
66e1da3f 968 else
ed2ae6f6 969 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
66e1da3f
MS
970
971 if (bitmap == NULL) {
8e24eea7 972 udf_error(sb, __func__,
66e1da3f
MS
973 "Unable to allocate space for bitmap "
974 "and %d buffer_head pointers", nr_groups);
975 return NULL;
976 }
977
66e1da3f
MS
978 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
979 bitmap->s_nr_groups = nr_groups;
980 return bitmap;
981}
982
3fb38dfa
JK
983static int udf_fill_partdesc_info(struct super_block *sb,
984 struct partitionDesc *p, int p_index)
1da177e4 985{
6c79e987 986 struct udf_part_map *map;
165923fa 987 struct udf_sb_info *sbi = UDF_SB(sb);
3fb38dfa 988 struct partitionHeaderDesc *phd;
165923fa 989
3fb38dfa 990 map = &sbi->s_partmaps[p_index];
165923fa
MS
991
992 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
993 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
994
995 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
996 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
997 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
998 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
999 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1000 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1001 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1002 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1003
706047a7
SM
1004 udf_debug("Partition (%d type %x) starts at physical %d, "
1005 "block length %d\n", p_index,
165923fa
MS
1006 map->s_partition_type, map->s_partition_root,
1007 map->s_partition_len);
1008
1009 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1010 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
3fb38dfa 1011 return 0;
165923fa
MS
1012
1013 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1014 if (phd->unallocSpaceTable.extLength) {
5ca4e4be 1015 struct kernel_lb_addr loc = {
165923fa
MS
1016 .logicalBlockNum = le32_to_cpu(
1017 phd->unallocSpaceTable.extPosition),
3fb38dfa 1018 .partitionReferenceNum = p_index,
165923fa
MS
1019 };
1020
97e961fd 1021 map->s_uspace.s_table = udf_iget(sb, &loc);
165923fa
MS
1022 if (!map->s_uspace.s_table) {
1023 udf_debug("cannot load unallocSpaceTable (part %d)\n",
3fb38dfa
JK
1024 p_index);
1025 return 1;
165923fa
MS
1026 }
1027 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1028 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
3fb38dfa 1029 p_index, map->s_uspace.s_table->i_ino);
165923fa
MS
1030 }
1031
1032 if (phd->unallocSpaceBitmap.extLength) {
3fb38dfa
JK
1033 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1034 if (!bitmap)
1035 return 1;
165923fa 1036 map->s_uspace.s_bitmap = bitmap;
2e0838fd 1037 bitmap->s_extLength = le32_to_cpu(
165923fa 1038 phd->unallocSpaceBitmap.extLength);
2e0838fd 1039 bitmap->s_extPosition = le32_to_cpu(
165923fa 1040 phd->unallocSpaceBitmap.extPosition);
2e0838fd 1041 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
3fb38dfa 1042 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
2e0838fd 1043 bitmap->s_extPosition);
165923fa
MS
1044 }
1045
1046 if (phd->partitionIntegrityTable.extLength)
3fb38dfa 1047 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
165923fa
MS
1048
1049 if (phd->freedSpaceTable.extLength) {
5ca4e4be 1050 struct kernel_lb_addr loc = {
165923fa
MS
1051 .logicalBlockNum = le32_to_cpu(
1052 phd->freedSpaceTable.extPosition),
3fb38dfa 1053 .partitionReferenceNum = p_index,
165923fa
MS
1054 };
1055
97e961fd 1056 map->s_fspace.s_table = udf_iget(sb, &loc);
165923fa 1057 if (!map->s_fspace.s_table) {
3fb38dfa
JK
1058 udf_debug("cannot load freedSpaceTable (part %d)\n",
1059 p_index);
1060 return 1;
165923fa
MS
1061 }
1062
1063 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1064 udf_debug("freedSpaceTable (part %d) @ %ld\n",
3fb38dfa 1065 p_index, map->s_fspace.s_table->i_ino);
165923fa
MS
1066 }
1067
1068 if (phd->freedSpaceBitmap.extLength) {
3fb38dfa
JK
1069 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1070 if (!bitmap)
1071 return 1;
165923fa 1072 map->s_fspace.s_bitmap = bitmap;
2e0838fd 1073 bitmap->s_extLength = le32_to_cpu(
165923fa 1074 phd->freedSpaceBitmap.extLength);
2e0838fd 1075 bitmap->s_extPosition = le32_to_cpu(
165923fa 1076 phd->freedSpaceBitmap.extPosition);
2e0838fd 1077 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
3fb38dfa 1078 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
2e0838fd 1079 bitmap->s_extPosition);
165923fa 1080 }
3fb38dfa
JK
1081 return 0;
1082}
1083
e971b0b9
JK
1084static void udf_find_vat_block(struct super_block *sb, int p_index,
1085 int type1_index, sector_t start_block)
38b74a53
JK
1086{
1087 struct udf_sb_info *sbi = UDF_SB(sb);
1088 struct udf_part_map *map = &sbi->s_partmaps[p_index];
e971b0b9 1089 sector_t vat_block;
5ca4e4be 1090 struct kernel_lb_addr ino;
e971b0b9
JK
1091
1092 /*
1093 * VAT file entry is in the last recorded block. Some broken disks have
1094 * it a few blocks before so try a bit harder...
1095 */
1096 ino.partitionReferenceNum = type1_index;
1097 for (vat_block = start_block;
1098 vat_block >= map->s_partition_root &&
1099 vat_block >= start_block - 3 &&
1100 !sbi->s_vat_inode; vat_block--) {
1101 ino.logicalBlockNum = vat_block - map->s_partition_root;
1102 sbi->s_vat_inode = udf_iget(sb, &ino);
1103 }
1104}
1105
1106static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1107{
1108 struct udf_sb_info *sbi = UDF_SB(sb);
1109 struct udf_part_map *map = &sbi->s_partmaps[p_index];
fa5e0815
JK
1110 struct buffer_head *bh = NULL;
1111 struct udf_inode_info *vati;
1112 uint32_t pos;
1113 struct virtualAllocationTable20 *vat20;
4bf17af0 1114 sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
38b74a53 1115
e971b0b9 1116 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
4bf17af0
JK
1117 if (!sbi->s_vat_inode &&
1118 sbi->s_last_block != blocks - 1) {
1119 printk(KERN_NOTICE "UDF-fs: Failed to read VAT inode from the"
1120 " last recorded block (%lu), retrying with the last "
1121 "block of the device (%lu).\n",
1122 (unsigned long)sbi->s_last_block,
1123 (unsigned long)blocks - 1);
e971b0b9 1124 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
4bf17af0 1125 }
38b74a53
JK
1126 if (!sbi->s_vat_inode)
1127 return 1;
1128
1129 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
47c9358a 1130 map->s_type_specific.s_virtual.s_start_offset = 0;
38b74a53
JK
1131 map->s_type_specific.s_virtual.s_num_entries =
1132 (sbi->s_vat_inode->i_size - 36) >> 2;
1133 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
fa5e0815
JK
1134 vati = UDF_I(sbi->s_vat_inode);
1135 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1136 pos = udf_block_map(sbi->s_vat_inode, 0);
1137 bh = sb_bread(sb, pos);
1138 if (!bh)
1139 return 1;
1140 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1141 } else {
1142 vat20 = (struct virtualAllocationTable20 *)
1143 vati->i_ext.i_data;
1144 }
38b74a53 1145
38b74a53 1146 map->s_type_specific.s_virtual.s_start_offset =
47c9358a 1147 le16_to_cpu(vat20->lengthHeader);
38b74a53
JK
1148 map->s_type_specific.s_virtual.s_num_entries =
1149 (sbi->s_vat_inode->i_size -
1150 map->s_type_specific.s_virtual.
1151 s_start_offset) >> 2;
1152 brelse(bh);
1153 }
1154 return 0;
1155}
1156
3fb38dfa
JK
1157static int udf_load_partdesc(struct super_block *sb, sector_t block)
1158{
1159 struct buffer_head *bh;
1160 struct partitionDesc *p;
1161 struct udf_part_map *map;
1162 struct udf_sb_info *sbi = UDF_SB(sb);
38b74a53 1163 int i, type1_idx;
3fb38dfa
JK
1164 uint16_t partitionNumber;
1165 uint16_t ident;
1166 int ret = 0;
1167
1168 bh = udf_read_tagged(sb, block, block, &ident);
1169 if (!bh)
1170 return 1;
1171 if (ident != TAG_IDENT_PD)
1172 goto out_bh;
1173
1174 p = (struct partitionDesc *)bh->b_data;
1175 partitionNumber = le16_to_cpu(p->partitionNumber);
38b74a53 1176
bfb257a5 1177 /* First scan for TYPE1, SPARABLE and METADATA partitions */
3fb38dfa
JK
1178 for (i = 0; i < sbi->s_partitions; i++) {
1179 map = &sbi->s_partmaps[i];
1180 udf_debug("Searching map: (%d == %d)\n",
1181 map->s_partition_num, partitionNumber);
38b74a53
JK
1182 if (map->s_partition_num == partitionNumber &&
1183 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1184 map->s_partition_type == UDF_SPARABLE_MAP15))
3fb38dfa
JK
1185 break;
1186 }
1187
38b74a53 1188 if (i >= sbi->s_partitions) {
3fb38dfa
JK
1189 udf_debug("Partition (%d) not found in partition map\n",
1190 partitionNumber);
1191 goto out_bh;
1192 }
165923fa 1193
3fb38dfa 1194 ret = udf_fill_partdesc_info(sb, p, i);
38b74a53
JK
1195
1196 /*
bfb257a5
JK
1197 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1198 * PHYSICAL partitions are already set up
38b74a53
JK
1199 */
1200 type1_idx = i;
1201 for (i = 0; i < sbi->s_partitions; i++) {
1202 map = &sbi->s_partmaps[i];
1203
1204 if (map->s_partition_num == partitionNumber &&
1205 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
bfb257a5
JK
1206 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1207 map->s_partition_type == UDF_METADATA_MAP25))
38b74a53
JK
1208 break;
1209 }
1210
1211 if (i >= sbi->s_partitions)
1212 goto out_bh;
1213
1214 ret = udf_fill_partdesc_info(sb, p, i);
1215 if (ret)
1216 goto out_bh;
1217
bfb257a5
JK
1218 if (map->s_partition_type == UDF_METADATA_MAP25) {
1219 ret = udf_load_metadata_files(sb, i);
1220 if (ret) {
1221 printk(KERN_ERR "UDF-fs: error loading MetaData "
1222 "partition map %d\n", i);
1223 goto out_bh;
1224 }
1225 } else {
1226 ret = udf_load_vat(sb, i, type1_idx);
1227 if (ret)
1228 goto out_bh;
1229 /*
1230 * Mark filesystem read-only if we have a partition with
1231 * virtual map since we don't handle writing to it (we
1232 * overwrite blocks instead of relocating them).
1233 */
1234 sb->s_flags |= MS_RDONLY;
1235 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1236 "because writing to pseudooverwrite partition is "
1237 "not implemented.\n");
1238 }
c0eb31ed 1239out_bh:
2e0838fd 1240 /* In case loading failed, we handle cleanup in udf_fill_super */
c0eb31ed
JK
1241 brelse(bh);
1242 return ret;
1da177e4
LT
1243}
1244
c0eb31ed 1245static int udf_load_logicalvol(struct super_block *sb, sector_t block,
5ca4e4be 1246 struct kernel_lb_addr *fileset)
1da177e4
LT
1247{
1248 struct logicalVolDesc *lvd;
1249 int i, j, offset;
1250 uint8_t type;
6c79e987 1251 struct udf_sb_info *sbi = UDF_SB(sb);
4b11111a 1252 struct genericPartitionMap *gpm;
c0eb31ed
JK
1253 uint16_t ident;
1254 struct buffer_head *bh;
1255 int ret = 0;
1da177e4 1256
c0eb31ed
JK
1257 bh = udf_read_tagged(sb, block, block, &ident);
1258 if (!bh)
1259 return 1;
1260 BUG_ON(ident != TAG_IDENT_LVD);
1da177e4
LT
1261 lvd = (struct logicalVolDesc *)bh->b_data;
1262
dc5d39be 1263 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
c0eb31ed
JK
1264 if (i != 0) {
1265 ret = i;
1266 goto out_bh;
1267 }
1da177e4 1268
cb00ea35 1269 for (i = 0, offset = 0;
6c79e987 1270 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
4b11111a
MS
1271 i++, offset += gpm->partitionMapLength) {
1272 struct udf_part_map *map = &sbi->s_partmaps[i];
1273 gpm = (struct genericPartitionMap *)
1274 &(lvd->partitionMaps[offset]);
1275 type = gpm->partitionMapType;
cb00ea35 1276 if (type == 1) {
4b11111a
MS
1277 struct genericPartitionMap1 *gpm1 =
1278 (struct genericPartitionMap1 *)gpm;
6c79e987
MS
1279 map->s_partition_type = UDF_TYPE1_MAP15;
1280 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1281 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1282 map->s_partition_func = NULL;
cb00ea35 1283 } else if (type == 2) {
4b11111a
MS
1284 struct udfPartitionMap2 *upm2 =
1285 (struct udfPartitionMap2 *)gpm;
1286 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1287 strlen(UDF_ID_VIRTUAL))) {
1288 u16 suf =
1289 le16_to_cpu(((__le16 *)upm2->partIdent.
1290 identSuffix)[0]);
c82a1275 1291 if (suf < 0x0200) {
4b11111a
MS
1292 map->s_partition_type =
1293 UDF_VIRTUAL_MAP15;
1294 map->s_partition_func =
1295 udf_get_pblock_virt15;
c82a1275 1296 } else {
4b11111a
MS
1297 map->s_partition_type =
1298 UDF_VIRTUAL_MAP20;
1299 map->s_partition_func =
1300 udf_get_pblock_virt20;
1da177e4 1301 }
4b11111a
MS
1302 } else if (!strncmp(upm2->partIdent.ident,
1303 UDF_ID_SPARABLE,
1304 strlen(UDF_ID_SPARABLE))) {
1da177e4 1305 uint32_t loc;
1da177e4 1306 struct sparingTable *st;
4b11111a
MS
1307 struct sparablePartitionMap *spm =
1308 (struct sparablePartitionMap *)gpm;
28de7948 1309
6c79e987 1310 map->s_partition_type = UDF_SPARABLE_MAP15;
4b11111a
MS
1311 map->s_type_specific.s_sparing.s_packet_len =
1312 le16_to_cpu(spm->packetLength);
cb00ea35 1313 for (j = 0; j < spm->numSparingTables; j++) {
4b11111a
MS
1314 struct buffer_head *bh2;
1315
1316 loc = le32_to_cpu(
1317 spm->locSparingTable[j]);
1318 bh2 = udf_read_tagged(sb, loc, loc,
1319 &ident);
1320 map->s_type_specific.s_sparing.
1321 s_spar_map[j] = bh2;
1322
165923fa
MS
1323 if (bh2 == NULL)
1324 continue;
1325
1326 st = (struct sparingTable *)bh2->b_data;
1327 if (ident != 0 || strncmp(
1328 st->sparingIdent.ident,
1329 UDF_ID_SPARING,
1330 strlen(UDF_ID_SPARING))) {
1331 brelse(bh2);
1332 map->s_type_specific.s_sparing.
1333 s_spar_map[j] = NULL;
1da177e4
LT
1334 }
1335 }
6c79e987 1336 map->s_partition_func = udf_get_pblock_spar15;
bfb257a5
JK
1337 } else if (!strncmp(upm2->partIdent.ident,
1338 UDF_ID_METADATA,
1339 strlen(UDF_ID_METADATA))) {
1340 struct udf_meta_data *mdata =
1341 &map->s_type_specific.s_metadata;
1342 struct metadataPartitionMap *mdm =
1343 (struct metadataPartitionMap *)
1344 &(lvd->partitionMaps[offset]);
1345 udf_debug("Parsing Logical vol part %d "
1346 "type %d id=%s\n", i, type,
1347 UDF_ID_METADATA);
1348
1349 map->s_partition_type = UDF_METADATA_MAP25;
1350 map->s_partition_func = udf_get_pblock_meta25;
1351
1352 mdata->s_meta_file_loc =
1353 le32_to_cpu(mdm->metadataFileLoc);
1354 mdata->s_mirror_file_loc =
1355 le32_to_cpu(mdm->metadataMirrorFileLoc);
1356 mdata->s_bitmap_file_loc =
1357 le32_to_cpu(mdm->metadataBitmapFileLoc);
1358 mdata->s_alloc_unit_size =
1359 le32_to_cpu(mdm->allocUnitSize);
1360 mdata->s_align_unit_size =
1361 le16_to_cpu(mdm->alignUnitSize);
1362 mdata->s_dup_md_flag =
1363 mdm->flags & 0x01;
1364
1365 udf_debug("Metadata Ident suffix=0x%x\n",
1366 (le16_to_cpu(
1367 ((__le16 *)
1368 mdm->partIdent.identSuffix)[0])));
1369 udf_debug("Metadata part num=%d\n",
1370 le16_to_cpu(mdm->partitionNum));
1371 udf_debug("Metadata part alloc unit size=%d\n",
1372 le32_to_cpu(mdm->allocUnitSize));
1373 udf_debug("Metadata file loc=%d\n",
1374 le32_to_cpu(mdm->metadataFileLoc));
1375 udf_debug("Mirror file loc=%d\n",
1376 le32_to_cpu(mdm->metadataMirrorFileLoc));
1377 udf_debug("Bitmap file loc=%d\n",
1378 le32_to_cpu(mdm->metadataBitmapFileLoc));
1379 udf_debug("Duplicate Flag: %d %d\n",
1380 mdata->s_dup_md_flag, mdm->flags);
cb00ea35 1381 } else {
3a71fc5d
MS
1382 udf_debug("Unknown ident: %s\n",
1383 upm2->partIdent.ident);
1da177e4
LT
1384 continue;
1385 }
6c79e987
MS
1386 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1387 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
1da177e4
LT
1388 }
1389 udf_debug("Partition (%d:%d) type %d on volume %d\n",
6c79e987
MS
1390 i, map->s_partition_num, type,
1391 map->s_volumeseqnum);
1da177e4
LT
1392 }
1393
cb00ea35 1394 if (fileset) {
5ca4e4be 1395 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
1da177e4
LT
1396
1397 *fileset = lelb_to_cpu(la->extLocation);
3a71fc5d
MS
1398 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1399 "partition=%d\n", fileset->logicalBlockNum,
28de7948 1400 fileset->partitionReferenceNum);
1da177e4
LT
1401 }
1402 if (lvd->integritySeqExt.extLength)
1403 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
28de7948 1404
c0eb31ed
JK
1405out_bh:
1406 brelse(bh);
1407 return ret;
1da177e4
LT
1408}
1409
1410/*
1411 * udf_load_logicalvolint
1412 *
1413 */
5ca4e4be 1414static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
1da177e4
LT
1415{
1416 struct buffer_head *bh = NULL;
1417 uint16_t ident;
6c79e987
MS
1418 struct udf_sb_info *sbi = UDF_SB(sb);
1419 struct logicalVolIntegrityDesc *lvid;
1da177e4
LT
1420
1421 while (loc.extLength > 0 &&
cb00ea35
CG
1422 (bh = udf_read_tagged(sb, loc.extLocation,
1423 loc.extLocation, &ident)) &&
1424 ident == TAG_IDENT_LVID) {
6c79e987
MS
1425 sbi->s_lvid_bh = bh;
1426 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
cb00ea35 1427
6c79e987 1428 if (lvid->nextIntegrityExt.extLength)
3a71fc5d 1429 udf_load_logicalvolint(sb,
6c79e987 1430 leea_to_cpu(lvid->nextIntegrityExt));
cb00ea35 1431
6c79e987 1432 if (sbi->s_lvid_bh != bh)
3bf25cb4 1433 brelse(bh);
1da177e4 1434 loc.extLength -= sb->s_blocksize;
cb00ea35 1435 loc.extLocation++;
1da177e4 1436 }
6c79e987 1437 if (sbi->s_lvid_bh != bh)
3bf25cb4 1438 brelse(bh);
1da177e4
LT
1439}
1440
1441/*
1442 * udf_process_sequence
1443 *
1444 * PURPOSE
1445 * Process a main/reserve volume descriptor sequence.
1446 *
1447 * PRE-CONDITIONS
1448 * sb Pointer to _locked_ superblock.
1449 * block First block of first extent of the sequence.
1450 * lastblock Lastblock of first extent of the sequence.
1451 *
1452 * HISTORY
1453 * July 1, 1997 - Andrew E. Mileski
1454 * Written, tested, and released.
1455 */
200a3592 1456static noinline int udf_process_sequence(struct super_block *sb, long block,
5ca4e4be 1457 long lastblock, struct kernel_lb_addr *fileset)
1da177e4
LT
1458{
1459 struct buffer_head *bh = NULL;
1460 struct udf_vds_record vds[VDS_POS_LENGTH];
4b11111a 1461 struct udf_vds_record *curr;
1da177e4
LT
1462 struct generic_desc *gd;
1463 struct volDescPtr *vdp;
cb00ea35 1464 int done = 0;
1da177e4
LT
1465 uint32_t vdsn;
1466 uint16_t ident;
1467 long next_s = 0, next_e = 0;
1468
1469 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1470
c0eb31ed
JK
1471 /*
1472 * Read the main descriptor sequence and find which descriptors
1473 * are in it.
1474 */
cb00ea35 1475 for (; (!done && block <= lastblock); block++) {
1da177e4
LT
1476
1477 bh = udf_read_tagged(sb, block, block, &ident);
c0eb31ed
JK
1478 if (!bh) {
1479 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1480 "sequence is corrupted or we could not read "
1481 "it.\n", (unsigned long long)block);
1482 return 1;
1483 }
1da177e4
LT
1484
1485 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1486 gd = (struct generic_desc *)bh->b_data;
1487 vdsn = le32_to_cpu(gd->volDescSeqNum);
cb00ea35 1488 switch (ident) {
28de7948 1489 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
4b11111a
MS
1490 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1491 if (vdsn >= curr->volDescSeqNum) {
1492 curr->volDescSeqNum = vdsn;
1493 curr->block = block;
cb00ea35
CG
1494 }
1495 break;
28de7948 1496 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
4b11111a
MS
1497 curr = &vds[VDS_POS_VOL_DESC_PTR];
1498 if (vdsn >= curr->volDescSeqNum) {
1499 curr->volDescSeqNum = vdsn;
1500 curr->block = block;
cb00ea35
CG
1501
1502 vdp = (struct volDescPtr *)bh->b_data;
4b11111a
MS
1503 next_s = le32_to_cpu(
1504 vdp->nextVolDescSeqExt.extLocation);
1505 next_e = le32_to_cpu(
1506 vdp->nextVolDescSeqExt.extLength);
cb00ea35
CG
1507 next_e = next_e >> sb->s_blocksize_bits;
1508 next_e += next_s;
1509 }
1510 break;
28de7948 1511 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
4b11111a
MS
1512 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1513 if (vdsn >= curr->volDescSeqNum) {
1514 curr->volDescSeqNum = vdsn;
1515 curr->block = block;
cb00ea35
CG
1516 }
1517 break;
28de7948 1518 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
4b11111a
MS
1519 curr = &vds[VDS_POS_PARTITION_DESC];
1520 if (!curr->block)
1521 curr->block = block;
cb00ea35 1522 break;
28de7948 1523 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
4b11111a
MS
1524 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1525 if (vdsn >= curr->volDescSeqNum) {
1526 curr->volDescSeqNum = vdsn;
1527 curr->block = block;
cb00ea35
CG
1528 }
1529 break;
28de7948 1530 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
4b11111a
MS
1531 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1532 if (vdsn >= curr->volDescSeqNum) {
1533 curr->volDescSeqNum = vdsn;
1534 curr->block = block;
cb00ea35
CG
1535 }
1536 break;
28de7948 1537 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
cb00ea35
CG
1538 vds[VDS_POS_TERMINATING_DESC].block = block;
1539 if (next_e) {
1540 block = next_s;
1541 lastblock = next_e;
1542 next_s = next_e = 0;
4b11111a 1543 } else
cb00ea35
CG
1544 done = 1;
1545 break;
1da177e4 1546 }
3bf25cb4 1547 brelse(bh);
1da177e4 1548 }
c0eb31ed
JK
1549 /*
1550 * Now read interesting descriptors again and process them
1551 * in a suitable order
1552 */
1553 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1554 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1555 return 1;
1556 }
1557 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1558 return 1;
165923fa 1559
c0eb31ed
JK
1560 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1561 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1562 return 1;
165923fa 1563
c0eb31ed
JK
1564 if (vds[VDS_POS_PARTITION_DESC].block) {
1565 /*
1566 * We rescan the whole descriptor sequence to find
1567 * partition descriptor blocks and process them.
1568 */
1569 for (block = vds[VDS_POS_PARTITION_DESC].block;
1570 block < vds[VDS_POS_TERMINATING_DESC].block;
1571 block++)
1572 if (udf_load_partdesc(sb, block))
165923fa 1573 return 1;
1da177e4
LT
1574 }
1575
1576 return 0;
1577}
1578
40346005
JK
1579static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1580 struct kernel_lb_addr *fileset)
1da177e4 1581{
40346005
JK
1582 struct anchorVolDescPtr *anchor;
1583 long main_s, main_e, reserve_s, reserve_e;
1da177e4 1584
40346005
JK
1585 anchor = (struct anchorVolDescPtr *)bh->b_data;
1586
1587 /* Locate the main sequence */
1588 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1589 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1590 main_e = main_e >> sb->s_blocksize_bits;
1591 main_e += main_s;
1592
1593 /* Locate the reserve sequence */
1594 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1595 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1596 reserve_e = reserve_e >> sb->s_blocksize_bits;
1597 reserve_e += reserve_s;
1598
1599 /* Process the main & reserve sequences */
1600 /* responsible for finding the PartitionDesc(s) */
1601 if (!udf_process_sequence(sb, main_s, main_e, fileset))
1602 return 1;
1603 return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1da177e4
LT
1604}
1605
40346005
JK
1606/*
1607 * Check whether there is an anchor block in the given block and
1608 * load Volume Descriptor Sequence if so.
1609 */
1610static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1611 struct kernel_lb_addr *fileset)
1197e4df 1612{
40346005
JK
1613 struct buffer_head *bh;
1614 uint16_t ident;
1615 int ret;
1197e4df 1616
40346005
JK
1617 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1618 udf_fixed_to_variable(block) >=
1619 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
1197e4df 1620 return 0;
40346005
JK
1621
1622 bh = udf_read_tagged(sb, block, block, &ident);
1623 if (!bh)
1197e4df 1624 return 0;
40346005
JK
1625 if (ident != TAG_IDENT_AVDP) {
1626 brelse(bh);
1197e4df
CL
1627 return 0;
1628 }
40346005
JK
1629 ret = udf_load_sequence(sb, bh, fileset);
1630 brelse(bh);
1631 return ret;
1197e4df
CL
1632}
1633
40346005
JK
1634/* Search for an anchor volume descriptor pointer */
1635static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
1636 struct kernel_lb_addr *fileset)
1da177e4 1637{
40346005 1638 sector_t last[6];
38b74a53 1639 int i;
40346005
JK
1640 struct udf_sb_info *sbi = UDF_SB(sb);
1641 int last_count = 0;
1da177e4 1642
40346005
JK
1643 /* First try user provided anchor */
1644 if (sbi->s_anchor) {
1645 if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
1646 return lastblock;
1647 }
1648 /*
1649 * according to spec, anchor is in either:
1650 * block 256
1651 * lastblock-256
1652 * lastblock
1653 * however, if the disc isn't closed, it could be 512.
1654 */
1655 if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
1656 return lastblock;
1657 /*
1658 * The trouble is which block is the last one. Drives often misreport
1659 * this so we try various possibilities.
1660 */
1661 last[last_count++] = lastblock;
1662 if (lastblock >= 1)
1663 last[last_count++] = lastblock - 1;
1664 last[last_count++] = lastblock + 1;
1665 if (lastblock >= 2)
1666 last[last_count++] = lastblock - 2;
1667 if (lastblock >= 150)
1668 last[last_count++] = lastblock - 150;
1669 if (lastblock >= 152)
1670 last[last_count++] = lastblock - 152;
1da177e4 1671
40346005
JK
1672 for (i = 0; i < last_count; i++) {
1673 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
1674 sb->s_blocksize_bits)
28f7c4d4 1675 continue;
40346005
JK
1676 if (udf_check_anchor_block(sb, last[i], fileset))
1677 return last[i];
1678 if (last[i] < 256)
28f7c4d4 1679 continue;
40346005
JK
1680 if (udf_check_anchor_block(sb, last[i] - 256, fileset))
1681 return last[i];
1682 }
28f7c4d4 1683
40346005
JK
1684 /* Finally try block 512 in case media is open */
1685 if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
1686 return last[0];
1687 return 0;
1688}
28f7c4d4 1689
40346005
JK
1690/*
1691 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1692 * area specified by it. The function expects sbi->s_lastblock to be the last
1693 * block on the media.
1694 *
1695 * Return 1 if ok, 0 if not found.
1696 *
1697 */
1698static int udf_find_anchor(struct super_block *sb,
1699 struct kernel_lb_addr *fileset)
1700{
1701 sector_t lastblock;
1702 struct udf_sb_info *sbi = UDF_SB(sb);
28f7c4d4 1703
40346005
JK
1704 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1705 if (lastblock)
1706 goto out;
1da177e4 1707
40346005
JK
1708 /* No anchor found? Try VARCONV conversion of block numbers */
1709 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1710 /* Firstly, we try to not convert number of the last block */
1711 lastblock = udf_scan_anchors(sb,
1712 udf_variable_to_fixed(sbi->s_last_block),
1713 fileset);
1714 if (lastblock)
1715 goto out;
1da177e4 1716
40346005
JK
1717 /* Secondly, we try with converted number of the last block */
1718 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1719 if (!lastblock) {
1720 /* VARCONV didn't help. Clear it. */
1721 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1722 return 0;
1da177e4 1723 }
40346005
JK
1724out:
1725 sbi->s_last_block = lastblock;
1726 return 1;
1727}
1da177e4 1728
40346005
JK
1729/*
1730 * Check Volume Structure Descriptor, find Anchor block and load Volume
1731 * Descriptor Sequence
1732 */
1733static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1734 int silent, struct kernel_lb_addr *fileset)
1735{
1736 struct udf_sb_info *sbi = UDF_SB(sb);
1737 loff_t nsr_off;
1738
1739 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1740 if (!silent)
1741 printk(KERN_WARNING "UDF-fs: Bad block size\n");
1742 return 0;
1743 }
1744 sbi->s_last_block = uopt->lastblock;
1745 if (!uopt->novrs) {
1746 /* Check that it is NSR02 compliant */
1747 nsr_off = udf_check_vsd(sb);
1748 if (!nsr_off) {
1749 if (!silent)
1750 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1751 return 0;
1752 }
1753 if (nsr_off == -1)
1754 udf_debug("Failed to read byte 32768. Assuming open "
1755 "disc. Skipping validity check\n");
1756 if (!sbi->s_last_block)
1757 sbi->s_last_block = udf_get_last_block(sb);
1758 } else {
1759 udf_debug("Validity check skipped because of novrs option\n");
28f7c4d4 1760 }
1da177e4 1761
40346005
JK
1762 /* Look for anchor block and load Volume Descriptor Sequence */
1763 sbi->s_anchor = uopt->anchor;
1764 if (!udf_find_anchor(sb, fileset)) {
1765 if (!silent)
1766 printk(KERN_WARNING "UDF-fs: No anchor found\n");
1767 return 0;
1768 }
1769 return 1;
1da177e4
LT
1770}
1771
1772static void udf_open_lvid(struct super_block *sb)
1773{
6c79e987
MS
1774 struct udf_sb_info *sbi = UDF_SB(sb);
1775 struct buffer_head *bh = sbi->s_lvid_bh;
165923fa
MS
1776 struct logicalVolIntegrityDesc *lvid;
1777 struct logicalVolIntegrityDescImpUse *lvidiu;
146bca72 1778
165923fa
MS
1779 if (!bh)
1780 return;
949f4a7c
JK
1781
1782 mutex_lock(&sbi->s_alloc_mutex);
165923fa
MS
1783 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1784 lvidiu = udf_sb_lvidiu(sbi);
1785
1786 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1787 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1788 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1789 CURRENT_TIME);
146bca72 1790 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
165923fa
MS
1791
1792 lvid->descTag.descCRC = cpu_to_le16(
5ca4e4be 1793 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
f845fced 1794 le16_to_cpu(lvid->descTag.descCRCLength)));
165923fa
MS
1795
1796 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1797 mark_buffer_dirty(bh);
146bca72 1798 sbi->s_lvid_dirty = 0;
949f4a7c 1799 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
1800}
1801
1802static void udf_close_lvid(struct super_block *sb)
1803{
6c79e987
MS
1804 struct udf_sb_info *sbi = UDF_SB(sb);
1805 struct buffer_head *bh = sbi->s_lvid_bh;
1806 struct logicalVolIntegrityDesc *lvid;
165923fa 1807 struct logicalVolIntegrityDescImpUse *lvidiu;
28de7948 1808
6c79e987
MS
1809 if (!bh)
1810 return;
1811
949f4a7c 1812 mutex_lock(&sbi->s_alloc_mutex);
6c79e987 1813 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
165923fa
MS
1814 lvidiu = udf_sb_lvidiu(sbi);
1815 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1816 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1817 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1818 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1819 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1820 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1821 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1822 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1823 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1824 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
1825
1826 lvid->descTag.descCRC = cpu_to_le16(
5ca4e4be 1827 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
f845fced 1828 le16_to_cpu(lvid->descTag.descCRCLength)));
165923fa
MS
1829
1830 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1831 mark_buffer_dirty(bh);
146bca72 1832 sbi->s_lvid_dirty = 0;
949f4a7c 1833 mutex_unlock(&sbi->s_alloc_mutex);
1da177e4
LT
1834}
1835
d664b6af
JK
1836u64 lvid_get_unique_id(struct super_block *sb)
1837{
1838 struct buffer_head *bh;
1839 struct udf_sb_info *sbi = UDF_SB(sb);
1840 struct logicalVolIntegrityDesc *lvid;
1841 struct logicalVolHeaderDesc *lvhd;
1842 u64 uniqueID;
1843 u64 ret;
1844
1845 bh = sbi->s_lvid_bh;
1846 if (!bh)
1847 return 0;
1848
1849 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1850 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
1851
1852 mutex_lock(&sbi->s_alloc_mutex);
1853 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
1854 if (!(++uniqueID & 0xFFFFFFFF))
1855 uniqueID += 16;
1856 lvhd->uniqueID = cpu_to_le64(uniqueID);
1857 mutex_unlock(&sbi->s_alloc_mutex);
1858 mark_buffer_dirty(bh);
1859
1860 return ret;
1da177e4
LT
1861}
1862
66e1da3f
MS
1863static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1864{
1865 int i;
1866 int nr_groups = bitmap->s_nr_groups;
4b11111a
MS
1867 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1868 nr_groups);
66e1da3f
MS
1869
1870 for (i = 0; i < nr_groups; i++)
1871 if (bitmap->s_block_bitmap[i])
1872 brelse(bitmap->s_block_bitmap[i]);
1873
1874 if (size <= PAGE_SIZE)
1875 kfree(bitmap);
1876 else
1877 vfree(bitmap);
1878}
1879
2e0838fd
JK
1880static void udf_free_partition(struct udf_part_map *map)
1881{
1882 int i;
bfb257a5 1883 struct udf_meta_data *mdata;
2e0838fd
JK
1884
1885 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1886 iput(map->s_uspace.s_table);
1887 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1888 iput(map->s_fspace.s_table);
1889 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1890 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1891 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1892 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1893 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1894 for (i = 0; i < 4; i++)
1895 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
bfb257a5
JK
1896 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1897 mdata = &map->s_type_specific.s_metadata;
1898 iput(mdata->s_metadata_fe);
1899 mdata->s_metadata_fe = NULL;
1900
1901 iput(mdata->s_mirror_fe);
1902 mdata->s_mirror_fe = NULL;
1903
1904 iput(mdata->s_bitmap_fe);
1905 mdata->s_bitmap_fe = NULL;
1906 }
2e0838fd
JK
1907}
1908
1da177e4
LT
1909static int udf_fill_super(struct super_block *sb, void *options, int silent)
1910{
1911 int i;
40346005 1912 int ret;
cb00ea35 1913 struct inode *inode = NULL;
1da177e4 1914 struct udf_options uopt;
5ca4e4be 1915 struct kernel_lb_addr rootdir, fileset;
1da177e4
LT
1916 struct udf_sb_info *sbi;
1917
1918 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1919 uopt.uid = -1;
1920 uopt.gid = -1;
1921 uopt.umask = 0;
87bc730c
MS
1922 uopt.fmode = UDF_INVALID_MODE;
1923 uopt.dmode = UDF_INVALID_MODE;
1da177e4 1924
6c79e987 1925 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
9db9f9e3 1926 if (!sbi)
1da177e4 1927 return -ENOMEM;
28de7948 1928
1da177e4 1929 sb->s_fs_info = sbi;
1da177e4 1930
1e7933de 1931 mutex_init(&sbi->s_alloc_mutex);
1da177e4 1932
6da80894 1933 if (!udf_parse_options((char *)options, &uopt, false))
1da177e4
LT
1934 goto error_out;
1935
1936 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
cb00ea35 1937 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
1da177e4 1938 udf_error(sb, "udf_read_super",
cb00ea35 1939 "utf8 cannot be combined with iocharset\n");
1da177e4
LT
1940 goto error_out;
1941 }
1942#ifdef CONFIG_UDF_NLS
cb00ea35 1943 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
1da177e4
LT
1944 uopt.nls_map = load_nls_default();
1945 if (!uopt.nls_map)
1946 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1947 else
1948 udf_debug("Using default NLS map\n");
1949 }
1950#endif
1951 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1952 uopt.flags |= (1 << UDF_FLAG_UTF8);
1953
1954 fileset.logicalBlockNum = 0xFFFFFFFF;
1955 fileset.partitionReferenceNum = 0xFFFF;
1956
6c79e987
MS
1957 sbi->s_flags = uopt.flags;
1958 sbi->s_uid = uopt.uid;
1959 sbi->s_gid = uopt.gid;
1960 sbi->s_umask = uopt.umask;
7ac9bcd5
MS
1961 sbi->s_fmode = uopt.fmode;
1962 sbi->s_dmode = uopt.dmode;
6c79e987 1963 sbi->s_nls_map = uopt.nls_map;
c03cad24 1964 rwlock_init(&sbi->s_cred_lock);
1da177e4 1965
cb00ea35 1966 if (uopt.session == 0xFFFFFFFF)
6c79e987 1967 sbi->s_session = udf_get_last_session(sb);
1da177e4 1968 else
6c79e987 1969 sbi->s_session = uopt.session;
1da177e4 1970
6c79e987 1971 udf_debug("Multi-session=%d\n", sbi->s_session);
1da177e4 1972
40346005
JK
1973 /* Fill in the rest of the superblock */
1974 sb->s_op = &udf_sb_ops;
1975 sb->s_export_op = &udf_export_ops;
123e9caf 1976
40346005
JK
1977 sb->s_dirt = 0;
1978 sb->s_magic = UDF_SUPER_MAGIC;
1979 sb->s_time_gran = 1000;
1980
1197e4df 1981 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
40346005 1982 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1197e4df 1983 } else {
e1defc4f 1984 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
40346005
JK
1985 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1986 if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
1197e4df
CL
1987 if (!silent)
1988 printk(KERN_NOTICE
1989 "UDF-fs: Rescanning with blocksize "
1990 "%d\n", UDF_DEFAULT_BLOCKSIZE);
1991 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
40346005 1992 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1197e4df 1993 }
1da177e4 1994 }
40346005 1995 if (!ret) {
3a71fc5d 1996 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
1da177e4
LT
1997 goto error_out;
1998 }
1999
6c79e987 2000 udf_debug("Lastblock=%d\n", sbi->s_last_block);
1da177e4 2001
6c79e987 2002 if (sbi->s_lvid_bh) {
4b11111a
MS
2003 struct logicalVolIntegrityDescImpUse *lvidiu =
2004 udf_sb_lvidiu(sbi);
6c79e987
MS
2005 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2006 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
4b11111a
MS
2007 /* uint16_t maxUDFWriteRev =
2008 le16_to_cpu(lvidiu->maxUDFWriteRev); */
1da177e4 2009
cb00ea35 2010 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
4b11111a
MS
2011 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
2012 "(max is %x)\n",
6c79e987 2013 le16_to_cpu(lvidiu->minUDFReadRev),
cb00ea35 2014 UDF_MAX_READ_VERSION);
1da177e4 2015 goto error_out;
4b11111a 2016 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
1da177e4 2017 sb->s_flags |= MS_RDONLY;
1da177e4 2018
6c79e987 2019 sbi->s_udfrev = minUDFWriteRev;
1da177e4
LT
2020
2021 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2022 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2023 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2024 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2025 }
2026
6c79e987 2027 if (!sbi->s_partitions) {
3a71fc5d 2028 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
1da177e4
LT
2029 goto error_out;
2030 }
2031
4b11111a
MS
2032 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2033 UDF_PART_FLAG_READ_ONLY) {
2034 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
2035 "forcing readonly mount\n");
39b3f6d6 2036 sb->s_flags |= MS_RDONLY;
c1a26e7d 2037 }
39b3f6d6 2038
cb00ea35 2039 if (udf_find_fileset(sb, &fileset, &rootdir)) {
3a71fc5d 2040 printk(KERN_WARNING "UDF-fs: No fileset found\n");
1da177e4
LT
2041 goto error_out;
2042 }
2043
cb00ea35 2044 if (!silent) {
5ca4e4be 2045 struct timestamp ts;
56774805 2046 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
a9ca6635 2047 udf_info("UDF: Mounting volume '%s', "
28de7948 2048 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
56774805
MS
2049 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2050 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
1da177e4
LT
2051 }
2052 if (!(sb->s_flags & MS_RDONLY))
2053 udf_open_lvid(sb);
2054
2055 /* Assign the root inode */
2056 /* assign inodes by physical block number */
2057 /* perhaps it's not extensible enough, but for now ... */
97e961fd 2058 inode = udf_iget(sb, &rootdir);
cb00ea35 2059 if (!inode) {
4b11111a
MS
2060 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2061 "partition=%d\n",
cb00ea35 2062 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
1da177e4
LT
2063 goto error_out;
2064 }
2065
2066 /* Allocate a dentry for the root inode */
2067 sb->s_root = d_alloc_root(inode);
cb00ea35 2068 if (!sb->s_root) {
3a71fc5d 2069 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
1da177e4
LT
2070 iput(inode);
2071 goto error_out;
2072 }
31170b6a 2073 sb->s_maxbytes = MAX_LFS_FILESIZE;
1da177e4
LT
2074 return 0;
2075
28de7948 2076error_out:
6c79e987
MS
2077 if (sbi->s_vat_inode)
2078 iput(sbi->s_vat_inode);
2e0838fd
JK
2079 if (sbi->s_partitions)
2080 for (i = 0; i < sbi->s_partitions; i++)
2081 udf_free_partition(&sbi->s_partmaps[i]);
1da177e4
LT
2082#ifdef CONFIG_UDF_NLS
2083 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
6c79e987 2084 unload_nls(sbi->s_nls_map);
1da177e4
LT
2085#endif
2086 if (!(sb->s_flags & MS_RDONLY))
2087 udf_close_lvid(sb);
6c79e987
MS
2088 brelse(sbi->s_lvid_bh);
2089
2090 kfree(sbi->s_partmaps);
1da177e4
LT
2091 kfree(sbi);
2092 sb->s_fs_info = NULL;
28de7948 2093
1da177e4
LT
2094 return -EINVAL;
2095}
2096
7e273e3b
JP
2097void udf_error(struct super_block *sb, const char *function,
2098 const char *fmt, ...)
1da177e4
LT
2099{
2100 va_list args;
2101
cb00ea35 2102 if (!(sb->s_flags & MS_RDONLY)) {
1da177e4
LT
2103 /* mark sb error */
2104 sb->s_dirt = 1;
2105 }
2106 va_start(args, fmt);
4a6e617a 2107 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1da177e4 2108 va_end(args);
3a71fc5d 2109 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
28de7948 2110 sb->s_id, function, error_buf);
1da177e4
LT
2111}
2112
2113void udf_warning(struct super_block *sb, const char *function,
cb00ea35 2114 const char *fmt, ...)
1da177e4
LT
2115{
2116 va_list args;
2117
cb00ea35 2118 va_start(args, fmt);
4a6e617a 2119 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
1da177e4
LT
2120 va_end(args);
2121 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
cb00ea35 2122 sb->s_id, function, error_buf);
1da177e4
LT
2123}
2124
cb00ea35 2125static void udf_put_super(struct super_block *sb)
1da177e4
LT
2126{
2127 int i;
6c79e987 2128 struct udf_sb_info *sbi;
1da177e4 2129
6c79e987 2130 sbi = UDF_SB(sb);
6cfd0148 2131
6c79e987
MS
2132 if (sbi->s_vat_inode)
2133 iput(sbi->s_vat_inode);
2e0838fd
JK
2134 if (sbi->s_partitions)
2135 for (i = 0; i < sbi->s_partitions; i++)
2136 udf_free_partition(&sbi->s_partmaps[i]);
1da177e4
LT
2137#ifdef CONFIG_UDF_NLS
2138 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
6c79e987 2139 unload_nls(sbi->s_nls_map);
1da177e4
LT
2140#endif
2141 if (!(sb->s_flags & MS_RDONLY))
2142 udf_close_lvid(sb);
6c79e987
MS
2143 brelse(sbi->s_lvid_bh);
2144 kfree(sbi->s_partmaps);
1da177e4
LT
2145 kfree(sb->s_fs_info);
2146 sb->s_fs_info = NULL;
2147}
2148
146bca72
JK
2149static int udf_sync_fs(struct super_block *sb, int wait)
2150{
2151 struct udf_sb_info *sbi = UDF_SB(sb);
2152
2153 mutex_lock(&sbi->s_alloc_mutex);
2154 if (sbi->s_lvid_dirty) {
2155 /*
2156 * Blockdevice will be synced later so we don't have to submit
2157 * the buffer for IO
2158 */
2159 mark_buffer_dirty(sbi->s_lvid_bh);
2160 sb->s_dirt = 0;
2161 sbi->s_lvid_dirty = 0;
2162 }
2163 mutex_unlock(&sbi->s_alloc_mutex);
2164
2165 return 0;
2166}
2167
cb00ea35 2168static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 2169{
726c3342 2170 struct super_block *sb = dentry->d_sb;
6c79e987
MS
2171 struct udf_sb_info *sbi = UDF_SB(sb);
2172 struct logicalVolIntegrityDescImpUse *lvidiu;
557f5a14 2173 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
6c79e987
MS
2174
2175 if (sbi->s_lvid_bh != NULL)
2176 lvidiu = udf_sb_lvidiu(sbi);
2177 else
2178 lvidiu = NULL;
726c3342 2179
1da177e4
LT
2180 buf->f_type = UDF_SUPER_MAGIC;
2181 buf->f_bsize = sb->s_blocksize;
6c79e987 2182 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
1da177e4
LT
2183 buf->f_bfree = udf_count_free(sb);
2184 buf->f_bavail = buf->f_bfree;
6c79e987
MS
2185 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2186 le32_to_cpu(lvidiu->numDirs)) : 0)
2187 + buf->f_bfree;
1da177e4 2188 buf->f_ffree = buf->f_bfree;
cb00ea35 2189 buf->f_namelen = UDF_NAME_LEN - 2;
557f5a14
CL
2190 buf->f_fsid.val[0] = (u32)id;
2191 buf->f_fsid.val[1] = (u32)(id >> 32);
1da177e4
LT
2192
2193 return 0;
2194}
2195
4b11111a
MS
2196static unsigned int udf_count_free_bitmap(struct super_block *sb,
2197 struct udf_bitmap *bitmap)
1da177e4
LT
2198{
2199 struct buffer_head *bh = NULL;
2200 unsigned int accum = 0;
2201 int index;
2202 int block = 0, newblock;
5ca4e4be 2203 struct kernel_lb_addr loc;
1da177e4 2204 uint32_t bytes;
1da177e4
LT
2205 uint8_t *ptr;
2206 uint16_t ident;
2207 struct spaceBitmapDesc *bm;
2208
1da177e4 2209 loc.logicalBlockNum = bitmap->s_extPosition;
6c79e987 2210 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
97e961fd 2211 bh = udf_read_ptagged(sb, &loc, 0, &ident);
1da177e4 2212
cb00ea35 2213 if (!bh) {
1da177e4
LT
2214 printk(KERN_ERR "udf: udf_count_free failed\n");
2215 goto out;
cb00ea35 2216 } else if (ident != TAG_IDENT_SBD) {
3bf25cb4 2217 brelse(bh);
1da177e4
LT
2218 printk(KERN_ERR "udf: udf_count_free failed\n");
2219 goto out;
2220 }
2221
2222 bm = (struct spaceBitmapDesc *)bh->b_data;
2223 bytes = le32_to_cpu(bm->numOfBytes);
28de7948
CG
2224 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2225 ptr = (uint8_t *)bh->b_data;
1da177e4 2226
cb00ea35 2227 while (bytes > 0) {
01b954a3
MS
2228 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2229 accum += bitmap_weight((const unsigned long *)(ptr + index),
2230 cur_bytes * 8);
2231 bytes -= cur_bytes;
cb00ea35 2232 if (bytes) {
3bf25cb4 2233 brelse(bh);
97e961fd 2234 newblock = udf_get_lb_pblock(sb, &loc, ++block);
1da177e4 2235 bh = udf_tread(sb, newblock);
cb00ea35 2236 if (!bh) {
1da177e4
LT
2237 udf_debug("read failed\n");
2238 goto out;
2239 }
2240 index = 0;
28de7948 2241 ptr = (uint8_t *)bh->b_data;
1da177e4
LT
2242 }
2243 }
3bf25cb4 2244 brelse(bh);
28de7948 2245out:
1da177e4
LT
2246 return accum;
2247}
2248
4b11111a
MS
2249static unsigned int udf_count_free_table(struct super_block *sb,
2250 struct inode *table)
1da177e4
LT
2251{
2252 unsigned int accum = 0;
ff116fc8 2253 uint32_t elen;
5ca4e4be 2254 struct kernel_lb_addr eloc;
1da177e4 2255 int8_t etype;
ff116fc8 2256 struct extent_position epos;
1da177e4 2257
d1668fe3 2258 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
c0b34438 2259 epos.block = UDF_I(table)->i_location;
ff116fc8
JK
2260 epos.offset = sizeof(struct unallocSpaceEntry);
2261 epos.bh = NULL;
1da177e4 2262
3a71fc5d 2263 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
1da177e4 2264 accum += (elen >> table->i_sb->s_blocksize_bits);
3a71fc5d 2265
3bf25cb4 2266 brelse(epos.bh);
d1668fe3 2267 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
1da177e4
LT
2268
2269 return accum;
2270}
cb00ea35
CG
2271
2272static unsigned int udf_count_free(struct super_block *sb)
1da177e4
LT
2273{
2274 unsigned int accum = 0;
6c79e987
MS
2275 struct udf_sb_info *sbi;
2276 struct udf_part_map *map;
1da177e4 2277
6c79e987
MS
2278 sbi = UDF_SB(sb);
2279 if (sbi->s_lvid_bh) {
4b11111a
MS
2280 struct logicalVolIntegrityDesc *lvid =
2281 (struct logicalVolIntegrityDesc *)
2282 sbi->s_lvid_bh->b_data;
6c79e987 2283 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
4b11111a
MS
2284 accum = le32_to_cpu(
2285 lvid->freeSpaceTable[sbi->s_partition]);
1da177e4
LT
2286 if (accum == 0xFFFFFFFF)
2287 accum = 0;
2288 }
2289 }
2290
2291 if (accum)
2292 return accum;
2293
6c79e987
MS
2294 map = &sbi->s_partmaps[sbi->s_partition];
2295 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
28de7948 2296 accum += udf_count_free_bitmap(sb,
6c79e987 2297 map->s_uspace.s_bitmap);
1da177e4 2298 }
6c79e987 2299 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
28de7948 2300 accum += udf_count_free_bitmap(sb,
6c79e987 2301 map->s_fspace.s_bitmap);
1da177e4
LT
2302 }
2303 if (accum)
2304 return accum;
2305
6c79e987 2306 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
28de7948 2307 accum += udf_count_free_table(sb,
6c79e987 2308 map->s_uspace.s_table);
1da177e4 2309 }
6c79e987 2310 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
28de7948 2311 accum += udf_count_free_table(sb,
6c79e987 2312 map->s_fspace.s_table);
1da177e4
LT
2313 }
2314
2315 return accum;
2316}