blob: 3723f04c07995da1c5082b3a339af6165675da83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * super.c
3 *
4 * PURPOSE
5 * Super block routines for the OSTA-UDF(tm) filesystem.
6 *
7 * DESCRIPTION
8 * OSTA-UDF(tm) = Optical Storage Technology Association
9 * Universal Disk Format.
10 *
11 * This code is based on version 2.00 of the UDF specification,
12 * and revision 3 of the ECMA 167 standard [equivalent to ISO 13346].
13 * http://www.osta.org/
14 * http://www.ecma.ch/
15 * http://www.iso.org/
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * COPYRIGHT
18 * This file is distributed under the terms of the GNU General Public
19 * License (GPL). Copies of the GPL can be obtained from:
20 * ftp://prep.ai.mit.edu/pub/gnu/GPL
21 * Each contributing author retains all rights to their own work.
22 *
23 * (C) 1998 Dave Boynton
24 * (C) 1998-2004 Ben Fennema
25 * (C) 2000 Stelias Computing Inc
26 *
27 * HISTORY
28 *
29 * 09/24/98 dgb changed to allow compiling outside of kernel, and
30 * added some debugging.
31 * 10/01/98 dgb updated to allow (some) possibility of compiling w/2.0.34
32 * 10/16/98 attempting some multi-session support
33 * 10/17/98 added freespace count for "df"
34 * 11/11/98 gr added novrs option
35 * 11/26/98 dgb added fileset,anchor mount options
Marcin Slusarz3a71fc52008-02-08 04:20:28 -080036 * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced
37 * vol descs. rewrote option handling based on isofs
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * 12/20/98 find the free space bitmap (if it exists)
39 */
40
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070041#include "udfdecl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/blkdev.h>
44#include <linux/slab.h>
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/parser.h>
48#include <linux/stat.h>
49#include <linux/cdrom.h>
50#include <linux/nls.h>
51#include <linux/smp_lock.h>
52#include <linux/buffer_head.h>
53#include <linux/vfs.h>
54#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080055#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080056#include <linux/mount.h>
57#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/byteorder.h>
59
60#include <linux/udf_fs.h>
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
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
83static void udf_write_super(struct super_block *);
84static int udf_remount_fs(struct super_block *, int *, char *);
85static int udf_check_valid(struct super_block *, int, int);
86static int udf_vrs(struct super_block *sb, int silent);
87static int udf_load_partition(struct super_block *, kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088static int udf_load_logicalvol(struct super_block *, struct buffer_head *,
89 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void udf_load_logicalvolint(struct super_block *, kernel_extent_ad);
91static void udf_find_anchor(struct super_block *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static int udf_find_fileset(struct super_block *, kernel_lb_addr *,
93 kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_load_pvoldesc(struct super_block *, struct buffer_head *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095static void udf_load_fileset(struct super_block *, struct buffer_head *,
96 kernel_lb_addr *);
Jan Karabcec4472007-08-30 23:56:22 -070097static int udf_load_partdesc(struct super_block *, struct buffer_head *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static void udf_open_lvid(struct super_block *);
99static void udf_close_lvid(struct super_block *);
100static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -0700101static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800102static int udf_show_options(struct seq_file *, struct vfsmount *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800104struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
105{
Marcin Slusarz4b111112008-02-08 04:20:36 -0800106 struct logicalVolIntegrityDesc *lvid =
107 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800109 __u32 offset = number_of_partitions * 2 *
110 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800111 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* UDF filesystem type */
David Howells454e2392006-06-23 02:02:57 -0700115static int udf_get_sb(struct file_system_type *fs_type,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 int flags, const char *dev_name, void *data,
117 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David Howells454e2392006-06-23 02:02:57 -0700119 return get_sb_bdev(fs_type, flags, dev_name, data, udf_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700123 .owner = THIS_MODULE,
124 .name = "udf",
125 .get_sb = udf_get_sb,
126 .kill_sb = kill_block_super,
127 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700130static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132static struct inode *udf_alloc_inode(struct super_block *sb)
133{
134 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800135 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (!ei)
137 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700138
139 ei->i_unique = 0;
140 ei->i_lenExtents = 0;
141 ei->i_next_alloc_block = 0;
142 ei->i_next_alloc_goal = 0;
143 ei->i_strat4096 = 0;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return &ei->vfs_inode;
146}
147
148static void udf_destroy_inode(struct inode *inode)
149{
150 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
151}
152
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700153static void init_once(struct kmem_cache *cachep, void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Christoph Lametera35afb82007-05-16 22:10:57 -0700157 ei->i_ext.i_data = NULL;
158 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static int init_inodecache(void)
162{
163 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
164 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 0, (SLAB_RECLAIM_ACCOUNT |
166 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900167 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700168 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return -ENOMEM;
170 return 0;
171}
172
173static void destroy_inodecache(void)
174{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700175 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800179static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700180 .alloc_inode = udf_alloc_inode,
181 .destroy_inode = udf_destroy_inode,
182 .write_inode = udf_write_inode,
183 .delete_inode = udf_delete_inode,
184 .clear_inode = udf_clear_inode,
185 .put_super = udf_put_super,
186 .write_super = udf_write_super,
187 .statfs = udf_statfs,
188 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800189 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};
191
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned char novrs;
194 unsigned int blocksize;
195 unsigned int session;
196 unsigned int lastblock;
197 unsigned int anchor;
198 unsigned int volume;
199 unsigned short partition;
200 unsigned int fileset;
201 unsigned int rootdir;
202 unsigned int flags;
203 mode_t umask;
204 gid_t gid;
205 uid_t uid;
206 struct nls_table *nls_map;
207};
208
209static int __init init_udf_fs(void)
210{
211 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 err = init_inodecache();
214 if (err)
215 goto out1;
216 err = register_filesystem(&udf_fstype);
217 if (err)
218 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700221
222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700224
225out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 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)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700236module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800238static 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) {
245 udf_error(sb, __FUNCTION__,
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
Miklos Szeredi6da80892008-02-08 04:21:50 -0800256static 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");
263 if (sb->s_blocksize != UDF_DEFAULT_BLOCKSIZE)
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);
287 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
288 seq_printf(seq, ",session=%u", sbi->s_session);
289 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
290 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
291 /*
292 * s_anchor[2] could be zeroed out in case there is no anchor
293 * in the specified block, but then the "anchor=N" option
294 * originally given by the user wasn't effective, so it's OK
295 * if we don't show it.
296 */
297 if (sbi->s_anchor[2] != 0)
298 seq_printf(seq, ",anchor=%u", sbi->s_anchor[2]);
299 /*
300 * volume, partition, fileset and rootdir seem to be ignored
301 * currently
302 */
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
304 seq_puts(seq, ",utf8");
305 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
306 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
307
308 return 0;
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
312 * udf_parse_options
313 *
314 * PURPOSE
315 * Parse mount options.
316 *
317 * DESCRIPTION
318 * The following mount options are supported:
319 *
320 * gid= Set the default group.
321 * umask= Set the default umask.
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 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700335 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 *
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 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365enum {
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,
Phillip Susi4d6660e2006-03-07 21:55:24 -0800371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372};
373
374static match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700375 {Opt_novrs, "novrs"},
376 {Opt_nostrict, "nostrict"},
377 {Opt_bs, "bs=%u"},
378 {Opt_unhide, "unhide"},
379 {Opt_undelete, "undelete"},
380 {Opt_noadinicb, "noadinicb"},
381 {Opt_adinicb, "adinicb"},
382 {Opt_shortad, "shortad"},
383 {Opt_longad, "longad"},
384 {Opt_uforget, "uid=forget"},
385 {Opt_uignore, "uid=ignore"},
386 {Opt_gforget, "gid=forget"},
387 {Opt_gignore, "gid=ignore"},
388 {Opt_gid, "gid=%u"},
389 {Opt_uid, "uid=%u"},
390 {Opt_umask, "umask=%o"},
391 {Opt_session, "session=%u"},
392 {Opt_lastblock, "lastblock=%u"},
393 {Opt_anchor, "anchor=%u"},
394 {Opt_volume, "volume=%u"},
395 {Opt_partition, "partition=%u"},
396 {Opt_fileset, "fileset=%u"},
397 {Opt_rootdir, "rootdir=%u"},
398 {Opt_utf8, "utf8"},
399 {Opt_iocharset, "iocharset=%s"},
400 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Miklos Szeredi6da80892008-02-08 04:21:50 -0800403static int udf_parse_options(char *options, struct udf_options *uopt,
404 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 char *p;
407 int option;
408
409 uopt->novrs = 0;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800410 uopt->blocksize = UDF_DEFAULT_BLOCKSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 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
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700423 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 substring_t args[MAX_OPT_ARGS];
425 int token;
426 if (!*p)
427 continue;
428
429 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700430 switch (token) {
431 case Opt_novrs:
432 uopt->novrs = 1;
433 case Opt_bs:
434 if (match_int(&args[0], &option))
435 return 0;
436 uopt->blocksize = option;
437 break;
438 case Opt_unhide:
439 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
440 break;
441 case Opt_undelete:
442 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
443 break;
444 case Opt_noadinicb:
445 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
446 break;
447 case Opt_adinicb:
448 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
449 break;
450 case Opt_shortad:
451 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
452 break;
453 case Opt_longad:
454 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
455 break;
456 case Opt_gid:
457 if (match_int(args, &option))
458 return 0;
459 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700460 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700461 break;
462 case Opt_uid:
463 if (match_int(args, &option))
464 return 0;
465 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700466 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700467 break;
468 case Opt_umask:
469 if (match_octal(args, &option))
470 return 0;
471 uopt->umask = option;
472 break;
473 case Opt_nostrict:
474 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
475 break;
476 case Opt_session:
477 if (match_int(args, &option))
478 return 0;
479 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800480 if (!remount)
481 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700482 break;
483 case Opt_lastblock:
484 if (match_int(args, &option))
485 return 0;
486 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800487 if (!remount)
488 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700489 break;
490 case Opt_anchor:
491 if (match_int(args, &option))
492 return 0;
493 uopt->anchor = option;
494 break;
495 case Opt_volume:
496 if (match_int(args, &option))
497 return 0;
498 uopt->volume = option;
499 break;
500 case Opt_partition:
501 if (match_int(args, &option))
502 return 0;
503 uopt->partition = option;
504 break;
505 case Opt_fileset:
506 if (match_int(args, &option))
507 return 0;
508 uopt->fileset = option;
509 break;
510 case Opt_rootdir:
511 if (match_int(args, &option))
512 return 0;
513 uopt->rootdir = option;
514 break;
515 case Opt_utf8:
516 uopt->flags |= (1 << UDF_FLAG_UTF8);
517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700519 case Opt_iocharset:
520 uopt->nls_map = load_nls(args[0].from);
521 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700524 case Opt_uignore:
525 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
526 break;
527 case Opt_uforget:
528 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
529 break;
530 case Opt_gignore:
531 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
532 break;
533 case Opt_gforget:
534 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
535 break;
536 default:
537 printk(KERN_ERR "udf: bad mount option \"%s\" "
538 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 0;
540 }
541 }
542 return 1;
543}
544
Marcin Slusarzbd45a422008-02-08 04:20:35 -0800545static void udf_write_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 lock_kernel();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (!(sb->s_flags & MS_RDONLY))
550 udf_open_lvid(sb);
551 sb->s_dirt = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unlock_kernel();
554}
555
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800559 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800561 uopt.flags = sbi->s_flags;
562 uopt.uid = sbi->s_uid;
563 uopt.gid = sbi->s_gid;
564 uopt.umask = sbi->s_umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Miklos Szeredi6da80892008-02-08 04:21:50 -0800566 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return -EINVAL;
568
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800569 sbi->s_flags = uopt.flags;
570 sbi->s_uid = uopt.uid;
571 sbi->s_gid = uopt.gid;
572 sbi->s_umask = uopt.umask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800574 if (sbi->s_lvid_bh) {
575 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (write_rev > UDF_MAX_WRITE_VERSION)
577 *flags |= MS_RDONLY;
578 }
579
580 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
581 return 0;
582 if (*flags & MS_RDONLY)
583 udf_close_lvid(sb);
584 else
585 udf_open_lvid(sb);
586
587 return 0;
588}
589
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700590static int udf_vrs(struct super_block *sb, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 struct volStructDesc *vsd = NULL;
593 int sector = 32768;
594 int sectorsize;
595 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700596 int iso9660 = 0;
597 int nsr02 = 0;
598 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800599 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* Block size must be a multiple of 512 */
602 if (sb->s_blocksize & 511)
603 return 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800604 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (sb->s_blocksize < sizeof(struct volStructDesc))
607 sectorsize = sizeof(struct volStructDesc);
608 else
609 sectorsize = sb->s_blocksize;
610
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800611 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 (sector >> sb->s_blocksize_bits), sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700616 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Read a block */
618 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
619 if (!bh)
620 break;
621
622 /* Look for ISO descriptors */
623 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800624 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700626 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700627 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800629 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
630 VSD_STD_ID_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 iso9660 = sector;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700632 switch (vsd->structType) {
633 case 0:
634 udf_debug("ISO9660 Boot Record found\n");
635 break;
636 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800637 udf_debug("ISO9660 Primary Volume Descriptor "
638 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700639 break;
640 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800641 udf_debug("ISO9660 Supplementary Volume "
642 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700643 break;
644 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800645 udf_debug("ISO9660 Volume Partition Descriptor "
646 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700647 break;
648 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800649 udf_debug("ISO9660 Volume Descriptor Set "
650 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700651 break;
652 default:
653 udf_debug("ISO9660 VRS (%u) found\n",
654 vsd->structType);
655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800657 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
658 VSD_STD_ID_LEN))
659 ; /* nothing */
660 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
661 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700662 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800664 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
665 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800667 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
668 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700670 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 if (nsr03)
674 return nsr03;
675 else if (nsr02)
676 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800677 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return -1;
679 else
680 return 0;
681}
682
683/*
684 * udf_find_anchor
685 *
686 * PURPOSE
687 * Find an anchor volume descriptor.
688 *
689 * PRE-CONDITIONS
690 * sb Pointer to _locked_ superblock.
691 * lastblock Last block on media.
692 *
693 * POST-CONDITIONS
694 * <return> 1 if not found, 0 if ok
695 *
696 * HISTORY
697 * July 1, 1997 - Andrew E. Mileski
698 * Written, tested, and released.
699 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700700static void udf_find_anchor(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800702 int lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct buffer_head *bh = NULL;
704 uint16_t ident;
705 uint32_t location;
706 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800707 struct udf_sb_info *sbi;
708
709 sbi = UDF_SB(sb);
710 lastblock = sbi->s_last_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700712 if (lastblock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int varlastblock = udf_variable_to_fixed(lastblock);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700714 int last[] = { lastblock, lastblock - 2,
715 lastblock - 150, lastblock - 152,
716 varlastblock, varlastblock - 2,
717 varlastblock - 150, varlastblock - 152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 lastblock = 0;
720
721 /* Search for an anchor volume descriptor pointer */
722
723 /* according to spec, anchor is in either:
724 * block 256
725 * lastblock-256
726 * lastblock
727 * however, if the disc isn't closed, it could be 512 */
728
Tobias Klausere8c96f82006-03-24 03:15:34 -0800729 for (i = 0; !lastblock && i < ARRAY_SIZE(last); i++) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800730 ident = location = 0;
731 if (last[i] >= 0) {
732 bh = sb_bread(sb, last[i]);
733 if (bh) {
734 tag *t = (tag *)bh->b_data;
735 ident = le16_to_cpu(t->tagIdent);
736 location = le32_to_cpu(t->tagLocation);
737 brelse(bh);
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Tobias Klausere8c96f82006-03-24 03:15:34 -0800740
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700741 if (ident == TAG_IDENT_AVDP) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800742 if (location == last[i] - sbi->s_session) {
743 lastblock = last[i] - sbi->s_session;
744 sbi->s_anchor[0] = lastblock;
745 sbi->s_anchor[1] = lastblock - 256;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800746 } else if (location ==
747 udf_variable_to_fixed(last[i]) -
748 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800750 lastblock =
751 udf_variable_to_fixed(last[i]) -
752 sbi->s_session;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800753 sbi->s_anchor[0] = lastblock;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800754 sbi->s_anchor[1] = lastblock - 256 -
755 sbi->s_session;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700756 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800757 udf_debug("Anchor found at block %d, "
758 "location mismatch %d.\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700759 last[i], location);
760 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800761 } else if (ident == TAG_IDENT_FE ||
762 ident == TAG_IDENT_EFE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800764 sbi->s_anchor[3] = 512;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700765 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800766 ident = location = 0;
767 if (last[i] >= 256) {
768 bh = sb_bread(sb, last[i] - 256);
769 if (bh) {
770 tag *t = (tag *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800771 ident = le16_to_cpu(
772 t->tagIdent);
773 location = le32_to_cpu(
774 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800775 brelse(bh);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (ident == TAG_IDENT_AVDP &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800780 location == last[i] - 256 -
781 sbi->s_session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 lastblock = last[i];
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800783 sbi->s_anchor[1] = last[i] - 256;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700784 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800785 ident = location = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800786 if (last[i] >= 312 + sbi->s_session) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800787 bh = sb_bread(sb,
788 last[i] - 312 -
789 sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800790 if (bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800791 tag *t = (tag *)
792 bh->b_data;
793 ident = le16_to_cpu(
794 t->tagIdent);
795 location = le32_to_cpu(
796 t->tagLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800797 brelse(bh);
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (ident == TAG_IDENT_AVDP &&
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700802 location == udf_variable_to_fixed(last[i]) - 256) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800803 UDF_SET_FLAG(sb,
804 UDF_FLAG_VARCONV);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700805 lastblock = udf_variable_to_fixed(last[i]);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800806 sbi->s_anchor[1] = lastblock - 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
808 }
809 }
810 }
811 }
812
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700813 if (!lastblock) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800814 /* We haven't found the lastblock. check 312 */
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800815 bh = sb_bread(sb, 312 + sbi->s_session);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800816 if (bh) {
817 tag *t = (tag *)bh->b_data;
818 ident = le16_to_cpu(t->tagIdent);
819 location = le32_to_cpu(t->tagLocation);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700820 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 if (ident == TAG_IDENT_AVDP && location == 256)
823 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
824 }
825 }
826
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800827 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
828 if (sbi->s_anchor[i]) {
829 bh = udf_read_tagged(sb, sbi->s_anchor[i],
830 sbi->s_anchor[i], &ident);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800831 if (!bh)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800832 sbi->s_anchor[i] = 0;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800833 else {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700834 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700835 if ((ident != TAG_IDENT_AVDP) &&
Marcin Slusarz4b111112008-02-08 04:20:36 -0800836 (i || (ident != TAG_IDENT_FE &&
837 ident != TAG_IDENT_EFE)))
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800838 sbi->s_anchor[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840 }
841 }
842
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800843 sbi->s_last_block = lastblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800846static int udf_find_fileset(struct super_block *sb,
847 kernel_lb_addr *fileset,
848 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct buffer_head *bh = NULL;
851 long lastblock;
852 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800853 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700856 fileset->partitionReferenceNum != 0xFFFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 bh = udf_read_ptagged(sb, *fileset, 0, &ident);
858
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700859 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700861 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700862 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 1;
864 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800868 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800869 if (!bh) {
870 /* Search backwards through the partitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 kernel_lb_addr newfileset;
872
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700873/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700875
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800876 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700877 (newfileset.partitionReferenceNum != 0xFFFF &&
878 fileset->logicalBlockNum == 0xFFFFFFFF &&
879 fileset->partitionReferenceNum == 0xFFFF);
880 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800881 lastblock = sbi->s_partmaps
882 [newfileset.partitionReferenceNum]
883 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 newfileset.logicalBlockNum = 0;
885
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700886 do {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800887 bh = udf_read_ptagged(sb, newfileset, 0,
888 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700889 if (!bh) {
890 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 continue;
892 }
893
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700894 switch (ident) {
895 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700896 {
897 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800898 sp = (struct spaceBitmapDesc *)
899 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700900 newfileset.logicalBlockNum += 1 +
901 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800902 sizeof(struct spaceBitmapDesc)
903 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700904 brelse(bh);
905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700907 case TAG_IDENT_FSD:
908 *fileset = newfileset;
909 break;
910 default:
911 newfileset.logicalBlockNum++;
912 brelse(bh);
913 bh = NULL;
914 break;
915 }
916 } while (newfileset.logicalBlockNum < lastblock &&
917 fileset->logicalBlockNum == 0xFFFFFFFF &&
918 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
921
922 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700923 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700925 fileset->logicalBlockNum,
926 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800928 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700930 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return 0;
932 }
933 return 1;
934}
935
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700936static void udf_load_pvoldesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct primaryVolDesc *pvoldesc;
939 time_t recording;
940 long recording_usec;
941 struct ustr instr;
942 struct ustr outstr;
943
944 pvoldesc = (struct primaryVolDesc *)bh->b_data;
945
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700946 if (udf_stamp_to_time(&recording, &recording_usec,
947 lets_to_cpu(pvoldesc->recordingDateAndTime))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 kernel_timestamp ts;
949 ts = lets_to_cpu(pvoldesc->recordingDateAndTime);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800950 udf_debug("recording time %ld/%ld, %04u/%02u/%02u"
951 " %02u:%02u (%x)\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700952 recording, recording_usec,
953 ts.year, ts.month, ts.day, ts.hour,
954 ts.minute, ts.typeAndTimezone);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800955 UDF_SB(sb)->s_record_time.tv_sec = recording;
956 UDF_SB(sb)->s_record_time.tv_nsec = recording_usec * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
Marcin Slusarz4b111112008-02-08 04:20:36 -0800959 if (!udf_build_ustr(&instr, pvoldesc->volIdent, 32))
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700960 if (udf_CS0toUTF8(&outstr, &instr)) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800961 strncpy(UDF_SB(sb)->s_volume_ident, outstr.u_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 outstr.u_len > 31 ? 31 : outstr.u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800963 udf_debug("volIdent[] = '%s'\n",
964 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Marcin Slusarz4b111112008-02-08 04:20:36 -0800967 if (!udf_build_ustr(&instr, pvoldesc->volSetIdent, 128))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (udf_CS0toUTF8(&outstr, &instr))
969 udf_debug("volSetIdent[] = '%s'\n", outstr.u_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700972static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
973 kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct fileSetDesc *fset;
976
977 fset = (struct fileSetDesc *)bh->b_data;
978
979 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
980
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800981 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700983 udf_debug("Rootdir at block=%d, partition=%d\n",
984 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800987int udf_compute_nr_groups(struct super_block *sb, u32 partition)
988{
989 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
990 return (map->s_partition_len +
991 (sizeof(struct spaceBitmapDesc) << 3) +
992 (sb->s_blocksize * 8) - 1) /
993 (sb->s_blocksize * 8);
994}
995
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800996static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
997{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800998 struct udf_bitmap *bitmap;
999 int nr_groups;
1000 int size;
1001
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001002 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001003 size = sizeof(struct udf_bitmap) +
1004 (sizeof(struct buffer_head *) * nr_groups);
1005
1006 if (size <= PAGE_SIZE)
1007 bitmap = kmalloc(size, GFP_KERNEL);
1008 else
1009 bitmap = vmalloc(size); /* TODO: get rid of vmalloc */
1010
1011 if (bitmap == NULL) {
1012 udf_error(sb, __FUNCTION__,
1013 "Unable to allocate space for bitmap "
1014 "and %d buffer_head pointers", nr_groups);
1015 return NULL;
1016 }
1017
1018 memset(bitmap, 0x00, size);
1019 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
1020 bitmap->s_nr_groups = nr_groups;
1021 return bitmap;
1022}
1023
Jan Karabcec4472007-08-30 23:56:22 -07001024static int udf_load_partdesc(struct super_block *sb, struct buffer_head *bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct partitionDesc *p;
1027 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001028 struct udf_part_map *map;
1029 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 p = (struct partitionDesc *)bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001032 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001034 for (i = 0; i < sbi->s_partitions; i++) {
1035 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001036 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001037 map->s_partition_num,
1038 le16_to_cpu(p->partitionNumber));
1039 if (map->s_partition_num ==
1040 le16_to_cpu(p->partitionNumber)) {
1041 map->s_partition_len =
1042 le32_to_cpu(p->partitionLength); /* blocks */
1043 map->s_partition_root =
1044 le32_to_cpu(p->partitionStartingLocation);
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001045 if (p->accessType ==
1046 cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001047 map->s_partition_flags |=
1048 UDF_PART_FLAG_READ_ONLY;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001049 if (p->accessType ==
1050 cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001051 map->s_partition_flags |=
1052 UDF_PART_FLAG_WRITE_ONCE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001053 if (p->accessType ==
1054 cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001055 map->s_partition_flags |=
1056 UDF_PART_FLAG_REWRITABLE;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -08001057 if (p->accessType ==
1058 cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001059 map->s_partition_flags |=
1060 UDF_PART_FLAG_OVERWRITABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001062 if (!strcmp(p->partitionContents.ident,
1063 PD_PARTITION_CONTENTS_NSR02) ||
1064 !strcmp(p->partitionContents.ident,
1065 PD_PARTITION_CONTENTS_NSR03)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct partitionHeaderDesc *phd;
1067
Marcin Slusarz4b111112008-02-08 04:20:36 -08001068 phd = (struct partitionHeaderDesc *)
1069 (p->partitionContentsUse);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001070 if (phd->unallocSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001071 kernel_lb_addr loc = {
1072 .logicalBlockNum = le32_to_cpu(phd->unallocSpaceTable.extPosition),
1073 .partitionReferenceNum = i,
1074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001076 map->s_uspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001077 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001078 if (!map->s_uspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001079 udf_debug("cannot load unallocSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001080 return 1;
1081 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001082 map->s_partition_flags |=
1083 UDF_PART_FLAG_UNALLOC_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001084 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001085 i, map->s_uspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001087 if (phd->unallocSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001088 struct udf_bitmap *bitmap =
1089 udf_sb_alloc_bitmap(sb, i);
1090 map->s_uspace.s_bitmap = bitmap;
1091 if (bitmap != NULL) {
1092 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001093 le32_to_cpu(phd->unallocSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001094 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001095 le32_to_cpu(phd->unallocSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001096 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001097 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001098 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 }
1101 if (phd->partitionIntegrityTable.extLength)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001102 udf_debug("partitionIntegrityTable (part %d)\n", i);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001103 if (phd->freedSpaceTable.extLength) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001104 kernel_lb_addr loc = {
1105 .logicalBlockNum = le32_to_cpu(phd->freedSpaceTable.extPosition),
1106 .partitionReferenceNum = i,
1107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001109 map->s_fspace.s_table =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001110 udf_iget(sb, loc);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001111 if (!map->s_fspace.s_table) {
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001112 udf_debug("cannot load freedSpaceTable (part %d)\n", i);
Jan Karabcec4472007-08-30 23:56:22 -07001113 return 1;
1114 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001115 map->s_partition_flags |=
1116 UDF_PART_FLAG_FREED_TABLE;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001117 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001118 i, map->s_fspace.s_table->i_ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001120 if (phd->freedSpaceBitmap.extLength) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001121 struct udf_bitmap *bitmap =
1122 udf_sb_alloc_bitmap(sb, i);
1123 map->s_fspace.s_bitmap = bitmap;
1124 if (bitmap != NULL) {
1125 bitmap->s_extLength =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001126 le32_to_cpu(phd->freedSpaceBitmap.extLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001127 bitmap->s_extPosition =
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001128 le32_to_cpu(phd->freedSpaceBitmap.extPosition);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001129 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001130 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
Marcin Slusarz4b111112008-02-08 04:20:36 -08001131 i, bitmap->s_extPosition);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133 }
1134 }
1135 break;
1136 }
1137 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001138 if (i == sbi->s_partitions)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001139 udf_debug("Partition (%d) not found in partition map\n",
1140 le16_to_cpu(p->partitionNumber));
Marcin Slusarz4b111112008-02-08 04:20:36 -08001141 else
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001142 udf_debug("Partition (%d:%d type %x) starts at physical %d, "
1143 "block length %d\n",
1144 le16_to_cpu(p->partitionNumber), i,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001145 map->s_partition_type,
1146 map->s_partition_root,
1147 map->s_partition_len);
Jan Karabcec4472007-08-30 23:56:22 -07001148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001151static int udf_load_logicalvol(struct super_block *sb, struct buffer_head *bh,
1152 kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 struct logicalVolDesc *lvd;
1155 int i, j, offset;
1156 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001157 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001158 struct genericPartitionMap *gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 lvd = (struct logicalVolDesc *)bh->b_data;
1161
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001162 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1163 if (i != 0)
1164 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001166 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001167 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001168 i++, offset += gpm->partitionMapLength) {
1169 struct udf_part_map *map = &sbi->s_partmaps[i];
1170 gpm = (struct genericPartitionMap *)
1171 &(lvd->partitionMaps[offset]);
1172 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001173 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001174 struct genericPartitionMap1 *gpm1 =
1175 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001176 map->s_partition_type = UDF_TYPE1_MAP15;
1177 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1178 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1179 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001180 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001181 struct udfPartitionMap2 *upm2 =
1182 (struct udfPartitionMap2 *)gpm;
1183 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1184 strlen(UDF_ID_VIRTUAL))) {
1185 u16 suf =
1186 le16_to_cpu(((__le16 *)upm2->partIdent.
1187 identSuffix)[0]);
1188 if (suf == 0x0150) {
1189 map->s_partition_type =
1190 UDF_VIRTUAL_MAP15;
1191 map->s_partition_func =
1192 udf_get_pblock_virt15;
1193 } else if (suf == 0x0200) {
1194 map->s_partition_type =
1195 UDF_VIRTUAL_MAP20;
1196 map->s_partition_func =
1197 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001199 } else if (!strncmp(upm2->partIdent.ident,
1200 UDF_ID_SPARABLE,
1201 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 uint32_t loc;
1203 uint16_t ident;
1204 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001205 struct sparablePartitionMap *spm =
1206 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001208 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001209 map->s_type_specific.s_sparing.s_packet_len =
1210 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001211 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001212 struct buffer_head *bh2;
1213
1214 loc = le32_to_cpu(
1215 spm->locSparingTable[j]);
1216 bh2 = udf_read_tagged(sb, loc, loc,
1217 &ident);
1218 map->s_type_specific.s_sparing.
1219 s_spar_map[j] = bh2;
1220
1221 if (bh2 != NULL) {
1222 st = (struct sparingTable *)
1223 bh2->b_data;
1224 if (ident != 0 || strncmp(
1225 st->sparingIdent.ident,
1226 UDF_ID_SPARING,
1227 strlen(UDF_ID_SPARING))) {
1228 brelse(bh2);
1229 map->s_type_specific.
1230 s_sparing.
1231 s_spar_map[j] =
1232 NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 }
1234 }
1235 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001236 map->s_partition_func = udf_get_pblock_spar15;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001237 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001238 udf_debug("Unknown ident: %s\n",
1239 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 continue;
1241 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001242 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1243 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001246 i, map->s_partition_num, type,
1247 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001250 if (fileset) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001251 long_ad *la = (long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001254 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1255 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001256 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 if (lvd->integritySeqExt.extLength)
1259 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return 0;
1262}
1263
1264/*
1265 * udf_load_logicalvolint
1266 *
1267 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001268static void udf_load_logicalvolint(struct super_block *sb, kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 struct buffer_head *bh = NULL;
1271 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001272 struct udf_sb_info *sbi = UDF_SB(sb);
1273 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001276 (bh = udf_read_tagged(sb, loc.extLocation,
1277 loc.extLocation, &ident)) &&
1278 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001279 sbi->s_lvid_bh = bh;
1280 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001281
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001282 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001283 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001284 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001285
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001286 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001287 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001289 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001291 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001292 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*
1296 * udf_process_sequence
1297 *
1298 * PURPOSE
1299 * Process a main/reserve volume descriptor sequence.
1300 *
1301 * PRE-CONDITIONS
1302 * sb Pointer to _locked_ superblock.
1303 * block First block of first extent of the sequence.
1304 * lastblock Lastblock of first extent of the sequence.
1305 *
1306 * HISTORY
1307 * July 1, 1997 - Andrew E. Mileski
1308 * Written, tested, and released.
1309 */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001310static int udf_process_sequence(struct super_block *sb, long block,
1311 long lastblock, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct buffer_head *bh = NULL;
1314 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001315 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct generic_desc *gd;
1317 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001318 int done = 0;
1319 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 uint32_t vdsn;
1321 uint16_t ident;
1322 long next_s = 0, next_e = 0;
1323
1324 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1325
1326 /* Read the main descriptor sequence */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001327 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 bh = udf_read_tagged(sb, block, block, &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001330 if (!bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332
1333 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1334 gd = (struct generic_desc *)bh->b_data;
1335 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001336 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001337 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001338 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1339 if (vdsn >= curr->volDescSeqNum) {
1340 curr->volDescSeqNum = vdsn;
1341 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001342 }
1343 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001344 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001345 curr = &vds[VDS_POS_VOL_DESC_PTR];
1346 if (vdsn >= curr->volDescSeqNum) {
1347 curr->volDescSeqNum = vdsn;
1348 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001350 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001351 next_s = le32_to_cpu(
1352 vdp->nextVolDescSeqExt.extLocation);
1353 next_e = le32_to_cpu(
1354 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001355 next_e = next_e >> sb->s_blocksize_bits;
1356 next_e += next_s;
1357 }
1358 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001359 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001360 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1361 if (vdsn >= curr->volDescSeqNum) {
1362 curr->volDescSeqNum = vdsn;
1363 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001364 }
1365 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001366 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001367 curr = &vds[VDS_POS_PARTITION_DESC];
1368 if (!curr->block)
1369 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001370 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001371 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001372 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1373 if (vdsn >= curr->volDescSeqNum) {
1374 curr->volDescSeqNum = vdsn;
1375 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001376 }
1377 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001378 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001379 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1380 if (vdsn >= curr->volDescSeqNum) {
1381 curr->volDescSeqNum = vdsn;
1382 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001383 }
1384 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001385 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001386 vds[VDS_POS_TERMINATING_DESC].block = block;
1387 if (next_e) {
1388 block = next_s;
1389 lastblock = next_e;
1390 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001391 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001392 done = 1;
1393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001395 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001397 for (i = 0; i < VDS_POS_LENGTH; i++) {
1398 if (vds[i].block) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001399 bh = udf_read_tagged(sb, vds[i].block, vds[i].block,
1400 &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001402 if (i == VDS_POS_PRIMARY_VOL_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 udf_load_pvoldesc(sb, bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404 } else if (i == VDS_POS_LOGICAL_VOL_DESC) {
Marcin Slusarzdeae6cf2008-02-08 04:20:33 -08001405 if (udf_load_logicalvol(sb, bh, fileset)) {
1406 brelse(bh);
1407 return 1;
1408 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001409 } else if (i == VDS_POS_PARTITION_DESC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct buffer_head *bh2 = NULL;
Jan Karabcec4472007-08-30 23:56:22 -07001411 if (udf_load_partdesc(sb, bh)) {
1412 brelse(bh);
1413 return 1;
1414 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001415 for (j = vds[i].block + 1;
1416 j < vds[VDS_POS_TERMINATING_DESC].block;
1417 j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 bh2 = udf_read_tagged(sb, j, j, &ident);
1419 gd = (struct generic_desc *)bh2->b_data;
1420 if (ident == TAG_IDENT_PD)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001421 if (udf_load_partdesc(sb,
1422 bh2)) {
Jan Karabcec4472007-08-30 23:56:22 -07001423 brelse(bh);
1424 brelse(bh2);
1425 return 1;
1426 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001427 brelse(bh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001430 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432 }
1433
1434 return 0;
1435}
1436
1437/*
1438 * udf_check_valid()
1439 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001440static int udf_check_valid(struct super_block *sb, int novrs, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 long block;
1443
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001444 if (novrs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 udf_debug("Validity check skipped because of novrs option\n");
1446 return 0;
1447 }
1448 /* Check that it is NSR02 compliant */
1449 /* Process any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001450 else {
1451 block = udf_vrs(sb, silent);
1452 if (block == -1) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001453 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001454 udf_debug("Failed to read byte 32768. Assuming open "
1455 "disc. Skipping validity check\n");
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001456 if (!sbi->s_last_block)
1457 sbi->s_last_block = udf_get_last_block(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001458 return 0;
1459 } else
1460 return !block;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001464static int udf_load_partition(struct super_block *sb, kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct anchorVolDescPtr *anchor;
1467 uint16_t ident;
1468 struct buffer_head *bh;
1469 long main_s, main_e, reserve_s, reserve_e;
1470 int i, j;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001471 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (!sb)
1474 return 1;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001475 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001477 for (i = 0; i < ARRAY_SIZE(sbi->s_anchor); i++) {
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001478 if (!sbi->s_anchor[i])
1479 continue;
1480 bh = udf_read_tagged(sb, sbi->s_anchor[i], sbi->s_anchor[i],
1481 &ident);
1482 if (!bh)
1483 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001485 anchor = (struct anchorVolDescPtr *)bh->b_data;
Tobias Klausere8c96f82006-03-24 03:15:34 -08001486
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001487 /* Locate the main sequence */
1488 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1489 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1490 main_e = main_e >> sb->s_blocksize_bits;
1491 main_e += main_s;
1492
1493 /* Locate the reserve sequence */
1494 reserve_s = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001495 anchor->reserveVolDescSeqExt.extLocation);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001496 reserve_e = le32_to_cpu(
Marcin Slusarz4b111112008-02-08 04:20:36 -08001497 anchor->reserveVolDescSeqExt.extLength);
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001498 reserve_e = reserve_e >> sb->s_blocksize_bits;
1499 reserve_e += reserve_s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001501 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001503 /* Process the main & reserve sequences */
1504 /* responsible for finding the PartitionDesc(s) */
1505 if (!(udf_process_sequence(sb, main_s, main_e,
1506 fileset) &&
1507 udf_process_sequence(sb, reserve_s, reserve_e,
1508 fileset)))
1509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001512 if (i == ARRAY_SIZE(sbi->s_anchor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 udf_debug("No Anchor block found\n");
1514 return 1;
Marcin Slusarz28f7c4d2008-02-08 04:20:46 -08001515 }
1516 udf_debug("Using anchor in block %d\n", sbi->s_anchor[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 for (i = 0; i < sbi->s_partitions; i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001519 kernel_lb_addr uninitialized_var(ino);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001520 struct udf_part_map *map = &sbi->s_partmaps[i];
1521 switch (map->s_partition_type) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001522 case UDF_VIRTUAL_MAP15:
1523 case UDF_VIRTUAL_MAP20:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001524 if (!sbi->s_last_block) {
1525 sbi->s_last_block = udf_get_last_block(sb);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001526 udf_find_anchor(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001528
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001529 if (!sbi->s_last_block) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 udf_debug("Unable to determine Lastblock (For "
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001531 "Virtual Partition)\n");
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001532 return 1;
1533 }
1534
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001535 for (j = 0; j < sbi->s_partitions; j++) {
1536 struct udf_part_map *map2 = &sbi->s_partmaps[j];
Cyrill Gorcunovb1e7a4b2007-10-16 23:30:08 -07001537 if (j != i &&
Marcin Slusarz4b111112008-02-08 04:20:36 -08001538 map->s_volumeseqnum ==
1539 map2->s_volumeseqnum &&
1540 map->s_partition_num ==
1541 map2->s_partition_num) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001542 ino.partitionReferenceNum = j;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001543 ino.logicalBlockNum =
1544 sbi->s_last_block -
1545 map2->s_partition_root;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001546 break;
1547 }
1548 }
1549
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001550 if (j == sbi->s_partitions)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001551 return 1;
1552
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 sbi->s_vat_inode = udf_iget(sb, ino);
1554 if (!sbi->s_vat_inode)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001555 return 1;
1556
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001557 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
1558 map->s_type_specific.s_virtual.s_start_offset =
1559 udf_ext0_offset(sbi->s_vat_inode);
1560 map->s_type_specific.s_virtual.s_num_entries =
1561 (sbi->s_vat_inode->i_size - 36) >> 2;
1562 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001563 uint32_t pos;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001564 struct virtualAllocationTable20 *vat20;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001565
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001566 pos = udf_block_map(sbi->s_vat_inode, 0);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001567 bh = sb_bread(sb, pos);
1568 if (!bh)
1569 return 1;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001570 vat20 = (struct virtualAllocationTable20 *)
1571 bh->b_data +
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001572 udf_ext0_offset(sbi->s_vat_inode);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001573 map->s_type_specific.s_virtual.s_start_offset =
1574 le16_to_cpu(vat20->lengthHeader) +
1575 udf_ext0_offset(sbi->s_vat_inode);
1576 map->s_type_specific.s_virtual.s_num_entries =
1577 (sbi->s_vat_inode->i_size -
1578 map->s_type_specific.s_virtual.
1579 s_start_offset) >> 2;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001580 brelse(bh);
1581 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001582 map->s_partition_root = udf_get_pblock(sb, 0, i, 0);
1583 map->s_partition_len =
1584 sbi->s_partmaps[ino.partitionReferenceNum].
1585 s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 }
1588 return 0;
1589}
1590
1591static void udf_open_lvid(struct super_block *sb)
1592{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001593 struct udf_sb_info *sbi = UDF_SB(sb);
1594 struct buffer_head *bh = sbi->s_lvid_bh;
1595 if (bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 kernel_timestamp cpu_time;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001597 struct logicalVolIntegrityDesc *lvid =
1598 (struct logicalVolIntegrityDesc *)bh->b_data;
1599 struct logicalVolIntegrityDescImpUse *lvidiu =
1600 udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001602 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1603 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001605 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1606 lvid->integrityType = LVID_INTEGRITY_TYPE_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Marcin Slusarz4b111112008-02-08 04:20:36 -08001608 lvid->descTag.descCRC = cpu_to_le16(
1609 udf_crc((char *)lvid + sizeof(tag),
1610 le16_to_cpu(lvid->descTag.descCRCLength),
1611 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001613 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001614 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616}
1617
1618static void udf_close_lvid(struct super_block *sb)
1619{
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001620 kernel_timestamp cpu_time;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001621 struct udf_sb_info *sbi = UDF_SB(sb);
1622 struct buffer_head *bh = sbi->s_lvid_bh;
1623 struct logicalVolIntegrityDesc *lvid;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001624
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001625 if (!bh)
1626 return;
1627
1628 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1629
1630 if (lvid->integrityType == LVID_INTEGRITY_TYPE_OPEN) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001631 struct logicalVolIntegrityDescImpUse *lvidiu =
1632 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001633 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1634 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (udf_time_to_stamp(&cpu_time, CURRENT_TIME))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001636 lvid->recordingDateAndTime = cpu_to_lets(cpu_time);
1637 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
Marcin Slusarz4b111112008-02-08 04:20:36 -08001638 lvidiu->maxUDFWriteRev =
1639 cpu_to_le16(UDF_MAX_WRITE_VERSION);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001640 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1641 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1642 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1643 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1644 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Marcin Slusarz4b111112008-02-08 04:20:36 -08001646 lvid->descTag.descCRC = cpu_to_le16(
1647 udf_crc((char *)lvid + sizeof(tag),
1648 le16_to_cpu(lvid->descTag.descCRCLength),
1649 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Marcin Slusarz3f2587b2008-02-08 04:20:39 -08001651 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001652 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654}
1655
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001656static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1657{
1658 int i;
1659 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001660 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1661 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001662
1663 for (i = 0; i < nr_groups; i++)
1664 if (bitmap->s_block_bitmap[i])
1665 brelse(bitmap->s_block_bitmap[i]);
1666
1667 if (size <= PAGE_SIZE)
1668 kfree(bitmap);
1669 else
1670 vfree(bitmap);
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/*
1674 * udf_read_super
1675 *
1676 * PURPOSE
1677 * Complete the specified super block.
1678 *
1679 * PRE-CONDITIONS
1680 * sb Pointer to superblock to complete - never NULL.
1681 * sb->s_dev Device to read suberblock from.
1682 * options Pointer to mount options.
1683 * silent Silent flag.
1684 *
1685 * HISTORY
1686 * July 1, 1997 - Andrew E. Mileski
1687 * Written, tested, and released.
1688 */
1689static int udf_fill_super(struct super_block *sb, void *options, int silent)
1690{
1691 int i;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001692 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct udf_options uopt;
1694 kernel_lb_addr rootdir, fileset;
1695 struct udf_sb_info *sbi;
1696
1697 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1698 uopt.uid = -1;
1699 uopt.gid = -1;
1700 uopt.umask = 0;
1701
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001702 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (!sbi)
1704 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001708 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Miklos Szeredi6da80892008-02-08 04:21:50 -08001710 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto error_out;
1712
1713 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001714 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001716 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 goto error_out;
1718 }
1719#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001720 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 uopt.nls_map = load_nls_default();
1722 if (!uopt.nls_map)
1723 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1724 else
1725 udf_debug("Using default NLS map\n");
1726 }
1727#endif
1728 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1729 uopt.flags |= (1 << UDF_FLAG_UTF8);
1730
1731 fileset.logicalBlockNum = 0xFFFFFFFF;
1732 fileset.partitionReferenceNum = 0xFFFF;
1733
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001734 sbi->s_flags = uopt.flags;
1735 sbi->s_uid = uopt.uid;
1736 sbi->s_gid = uopt.gid;
1737 sbi->s_umask = uopt.umask;
1738 sbi->s_nls_map = uopt.nls_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* Set the block size for all transfers */
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001741 if (!sb_min_blocksize(sb, uopt.blocksize)) {
1742 udf_debug("Bad block size (%d)\n", uopt.blocksize);
1743 printk(KERN_ERR "udf: bad block size (%d)\n", uopt.blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto error_out;
Christoph Hellwigf1f73ba82008-02-22 12:38:02 +01001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001747 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001748 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001750 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001752 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001754 sbi->s_last_block = uopt.lastblock;
1755 sbi->s_anchor[0] = sbi->s_anchor[1] = 0;
1756 sbi->s_anchor[2] = uopt.anchor;
1757 sbi->s_anchor[3] = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001759 if (udf_check_valid(sb, uopt.novrs, silent)) {
1760 /* read volume recognition sequences */
1761 printk(KERN_WARNING "UDF-fs: No VRS found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001762 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
1765 udf_find_anchor(sb);
1766
1767 /* Fill in the rest of the superblock */
1768 sb->s_op = &udf_sb_ops;
1769 sb->dq_op = NULL;
1770 sb->s_dirt = 0;
1771 sb->s_magic = UDF_SUPER_MAGIC;
1772 sb->s_time_gran = 1000;
1773
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001774 if (udf_load_partition(sb, &fileset)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001775 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto error_out;
1777 }
1778
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001779 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001781 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001782 struct logicalVolIntegrityDescImpUse *lvidiu =
1783 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001784 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
1785 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001786 /* uint16_t maxUDFWriteRev =
1787 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001789 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001790 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
1791 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001792 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001793 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001795 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001798 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
1801 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
1802 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
1803 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
1804 }
1805
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001806 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001807 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto error_out;
1809 }
1810
Marcin Slusarz4b111112008-02-08 04:20:36 -08001811 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
1812 UDF_PART_FLAG_READ_ONLY) {
1813 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
1814 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001815 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02001816 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07001817
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001818 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001819 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto error_out;
1821 }
1822
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001823 if (!silent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 kernel_timestamp ts;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001825 udf_time_to_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08001826 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001827 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001828 sbi->s_volume_ident, ts.year, ts.month, ts.day,
1829 ts.hour, ts.minute, ts.typeAndTimezone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
1831 if (!(sb->s_flags & MS_RDONLY))
1832 udf_open_lvid(sb);
1833
1834 /* Assign the root inode */
1835 /* assign inodes by physical block number */
1836 /* perhaps it's not extensible enough, but for now ... */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001837 inode = udf_iget(sb, rootdir);
1838 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001839 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
1840 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001841 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto error_out;
1843 }
1844
1845 /* Allocate a dentry for the root inode */
1846 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001847 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001848 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 iput(inode);
1850 goto error_out;
1851 }
Jan Kara31170b62007-05-08 00:35:21 -07001852 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001855error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001856 if (sbi->s_vat_inode)
1857 iput(sbi->s_vat_inode);
1858 if (sbi->s_partitions) {
1859 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1860 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1861 iput(map->s_uspace.s_table);
1862 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1863 iput(map->s_fspace.s_table);
1864 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001865 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001866 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001867 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001868 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001869 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001870 brelse(map->s_type_specific.s_sparing.
1871 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873#ifdef CONFIG_UDF_NLS
1874 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001875 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876#endif
1877 if (!(sb->s_flags & MS_RDONLY))
1878 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001879 brelse(sbi->s_lvid_bh);
1880
1881 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 kfree(sbi);
1883 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return -EINVAL;
1886}
1887
1888void udf_error(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001889 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 va_list args;
1892
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001893 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* mark sb error */
1895 sb->s_dirt = 1;
1896 }
1897 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001898 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001900 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001901 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001905 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 va_list args;
1908
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001909 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08001910 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 va_end(args);
1912 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
1916/*
1917 * udf_put_super
1918 *
1919 * PURPOSE
1920 * Prepare for destruction of the superblock.
1921 *
1922 * DESCRIPTION
1923 * Called before the filesystem is unmounted.
1924 *
1925 * HISTORY
1926 * July 1, 1997 - Andrew E. Mileski
1927 * Written, tested, and released.
1928 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001929static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001932 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001934 sbi = UDF_SB(sb);
1935 if (sbi->s_vat_inode)
1936 iput(sbi->s_vat_inode);
1937 if (sbi->s_partitions) {
1938 struct udf_part_map *map = &sbi->s_partmaps[sbi->s_partition];
1939 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1940 iput(map->s_uspace.s_table);
1941 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1942 iput(map->s_fspace.s_table);
1943 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001944 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001945 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001946 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001947 if (map->s_partition_type == UDF_SPARABLE_MAP15)
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001948 for (i = 0; i < 4; i++)
Marcin Slusarz4b111112008-02-08 04:20:36 -08001949 brelse(map->s_type_specific.s_sparing.
1950 s_spar_map[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952#ifdef CONFIG_UDF_NLS
1953 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001954 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955#endif
1956 if (!(sb->s_flags & MS_RDONLY))
1957 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001958 brelse(sbi->s_lvid_bh);
1959 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 kfree(sb->s_fs_info);
1961 sb->s_fs_info = NULL;
1962}
1963
1964/*
1965 * udf_stat_fs
1966 *
1967 * PURPOSE
1968 * Return info about the filesystem.
1969 *
1970 * DESCRIPTION
1971 * Called by sys_statfs()
1972 *
1973 * HISTORY
1974 * July 1, 1997 - Andrew E. Mileski
1975 * Written, tested, and released.
1976 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001977static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
David Howells726c3342006-06-23 02:02:58 -07001979 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001980 struct udf_sb_info *sbi = UDF_SB(sb);
1981 struct logicalVolIntegrityDescImpUse *lvidiu;
1982
1983 if (sbi->s_lvid_bh != NULL)
1984 lvidiu = udf_sb_lvidiu(sbi);
1985 else
1986 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 buf->f_type = UDF_SUPER_MAGIC;
1989 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001990 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 buf->f_bfree = udf_count_free(sb);
1992 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001993 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
1994 le32_to_cpu(lvidiu->numDirs)) : 0)
1995 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 buf->f_ffree = buf->f_bfree;
1997 /* __kernel_fsid_t f_fsid */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001998 buf->f_namelen = UDF_NAME_LEN - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 return 0;
2001}
2002
2003static unsigned char udf_bitmap_lookup[16] = {
2004 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
2005};
2006
Marcin Slusarz4b111112008-02-08 04:20:36 -08002007static unsigned int udf_count_free_bitmap(struct super_block *sb,
2008 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct buffer_head *bh = NULL;
2011 unsigned int accum = 0;
2012 int index;
2013 int block = 0, newblock;
2014 kernel_lb_addr loc;
2015 uint32_t bytes;
2016 uint8_t value;
2017 uint8_t *ptr;
2018 uint16_t ident;
2019 struct spaceBitmapDesc *bm;
2020
2021 lock_kernel();
2022
2023 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002024 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 bh = udf_read_ptagged(sb, loc, 0, &ident);
2026
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002027 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 printk(KERN_ERR "udf: udf_count_free failed\n");
2029 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002030 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002031 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 printk(KERN_ERR "udf: udf_count_free failed\n");
2033 goto out;
2034 }
2035
2036 bm = (struct spaceBitmapDesc *)bh->b_data;
2037 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002038 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2039 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002041 while (bytes > 0) {
2042 while ((bytes > 0) && (index < sb->s_blocksize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 value = ptr[index];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002044 accum += udf_bitmap_lookup[value & 0x0f];
2045 accum += udf_bitmap_lookup[value >> 4];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 index++;
2047 bytes--;
2048 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002049 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002050 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 newblock = udf_get_lb_pblock(sb, loc, ++block);
2052 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002053 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 udf_debug("read failed\n");
2055 goto out;
2056 }
2057 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002058 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002061 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002063out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 unlock_kernel();
2065
2066 return accum;
2067}
2068
Marcin Slusarz4b111112008-02-08 04:20:36 -08002069static unsigned int udf_count_free_table(struct super_block *sb,
2070 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002073 uint32_t elen;
2074 kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002076 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 lock_kernel();
2079
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002080 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002081 epos.offset = sizeof(struct unallocSpaceEntry);
2082 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002084 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002086
Jan Kara3bf25cb2007-05-08 00:35:16 -07002087 brelse(epos.bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 unlock_kernel();
2090
2091 return accum;
2092}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002093
2094static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002097 struct udf_sb_info *sbi;
2098 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002100 sbi = UDF_SB(sb);
2101 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002102 struct logicalVolIntegrityDesc *lvid =
2103 (struct logicalVolIntegrityDesc *)
2104 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002105 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002106 accum = le32_to_cpu(
2107 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (accum == 0xFFFFFFFF)
2109 accum = 0;
2110 }
2111 }
2112
2113 if (accum)
2114 return accum;
2115
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002116 map = &sbi->s_partmaps[sbi->s_partition];
2117 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002118 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002119 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002121 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002122 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002123 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125 if (accum)
2126 return accum;
2127
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002128 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002129 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002130 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002132 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002133 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002134 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
2137 return accum;
2138}