blob: 64f2b7334d08bf41f6ec9ed0910393eb680d9fbc [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>
Jan Kara1df2ae32012-06-27 21:23:07 +020059#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/byteorder.h>
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "udf_sb.h"
63#include "udf_i.h"
64
65#include <linux/init.h>
66#include <asm/uaccess.h>
67
68#define VDS_POS_PRIMARY_VOL_DESC 0
69#define VDS_POS_UNALLOC_SPACE_DESC 1
70#define VDS_POS_LOGICAL_VOL_DESC 2
71#define VDS_POS_PARTITION_DESC 3
72#define VDS_POS_IMP_USE_VOL_DESC 4
73#define VDS_POS_VOL_DESC_PTR 5
74#define VDS_POS_TERMINATING_DESC 6
75#define VDS_POS_LENGTH 7
76
Miklos Szeredi6da80892008-02-08 04:21:50 -080077#define UDF_DEFAULT_BLOCKSIZE 2048
78
Peter A. Felvegi44499602013-10-18 20:07:44 +020079#define VSD_FIRST_SECTOR_OFFSET 32768
80#define VSD_MAX_SECTOR_OFFSET 0x800000
81
Al Viro8de52772012-02-06 12:45:27 -050082enum { UDF_MAX_LINKS = 0xffff };
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* These are the "meat" - everything else is stuffing */
85static int udf_fill_super(struct super_block *, void *, int);
86static void udf_put_super(struct super_block *);
Jan Kara146bca72009-03-16 18:27:37 +010087static int udf_sync_fs(struct super_block *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static int udf_remount_fs(struct super_block *, int *, char *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020089static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020090static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
91 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020093 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void udf_open_lvid(struct super_block *);
95static void udf_close_lvid(struct super_block *);
96static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -070097static int udf_statfs(struct dentry *, struct kstatfs *);
Al Viro34c80b12011-12-08 21:32:45 -050098static int udf_show_options(struct seq_file *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jan Kara69d75672013-09-12 22:00:15 +0200100struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800101{
Jan Kara69d75672013-09-12 22:00:15 +0200102 struct logicalVolIntegrityDesc *lvid;
103 unsigned int partnum;
104 unsigned int offset;
105
106 if (!UDF_SB(sb)->s_lvid_bh)
107 return NULL;
108 lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
109 partnum = le32_to_cpu(lvid->numOfPartitions);
110 if ((sb->s_blocksize - sizeof(struct logicalVolIntegrityDescImpUse) -
111 offsetof(struct logicalVolIntegrityDesc, impUse)) /
112 (2 * sizeof(uint32_t)) < partnum) {
113 udf_err(sb, "Logical volume integrity descriptor corrupted "
114 "(numOfPartitions = %u)!\n", partnum);
115 return NULL;
116 }
117 /* The offset is to skip freeSpaceTable and sizeTable arrays */
118 offset = partnum * 2 * sizeof(uint32_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800119 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* UDF filesystem type */
Al Viro152a0832010-07-25 00:46:55 +0400123static struct dentry *udf_mount(struct file_system_type *fs_type,
124 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Al Viro152a0832010-07-25 00:46:55 +0400126 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700130 .owner = THIS_MODULE,
131 .name = "udf",
Al Viro152a0832010-07-25 00:46:55 +0400132 .mount = udf_mount,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700133 .kill_sb = kill_block_super,
134 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
Eric W. Biederman3e64fe52013-03-11 07:05:42 -0700136MODULE_ALIAS_FS("udf");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700138static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140static struct inode *udf_alloc_inode(struct super_block *sb)
141{
142 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800143 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!ei)
145 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700146
147 ei->i_unique = 0;
148 ei->i_lenExtents = 0;
149 ei->i_next_alloc_block = 0;
150 ei->i_next_alloc_goal = 0;
151 ei->i_strat4096 = 0;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100152 init_rwsem(&ei->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900153 ei->cached_extent.lstart = -1;
154 spin_lock_init(&ei->i_extent_cache_lock);
Dan Bastone95f87972006-08-13 23:24:18 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return &ei->vfs_inode;
157}
158
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100159static void udf_i_callback(struct rcu_head *head)
160{
161 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100162 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void udf_destroy_inode(struct inode *inode)
166{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100167 call_rcu(&inode->i_rcu, udf_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700170static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700172 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Christoph Lametera35afb82007-05-16 22:10:57 -0700174 ei->i_ext.i_data = NULL;
175 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178static int init_inodecache(void)
179{
180 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
181 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700182 0, (SLAB_RECLAIM_ACCOUNT |
183 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900184 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700185 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENOMEM;
187 return 0;
188}
189
190static void destroy_inodecache(void)
191{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000192 /*
193 * Make sure all delayed rcu free inodes are flushed before we
194 * destroy cache.
195 */
196 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700197 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800201static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700202 .alloc_inode = udf_alloc_inode,
203 .destroy_inode = udf_destroy_inode,
204 .write_inode = udf_write_inode,
Al Viro3aac2b62010-06-07 00:43:39 -0400205 .evict_inode = udf_evict_inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700206 .put_super = udf_put_super,
Jan Kara146bca72009-03-16 18:27:37 +0100207 .sync_fs = udf_sync_fs,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700208 .statfs = udf_statfs,
209 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800210 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211};
212
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700213struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned char novrs;
215 unsigned int blocksize;
216 unsigned int session;
217 unsigned int lastblock;
218 unsigned int anchor;
219 unsigned int volume;
220 unsigned short partition;
221 unsigned int fileset;
222 unsigned int rootdir;
223 unsigned int flags;
Al Virofaa17292011-07-26 03:18:29 -0400224 umode_t umask;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800225 kgid_t gid;
226 kuid_t uid;
Al Virofaa17292011-07-26 03:18:29 -0400227 umode_t fmode;
228 umode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 struct nls_table *nls_map;
230};
231
232static int __init init_udf_fs(void)
233{
234 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 err = init_inodecache();
237 if (err)
238 goto out1;
239 err = register_filesystem(&udf_fstype);
240 if (err)
241 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244
245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700247
248out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return err;
250}
251
252static void __exit exit_udf_fs(void)
253{
254 unregister_filesystem(&udf_fstype);
255 destroy_inodecache();
256}
257
258module_init(init_udf_fs)
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700259module_exit(exit_udf_fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800261static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
262{
263 struct udf_sb_info *sbi = UDF_SB(sb);
264
265 sbi->s_partmaps = kcalloc(count, sizeof(struct udf_part_map),
266 GFP_KERNEL);
267 if (!sbi->s_partmaps) {
Joe Perches8076c362011-10-10 01:08:03 -0700268 udf_err(sb, "Unable to allocate space for %d partition maps\n",
269 count);
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800270 sbi->s_partitions = 0;
271 return -ENOMEM;
272 }
273
274 sbi->s_partitions = count;
275 return 0;
276}
277
Jan Karabff943a2012-06-27 22:27:05 +0200278static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
279{
280 int i;
281 int nr_groups = bitmap->s_nr_groups;
282 int size = sizeof(struct udf_bitmap) + (sizeof(struct buffer_head *) *
283 nr_groups);
284
285 for (i = 0; i < nr_groups; i++)
286 if (bitmap->s_block_bitmap[i])
287 brelse(bitmap->s_block_bitmap[i]);
288
289 if (size <= PAGE_SIZE)
290 kfree(bitmap);
291 else
292 vfree(bitmap);
293}
294
295static void udf_free_partition(struct udf_part_map *map)
296{
297 int i;
298 struct udf_meta_data *mdata;
299
300 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
301 iput(map->s_uspace.s_table);
302 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
303 iput(map->s_fspace.s_table);
304 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
305 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
306 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
307 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
308 if (map->s_partition_type == UDF_SPARABLE_MAP15)
309 for (i = 0; i < 4; i++)
310 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
311 else if (map->s_partition_type == UDF_METADATA_MAP25) {
312 mdata = &map->s_type_specific.s_metadata;
313 iput(mdata->s_metadata_fe);
314 mdata->s_metadata_fe = NULL;
315
316 iput(mdata->s_mirror_fe);
317 mdata->s_mirror_fe = NULL;
318
319 iput(mdata->s_bitmap_fe);
320 mdata->s_bitmap_fe = NULL;
321 }
322}
323
324static void udf_sb_free_partitions(struct super_block *sb)
325{
326 struct udf_sb_info *sbi = UDF_SB(sb);
327 int i;
Namjae Jeon1b1baff2013-01-14 22:53:47 +0100328 if (sbi->s_partmaps == NULL)
329 return;
Jan Karabff943a2012-06-27 22:27:05 +0200330 for (i = 0; i < sbi->s_partitions; i++)
331 udf_free_partition(&sbi->s_partmaps[i]);
332 kfree(sbi->s_partmaps);
333 sbi->s_partmaps = NULL;
334}
335
Al Viro34c80b12011-12-08 21:32:45 -0500336static int udf_show_options(struct seq_file *seq, struct dentry *root)
Miklos Szeredi6da80892008-02-08 04:21:50 -0800337{
Al Viro34c80b12011-12-08 21:32:45 -0500338 struct super_block *sb = root->d_sb;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800339 struct udf_sb_info *sbi = UDF_SB(sb);
340
341 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
342 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100343 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800344 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
345 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
346 seq_puts(seq, ",unhide");
347 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
348 seq_puts(seq, ",undelete");
349 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
350 seq_puts(seq, ",noadinicb");
351 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
352 seq_puts(seq, ",shortad");
353 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
354 seq_puts(seq, ",uid=forget");
355 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_IGNORE))
356 seq_puts(seq, ",uid=ignore");
357 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
358 seq_puts(seq, ",gid=forget");
359 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_IGNORE))
360 seq_puts(seq, ",gid=ignore");
361 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800362 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800363 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800364 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800365 if (sbi->s_umask != 0)
Al Virofaa17292011-07-26 03:18:29 -0400366 seq_printf(seq, ",umask=%ho", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100367 if (sbi->s_fmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400368 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100369 if (sbi->s_dmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400370 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800371 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
372 seq_printf(seq, ",session=%u", sbi->s_session);
373 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
374 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara40346002009-03-19 16:21:38 +0100375 if (sbi->s_anchor != 0)
376 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800377 /*
378 * volume, partition, fileset and rootdir seem to be ignored
379 * currently
380 */
381 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
382 seq_puts(seq, ",utf8");
383 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
384 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
385
386 return 0;
387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * udf_parse_options
391 *
392 * PURPOSE
393 * Parse mount options.
394 *
395 * DESCRIPTION
396 * The following mount options are supported:
397 *
398 * gid= Set the default group.
399 * umask= Set the default umask.
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100400 * mode= Set the default file permissions.
401 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 * uid= Set the default user.
403 * bs= Set the block size.
404 * unhide Show otherwise hidden files.
405 * undelete Show deleted files in lists.
406 * adinicb Embed data in the inode (default)
407 * noadinicb Don't embed data in the inode
408 * shortad Use short ad's
409 * longad Use long ad's (default)
410 * nostrict Unset strict conformance
411 * iocharset= Set the NLS character set
412 *
413 * The remaining are for debugging and disaster recovery:
414 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700415 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
417 * The following expect a offset from 0.
418 *
419 * session= Set the CDROM session (default= last session)
420 * anchor= Override standard anchor location. (default= 256)
421 * volume= Override the VolumeDesc location. (unused)
422 * partition= Override the PartitionDesc location. (unused)
423 * lastblock= Set the last block of the filesystem/
424 *
425 * The following expect a offset from the partition root.
426 *
427 * fileset= Override the fileset block location. (unused)
428 * rootdir= Override the root directory location. (unused)
429 * WARNING: overriding the rootdir to a non-directory may
430 * yield highly unpredictable results.
431 *
432 * PRE-CONDITIONS
433 * options Pointer to mount options string.
434 * uopts Pointer to mount options variable.
435 *
436 * POST-CONDITIONS
437 * <return> 1 Mount options parsed okay.
438 * <return> 0 Error parsing mount options.
439 *
440 * HISTORY
441 * July 1, 1997 - Andrew E. Mileski
442 * Written, tested, and released.
443 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445enum {
446 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
447 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
448 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
449 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
450 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100451 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
452 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453};
454
Steven Whitehousea447c092008-10-13 10:46:57 +0100455static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700456 {Opt_novrs, "novrs"},
457 {Opt_nostrict, "nostrict"},
458 {Opt_bs, "bs=%u"},
459 {Opt_unhide, "unhide"},
460 {Opt_undelete, "undelete"},
461 {Opt_noadinicb, "noadinicb"},
462 {Opt_adinicb, "adinicb"},
463 {Opt_shortad, "shortad"},
464 {Opt_longad, "longad"},
465 {Opt_uforget, "uid=forget"},
466 {Opt_uignore, "uid=ignore"},
467 {Opt_gforget, "gid=forget"},
468 {Opt_gignore, "gid=ignore"},
469 {Opt_gid, "gid=%u"},
470 {Opt_uid, "uid=%u"},
471 {Opt_umask, "umask=%o"},
472 {Opt_session, "session=%u"},
473 {Opt_lastblock, "lastblock=%u"},
474 {Opt_anchor, "anchor=%u"},
475 {Opt_volume, "volume=%u"},
476 {Opt_partition, "partition=%u"},
477 {Opt_fileset, "fileset=%u"},
478 {Opt_rootdir, "rootdir=%u"},
479 {Opt_utf8, "utf8"},
480 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100481 {Opt_fmode, "mode=%o"},
482 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700483 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484};
485
Miklos Szeredi6da80892008-02-08 04:21:50 -0800486static int udf_parse_options(char *options, struct udf_options *uopt,
487 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 char *p;
490 int option;
491
492 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 uopt->partition = 0xFFFF;
494 uopt->session = 0xFFFFFFFF;
495 uopt->lastblock = 0;
496 uopt->anchor = 0;
497 uopt->volume = 0xFFFFFFFF;
498 uopt->rootdir = 0xFFFFFFFF;
499 uopt->fileset = 0xFFFFFFFF;
500 uopt->nls_map = NULL;
501
502 if (!options)
503 return 1;
504
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 substring_t args[MAX_OPT_ARGS];
507 int token;
508 if (!*p)
509 continue;
510
511 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700512 switch (token) {
513 case Opt_novrs:
514 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100515 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700516 case Opt_bs:
517 if (match_int(&args[0], &option))
518 return 0;
519 uopt->blocksize = option;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100520 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700521 break;
522 case Opt_unhide:
523 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
524 break;
525 case Opt_undelete:
526 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
527 break;
528 case Opt_noadinicb:
529 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
530 break;
531 case Opt_adinicb:
532 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
533 break;
534 case Opt_shortad:
535 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
536 break;
537 case Opt_longad:
538 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
539 break;
540 case Opt_gid:
541 if (match_int(args, &option))
542 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800543 uopt->gid = make_kgid(current_user_ns(), option);
544 if (!gid_valid(uopt->gid))
545 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700546 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700547 break;
548 case Opt_uid:
549 if (match_int(args, &option))
550 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800551 uopt->uid = make_kuid(current_user_ns(), option);
552 if (!uid_valid(uopt->uid))
553 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700554 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700555 break;
556 case Opt_umask:
557 if (match_octal(args, &option))
558 return 0;
559 uopt->umask = option;
560 break;
561 case Opt_nostrict:
562 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
563 break;
564 case Opt_session:
565 if (match_int(args, &option))
566 return 0;
567 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800568 if (!remount)
569 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700570 break;
571 case Opt_lastblock:
572 if (match_int(args, &option))
573 return 0;
574 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800575 if (!remount)
576 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700577 break;
578 case Opt_anchor:
579 if (match_int(args, &option))
580 return 0;
581 uopt->anchor = option;
582 break;
583 case Opt_volume:
584 if (match_int(args, &option))
585 return 0;
586 uopt->volume = option;
587 break;
588 case Opt_partition:
589 if (match_int(args, &option))
590 return 0;
591 uopt->partition = option;
592 break;
593 case Opt_fileset:
594 if (match_int(args, &option))
595 return 0;
596 uopt->fileset = option;
597 break;
598 case Opt_rootdir:
599 if (match_int(args, &option))
600 return 0;
601 uopt->rootdir = option;
602 break;
603 case Opt_utf8:
604 uopt->flags |= (1 << UDF_FLAG_UTF8);
605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607 case Opt_iocharset:
608 uopt->nls_map = load_nls(args[0].from);
609 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700612 case Opt_uignore:
613 uopt->flags |= (1 << UDF_FLAG_UID_IGNORE);
614 break;
615 case Opt_uforget:
616 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
617 break;
618 case Opt_gignore:
619 uopt->flags |= (1 << UDF_FLAG_GID_IGNORE);
620 break;
621 case Opt_gforget:
622 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
623 break;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100624 case Opt_fmode:
625 if (match_octal(args, &option))
626 return 0;
627 uopt->fmode = option & 0777;
628 break;
629 case Opt_dmode:
630 if (match_octal(args, &option))
631 return 0;
632 uopt->dmode = option & 0777;
633 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700634 default:
Joe Perches78ace702011-10-10 01:08:05 -0700635 pr_err("bad mount option \"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return 0;
637 }
638 }
639 return 1;
640}
641
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700642static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800645 struct udf_sb_info *sbi = UDF_SB(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400646 int error = 0;
Jan Kara69d75672013-09-12 22:00:15 +0200647 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Theodore Ts'o02b99842014-03-13 10:14:33 -0400649 sync_filesystem(sb);
Jan Kara69d75672013-09-12 22:00:15 +0200650 if (lvidiu) {
651 int write_rev = le16_to_cpu(lvidiu->minUDFWriteRev);
Jan Karae729eac2013-07-25 16:15:16 +0200652 if (write_rev > UDF_MAX_WRITE_VERSION && !(*flags & MS_RDONLY))
653 return -EACCES;
654 }
655
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800656 uopt.flags = sbi->s_flags;
657 uopt.uid = sbi->s_uid;
658 uopt.gid = sbi->s_gid;
659 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100660 uopt.fmode = sbi->s_fmode;
661 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Miklos Szeredi6da80892008-02-08 04:21:50 -0800663 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -EINVAL;
665
Jan Karac03cad22010-10-20 22:17:28 +0200666 write_lock(&sbi->s_cred_lock);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800667 sbi->s_flags = uopt.flags;
668 sbi->s_uid = uopt.uid;
669 sbi->s_gid = uopt.gid;
670 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100671 sbi->s_fmode = uopt.fmode;
672 sbi->s_dmode = uopt.dmode;
Jan Karac03cad22010-10-20 22:17:28 +0200673 write_unlock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400675 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
676 goto out_unlock;
677
Jan Kara36350462010-05-19 16:28:56 +0200678 if (*flags & MS_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 udf_close_lvid(sb);
Jan Kara36350462010-05-19 16:28:56 +0200680 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 udf_open_lvid(sb);
682
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400683out_unlock:
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400684 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Jan Kara40346002009-03-19 16:21:38 +0100687/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
688/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
689static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
691 struct volStructDesc *vsd = NULL;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200692 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 int sectorsize;
694 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700695 int nsr02 = 0;
696 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800697 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800699 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (sb->s_blocksize < sizeof(struct volStructDesc))
701 sectorsize = sizeof(struct volStructDesc);
702 else
703 sectorsize = sb->s_blocksize;
704
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800705 sector += (sbi->s_session << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 udf_debug("Starting at sector %u (%ld byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200708 (unsigned int)(sector >> sb->s_blocksize_bits),
709 sb->s_blocksize);
Peter A. Felvegi44499602013-10-18 20:07:44 +0200710 /* Process the sequence (if applicable). The hard limit on the sector
711 * offset is arbitrary, hopefully large enough so that all valid UDF
712 * filesystems will be recognised. There is no mention of an upper
713 * bound to the size of the volume recognition area in the standard.
714 * The limit will prevent the code to read all the sectors of a
715 * specially crafted image (like a bluray disc full of CD001 sectors),
716 * potentially causing minutes or even hours of uninterruptible I/O
717 * activity. This actually happened with uninitialised SSD partitions
718 * (all 0xFF) before the check for the limit and all valid IDs were
719 * added */
720 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
721 sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 /* Read a block */
723 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
724 if (!bh)
725 break;
726
727 /* Look for ISO descriptors */
728 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800729 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Peter A. Felvegi44499602013-10-18 20:07:44 +0200731 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800732 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733 switch (vsd->structType) {
734 case 0:
735 udf_debug("ISO9660 Boot Record found\n");
736 break;
737 case 1:
Joe Perchesa983f362011-10-10 01:08:07 -0700738 udf_debug("ISO9660 Primary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 break;
740 case 2:
Joe Perchesa983f362011-10-10 01:08:07 -0700741 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 break;
743 case 3:
Joe Perchesa983f362011-10-10 01:08:07 -0700744 udf_debug("ISO9660 Volume Partition Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700745 break;
746 case 255:
Joe Perchesa983f362011-10-10 01:08:07 -0700747 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700748 break;
749 default:
750 udf_debug("ISO9660 VRS (%u) found\n",
751 vsd->structType);
752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800754 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
755 VSD_STD_ID_LEN))
756 ; /* nothing */
757 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
758 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700759 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800761 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
762 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800764 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
765 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 nsr03 = sector;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200767 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
768 VSD_STD_ID_LEN))
769 ; /* nothing */
770 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
771 VSD_STD_ID_LEN))
772 ; /* nothing */
773 else {
774 /* invalid id : end of volume recognition area */
775 brelse(bh);
776 break;
777 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700778 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
781 if (nsr03)
782 return nsr03;
783 else if (nsr02)
784 return nsr02;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200785 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
786 VSD_FIRST_SECTOR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return -1;
788 else
789 return 0;
790}
791
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800792static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200793 struct kernel_lb_addr *fileset,
794 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct buffer_head *bh = NULL;
797 long lastblock;
798 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800799 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700802 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200803 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700805 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700807 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700808 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 1;
810 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800814 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800815 if (!bh) {
816 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200817 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700819/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700821
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800822 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700823 (newfileset.partitionReferenceNum != 0xFFFF &&
824 fileset->logicalBlockNum == 0xFFFFFFFF &&
825 fileset->partitionReferenceNum == 0xFFFF);
826 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800827 lastblock = sbi->s_partmaps
828 [newfileset.partitionReferenceNum]
829 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 newfileset.logicalBlockNum = 0;
831
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700832 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200833 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800834 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700835 if (!bh) {
836 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 continue;
838 }
839
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700840 switch (ident) {
841 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700842 {
843 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800844 sp = (struct spaceBitmapDesc *)
845 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700846 newfileset.logicalBlockNum += 1 +
847 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800848 sizeof(struct spaceBitmapDesc)
849 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700850 brelse(bh);
851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700853 case TAG_IDENT_FSD:
854 *fileset = newfileset;
855 break;
856 default:
857 newfileset.logicalBlockNum++;
858 brelse(bh);
859 bh = NULL;
860 break;
861 }
862 } while (newfileset.logicalBlockNum < lastblock &&
863 fileset->logicalBlockNum == 0xFFFFFFFF &&
864 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867
868 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700869 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 udf_debug("Fileset at block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700871 fileset->logicalBlockNum,
872 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800874 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700876 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return 0;
878 }
879 return 1;
880}
881
Jan Karad759bfa42013-07-25 19:10:59 +0200882/*
883 * Load primary Volume Descriptor Sequence
884 *
885 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
886 * should be tried.
887 */
Jan Karac0eb31e2008-04-01 16:50:35 +0200888static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct primaryVolDesc *pvoldesc;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100891 struct ustr *instr, *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200892 struct buffer_head *bh;
893 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +0200894 int ret = -ENOMEM;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100895
896 instr = kmalloc(sizeof(struct ustr), GFP_NOFS);
897 if (!instr)
Jan Karad759bfa42013-07-25 19:10:59 +0200898 return -ENOMEM;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100899
900 outstr = kmalloc(sizeof(struct ustr), GFP_NOFS);
901 if (!outstr)
902 goto out1;
Jan Karac0eb31e2008-04-01 16:50:35 +0200903
904 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karad759bfa42013-07-25 19:10:59 +0200905 if (!bh) {
906 ret = -EAGAIN;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100907 goto out2;
Jan Karad759bfa42013-07-25 19:10:59 +0200908 }
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100909
Jan Karad759bfa42013-07-25 19:10:59 +0200910 if (ident != TAG_IDENT_PVD) {
911 ret = -EIO;
912 goto out_bh;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 pvoldesc = (struct primaryVolDesc *)bh->b_data;
916
Marcin Slusarz56774802008-02-10 11:25:31 +0100917 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
918 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100919#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200920 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
Joe Perchesa983f362011-10-10 01:08:07 -0700921 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100922 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
923 ts->minute, le16_to_cpu(ts->typeAndTimezone));
924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100927 if (!udf_build_ustr(instr, pvoldesc->volIdent, 32))
928 if (udf_CS0toUTF8(outstr, instr)) {
929 strncpy(UDF_SB(sb)->s_volume_ident, outstr->u_name,
930 outstr->u_len > 31 ? 31 : outstr->u_len);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800931 udf_debug("volIdent[] = '%s'\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700932 UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100935 if (!udf_build_ustr(instr, pvoldesc->volSetIdent, 128))
936 if (udf_CS0toUTF8(outstr, instr))
937 udf_debug("volSetIdent[] = '%s'\n", outstr->u_name);
Jan Karac0eb31e2008-04-01 16:50:35 +0200938
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100939 ret = 0;
Jan Karad759bfa42013-07-25 19:10:59 +0200940out_bh:
941 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100942out2:
943 kfree(outstr);
944out1:
945 kfree(instr);
946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Namjae Jeon3080a742011-10-23 19:28:32 +0900949struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
950 u32 meta_file_loc, u32 partition_num)
951{
952 struct kernel_lb_addr addr;
953 struct inode *metadata_fe;
954
955 addr.logicalBlockNum = meta_file_loc;
956 addr.partitionReferenceNum = partition_num;
957
958 metadata_fe = udf_iget(sb, &addr);
959
960 if (metadata_fe == NULL)
961 udf_warn(sb, "metadata inode efe not found\n");
962 else if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
963 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
964 iput(metadata_fe);
965 metadata_fe = NULL;
966 }
967
968 return metadata_fe;
969}
970
Jan Karabfb257a2008-04-08 20:37:21 +0200971static int udf_load_metadata_files(struct super_block *sb, int partition)
972{
973 struct udf_sb_info *sbi = UDF_SB(sb);
974 struct udf_part_map *map;
975 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200976 struct kernel_lb_addr addr;
Jan Karabfb257a2008-04-08 20:37:21 +0200977
978 map = &sbi->s_partmaps[partition];
979 mdata = &map->s_type_specific.s_metadata;
980
981 /* metadata address */
Jan Karabfb257a2008-04-08 20:37:21 +0200982 udf_debug("Metadata file location: block = %d part = %d\n",
Namjae Jeon3080a742011-10-23 19:28:32 +0900983 mdata->s_meta_file_loc, map->s_partition_num);
Jan Karabfb257a2008-04-08 20:37:21 +0200984
Namjae Jeon3080a742011-10-23 19:28:32 +0900985 mdata->s_metadata_fe = udf_find_metadata_inode_efe(sb,
986 mdata->s_meta_file_loc, map->s_partition_num);
Jan Karabfb257a2008-04-08 20:37:21 +0200987
988 if (mdata->s_metadata_fe == NULL) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900989 /* mirror file entry */
990 udf_debug("Mirror metadata file location: block = %d part = %d\n",
991 mdata->s_mirror_file_loc, map->s_partition_num);
Jan Karabfb257a2008-04-08 20:37:21 +0200992
Namjae Jeon3080a742011-10-23 19:28:32 +0900993 mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
994 mdata->s_mirror_file_loc, map->s_partition_num);
Jan Karabfb257a2008-04-08 20:37:21 +0200995
Namjae Jeon3080a742011-10-23 19:28:32 +0900996 if (mdata->s_mirror_fe == NULL) {
997 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
Jan Karad759bfa42013-07-25 19:10:59 +0200998 return -EIO;
Namjae Jeon3080a742011-10-23 19:28:32 +0900999 }
Jan Karabfb257a2008-04-08 20:37:21 +02001000 }
1001
1002 /*
1003 * bitmap file entry
1004 * Note:
1005 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1006 */
1007 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1008 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
1009 addr.partitionReferenceNum = map->s_partition_num;
1010
1011 udf_debug("Bitmap file location: block = %d part = %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001012 addr.logicalBlockNum, addr.partitionReferenceNum);
Jan Karabfb257a2008-04-08 20:37:21 +02001013
Pekka Enberg97e961f2008-10-15 12:29:03 +02001014 mdata->s_bitmap_fe = udf_iget(sb, &addr);
Jan Karabfb257a2008-04-08 20:37:21 +02001015 if (mdata->s_bitmap_fe == NULL) {
1016 if (sb->s_flags & MS_RDONLY)
Joe Perchesa40ecd72011-10-10 01:08:04 -07001017 udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n");
Jan Karabfb257a2008-04-08 20:37:21 +02001018 else {
Joe Perches8076c362011-10-10 01:08:03 -07001019 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001020 return -EIO;
Jan Karabfb257a2008-04-08 20:37:21 +02001021 }
1022 }
1023 }
1024
1025 udf_debug("udf_load_metadata_files Ok\n");
Jan Karabfb257a2008-04-08 20:37:21 +02001026 return 0;
Jan Karabfb257a2008-04-08 20:37:21 +02001027}
1028
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001029static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001030 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct fileSetDesc *fset;
1033
1034 fset = (struct fileSetDesc *)bh->b_data;
1035
1036 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1037
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001038 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001040 udf_debug("Rootdir at block=%d, partition=%d\n",
1041 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001044int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1045{
1046 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001047 return DIV_ROUND_UP(map->s_partition_len +
1048 (sizeof(struct spaceBitmapDesc) << 3),
1049 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001050}
1051
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001052static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1053{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001054 struct udf_bitmap *bitmap;
1055 int nr_groups;
1056 int size;
1057
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001058 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001059 size = sizeof(struct udf_bitmap) +
1060 (sizeof(struct buffer_head *) * nr_groups);
1061
1062 if (size <= PAGE_SIZE)
Joe Perchesed2ae6f2010-11-04 20:08:04 -07001063 bitmap = kzalloc(size, GFP_KERNEL);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001064 else
Joe Perchesed2ae6f2010-11-04 20:08:04 -07001065 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001066
Joe Perches75b09e02012-01-31 14:42:10 -08001067 if (bitmap == NULL)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001068 return NULL;
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001069
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001070 bitmap->s_nr_groups = nr_groups;
1071 return bitmap;
1072}
1073
Jan Kara3fb38df2008-04-02 12:26:36 +02001074static int udf_fill_partdesc_info(struct super_block *sb,
1075 struct partitionDesc *p, int p_index)
1076{
1077 struct udf_part_map *map;
1078 struct udf_sb_info *sbi = UDF_SB(sb);
1079 struct partitionHeaderDesc *phd;
1080
1081 map = &sbi->s_partmaps[p_index];
1082
1083 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1084 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1085
1086 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1087 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1088 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1089 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1090 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1091 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1092 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1093 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1094
Joe Perchesa983f362011-10-10 01:08:07 -07001095 udf_debug("Partition (%d type %x) starts at physical %d, block length %d\n",
1096 p_index, map->s_partition_type,
1097 map->s_partition_root, map->s_partition_len);
Jan Kara3fb38df2008-04-02 12:26:36 +02001098
1099 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1100 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1101 return 0;
1102
1103 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1104 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001105 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001106 .logicalBlockNum = le32_to_cpu(
1107 phd->unallocSpaceTable.extPosition),
1108 .partitionReferenceNum = p_index,
1109 };
1110
Pekka Enberg97e961f2008-10-15 12:29:03 +02001111 map->s_uspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001112 if (!map->s_uspace.s_table) {
1113 udf_debug("cannot load unallocSpaceTable (part %d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001114 p_index);
Jan Karad759bfa42013-07-25 19:10:59 +02001115 return -EIO;
Jan Kara3fb38df2008-04-02 12:26:36 +02001116 }
1117 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
1118 udf_debug("unallocSpaceTable (part %d) @ %ld\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001119 p_index, map->s_uspace.s_table->i_ino);
Jan Kara3fb38df2008-04-02 12:26:36 +02001120 }
1121
1122 if (phd->unallocSpaceBitmap.extLength) {
1123 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1124 if (!bitmap)
Jan Karad759bfa42013-07-25 19:10:59 +02001125 return -ENOMEM;
Jan Kara3fb38df2008-04-02 12:26:36 +02001126 map->s_uspace.s_bitmap = bitmap;
Jan Kara3fb38df2008-04-02 12:26:36 +02001127 bitmap->s_extPosition = le32_to_cpu(
1128 phd->unallocSpaceBitmap.extPosition);
1129 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Joe Perchesa983f362011-10-10 01:08:07 -07001130 udf_debug("unallocSpaceBitmap (part %d) @ %d\n",
1131 p_index, bitmap->s_extPosition);
Jan Kara3fb38df2008-04-02 12:26:36 +02001132 }
1133
1134 if (phd->partitionIntegrityTable.extLength)
1135 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1136
1137 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001138 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001139 .logicalBlockNum = le32_to_cpu(
1140 phd->freedSpaceTable.extPosition),
1141 .partitionReferenceNum = p_index,
1142 };
1143
Pekka Enberg97e961f2008-10-15 12:29:03 +02001144 map->s_fspace.s_table = udf_iget(sb, &loc);
Jan Kara3fb38df2008-04-02 12:26:36 +02001145 if (!map->s_fspace.s_table) {
1146 udf_debug("cannot load freedSpaceTable (part %d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001147 p_index);
Jan Karad759bfa42013-07-25 19:10:59 +02001148 return -EIO;
Jan Kara3fb38df2008-04-02 12:26:36 +02001149 }
1150
1151 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
1152 udf_debug("freedSpaceTable (part %d) @ %ld\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001153 p_index, map->s_fspace.s_table->i_ino);
Jan Kara3fb38df2008-04-02 12:26:36 +02001154 }
1155
1156 if (phd->freedSpaceBitmap.extLength) {
1157 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1158 if (!bitmap)
Jan Karad759bfa42013-07-25 19:10:59 +02001159 return -ENOMEM;
Jan Kara3fb38df2008-04-02 12:26:36 +02001160 map->s_fspace.s_bitmap = bitmap;
Jan Kara3fb38df2008-04-02 12:26:36 +02001161 bitmap->s_extPosition = le32_to_cpu(
1162 phd->freedSpaceBitmap.extPosition);
1163 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Joe Perchesa983f362011-10-10 01:08:07 -07001164 udf_debug("freedSpaceBitmap (part %d) @ %d\n",
1165 p_index, bitmap->s_extPosition);
Jan Kara3fb38df2008-04-02 12:26:36 +02001166 }
1167 return 0;
1168}
1169
Jan Karae971b0b2009-11-30 19:47:55 +01001170static void udf_find_vat_block(struct super_block *sb, int p_index,
1171 int type1_index, sector_t start_block)
1172{
1173 struct udf_sb_info *sbi = UDF_SB(sb);
1174 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1175 sector_t vat_block;
1176 struct kernel_lb_addr ino;
1177
1178 /*
1179 * VAT file entry is in the last recorded block. Some broken disks have
1180 * it a few blocks before so try a bit harder...
1181 */
1182 ino.partitionReferenceNum = type1_index;
1183 for (vat_block = start_block;
1184 vat_block >= map->s_partition_root &&
1185 vat_block >= start_block - 3 &&
1186 !sbi->s_vat_inode; vat_block--) {
1187 ino.logicalBlockNum = vat_block - map->s_partition_root;
1188 sbi->s_vat_inode = udf_iget(sb, &ino);
1189 }
1190}
1191
Jan Kara38b74a52008-04-02 16:01:35 +02001192static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1193{
1194 struct udf_sb_info *sbi = UDF_SB(sb);
1195 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Jan Karafa5e0812008-04-08 02:08:53 +02001196 struct buffer_head *bh = NULL;
1197 struct udf_inode_info *vati;
1198 uint32_t pos;
1199 struct virtualAllocationTable20 *vat20;
Jan Kara4bf17af2009-07-14 19:30:23 +02001200 sector_t blocks = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
Jan Kara38b74a52008-04-02 16:01:35 +02001201
Jan Karae971b0b2009-11-30 19:47:55 +01001202 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
Jan Kara4bf17af2009-07-14 19:30:23 +02001203 if (!sbi->s_vat_inode &&
1204 sbi->s_last_block != blocks - 1) {
Joe Perches78ace702011-10-10 01:08:05 -07001205 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1206 (unsigned long)sbi->s_last_block,
1207 (unsigned long)blocks - 1);
Jan Karae971b0b2009-11-30 19:47:55 +01001208 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
Jan Kara4bf17af2009-07-14 19:30:23 +02001209 }
Jan Kara38b74a52008-04-02 16:01:35 +02001210 if (!sbi->s_vat_inode)
Jan Karad759bfa42013-07-25 19:10:59 +02001211 return -EIO;
Jan Kara38b74a52008-04-02 16:01:35 +02001212
1213 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001214 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001215 map->s_type_specific.s_virtual.s_num_entries =
1216 (sbi->s_vat_inode->i_size - 36) >> 2;
1217 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001218 vati = UDF_I(sbi->s_vat_inode);
1219 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1220 pos = udf_block_map(sbi->s_vat_inode, 0);
1221 bh = sb_bread(sb, pos);
1222 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001223 return -EIO;
Jan Karafa5e0812008-04-08 02:08:53 +02001224 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1225 } else {
1226 vat20 = (struct virtualAllocationTable20 *)
1227 vati->i_ext.i_data;
1228 }
Jan Kara38b74a52008-04-02 16:01:35 +02001229
Jan Kara38b74a52008-04-02 16:01:35 +02001230 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001231 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001232 map->s_type_specific.s_virtual.s_num_entries =
1233 (sbi->s_vat_inode->i_size -
1234 map->s_type_specific.s_virtual.
1235 s_start_offset) >> 2;
1236 brelse(bh);
1237 }
1238 return 0;
1239}
1240
Jan Karad759bfa42013-07-25 19:10:59 +02001241/*
1242 * Load partition descriptor block
1243 *
1244 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1245 * sequence.
1246 */
Jan Karac0eb31e2008-04-01 16:50:35 +02001247static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Jan Karac0eb31e2008-04-01 16:50:35 +02001249 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001250 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001251 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001252 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001253 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001254 uint16_t partitionNumber;
1255 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +02001256 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jan Karac0eb31e2008-04-01 16:50:35 +02001258 bh = udf_read_tagged(sb, block, block, &ident);
1259 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001260 return -EAGAIN;
1261 if (ident != TAG_IDENT_PD) {
1262 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001263 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001264 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001265
1266 p = (struct partitionDesc *)bh->b_data;
1267 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001268
Jan Karabfb257a2008-04-08 20:37:21 +02001269 /* First scan for TYPE1, SPARABLE and METADATA partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001270 for (i = 0; i < sbi->s_partitions; i++) {
1271 map = &sbi->s_partmaps[i];
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001272 udf_debug("Searching map: (%d == %d)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001273 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001274 if (map->s_partition_num == partitionNumber &&
1275 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1276 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001278 }
1279
Jan Kara38b74a52008-04-02 16:01:35 +02001280 if (i >= sbi->s_partitions) {
Marcin Slusarz165923f2008-02-10 11:33:08 +01001281 udf_debug("Partition (%d) not found in partition map\n",
1282 partitionNumber);
Jan Karad759bfa42013-07-25 19:10:59 +02001283 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001284 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001285 }
1286
Jan Kara3fb38df2008-04-02 12:26:36 +02001287 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001288 if (ret < 0)
1289 goto out_bh;
Jan Kara38b74a52008-04-02 16:01:35 +02001290
1291 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001292 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1293 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001294 */
1295 type1_idx = i;
Peter A. Felvegi44499602013-10-18 20:07:44 +02001296#ifdef UDFFS_DEBUG
1297 map = NULL; /* supress 'maybe used uninitialized' warning */
1298#endif
Jan Kara38b74a52008-04-02 16:01:35 +02001299 for (i = 0; i < sbi->s_partitions; i++) {
1300 map = &sbi->s_partmaps[i];
1301
1302 if (map->s_partition_num == partitionNumber &&
1303 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001304 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1305 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001306 break;
1307 }
1308
Jan Karad759bfa42013-07-25 19:10:59 +02001309 if (i >= sbi->s_partitions) {
1310 ret = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001311 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001312 }
Jan Kara38b74a52008-04-02 16:01:35 +02001313
1314 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001315 if (ret < 0)
Jan Kara38b74a52008-04-02 16:01:35 +02001316 goto out_bh;
1317
Jan Karabfb257a2008-04-08 20:37:21 +02001318 if (map->s_partition_type == UDF_METADATA_MAP25) {
1319 ret = udf_load_metadata_files(sb, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001320 if (ret < 0) {
Joe Perches78ace702011-10-10 01:08:05 -07001321 udf_err(sb, "error loading MetaData partition map %d\n",
1322 i);
Jan Karabfb257a2008-04-08 20:37:21 +02001323 goto out_bh;
1324 }
1325 } else {
Jan Karae729eac2013-07-25 16:15:16 +02001326 /*
1327 * If we have a partition with virtual map, we don't handle
1328 * writing to it (we overwrite blocks instead of relocating
1329 * them).
1330 */
1331 if (!(sb->s_flags & MS_RDONLY)) {
1332 ret = -EACCES;
1333 goto out_bh;
1334 }
Jan Karabfb257a2008-04-08 20:37:21 +02001335 ret = udf_load_vat(sb, i, type1_idx);
Jan Karad759bfa42013-07-25 19:10:59 +02001336 if (ret < 0)
Jan Karabfb257a2008-04-08 20:37:21 +02001337 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001338 }
Jan Karad759bfa42013-07-25 19:10:59 +02001339 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001340out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001341 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001342 brelse(bh);
1343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Jan Kara1df2ae32012-06-27 21:23:07 +02001346static int udf_load_sparable_map(struct super_block *sb,
1347 struct udf_part_map *map,
1348 struct sparablePartitionMap *spm)
1349{
1350 uint32_t loc;
1351 uint16_t ident;
1352 struct sparingTable *st;
1353 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1354 int i;
1355 struct buffer_head *bh;
1356
1357 map->s_partition_type = UDF_SPARABLE_MAP15;
1358 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1359 if (!is_power_of_2(sdata->s_packet_len)) {
1360 udf_err(sb, "error loading logical volume descriptor: "
1361 "Invalid packet length %u\n",
1362 (unsigned)sdata->s_packet_len);
1363 return -EIO;
1364 }
1365 if (spm->numSparingTables > 4) {
1366 udf_err(sb, "error loading logical volume descriptor: "
1367 "Too many sparing tables (%d)\n",
1368 (int)spm->numSparingTables);
1369 return -EIO;
1370 }
1371
1372 for (i = 0; i < spm->numSparingTables; i++) {
1373 loc = le32_to_cpu(spm->locSparingTable[i]);
1374 bh = udf_read_tagged(sb, loc, loc, &ident);
1375 if (!bh)
1376 continue;
1377
1378 st = (struct sparingTable *)bh->b_data;
1379 if (ident != 0 ||
1380 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1381 strlen(UDF_ID_SPARING)) ||
1382 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1383 sb->s_blocksize) {
1384 brelse(bh);
1385 continue;
1386 }
1387
1388 sdata->s_spar_map[i] = bh;
1389 }
1390 map->s_partition_func = udf_get_pblock_spar15;
1391 return 0;
1392}
1393
Jan Karac0eb31e2008-04-01 16:50:35 +02001394static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001395 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct logicalVolDesc *lvd;
Jan Kara1df2ae32012-06-27 21:23:07 +02001398 int i, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001400 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001401 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001402 uint16_t ident;
1403 struct buffer_head *bh;
Jan Karaadee11b2012-06-27 20:20:22 +02001404 unsigned int table_len;
Jan Karad759bfa42013-07-25 19:10:59 +02001405 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Jan Karac0eb31e2008-04-01 16:50:35 +02001407 bh = udf_read_tagged(sb, block, block, &ident);
1408 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001409 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001410 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 lvd = (struct logicalVolDesc *)bh->b_data;
Jan Karaadee11b2012-06-27 20:20:22 +02001412 table_len = le32_to_cpu(lvd->mapTableLength);
Jan Kara57b96552012-07-10 17:58:04 +02001413 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
Jan Karaadee11b2012-06-27 20:20:22 +02001414 udf_err(sb, "error loading logical volume descriptor: "
1415 "Partition table too long (%u > %lu)\n", table_len,
1416 sb->s_blocksize - sizeof(*lvd));
Jan Karad759bfa42013-07-25 19:10:59 +02001417 ret = -EIO;
Jan Karaadee11b2012-06-27 20:20:22 +02001418 goto out_bh;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jan Karacb14d342012-06-27 20:08:44 +02001421 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1422 if (ret)
Jan Karac0eb31e2008-04-01 16:50:35 +02001423 goto out_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001425 for (i = 0, offset = 0;
Jan Karaadee11b2012-06-27 20:20:22 +02001426 i < sbi->s_partitions && offset < table_len;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001427 i++, offset += gpm->partitionMapLength) {
1428 struct udf_part_map *map = &sbi->s_partmaps[i];
1429 gpm = (struct genericPartitionMap *)
1430 &(lvd->partitionMaps[offset]);
1431 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001432 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001433 struct genericPartitionMap1 *gpm1 =
1434 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001435 map->s_partition_type = UDF_TYPE1_MAP15;
1436 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1437 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1438 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001439 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001440 struct udfPartitionMap2 *upm2 =
1441 (struct udfPartitionMap2 *)gpm;
1442 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1443 strlen(UDF_ID_VIRTUAL))) {
1444 u16 suf =
1445 le16_to_cpu(((__le16 *)upm2->partIdent.
1446 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001447 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001448 map->s_partition_type =
1449 UDF_VIRTUAL_MAP15;
1450 map->s_partition_func =
1451 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001452 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001453 map->s_partition_type =
1454 UDF_VIRTUAL_MAP20;
1455 map->s_partition_func =
1456 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001458 } else if (!strncmp(upm2->partIdent.ident,
1459 UDF_ID_SPARABLE,
1460 strlen(UDF_ID_SPARABLE))) {
Jan Karad759bfa42013-07-25 19:10:59 +02001461 ret = udf_load_sparable_map(sb, map,
1462 (struct sparablePartitionMap *)gpm);
1463 if (ret < 0)
Jan Kara1df2ae32012-06-27 21:23:07 +02001464 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001465 } else if (!strncmp(upm2->partIdent.ident,
1466 UDF_ID_METADATA,
1467 strlen(UDF_ID_METADATA))) {
1468 struct udf_meta_data *mdata =
1469 &map->s_type_specific.s_metadata;
1470 struct metadataPartitionMap *mdm =
1471 (struct metadataPartitionMap *)
1472 &(lvd->partitionMaps[offset]);
Joe Perchesa983f362011-10-10 01:08:07 -07001473 udf_debug("Parsing Logical vol part %d type %d id=%s\n",
1474 i, type, UDF_ID_METADATA);
Jan Karabfb257a2008-04-08 20:37:21 +02001475
1476 map->s_partition_type = UDF_METADATA_MAP25;
1477 map->s_partition_func = udf_get_pblock_meta25;
1478
1479 mdata->s_meta_file_loc =
1480 le32_to_cpu(mdm->metadataFileLoc);
1481 mdata->s_mirror_file_loc =
1482 le32_to_cpu(mdm->metadataMirrorFileLoc);
1483 mdata->s_bitmap_file_loc =
1484 le32_to_cpu(mdm->metadataBitmapFileLoc);
1485 mdata->s_alloc_unit_size =
1486 le32_to_cpu(mdm->allocUnitSize);
1487 mdata->s_align_unit_size =
1488 le16_to_cpu(mdm->alignUnitSize);
Jan Karaed47a7d2011-10-24 16:47:48 +02001489 if (mdm->flags & 0x01)
1490 mdata->s_flags |= MF_DUPLICATE_MD;
Jan Karabfb257a2008-04-08 20:37:21 +02001491
1492 udf_debug("Metadata Ident suffix=0x%x\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001493 le16_to_cpu(*(__le16 *)
1494 mdm->partIdent.identSuffix));
Jan Karabfb257a2008-04-08 20:37:21 +02001495 udf_debug("Metadata part num=%d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001496 le16_to_cpu(mdm->partitionNum));
Jan Karabfb257a2008-04-08 20:37:21 +02001497 udf_debug("Metadata part alloc unit size=%d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001498 le32_to_cpu(mdm->allocUnitSize));
Jan Karabfb257a2008-04-08 20:37:21 +02001499 udf_debug("Metadata file loc=%d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001500 le32_to_cpu(mdm->metadataFileLoc));
Jan Karabfb257a2008-04-08 20:37:21 +02001501 udf_debug("Mirror file loc=%d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001502 le32_to_cpu(mdm->metadataMirrorFileLoc));
Jan Karabfb257a2008-04-08 20:37:21 +02001503 udf_debug("Bitmap file loc=%d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001504 le32_to_cpu(mdm->metadataBitmapFileLoc));
Jan Karaed47a7d2011-10-24 16:47:48 +02001505 udf_debug("Flags: %d %d\n",
1506 mdata->s_flags, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001507 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001508 udf_debug("Unknown ident: %s\n",
1509 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 continue;
1511 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001512 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1513 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 udf_debug("Partition (%d:%d) type %d on volume %d\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001516 i, map->s_partition_num, type, map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001519 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001520 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 *fileset = lelb_to_cpu(la->extLocation);
Joe Perchesa983f362011-10-10 01:08:07 -07001523 udf_debug("FileSet found in LogicalVolDesc at block=%d, partition=%d\n",
1524 fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001525 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 if (lvd->integritySeqExt.extLength)
1528 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Jan Karad759bfa42013-07-25 19:10:59 +02001529 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001530out_bh:
1531 brelse(bh);
1532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
1536 * udf_load_logicalvolint
1537 *
1538 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001539static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct buffer_head *bh = NULL;
1542 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001543 struct udf_sb_info *sbi = UDF_SB(sb);
1544 struct logicalVolIntegrityDesc *lvid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 while (loc.extLength > 0 &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001547 (bh = udf_read_tagged(sb, loc.extLocation,
1548 loc.extLocation, &ident)) &&
1549 ident == TAG_IDENT_LVID) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001550 sbi->s_lvid_bh = bh;
1551 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001552
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001553 if (lvid->nextIntegrityExt.extLength)
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001554 udf_load_logicalvolint(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001555 leea_to_cpu(lvid->nextIntegrityExt));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001556
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001557 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001558 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 loc.extLength -= sb->s_blocksize;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001560 loc.extLocation++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001562 if (sbi->s_lvid_bh != bh)
Jan Kara3bf25cb2007-05-08 00:35:16 -07001563 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
Jan Karad759bfa42013-07-25 19:10:59 +02001567 * Process a main/reserve volume descriptor sequence.
1568 * @block First block of first extent of the sequence.
1569 * @lastblock Lastblock of first extent of the sequence.
1570 * @fileset There we store extent containing root fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 *
Jan Karad759bfa42013-07-25 19:10:59 +02001572 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1573 * sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Jan Karad759bfa42013-07-25 19:10:59 +02001575static noinline int udf_process_sequence(
1576 struct super_block *sb,
1577 sector_t block, sector_t lastblock,
1578 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct buffer_head *bh = NULL;
1581 struct udf_vds_record vds[VDS_POS_LENGTH];
Marcin Slusarz4b111112008-02-08 04:20:36 -08001582 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct generic_desc *gd;
1584 struct volDescPtr *vdp;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001585 int done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 uint32_t vdsn;
1587 uint16_t ident;
1588 long next_s = 0, next_e = 0;
Jan Karad759bfa42013-07-25 19:10:59 +02001589 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 memset(vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1592
Jan Karac0eb31e2008-04-01 16:50:35 +02001593 /*
1594 * Read the main descriptor sequence and find which descriptors
1595 * are in it.
1596 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001597 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karac0eb31e2008-04-01 16:50:35 +02001600 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07001601 udf_err(sb,
1602 "Block %llu of volume descriptor sequence is corrupted or we could not read it\n",
1603 (unsigned long long)block);
Jan Karad759bfa42013-07-25 19:10:59 +02001604 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1608 gd = (struct generic_desc *)bh->b_data;
1609 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001610 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001611 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001612 curr = &vds[VDS_POS_PRIMARY_VOL_DESC];
1613 if (vdsn >= curr->volDescSeqNum) {
1614 curr->volDescSeqNum = vdsn;
1615 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001616 }
1617 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001618 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001619 curr = &vds[VDS_POS_VOL_DESC_PTR];
1620 if (vdsn >= curr->volDescSeqNum) {
1621 curr->volDescSeqNum = vdsn;
1622 curr->block = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001624 vdp = (struct volDescPtr *)bh->b_data;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001625 next_s = le32_to_cpu(
1626 vdp->nextVolDescSeqExt.extLocation);
1627 next_e = le32_to_cpu(
1628 vdp->nextVolDescSeqExt.extLength);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001629 next_e = next_e >> sb->s_blocksize_bits;
1630 next_e += next_s;
1631 }
1632 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001633 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001634 curr = &vds[VDS_POS_IMP_USE_VOL_DESC];
1635 if (vdsn >= curr->volDescSeqNum) {
1636 curr->volDescSeqNum = vdsn;
1637 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001638 }
1639 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001640 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001641 curr = &vds[VDS_POS_PARTITION_DESC];
1642 if (!curr->block)
1643 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001644 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001645 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001646 curr = &vds[VDS_POS_LOGICAL_VOL_DESC];
1647 if (vdsn >= curr->volDescSeqNum) {
1648 curr->volDescSeqNum = vdsn;
1649 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001650 }
1651 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001652 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Marcin Slusarz4b111112008-02-08 04:20:36 -08001653 curr = &vds[VDS_POS_UNALLOC_SPACE_DESC];
1654 if (vdsn >= curr->volDescSeqNum) {
1655 curr->volDescSeqNum = vdsn;
1656 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001657 }
1658 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001659 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001660 vds[VDS_POS_TERMINATING_DESC].block = block;
1661 if (next_e) {
1662 block = next_s;
1663 lastblock = next_e;
1664 next_s = next_e = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001665 } else
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001666 done = 1;
1667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001669 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001671 /*
1672 * Now read interesting descriptors again and process them
1673 * in a suitable order
1674 */
1675 if (!vds[VDS_POS_PRIMARY_VOL_DESC].block) {
Joe Perches78ace702011-10-10 01:08:05 -07001676 udf_err(sb, "Primary Volume Descriptor not found!\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001677 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001678 }
Jan Karad759bfa42013-07-25 19:10:59 +02001679 ret = udf_load_pvoldesc(sb, vds[VDS_POS_PRIMARY_VOL_DESC].block);
1680 if (ret < 0)
1681 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jan Karad759bfa42013-07-25 19:10:59 +02001683 if (vds[VDS_POS_LOGICAL_VOL_DESC].block) {
1684 ret = udf_load_logicalvol(sb,
1685 vds[VDS_POS_LOGICAL_VOL_DESC].block,
1686 fileset);
1687 if (ret < 0)
1688 return ret;
1689 }
Marcin Slusarz165923f2008-02-10 11:33:08 +01001690
Jan Karac0eb31e2008-04-01 16:50:35 +02001691 if (vds[VDS_POS_PARTITION_DESC].block) {
1692 /*
1693 * We rescan the whole descriptor sequence to find
1694 * partition descriptor blocks and process them.
1695 */
1696 for (block = vds[VDS_POS_PARTITION_DESC].block;
1697 block < vds[VDS_POS_TERMINATING_DESC].block;
Jan Karad759bfa42013-07-25 19:10:59 +02001698 block++) {
1699 ret = udf_load_partdesc(sb, block);
1700 if (ret < 0)
1701 return ret;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
1705 return 0;
1706}
1707
Jan Karad759bfa42013-07-25 19:10:59 +02001708/*
1709 * Load Volume Descriptor Sequence described by anchor in bh
1710 *
1711 * Returns <0 on error, 0 on success
1712 */
Jan Kara40346002009-03-19 16:21:38 +01001713static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1714 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Jan Kara40346002009-03-19 16:21:38 +01001716 struct anchorVolDescPtr *anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001717 sector_t main_s, main_e, reserve_s, reserve_e;
1718 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jan Kara40346002009-03-19 16:21:38 +01001720 anchor = (struct anchorVolDescPtr *)bh->b_data;
1721
1722 /* Locate the main sequence */
1723 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1724 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1725 main_e = main_e >> sb->s_blocksize_bits;
1726 main_e += main_s;
1727
1728 /* Locate the reserve sequence */
1729 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1730 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1731 reserve_e = reserve_e >> sb->s_blocksize_bits;
1732 reserve_e += reserve_s;
1733
1734 /* Process the main & reserve sequences */
1735 /* responsible for finding the PartitionDesc(s) */
Jan Karad759bfa42013-07-25 19:10:59 +02001736 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1737 if (ret != -EAGAIN)
1738 return ret;
Jan Karabff943a2012-06-27 22:27:05 +02001739 udf_sb_free_partitions(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001740 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1741 if (ret < 0) {
1742 udf_sb_free_partitions(sb);
1743 /* No sequence was OK, return -EIO */
1744 if (ret == -EAGAIN)
1745 ret = -EIO;
1746 }
1747 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
Jan Kara40346002009-03-19 16:21:38 +01001750/*
1751 * Check whether there is an anchor block in the given block and
1752 * load Volume Descriptor Sequence if so.
Jan Karad759bfa42013-07-25 19:10:59 +02001753 *
1754 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1755 * block
Jan Kara40346002009-03-19 16:21:38 +01001756 */
1757static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1758 struct kernel_lb_addr *fileset)
1759{
1760 struct buffer_head *bh;
1761 uint16_t ident;
1762 int ret;
1763
1764 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1765 udf_fixed_to_variable(block) >=
1766 sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits)
Jan Karad759bfa42013-07-25 19:10:59 +02001767 return -EAGAIN;
Jan Kara40346002009-03-19 16:21:38 +01001768
1769 bh = udf_read_tagged(sb, block, block, &ident);
1770 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001771 return -EAGAIN;
Jan Kara40346002009-03-19 16:21:38 +01001772 if (ident != TAG_IDENT_AVDP) {
1773 brelse(bh);
Jan Karad759bfa42013-07-25 19:10:59 +02001774 return -EAGAIN;
Jan Kara40346002009-03-19 16:21:38 +01001775 }
1776 ret = udf_load_sequence(sb, bh, fileset);
1777 brelse(bh);
1778 return ret;
1779}
1780
Jan Karad759bfa42013-07-25 19:10:59 +02001781/*
1782 * Search for an anchor volume descriptor pointer.
1783 *
1784 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1785 * of anchors.
1786 */
1787static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1788 struct kernel_lb_addr *fileset)
Jan Kara40346002009-03-19 16:21:38 +01001789{
1790 sector_t last[6];
1791 int i;
1792 struct udf_sb_info *sbi = UDF_SB(sb);
1793 int last_count = 0;
Jan Karad759bfa42013-07-25 19:10:59 +02001794 int ret;
Jan Kara40346002009-03-19 16:21:38 +01001795
1796 /* First try user provided anchor */
1797 if (sbi->s_anchor) {
Jan Karad759bfa42013-07-25 19:10:59 +02001798 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1799 if (ret != -EAGAIN)
1800 return ret;
Jan Kara40346002009-03-19 16:21:38 +01001801 }
1802 /*
1803 * according to spec, anchor is in either:
1804 * block 256
1805 * lastblock-256
1806 * lastblock
1807 * however, if the disc isn't closed, it could be 512.
1808 */
Jan Karad759bfa42013-07-25 19:10:59 +02001809 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1810 if (ret != -EAGAIN)
1811 return ret;
Jan Kara40346002009-03-19 16:21:38 +01001812 /*
1813 * The trouble is which block is the last one. Drives often misreport
1814 * this so we try various possibilities.
1815 */
Jan Karad759bfa42013-07-25 19:10:59 +02001816 last[last_count++] = *lastblock;
1817 if (*lastblock >= 1)
1818 last[last_count++] = *lastblock - 1;
1819 last[last_count++] = *lastblock + 1;
1820 if (*lastblock >= 2)
1821 last[last_count++] = *lastblock - 2;
1822 if (*lastblock >= 150)
1823 last[last_count++] = *lastblock - 150;
1824 if (*lastblock >= 152)
1825 last[last_count++] = *lastblock - 152;
Jan Kara40346002009-03-19 16:21:38 +01001826
1827 for (i = 0; i < last_count; i++) {
1828 if (last[i] >= sb->s_bdev->bd_inode->i_size >>
1829 sb->s_blocksize_bits)
1830 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001831 ret = udf_check_anchor_block(sb, last[i], fileset);
1832 if (ret != -EAGAIN) {
1833 if (!ret)
1834 *lastblock = last[i];
1835 return ret;
1836 }
Jan Kara40346002009-03-19 16:21:38 +01001837 if (last[i] < 256)
1838 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001839 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1840 if (ret != -EAGAIN) {
1841 if (!ret)
1842 *lastblock = last[i];
1843 return ret;
1844 }
Jan Kara40346002009-03-19 16:21:38 +01001845 }
1846
1847 /* Finally try block 512 in case media is open */
Jan Karad759bfa42013-07-25 19:10:59 +02001848 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
Jan Kara40346002009-03-19 16:21:38 +01001849}
1850
1851/*
1852 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1853 * area specified by it. The function expects sbi->s_lastblock to be the last
1854 * block on the media.
1855 *
Jan Karad759bfa42013-07-25 19:10:59 +02001856 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1857 * was not found.
Jan Kara40346002009-03-19 16:21:38 +01001858 */
1859static int udf_find_anchor(struct super_block *sb,
1860 struct kernel_lb_addr *fileset)
1861{
Jan Kara40346002009-03-19 16:21:38 +01001862 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001863 sector_t lastblock = sbi->s_last_block;
1864 int ret;
Jan Kara40346002009-03-19 16:21:38 +01001865
Jan Karad759bfa42013-07-25 19:10:59 +02001866 ret = udf_scan_anchors(sb, &lastblock, fileset);
1867 if (ret != -EAGAIN)
Jan Kara40346002009-03-19 16:21:38 +01001868 goto out;
1869
1870 /* No anchor found? Try VARCONV conversion of block numbers */
1871 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Karad759bfa42013-07-25 19:10:59 +02001872 lastblock = udf_variable_to_fixed(sbi->s_last_block);
Jan Kara40346002009-03-19 16:21:38 +01001873 /* Firstly, we try to not convert number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001874 ret = udf_scan_anchors(sb, &lastblock, fileset);
1875 if (ret != -EAGAIN)
Jan Kara40346002009-03-19 16:21:38 +01001876 goto out;
1877
Jan Karad759bfa42013-07-25 19:10:59 +02001878 lastblock = sbi->s_last_block;
Jan Kara40346002009-03-19 16:21:38 +01001879 /* Secondly, we try with converted number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001880 ret = udf_scan_anchors(sb, &lastblock, fileset);
1881 if (ret < 0) {
Jan Kara40346002009-03-19 16:21:38 +01001882 /* VARCONV didn't help. Clear it. */
1883 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara40346002009-03-19 16:21:38 +01001884 }
1885out:
Jan Karad759bfa42013-07-25 19:10:59 +02001886 if (ret == 0)
1887 sbi->s_last_block = lastblock;
1888 return ret;
Jan Kara40346002009-03-19 16:21:38 +01001889}
1890
1891/*
1892 * Check Volume Structure Descriptor, find Anchor block and load Volume
Jan Karad759bfa42013-07-25 19:10:59 +02001893 * Descriptor Sequence.
1894 *
1895 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1896 * block was not found.
Jan Kara40346002009-03-19 16:21:38 +01001897 */
1898static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1899 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001900{
1901 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara40346002009-03-19 16:21:38 +01001902 loff_t nsr_off;
Jan Karad759bfa42013-07-25 19:10:59 +02001903 int ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001904
1905 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1906 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001907 udf_warn(sb, "Bad block size\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001908 return -EINVAL;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001909 }
1910 sbi->s_last_block = uopt->lastblock;
Jan Kara40346002009-03-19 16:21:38 +01001911 if (!uopt->novrs) {
1912 /* Check that it is NSR02 compliant */
1913 nsr_off = udf_check_vsd(sb);
1914 if (!nsr_off) {
1915 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001916 udf_warn(sb, "No VRS found\n");
Jan Kara40346002009-03-19 16:21:38 +01001917 return 0;
1918 }
1919 if (nsr_off == -1)
Peter A. Felvegi44499602013-10-18 20:07:44 +02001920 udf_debug("Failed to read sector at offset %d. "
1921 "Assuming open disc. Skipping validity "
1922 "check\n", VSD_FIRST_SECTOR_OFFSET);
Jan Kara40346002009-03-19 16:21:38 +01001923 if (!sbi->s_last_block)
1924 sbi->s_last_block = udf_get_last_block(sb);
1925 } else {
1926 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001927 }
Jan Kara40346002009-03-19 16:21:38 +01001928
1929 /* Look for anchor block and load Volume Descriptor Sequence */
1930 sbi->s_anchor = uopt->anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001931 ret = udf_find_anchor(sb, fileset);
1932 if (ret < 0) {
1933 if (!silent && ret == -EAGAIN)
Joe Perches78ace702011-10-10 01:08:05 -07001934 udf_warn(sb, "No anchor found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001935 return ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001936 }
Jan Karad759bfa42013-07-25 19:10:59 +02001937 return 0;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001938}
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940static void udf_open_lvid(struct super_block *sb)
1941{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001942 struct udf_sb_info *sbi = UDF_SB(sb);
1943 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001944 struct logicalVolIntegrityDesc *lvid;
1945 struct logicalVolIntegrityDescImpUse *lvidiu;
Jan Kara146bca72009-03-16 18:27:37 +01001946
Marcin Slusarz165923f2008-02-10 11:33:08 +01001947 if (!bh)
1948 return;
Jan Kara69d75672013-09-12 22:00:15 +02001949 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1950 lvidiu = udf_sb_lvidiu(sb);
1951 if (!lvidiu)
1952 return;
Jan Kara949f4a72010-10-20 18:49:20 +02001953
1954 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001955 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1956 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1957 udf_time_to_disk_stamp(&lvid->recordingDateAndTime,
1958 CURRENT_TIME);
Jan Kara146bca72009-03-16 18:27:37 +01001959 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Marcin Slusarz165923f2008-02-10 11:33:08 +01001961 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001962 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001963 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001964
1965 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1966 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001967 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02001968 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c972013-01-17 22:11:38 +01001969 /* Make opening of filesystem visible on the media immediately */
1970 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973static void udf_close_lvid(struct super_block *sb)
1974{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001975 struct udf_sb_info *sbi = UDF_SB(sb);
1976 struct buffer_head *bh = sbi->s_lvid_bh;
1977 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001978 struct logicalVolIntegrityDescImpUse *lvidiu;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001979
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001980 if (!bh)
1981 return;
Jan Kara69d75672013-09-12 22:00:15 +02001982 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1983 lvidiu = udf_sb_lvidiu(sb);
1984 if (!lvidiu)
1985 return;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001986
Jan Kara949f4a72010-10-20 18:49:20 +02001987 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001988 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1989 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1990 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, CURRENT_TIME);
1991 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
1992 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
1993 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
1994 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
1995 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
1996 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
1997 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Marcin Slusarz165923f2008-02-10 11:33:08 +01001999 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002000 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02002001 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01002002
2003 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Jan Kara853a0c22011-12-23 11:53:07 +01002004 /*
2005 * We set buffer uptodate unconditionally here to avoid spurious
2006 * warnings from mark_buffer_dirty() when previous EIO has marked
2007 * the buffer as !uptodate
2008 */
2009 set_buffer_uptodate(bh);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002010 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01002011 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02002012 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c972013-01-17 22:11:38 +01002013 /* Make closing of filesystem visible on the media immediately */
2014 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Jan Karad664b6a2010-10-20 18:28:46 +02002017u64 lvid_get_unique_id(struct super_block *sb)
2018{
2019 struct buffer_head *bh;
2020 struct udf_sb_info *sbi = UDF_SB(sb);
2021 struct logicalVolIntegrityDesc *lvid;
2022 struct logicalVolHeaderDesc *lvhd;
2023 u64 uniqueID;
2024 u64 ret;
2025
2026 bh = sbi->s_lvid_bh;
2027 if (!bh)
2028 return 0;
2029
2030 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2031 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2032
2033 mutex_lock(&sbi->s_alloc_mutex);
2034 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2035 if (!(++uniqueID & 0xFFFFFFFF))
2036 uniqueID += 16;
2037 lvhd->uniqueID = cpu_to_le64(uniqueID);
2038 mutex_unlock(&sbi->s_alloc_mutex);
2039 mark_buffer_dirty(bh);
2040
2041 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044static int udf_fill_super(struct super_block *sb, void *options, int silent)
2045{
Jan Karad759bfa42013-07-25 19:10:59 +02002046 int ret = -EINVAL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002047 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002049 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct udf_sb_info *sbi;
2051
2052 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -08002053 uopt.uid = INVALID_UID;
2054 uopt.gid = INVALID_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01002056 uopt.fmode = UDF_INVALID_MODE;
2057 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002059 sbi = kzalloc(sizeof(struct udf_sb_info), GFP_KERNEL);
Alessio Igor Bogani9db9f9e2010-11-16 18:40:49 +01002060 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Ingo Molnar1e7933d2006-03-23 03:00:44 -08002065 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Miklos Szeredi6da80892008-02-08 04:21:50 -08002067 if (!udf_parse_options((char *)options, &uopt, false))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 goto error_out;
2069
2070 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002071 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Joe Perches8076c362011-10-10 01:08:03 -07002072 udf_err(sb, "utf8 cannot be combined with iocharset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto error_out;
2074 }
2075#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002076 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 uopt.nls_map = load_nls_default();
2078 if (!uopt.nls_map)
2079 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2080 else
2081 udf_debug("Using default NLS map\n");
2082 }
2083#endif
2084 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2085 uopt.flags |= (1 << UDF_FLAG_UTF8);
2086
2087 fileset.logicalBlockNum = 0xFFFFFFFF;
2088 fileset.partitionReferenceNum = 0xFFFF;
2089
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002090 sbi->s_flags = uopt.flags;
2091 sbi->s_uid = uopt.uid;
2092 sbi->s_gid = uopt.gid;
2093 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01002094 sbi->s_fmode = uopt.fmode;
2095 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002096 sbi->s_nls_map = uopt.nls_map;
Jan Karac03cad22010-10-20 22:17:28 +02002097 rwlock_init(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002099 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002100 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002102 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002104 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* Fill in the rest of the superblock */
2107 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02002108 sb->s_export_op = &udf_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 sb->s_magic = UDF_SUPER_MAGIC;
2111 sb->s_time_gran = 1000;
2112
Jan Kara40346002009-03-19 16:21:38 +01002113 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2114 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2115 } else {
Martin K. Petersene1defc42009-05-22 17:17:49 -04002116 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
Jan Kara40346002009-03-19 16:21:38 +01002117 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
Jan Karad759bfa42013-07-25 19:10:59 +02002118 if (ret == -EAGAIN && uopt.blocksize != UDF_DEFAULT_BLOCKSIZE) {
Jan Kara40346002009-03-19 16:21:38 +01002119 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07002120 pr_notice("Rescanning with blocksize %d\n",
2121 UDF_DEFAULT_BLOCKSIZE);
Ashish Sangwandc141a42012-07-21 16:35:17 +05302122 brelse(sbi->s_lvid_bh);
2123 sbi->s_lvid_bh = NULL;
Jan Kara40346002009-03-19 16:21:38 +01002124 uopt.blocksize = UDF_DEFAULT_BLOCKSIZE;
2125 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2126 }
2127 }
Jan Karad759bfa42013-07-25 19:10:59 +02002128 if (ret < 0) {
2129 if (ret == -EAGAIN) {
2130 udf_warn(sb, "No partition found (1)\n");
2131 ret = -EINVAL;
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto error_out;
2134 }
2135
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002136 udf_debug("Lastblock=%d\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002138 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002139 struct logicalVolIntegrityDescImpUse *lvidiu =
Jan Kara69d75672013-09-12 22:00:15 +02002140 udf_sb_lvidiu(sb);
2141 uint16_t minUDFReadRev;
2142 uint16_t minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Jan Kara69d75672013-09-12 22:00:15 +02002144 if (!lvidiu) {
2145 ret = -EINVAL;
2146 goto error_out;
2147 }
2148 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2149 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002150 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Joe Perches78ace702011-10-10 01:08:05 -07002151 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
Jan Kara69d75672013-09-12 22:00:15 +02002152 minUDFReadRev,
Joe Perches78ace702011-10-10 01:08:05 -07002153 UDF_MAX_READ_VERSION);
Jan Karad759bfa42013-07-25 19:10:59 +02002154 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto error_out;
Jan Karae729eac2013-07-25 16:15:16 +02002156 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
2157 !(sb->s_flags & MS_RDONLY)) {
2158 ret = -EACCES;
2159 goto error_out;
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002162 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2165 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2166 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2167 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2168 }
2169
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002170 if (!sbi->s_partitions) {
Joe Perches78ace702011-10-10 01:08:05 -07002171 udf_warn(sb, "No partition found (2)\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002172 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto error_out;
2174 }
2175
Marcin Slusarz4b111112008-02-08 04:20:36 -08002176 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
Jan Karae729eac2013-07-25 16:15:16 +02002177 UDF_PART_FLAG_READ_ONLY &&
2178 !(sb->s_flags & MS_RDONLY)) {
2179 ret = -EACCES;
2180 goto error_out;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002181 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002182
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002183 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Joe Perches78ace702011-10-10 01:08:05 -07002184 udf_warn(sb, "No fileset found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002185 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto error_out;
2187 }
2188
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002189 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002190 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002191 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Joe Perches78ace702011-10-10 01:08:05 -07002192 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2193 sbi->s_volume_ident,
2194 le16_to_cpu(ts.year), ts.month, ts.day,
Marcin Slusarz56774802008-02-10 11:25:31 +01002195 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 if (!(sb->s_flags & MS_RDONLY))
2198 udf_open_lvid(sb);
2199
2200 /* Assign the root inode */
2201 /* assign inodes by physical block number */
2202 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002203 inode = udf_iget(sb, &rootdir);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002204 if (!inode) {
Joe Perches78ace702011-10-10 01:08:05 -07002205 udf_err(sb, "Error in udf_iget, block=%d, partition=%d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002206 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Jan Karad759bfa42013-07-25 19:10:59 +02002207 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto error_out;
2209 }
2210
2211 /* Allocate a dentry for the root inode */
Al Viro48fde702012-01-08 22:15:13 -05002212 sb->s_root = d_make_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002213 if (!sb->s_root) {
Joe Perches78ace702011-10-10 01:08:05 -07002214 udf_err(sb, "Couldn't allocate root dentry\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002215 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 goto error_out;
2217 }
Jan Kara31170b62007-05-08 00:35:21 -07002218 sb->s_maxbytes = MAX_LFS_FILESIZE;
Al Viro8de52772012-02-06 12:45:27 -05002219 sb->s_max_links = UDF_MAX_LINKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 0;
2221
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002222error_out:
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002223 if (sbi->s_vat_inode)
2224 iput(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#ifdef CONFIG_UDF_NLS
2226 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002227 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228#endif
2229 if (!(sb->s_flags & MS_RDONLY))
2230 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002231 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002232 udf_sb_free_partitions(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 kfree(sbi);
2234 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002235
Jan Karad759bfa42013-07-25 19:10:59 +02002236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Joe Perches8076c362011-10-10 01:08:03 -07002239void _udf_err(struct super_block *sb, const char *function,
2240 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Joe Perchesc2bff362011-10-10 01:08:06 -07002242 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 va_list args;
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002246
2247 vaf.fmt = fmt;
2248 vaf.va = &args;
2249
2250 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
Joe Perchesa40ecd72011-10-10 01:08:04 -07002255void _udf_warn(struct super_block *sb, const char *function,
2256 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Joe Perchesc2bff362011-10-10 01:08:06 -07002258 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 va_list args;
2260
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002261 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002262
2263 vaf.fmt = fmt;
2264 vaf.va = &args;
2265
2266 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002271static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002273 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002275 sbi = UDF_SB(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002276
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002277 if (sbi->s_vat_inode)
2278 iput(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279#ifdef CONFIG_UDF_NLS
2280 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002281 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282#endif
2283 if (!(sb->s_flags & MS_RDONLY))
2284 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002285 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002286 udf_sb_free_partitions(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 kfree(sb->s_fs_info);
2288 sb->s_fs_info = NULL;
2289}
2290
Jan Kara146bca72009-03-16 18:27:37 +01002291static int udf_sync_fs(struct super_block *sb, int wait)
2292{
2293 struct udf_sb_info *sbi = UDF_SB(sb);
2294
2295 mutex_lock(&sbi->s_alloc_mutex);
2296 if (sbi->s_lvid_dirty) {
2297 /*
2298 * Blockdevice will be synced later so we don't have to submit
2299 * the buffer for IO
2300 */
2301 mark_buffer_dirty(sbi->s_lvid_bh);
Jan Kara146bca72009-03-16 18:27:37 +01002302 sbi->s_lvid_dirty = 0;
2303 }
2304 mutex_unlock(&sbi->s_alloc_mutex);
2305
2306 return 0;
2307}
2308
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002309static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
David Howells726c3342006-06-23 02:02:58 -07002311 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002312 struct udf_sb_info *sbi = UDF_SB(sb);
2313 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002314 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002315
Jan Kara69d75672013-09-12 22:00:15 +02002316 lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 buf->f_type = UDF_SUPER_MAGIC;
2318 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002319 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 buf->f_bfree = udf_count_free(sb);
2321 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002322 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2323 le32_to_cpu(lvidiu->numDirs)) : 0)
2324 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 buf->f_ffree = buf->f_bfree;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002326 buf->f_namelen = UDF_NAME_LEN - 2;
Coly Li557f5a12009-01-20 01:36:55 +08002327 buf->f_fsid.val[0] = (u32)id;
2328 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330 return 0;
2331}
2332
Marcin Slusarz4b111112008-02-08 04:20:36 -08002333static unsigned int udf_count_free_bitmap(struct super_block *sb,
2334 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 struct buffer_head *bh = NULL;
2337 unsigned int accum = 0;
2338 int index;
2339 int block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002340 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 uint8_t *ptr;
2343 uint16_t ident;
2344 struct spaceBitmapDesc *bm;
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002347 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002348 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002350 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07002351 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002353 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002354 brelse(bh);
Joe Perches78ace702011-10-10 01:08:05 -07002355 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 goto out;
2357 }
2358
2359 bm = (struct spaceBitmapDesc *)bh->b_data;
2360 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002361 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2362 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002364 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002365 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2366 accum += bitmap_weight((const unsigned long *)(ptr + index),
2367 cur_bytes * 8);
2368 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002369 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002370 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002371 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002373 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 udf_debug("read failed\n");
2375 goto out;
2376 }
2377 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002378 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002381 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002382out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return accum;
2384}
2385
Marcin Slusarz4b111112008-02-08 04:20:36 -08002386static unsigned int udf_count_free_table(struct super_block *sb,
2387 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002390 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002391 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002393 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Jan Karad1668fe2010-10-20 23:24:12 +02002395 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002396 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002397 epos.offset = sizeof(struct unallocSpaceEntry);
2398 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002400 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002402
Jan Kara3bf25cb2007-05-08 00:35:16 -07002403 brelse(epos.bh);
Jan Karad1668fe2010-10-20 23:24:12 +02002404 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 return accum;
2407}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002408
2409static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002412 struct udf_sb_info *sbi;
2413 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002415 sbi = UDF_SB(sb);
2416 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002417 struct logicalVolIntegrityDesc *lvid =
2418 (struct logicalVolIntegrityDesc *)
2419 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002420 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002421 accum = le32_to_cpu(
2422 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (accum == 0xFFFFFFFF)
2424 accum = 0;
2425 }
2426 }
2427
2428 if (accum)
2429 return accum;
2430
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002431 map = &sbi->s_partmaps[sbi->s_partition];
2432 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002433 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002434 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002436 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002437 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002438 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440 if (accum)
2441 return accum;
2442
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002443 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002444 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002445 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002447 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002448 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002449 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
2452 return accum;
2453}