blob: 8f2f56d9a1bbfb8b110812d45167ce7e7b456099 [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/vfs.h>
52#include <linux/vmalloc.h>
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -080053#include <linux/errno.h>
Miklos Szeredi6da80892008-02-08 04:21:50 -080054#include <linux/mount.h>
55#include <linux/seq_file.h>
Marcin Slusarz01b954a2008-02-02 22:37:07 +010056#include <linux/bitmap.h>
Bob Copelandf845fce2008-04-17 09:47:48 +020057#include <linux/crc-itu-t.h>
Jan Kara1df2ae32012-06-27 21:23:07 +020058#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/byteorder.h>
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include "udf_sb.h"
62#include "udf_i.h"
63
64#include <linux/init.h>
Fabian Fredericke9736062014-06-18 19:38:24 +020065#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jan Kara4b8d4252018-02-09 12:52:28 +010067enum {
68 VDS_POS_PRIMARY_VOL_DESC,
69 VDS_POS_UNALLOC_SPACE_DESC,
70 VDS_POS_LOGICAL_VOL_DESC,
Jan Kara4b8d4252018-02-09 12:52:28 +010071 VDS_POS_IMP_USE_VOL_DESC,
Jan Kara4b8d4252018-02-09 12:52:28 +010072 VDS_POS_LENGTH
73};
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Peter A. Felvegi44499602013-10-18 20:07:44 +020075#define VSD_FIRST_SECTOR_OFFSET 32768
76#define VSD_MAX_SECTOR_OFFSET 0x800000
77
Alden Tondettara47241c2016-04-25 19:27:56 -070078/*
79 * Maximum number of Terminating Descriptor / Logical Volume Integrity
80 * Descriptor redirections. The chosen numbers are arbitrary - just that we
81 * hopefully don't limit any real use of rewritten inode on write-once media
82 * but avoid looping for too long on corrupted media.
83 */
84#define UDF_MAX_TD_NESTING 64
85#define UDF_MAX_LVID_NESTING 1000
86
Al Viro8de52772012-02-06 12:45:27 -050087enum { UDF_MAX_LINKS = 0xffff };
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* These are the "meat" - everything else is stuffing */
90static int udf_fill_super(struct super_block *, void *, int);
91static void udf_put_super(struct super_block *);
Jan Kara146bca72009-03-16 18:27:37 +010092static int udf_sync_fs(struct super_block *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int udf_remount_fs(struct super_block *, int *, char *);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020094static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad);
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020095static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *,
96 struct kernel_lb_addr *);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070097static void udf_load_fileset(struct super_block *, struct buffer_head *,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020098 struct kernel_lb_addr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void udf_open_lvid(struct super_block *);
100static void udf_close_lvid(struct super_block *);
101static unsigned int udf_count_free(struct super_block *);
David Howells726c3342006-06-23 02:02:58 -0700102static int udf_statfs(struct dentry *, struct kstatfs *);
Al Viro34c80b12011-12-08 21:32:45 -0500103static int udf_show_options(struct seq_file *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Jan Kara69d75672013-09-12 22:00:15 +0200105struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb)
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800106{
Jan Kara69d75672013-09-12 22:00:15 +0200107 struct logicalVolIntegrityDesc *lvid;
108 unsigned int partnum;
109 unsigned int offset;
110
111 if (!UDF_SB(sb)->s_lvid_bh)
112 return NULL;
113 lvid = (struct logicalVolIntegrityDesc *)UDF_SB(sb)->s_lvid_bh->b_data;
114 partnum = le32_to_cpu(lvid->numOfPartitions);
115 if ((sb->s_blocksize - sizeof(struct logicalVolIntegrityDescImpUse) -
116 offsetof(struct logicalVolIntegrityDesc, impUse)) /
117 (2 * sizeof(uint32_t)) < partnum) {
118 udf_err(sb, "Logical volume integrity descriptor corrupted "
119 "(numOfPartitions = %u)!\n", partnum);
120 return NULL;
121 }
122 /* The offset is to skip freeSpaceTable and sizeTable arrays */
123 offset = partnum * 2 * sizeof(uint32_t);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800124 return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]);
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/* UDF filesystem type */
Al Viro152a0832010-07-25 00:46:55 +0400128static struct dentry *udf_mount(struct file_system_type *fs_type,
129 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Al Viro152a0832010-07-25 00:46:55 +0400131 return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static struct file_system_type udf_fstype = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700135 .owner = THIS_MODULE,
136 .name = "udf",
Al Viro152a0832010-07-25 00:46:55 +0400137 .mount = udf_mount,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700138 .kill_sb = kill_block_super,
139 .fs_flags = FS_REQUIRES_DEV,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
Eric W. Biederman3e64fe52013-03-11 07:05:42 -0700141MODULE_ALIAS_FS("udf");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700143static struct kmem_cache *udf_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145static struct inode *udf_alloc_inode(struct super_block *sb)
146{
147 struct udf_inode_info *ei;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800148 ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!ei)
150 return NULL;
Dan Bastone95f87972006-08-13 23:24:18 -0700151
152 ei->i_unique = 0;
153 ei->i_lenExtents = 0;
154 ei->i_next_alloc_block = 0;
155 ei->i_next_alloc_goal = 0;
156 ei->i_strat4096 = 0;
Alessio Igor Bogani4d0fb622010-11-16 18:40:47 +0100157 init_rwsem(&ei->i_data_sem);
Namjae Jeon99600052013-01-19 11:17:14 +0900158 ei->cached_extent.lstart = -1;
159 spin_lock_init(&ei->i_extent_cache_lock);
Dan Bastone95f87972006-08-13 23:24:18 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return &ei->vfs_inode;
162}
163
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100164static void udf_i_callback(struct rcu_head *head)
165{
166 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100167 kmem_cache_free(udf_inode_cachep, UDF_I(inode));
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static void udf_destroy_inode(struct inode *inode)
171{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100172 call_rcu(&inode->i_rcu, udf_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700175static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700177 struct udf_inode_info *ei = (struct udf_inode_info *)foo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Lametera35afb82007-05-16 22:10:57 -0700179 ei->i_ext.i_data = NULL;
180 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Fabian Frederick53ea18d2014-02-01 15:45:18 +0800183static int __init init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 udf_inode_cachep = kmem_cache_create("udf_inode_cache",
186 sizeof(struct udf_inode_info),
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187 0, (SLAB_RECLAIM_ACCOUNT |
Vladimir Davydov5d097052016-01-14 15:18:21 -0800188 SLAB_MEM_SPREAD |
189 SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +0900190 init_once);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700191 if (!udf_inode_cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return -ENOMEM;
193 return 0;
194}
195
196static void destroy_inodecache(void)
197{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000198 /*
199 * Make sure all delayed rcu free inodes are flushed before we
200 * destroy cache.
201 */
202 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700203 kmem_cache_destroy(udf_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/* Superblock operations */
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800207static const struct super_operations udf_sb_ops = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700208 .alloc_inode = udf_alloc_inode,
209 .destroy_inode = udf_destroy_inode,
210 .write_inode = udf_write_inode,
Al Viro3aac2b62010-06-07 00:43:39 -0400211 .evict_inode = udf_evict_inode,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700212 .put_super = udf_put_super,
Jan Kara146bca72009-03-16 18:27:37 +0100213 .sync_fs = udf_sync_fs,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700214 .statfs = udf_statfs,
215 .remount_fs = udf_remount_fs,
Miklos Szeredi6da80892008-02-08 04:21:50 -0800216 .show_options = udf_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700219struct udf_options {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned char novrs;
221 unsigned int blocksize;
222 unsigned int session;
223 unsigned int lastblock;
224 unsigned int anchor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 unsigned int flags;
Al Virofaa17292011-07-26 03:18:29 -0400226 umode_t umask;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800227 kgid_t gid;
228 kuid_t uid;
Al Virofaa17292011-07-26 03:18:29 -0400229 umode_t fmode;
230 umode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct nls_table *nls_map;
232};
233
234static int __init init_udf_fs(void)
235{
236 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 err = init_inodecache();
239 if (err)
240 goto out1;
241 err = register_filesystem(&udf_fstype);
242 if (err)
243 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700246
247out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700249
250out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return err;
252}
253
254static void __exit exit_udf_fs(void)
255{
256 unregister_filesystem(&udf_fstype);
257 destroy_inodecache();
258}
259
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800260static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
261{
262 struct udf_sb_info *sbi = UDF_SB(sb);
263
Markus Elfring033c9da2017-08-15 16:45:44 +0200264 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800265 if (!sbi->s_partmaps) {
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800266 sbi->s_partitions = 0;
267 return -ENOMEM;
268 }
269
270 sbi->s_partitions = count;
271 return 0;
272}
273
Jan Karabff943a2012-06-27 22:27:05 +0200274static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
275{
276 int i;
277 int nr_groups = bitmap->s_nr_groups;
Jan Karabff943a2012-06-27 22:27:05 +0200278
279 for (i = 0; i < nr_groups; i++)
280 if (bitmap->s_block_bitmap[i])
281 brelse(bitmap->s_block_bitmap[i]);
282
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800283 kvfree(bitmap);
Jan Karabff943a2012-06-27 22:27:05 +0200284}
285
286static void udf_free_partition(struct udf_part_map *map)
287{
288 int i;
289 struct udf_meta_data *mdata;
290
291 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
292 iput(map->s_uspace.s_table);
Jan Karabff943a2012-06-27 22:27:05 +0200293 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
294 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
Jan Karabff943a2012-06-27 22:27:05 +0200295 if (map->s_partition_type == UDF_SPARABLE_MAP15)
296 for (i = 0; i < 4; i++)
297 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
298 else if (map->s_partition_type == UDF_METADATA_MAP25) {
299 mdata = &map->s_type_specific.s_metadata;
300 iput(mdata->s_metadata_fe);
301 mdata->s_metadata_fe = NULL;
302
303 iput(mdata->s_mirror_fe);
304 mdata->s_mirror_fe = NULL;
305
306 iput(mdata->s_bitmap_fe);
307 mdata->s_bitmap_fe = NULL;
308 }
309}
310
311static void udf_sb_free_partitions(struct super_block *sb)
312{
313 struct udf_sb_info *sbi = UDF_SB(sb);
314 int i;
Markus Elfringba2eb862017-08-15 17:03:06 +0200315
316 if (!sbi->s_partmaps)
Namjae Jeon1b1baff2013-01-14 22:53:47 +0100317 return;
Jan Karabff943a2012-06-27 22:27:05 +0200318 for (i = 0; i < sbi->s_partitions; i++)
319 udf_free_partition(&sbi->s_partmaps[i]);
320 kfree(sbi->s_partmaps);
321 sbi->s_partmaps = NULL;
322}
323
Al Viro34c80b12011-12-08 21:32:45 -0500324static int udf_show_options(struct seq_file *seq, struct dentry *root)
Miklos Szeredi6da80892008-02-08 04:21:50 -0800325{
Al Viro34c80b12011-12-08 21:32:45 -0500326 struct super_block *sb = root->d_sb;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800327 struct udf_sb_info *sbi = UDF_SB(sb);
328
329 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
330 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100331 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800332 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
333 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
334 seq_puts(seq, ",unhide");
335 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
336 seq_puts(seq, ",undelete");
337 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
338 seq_puts(seq, ",noadinicb");
339 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
340 seq_puts(seq, ",shortad");
341 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
342 seq_puts(seq, ",uid=forget");
Miklos Szeredi6da80892008-02-08 04:21:50 -0800343 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
344 seq_puts(seq, ",gid=forget");
Miklos Szeredi6da80892008-02-08 04:21:50 -0800345 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800346 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800347 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800348 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800349 if (sbi->s_umask != 0)
Al Virofaa17292011-07-26 03:18:29 -0400350 seq_printf(seq, ",umask=%ho", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100351 if (sbi->s_fmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400352 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100353 if (sbi->s_dmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400354 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800355 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
Steve Magnanifcbf7632017-10-12 08:48:41 -0500356 seq_printf(seq, ",session=%d", sbi->s_session);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800357 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
358 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara403460052009-03-19 16:21:38 +0100359 if (sbi->s_anchor != 0)
360 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800361 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
362 seq_puts(seq, ",utf8");
363 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
364 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
365
366 return 0;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * udf_parse_options
371 *
372 * PURPOSE
373 * Parse mount options.
374 *
375 * DESCRIPTION
376 * The following mount options are supported:
377 *
378 * gid= Set the default group.
379 * umask= Set the default umask.
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100380 * mode= Set the default file permissions.
381 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * uid= Set the default user.
383 * bs= Set the block size.
384 * unhide Show otherwise hidden files.
385 * undelete Show deleted files in lists.
386 * adinicb Embed data in the inode (default)
387 * noadinicb Don't embed data in the inode
388 * shortad Use short ad's
389 * longad Use long ad's (default)
390 * nostrict Unset strict conformance
391 * iocharset= Set the NLS character set
392 *
393 * The remaining are for debugging and disaster recovery:
394 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700395 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
397 * The following expect a offset from 0.
398 *
399 * session= Set the CDROM session (default= last session)
400 * anchor= Override standard anchor location. (default= 256)
401 * volume= Override the VolumeDesc location. (unused)
402 * partition= Override the PartitionDesc location. (unused)
403 * lastblock= Set the last block of the filesystem/
404 *
405 * The following expect a offset from the partition root.
406 *
407 * fileset= Override the fileset block location. (unused)
408 * rootdir= Override the root directory location. (unused)
409 * WARNING: overriding the rootdir to a non-directory may
410 * yield highly unpredictable results.
411 *
412 * PRE-CONDITIONS
413 * options Pointer to mount options string.
414 * uopts Pointer to mount options variable.
415 *
416 * POST-CONDITIONS
417 * <return> 1 Mount options parsed okay.
418 * <return> 0 Error parsing mount options.
419 *
420 * HISTORY
421 * July 1, 1997 - Andrew E. Mileski
422 * Written, tested, and released.
423 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425enum {
426 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
427 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
428 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
429 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
430 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100431 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
432 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
Steven Whitehousea447c092008-10-13 10:46:57 +0100435static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436 {Opt_novrs, "novrs"},
437 {Opt_nostrict, "nostrict"},
438 {Opt_bs, "bs=%u"},
439 {Opt_unhide, "unhide"},
440 {Opt_undelete, "undelete"},
441 {Opt_noadinicb, "noadinicb"},
442 {Opt_adinicb, "adinicb"},
443 {Opt_shortad, "shortad"},
444 {Opt_longad, "longad"},
445 {Opt_uforget, "uid=forget"},
446 {Opt_uignore, "uid=ignore"},
447 {Opt_gforget, "gid=forget"},
448 {Opt_gignore, "gid=ignore"},
449 {Opt_gid, "gid=%u"},
450 {Opt_uid, "uid=%u"},
451 {Opt_umask, "umask=%o"},
452 {Opt_session, "session=%u"},
453 {Opt_lastblock, "lastblock=%u"},
454 {Opt_anchor, "anchor=%u"},
455 {Opt_volume, "volume=%u"},
456 {Opt_partition, "partition=%u"},
457 {Opt_fileset, "fileset=%u"},
458 {Opt_rootdir, "rootdir=%u"},
459 {Opt_utf8, "utf8"},
460 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100461 {Opt_fmode, "mode=%o"},
462 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700463 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465
Miklos Szeredi6da80892008-02-08 04:21:50 -0800466static int udf_parse_options(char *options, struct udf_options *uopt,
467 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 char *p;
470 int option;
471
472 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 uopt->session = 0xFFFFFFFF;
474 uopt->lastblock = 0;
475 uopt->anchor = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 if (!options)
478 return 1;
479
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700480 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 substring_t args[MAX_OPT_ARGS];
482 int token;
Fabian Frederick8c6915a2014-01-29 17:13:16 +0800483 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!*p)
485 continue;
486
487 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700488 switch (token) {
489 case Opt_novrs:
490 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100491 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700492 case Opt_bs:
493 if (match_int(&args[0], &option))
494 return 0;
Fabian Frederick8c6915a2014-01-29 17:13:16 +0800495 n = option;
496 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
497 return 0;
498 uopt->blocksize = n;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100499 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700500 break;
501 case Opt_unhide:
502 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
503 break;
504 case Opt_undelete:
505 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
506 break;
507 case Opt_noadinicb:
508 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
509 break;
510 case Opt_adinicb:
511 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
512 break;
513 case Opt_shortad:
514 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
515 break;
516 case Opt_longad:
517 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
518 break;
519 case Opt_gid:
520 if (match_int(args, &option))
521 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800522 uopt->gid = make_kgid(current_user_ns(), option);
523 if (!gid_valid(uopt->gid))
524 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700525 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700526 break;
527 case Opt_uid:
528 if (match_int(args, &option))
529 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800530 uopt->uid = make_kuid(current_user_ns(), option);
531 if (!uid_valid(uopt->uid))
532 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700533 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700534 break;
535 case Opt_umask:
536 if (match_octal(args, &option))
537 return 0;
538 uopt->umask = option;
539 break;
540 case Opt_nostrict:
541 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
542 break;
543 case Opt_session:
544 if (match_int(args, &option))
545 return 0;
546 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800547 if (!remount)
548 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700549 break;
550 case Opt_lastblock:
551 if (match_int(args, &option))
552 return 0;
553 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800554 if (!remount)
555 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700556 break;
557 case Opt_anchor:
558 if (match_int(args, &option))
559 return 0;
560 uopt->anchor = option;
561 break;
562 case Opt_volume:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700563 case Opt_partition:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700564 case Opt_fileset:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700565 case Opt_rootdir:
Jan Karaf0c4a812018-02-22 14:33:04 +0100566 /* Ignored (never implemented properly) */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700567 break;
568 case Opt_utf8:
569 uopt->flags |= (1 << UDF_FLAG_UTF8);
570 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700571 case Opt_iocharset:
Chengguang Xu785dffe2018-02-25 21:25:07 +0800572 if (!remount) {
573 if (uopt->nls_map)
574 unload_nls(uopt->nls_map);
575 uopt->nls_map = load_nls(args[0].from);
576 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
577 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700578 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700579 case Opt_uforget:
580 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
581 break;
Jan Kara70260e42018-02-21 17:27:44 +0100582 case Opt_uignore:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700583 case Opt_gignore:
Jan Kara70260e42018-02-21 17:27:44 +0100584 /* These options are superseeded by uid=<number> */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700585 break;
586 case Opt_gforget:
587 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
588 break;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100589 case Opt_fmode:
590 if (match_octal(args, &option))
591 return 0;
592 uopt->fmode = option & 0777;
593 break;
594 case Opt_dmode:
595 if (match_octal(args, &option))
596 return 0;
597 uopt->dmode = option & 0777;
598 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700599 default:
Joe Perches78ace702011-10-10 01:08:05 -0700600 pr_err("bad mount option \"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602 }
603 }
604 return 1;
605}
606
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700607static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800610 struct udf_sb_info *sbi = UDF_SB(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400611 int error = 0;
Jan Karaa9ad01b2018-09-06 15:56:10 +0200612
613 if (!(*flags & SB_RDONLY) && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
614 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Theodore Ts'o02b99842014-03-13 10:14:33 -0400616 sync_filesystem(sb);
Jan Karae729eac2013-07-25 16:15:16 +0200617
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800618 uopt.flags = sbi->s_flags;
619 uopt.uid = sbi->s_uid;
620 uopt.gid = sbi->s_gid;
621 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100622 uopt.fmode = sbi->s_fmode;
623 uopt.dmode = sbi->s_dmode;
Chengguang Xu785dffe2018-02-25 21:25:07 +0800624 uopt.nls_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Miklos Szeredi6da80892008-02-08 04:21:50 -0800626 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -EINVAL;
628
Jan Karac03cad22010-10-20 22:17:28 +0200629 write_lock(&sbi->s_cred_lock);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800630 sbi->s_flags = uopt.flags;
631 sbi->s_uid = uopt.uid;
632 sbi->s_gid = uopt.gid;
633 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100634 sbi->s_fmode = uopt.fmode;
635 sbi->s_dmode = uopt.dmode;
Jan Karac03cad22010-10-20 22:17:28 +0200636 write_unlock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800638 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400639 goto out_unlock;
640
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800641 if (*flags & SB_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 udf_close_lvid(sb);
Jan Kara36350462010-05-19 16:28:56 +0200643 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 udf_open_lvid(sb);
645
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400646out_unlock:
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400647 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Jan Kara403460052009-03-19 16:21:38 +0100650/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
651/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
652static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 struct volStructDesc *vsd = NULL;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200655 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int sectorsize;
657 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700658 int nsr02 = 0;
659 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800660 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800662 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (sb->s_blocksize < sizeof(struct volStructDesc))
664 sectorsize = sizeof(struct volStructDesc);
665 else
666 sectorsize = sb->s_blocksize;
667
Jan Karaabdc0eb2017-10-16 11:38:11 +0200668 sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Steve Magnanifcbf7632017-10-12 08:48:41 -0500670 udf_debug("Starting at sector %u (%lu byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200671 (unsigned int)(sector >> sb->s_blocksize_bits),
672 sb->s_blocksize);
Peter A. Felvegi44499602013-10-18 20:07:44 +0200673 /* Process the sequence (if applicable). The hard limit on the sector
674 * offset is arbitrary, hopefully large enough so that all valid UDF
675 * filesystems will be recognised. There is no mention of an upper
676 * bound to the size of the volume recognition area in the standard.
677 * The limit will prevent the code to read all the sectors of a
678 * specially crafted image (like a bluray disc full of CD001 sectors),
679 * potentially causing minutes or even hours of uninterruptible I/O
680 * activity. This actually happened with uninitialised SSD partitions
681 * (all 0xFF) before the check for the limit and all valid IDs were
682 * added */
683 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
684 sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* Read a block */
686 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
687 if (!bh)
688 break;
689
690 /* Look for ISO descriptors */
691 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800692 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Peter A. Felvegi44499602013-10-18 20:07:44 +0200694 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800695 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700696 switch (vsd->structType) {
697 case 0:
698 udf_debug("ISO9660 Boot Record found\n");
699 break;
700 case 1:
Joe Perchesa983f362011-10-10 01:08:07 -0700701 udf_debug("ISO9660 Primary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700702 break;
703 case 2:
Joe Perchesa983f362011-10-10 01:08:07 -0700704 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700705 break;
706 case 3:
Joe Perchesa983f362011-10-10 01:08:07 -0700707 udf_debug("ISO9660 Volume Partition Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700708 break;
709 case 255:
Joe Perchesa983f362011-10-10 01:08:07 -0700710 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700711 break;
712 default:
713 udf_debug("ISO9660 VRS (%u) found\n",
714 vsd->structType);
715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800717 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
718 VSD_STD_ID_LEN))
719 ; /* nothing */
720 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
721 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700722 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800724 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
725 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800727 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
728 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 nsr03 = sector;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200730 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
731 VSD_STD_ID_LEN))
732 ; /* nothing */
733 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
734 VSD_STD_ID_LEN))
735 ; /* nothing */
736 else {
737 /* invalid id : end of volume recognition area */
738 brelse(bh);
739 break;
740 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700741 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743
744 if (nsr03)
745 return nsr03;
746 else if (nsr02)
747 return nsr02;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200748 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
749 VSD_FIRST_SECTOR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -1;
751 else
752 return 0;
753}
754
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800755static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200756 struct kernel_lb_addr *fileset,
757 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct buffer_head *bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 uint16_t ident;
761
762 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700763 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200764 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700766 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700768 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700769 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return 1;
771 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700772
Steve Magnanifcbf7632017-10-12 08:48:41 -0500773 udf_debug("Fileset at block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700774 fileset->logicalBlockNum,
775 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Jan Kara82c82ab2018-08-21 12:00:08 +0200777 UDF_SB(sb)->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700779 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return 0;
781 }
782 return 1;
783}
784
Jan Karad759bfa42013-07-25 19:10:59 +0200785/*
786 * Load primary Volume Descriptor Sequence
787 *
788 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
789 * should be tried.
790 */
Jan Karac0eb31e2008-04-01 16:50:35 +0200791static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct primaryVolDesc *pvoldesc;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600794 uint8_t *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200795 struct buffer_head *bh;
796 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +0200797 int ret = -ENOMEM;
Deepa Dinamani0220edd2018-05-10 08:26:17 -0700798#ifdef UDFFS_DEBUG
799 struct timestamp *ts;
800#endif
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100801
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600802 outstr = kmalloc(128, GFP_NOFS);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100803 if (!outstr)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600804 return -ENOMEM;
Jan Karac0eb31e2008-04-01 16:50:35 +0200805
806 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karad759bfa42013-07-25 19:10:59 +0200807 if (!bh) {
808 ret = -EAGAIN;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100809 goto out2;
Jan Karad759bfa42013-07-25 19:10:59 +0200810 }
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100811
Jan Karad759bfa42013-07-25 19:10:59 +0200812 if (ident != TAG_IDENT_PVD) {
813 ret = -EIO;
814 goto out_bh;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 pvoldesc = (struct primaryVolDesc *)bh->b_data;
818
Deepa Dinamani0220edd2018-05-10 08:26:17 -0700819 udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
820 pvoldesc->recordingDateAndTime);
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100821#ifdef UDFFS_DEBUG
Deepa Dinamani0220edd2018-05-10 08:26:17 -0700822 ts = &pvoldesc->recordingDateAndTime;
823 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
824 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
825 ts->minute, le16_to_cpu(ts->typeAndTimezone));
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100826#endif
Deepa Dinamani0220edd2018-05-10 08:26:17 -0700827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Jan Karae966fc8d2018-04-16 15:44:19 +0200829 ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32);
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600830 if (ret < 0)
831 goto out_bh;
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200832
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600833 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
834 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Jan Karae966fc8d2018-04-16 15:44:19 +0200836 ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128);
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600837 if (ret < 0)
838 goto out_bh;
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200839
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600840 outstr[ret] = 0;
841 udf_debug("volSetIdent[] = '%s'\n", outstr);
Jan Karac0eb31e2008-04-01 16:50:35 +0200842
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100843 ret = 0;
Jan Karad759bfa42013-07-25 19:10:59 +0200844out_bh:
845 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100846out2:
847 kfree(outstr);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100848 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Namjae Jeon3080a742011-10-23 19:28:32 +0900851struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
Alden Tondettar78888242016-05-18 14:09:19 -0700852 u32 meta_file_loc, u32 partition_ref)
Namjae Jeon3080a742011-10-23 19:28:32 +0900853{
854 struct kernel_lb_addr addr;
855 struct inode *metadata_fe;
856
857 addr.logicalBlockNum = meta_file_loc;
Alden Tondettar78888242016-05-18 14:09:19 -0700858 addr.partitionReferenceNum = partition_ref;
Namjae Jeon3080a742011-10-23 19:28:32 +0900859
Jan Kara6174c2e2014-10-09 12:52:16 +0200860 metadata_fe = udf_iget_special(sb, &addr);
Namjae Jeon3080a742011-10-23 19:28:32 +0900861
Jan Kara6d3d5e82014-09-04 16:15:51 +0200862 if (IS_ERR(metadata_fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900863 udf_warn(sb, "metadata inode efe not found\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +0200864 return metadata_fe;
865 }
866 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900867 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
868 iput(metadata_fe);
Jan Kara6d3d5e82014-09-04 16:15:51 +0200869 return ERR_PTR(-EIO);
Namjae Jeon3080a742011-10-23 19:28:32 +0900870 }
871
872 return metadata_fe;
873}
874
Alden Tondettar78888242016-05-18 14:09:19 -0700875static int udf_load_metadata_files(struct super_block *sb, int partition,
876 int type1_index)
Jan Karabfb257a2008-04-08 20:37:21 +0200877{
878 struct udf_sb_info *sbi = UDF_SB(sb);
879 struct udf_part_map *map;
880 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200881 struct kernel_lb_addr addr;
Jan Kara6d3d5e82014-09-04 16:15:51 +0200882 struct inode *fe;
Jan Karabfb257a2008-04-08 20:37:21 +0200883
884 map = &sbi->s_partmaps[partition];
885 mdata = &map->s_type_specific.s_metadata;
Alden Tondettar78888242016-05-18 14:09:19 -0700886 mdata->s_phys_partition_ref = type1_index;
Jan Karabfb257a2008-04-08 20:37:21 +0200887
888 /* metadata address */
Steve Magnanifcbf7632017-10-12 08:48:41 -0500889 udf_debug("Metadata file location: block = %u part = %u\n",
Alden Tondettar78888242016-05-18 14:09:19 -0700890 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200891
Jan Kara6d3d5e82014-09-04 16:15:51 +0200892 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
Alden Tondettar78888242016-05-18 14:09:19 -0700893 mdata->s_phys_partition_ref);
Jan Kara6d3d5e82014-09-04 16:15:51 +0200894 if (IS_ERR(fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900895 /* mirror file entry */
Steve Magnanifcbf7632017-10-12 08:48:41 -0500896 udf_debug("Mirror metadata file location: block = %u part = %u\n",
Alden Tondettar78888242016-05-18 14:09:19 -0700897 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200898
Jan Kara6d3d5e82014-09-04 16:15:51 +0200899 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
Alden Tondettar78888242016-05-18 14:09:19 -0700900 mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200901
Jan Kara6d3d5e82014-09-04 16:15:51 +0200902 if (IS_ERR(fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900903 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +0200904 return PTR_ERR(fe);
Namjae Jeon3080a742011-10-23 19:28:32 +0900905 }
Jan Kara6d3d5e82014-09-04 16:15:51 +0200906 mdata->s_mirror_fe = fe;
907 } else
908 mdata->s_metadata_fe = fe;
909
Jan Karabfb257a2008-04-08 20:37:21 +0200910
911 /*
912 * bitmap file entry
913 * Note:
914 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
915 */
916 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
917 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
Alden Tondettar78888242016-05-18 14:09:19 -0700918 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
Jan Karabfb257a2008-04-08 20:37:21 +0200919
Steve Magnanifcbf7632017-10-12 08:48:41 -0500920 udf_debug("Bitmap file location: block = %u part = %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -0700921 addr.logicalBlockNum, addr.partitionReferenceNum);
Jan Karabfb257a2008-04-08 20:37:21 +0200922
Jan Kara6174c2e2014-10-09 12:52:16 +0200923 fe = udf_iget_special(sb, &addr);
Jan Kara6d3d5e82014-09-04 16:15:51 +0200924 if (IS_ERR(fe)) {
David Howellsbc98a422017-07-17 08:45:34 +0100925 if (sb_rdonly(sb))
Joe Perchesa40ecd72011-10-10 01:08:04 -0700926 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 +0200927 else {
Joe Perches8076c362011-10-10 01:08:03 -0700928 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +0200929 return PTR_ERR(fe);
Jan Karabfb257a2008-04-08 20:37:21 +0200930 }
Jan Kara6d3d5e82014-09-04 16:15:51 +0200931 } else
932 mdata->s_bitmap_fe = fe;
Jan Karabfb257a2008-04-08 20:37:21 +0200933 }
934
935 udf_debug("udf_load_metadata_files Ok\n");
Jan Karabfb257a2008-04-08 20:37:21 +0200936 return 0;
Jan Karabfb257a2008-04-08 20:37:21 +0200937}
938
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700939static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200940 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 struct fileSetDesc *fset;
943
944 fset = (struct fileSetDesc *)bh->b_data;
945
946 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
947
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800948 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Steve Magnanifcbf7632017-10-12 08:48:41 -0500950 udf_debug("Rootdir at block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700951 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Marcin Slusarz883cb9d12008-02-08 04:20:34 -0800954int udf_compute_nr_groups(struct super_block *sb, u32 partition)
955{
956 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +0100957 return DIV_ROUND_UP(map->s_partition_len +
958 (sizeof(struct spaceBitmapDesc) << 3),
959 sb->s_blocksize * 8);
Marcin Slusarz883cb9d12008-02-08 04:20:34 -0800960}
961
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800962static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
963{
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800964 struct udf_bitmap *bitmap;
965 int nr_groups;
966 int size;
967
Marcin Slusarz883cb9d12008-02-08 04:20:34 -0800968 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800969 size = sizeof(struct udf_bitmap) +
970 (sizeof(struct buffer_head *) * nr_groups);
971
972 if (size <= PAGE_SIZE)
Joe Perchesed2ae6f2010-11-04 20:08:04 -0700973 bitmap = kzalloc(size, GFP_KERNEL);
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800974 else
Joe Perchesed2ae6f2010-11-04 20:08:04 -0700975 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800976
Markus Elfringba2eb862017-08-15 17:03:06 +0200977 if (!bitmap)
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800978 return NULL;
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800979
Marcin Slusarz66e1da32008-02-08 04:20:33 -0800980 bitmap->s_nr_groups = nr_groups;
981 return bitmap;
982}
983
Jan Karab085fbe2018-09-06 15:46:17 +0200984static int check_partition_desc(struct super_block *sb,
985 struct partitionDesc *p,
986 struct udf_part_map *map)
987{
988 bool umap, utable, fmap, ftable;
989 struct partitionHeaderDesc *phd;
990
991 switch (le32_to_cpu(p->accessType)) {
992 case PD_ACCESS_TYPE_READ_ONLY:
993 case PD_ACCESS_TYPE_WRITE_ONCE:
994 case PD_ACCESS_TYPE_REWRITABLE:
995 case PD_ACCESS_TYPE_NONE:
996 goto force_ro;
997 }
998
999 /* No Partition Header Descriptor? */
1000 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1001 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1002 goto force_ro;
1003
1004 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1005 utable = phd->unallocSpaceTable.extLength;
1006 umap = phd->unallocSpaceBitmap.extLength;
1007 ftable = phd->freedSpaceTable.extLength;
1008 fmap = phd->freedSpaceBitmap.extLength;
1009
1010 /* No allocation info? */
1011 if (!utable && !umap && !ftable && !fmap)
1012 goto force_ro;
1013
1014 /* We don't support blocks that require erasing before overwrite */
1015 if (ftable || fmap)
1016 goto force_ro;
1017 /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */
1018 if (utable && umap)
1019 goto force_ro;
1020
1021 if (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
1022 map->s_partition_type == UDF_VIRTUAL_MAP20)
1023 goto force_ro;
1024
1025 return 0;
1026force_ro:
1027 if (!sb_rdonly(sb))
1028 return -EACCES;
1029 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
1030 return 0;
1031}
1032
Jan Kara3fb38df2008-04-02 12:26:36 +02001033static int udf_fill_partdesc_info(struct super_block *sb,
1034 struct partitionDesc *p, int p_index)
1035{
1036 struct udf_part_map *map;
1037 struct udf_sb_info *sbi = UDF_SB(sb);
1038 struct partitionHeaderDesc *phd;
Jan Karab085fbe2018-09-06 15:46:17 +02001039 int err;
Jan Kara3fb38df2008-04-02 12:26:36 +02001040
1041 map = &sbi->s_partmaps[p_index];
1042
1043 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1044 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1045
1046 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1047 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1048 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1049 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1050 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1051 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1052 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1053 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1054
Steve Magnanifcbf7632017-10-12 08:48:41 -05001055 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001056 p_index, map->s_partition_type,
1057 map->s_partition_root, map->s_partition_len);
Jan Kara3fb38df2008-04-02 12:26:36 +02001058
Jan Karab085fbe2018-09-06 15:46:17 +02001059 err = check_partition_desc(sb, p, map);
1060 if (err)
1061 return err;
1062
1063 /*
1064 * Skip loading allocation info it we cannot ever write to the fs.
1065 * This is a correctness thing as we may have decided to force ro mount
1066 * to avoid allocation info we don't support.
1067 */
1068 if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT))
Jan Kara3fb38df2008-04-02 12:26:36 +02001069 return 0;
1070
1071 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1072 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001073 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001074 .logicalBlockNum = le32_to_cpu(
1075 phd->unallocSpaceTable.extPosition),
1076 .partitionReferenceNum = p_index,
1077 };
Jan Kara6d3d5e82014-09-04 16:15:51 +02001078 struct inode *inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001079
Jan Kara6174c2e2014-10-09 12:52:16 +02001080 inode = udf_iget_special(sb, &loc);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001081 if (IS_ERR(inode)) {
Jan Kara3fb38df2008-04-02 12:26:36 +02001082 udf_debug("cannot load unallocSpaceTable (part %d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001083 p_index);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001084 return PTR_ERR(inode);
Jan Kara3fb38df2008-04-02 12:26:36 +02001085 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001086 map->s_uspace.s_table = inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001087 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001088 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001089 p_index, map->s_uspace.s_table->i_ino);
Jan Kara3fb38df2008-04-02 12:26:36 +02001090 }
1091
1092 if (phd->unallocSpaceBitmap.extLength) {
1093 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1094 if (!bitmap)
Jan Karad759bfa42013-07-25 19:10:59 +02001095 return -ENOMEM;
Jan Kara3fb38df2008-04-02 12:26:36 +02001096 map->s_uspace.s_bitmap = bitmap;
Jan Kara3fb38df2008-04-02 12:26:36 +02001097 bitmap->s_extPosition = le32_to_cpu(
1098 phd->unallocSpaceBitmap.extPosition);
1099 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001100 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001101 p_index, bitmap->s_extPosition);
Jan Kara3fb38df2008-04-02 12:26:36 +02001102 }
1103
Jan Kara3fb38df2008-04-02 12:26:36 +02001104 return 0;
1105}
1106
Jan Karae971b0b2009-11-30 19:47:55 +01001107static void udf_find_vat_block(struct super_block *sb, int p_index,
1108 int type1_index, sector_t start_block)
1109{
1110 struct udf_sb_info *sbi = UDF_SB(sb);
1111 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1112 sector_t vat_block;
1113 struct kernel_lb_addr ino;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001114 struct inode *inode;
Jan Karae971b0b2009-11-30 19:47:55 +01001115
1116 /*
1117 * VAT file entry is in the last recorded block. Some broken disks have
1118 * it a few blocks before so try a bit harder...
1119 */
1120 ino.partitionReferenceNum = type1_index;
1121 for (vat_block = start_block;
1122 vat_block >= map->s_partition_root &&
Jan Kara6d3d5e82014-09-04 16:15:51 +02001123 vat_block >= start_block - 3; vat_block--) {
Jan Karae971b0b2009-11-30 19:47:55 +01001124 ino.logicalBlockNum = vat_block - map->s_partition_root;
Jan Kara6174c2e2014-10-09 12:52:16 +02001125 inode = udf_iget_special(sb, &ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001126 if (!IS_ERR(inode)) {
1127 sbi->s_vat_inode = inode;
1128 break;
1129 }
Jan Karae971b0b2009-11-30 19:47:55 +01001130 }
1131}
1132
Jan Kara38b74a52008-04-02 16:01:35 +02001133static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1134{
1135 struct udf_sb_info *sbi = UDF_SB(sb);
1136 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Jan Karafa5e0812008-04-08 02:08:53 +02001137 struct buffer_head *bh = NULL;
1138 struct udf_inode_info *vati;
1139 uint32_t pos;
1140 struct virtualAllocationTable20 *vat20;
Fabian Frederick23bcda12017-01-06 21:54:41 +01001141 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1142 sb->s_blocksize_bits;
Jan Kara38b74a52008-04-02 16:01:35 +02001143
Jan Karae971b0b2009-11-30 19:47:55 +01001144 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
Jan Kara4bf17af2009-07-14 19:30:23 +02001145 if (!sbi->s_vat_inode &&
1146 sbi->s_last_block != blocks - 1) {
Joe Perches78ace702011-10-10 01:08:05 -07001147 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1148 (unsigned long)sbi->s_last_block,
1149 (unsigned long)blocks - 1);
Jan Karae971b0b2009-11-30 19:47:55 +01001150 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
Jan Kara4bf17af2009-07-14 19:30:23 +02001151 }
Jan Kara38b74a52008-04-02 16:01:35 +02001152 if (!sbi->s_vat_inode)
Jan Karad759bfa42013-07-25 19:10:59 +02001153 return -EIO;
Jan Kara38b74a52008-04-02 16:01:35 +02001154
1155 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001156 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001157 map->s_type_specific.s_virtual.s_num_entries =
1158 (sbi->s_vat_inode->i_size - 36) >> 2;
1159 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001160 vati = UDF_I(sbi->s_vat_inode);
1161 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1162 pos = udf_block_map(sbi->s_vat_inode, 0);
1163 bh = sb_bread(sb, pos);
1164 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001165 return -EIO;
Jan Karafa5e0812008-04-08 02:08:53 +02001166 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1167 } else {
1168 vat20 = (struct virtualAllocationTable20 *)
1169 vati->i_ext.i_data;
1170 }
Jan Kara38b74a52008-04-02 16:01:35 +02001171
Jan Kara38b74a52008-04-02 16:01:35 +02001172 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001173 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001174 map->s_type_specific.s_virtual.s_num_entries =
1175 (sbi->s_vat_inode->i_size -
1176 map->s_type_specific.s_virtual.
1177 s_start_offset) >> 2;
1178 brelse(bh);
1179 }
1180 return 0;
1181}
1182
Jan Karad759bfa42013-07-25 19:10:59 +02001183/*
1184 * Load partition descriptor block
1185 *
1186 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1187 * sequence.
1188 */
Jan Karac0eb31e2008-04-01 16:50:35 +02001189static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Jan Karac0eb31e2008-04-01 16:50:35 +02001191 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001192 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001193 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001194 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001195 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001196 uint16_t partitionNumber;
1197 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +02001198 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Jan Karac0eb31e2008-04-01 16:50:35 +02001200 bh = udf_read_tagged(sb, block, block, &ident);
1201 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001202 return -EAGAIN;
1203 if (ident != TAG_IDENT_PD) {
1204 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001205 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001206 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001207
1208 p = (struct partitionDesc *)bh->b_data;
1209 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001210
Alden Tondettar78888242016-05-18 14:09:19 -07001211 /* First scan for TYPE1 and SPARABLE partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001212 for (i = 0; i < sbi->s_partitions; i++) {
1213 map = &sbi->s_partmaps[i];
Steve Magnanifcbf7632017-10-12 08:48:41 -05001214 udf_debug("Searching map: (%u == %u)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001215 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001216 if (map->s_partition_num == partitionNumber &&
1217 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1218 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001220 }
1221
Jan Kara38b74a52008-04-02 16:01:35 +02001222 if (i >= sbi->s_partitions) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001223 udf_debug("Partition (%u) not found in partition map\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001224 partitionNumber);
Jan Karad759bfa42013-07-25 19:10:59 +02001225 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001226 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001227 }
1228
Jan Kara3fb38df2008-04-02 12:26:36 +02001229 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001230 if (ret < 0)
1231 goto out_bh;
Jan Kara38b74a52008-04-02 16:01:35 +02001232
1233 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001234 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1235 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001236 */
1237 type1_idx = i;
Peter A. Felvegi44499602013-10-18 20:07:44 +02001238#ifdef UDFFS_DEBUG
1239 map = NULL; /* supress 'maybe used uninitialized' warning */
1240#endif
Jan Kara38b74a52008-04-02 16:01:35 +02001241 for (i = 0; i < sbi->s_partitions; i++) {
1242 map = &sbi->s_partmaps[i];
1243
1244 if (map->s_partition_num == partitionNumber &&
1245 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001246 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1247 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001248 break;
1249 }
1250
Jan Karad759bfa42013-07-25 19:10:59 +02001251 if (i >= sbi->s_partitions) {
1252 ret = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001253 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001254 }
Jan Kara38b74a52008-04-02 16:01:35 +02001255
1256 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001257 if (ret < 0)
Jan Kara38b74a52008-04-02 16:01:35 +02001258 goto out_bh;
1259
Jan Karabfb257a2008-04-08 20:37:21 +02001260 if (map->s_partition_type == UDF_METADATA_MAP25) {
Alden Tondettar78888242016-05-18 14:09:19 -07001261 ret = udf_load_metadata_files(sb, i, type1_idx);
Jan Karad759bfa42013-07-25 19:10:59 +02001262 if (ret < 0) {
Joe Perches78ace702011-10-10 01:08:05 -07001263 udf_err(sb, "error loading MetaData partition map %d\n",
1264 i);
Jan Karabfb257a2008-04-08 20:37:21 +02001265 goto out_bh;
1266 }
1267 } else {
Jan Karae729eac2013-07-25 16:15:16 +02001268 /*
1269 * If we have a partition with virtual map, we don't handle
1270 * writing to it (we overwrite blocks instead of relocating
1271 * them).
1272 */
David Howellsbc98a422017-07-17 08:45:34 +01001273 if (!sb_rdonly(sb)) {
Jan Karae729eac2013-07-25 16:15:16 +02001274 ret = -EACCES;
1275 goto out_bh;
1276 }
Jan Karaa9ad01b2018-09-06 15:56:10 +02001277 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
Jan Karabfb257a2008-04-08 20:37:21 +02001278 ret = udf_load_vat(sb, i, type1_idx);
Jan Karad759bfa42013-07-25 19:10:59 +02001279 if (ret < 0)
Jan Karabfb257a2008-04-08 20:37:21 +02001280 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001281 }
Jan Karad759bfa42013-07-25 19:10:59 +02001282 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001283out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001284 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001285 brelse(bh);
1286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Jan Kara1df2ae32012-06-27 21:23:07 +02001289static int udf_load_sparable_map(struct super_block *sb,
1290 struct udf_part_map *map,
1291 struct sparablePartitionMap *spm)
1292{
1293 uint32_t loc;
1294 uint16_t ident;
1295 struct sparingTable *st;
1296 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1297 int i;
1298 struct buffer_head *bh;
1299
1300 map->s_partition_type = UDF_SPARABLE_MAP15;
1301 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1302 if (!is_power_of_2(sdata->s_packet_len)) {
1303 udf_err(sb, "error loading logical volume descriptor: "
1304 "Invalid packet length %u\n",
1305 (unsigned)sdata->s_packet_len);
1306 return -EIO;
1307 }
1308 if (spm->numSparingTables > 4) {
1309 udf_err(sb, "error loading logical volume descriptor: "
1310 "Too many sparing tables (%d)\n",
1311 (int)spm->numSparingTables);
1312 return -EIO;
1313 }
1314
1315 for (i = 0; i < spm->numSparingTables; i++) {
1316 loc = le32_to_cpu(spm->locSparingTable[i]);
1317 bh = udf_read_tagged(sb, loc, loc, &ident);
1318 if (!bh)
1319 continue;
1320
1321 st = (struct sparingTable *)bh->b_data;
1322 if (ident != 0 ||
1323 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1324 strlen(UDF_ID_SPARING)) ||
1325 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1326 sb->s_blocksize) {
1327 brelse(bh);
1328 continue;
1329 }
1330
1331 sdata->s_spar_map[i] = bh;
1332 }
1333 map->s_partition_func = udf_get_pblock_spar15;
1334 return 0;
1335}
1336
Jan Karac0eb31e2008-04-01 16:50:35 +02001337static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001338 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct logicalVolDesc *lvd;
Jan Kara1df2ae32012-06-27 21:23:07 +02001341 int i, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001343 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001344 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001345 uint16_t ident;
1346 struct buffer_head *bh;
Jan Karaadee11b2012-06-27 20:20:22 +02001347 unsigned int table_len;
Jan Karad759bfa42013-07-25 19:10:59 +02001348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jan Karac0eb31e2008-04-01 16:50:35 +02001350 bh = udf_read_tagged(sb, block, block, &ident);
1351 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001352 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001353 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 lvd = (struct logicalVolDesc *)bh->b_data;
Jan Karaadee11b2012-06-27 20:20:22 +02001355 table_len = le32_to_cpu(lvd->mapTableLength);
Jan Kara57b96552012-07-10 17:58:04 +02001356 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
Jan Karaadee11b2012-06-27 20:20:22 +02001357 udf_err(sb, "error loading logical volume descriptor: "
1358 "Partition table too long (%u > %lu)\n", table_len,
1359 sb->s_blocksize - sizeof(*lvd));
Jan Karad759bfa42013-07-25 19:10:59 +02001360 ret = -EIO;
Jan Karaadee11b2012-06-27 20:20:22 +02001361 goto out_bh;
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Jan Karacb14d342012-06-27 20:08:44 +02001364 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1365 if (ret)
Jan Karac0eb31e2008-04-01 16:50:35 +02001366 goto out_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001368 for (i = 0, offset = 0;
Jan Karaadee11b2012-06-27 20:20:22 +02001369 i < sbi->s_partitions && offset < table_len;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001370 i++, offset += gpm->partitionMapLength) {
1371 struct udf_part_map *map = &sbi->s_partmaps[i];
1372 gpm = (struct genericPartitionMap *)
1373 &(lvd->partitionMaps[offset]);
1374 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001375 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001376 struct genericPartitionMap1 *gpm1 =
1377 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001378 map->s_partition_type = UDF_TYPE1_MAP15;
1379 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1380 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1381 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001382 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001383 struct udfPartitionMap2 *upm2 =
1384 (struct udfPartitionMap2 *)gpm;
1385 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1386 strlen(UDF_ID_VIRTUAL))) {
1387 u16 suf =
1388 le16_to_cpu(((__le16 *)upm2->partIdent.
1389 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001390 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001391 map->s_partition_type =
1392 UDF_VIRTUAL_MAP15;
1393 map->s_partition_func =
1394 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001395 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001396 map->s_partition_type =
1397 UDF_VIRTUAL_MAP20;
1398 map->s_partition_func =
1399 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001401 } else if (!strncmp(upm2->partIdent.ident,
1402 UDF_ID_SPARABLE,
1403 strlen(UDF_ID_SPARABLE))) {
Jan Karad759bfa42013-07-25 19:10:59 +02001404 ret = udf_load_sparable_map(sb, map,
1405 (struct sparablePartitionMap *)gpm);
1406 if (ret < 0)
Jan Kara1df2ae32012-06-27 21:23:07 +02001407 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001408 } else if (!strncmp(upm2->partIdent.ident,
1409 UDF_ID_METADATA,
1410 strlen(UDF_ID_METADATA))) {
1411 struct udf_meta_data *mdata =
1412 &map->s_type_specific.s_metadata;
1413 struct metadataPartitionMap *mdm =
1414 (struct metadataPartitionMap *)
1415 &(lvd->partitionMaps[offset]);
Steve Magnanifcbf7632017-10-12 08:48:41 -05001416 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001417 i, type, UDF_ID_METADATA);
Jan Karabfb257a2008-04-08 20:37:21 +02001418
1419 map->s_partition_type = UDF_METADATA_MAP25;
1420 map->s_partition_func = udf_get_pblock_meta25;
1421
1422 mdata->s_meta_file_loc =
1423 le32_to_cpu(mdm->metadataFileLoc);
1424 mdata->s_mirror_file_loc =
1425 le32_to_cpu(mdm->metadataMirrorFileLoc);
1426 mdata->s_bitmap_file_loc =
1427 le32_to_cpu(mdm->metadataBitmapFileLoc);
1428 mdata->s_alloc_unit_size =
1429 le32_to_cpu(mdm->allocUnitSize);
1430 mdata->s_align_unit_size =
1431 le16_to_cpu(mdm->alignUnitSize);
Jan Karaed47a7d2011-10-24 16:47:48 +02001432 if (mdm->flags & 0x01)
1433 mdata->s_flags |= MF_DUPLICATE_MD;
Jan Karabfb257a2008-04-08 20:37:21 +02001434
1435 udf_debug("Metadata Ident suffix=0x%x\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001436 le16_to_cpu(*(__le16 *)
1437 mdm->partIdent.identSuffix));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001438 udf_debug("Metadata part num=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001439 le16_to_cpu(mdm->partitionNum));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001440 udf_debug("Metadata part alloc unit size=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001441 le32_to_cpu(mdm->allocUnitSize));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001442 udf_debug("Metadata file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001443 le32_to_cpu(mdm->metadataFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001444 udf_debug("Mirror file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001445 le32_to_cpu(mdm->metadataMirrorFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001446 udf_debug("Bitmap file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001447 le32_to_cpu(mdm->metadataBitmapFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001448 udf_debug("Flags: %d %u\n",
Jan Karaed47a7d2011-10-24 16:47:48 +02001449 mdata->s_flags, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001450 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001451 udf_debug("Unknown ident: %s\n",
1452 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 continue;
1454 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001455 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1456 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Steve Magnanifcbf7632017-10-12 08:48:41 -05001458 udf_debug("Partition (%d:%u) type %u on volume %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001459 i, map->s_partition_num, type, map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001462 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001463 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 *fileset = lelb_to_cpu(la->extLocation);
Steve Magnanifcbf7632017-10-12 08:48:41 -05001466 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001467 fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001468 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 if (lvd->integritySeqExt.extLength)
1471 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Jan Karad759bfa42013-07-25 19:10:59 +02001472 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001473out_bh:
1474 brelse(bh);
1475 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478/*
Alden Tondettara47241c2016-04-25 19:27:56 -07001479 * Find the prevailing Logical Volume Integrity Descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001481static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Alden Tondettara47241c2016-04-25 19:27:56 -07001483 struct buffer_head *bh, *final_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001485 struct udf_sb_info *sbi = UDF_SB(sb);
1486 struct logicalVolIntegrityDesc *lvid;
Alden Tondettara47241c2016-04-25 19:27:56 -07001487 int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Alden Tondettara47241c2016-04-25 19:27:56 -07001489 while (++indirections <= UDF_MAX_LVID_NESTING) {
1490 final_bh = NULL;
1491 while (loc.extLength > 0 &&
1492 (bh = udf_read_tagged(sb, loc.extLocation,
1493 loc.extLocation, &ident))) {
1494 if (ident != TAG_IDENT_LVID) {
1495 brelse(bh);
1496 break;
1497 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001498
Alden Tondettara47241c2016-04-25 19:27:56 -07001499 brelse(final_bh);
1500 final_bh = bh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001501
Alden Tondettara47241c2016-04-25 19:27:56 -07001502 loc.extLength -= sb->s_blocksize;
1503 loc.extLocation++;
1504 }
1505
1506 if (!final_bh)
1507 return;
1508
1509 brelse(sbi->s_lvid_bh);
1510 sbi->s_lvid_bh = final_bh;
1511
1512 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1513 if (lvid->nextIntegrityExt.extLength == 0)
1514 return;
1515
1516 loc = leea_to_cpu(lvid->nextIntegrityExt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
Alden Tondettara47241c2016-04-25 19:27:56 -07001518
1519 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1520 UDF_MAX_LVID_NESTING);
1521 brelse(sbi->s_lvid_bh);
1522 sbi->s_lvid_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Jan Kara7b78fd02018-02-09 15:56:34 +01001525/*
1526 * Step for reallocation of table of partition descriptor sequence numbers.
1527 * Must be power of 2.
1528 */
1529#define PART_DESC_ALLOC_STEP 32
1530
Jan Karaee4af502018-08-21 14:52:34 +02001531struct part_desc_seq_scan_data {
1532 struct udf_vds_record rec;
1533 u32 partnum;
1534};
1535
Jan Kara7b78fd02018-02-09 15:56:34 +01001536struct desc_seq_scan_data {
1537 struct udf_vds_record vds[VDS_POS_LENGTH];
1538 unsigned int size_part_descs;
Jan Karaee4af502018-08-21 14:52:34 +02001539 unsigned int num_part_descs;
1540 struct part_desc_seq_scan_data *part_descs_loc;
Jan Kara7b78fd02018-02-09 15:56:34 +01001541};
1542
1543static struct udf_vds_record *handle_partition_descriptor(
1544 struct buffer_head *bh,
1545 struct desc_seq_scan_data *data)
1546{
1547 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1548 int partnum;
Jan Karaee4af502018-08-21 14:52:34 +02001549 int i;
Jan Kara7b78fd02018-02-09 15:56:34 +01001550
1551 partnum = le16_to_cpu(desc->partitionNumber);
Jan Karaee4af502018-08-21 14:52:34 +02001552 for (i = 0; i < data->num_part_descs; i++)
1553 if (partnum == data->part_descs_loc[i].partnum)
1554 return &(data->part_descs_loc[i].rec);
1555 if (data->num_part_descs >= data->size_part_descs) {
1556 struct part_desc_seq_scan_data *new_loc;
Jan Kara7b78fd02018-02-09 15:56:34 +01001557 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1558
Kees Cook6396bb22018-06-12 14:03:40 -07001559 new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL);
Jan Kara7b78fd02018-02-09 15:56:34 +01001560 if (!new_loc)
1561 return ERR_PTR(-ENOMEM);
1562 memcpy(new_loc, data->part_descs_loc,
1563 data->size_part_descs * sizeof(*new_loc));
1564 kfree(data->part_descs_loc);
1565 data->part_descs_loc = new_loc;
1566 data->size_part_descs = new_size;
1567 }
Jan Karaee4af502018-08-21 14:52:34 +02001568 return &(data->part_descs_loc[data->num_part_descs++].rec);
Jan Kara7b78fd02018-02-09 15:56:34 +01001569}
1570
1571
1572static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1573 struct buffer_head *bh, struct desc_seq_scan_data *data)
Jan Kara18cf47812018-02-09 15:15:39 +01001574{
1575 switch (ident) {
1576 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001577 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001578 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001579 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001580 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001581 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001582 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001583 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1584 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1585 return handle_partition_descriptor(bh, data);
Jan Kara18cf47812018-02-09 15:15:39 +01001586 }
1587 return NULL;
1588}
Vegard Nossume7a4eb82015-12-10 17:13:41 +01001589
1590/*
Jan Karad759bfa42013-07-25 19:10:59 +02001591 * Process a main/reserve volume descriptor sequence.
1592 * @block First block of first extent of the sequence.
1593 * @lastblock Lastblock of first extent of the sequence.
1594 * @fileset There we store extent containing root fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 *
Jan Karad759bfa42013-07-25 19:10:59 +02001596 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1597 * sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
Jan Karad759bfa42013-07-25 19:10:59 +02001599static noinline int udf_process_sequence(
1600 struct super_block *sb,
1601 sector_t block, sector_t lastblock,
1602 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct buffer_head *bh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001605 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct generic_desc *gd;
1607 struct volDescPtr *vdp;
Fabian Frederick2b8f9422015-02-04 18:15:04 +01001608 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 uint32_t vdsn;
1610 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +02001611 int ret;
Vegard Nossume7a4eb82015-12-10 17:13:41 +01001612 unsigned int indirections = 0;
Jan Kara7b78fd02018-02-09 15:56:34 +01001613 struct desc_seq_scan_data data;
1614 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jan Kara7b78fd02018-02-09 15:56:34 +01001616 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1617 data.size_part_descs = PART_DESC_ALLOC_STEP;
Jan Karaee4af502018-08-21 14:52:34 +02001618 data.num_part_descs = 0;
Kees Cook6396bb22018-06-12 14:03:40 -07001619 data.part_descs_loc = kcalloc(data.size_part_descs,
1620 sizeof(*data.part_descs_loc),
1621 GFP_KERNEL);
Jan Kara7b78fd02018-02-09 15:56:34 +01001622 if (!data.part_descs_loc)
1623 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Jan Karac0eb31e2008-04-01 16:50:35 +02001625 /*
1626 * Read the main descriptor sequence and find which descriptors
1627 * are in it.
1628 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001629 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 bh = udf_read_tagged(sb, block, block, &ident);
Jan Kara67621672018-02-08 18:10:54 +01001631 if (!bh)
1632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1635 gd = (struct generic_desc *)bh->b_data;
1636 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001637 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001638 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Jan Kara7b568cb2018-02-09 13:28:41 +01001639 if (++indirections > UDF_MAX_TD_NESTING) {
1640 udf_err(sb, "too many Volume Descriptor "
1641 "Pointers (max %u supported)\n",
1642 UDF_MAX_TD_NESTING);
1643 brelse(bh);
1644 return -EIO;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001645 }
Jan Kara7b568cb2018-02-09 13:28:41 +01001646
1647 vdp = (struct volDescPtr *)bh->b_data;
1648 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1649 lastblock = le32_to_cpu(
1650 vdp->nextVolDescSeqExt.extLength) >>
1651 sb->s_blocksize_bits;
1652 lastblock += block - 1;
1653 /* For loop is going to increment 'block' again */
1654 block--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001655 break;
Jan Kara18cf47812018-02-09 15:15:39 +01001656 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001657 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Jan Kara18cf47812018-02-09 15:15:39 +01001658 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1659 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001660 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1661 curr = get_volume_descriptor_record(ident, bh, &data);
1662 if (IS_ERR(curr)) {
1663 brelse(bh);
1664 return PTR_ERR(curr);
1665 }
1666 /* Descriptor we don't care about? */
1667 if (!curr)
1668 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001669 if (vdsn >= curr->volDescSeqNum) {
1670 curr->volDescSeqNum = vdsn;
1671 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001672 }
1673 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001674 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Jan Kara7b568cb2018-02-09 13:28:41 +01001675 done = true;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001676 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001678 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001680 /*
1681 * Now read interesting descriptors again and process them
1682 * in a suitable order
1683 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001684 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
Joe Perches78ace702011-10-10 01:08:05 -07001685 udf_err(sb, "Primary Volume Descriptor not found!\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001686 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001687 }
Jan Kara7b78fd02018-02-09 15:56:34 +01001688 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
Jan Karad759bfa42013-07-25 19:10:59 +02001689 if (ret < 0)
1690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Jan Kara7b78fd02018-02-09 15:56:34 +01001692 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
Jan Karad759bfa42013-07-25 19:10:59 +02001693 ret = udf_load_logicalvol(sb,
Jan Kara7b78fd02018-02-09 15:56:34 +01001694 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1695 fileset);
Jan Karad759bfa42013-07-25 19:10:59 +02001696 if (ret < 0)
1697 return ret;
1698 }
Marcin Slusarz165923f2008-02-10 11:33:08 +01001699
Jan Kara7b78fd02018-02-09 15:56:34 +01001700 /* Now handle prevailing Partition Descriptors */
Jan Karaee4af502018-08-21 14:52:34 +02001701 for (i = 0; i < data.num_part_descs; i++) {
1702 ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block);
1703 if (ret < 0)
1704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706
1707 return 0;
1708}
1709
Jan Karad759bfa42013-07-25 19:10:59 +02001710/*
1711 * Load Volume Descriptor Sequence described by anchor in bh
1712 *
1713 * Returns <0 on error, 0 on success
1714 */
Jan Kara403460052009-03-19 16:21:38 +01001715static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1716 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Jan Kara403460052009-03-19 16:21:38 +01001718 struct anchorVolDescPtr *anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001719 sector_t main_s, main_e, reserve_s, reserve_e;
1720 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Jan Kara403460052009-03-19 16:21:38 +01001722 anchor = (struct anchorVolDescPtr *)bh->b_data;
1723
1724 /* Locate the main sequence */
1725 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1726 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1727 main_e = main_e >> sb->s_blocksize_bits;
Jan Kara91c9c9e2018-02-08 17:39:01 +01001728 main_e += main_s - 1;
Jan Kara403460052009-03-19 16:21:38 +01001729
1730 /* Locate the reserve sequence */
1731 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1732 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1733 reserve_e = reserve_e >> sb->s_blocksize_bits;
Jan Kara91c9c9e2018-02-08 17:39:01 +01001734 reserve_e += reserve_s - 1;
Jan Kara403460052009-03-19 16:21:38 +01001735
1736 /* Process the main & reserve sequences */
1737 /* responsible for finding the PartitionDesc(s) */
Jan Karad759bfa42013-07-25 19:10:59 +02001738 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1739 if (ret != -EAGAIN)
1740 return ret;
Jan Karabff943a2012-06-27 22:27:05 +02001741 udf_sb_free_partitions(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001742 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1743 if (ret < 0) {
1744 udf_sb_free_partitions(sb);
1745 /* No sequence was OK, return -EIO */
1746 if (ret == -EAGAIN)
1747 ret = -EIO;
1748 }
1749 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
Jan Kara403460052009-03-19 16:21:38 +01001752/*
1753 * Check whether there is an anchor block in the given block and
1754 * load Volume Descriptor Sequence if so.
Jan Karad759bfa42013-07-25 19:10:59 +02001755 *
1756 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1757 * block
Jan Kara403460052009-03-19 16:21:38 +01001758 */
1759static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1760 struct kernel_lb_addr *fileset)
1761{
1762 struct buffer_head *bh;
1763 uint16_t ident;
1764 int ret;
1765
1766 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1767 udf_fixed_to_variable(block) >=
Fabian Frederick23bcda12017-01-06 21:54:41 +01001768 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
Jan Karad759bfa42013-07-25 19:10:59 +02001769 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001770
1771 bh = udf_read_tagged(sb, block, block, &ident);
1772 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001773 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001774 if (ident != TAG_IDENT_AVDP) {
1775 brelse(bh);
Jan Karad759bfa42013-07-25 19:10:59 +02001776 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001777 }
1778 ret = udf_load_sequence(sb, bh, fileset);
1779 brelse(bh);
1780 return ret;
1781}
1782
Jan Karad759bfa42013-07-25 19:10:59 +02001783/*
1784 * Search for an anchor volume descriptor pointer.
1785 *
1786 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1787 * of anchors.
1788 */
1789static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1790 struct kernel_lb_addr *fileset)
Jan Kara403460052009-03-19 16:21:38 +01001791{
1792 sector_t last[6];
1793 int i;
1794 struct udf_sb_info *sbi = UDF_SB(sb);
1795 int last_count = 0;
Jan Karad759bfa42013-07-25 19:10:59 +02001796 int ret;
Jan Kara403460052009-03-19 16:21:38 +01001797
1798 /* First try user provided anchor */
1799 if (sbi->s_anchor) {
Jan Karad759bfa42013-07-25 19:10:59 +02001800 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1801 if (ret != -EAGAIN)
1802 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001803 }
1804 /*
1805 * according to spec, anchor is in either:
1806 * block 256
1807 * lastblock-256
1808 * lastblock
1809 * however, if the disc isn't closed, it could be 512.
1810 */
Jan Karad759bfa42013-07-25 19:10:59 +02001811 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1812 if (ret != -EAGAIN)
1813 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001814 /*
1815 * The trouble is which block is the last one. Drives often misreport
1816 * this so we try various possibilities.
1817 */
Jan Karad759bfa42013-07-25 19:10:59 +02001818 last[last_count++] = *lastblock;
1819 if (*lastblock >= 1)
1820 last[last_count++] = *lastblock - 1;
1821 last[last_count++] = *lastblock + 1;
1822 if (*lastblock >= 2)
1823 last[last_count++] = *lastblock - 2;
1824 if (*lastblock >= 150)
1825 last[last_count++] = *lastblock - 150;
1826 if (*lastblock >= 152)
1827 last[last_count++] = *lastblock - 152;
Jan Kara403460052009-03-19 16:21:38 +01001828
1829 for (i = 0; i < last_count; i++) {
Fabian Frederick23bcda12017-01-06 21:54:41 +01001830 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
Jan Kara403460052009-03-19 16:21:38 +01001831 sb->s_blocksize_bits)
1832 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001833 ret = udf_check_anchor_block(sb, last[i], fileset);
1834 if (ret != -EAGAIN) {
1835 if (!ret)
1836 *lastblock = last[i];
1837 return ret;
1838 }
Jan Kara403460052009-03-19 16:21:38 +01001839 if (last[i] < 256)
1840 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001841 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1842 if (ret != -EAGAIN) {
1843 if (!ret)
1844 *lastblock = last[i];
1845 return ret;
1846 }
Jan Kara403460052009-03-19 16:21:38 +01001847 }
1848
1849 /* Finally try block 512 in case media is open */
Jan Karad759bfa42013-07-25 19:10:59 +02001850 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
Jan Kara403460052009-03-19 16:21:38 +01001851}
1852
1853/*
1854 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1855 * area specified by it. The function expects sbi->s_lastblock to be the last
1856 * block on the media.
1857 *
Jan Karad759bfa42013-07-25 19:10:59 +02001858 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1859 * was not found.
Jan Kara403460052009-03-19 16:21:38 +01001860 */
1861static int udf_find_anchor(struct super_block *sb,
1862 struct kernel_lb_addr *fileset)
1863{
Jan Kara403460052009-03-19 16:21:38 +01001864 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001865 sector_t lastblock = sbi->s_last_block;
1866 int ret;
Jan Kara403460052009-03-19 16:21:38 +01001867
Jan Karad759bfa42013-07-25 19:10:59 +02001868 ret = udf_scan_anchors(sb, &lastblock, fileset);
1869 if (ret != -EAGAIN)
Jan Kara403460052009-03-19 16:21:38 +01001870 goto out;
1871
1872 /* No anchor found? Try VARCONV conversion of block numbers */
1873 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Karad759bfa42013-07-25 19:10:59 +02001874 lastblock = udf_variable_to_fixed(sbi->s_last_block);
Jan Kara403460052009-03-19 16:21:38 +01001875 /* Firstly, we try to not convert number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001876 ret = udf_scan_anchors(sb, &lastblock, fileset);
1877 if (ret != -EAGAIN)
Jan Kara403460052009-03-19 16:21:38 +01001878 goto out;
1879
Jan Karad759bfa42013-07-25 19:10:59 +02001880 lastblock = sbi->s_last_block;
Jan Kara403460052009-03-19 16:21:38 +01001881 /* Secondly, we try with converted number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001882 ret = udf_scan_anchors(sb, &lastblock, fileset);
1883 if (ret < 0) {
Jan Kara403460052009-03-19 16:21:38 +01001884 /* VARCONV didn't help. Clear it. */
1885 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara403460052009-03-19 16:21:38 +01001886 }
1887out:
Jan Karad759bfa42013-07-25 19:10:59 +02001888 if (ret == 0)
1889 sbi->s_last_block = lastblock;
1890 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001891}
1892
1893/*
1894 * Check Volume Structure Descriptor, find Anchor block and load Volume
Jan Karad759bfa42013-07-25 19:10:59 +02001895 * Descriptor Sequence.
1896 *
1897 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1898 * block was not found.
Jan Kara403460052009-03-19 16:21:38 +01001899 */
1900static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1901 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001902{
1903 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara403460052009-03-19 16:21:38 +01001904 loff_t nsr_off;
Jan Karad759bfa42013-07-25 19:10:59 +02001905 int ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001906
1907 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1908 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001909 udf_warn(sb, "Bad block size\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001910 return -EINVAL;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001911 }
1912 sbi->s_last_block = uopt->lastblock;
Jan Kara403460052009-03-19 16:21:38 +01001913 if (!uopt->novrs) {
1914 /* Check that it is NSR02 compliant */
1915 nsr_off = udf_check_vsd(sb);
1916 if (!nsr_off) {
1917 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001918 udf_warn(sb, "No VRS found\n");
Fabian Frederick70f16ce2017-01-18 19:39:35 +01001919 return -EINVAL;
Jan Kara403460052009-03-19 16:21:38 +01001920 }
1921 if (nsr_off == -1)
Peter A. Felvegi44499602013-10-18 20:07:44 +02001922 udf_debug("Failed to read sector at offset %d. "
1923 "Assuming open disc. Skipping validity "
1924 "check\n", VSD_FIRST_SECTOR_OFFSET);
Jan Kara403460052009-03-19 16:21:38 +01001925 if (!sbi->s_last_block)
1926 sbi->s_last_block = udf_get_last_block(sb);
1927 } else {
1928 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001929 }
Jan Kara403460052009-03-19 16:21:38 +01001930
1931 /* Look for anchor block and load Volume Descriptor Sequence */
1932 sbi->s_anchor = uopt->anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001933 ret = udf_find_anchor(sb, fileset);
1934 if (ret < 0) {
1935 if (!silent && ret == -EAGAIN)
Joe Perches78ace702011-10-10 01:08:05 -07001936 udf_warn(sb, "No anchor found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001937 return ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001938 }
Jan Karad759bfa42013-07-25 19:10:59 +02001939 return 0;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static void udf_open_lvid(struct super_block *sb)
1943{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001944 struct udf_sb_info *sbi = UDF_SB(sb);
1945 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001946 struct logicalVolIntegrityDesc *lvid;
1947 struct logicalVolIntegrityDescImpUse *lvidiu;
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001948 struct timespec64 ts;
Jan Kara146bca72009-03-16 18:27:37 +01001949
Marcin Slusarz165923f2008-02-10 11:33:08 +01001950 if (!bh)
1951 return;
Jan Kara69d75672013-09-12 22:00:15 +02001952 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1953 lvidiu = udf_sb_lvidiu(sb);
1954 if (!lvidiu)
1955 return;
Jan Kara949f4a72010-10-20 18:49:20 +02001956
1957 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001958 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1959 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001960 ktime_get_real_ts64(&ts);
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08001961 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
Jan Karab72e6322018-02-27 18:55:31 +01001962 if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE)
1963 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
1964 else
1965 UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Marcin Slusarz165923f2008-02-10 11:33:08 +01001967 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001968 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02001969 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01001970
1971 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
1972 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01001973 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02001974 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c9712013-01-17 22:11:38 +01001975 /* Make opening of filesystem visible on the media immediately */
1976 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
1979static void udf_close_lvid(struct super_block *sb)
1980{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001981 struct udf_sb_info *sbi = UDF_SB(sb);
1982 struct buffer_head *bh = sbi->s_lvid_bh;
1983 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001984 struct logicalVolIntegrityDescImpUse *lvidiu;
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001985 struct timespec64 ts;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001986
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001987 if (!bh)
1988 return;
Jan Kara69d75672013-09-12 22:00:15 +02001989 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
1990 lvidiu = udf_sb_lvidiu(sb);
1991 if (!lvidiu)
1992 return;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001993
Jan Kara949f4a72010-10-20 18:49:20 +02001994 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001995 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1996 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Arnd Bergmannc3b9cec2018-06-20 10:15:13 +02001997 ktime_get_real_ts64(&ts);
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08001998 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
Marcin Slusarz165923f2008-02-10 11:33:08 +01001999 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2000 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2001 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2002 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2003 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2004 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
Jan Karab72e6322018-02-27 18:55:31 +01002005 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT))
2006 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Marcin Slusarz165923f2008-02-10 11:33:08 +01002008 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002009 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02002010 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01002011
2012 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Jan Kara853a0c22011-12-23 11:53:07 +01002013 /*
2014 * We set buffer uptodate unconditionally here to avoid spurious
2015 * warnings from mark_buffer_dirty() when previous EIO has marked
2016 * the buffer as !uptodate
2017 */
2018 set_buffer_uptodate(bh);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002019 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01002020 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02002021 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c9712013-01-17 22:11:38 +01002022 /* Make closing of filesystem visible on the media immediately */
2023 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
2025
Jan Karad664b6a2010-10-20 18:28:46 +02002026u64 lvid_get_unique_id(struct super_block *sb)
2027{
2028 struct buffer_head *bh;
2029 struct udf_sb_info *sbi = UDF_SB(sb);
2030 struct logicalVolIntegrityDesc *lvid;
2031 struct logicalVolHeaderDesc *lvhd;
2032 u64 uniqueID;
2033 u64 ret;
2034
2035 bh = sbi->s_lvid_bh;
2036 if (!bh)
2037 return 0;
2038
2039 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2040 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2041
2042 mutex_lock(&sbi->s_alloc_mutex);
2043 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2044 if (!(++uniqueID & 0xFFFFFFFF))
2045 uniqueID += 16;
2046 lvhd->uniqueID = cpu_to_le64(uniqueID);
2047 mutex_unlock(&sbi->s_alloc_mutex);
2048 mark_buffer_dirty(bh);
2049
2050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053static int udf_fill_super(struct super_block *sb, void *options, int silent)
2054{
Jan Karad759bfa42013-07-25 19:10:59 +02002055 int ret = -EINVAL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002056 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002058 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 struct udf_sb_info *sbi;
Jan Kara9181f8b2015-08-20 14:50:07 +02002060 bool lvid_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
Jan Kara116e5252018-02-22 10:39:52 +01002063 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2064 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2065 uopt.gid = make_kgid(current_user_ns(), overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01002067 uopt.fmode = UDF_INVALID_MODE;
2068 uopt.dmode = UDF_INVALID_MODE;
Chengguang Xu785dffe2018-02-25 21:25:07 +08002069 uopt.nls_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Markus Elfring033c9da2017-08-15 16:45:44 +02002071 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Alessio Igor Bogani9db9f9e2010-11-16 18:40:49 +01002072 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Ingo Molnar1e7933d2006-03-23 03:00:44 -08002077 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Miklos Szeredi6da80892008-02-08 04:21:50 -08002079 if (!udf_parse_options((char *)options, &uopt, false))
Markus Elfringfdf26572014-11-18 18:29:10 +01002080 goto parse_options_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002083 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Joe Perches8076c362011-10-10 01:08:03 -07002084 udf_err(sb, "utf8 cannot be combined with iocharset\n");
Markus Elfringfdf26572014-11-18 18:29:10 +01002085 goto parse_options_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002087 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 uopt.nls_map = load_nls_default();
2089 if (!uopt.nls_map)
2090 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2091 else
2092 udf_debug("Using default NLS map\n");
2093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2095 uopt.flags |= (1 << UDF_FLAG_UTF8);
2096
2097 fileset.logicalBlockNum = 0xFFFFFFFF;
2098 fileset.partitionReferenceNum = 0xFFFF;
2099
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002100 sbi->s_flags = uopt.flags;
2101 sbi->s_uid = uopt.uid;
2102 sbi->s_gid = uopt.gid;
2103 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01002104 sbi->s_fmode = uopt.fmode;
2105 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002106 sbi->s_nls_map = uopt.nls_map;
Jan Karac03cad22010-10-20 22:17:28 +02002107 rwlock_init(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002109 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002110 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002112 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002114 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /* Fill in the rest of the superblock */
2117 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02002118 sb->s_export_op = &udf_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 sb->s_magic = UDF_SUPER_MAGIC;
2121 sb->s_time_gran = 1000;
2122
Jan Kara403460052009-03-19 16:21:38 +01002123 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2124 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2125 } else {
Martin K. Petersene1defc42009-05-22 17:17:49 -04002126 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002127 while (uopt.blocksize <= 4096) {
Jan Kara403460052009-03-19 16:21:38 +01002128 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002129 if (ret < 0) {
2130 if (!silent && ret != -EACCES) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002131 pr_notice("Scanning with blocksize %u failed\n",
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002132 uopt.blocksize);
2133 }
2134 brelse(sbi->s_lvid_bh);
2135 sbi->s_lvid_bh = NULL;
2136 /*
2137 * EACCES is special - we want to propagate to
2138 * upper layers that we cannot handle RW mount.
2139 */
2140 if (ret == -EACCES)
2141 break;
2142 } else
2143 break;
2144
2145 uopt.blocksize <<= 1;
Jan Kara403460052009-03-19 16:21:38 +01002146 }
2147 }
Jan Karad759bfa42013-07-25 19:10:59 +02002148 if (ret < 0) {
2149 if (ret == -EAGAIN) {
2150 udf_warn(sb, "No partition found (1)\n");
2151 ret = -EINVAL;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 goto error_out;
2154 }
2155
Steve Magnanifcbf7632017-10-12 08:48:41 -05002156 udf_debug("Lastblock=%u\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002158 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002159 struct logicalVolIntegrityDescImpUse *lvidiu =
Jan Kara69d75672013-09-12 22:00:15 +02002160 udf_sb_lvidiu(sb);
2161 uint16_t minUDFReadRev;
2162 uint16_t minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Jan Kara69d75672013-09-12 22:00:15 +02002164 if (!lvidiu) {
2165 ret = -EINVAL;
2166 goto error_out;
2167 }
2168 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2169 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002170 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Joe Perches78ace702011-10-10 01:08:05 -07002171 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
Jan Kara69d75672013-09-12 22:00:15 +02002172 minUDFReadRev,
Joe Perches78ace702011-10-10 01:08:05 -07002173 UDF_MAX_READ_VERSION);
Jan Karad759bfa42013-07-25 19:10:59 +02002174 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto error_out;
Jan Karaa9ad01b2018-09-06 15:56:10 +02002176 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) {
2177 if (!sb_rdonly(sb)) {
2178 ret = -EACCES;
2179 goto error_out;
2180 }
2181 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
Jan Karae729eac2013-07-25 16:15:16 +02002182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002184 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2187 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2188 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2189 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2190 }
2191
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002192 if (!sbi->s_partitions) {
Joe Perches78ace702011-10-10 01:08:05 -07002193 udf_warn(sb, "No partition found (2)\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002194 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto error_out;
2196 }
2197
Marcin Slusarz4b111112008-02-08 04:20:36 -08002198 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
Jan Karaa9ad01b2018-09-06 15:56:10 +02002199 UDF_PART_FLAG_READ_ONLY) {
2200 if (!sb_rdonly(sb)) {
2201 ret = -EACCES;
2202 goto error_out;
2203 }
2204 UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT);
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002205 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002206
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002207 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Joe Perches78ace702011-10-10 01:08:05 -07002208 udf_warn(sb, "No fileset found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002209 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto error_out;
2211 }
2212
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002213 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002214 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002215 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Joe Perches78ace702011-10-10 01:08:05 -07002216 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2217 sbi->s_volume_ident,
2218 le16_to_cpu(ts.year), ts.month, ts.day,
Marcin Slusarz56774802008-02-10 11:25:31 +01002219 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
David Howellsbc98a422017-07-17 08:45:34 +01002221 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 udf_open_lvid(sb);
Jan Kara9181f8b2015-08-20 14:50:07 +02002223 lvid_open = true;
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* Assign the root inode */
2227 /* assign inodes by physical block number */
2228 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002229 inode = udf_iget(sb, &rootdir);
Jan Kara6d3d5e82014-09-04 16:15:51 +02002230 if (IS_ERR(inode)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002231 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002232 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Jan Kara6d3d5e82014-09-04 16:15:51 +02002233 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto error_out;
2235 }
2236
2237 /* Allocate a dentry for the root inode */
Al Viro48fde702012-01-08 22:15:13 -05002238 sb->s_root = d_make_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002239 if (!sb->s_root) {
Joe Perches78ace702011-10-10 01:08:05 -07002240 udf_err(sb, "Couldn't allocate root dentry\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002241 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 goto error_out;
2243 }
Jan Kara31170b62007-05-08 00:35:21 -07002244 sb->s_maxbytes = MAX_LFS_FILESIZE;
Al Viro8de52772012-02-06 12:45:27 -05002245 sb->s_max_links = UDF_MAX_LINKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
2247
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002248error_out:
Markus Elfring0d454e42014-11-18 17:17:46 +01002249 iput(sbi->s_vat_inode);
Markus Elfringfdf26572014-11-18 18:29:10 +01002250parse_options_failure:
Chengguang Xu785dffe2018-02-25 21:25:07 +08002251 if (uopt.nls_map)
2252 unload_nls(uopt.nls_map);
Jan Kara9181f8b2015-08-20 14:50:07 +02002253 if (lvid_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002255 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002256 udf_sb_free_partitions(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 kfree(sbi);
2258 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002259
Jan Karad759bfa42013-07-25 19:10:59 +02002260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Joe Perches8076c362011-10-10 01:08:03 -07002263void _udf_err(struct super_block *sb, const char *function,
2264 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Joe Perchesc2bff362011-10-10 01:08:06 -07002266 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 va_list args;
2268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002270
2271 vaf.fmt = fmt;
2272 vaf.va = &args;
2273
2274 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
Joe Perchesa40ecd72011-10-10 01:08:04 -07002279void _udf_warn(struct super_block *sb, const char *function,
2280 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Joe Perchesc2bff362011-10-10 01:08:06 -07002282 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 va_list args;
2284
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002285 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002286
2287 vaf.fmt = fmt;
2288 vaf.va = &args;
2289
2290 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002295static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002297 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002299 sbi = UDF_SB(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002300
Markus Elfring0d454e42014-11-18 17:17:46 +01002301 iput(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002303 unload_nls(sbi->s_nls_map);
David Howellsbc98a422017-07-17 08:45:34 +01002304 if (!sb_rdonly(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002306 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002307 udf_sb_free_partitions(sb);
Fabian Frederickbbe48dd2015-01-10 19:13:32 +01002308 mutex_destroy(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 kfree(sb->s_fs_info);
2310 sb->s_fs_info = NULL;
2311}
2312
Jan Kara146bca72009-03-16 18:27:37 +01002313static int udf_sync_fs(struct super_block *sb, int wait)
2314{
2315 struct udf_sb_info *sbi = UDF_SB(sb);
2316
2317 mutex_lock(&sbi->s_alloc_mutex);
2318 if (sbi->s_lvid_dirty) {
2319 /*
2320 * Blockdevice will be synced later so we don't have to submit
2321 * the buffer for IO
2322 */
2323 mark_buffer_dirty(sbi->s_lvid_bh);
Jan Kara146bca72009-03-16 18:27:37 +01002324 sbi->s_lvid_dirty = 0;
2325 }
2326 mutex_unlock(&sbi->s_alloc_mutex);
2327
2328 return 0;
2329}
2330
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002331static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
David Howells726c3342006-06-23 02:02:58 -07002333 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002334 struct udf_sb_info *sbi = UDF_SB(sb);
2335 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002336 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002337
Jan Kara69d75672013-09-12 22:00:15 +02002338 lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 buf->f_type = UDF_SUPER_MAGIC;
2340 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002341 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 buf->f_bfree = udf_count_free(sb);
2343 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002344 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2345 le32_to_cpu(lvidiu->numDirs)) : 0)
2346 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 buf->f_ffree = buf->f_bfree;
Andrew Gabbasov9fba7052016-01-15 02:44:21 -06002348 buf->f_namelen = UDF_NAME_LEN;
Coly Li557f5a12009-01-20 01:36:55 +08002349 buf->f_fsid.val[0] = (u32)id;
2350 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 return 0;
2353}
2354
Marcin Slusarz4b111112008-02-08 04:20:36 -08002355static unsigned int udf_count_free_bitmap(struct super_block *sb,
2356 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 struct buffer_head *bh = NULL;
2359 unsigned int accum = 0;
2360 int index;
Steve Magnanib490bdd2017-10-12 08:48:40 -05002361 udf_pblk_t block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002362 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 uint8_t *ptr;
2365 uint16_t ident;
2366 struct spaceBitmapDesc *bm;
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002369 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002370 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002372 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07002373 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002375 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002376 brelse(bh);
Joe Perches78ace702011-10-10 01:08:05 -07002377 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto out;
2379 }
2380
2381 bm = (struct spaceBitmapDesc *)bh->b_data;
2382 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002383 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2384 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002386 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002387 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2388 accum += bitmap_weight((const unsigned long *)(ptr + index),
2389 cur_bytes * 8);
2390 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002391 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002392 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002393 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002395 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 udf_debug("read failed\n");
2397 goto out;
2398 }
2399 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002400 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002403 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return accum;
2406}
2407
Marcin Slusarz4b111112008-02-08 04:20:36 -08002408static unsigned int udf_count_free_table(struct super_block *sb,
2409 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
2411 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002412 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002413 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002415 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Jan Karad1668fe2010-10-20 23:24:12 +02002417 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002418 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002419 epos.offset = sizeof(struct unallocSpaceEntry);
2420 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002422 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002424
Jan Kara3bf25cb2007-05-08 00:35:16 -07002425 brelse(epos.bh);
Jan Karad1668fe2010-10-20 23:24:12 +02002426 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 return accum;
2429}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002430
2431static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002434 struct udf_sb_info *sbi;
2435 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002437 sbi = UDF_SB(sb);
2438 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002439 struct logicalVolIntegrityDesc *lvid =
2440 (struct logicalVolIntegrityDesc *)
2441 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002442 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002443 accum = le32_to_cpu(
2444 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 if (accum == 0xFFFFFFFF)
2446 accum = 0;
2447 }
2448 }
2449
2450 if (accum)
2451 return accum;
2452
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002453 map = &sbi->s_partmaps[sbi->s_partition];
2454 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002455 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002456 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (accum)
2459 return accum;
2460
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002461 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002462 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002463 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return accum;
2466}
Fabian Frederick54bb60d2017-01-06 21:53:57 +01002467
2468MODULE_AUTHOR("Ben Fennema");
2469MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2470MODULE_LICENSE("GPL");
2471module_init(init_udf_fs)
2472module_exit(exit_udf_fs)