blob: 80f47ce515bcb540e3d33c825b85cea4c67a4291 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/buffer_head.h>
52#include <linux/vfs.h>
53#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080054#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080055#include <linux/mount.h>
56#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010057#include <linux/bitmap.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020058#include <linux/crc-itu-t.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
65#include <asm/uaccess.h>
66
67#define VDS_POS_PRIMARY_VOL_DESC 0
68#define VDS_POS_UNALLOC_SPACE_DESC 1
69#define VDS_POS_LOGICAL_VOL_DESC 2
70#define VDS_POS_PARTITION_DESC 3
71#define VDS_POS_IMP_USE_VOL_DESC 4
72#define VDS_POS_VOL_DESC_PTR 5
73#define VDS_POS_TERMINATING_DESC 6
74#define VDS_POS_LENGTH 7
75
Miklos Szeredi6da80892008-02-08 04:21:50 -080076#define UDF_DEFAULT_BLOCKSIZE 2048
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static char error_buf[1024];
79
80/* These are the "meat" - everything else is stuffing */
81static int udf_fill_super(struct super_block *, void *, int);
82static void udf_put_super(struct super_block *);
Jan Kara146bca72009-03-16 18:27:37 +010083static int udf_sync_fs(struct super_block *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int udf_remount_fs(struct super_block *, int *, char *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020085static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020086static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
87 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070088static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020089 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void udf_open_lvid(struct super_block *);
91static void udf_close_lvid(struct super_block *);
92static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070093static int udf_statfs(struct dentry *, struct kstatfs *);
Miklos Szeredi6da80892008-02-08 04:21:50 -080094static int udf_show_options(struct seq_file *, struct vfsmount *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Marcin Slusarz6c79e982008-02-08 04:20:30 -080096struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct udf_sb_info *sbi)
97{
Marcin Slusarz4b111112008-02-08 04:20:36 -080098 struct logicalVolIntegrityDesc *lvid =
99 (struct logicalVolIntegrityDesc *)sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800100 __u32 number_of_partitions = le32_to_cpu(lvid->numOfPartitions);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800101 __u32 offset = number_of_partitions * 2 *
102 sizeof(uint32_t)/sizeof(uint8_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800103 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* UDF filesystem type */
Al Viro152a0832010-07-25 00:46:55 +0400107static struct dentry *udf_mount(struct file_system_type *fs_type,
108 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Al Viro152a0832010-07-25 00:46:55 +0400110 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700114 .owner = THIS_MODULE,
115 .name = "udf",
Al Viro152a0832010-07-25 00:46:55 +0400116 .mount = udf_mount,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700117 .kill_sb = kill_block_super,
118 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119};
120
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700121static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123static struct inode *udf_alloc_inode(struct super_block *sb)
124{
125 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800126 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!ei)
128 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700129
130 ei->i_unique = 0;
131 ei->i_lenExtents = 0;
132 ei->i_next_alloc_block = 0;
133 ei->i_next_alloc_goal = 0;
134 ei->i_strat4096 = 0;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100135 init_rwsem(&ei->i_data_sem);
Dan Bastone95f87972006-08-13 23:24:18 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return &ei->vfs_inode;
138}
139
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100140static void udf_i_callback(struct rcu_head *head)
141{
142 struct inode *inode = container_of(head, struct inode, i_rcu);
143 INIT_LIST_HEAD(&inode->i_dentry);
144 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static void udf_destroy_inode(struct inode *inode)
148{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100149 call_rcu(&inode->i_rcu, udf_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700152static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700154 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Christoph Lametera35afb82007-05-16 22:10:57 -0700156 ei->i_ext.i_data = NULL;
157 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160static int init_inodecache(void)
161{
162 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
163 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 0, (SLAB_RECLAIM_ACCOUNT |
165 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900166 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700167 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -ENOMEM;
169 return 0;
170}
171
172static void destroy_inodecache(void)
173{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700174 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800178static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700179 .alloc_inode = udf_alloc_inode,
180 .destroy_inode = udf_destroy_inode,
181 .write_inode = udf_write_inode,
Al Viro3aac2b62010-06-07 00:43:39 -0400182 .evict_inode = udf_evict_inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700183 .put_super = udf_put_super,
Jan Kara146bca72009-03-16 18:27:37 +0100184 .sync_fs = udf_sync_fs,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700185 .statfs = udf_statfs,
186 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800187 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned char novrs;
192 unsigned int blocksize;
193 unsigned int session;
194 unsigned int lastblock;
195 unsigned int anchor;
196 unsigned int volume;
197 unsigned short partition;
198 unsigned int fileset;
199 unsigned int rootdir;
200 unsigned int flags;
201 mode_t umask;
202 gid_t gid;
203 uid_t uid;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100204 mode_t fmode;
205 mode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 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) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700245 udf_error(sb, __func__,
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800246 "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");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100263 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800264 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);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100287 if (sbi->s_fmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100288 seq_printf(seq, ",mode=%o", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100289 if (sbi->s_dmode != UDF_INVALID_MODE)
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100290 seq_printf(seq, ",dmode=%o", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800291 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
292 seq_printf(seq, ",session=%u", sbi->s_session);
293 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
294 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara40346002009-03-19 16:21:38 +0100295 if (sbi->s_anchor != 0)
296 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800297 /*
298 * volume, partition, fileset and rootdir seem to be ignored
299 * currently
300 */
301 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
302 seq_puts(seq, ",utf8");
303 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
304 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
305
306 return 0;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * udf_parse_options
311 *
312 * PURPOSE
313 * Parse mount options.
314 *
315 * DESCRIPTION
316 * The following mount options are supported:
317 *
318 * gid= Set the default group.
319 * umask= Set the default umask.
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100320 * mode= Set the default file permissions.
321 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * 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,
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100371 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
372 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373};
374
Steven Whitehousea447c092008-10-13 10:46:57 +0100375static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700376 {Opt_novrs, "novrs"},
377 {Opt_nostrict, "nostrict"},
378 {Opt_bs, "bs=%u"},
379 {Opt_unhide, "unhide"},
380 {Opt_undelete, "undelete"},
381 {Opt_noadinicb, "noadinicb"},
382 {Opt_adinicb, "adinicb"},
383 {Opt_shortad, "shortad"},
384 {Opt_longad, "longad"},
385 {Opt_uforget, "uid=forget"},
386 {Opt_uignore, "uid=ignore"},
387 {Opt_gforget, "gid=forget"},
388 {Opt_gignore, "gid=ignore"},
389 {Opt_gid, "gid=%u"},
390 {Opt_uid, "uid=%u"},
391 {Opt_umask, "umask=%o"},
392 {Opt_session, "session=%u"},
393 {Opt_lastblock, "lastblock=%u"},
394 {Opt_anchor, "anchor=%u"},
395 {Opt_volume, "volume=%u"},
396 {Opt_partition, "partition=%u"},
397 {Opt_fileset, "fileset=%u"},
398 {Opt_rootdir, "rootdir=%u"},
399 {Opt_utf8, "utf8"},
400 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100401 {Opt_fmode, "mode=%o"},
402 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700403 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Miklos Szeredi6da80892008-02-08 04:21:50 -0800406static int udf_parse_options(char *options, struct udf_options *uopt,
407 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 char *p;
410 int option;
411
412 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 uopt->partition = 0xFFFF;
414 uopt->session = 0xFFFFFFFF;
415 uopt->lastblock = 0;
416 uopt->anchor = 0;
417 uopt->volume = 0xFFFFFFFF;
418 uopt->rootdir = 0xFFFFFFFF;
419 uopt->fileset = 0xFFFFFFFF;
420 uopt->nls_map = NULL;
421
422 if (!options)
423 return 1;
424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700425 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 substring_t args[MAX_OPT_ARGS];
427 int token;
428 if (!*p)
429 continue;
430
431 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700432 switch (token) {
433 case Opt_novrs:
434 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100435 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700436 case Opt_bs:
437 if (match_int(&args[0], &option))
438 return 0;
439 uopt->blocksize = option;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100440 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700441 break;
442 case Opt_unhide:
443 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
444 break;
445 case Opt_undelete:
446 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
447 break;
448 case Opt_noadinicb:
449 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
450 break;
451 case Opt_adinicb:
452 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
453 break;
454 case Opt_shortad:
455 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
456 break;
457 case Opt_longad:
458 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
459 break;
460 case Opt_gid:
461 if (match_int(args, &option))
462 return 0;
463 uopt->gid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700464 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700465 break;
466 case Opt_uid:
467 if (match_int(args, &option))
468 return 0;
469 uopt->uid = option;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700470 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700471 break;
472 case Opt_umask:
473 if (match_octal(args, &option))
474 return 0;
475 uopt->umask = option;
476 break;
477 case Opt_nostrict:
478 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
479 break;
480 case Opt_session:
481 if (match_int(args, &option))
482 return 0;
483 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800484 if (!remount)
485 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700486 break;
487 case Opt_lastblock:
488 if (match_int(args, &option))
489 return 0;
490 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800491 if (!remount)
492 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700493 break;
494 case Opt_anchor:
495 if (match_int(args, &option))
496 return 0;
497 uopt->anchor = option;
498 break;
499 case Opt_volume:
500 if (match_int(args, &option))
501 return 0;
502 uopt->volume = option;
503 break;
504 case Opt_partition:
505 if (match_int(args, &option))
506 return 0;
507 uopt->partition = option;
508 break;
509 case Opt_fileset:
510 if (match_int(args, &option))
511 return 0;
512 uopt->fileset = option;
513 break;
514 case Opt_rootdir:
515 if (match_int(args, &option))
516 return 0;
517 uopt->rootdir = option;
518 break;
519 case Opt_utf8:
520 uopt->flags |= (1 << UDF_FLAG_UTF8);
521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700523 case Opt_iocharset:
524 uopt->nls_map = load_nls(args[0].from);
525 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700528 case Opt_uignore:
529 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
530 break;
531 case Opt_uforget:
532 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
533 break;
534 case Opt_gignore:
535 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
536 break;
537 case Opt_gforget:
538 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
539 break;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100540 case Opt_fmode:
541 if (match_octal(args, &option))
542 return 0;
543 uopt->fmode = option & 0777;
544 break;
545 case Opt_dmode:
546 if (match_octal(args, &option))
547 return 0;
548 uopt->dmode = option & 0777;
549 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700550 default:
551 printk(KERN_ERR "udf: bad mount option \"%s\" "
552 "or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
554 }
555 }
556 return 1;
557}
558
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700559static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800562 struct udf_sb_info *sbi = UDF_SB(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400563 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800565 uopt.flags = sbi->s_flags;
566 uopt.uid = sbi->s_uid;
567 uopt.gid = sbi->s_gid;
568 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100569 uopt.fmode = sbi->s_fmode;
570 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Miklos Szeredi6da80892008-02-08 04:21:50 -0800572 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -EINVAL;
574
Jan Karac03cad22010-10-20 22:17:28 +0200575 write_lock(&sbi->s_cred_lock);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800576 sbi->s_flags = uopt.flags;
577 sbi->s_uid = uopt.uid;
578 sbi->s_gid = uopt.gid;
579 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +0100580 sbi->s_fmode = uopt.fmode;
581 sbi->s_dmode = uopt.dmode;
Jan Karac03cad22010-10-20 22:17:28 +0200582 write_unlock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800584 if (sbi->s_lvid_bh) {
585 int write_rev = le16_to_cpu(udf_sb_lvidiu(sbi)->minUDFWriteRev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (write_rev > UDF_MAX_WRITE_VERSION)
587 *flags |= MS_RDONLY;
588 }
589
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400590 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
591 goto out_unlock;
592
Jan Kara36350462010-05-19 16:28:56 +0200593 if (*flags & MS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 udf_close_lvid(sb);
Jan Kara36350462010-05-19 16:28:56 +0200595 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 udf_open_lvid(sb);
597
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400598out_unlock:
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400599 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Jan Kara40346002009-03-19 16:21:38 +0100602/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
603/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
604static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 struct volStructDesc *vsd = NULL;
Sebastian Manciuleaf4bcbbd2008-04-08 14:02:11 +0200607 loff_t sector = 32768;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int sectorsize;
609 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700610 int nsr02 = 0;
611 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800612 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800614 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (sb->s_blocksize < sizeof(struct volStructDesc))
616 sectorsize = sizeof(struct volStructDesc);
617 else
618 sectorsize = sb->s_blocksize;
619
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800620 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200623 (unsigned int)(sector >> sb->s_blocksize_bits),
624 sb->s_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* Process the sequence (if applicable) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700626 for (; !nsr02 && !nsr03; sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* Read a block */
628 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
629 if (!bh)
630 break;
631
632 /* Look for ISO descriptors */
633 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800634 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636 if (vsd->stdIdent[0] == 0) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700637 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800639 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
640 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700641 switch (vsd->structType) {
642 case 0:
643 udf_debug("ISO9660 Boot Record found\n");
644 break;
645 case 1:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800646 udf_debug("ISO9660 Primary Volume Descriptor "
647 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700648 break;
649 case 2:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800650 udf_debug("ISO9660 Supplementary Volume "
651 "Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700652 break;
653 case 3:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800654 udf_debug("ISO9660 Volume Partition Descriptor "
655 "found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700656 break;
657 case 255:
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800658 udf_debug("ISO9660 Volume Descriptor Set "
659 "Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700660 break;
661 default:
662 udf_debug("ISO9660 VRS (%u) found\n",
663 vsd->structType);
664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800666 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
667 VSD_STD_ID_LEN))
668 ; /* nothing */
669 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
670 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700671 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800673 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
674 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800676 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
677 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 nsr03 = sector;
Jan Kara3bf25cb2007-05-08 00:35:16 -0700679 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 if (nsr03)
683 return nsr03;
684 else if (nsr02)
685 return nsr02;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800686 else if (sector - (sbi->s_session << sb->s_blocksize_bits) == 32768)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -1;
688 else
689 return 0;
690}
691
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800692static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200693 struct kernel_lb_addr *fileset,
694 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct buffer_head *bh = NULL;
697 long lastblock;
698 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800699 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700702 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200703 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700705 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700707 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700708 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 1;
710 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800714 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800715 if (!bh) {
716 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200717 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700719/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700721
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800722 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700723 (newfileset.partitionReferenceNum != 0xFFFF &&
724 fileset->logicalBlockNum == 0xFFFFFFFF &&
725 fileset->partitionReferenceNum == 0xFFFF);
726 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800727 lastblock = sbi->s_partmaps
728 [newfileset.partitionReferenceNum]
729 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 newfileset.logicalBlockNum = 0;
731
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700732 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200733 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800734 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700735 if (!bh) {
736 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 continue;
738 }
739
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700740 switch (ident) {
741 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700742 {
743 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800744 sp = (struct spaceBitmapDesc *)
745 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700746 newfileset.logicalBlockNum += 1 +
747 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800748 sizeof(struct spaceBitmapDesc)
749 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700750 brelse(bh);
751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700753 case TAG_IDENT_FSD:
754 *fileset = newfileset;
755 break;
756 default:
757 newfileset.logicalBlockNum++;
758 brelse(bh);
759 bh = NULL;
760 break;
761 }
762 } while (newfileset.logicalBlockNum < lastblock &&
763 fileset->logicalBlockNum == 0xFFFFFFFF &&
764 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 }
767
768 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700769 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700771 fileset->logicalBlockNum,
772 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800774 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700776 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778 }
779 return 1;
780}
781
Jan Karac0eb31e2008-04-01 16:50:35 +0200782static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100785 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200786 struct buffer_head *bh;
787 uint16_t ident;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100788 int ret = 1;
789
790 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
791 if (!instr)
792 return 1;
793
794 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
795 if (!outstr)
796 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200797
798 bh = udf_read_tagged(sb, block, block, &ident);
799 if (!bh)
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100800 goto out2;
801
Jan Karac0eb31e2008-04-01 16:50:35 +0200802 BUG_ON(ident != TAG_IDENT_PVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 pvoldesc = (struct primaryVolDesc *)bh->b_data;
805
Marcin Slusarz56774802008-02-10 11:25:31 +0100806 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
807 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100808#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200809 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
marcin.slusarz@gmail.comcbf56762008-02-27 22:50:14 +0100810 udf_debug("recording time %04u/%02u/%02u"
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800811 " %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100812 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
813 ts->minute, le16_to_cpu(ts->typeAndTimezone));
814#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100817 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
818 if (udf_CS0toUTF8(outstr, instr)) {
819 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
820 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800821 udf_debug("volIdent[] = '%s'\n",
822 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100825 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
826 if (udf_CS0toUTF8(outstr, instr))
827 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200828
829 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100830 ret = 0;
831out2:
832 kfree(outstr);
833out1:
834 kfree(instr);
835 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Jan Karabfb257a2008-04-08 20:37:21 +0200838static int udf_load_metadata_files(struct super_block *sb, int partition)
839{
840 struct udf_sb_info *sbi = UDF_SB(sb);
841 struct udf_part_map *map;
842 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200843 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200844 int fe_error = 0;
845
846 map = &sbi->s_partmaps[partition];
847 mdata = &map->s_type_specific.s_metadata;
848
849 /* metadata address */
850 addr.logicalBlockNum = mdata->s_meta_file_loc;
851 addr.partitionReferenceNum = map->s_partition_num;
852
853 udf_debug("Metadata file location: block = %d part = %d\n",
854 addr.logicalBlockNum, addr.partitionReferenceNum);
855
Pekka Enberg97e961f2008-10-15 12:29:03 +0200856 mdata->s_metadata_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200857
858 if (mdata->s_metadata_fe == NULL) {
859 udf_warning(sb, __func__, "metadata inode efe not found, "
860 "will try mirror inode.");
861 fe_error = 1;
862 } else if (UDF_I(mdata->s_metadata_fe)->i_alloc_type !=
863 ICBTAG_FLAG_AD_SHORT) {
864 udf_warning(sb, __func__, "metadata inode efe does not have "
865 "short allocation descriptors!");
866 fe_error = 1;
867 iput(mdata->s_metadata_fe);
868 mdata->s_metadata_fe = NULL;
869 }
870
871 /* mirror file entry */
872 addr.logicalBlockNum = mdata->s_mirror_file_loc;
873 addr.partitionReferenceNum = map->s_partition_num;
874
875 udf_debug("Mirror metadata file location: block = %d part = %d\n",
876 addr.logicalBlockNum, addr.partitionReferenceNum);
877
Pekka Enberg97e961f2008-10-15 12:29:03 +0200878 mdata->s_mirror_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200879
880 if (mdata->s_mirror_fe == NULL) {
881 if (fe_error) {
882 udf_error(sb, __func__, "mirror inode efe not found "
883 "and metadata inode is missing too, exiting...");
884 goto error_exit;
885 } else
886 udf_warning(sb, __func__, "mirror inode efe not found,"
887 " but metadata inode is OK");
888 } else if (UDF_I(mdata->s_mirror_fe)->i_alloc_type !=
889 ICBTAG_FLAG_AD_SHORT) {
890 udf_warning(sb, __func__, "mirror inode efe does not have "
891 "short allocation descriptors!");
892 iput(mdata->s_mirror_fe);
893 mdata->s_mirror_fe = NULL;
894 if (fe_error)
895 goto error_exit;
896 }
897
898 /*
899 * bitmap file entry
900 * Note:
901 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
902 */
903 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
904 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
905 addr.partitionReferenceNum = map->s_partition_num;
906
907 udf_debug("Bitmap file location: block = %d part = %d\n",
908 addr.logicalBlockNum, addr.partitionReferenceNum);
909
Pekka Enberg97e961f2008-10-15 12:29:03 +0200910 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +0200911
912 if (mdata->s_bitmap_fe == NULL) {
913 if (sb->s_flags & MS_RDONLY)
914 udf_warning(sb, __func__, "bitmap inode efe "
915 "not found but it's ok since the disc"
916 " is mounted read-only");
917 else {
918 udf_error(sb, __func__, "bitmap inode efe not "
919 "found and attempted read-write mount");
920 goto error_exit;
921 }
922 }
923 }
924
925 udf_debug("udf_load_metadata_files Ok\n");
926
927 return 0;
928
929error_exit:
930 return 1;
931}
932
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700933static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200934 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct fileSetDesc *fset;
937
938 fset = (struct fileSetDesc *)bh->b_data;
939
940 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
941
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800942 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700944 udf_debug("Rootdir at block=%d, partition=%d\n",
945 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800948int udf_compute_nr_groups(struct super_block *sb, u32 partition)
949{
950 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100951 return DIV_ROUND_UP(map->s_partition_len +
952 (sizeof(struct spaceBitmapDesc) << 3),
953 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800954}
955
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800956static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
957{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800958 struct udf_bitmap *bitmap;
959 int nr_groups;
960 int size;
961
Marcin Slusarz883cb9d2008-02-08 04:20:34 -0800962 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800963 size = sizeof(struct udf_bitmap) +
964 (sizeof(struct buffer_head *) * nr_groups);
965
966 if (size <= PAGE_SIZE)
Joe Perchesed2ae6f2010-11-04 20:08:04 -0700967 bitmap = kzalloc(size, GFP_KERNEL);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800968 else
Joe Perchesed2ae6f2010-11-04 20:08:04 -0700969 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800970
971 if (bitmap == NULL) {
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700972 udf_error(sb, __func__,
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800973 "Unable to allocate space for bitmap "
974 "and %d buffer_head pointers", nr_groups);
975 return NULL;
976 }
977
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800978 bitmap->s_block_bitmap = (struct buffer_head **)(bitmap + 1);
979 bitmap->s_nr_groups = nr_groups;
980 return bitmap;
981}
982
Jan Kara3fb38df2008-04-02 12:26:36 +0200983static int udf_fill_partdesc_info(struct super_block *sb,
984 struct partitionDesc *p, int p_index)
985{
986 struct udf_part_map *map;
987 struct udf_sb_info *sbi = UDF_SB(sb);
988 struct partitionHeaderDesc *phd;
989
990 map = &sbi->s_partmaps[p_index];
991
992 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
993 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
994
995 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
996 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
997 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
998 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
999 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1000 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1001 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1002 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1003
Sebastian Manciulea706047a2008-04-14 17:13:01 +02001004 udf_debug("Partition (%d type %x) starts at physical %d, "
1005 "block length %d\n", p_index,
Jan Kara3fb38df2008-04-02 12:26:36 +02001006 map->s_partition_type, map->s_partition_root,
1007 map->s_partition_len);
1008
1009 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1010 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1011 return 0;
1012
1013 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1014 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001015 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001016 .logicalBlockNum = le32_to_cpu(
1017 phd->unallocSpaceTable.extPosition),
1018 .partitionReferenceNum = p_index,
1019 };
1020
Pekka Enberg97e961f2008-10-15 12:29:03 +02001021 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001022 if (!map->s_uspace.s_table) {
1023 udf_debug("cannot load unallocSpaceTable (part %d)\n",
1024 p_index);
1025 return 1;
1026 }
1027 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1028 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
1029 p_index, map->s_uspace.s_table->i_ino);
1030 }
1031
1032 if (phd->unallocSpaceBitmap.extLength) {
1033 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1034 if (!bitmap)
1035 return 1;
1036 map->s_uspace.s_bitmap = bitmap;
1037 bitmap->s_extLength = le32_to_cpu(
1038 phd->unallocSpaceBitmap.extLength);
1039 bitmap->s_extPosition = le32_to_cpu(
1040 phd->unallocSpaceBitmap.extPosition);
1041 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
1042 udf_debug("unallocSpaceBitmap (part %d) @ %d\n", p_index,
1043 bitmap->s_extPosition);
1044 }
1045
1046 if (phd->partitionIntegrityTable.extLength)
1047 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1048
1049 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001050 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001051 .logicalBlockNum = le32_to_cpu(
1052 phd->freedSpaceTable.extPosition),
1053 .partitionReferenceNum = p_index,
1054 };
1055
Pekka Enberg97e961f2008-10-15 12:29:03 +02001056 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001057 if (!map->s_fspace.s_table) {
1058 udf_debug("cannot load freedSpaceTable (part %d)\n",
1059 p_index);
1060 return 1;
1061 }
1062
1063 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1064 udf_debug("freedSpaceTable (part %d) @ %ld\n",
1065 p_index, map->s_fspace.s_table->i_ino);
1066 }
1067
1068 if (phd->freedSpaceBitmap.extLength) {
1069 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1070 if (!bitmap)
1071 return 1;
1072 map->s_fspace.s_bitmap = bitmap;
1073 bitmap->s_extLength = le32_to_cpu(
1074 phd->freedSpaceBitmap.extLength);
1075 bitmap->s_extPosition = le32_to_cpu(
1076 phd->freedSpaceBitmap.extPosition);
1077 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
1078 udf_debug("freedSpaceBitmap (part %d) @ %d\n", p_index,
1079 bitmap->s_extPosition);
1080 }
1081 return 0;
1082}
1083
Jan Karae971b0b2009-11-30 19:47:55 +01001084static void udf_find_vat_block(struct super_block *sb, int p_index,
1085 int type1_index, sector_t start_block)
1086{
1087 struct udf_sb_info *sbi = UDF_SB(sb);
1088 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1089 sector_t vat_block;
1090 struct kernel_lb_addr ino;
1091
1092 /*
1093 * VAT file entry is in the last recorded block. Some broken disks have
1094 * it a few blocks before so try a bit harder...
1095 */
1096 ino.partitionReferenceNum = type1_index;
1097 for (vat_block = start_block;
1098 vat_block >= map->s_partition_root &&
1099 vat_block >= start_block - 3 &&
1100 !sbi->s_vat_inode; vat_block--) {
1101 ino.logicalBlockNum = vat_block - map->s_partition_root;
1102 sbi->s_vat_inode = udf_iget(sb, &ino);
1103 }
1104}
1105
Jan Kara38b74a52008-04-02 16:01:35 +02001106static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1107{
1108 struct udf_sb_info *sbi = UDF_SB(sb);
1109 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Jan Karafa5e0812008-04-08 02:08:53 +02001110 struct buffer_head *bh = NULL;
1111 struct udf_inode_info *vati;
1112 uint32_t pos;
1113 struct virtualAllocationTable20 *vat20;
Jan Kara4bf17af2009-07-14 19:30:23 +02001114 sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
Jan Kara38b74a52008-04-02 16:01:35 +02001115
Jan Karae971b0b2009-11-30 19:47:55 +01001116 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
Jan Kara4bf17af2009-07-14 19:30:23 +02001117 if (!sbi->s_vat_inode &&
1118 sbi->s_last_block != blocks - 1) {
1119 printk(KERN_NOTICE "UDF-fs: Failed to read VAT inode from the"
1120 " last recorded block (%lu), retrying with the last "
1121 "block of the device (%lu).\n",
1122 (unsigned long)sbi->s_last_block,
1123 (unsigned long)blocks - 1);
Jan Karae971b0b2009-11-30 19:47:55 +01001124 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
Jan Kara4bf17af2009-07-14 19:30:23 +02001125 }
Jan Kara38b74a52008-04-02 16:01:35 +02001126 if (!sbi->s_vat_inode)
1127 return 1;
1128
1129 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001130 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001131 map->s_type_specific.s_virtual.s_num_entries =
1132 (sbi->s_vat_inode->i_size - 36) >> 2;
1133 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001134 vati = UDF_I(sbi->s_vat_inode);
1135 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1136 pos = udf_block_map(sbi->s_vat_inode, 0);
1137 bh = sb_bread(sb, pos);
1138 if (!bh)
1139 return 1;
1140 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1141 } else {
1142 vat20 = (struct virtualAllocationTable20 *)
1143 vati->i_ext.i_data;
1144 }
Jan Kara38b74a52008-04-02 16:01:35 +02001145
Jan Kara38b74a52008-04-02 16:01:35 +02001146 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001147 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001148 map->s_type_specific.s_virtual.s_num_entries =
1149 (sbi->s_vat_inode->i_size -
1150 map->s_type_specific.s_virtual.
1151 s_start_offset) >> 2;
1152 brelse(bh);
1153 }
1154 return 0;
1155}
1156
Jan Karac0eb31e2008-04-01 16:50:35 +02001157static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Jan Karac0eb31e2008-04-01 16:50:35 +02001159 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001160 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001161 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001162 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001163 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001164 uint16_t partitionNumber;
1165 uint16_t ident;
1166 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Jan Karac0eb31e2008-04-01 16:50:35 +02001168 bh = udf_read_tagged(sb, block, block, &ident);
1169 if (!bh)
1170 return 1;
1171 if (ident != TAG_IDENT_PD)
1172 goto out_bh;
1173
1174 p = (struct partitionDesc *)bh->b_data;
1175 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001176
Jan Karabfb257a2008-04-08 20:37:21 +02001177 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001178 for (i = 0; i < sbi->s_partitions; i++) {
1179 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001180 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001181 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001182 if (map->s_partition_num == partitionNumber &&
1183 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1184 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001186 }
1187
Jan Kara38b74a52008-04-02 16:01:35 +02001188 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001189 udf_debug("Partition (%d) not found in partition map\n",
1190 partitionNumber);
Jan Karac0eb31e2008-04-01 16:50:35 +02001191 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001192 }
1193
Jan Kara3fb38df2008-04-02 12:26:36 +02001194 ret = udf_fill_partdesc_info(sb, p, i);
Jan Kara38b74a52008-04-02 16:01:35 +02001195
1196 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001197 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1198 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001199 */
1200 type1_idx = i;
1201 for (i = 0; i < sbi->s_partitions; i++) {
1202 map = &sbi->s_partmaps[i];
1203
1204 if (map->s_partition_num == partitionNumber &&
1205 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001206 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1207 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001208 break;
1209 }
1210
1211 if (i >= sbi->s_partitions)
1212 goto out_bh;
1213
1214 ret = udf_fill_partdesc_info(sb, p, i);
1215 if (ret)
1216 goto out_bh;
1217
Jan Karabfb257a2008-04-08 20:37:21 +02001218 if (map->s_partition_type == UDF_METADATA_MAP25) {
1219 ret = udf_load_metadata_files(sb, i);
1220 if (ret) {
1221 printk(KERN_ERR "UDF-fs: error loading MetaData "
1222 "partition map %d\n", i);
1223 goto out_bh;
1224 }
1225 } else {
1226 ret = udf_load_vat(sb, i, type1_idx);
1227 if (ret)
1228 goto out_bh;
1229 /*
1230 * Mark filesystem read-only if we have a partition with
1231 * virtual map since we don't handle writing to it (we
1232 * overwrite blocks instead of relocating them).
1233 */
1234 sb->s_flags |= MS_RDONLY;
1235 printk(KERN_NOTICE "UDF-fs: Filesystem marked read-only "
1236 "because writing to pseudooverwrite partition is "
1237 "not implemented.\n");
1238 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001239out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001240 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001241 brelse(bh);
1242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Jan Karac0eb31e2008-04-01 16:50:35 +02001245static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001246 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct logicalVolDesc *lvd;
1249 int i, j, offset;
1250 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001251 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001252 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001253 uint16_t ident;
1254 struct buffer_head *bh;
1255 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Jan Karac0eb31e2008-04-01 16:50:35 +02001257 bh = udf_read_tagged(sb, block, block, &ident);
1258 if (!bh)
1259 return 1;
1260 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 lvd = (struct logicalVolDesc *)bh->b_data;
1262
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -08001263 i = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
Jan Karac0eb31e2008-04-01 16:50:35 +02001264 if (i != 0) {
1265 ret = i;
1266 goto out_bh;
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001269 for (i = 0, offset = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001270 i < sbi->s_partitions && offset < le32_to_cpu(lvd->mapTableLength);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001271 i++, offset += gpm->partitionMapLength) {
1272 struct udf_part_map *map = &sbi->s_partmaps[i];
1273 gpm = (struct genericPartitionMap *)
1274 &(lvd->partitionMaps[offset]);
1275 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001276 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001277 struct genericPartitionMap1 *gpm1 =
1278 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001279 map->s_partition_type = UDF_TYPE1_MAP15;
1280 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1281 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1282 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001283 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001284 struct udfPartitionMap2 *upm2 =
1285 (struct udfPartitionMap2 *)gpm;
1286 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1287 strlen(UDF_ID_VIRTUAL))) {
1288 u16 suf =
1289 le16_to_cpu(((__le16 *)upm2->partIdent.
1290 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001291 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001292 map->s_partition_type =
1293 UDF_VIRTUAL_MAP15;
1294 map->s_partition_func =
1295 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001296 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001297 map->s_partition_type =
1298 UDF_VIRTUAL_MAP20;
1299 map->s_partition_func =
1300 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001302 } else if (!strncmp(upm2->partIdent.ident,
1303 UDF_ID_SPARABLE,
1304 strlen(UDF_ID_SPARABLE))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 uint32_t loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 struct sparingTable *st;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001307 struct sparablePartitionMap *spm =
1308 (struct sparablePartitionMap *)gpm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001310 map->s_partition_type = UDF_SPARABLE_MAP15;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001311 map->s_type_specific.s_sparing.s_packet_len =
1312 le16_to_cpu(spm->packetLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001313 for (j = 0; j < spm->numSparingTables; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001314 struct buffer_head *bh2;
1315
1316 loc = le32_to_cpu(
1317 spm->locSparingTable[j]);
1318 bh2 = udf_read_tagged(sb, loc, loc,
1319 &ident);
1320 map->s_type_specific.s_sparing.
1321 s_spar_map[j] = bh2;
1322
Marcin Slusarz165923f2008-02-10 11:33:08 +01001323 if (bh2 == NULL)
1324 continue;
1325
1326 st = (struct sparingTable *)bh2->b_data;
1327 if (ident != 0 || strncmp(
1328 st->sparingIdent.ident,
1329 UDF_ID_SPARING,
1330 strlen(UDF_ID_SPARING))) {
1331 brelse(bh2);
1332 map->s_type_specific.s_sparing.
1333 s_spar_map[j] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001336 map->s_partition_func = udf_get_pblock_spar15;
Jan Karabfb257a2008-04-08 20:37:21 +02001337 } else if (!strncmp(upm2->partIdent.ident,
1338 UDF_ID_METADATA,
1339 strlen(UDF_ID_METADATA))) {
1340 struct udf_meta_data *mdata =
1341 &map->s_type_specific.s_metadata;
1342 struct metadataPartitionMap *mdm =
1343 (struct metadataPartitionMap *)
1344 &(lvd->partitionMaps[offset]);
1345 udf_debug("Parsing Logical vol part %d "
1346 "type %d id=%s\n", i, type,
1347 UDF_ID_METADATA);
1348
1349 map->s_partition_type = UDF_METADATA_MAP25;
1350 map->s_partition_func = udf_get_pblock_meta25;
1351
1352 mdata->s_meta_file_loc =
1353 le32_to_cpu(mdm->metadataFileLoc);
1354 mdata->s_mirror_file_loc =
1355 le32_to_cpu(mdm->metadataMirrorFileLoc);
1356 mdata->s_bitmap_file_loc =
1357 le32_to_cpu(mdm->metadataBitmapFileLoc);
1358 mdata->s_alloc_unit_size =
1359 le32_to_cpu(mdm->allocUnitSize);
1360 mdata->s_align_unit_size =
1361 le16_to_cpu(mdm->alignUnitSize);
1362 mdata->s_dup_md_flag =
1363 mdm->flags & 0x01;
1364
1365 udf_debug("Metadata Ident suffix=0x%x\n",
1366 (le16_to_cpu(
1367 ((__le16 *)
1368 mdm->partIdent.identSuffix)[0])));
1369 udf_debug("Metadata part num=%d\n",
1370 le16_to_cpu(mdm->partitionNum));
1371 udf_debug("Metadata part alloc unit size=%d\n",
1372 le32_to_cpu(mdm->allocUnitSize));
1373 udf_debug("Metadata file loc=%d\n",
1374 le32_to_cpu(mdm->metadataFileLoc));
1375 udf_debug("Mirror file loc=%d\n",
1376 le32_to_cpu(mdm->metadataMirrorFileLoc));
1377 udf_debug("Bitmap file loc=%d\n",
1378 le32_to_cpu(mdm->metadataBitmapFileLoc));
1379 udf_debug("Duplicate Flag: %d %d\n",
1380 mdata->s_dup_md_flag, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001381 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001382 udf_debug("Unknown ident: %s\n",
1383 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 continue;
1385 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001386 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1387 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001390 i, map->s_partition_num, type,
1391 map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001394 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001395 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 *fileset = lelb_to_cpu(la->extLocation);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001398 udf_debug("FileSet found in LogicalVolDesc at block=%d, "
1399 "partition=%d\n", fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001400 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 if (lvd->integritySeqExt.extLength)
1403 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001404
Jan Karac0eb31e2008-04-01 16:50:35 +02001405out_bh:
1406 brelse(bh);
1407 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410/*
1411 * udf_load_logicalvolint
1412 *
1413 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001414static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 struct buffer_head *bh = NULL;
1417 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001418 struct udf_sb_info *sbi = UDF_SB(sb);
1419 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001422 (bh = udf_read_tagged(sb, loc.extLocation,
1423 loc.extLocation, &ident)) &&
1424 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001425 sbi->s_lvid_bh = bh;
1426 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001427
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001428 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001429 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001430 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001431
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001432 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001433 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001435 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001437 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001438 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
1441/*
1442 * udf_process_sequence
1443 *
1444 * PURPOSE
1445 * Process a main/reserve volume descriptor sequence.
1446 *
1447 * PRE-CONDITIONS
1448 * sb Pointer to _locked_ superblock.
1449 * block First block of first extent of the sequence.
1450 * lastblock Lastblock of first extent of the sequence.
1451 *
1452 * HISTORY
1453 * July 1, 1997 - Andrew E. Mileski
1454 * Written, tested, and released.
1455 */
Jan Kara200a3592008-03-04 13:03:09 +01001456static noinline int udf_process_sequence(struct super_block *sb, long block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001457 long lastblock, struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct buffer_head *bh = NULL;
1460 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001461 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 struct generic_desc *gd;
1463 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001464 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 uint32_t vdsn;
1466 uint16_t ident;
1467 long next_s = 0, next_e = 0;
1468
1469 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1470
Jan Karac0eb31e2008-04-01 16:50:35 +02001471 /*
1472 * Read the main descriptor sequence and find which descriptors
1473 * are in it.
1474 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001475 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001478 if (!bh) {
1479 printk(KERN_ERR "udf: Block %Lu of volume descriptor "
1480 "sequence is corrupted or we could not read "
1481 "it.\n", (unsigned long long)block);
1482 return 1;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1486 gd = (struct generic_desc *)bh->b_data;
1487 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001488 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001489 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001490 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1491 if (vdsn >= curr->volDescSeqNum) {
1492 curr->volDescSeqNum = vdsn;
1493 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001494 }
1495 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001496 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001497 curr = &vds[VDS_POS_VOL_DESC_PTR];
1498 if (vdsn >= curr->volDescSeqNum) {
1499 curr->volDescSeqNum = vdsn;
1500 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001502 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001503 next_s = le32_to_cpu(
1504 vdp->nextVolDescSeqExt.extLocation);
1505 next_e = le32_to_cpu(
1506 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001507 next_e = next_e >> sb->s_blocksize_bits;
1508 next_e += next_s;
1509 }
1510 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001511 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001512 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1513 if (vdsn >= curr->volDescSeqNum) {
1514 curr->volDescSeqNum = vdsn;
1515 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001516 }
1517 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001518 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001519 curr = &vds[VDS_POS_PARTITION_DESC];
1520 if (!curr->block)
1521 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001522 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001523 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001524 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1525 if (vdsn >= curr->volDescSeqNum) {
1526 curr->volDescSeqNum = vdsn;
1527 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001528 }
1529 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001530 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001531 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1532 if (vdsn >= curr->volDescSeqNum) {
1533 curr->volDescSeqNum = vdsn;
1534 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001535 }
1536 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001537 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001538 vds[VDS_POS_TERMINATING_DESC].block = block;
1539 if (next_e) {
1540 block = next_s;
1541 lastblock = next_e;
1542 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001543 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001544 done = 1;
1545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001547 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001549 /*
1550 * Now read interesting descriptors again and process them
1551 * in a suitable order
1552 */
1553 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
1554 printk(KERN_ERR "udf: Primary Volume Descriptor not found!\n");
1555 return 1;
1556 }
1557 if (udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block))
1558 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Jan Karac0eb31e2008-04-01 16:50:35 +02001560 if (vds[VDS_POS_LOGICAL_VOL_DESC].block && udf_load_logicalvol(sb,
1561 vds[VDS_POS_LOGICAL_VOL_DESC].block, fileset))
1562 return 1;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001563
Jan Karac0eb31e2008-04-01 16:50:35 +02001564 if (vds[VDS_POS_PARTITION_DESC].block) {
1565 /*
1566 * We rescan the whole descriptor sequence to find
1567 * partition descriptor blocks and process them.
1568 */
1569 for (block = vds[VDS_POS_PARTITION_DESC].block;
1570 block < vds[VDS_POS_TERMINATING_DESC].block;
1571 block++)
1572 if (udf_load_partdesc(sb, block))
Marcin Slusarz165923f2008-02-10 11:33:08 +01001573 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
1576 return 0;
1577}
1578
Jan Kara40346002009-03-19 16:21:38 +01001579static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1580 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Jan Kara40346002009-03-19 16:21:38 +01001582 struct anchorVolDescPtr *anchor;
1583 long main_s, main_e, reserve_s, reserve_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jan Kara40346002009-03-19 16:21:38 +01001585 anchor = (struct anchorVolDescPtr *)bh->b_data;
1586
1587 /* Locate the main sequence */
1588 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1589 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1590 main_e = main_e >> sb->s_blocksize_bits;
1591 main_e += main_s;
1592
1593 /* Locate the reserve sequence */
1594 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1595 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1596 reserve_e = reserve_e >> sb->s_blocksize_bits;
1597 reserve_e += reserve_s;
1598
1599 /* Process the main & reserve sequences */
1600 /* responsible for finding the PartitionDesc(s) */
1601 if (!udf_process_sequence(sb, main_s, main_e, fileset))
1602 return 1;
1603 return !udf_process_sequence(sb, reserve_s, reserve_e, fileset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Jan Kara40346002009-03-19 16:21:38 +01001606/*
1607 * Check whether there is an anchor block in the given block and
1608 * load Volume Descriptor Sequence if so.
1609 */
1610static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1611 struct kernel_lb_addr *fileset)
1612{
1613 struct buffer_head *bh;
1614 uint16_t ident;
1615 int ret;
1616
1617 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1618 udf_fixed_to_variable(block) >=
1619 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
1620 return 0;
1621
1622 bh = udf_read_tagged(sb, block, block, &ident);
1623 if (!bh)
1624 return 0;
1625 if (ident != TAG_IDENT_AVDP) {
1626 brelse(bh);
1627 return 0;
1628 }
1629 ret = udf_load_sequence(sb, bh, fileset);
1630 brelse(bh);
1631 return ret;
1632}
1633
1634/* Search for an anchor volume descriptor pointer */
1635static sector_t udf_scan_anchors(struct super_block *sb, sector_t lastblock,
1636 struct kernel_lb_addr *fileset)
1637{
1638 sector_t last[6];
1639 int i;
1640 struct udf_sb_info *sbi = UDF_SB(sb);
1641 int last_count = 0;
1642
1643 /* First try user provided anchor */
1644 if (sbi->s_anchor) {
1645 if (udf_check_anchor_block(sb, sbi->s_anchor, fileset))
1646 return lastblock;
1647 }
1648 /*
1649 * according to spec, anchor is in either:
1650 * block 256
1651 * lastblock-256
1652 * lastblock
1653 * however, if the disc isn't closed, it could be 512.
1654 */
1655 if (udf_check_anchor_block(sb, sbi->s_session + 256, fileset))
1656 return lastblock;
1657 /*
1658 * The trouble is which block is the last one. Drives often misreport
1659 * this so we try various possibilities.
1660 */
1661 last[last_count++] = lastblock;
1662 if (lastblock >= 1)
1663 last[last_count++] = lastblock - 1;
1664 last[last_count++] = lastblock + 1;
1665 if (lastblock >= 2)
1666 last[last_count++] = lastblock - 2;
1667 if (lastblock >= 150)
1668 last[last_count++] = lastblock - 150;
1669 if (lastblock >= 152)
1670 last[last_count++] = lastblock - 152;
1671
1672 for (i = 0; i < last_count; i++) {
1673 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
1674 sb->s_blocksize_bits)
1675 continue;
1676 if (udf_check_anchor_block(sb, last[i], fileset))
1677 return last[i];
1678 if (last[i] < 256)
1679 continue;
1680 if (udf_check_anchor_block(sb, last[i] - 256, fileset))
1681 return last[i];
1682 }
1683
1684 /* Finally try block 512 in case media is open */
1685 if (udf_check_anchor_block(sb, sbi->s_session + 512, fileset))
1686 return last[0];
1687 return 0;
1688}
1689
1690/*
1691 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1692 * area specified by it. The function expects sbi->s_lastblock to be the last
1693 * block on the media.
1694 *
1695 * Return 1 if ok, 0 if not found.
1696 *
1697 */
1698static int udf_find_anchor(struct super_block *sb,
1699 struct kernel_lb_addr *fileset)
1700{
1701 sector_t lastblock;
1702 struct udf_sb_info *sbi = UDF_SB(sb);
1703
1704 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1705 if (lastblock)
1706 goto out;
1707
1708 /* No anchor found? Try VARCONV conversion of block numbers */
1709 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
1710 /* Firstly, we try to not convert number of the last block */
1711 lastblock = udf_scan_anchors(sb,
1712 udf_variable_to_fixed(sbi->s_last_block),
1713 fileset);
1714 if (lastblock)
1715 goto out;
1716
1717 /* Secondly, we try with converted number of the last block */
1718 lastblock = udf_scan_anchors(sb, sbi->s_last_block, fileset);
1719 if (!lastblock) {
1720 /* VARCONV didn't help. Clear it. */
1721 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
1722 return 0;
1723 }
1724out:
1725 sbi->s_last_block = lastblock;
1726 return 1;
1727}
1728
1729/*
1730 * Check Volume Structure Descriptor, find Anchor block and load Volume
1731 * Descriptor Sequence
1732 */
1733static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1734 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001735{
1736 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara40346002009-03-19 16:21:38 +01001737 loff_t nsr_off;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001738
1739 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1740 if (!silent)
1741 printk(KERN_WARNING "UDF-fs: Bad block size\n");
1742 return 0;
1743 }
1744 sbi->s_last_block = uopt->lastblock;
Jan Kara40346002009-03-19 16:21:38 +01001745 if (!uopt->novrs) {
1746 /* Check that it is NSR02 compliant */
1747 nsr_off = udf_check_vsd(sb);
1748 if (!nsr_off) {
1749 if (!silent)
1750 printk(KERN_WARNING "UDF-fs: No VRS found\n");
1751 return 0;
1752 }
1753 if (nsr_off == -1)
1754 udf_debug("Failed to read byte 32768. Assuming open "
1755 "disc. Skipping validity check\n");
1756 if (!sbi->s_last_block)
1757 sbi->s_last_block = udf_get_last_block(sb);
1758 } else {
1759 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001760 }
Jan Kara40346002009-03-19 16:21:38 +01001761
1762 /* Look for anchor block and load Volume Descriptor Sequence */
1763 sbi->s_anchor = uopt->anchor;
1764 if (!udf_find_anchor(sb, fileset)) {
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001765 if (!silent)
1766 printk(KERN_WARNING "UDF-fs: No anchor found\n");
1767 return 0;
1768 }
1769 return 1;
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772static void udf_open_lvid(struct super_block *sb)
1773{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001774 struct udf_sb_info *sbi = UDF_SB(sb);
1775 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001776 struct logicalVolIntegrityDesc *lvid;
1777 struct logicalVolIntegrityDescImpUse *lvidiu;
Jan Kara146bca72009-03-16 18:27:37 +01001778
Marcin Slusarz165923f2008-02-10 11:33:08 +01001779 if (!bh)
1780 return;
Jan Kara949f4a72010-10-20 18:49:20 +02001781
1782 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001783 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1784 lvidiu = udf_sb_lvidiu(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Marcin Slusarz165923f2008-02-10 11:33:08 +01001786 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1787 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1788 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1789 CURRENT_TIME);
Jan Kara146bca72009-03-16 18:27:37 +01001790 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Marcin Slusarz165923f2008-02-10 11:33:08 +01001792 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001793 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001794 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001795
1796 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1797 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001798 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02001799 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
1802static void udf_close_lvid(struct super_block *sb)
1803{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001804 struct udf_sb_info *sbi = UDF_SB(sb);
1805 struct buffer_head *bh = sbi->s_lvid_bh;
1806 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001807 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001808
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001809 if (!bh)
1810 return;
1811
Jan Kara949f4a72010-10-20 18:49:20 +02001812 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001813 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001814 lvidiu = udf_sb_lvidiu(sbi);
1815 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1816 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1817 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1818 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1819 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1820 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1821 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1822 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1823 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1824 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Marcin Slusarz165923f2008-02-10 11:33:08 +01001826 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001827 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001828 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001829
1830 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1831 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001832 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02001833 mutex_unlock(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Jan Karad664b6a2010-10-20 18:28:46 +02001836u64 lvid_get_unique_id(struct super_block *sb)
1837{
1838 struct buffer_head *bh;
1839 struct udf_sb_info *sbi = UDF_SB(sb);
1840 struct logicalVolIntegrityDesc *lvid;
1841 struct logicalVolHeaderDesc *lvhd;
1842 u64 uniqueID;
1843 u64 ret;
1844
1845 bh = sbi->s_lvid_bh;
1846 if (!bh)
1847 return 0;
1848
1849 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1850 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
1851
1852 mutex_lock(&sbi->s_alloc_mutex);
1853 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
1854 if (!(++uniqueID & 0xFFFFFFFF))
1855 uniqueID += 16;
1856 lvhd->uniqueID = cpu_to_le64(uniqueID);
1857 mutex_unlock(&sbi->s_alloc_mutex);
1858 mark_buffer_dirty(bh);
1859
1860 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001863static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
1864{
1865 int i;
1866 int nr_groups = bitmap->s_nr_groups;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001867 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
1868 nr_groups);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001869
1870 for (i = 0; i < nr_groups; i++)
1871 if (bitmap->s_block_bitmap[i])
1872 brelse(bitmap->s_block_bitmap[i]);
1873
1874 if (size <= PAGE_SIZE)
1875 kfree(bitmap);
1876 else
1877 vfree(bitmap);
1878}
1879
Jan Kara2e0838f2008-04-01 18:08:51 +02001880static void udf_free_partition(struct udf_part_map *map)
1881{
1882 int i;
Jan Karabfb257a2008-04-08 20:37:21 +02001883 struct udf_meta_data *mdata;
Jan Kara2e0838f2008-04-01 18:08:51 +02001884
1885 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
1886 iput(map->s_uspace.s_table);
1887 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
1888 iput(map->s_fspace.s_table);
1889 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
1890 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
1891 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
1892 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
1893 if (map->s_partition_type == UDF_SPARABLE_MAP15)
1894 for (i = 0; i < 4; i++)
1895 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
Jan Karabfb257a2008-04-08 20:37:21 +02001896 else if (map->s_partition_type == UDF_METADATA_MAP25) {
1897 mdata = &map->s_type_specific.s_metadata;
1898 iput(mdata->s_metadata_fe);
1899 mdata->s_metadata_fe = NULL;
1900
1901 iput(mdata->s_mirror_fe);
1902 mdata->s_mirror_fe = NULL;
1903
1904 iput(mdata->s_bitmap_fe);
1905 mdata->s_bitmap_fe = NULL;
1906 }
Jan Kara2e0838f2008-04-01 18:08:51 +02001907}
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909static int udf_fill_super(struct super_block *sb, void *options, int silent)
1910{
1911 int i;
Jan Kara40346002009-03-19 16:21:38 +01001912 int ret;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001913 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001915 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct udf_sb_info *sbi;
1917
1918 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
1919 uopt.uid = -1;
1920 uopt.gid = -1;
1921 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01001922 uopt.fmode = UDF_INVALID_MODE;
1923 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001925 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Alessio Igor Bogani9db9f9e2010-11-16 18:40:49 +01001926 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Ingo Molnar1e7933d2006-03-23 03:00:44 -08001931 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Miklos Szeredi6da80892008-02-08 04:21:50 -08001933 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto error_out;
1935
1936 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001937 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 udf_error(sb, "udf_read_super",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001939 "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto error_out;
1941 }
1942#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001943 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 uopt.nls_map = load_nls_default();
1945 if (!uopt.nls_map)
1946 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
1947 else
1948 udf_debug("Using default NLS map\n");
1949 }
1950#endif
1951 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
1952 uopt.flags |= (1 << UDF_FLAG_UTF8);
1953
1954 fileset.logicalBlockNum = 0xFFFFFFFF;
1955 fileset.partitionReferenceNum = 0xFFFF;
1956
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001957 sbi->s_flags = uopt.flags;
1958 sbi->s_uid = uopt.uid;
1959 sbi->s_gid = uopt.gid;
1960 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd2008-11-16 20:52:19 +01001961 sbi->s_fmode = uopt.fmode;
1962 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001963 sbi->s_nls_map = uopt.nls_map;
Jan Karac03cad22010-10-20 22:17:28 +02001964 rwlock_init(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001966 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001967 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001969 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001971 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* Fill in the rest of the superblock */
1974 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02001975 sb->s_export_op = &udf_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 sb->s_dirt = 0;
1978 sb->s_magic = UDF_SUPER_MAGIC;
1979 sb->s_time_gran = 1000;
1980
Jan Kara40346002009-03-19 16:21:38 +01001981 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
1982 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1983 } else {
Martin K. Petersene1defc42009-05-22 17:17:49 -04001984 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
Jan Kara40346002009-03-19 16:21:38 +01001985 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1986 if (!ret && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
1987 if (!silent)
1988 printk(KERN_NOTICE
1989 "UDF-fs: Rescanning with blocksize "
1990 "%d\n", UDF_DEFAULT_BLOCKSIZE);
1991 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
1992 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
1993 }
1994 }
1995 if (!ret) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001996 printk(KERN_WARNING "UDF-fs: No partition found (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto error_out;
1998 }
1999
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002000 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002002 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002003 struct logicalVolIntegrityDescImpUse *lvidiu =
2004 udf_sb_lvidiu(sbi);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002005 uint16_t minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2006 uint16_t minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Marcin Slusarz4b111112008-02-08 04:20:36 -08002007 /* uint16_t maxUDFWriteRev =
2008 le16_to_cpu(lvidiu->maxUDFWriteRev); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002010 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002011 printk(KERN_ERR "UDF-fs: minUDFReadRev=%x "
2012 "(max is %x)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002013 le16_to_cpu(lvidiu->minUDFReadRev),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002014 UDF_MAX_READ_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 goto error_out;
Marcin Slusarz4b111112008-02-08 04:20:36 -08002016 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 sb->s_flags |= MS_RDONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002019 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2022 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2023 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2024 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2025 }
2026
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002027 if (!sbi->s_partitions) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002028 printk(KERN_WARNING "UDF-fs: No partition found (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 goto error_out;
2030 }
2031
Marcin Slusarz4b111112008-02-08 04:20:36 -08002032 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
2033 UDF_PART_FLAG_READ_ONLY) {
2034 printk(KERN_NOTICE "UDF-fs: Partition marked readonly; "
2035 "forcing readonly mount\n");
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002036 sb->s_flags |= MS_RDONLY;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002037 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002038
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002039 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002040 printk(KERN_WARNING "UDF-fs: No fileset found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 goto error_out;
2042 }
2043
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002044 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002045 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002046 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Adrian Bunka9ca6632008-02-08 04:20:47 -08002047 udf_info("UDF: Mounting volume '%s', "
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002048 "timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarz56774802008-02-10 11:25:31 +01002049 sbi->s_volume_ident, le16_to_cpu(ts.year), ts.month, ts.day,
2050 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 if (!(sb->s_flags & MS_RDONLY))
2053 udf_open_lvid(sb);
2054
2055 /* Assign the root inode */
2056 /* assign inodes by physical block number */
2057 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002058 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002059 if (!inode) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002060 printk(KERN_ERR "UDF-fs: Error in udf_iget, block=%d, "
2061 "partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002062 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 goto error_out;
2064 }
2065
2066 /* Allocate a dentry for the root inode */
2067 sb->s_root = d_alloc_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002068 if (!sb->s_root) {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002069 printk(KERN_ERR "UDF-fs: Couldn't allocate root dentry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 iput(inode);
2071 goto error_out;
2072 }
Jan Kara31170b62007-05-08 00:35:21 -07002073 sb->s_maxbytes = MAX_LFS_FILESIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002076error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002077 if (sbi->s_vat_inode)
2078 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002079 if (sbi->s_partitions)
2080 for (i = 0; i < sbi->s_partitions; i++)
2081 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#ifdef CONFIG_UDF_NLS
2083 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002084 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085#endif
2086 if (!(sb->s_flags & MS_RDONLY))
2087 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002088 brelse(sbi->s_lvid_bh);
2089
2090 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 kfree(sbi);
2092 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return -EINVAL;
2095}
2096
Joe Perches7e273e32011-10-10 01:08:02 -07002097void udf_error(struct super_block *sb, const char *function,
2098 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 va_list args;
2101
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002102 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /* mark sb error */
2104 sb->s_dirt = 1;
2105 }
2106 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002107 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 va_end(args);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002109 printk(KERN_CRIT "UDF-fs error (device %s): %s: %s\n",
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002110 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113void udf_warning(struct super_block *sb, const char *function,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002114 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 va_list args;
2117
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002118 va_start(args, fmt);
Alexey Dobriyan4a6e6172006-12-06 20:37:04 -08002119 vsnprintf(error_buf, sizeof(error_buf), fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 va_end(args);
2121 printk(KERN_WARNING "UDF-fs warning (device %s): %s: %s\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002122 sb->s_id, function, error_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002125static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
2127 int i;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002128 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002130 sbi = UDF_SB(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002131
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002132 if (sbi->s_vat_inode)
2133 iput(sbi->s_vat_inode);
Jan Kara2e0838f2008-04-01 18:08:51 +02002134 if (sbi->s_partitions)
2135 for (i = 0; i < sbi->s_partitions; i++)
2136 udf_free_partition(&sbi->s_partmaps[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137#ifdef CONFIG_UDF_NLS
2138 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002139 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140#endif
2141 if (!(sb->s_flags & MS_RDONLY))
2142 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002143 brelse(sbi->s_lvid_bh);
2144 kfree(sbi->s_partmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 kfree(sb->s_fs_info);
2146 sb->s_fs_info = NULL;
2147}
2148
Jan Kara146bca72009-03-16 18:27:37 +01002149static int udf_sync_fs(struct super_block *sb, int wait)
2150{
2151 struct udf_sb_info *sbi = UDF_SB(sb);
2152
2153 mutex_lock(&sbi->s_alloc_mutex);
2154 if (sbi->s_lvid_dirty) {
2155 /*
2156 * Blockdevice will be synced later so we don't have to submit
2157 * the buffer for IO
2158 */
2159 mark_buffer_dirty(sbi->s_lvid_bh);
2160 sb->s_dirt = 0;
2161 sbi->s_lvid_dirty = 0;
2162 }
2163 mutex_unlock(&sbi->s_alloc_mutex);
2164
2165 return 0;
2166}
2167
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002168static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
David Howells726c3342006-06-23 02:02:58 -07002170 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002171 struct udf_sb_info *sbi = UDF_SB(sb);
2172 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002173 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002174
2175 if (sbi->s_lvid_bh != NULL)
2176 lvidiu = udf_sb_lvidiu(sbi);
2177 else
2178 lvidiu = NULL;
David Howells726c3342006-06-23 02:02:58 -07002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 buf->f_type = UDF_SUPER_MAGIC;
2181 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002182 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 buf->f_bfree = udf_count_free(sb);
2184 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002185 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2186 le32_to_cpu(lvidiu->numDirs)) : 0)
2187 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 buf->f_ffree = buf->f_bfree;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002189 buf->f_namelen = UDF_NAME_LEN - 2;
Coly Li557f5a12009-01-20 01:36:55 +08002190 buf->f_fsid.val[0] = (u32)id;
2191 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 return 0;
2194}
2195
Marcin Slusarz4b111112008-02-08 04:20:36 -08002196static unsigned int udf_count_free_bitmap(struct super_block *sb,
2197 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 struct buffer_head *bh = NULL;
2200 unsigned int accum = 0;
2201 int index;
2202 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002203 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 uint8_t *ptr;
2206 uint16_t ident;
2207 struct spaceBitmapDesc *bm;
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002210 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002211 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002213 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 printk(KERN_ERR "udf: udf_count_free failed\n");
2215 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002216 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002217 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 printk(KERN_ERR "udf: udf_count_free failed\n");
2219 goto out;
2220 }
2221
2222 bm = (struct spaceBitmapDesc *)bh->b_data;
2223 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002224 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2225 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002227 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002228 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2229 accum += bitmap_weight((const unsigned long *)(ptr + index),
2230 cur_bytes * 8);
2231 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002232 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002233 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002234 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002236 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 udf_debug("read failed\n");
2238 goto out;
2239 }
2240 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002241 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002244 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return accum;
2247}
2248
Marcin Slusarz4b111112008-02-08 04:20:36 -08002249static unsigned int udf_count_free_table(struct super_block *sb,
2250 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002253 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002254 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002256 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Jan Karad1668fe2010-10-20 23:24:12 +02002258 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002259 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002260 epos.offset = sizeof(struct unallocSpaceEntry);
2261 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002263 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002265
Jan Kara3bf25cb2007-05-08 00:35:16 -07002266 brelse(epos.bh);
Jan Karad1668fe2010-10-20 23:24:12 +02002267 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return accum;
2270}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002271
2272static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
2274 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002275 struct udf_sb_info *sbi;
2276 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002278 sbi = UDF_SB(sb);
2279 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002280 struct logicalVolIntegrityDesc *lvid =
2281 (struct logicalVolIntegrityDesc *)
2282 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002283 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002284 accum = le32_to_cpu(
2285 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (accum == 0xFFFFFFFF)
2287 accum = 0;
2288 }
2289 }
2290
2291 if (accum)
2292 return accum;
2293
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002294 map = &sbi->s_partmaps[sbi->s_partition];
2295 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002296 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002297 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002299 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002300 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002301 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 if (accum)
2304 return accum;
2305
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002306 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002307 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002308 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002310 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002311 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002312 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
2314
2315 return accum;
2316}