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