blob: 31d8ce13fa1878549d3e08ce7952edb0dd3642ff [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 Viro3aac2b622010-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;
225 unsigned int volume;
226 unsigned short partition;
227 unsigned int fileset;
228 unsigned int rootdir;
229 unsigned int flags;
Al Virofaa17292011-07-26 03:18:29 -0400230 umode_t umask;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800231 kgid_t gid;
232 kuid_t uid;
Al Virofaa17292011-07-26 03:18:29 -0400233 umode_t fmode;
234 umode_t dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct nls_table *nls_map;
236};
237
238static int __init init_udf_fs(void)
239{
240 int err;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 err = init_inodecache();
243 if (err)
244 goto out1;
245 err = register_filesystem(&udf_fstype);
246 if (err)
247 goto out;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700250
251out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 destroy_inodecache();
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700253
254out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return err;
256}
257
258static void __exit exit_udf_fs(void)
259{
260 unregister_filesystem(&udf_fstype);
261 destroy_inodecache();
262}
263
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800264static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count)
265{
266 struct udf_sb_info *sbi = UDF_SB(sb);
267
Markus Elfring033c9da2017-08-15 16:45:44 +0200268 sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL);
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800269 if (!sbi->s_partmaps) {
Marcin Slusarzdc5d39b2008-02-08 04:20:32 -0800270 sbi->s_partitions = 0;
271 return -ENOMEM;
272 }
273
274 sbi->s_partitions = count;
275 return 0;
276}
277
Jan Karabff943a2012-06-27 22:27:05 +0200278static void udf_sb_free_bitmap(struct udf_bitmap *bitmap)
279{
280 int i;
281 int nr_groups = bitmap->s_nr_groups;
Jan Karabff943a2012-06-27 22:27:05 +0200282
283 for (i = 0; i < nr_groups; i++)
284 if (bitmap->s_block_bitmap[i])
285 brelse(bitmap->s_block_bitmap[i]);
286
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -0800287 kvfree(bitmap);
Jan Karabff943a2012-06-27 22:27:05 +0200288}
289
290static void udf_free_partition(struct udf_part_map *map)
291{
292 int i;
293 struct udf_meta_data *mdata;
294
295 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE)
296 iput(map->s_uspace.s_table);
297 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE)
298 iput(map->s_fspace.s_table);
299 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP)
300 udf_sb_free_bitmap(map->s_uspace.s_bitmap);
301 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP)
302 udf_sb_free_bitmap(map->s_fspace.s_bitmap);
303 if (map->s_partition_type == UDF_SPARABLE_MAP15)
304 for (i = 0; i < 4; i++)
305 brelse(map->s_type_specific.s_sparing.s_spar_map[i]);
306 else if (map->s_partition_type == UDF_METADATA_MAP25) {
307 mdata = &map->s_type_specific.s_metadata;
308 iput(mdata->s_metadata_fe);
309 mdata->s_metadata_fe = NULL;
310
311 iput(mdata->s_mirror_fe);
312 mdata->s_mirror_fe = NULL;
313
314 iput(mdata->s_bitmap_fe);
315 mdata->s_bitmap_fe = NULL;
316 }
317}
318
319static void udf_sb_free_partitions(struct super_block *sb)
320{
321 struct udf_sb_info *sbi = UDF_SB(sb);
322 int i;
Markus Elfringba2eb862017-08-15 17:03:06 +0200323
324 if (!sbi->s_partmaps)
Namjae Jeon1b1baff2013-01-14 22:53:47 +0100325 return;
Jan Karabff943a2012-06-27 22:27:05 +0200326 for (i = 0; i < sbi->s_partitions; i++)
327 udf_free_partition(&sbi->s_partmaps[i]);
328 kfree(sbi->s_partmaps);
329 sbi->s_partmaps = NULL;
330}
331
Al Viro34c80b12011-12-08 21:32:45 -0500332static int udf_show_options(struct seq_file *seq, struct dentry *root)
Miklos Szeredi6da80892008-02-08 04:21:50 -0800333{
Al Viro34c80b12011-12-08 21:32:45 -0500334 struct super_block *sb = root->d_sb;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800335 struct udf_sb_info *sbi = UDF_SB(sb);
336
337 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT))
338 seq_puts(seq, ",nostrict");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100339 if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET))
Miklos Szeredi6da80892008-02-08 04:21:50 -0800340 seq_printf(seq, ",bs=%lu", sb->s_blocksize);
341 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE))
342 seq_puts(seq, ",unhide");
343 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE))
344 seq_puts(seq, ",undelete");
345 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB))
346 seq_puts(seq, ",noadinicb");
347 if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD))
348 seq_puts(seq, ",shortad");
349 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET))
350 seq_puts(seq, ",uid=forget");
Miklos Szeredi6da80892008-02-08 04:21:50 -0800351 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET))
352 seq_puts(seq, ",gid=forget");
Miklos Szeredi6da80892008-02-08 04:21:50 -0800353 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800354 seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800355 if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET))
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800356 seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid));
Miklos Szeredi6da80892008-02-08 04:21:50 -0800357 if (sbi->s_umask != 0)
Al Virofaa17292011-07-26 03:18:29 -0400358 seq_printf(seq, ",umask=%ho", sbi->s_umask);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100359 if (sbi->s_fmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400360 seq_printf(seq, ",mode=%ho", sbi->s_fmode);
Marcin Slusarz87bc7302008-12-02 13:40:11 +0100361 if (sbi->s_dmode != UDF_INVALID_MODE)
Al Virofaa17292011-07-26 03:18:29 -0400362 seq_printf(seq, ",dmode=%ho", sbi->s_dmode);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800363 if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET))
Steve Magnanifcbf7632017-10-12 08:48:41 -0500364 seq_printf(seq, ",session=%d", sbi->s_session);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800365 if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET))
366 seq_printf(seq, ",lastblock=%u", sbi->s_last_block);
Jan Kara403460052009-03-19 16:21:38 +0100367 if (sbi->s_anchor != 0)
368 seq_printf(seq, ",anchor=%u", sbi->s_anchor);
Miklos Szeredi6da80892008-02-08 04:21:50 -0800369 /*
370 * volume, partition, fileset and rootdir seem to be ignored
371 * currently
372 */
373 if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8))
374 seq_puts(seq, ",utf8");
375 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map)
376 seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset);
377
378 return 0;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * udf_parse_options
383 *
384 * PURPOSE
385 * Parse mount options.
386 *
387 * DESCRIPTION
388 * The following mount options are supported:
389 *
390 * gid= Set the default group.
391 * umask= Set the default umask.
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100392 * mode= Set the default file permissions.
393 * dmode= Set the default directory permissions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * uid= Set the default user.
395 * bs= Set the block size.
396 * unhide Show otherwise hidden files.
397 * undelete Show deleted files in lists.
398 * adinicb Embed data in the inode (default)
399 * noadinicb Don't embed data in the inode
400 * shortad Use short ad's
401 * longad Use long ad's (default)
402 * nostrict Unset strict conformance
403 * iocharset= Set the NLS character set
404 *
405 * The remaining are for debugging and disaster recovery:
406 *
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700407 * novrs Skip volume sequence recognition
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 *
409 * The following expect a offset from 0.
410 *
411 * session= Set the CDROM session (default= last session)
412 * anchor= Override standard anchor location. (default= 256)
413 * volume= Override the VolumeDesc location. (unused)
414 * partition= Override the PartitionDesc location. (unused)
415 * lastblock= Set the last block of the filesystem/
416 *
417 * The following expect a offset from the partition root.
418 *
419 * fileset= Override the fileset block location. (unused)
420 * rootdir= Override the root directory location. (unused)
421 * WARNING: overriding the rootdir to a non-directory may
422 * yield highly unpredictable results.
423 *
424 * PRE-CONDITIONS
425 * options Pointer to mount options string.
426 * uopts Pointer to mount options variable.
427 *
428 * POST-CONDITIONS
429 * <return> 1 Mount options parsed okay.
430 * <return> 0 Error parsing mount options.
431 *
432 * HISTORY
433 * July 1, 1997 - Andrew E. Mileski
434 * Written, tested, and released.
435 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437enum {
438 Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete,
439 Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad,
440 Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock,
441 Opt_anchor, Opt_volume, Opt_partition, Opt_fileset,
442 Opt_rootdir, Opt_utf8, Opt_iocharset,
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100443 Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore,
444 Opt_fmode, Opt_dmode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Steven Whitehousea447c092008-10-13 10:46:57 +0100447static const match_table_t tokens = {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700448 {Opt_novrs, "novrs"},
449 {Opt_nostrict, "nostrict"},
450 {Opt_bs, "bs=%u"},
451 {Opt_unhide, "unhide"},
452 {Opt_undelete, "undelete"},
453 {Opt_noadinicb, "noadinicb"},
454 {Opt_adinicb, "adinicb"},
455 {Opt_shortad, "shortad"},
456 {Opt_longad, "longad"},
457 {Opt_uforget, "uid=forget"},
458 {Opt_uignore, "uid=ignore"},
459 {Opt_gforget, "gid=forget"},
460 {Opt_gignore, "gid=ignore"},
461 {Opt_gid, "gid=%u"},
462 {Opt_uid, "uid=%u"},
463 {Opt_umask, "umask=%o"},
464 {Opt_session, "session=%u"},
465 {Opt_lastblock, "lastblock=%u"},
466 {Opt_anchor, "anchor=%u"},
467 {Opt_volume, "volume=%u"},
468 {Opt_partition, "partition=%u"},
469 {Opt_fileset, "fileset=%u"},
470 {Opt_rootdir, "rootdir=%u"},
471 {Opt_utf8, "utf8"},
472 {Opt_iocharset, "iocharset=%s"},
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100473 {Opt_fmode, "mode=%o"},
474 {Opt_dmode, "dmode=%o"},
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700475 {Opt_err, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
477
Miklos Szeredi6da80892008-02-08 04:21:50 -0800478static int udf_parse_options(char *options, struct udf_options *uopt,
479 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 char *p;
482 int option;
483
484 uopt->novrs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 uopt->partition = 0xFFFF;
486 uopt->session = 0xFFFFFFFF;
487 uopt->lastblock = 0;
488 uopt->anchor = 0;
489 uopt->volume = 0xFFFFFFFF;
490 uopt->rootdir = 0xFFFFFFFF;
491 uopt->fileset = 0xFFFFFFFF;
492 uopt->nls_map = NULL;
493
494 if (!options)
495 return 1;
496
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700497 while ((p = strsep(&options, ",")) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 substring_t args[MAX_OPT_ARGS];
499 int token;
Fabian Frederick8c6915a2014-01-29 17:13:16 +0800500 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (!*p)
502 continue;
503
504 token = match_token(p, tokens, args);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700505 switch (token) {
506 case Opt_novrs:
507 uopt->novrs = 1;
Clemens Ladisch41368012009-03-06 09:16:49 +0100508 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700509 case Opt_bs:
510 if (match_int(&args[0], &option))
511 return 0;
Fabian Frederick8c6915a2014-01-29 17:13:16 +0800512 n = option;
513 if (n != 512 && n != 1024 && n != 2048 && n != 4096)
514 return 0;
515 uopt->blocksize = n;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +0100516 uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700517 break;
518 case Opt_unhide:
519 uopt->flags |= (1 << UDF_FLAG_UNHIDE);
520 break;
521 case Opt_undelete:
522 uopt->flags |= (1 << UDF_FLAG_UNDELETE);
523 break;
524 case Opt_noadinicb:
525 uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB);
526 break;
527 case Opt_adinicb:
528 uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB);
529 break;
530 case Opt_shortad:
531 uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD);
532 break;
533 case Opt_longad:
534 uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD);
535 break;
536 case Opt_gid:
537 if (match_int(args, &option))
538 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800539 uopt->gid = make_kgid(current_user_ns(), option);
540 if (!gid_valid(uopt->gid))
541 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700542 uopt->flags |= (1 << UDF_FLAG_GID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700543 break;
544 case Opt_uid:
545 if (match_int(args, &option))
546 return 0;
Eric W. Biedermanc2ba1382012-02-10 12:20:35 -0800547 uopt->uid = make_kuid(current_user_ns(), option);
548 if (!uid_valid(uopt->uid))
549 return 0;
Cyrill Gorcunovca76d2d2007-07-31 00:39:40 -0700550 uopt->flags |= (1 << UDF_FLAG_UID_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700551 break;
552 case Opt_umask:
553 if (match_octal(args, &option))
554 return 0;
555 uopt->umask = option;
556 break;
557 case Opt_nostrict:
558 uopt->flags &= ~(1 << UDF_FLAG_STRICT);
559 break;
560 case Opt_session:
561 if (match_int(args, &option))
562 return 0;
563 uopt->session = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800564 if (!remount)
565 uopt->flags |= (1 << UDF_FLAG_SESSION_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700566 break;
567 case Opt_lastblock:
568 if (match_int(args, &option))
569 return 0;
570 uopt->lastblock = option;
Miklos Szeredi6da80892008-02-08 04:21:50 -0800571 if (!remount)
572 uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700573 break;
574 case Opt_anchor:
575 if (match_int(args, &option))
576 return 0;
577 uopt->anchor = option;
578 break;
579 case Opt_volume:
580 if (match_int(args, &option))
581 return 0;
582 uopt->volume = option;
583 break;
584 case Opt_partition:
585 if (match_int(args, &option))
586 return 0;
587 uopt->partition = option;
588 break;
589 case Opt_fileset:
590 if (match_int(args, &option))
591 return 0;
592 uopt->fileset = option;
593 break;
594 case Opt_rootdir:
595 if (match_int(args, &option))
596 return 0;
597 uopt->rootdir = option;
598 break;
599 case Opt_utf8:
600 uopt->flags |= (1 << UDF_FLAG_UTF8);
601 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700603 case Opt_iocharset:
604 uopt->nls_map = load_nls(args[0].from);
605 uopt->flags |= (1 << UDF_FLAG_NLS_MAP);
606 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700608 case Opt_uforget:
609 uopt->flags |= (1 << UDF_FLAG_UID_FORGET);
610 break;
Jan Kara70260e42018-02-21 17:27:44 +0100611 case Opt_uignore:
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700612 case Opt_gignore:
Jan Kara70260e42018-02-21 17:27:44 +0100613 /* These options are superseeded by uid=<number> */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700614 break;
615 case Opt_gforget:
616 uopt->flags |= (1 << UDF_FLAG_GID_FORGET);
617 break;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100618 case Opt_fmode:
619 if (match_octal(args, &option))
620 return 0;
621 uopt->fmode = option & 0777;
622 break;
623 case Opt_dmode:
624 if (match_octal(args, &option))
625 return 0;
626 uopt->dmode = option & 0777;
627 break;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700628 default:
Joe Perches78ace702011-10-10 01:08:05 -0700629 pr_err("bad mount option \"%s\" or missing value\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return 0;
631 }
632 }
633 return 1;
634}
635
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700636static int udf_remount_fs(struct super_block *sb, int *flags, char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct udf_options uopt;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800639 struct udf_sb_info *sbi = UDF_SB(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400640 int error = 0;
Jan Kara69d75672013-09-12 22:00:15 +0200641 struct logicalVolIntegrityDescImpUse *lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Theodore Ts'o02b99842014-03-13 10:14:33 -0400643 sync_filesystem(sb);
Jan Kara69d75672013-09-12 22:00:15 +0200644 if (lvidiu) {
645 int write_rev = le16_to_cpu(lvidiu->minUDFWriteRev);
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800646 if (write_rev > UDF_MAX_WRITE_VERSION && !(*flags & SB_RDONLY))
Jan Karae729eac2013-07-25 16:15:16 +0200647 return -EACCES;
648 }
649
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800650 uopt.flags = sbi->s_flags;
651 uopt.uid = sbi->s_uid;
652 uopt.gid = sbi->s_gid;
653 uopt.umask = sbi->s_umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100654 uopt.fmode = sbi->s_fmode;
655 uopt.dmode = sbi->s_dmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Miklos Szeredi6da80892008-02-08 04:21:50 -0800657 if (!udf_parse_options(options, &uopt, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
659
Jan Karac03cad22010-10-20 22:17:28 +0200660 write_lock(&sbi->s_cred_lock);
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800661 sbi->s_flags = uopt.flags;
662 sbi->s_uid = uopt.uid;
663 sbi->s_gid = uopt.gid;
664 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +0100665 sbi->s_fmode = uopt.fmode;
666 sbi->s_dmode = uopt.dmode;
Jan Karac03cad22010-10-20 22:17:28 +0200667 write_unlock(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800669 if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400670 goto out_unlock;
671
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800672 if (*flags & SB_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 udf_close_lvid(sb);
Jan Kara36350462010-05-19 16:28:56 +0200674 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 udf_open_lvid(sb);
676
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400677out_unlock:
Christoph Hellwigc79d9672010-05-19 07:16:40 -0400678 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Jan Kara403460052009-03-19 16:21:38 +0100681/* Check Volume Structure Descriptors (ECMA 167 2/9.1) */
682/* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */
683static loff_t udf_check_vsd(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct volStructDesc *vsd = NULL;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200686 loff_t sector = VSD_FIRST_SECTOR_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int sectorsize;
688 struct buffer_head *bh = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700689 int nsr02 = 0;
690 int nsr03 = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800691 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800693 sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (sb->s_blocksize < sizeof(struct volStructDesc))
695 sectorsize = sizeof(struct volStructDesc);
696 else
697 sectorsize = sb->s_blocksize;
698
Jan Karaabdc0eb2017-10-16 11:38:11 +0200699 sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Steve Magnanifcbf7632017-10-12 08:48:41 -0500701 udf_debug("Starting at sector %u (%lu byte sectors)\n",
Sebastian Manciulea706047a2008-04-14 17:13:01 +0200702 (unsigned int)(sector >> sb->s_blocksize_bits),
703 sb->s_blocksize);
Peter A. Felvegi44499602013-10-18 20:07:44 +0200704 /* Process the sequence (if applicable). The hard limit on the sector
705 * offset is arbitrary, hopefully large enough so that all valid UDF
706 * filesystems will be recognised. There is no mention of an upper
707 * bound to the size of the volume recognition area in the standard.
708 * The limit will prevent the code to read all the sectors of a
709 * specially crafted image (like a bluray disc full of CD001 sectors),
710 * potentially causing minutes or even hours of uninterruptible I/O
711 * activity. This actually happened with uninitialised SSD partitions
712 * (all 0xFF) before the check for the limit and all valid IDs were
713 * added */
714 for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET;
715 sector += sectorsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* Read a block */
717 bh = udf_tread(sb, sector >> sb->s_blocksize_bits);
718 if (!bh)
719 break;
720
721 /* Look for ISO descriptors */
722 vsd = (struct volStructDesc *)(bh->b_data +
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800723 (sector & (sb->s_blocksize - 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Peter A. Felvegi44499602013-10-18 20:07:44 +0200725 if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800726 VSD_STD_ID_LEN)) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700727 switch (vsd->structType) {
728 case 0:
729 udf_debug("ISO9660 Boot Record found\n");
730 break;
731 case 1:
Joe Perchesa983f362011-10-10 01:08:07 -0700732 udf_debug("ISO9660 Primary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700733 break;
734 case 2:
Joe Perchesa983f362011-10-10 01:08:07 -0700735 udf_debug("ISO9660 Supplementary Volume Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700736 break;
737 case 3:
Joe Perchesa983f362011-10-10 01:08:07 -0700738 udf_debug("ISO9660 Volume Partition Descriptor found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700739 break;
740 case 255:
Joe Perchesa983f362011-10-10 01:08:07 -0700741 udf_debug("ISO9660 Volume Descriptor Set Terminator found\n");
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700742 break;
743 default:
744 udf_debug("ISO9660 VRS (%u) found\n",
745 vsd->structType);
746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800748 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01,
749 VSD_STD_ID_LEN))
750 ; /* nothing */
751 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01,
752 VSD_STD_ID_LEN)) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700753 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800755 } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02,
756 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 nsr02 = sector;
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800758 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03,
759 VSD_STD_ID_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 nsr03 = sector;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200761 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2,
762 VSD_STD_ID_LEN))
763 ; /* nothing */
764 else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02,
765 VSD_STD_ID_LEN))
766 ; /* nothing */
767 else {
768 /* invalid id : end of volume recognition area */
769 brelse(bh);
770 break;
771 }
Jan Kara3bf25cb2007-05-08 00:35:16 -0700772 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 if (nsr03)
776 return nsr03;
777 else if (nsr02)
778 return nsr02;
Peter A. Felvegi44499602013-10-18 20:07:44 +0200779 else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) ==
780 VSD_FIRST_SECTOR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return -1;
782 else
783 return 0;
784}
785
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800786static int udf_find_fileset(struct super_block *sb,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200787 struct kernel_lb_addr *fileset,
788 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct buffer_head *bh = NULL;
791 long lastblock;
792 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800793 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700796 fileset->partitionReferenceNum != 0xFFFF) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200797 bh = udf_read_ptagged(sb, fileset, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700799 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700801 } else if (ident != TAG_IDENT_FSD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700802 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 1;
804 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800808 sbi = UDF_SB(sb);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800809 if (!bh) {
810 /* Search backwards through the partitions */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200811 struct kernel_lb_addr newfileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700813/* --> cvg: FIXME - is it reasonable? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700815
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800816 for (newfileset.partitionReferenceNum = sbi->s_partitions - 1;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700817 (newfileset.partitionReferenceNum != 0xFFFF &&
818 fileset->logicalBlockNum == 0xFFFFFFFF &&
819 fileset->partitionReferenceNum == 0xFFFF);
820 newfileset.partitionReferenceNum--) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800821 lastblock = sbi->s_partmaps
822 [newfileset.partitionReferenceNum]
823 .s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 newfileset.logicalBlockNum = 0;
825
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700826 do {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200827 bh = udf_read_ptagged(sb, &newfileset, 0,
Marcin Slusarz3a71fc52008-02-08 04:20:28 -0800828 &ident);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700829 if (!bh) {
830 newfileset.logicalBlockNum++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 continue;
832 }
833
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700834 switch (ident) {
835 case TAG_IDENT_SBD:
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700836 {
837 struct spaceBitmapDesc *sp;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800838 sp = (struct spaceBitmapDesc *)
839 bh->b_data;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700840 newfileset.logicalBlockNum += 1 +
841 ((le32_to_cpu(sp->numOfBytes) +
Marcin Slusarz4b111112008-02-08 04:20:36 -0800842 sizeof(struct spaceBitmapDesc)
843 - 1) >> sb->s_blocksize_bits);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700844 brelse(bh);
845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700847 case TAG_IDENT_FSD:
848 *fileset = newfileset;
849 break;
850 default:
851 newfileset.logicalBlockNum++;
852 brelse(bh);
853 bh = NULL;
854 break;
855 }
856 } while (newfileset.logicalBlockNum < lastblock &&
857 fileset->logicalBlockNum == 0xFFFFFFFF &&
858 fileset->partitionReferenceNum == 0xFFFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
861
862 if ((fileset->logicalBlockNum != 0xFFFFFFFF ||
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700863 fileset->partitionReferenceNum != 0xFFFF) && bh) {
Steve Magnanifcbf7632017-10-12 08:48:41 -0500864 udf_debug("Fileset at block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700865 fileset->logicalBlockNum,
866 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800868 sbi->s_partition = fileset->partitionReferenceNum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 udf_load_fileset(sb, bh, root);
Jan Kara3bf25cb2007-05-08 00:35:16 -0700870 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872 }
873 return 1;
874}
875
Jan Karad759bfa42013-07-25 19:10:59 +0200876/*
877 * Load primary Volume Descriptor Sequence
878 *
879 * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence
880 * should be tried.
881 */
Jan Karac0eb31e2008-04-01 16:50:35 +0200882static int udf_load_pvoldesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 struct primaryVolDesc *pvoldesc;
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600885 uint8_t *outstr;
Jan Karac0eb31e2008-04-01 16:50:35 +0200886 struct buffer_head *bh;
887 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +0200888 int ret = -ENOMEM;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100889
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600890 outstr = kmalloc(128, GFP_NOFS);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100891 if (!outstr)
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600892 return -ENOMEM;
Jan Karac0eb31e2008-04-01 16:50:35 +0200893
894 bh = udf_read_tagged(sb, block, block, &ident);
Jan Karad759bfa42013-07-25 19:10:59 +0200895 if (!bh) {
896 ret = -EAGAIN;
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100897 goto out2;
Jan Karad759bfa42013-07-25 19:10:59 +0200898 }
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100899
Jan Karad759bfa42013-07-25 19:10:59 +0200900 if (ident != TAG_IDENT_PVD) {
901 ret = -EIO;
902 goto out_bh;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 pvoldesc = (struct primaryVolDesc *)bh->b_data;
906
Marcin Slusarz56774802008-02-10 11:25:31 +0100907 if (udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time,
908 pvoldesc->recordingDateAndTime)) {
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100909#ifdef UDFFS_DEBUG
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200910 struct timestamp *ts = &pvoldesc->recordingDateAndTime;
Joe Perchesa983f362011-10-10 01:08:07 -0700911 udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n",
Marcin Slusarzaf15a292008-02-10 11:29:10 +0100912 le16_to_cpu(ts->year), ts->month, ts->day, ts->hour,
913 ts->minute, le16_to_cpu(ts->typeAndTimezone));
914#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500917 ret = udf_dstrCS0toUTF8(outstr, 31, pvoldesc->volIdent, 32);
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600918 if (ret < 0)
919 goto out_bh;
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200920
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600921 strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret);
922 udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Andrew Gabbasovc26f6c62016-04-25 06:19:38 -0500924 ret = udf_dstrCS0toUTF8(outstr, 127, pvoldesc->volSetIdent, 128);
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600925 if (ret < 0)
926 goto out_bh;
Fabian Fredericke9d4cf412015-04-08 21:23:54 +0200927
Andrew Gabbasov9293fcf2016-01-15 02:44:22 -0600928 outstr[ret] = 0;
929 udf_debug("volSetIdent[] = '%s'\n", outstr);
Jan Karac0eb31e2008-04-01 16:50:35 +0200930
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100931 ret = 0;
Jan Karad759bfa42013-07-25 19:10:59 +0200932out_bh:
933 brelse(bh);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100934out2:
935 kfree(outstr);
Marcin Slusarzba9aadd2008-11-16 19:01:44 +0100936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
Namjae Jeon3080a742011-10-23 19:28:32 +0900939struct inode *udf_find_metadata_inode_efe(struct super_block *sb,
Alden Tondettar78888242016-05-18 14:09:19 -0700940 u32 meta_file_loc, u32 partition_ref)
Namjae Jeon3080a742011-10-23 19:28:32 +0900941{
942 struct kernel_lb_addr addr;
943 struct inode *metadata_fe;
944
945 addr.logicalBlockNum = meta_file_loc;
Alden Tondettar78888242016-05-18 14:09:19 -0700946 addr.partitionReferenceNum = partition_ref;
Namjae Jeon3080a742011-10-23 19:28:32 +0900947
Jan Kara6174c2e2014-10-09 12:52:16 +0200948 metadata_fe = udf_iget_special(sb, &addr);
Namjae Jeon3080a742011-10-23 19:28:32 +0900949
Jan Kara6d3d5e82014-09-04 16:15:51 +0200950 if (IS_ERR(metadata_fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900951 udf_warn(sb, "metadata inode efe not found\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +0200952 return metadata_fe;
953 }
954 if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900955 udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n");
956 iput(metadata_fe);
Jan Kara6d3d5e82014-09-04 16:15:51 +0200957 return ERR_PTR(-EIO);
Namjae Jeon3080a742011-10-23 19:28:32 +0900958 }
959
960 return metadata_fe;
961}
962
Alden Tondettar78888242016-05-18 14:09:19 -0700963static int udf_load_metadata_files(struct super_block *sb, int partition,
964 int type1_index)
Jan Karabfb257a2008-04-08 20:37:21 +0200965{
966 struct udf_sb_info *sbi = UDF_SB(sb);
967 struct udf_part_map *map;
968 struct udf_meta_data *mdata;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200969 struct kernel_lb_addr addr;
Jan Kara6d3d5e82014-09-04 16:15:51 +0200970 struct inode *fe;
Jan Karabfb257a2008-04-08 20:37:21 +0200971
972 map = &sbi->s_partmaps[partition];
973 mdata = &map->s_type_specific.s_metadata;
Alden Tondettar78888242016-05-18 14:09:19 -0700974 mdata->s_phys_partition_ref = type1_index;
Jan Karabfb257a2008-04-08 20:37:21 +0200975
976 /* metadata address */
Steve Magnanifcbf7632017-10-12 08:48:41 -0500977 udf_debug("Metadata file location: block = %u part = %u\n",
Alden Tondettar78888242016-05-18 14:09:19 -0700978 mdata->s_meta_file_loc, mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200979
Jan Kara6d3d5e82014-09-04 16:15:51 +0200980 fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc,
Alden Tondettar78888242016-05-18 14:09:19 -0700981 mdata->s_phys_partition_ref);
Jan Kara6d3d5e82014-09-04 16:15:51 +0200982 if (IS_ERR(fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900983 /* mirror file entry */
Steve Magnanifcbf7632017-10-12 08:48:41 -0500984 udf_debug("Mirror metadata file location: block = %u part = %u\n",
Alden Tondettar78888242016-05-18 14:09:19 -0700985 mdata->s_mirror_file_loc, mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200986
Jan Kara6d3d5e82014-09-04 16:15:51 +0200987 fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc,
Alden Tondettar78888242016-05-18 14:09:19 -0700988 mdata->s_phys_partition_ref);
Jan Karabfb257a2008-04-08 20:37:21 +0200989
Jan Kara6d3d5e82014-09-04 16:15:51 +0200990 if (IS_ERR(fe)) {
Namjae Jeon3080a742011-10-23 19:28:32 +0900991 udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +0200992 return PTR_ERR(fe);
Namjae Jeon3080a742011-10-23 19:28:32 +0900993 }
Jan Kara6d3d5e82014-09-04 16:15:51 +0200994 mdata->s_mirror_fe = fe;
995 } else
996 mdata->s_metadata_fe = fe;
997
Jan Karabfb257a2008-04-08 20:37:21 +0200998
999 /*
1000 * bitmap file entry
1001 * Note:
1002 * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102)
1003 */
1004 if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) {
1005 addr.logicalBlockNum = mdata->s_bitmap_file_loc;
Alden Tondettar78888242016-05-18 14:09:19 -07001006 addr.partitionReferenceNum = mdata->s_phys_partition_ref;
Jan Karabfb257a2008-04-08 20:37:21 +02001007
Steve Magnanifcbf7632017-10-12 08:48:41 -05001008 udf_debug("Bitmap file location: block = %u part = %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001009 addr.logicalBlockNum, addr.partitionReferenceNum);
Jan Karabfb257a2008-04-08 20:37:21 +02001010
Jan Kara6174c2e2014-10-09 12:52:16 +02001011 fe = udf_iget_special(sb, &addr);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001012 if (IS_ERR(fe)) {
David Howellsbc98a422017-07-17 08:45:34 +01001013 if (sb_rdonly(sb))
Joe Perchesa40ecd72011-10-10 01:08:04 -07001014 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 +02001015 else {
Joe Perches8076c362011-10-10 01:08:03 -07001016 udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n");
Jan Kara6d3d5e82014-09-04 16:15:51 +02001017 return PTR_ERR(fe);
Jan Karabfb257a2008-04-08 20:37:21 +02001018 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001019 } else
1020 mdata->s_bitmap_fe = fe;
Jan Karabfb257a2008-04-08 20:37:21 +02001021 }
1022
1023 udf_debug("udf_load_metadata_files Ok\n");
Jan Karabfb257a2008-04-08 20:37:21 +02001024 return 0;
Jan Karabfb257a2008-04-08 20:37:21 +02001025}
1026
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001027static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001028 struct kernel_lb_addr *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct fileSetDesc *fset;
1031
1032 fset = (struct fileSetDesc *)bh->b_data;
1033
1034 *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation);
1035
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001036 UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Steve Magnanifcbf7632017-10-12 08:48:41 -05001038 udf_debug("Rootdir at block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001039 root->logicalBlockNum, root->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001042int udf_compute_nr_groups(struct super_block *sb, u32 partition)
1043{
1044 struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition];
Julia Lawall8dee00b2008-02-14 16:15:45 +01001045 return DIV_ROUND_UP(map->s_partition_len +
1046 (sizeof(struct spaceBitmapDesc) << 3),
1047 sb->s_blocksize * 8);
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001048}
1049
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001050static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index)
1051{
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001052 struct udf_bitmap *bitmap;
1053 int nr_groups;
1054 int size;
1055
Marcin Slusarz883cb9d2008-02-08 04:20:34 -08001056 nr_groups = udf_compute_nr_groups(sb, index);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001057 size = sizeof(struct udf_bitmap) +
1058 (sizeof(struct buffer_head *) * nr_groups);
1059
1060 if (size <= PAGE_SIZE)
Joe Perchesed2ae6f2010-11-04 20:08:04 -07001061 bitmap = kzalloc(size, GFP_KERNEL);
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001062 else
Joe Perchesed2ae6f2010-11-04 20:08:04 -07001063 bitmap = vzalloc(size); /* TODO: get rid of vzalloc */
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001064
Markus Elfringba2eb862017-08-15 17:03:06 +02001065 if (!bitmap)
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001066 return NULL;
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001067
Marcin Slusarz66e1da32008-02-08 04:20:33 -08001068 bitmap->s_nr_groups = nr_groups;
1069 return bitmap;
1070}
1071
Jan Kara3fb38df2008-04-02 12:26:36 +02001072static int udf_fill_partdesc_info(struct super_block *sb,
1073 struct partitionDesc *p, int p_index)
1074{
1075 struct udf_part_map *map;
1076 struct udf_sb_info *sbi = UDF_SB(sb);
1077 struct partitionHeaderDesc *phd;
1078
1079 map = &sbi->s_partmaps[p_index];
1080
1081 map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */
1082 map->s_partition_root = le32_to_cpu(p->partitionStartingLocation);
1083
1084 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY))
1085 map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY;
1086 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE))
1087 map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE;
1088 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE))
1089 map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE;
1090 if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE))
1091 map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE;
1092
Steve Magnanifcbf7632017-10-12 08:48:41 -05001093 udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001094 p_index, map->s_partition_type,
1095 map->s_partition_root, map->s_partition_len);
Jan Kara3fb38df2008-04-02 12:26:36 +02001096
1097 if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) &&
1098 strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03))
1099 return 0;
1100
1101 phd = (struct partitionHeaderDesc *)p->partitionContentsUse;
1102 if (phd->unallocSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001103 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001104 .logicalBlockNum = le32_to_cpu(
1105 phd->unallocSpaceTable.extPosition),
1106 .partitionReferenceNum = p_index,
1107 };
Jan Kara6d3d5e82014-09-04 16:15:51 +02001108 struct inode *inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001109
Jan Kara6174c2e2014-10-09 12:52:16 +02001110 inode = udf_iget_special(sb, &loc);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001111 if (IS_ERR(inode)) {
Jan Kara3fb38df2008-04-02 12:26:36 +02001112 udf_debug("cannot load unallocSpaceTable (part %d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001113 p_index);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001114 return PTR_ERR(inode);
Jan Kara3fb38df2008-04-02 12:26:36 +02001115 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001116 map->s_uspace.s_table = inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001117 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001118 udf_debug("unallocSpaceTable (part %d) @ %lu\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001119 p_index, map->s_uspace.s_table->i_ino);
Jan Kara3fb38df2008-04-02 12:26:36 +02001120 }
1121
1122 if (phd->unallocSpaceBitmap.extLength) {
1123 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1124 if (!bitmap)
Jan Karad759bfa42013-07-25 19:10:59 +02001125 return -ENOMEM;
Jan Kara3fb38df2008-04-02 12:26:36 +02001126 map->s_uspace.s_bitmap = bitmap;
Jan Kara3fb38df2008-04-02 12:26:36 +02001127 bitmap->s_extPosition = le32_to_cpu(
1128 phd->unallocSpaceBitmap.extPosition);
1129 map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001130 udf_debug("unallocSpaceBitmap (part %d) @ %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001131 p_index, bitmap->s_extPosition);
Jan Kara3fb38df2008-04-02 12:26:36 +02001132 }
1133
1134 if (phd->partitionIntegrityTable.extLength)
1135 udf_debug("partitionIntegrityTable (part %d)\n", p_index);
1136
1137 if (phd->freedSpaceTable.extLength) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001138 struct kernel_lb_addr loc = {
Jan Kara3fb38df2008-04-02 12:26:36 +02001139 .logicalBlockNum = le32_to_cpu(
1140 phd->freedSpaceTable.extPosition),
1141 .partitionReferenceNum = p_index,
1142 };
Jan Kara6d3d5e82014-09-04 16:15:51 +02001143 struct inode *inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001144
Jan Kara6174c2e2014-10-09 12:52:16 +02001145 inode = udf_iget_special(sb, &loc);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001146 if (IS_ERR(inode)) {
Jan Kara3fb38df2008-04-02 12:26:36 +02001147 udf_debug("cannot load freedSpaceTable (part %d)\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001148 p_index);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001149 return PTR_ERR(inode);
Jan Kara3fb38df2008-04-02 12:26:36 +02001150 }
Jan Kara6d3d5e82014-09-04 16:15:51 +02001151 map->s_fspace.s_table = inode;
Jan Kara3fb38df2008-04-02 12:26:36 +02001152 map->s_partition_flags |= UDF_PART_FLAG_FREED_TABLE;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001153 udf_debug("freedSpaceTable (part %d) @ %lu\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001154 p_index, map->s_fspace.s_table->i_ino);
Jan Kara3fb38df2008-04-02 12:26:36 +02001155 }
1156
1157 if (phd->freedSpaceBitmap.extLength) {
1158 struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index);
1159 if (!bitmap)
Jan Karad759bfa42013-07-25 19:10:59 +02001160 return -ENOMEM;
Jan Kara3fb38df2008-04-02 12:26:36 +02001161 map->s_fspace.s_bitmap = bitmap;
Jan Kara3fb38df2008-04-02 12:26:36 +02001162 bitmap->s_extPosition = le32_to_cpu(
1163 phd->freedSpaceBitmap.extPosition);
1164 map->s_partition_flags |= UDF_PART_FLAG_FREED_BITMAP;
Steve Magnanifcbf7632017-10-12 08:48:41 -05001165 udf_debug("freedSpaceBitmap (part %d) @ %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001166 p_index, bitmap->s_extPosition);
Jan Kara3fb38df2008-04-02 12:26:36 +02001167 }
1168 return 0;
1169}
1170
Jan Karae971b0b2009-11-30 19:47:55 +01001171static void udf_find_vat_block(struct super_block *sb, int p_index,
1172 int type1_index, sector_t start_block)
1173{
1174 struct udf_sb_info *sbi = UDF_SB(sb);
1175 struct udf_part_map *map = &sbi->s_partmaps[p_index];
1176 sector_t vat_block;
1177 struct kernel_lb_addr ino;
Jan Kara6d3d5e82014-09-04 16:15:51 +02001178 struct inode *inode;
Jan Karae971b0b2009-11-30 19:47:55 +01001179
1180 /*
1181 * VAT file entry is in the last recorded block. Some broken disks have
1182 * it a few blocks before so try a bit harder...
1183 */
1184 ino.partitionReferenceNum = type1_index;
1185 for (vat_block = start_block;
1186 vat_block >= map->s_partition_root &&
Jan Kara6d3d5e82014-09-04 16:15:51 +02001187 vat_block >= start_block - 3; vat_block--) {
Jan Karae971b0b2009-11-30 19:47:55 +01001188 ino.logicalBlockNum = vat_block - map->s_partition_root;
Jan Kara6174c2e2014-10-09 12:52:16 +02001189 inode = udf_iget_special(sb, &ino);
Jan Kara6d3d5e82014-09-04 16:15:51 +02001190 if (!IS_ERR(inode)) {
1191 sbi->s_vat_inode = inode;
1192 break;
1193 }
Jan Karae971b0b2009-11-30 19:47:55 +01001194 }
1195}
1196
Jan Kara38b74a52008-04-02 16:01:35 +02001197static int udf_load_vat(struct super_block *sb, int p_index, int type1_index)
1198{
1199 struct udf_sb_info *sbi = UDF_SB(sb);
1200 struct udf_part_map *map = &sbi->s_partmaps[p_index];
Jan Karafa5e0812008-04-08 02:08:53 +02001201 struct buffer_head *bh = NULL;
1202 struct udf_inode_info *vati;
1203 uint32_t pos;
1204 struct virtualAllocationTable20 *vat20;
Fabian Frederick23bcda12017-01-06 21:54:41 +01001205 sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >>
1206 sb->s_blocksize_bits;
Jan Kara38b74a52008-04-02 16:01:35 +02001207
Jan Karae971b0b2009-11-30 19:47:55 +01001208 udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block);
Jan Kara4bf17af2009-07-14 19:30:23 +02001209 if (!sbi->s_vat_inode &&
1210 sbi->s_last_block != blocks - 1) {
Joe Perches78ace702011-10-10 01:08:05 -07001211 pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n",
1212 (unsigned long)sbi->s_last_block,
1213 (unsigned long)blocks - 1);
Jan Karae971b0b2009-11-30 19:47:55 +01001214 udf_find_vat_block(sb, p_index, type1_index, blocks - 1);
Jan Kara4bf17af2009-07-14 19:30:23 +02001215 }
Jan Kara38b74a52008-04-02 16:01:35 +02001216 if (!sbi->s_vat_inode)
Jan Karad759bfa42013-07-25 19:10:59 +02001217 return -EIO;
Jan Kara38b74a52008-04-02 16:01:35 +02001218
1219 if (map->s_partition_type == UDF_VIRTUAL_MAP15) {
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001220 map->s_type_specific.s_virtual.s_start_offset = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001221 map->s_type_specific.s_virtual.s_num_entries =
1222 (sbi->s_vat_inode->i_size - 36) >> 2;
1223 } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) {
Jan Karafa5e0812008-04-08 02:08:53 +02001224 vati = UDF_I(sbi->s_vat_inode);
1225 if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1226 pos = udf_block_map(sbi->s_vat_inode, 0);
1227 bh = sb_bread(sb, pos);
1228 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001229 return -EIO;
Jan Karafa5e0812008-04-08 02:08:53 +02001230 vat20 = (struct virtualAllocationTable20 *)bh->b_data;
1231 } else {
1232 vat20 = (struct virtualAllocationTable20 *)
1233 vati->i_ext.i_data;
1234 }
Jan Kara38b74a52008-04-02 16:01:35 +02001235
Jan Kara38b74a52008-04-02 16:01:35 +02001236 map->s_type_specific.s_virtual.s_start_offset =
Sebastian Manciulea47c93582008-04-14 17:06:36 +02001237 le16_to_cpu(vat20->lengthHeader);
Jan Kara38b74a52008-04-02 16:01:35 +02001238 map->s_type_specific.s_virtual.s_num_entries =
1239 (sbi->s_vat_inode->i_size -
1240 map->s_type_specific.s_virtual.
1241 s_start_offset) >> 2;
1242 brelse(bh);
1243 }
1244 return 0;
1245}
1246
Jan Karad759bfa42013-07-25 19:10:59 +02001247/*
1248 * Load partition descriptor block
1249 *
1250 * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor
1251 * sequence.
1252 */
Jan Karac0eb31e2008-04-01 16:50:35 +02001253static int udf_load_partdesc(struct super_block *sb, sector_t block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Jan Karac0eb31e2008-04-01 16:50:35 +02001255 struct buffer_head *bh;
Jan Karac0eb31e2008-04-01 16:50:35 +02001256 struct partitionDesc *p;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001257 struct udf_part_map *map;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001258 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara38b74a52008-04-02 16:01:35 +02001259 int i, type1_idx;
Jan Karac0eb31e2008-04-01 16:50:35 +02001260 uint16_t partitionNumber;
1261 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +02001262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Jan Karac0eb31e2008-04-01 16:50:35 +02001264 bh = udf_read_tagged(sb, block, block, &ident);
1265 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001266 return -EAGAIN;
1267 if (ident != TAG_IDENT_PD) {
1268 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001269 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001270 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001271
1272 p = (struct partitionDesc *)bh->b_data;
1273 partitionNumber = le16_to_cpu(p->partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001274
Alden Tondettar78888242016-05-18 14:09:19 -07001275 /* First scan for TYPE1 and SPARABLE partitions */
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001276 for (i = 0; i < sbi->s_partitions; i++) {
1277 map = &sbi->s_partmaps[i];
Steve Magnanifcbf7632017-10-12 08:48:41 -05001278 udf_debug("Searching map: (%u == %u)\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001279 map->s_partition_num, partitionNumber);
Jan Kara38b74a52008-04-02 16:01:35 +02001280 if (map->s_partition_num == partitionNumber &&
1281 (map->s_partition_type == UDF_TYPE1_MAP15 ||
1282 map->s_partition_type == UDF_SPARABLE_MAP15))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001284 }
1285
Jan Kara38b74a52008-04-02 16:01:35 +02001286 if (i >= sbi->s_partitions) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05001287 udf_debug("Partition (%u) not found in partition map\n",
Marcin Slusarz165923f2008-02-10 11:33:08 +01001288 partitionNumber);
Jan Karad759bfa42013-07-25 19:10:59 +02001289 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001290 goto out_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01001291 }
1292
Jan Kara3fb38df2008-04-02 12:26:36 +02001293 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001294 if (ret < 0)
1295 goto out_bh;
Jan Kara38b74a52008-04-02 16:01:35 +02001296
1297 /*
Jan Karabfb257a2008-04-08 20:37:21 +02001298 * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and
1299 * PHYSICAL partitions are already set up
Jan Kara38b74a52008-04-02 16:01:35 +02001300 */
1301 type1_idx = i;
Peter A. Felvegi44499602013-10-18 20:07:44 +02001302#ifdef UDFFS_DEBUG
1303 map = NULL; /* supress 'maybe used uninitialized' warning */
1304#endif
Jan Kara38b74a52008-04-02 16:01:35 +02001305 for (i = 0; i < sbi->s_partitions; i++) {
1306 map = &sbi->s_partmaps[i];
1307
1308 if (map->s_partition_num == partitionNumber &&
1309 (map->s_partition_type == UDF_VIRTUAL_MAP15 ||
Jan Karabfb257a2008-04-08 20:37:21 +02001310 map->s_partition_type == UDF_VIRTUAL_MAP20 ||
1311 map->s_partition_type == UDF_METADATA_MAP25))
Jan Kara38b74a52008-04-02 16:01:35 +02001312 break;
1313 }
1314
Jan Karad759bfa42013-07-25 19:10:59 +02001315 if (i >= sbi->s_partitions) {
1316 ret = 0;
Jan Kara38b74a52008-04-02 16:01:35 +02001317 goto out_bh;
Jan Karad759bfa42013-07-25 19:10:59 +02001318 }
Jan Kara38b74a52008-04-02 16:01:35 +02001319
1320 ret = udf_fill_partdesc_info(sb, p, i);
Jan Karad759bfa42013-07-25 19:10:59 +02001321 if (ret < 0)
Jan Kara38b74a52008-04-02 16:01:35 +02001322 goto out_bh;
1323
Jan Karabfb257a2008-04-08 20:37:21 +02001324 if (map->s_partition_type == UDF_METADATA_MAP25) {
Alden Tondettar78888242016-05-18 14:09:19 -07001325 ret = udf_load_metadata_files(sb, i, type1_idx);
Jan Karad759bfa42013-07-25 19:10:59 +02001326 if (ret < 0) {
Joe Perches78ace702011-10-10 01:08:05 -07001327 udf_err(sb, "error loading MetaData partition map %d\n",
1328 i);
Jan Karabfb257a2008-04-08 20:37:21 +02001329 goto out_bh;
1330 }
1331 } else {
Jan Karae729eac2013-07-25 16:15:16 +02001332 /*
1333 * If we have a partition with virtual map, we don't handle
1334 * writing to it (we overwrite blocks instead of relocating
1335 * them).
1336 */
David Howellsbc98a422017-07-17 08:45:34 +01001337 if (!sb_rdonly(sb)) {
Jan Karae729eac2013-07-25 16:15:16 +02001338 ret = -EACCES;
1339 goto out_bh;
1340 }
Jan Karabfb257a2008-04-08 20:37:21 +02001341 ret = udf_load_vat(sb, i, type1_idx);
Jan Karad759bfa42013-07-25 19:10:59 +02001342 if (ret < 0)
Jan Karabfb257a2008-04-08 20:37:21 +02001343 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001344 }
Jan Karad759bfa42013-07-25 19:10:59 +02001345 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001346out_bh:
Jan Kara2e0838f2008-04-01 18:08:51 +02001347 /* In case loading failed, we handle cleanup in udf_fill_super */
Jan Karac0eb31e2008-04-01 16:50:35 +02001348 brelse(bh);
1349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Jan Kara1df2ae32012-06-27 21:23:07 +02001352static int udf_load_sparable_map(struct super_block *sb,
1353 struct udf_part_map *map,
1354 struct sparablePartitionMap *spm)
1355{
1356 uint32_t loc;
1357 uint16_t ident;
1358 struct sparingTable *st;
1359 struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing;
1360 int i;
1361 struct buffer_head *bh;
1362
1363 map->s_partition_type = UDF_SPARABLE_MAP15;
1364 sdata->s_packet_len = le16_to_cpu(spm->packetLength);
1365 if (!is_power_of_2(sdata->s_packet_len)) {
1366 udf_err(sb, "error loading logical volume descriptor: "
1367 "Invalid packet length %u\n",
1368 (unsigned)sdata->s_packet_len);
1369 return -EIO;
1370 }
1371 if (spm->numSparingTables > 4) {
1372 udf_err(sb, "error loading logical volume descriptor: "
1373 "Too many sparing tables (%d)\n",
1374 (int)spm->numSparingTables);
1375 return -EIO;
1376 }
1377
1378 for (i = 0; i < spm->numSparingTables; i++) {
1379 loc = le32_to_cpu(spm->locSparingTable[i]);
1380 bh = udf_read_tagged(sb, loc, loc, &ident);
1381 if (!bh)
1382 continue;
1383
1384 st = (struct sparingTable *)bh->b_data;
1385 if (ident != 0 ||
1386 strncmp(st->sparingIdent.ident, UDF_ID_SPARING,
1387 strlen(UDF_ID_SPARING)) ||
1388 sizeof(*st) + le16_to_cpu(st->reallocationTableLen) >
1389 sb->s_blocksize) {
1390 brelse(bh);
1391 continue;
1392 }
1393
1394 sdata->s_spar_map[i] = bh;
1395 }
1396 map->s_partition_func = udf_get_pblock_spar15;
1397 return 0;
1398}
1399
Jan Karac0eb31e2008-04-01 16:50:35 +02001400static int udf_load_logicalvol(struct super_block *sb, sector_t block,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001401 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct logicalVolDesc *lvd;
Jan Kara1df2ae32012-06-27 21:23:07 +02001404 int i, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 uint8_t type;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001406 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -08001407 struct genericPartitionMap *gpm;
Jan Karac0eb31e2008-04-01 16:50:35 +02001408 uint16_t ident;
1409 struct buffer_head *bh;
Jan Karaadee11b2012-06-27 20:20:22 +02001410 unsigned int table_len;
Jan Karad759bfa42013-07-25 19:10:59 +02001411 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jan Karac0eb31e2008-04-01 16:50:35 +02001413 bh = udf_read_tagged(sb, block, block, &ident);
1414 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001415 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001416 BUG_ON(ident != TAG_IDENT_LVD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 lvd = (struct logicalVolDesc *)bh->b_data;
Jan Karaadee11b2012-06-27 20:20:22 +02001418 table_len = le32_to_cpu(lvd->mapTableLength);
Jan Kara57b96552012-07-10 17:58:04 +02001419 if (table_len > sb->s_blocksize - sizeof(*lvd)) {
Jan Karaadee11b2012-06-27 20:20:22 +02001420 udf_err(sb, "error loading logical volume descriptor: "
1421 "Partition table too long (%u > %lu)\n", table_len,
1422 sb->s_blocksize - sizeof(*lvd));
Jan Karad759bfa42013-07-25 19:10:59 +02001423 ret = -EIO;
Jan Karaadee11b2012-06-27 20:20:22 +02001424 goto out_bh;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Jan Karacb14d342012-06-27 20:08:44 +02001427 ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps));
1428 if (ret)
Jan Karac0eb31e2008-04-01 16:50:35 +02001429 goto out_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001431 for (i = 0, offset = 0;
Jan Karaadee11b2012-06-27 20:20:22 +02001432 i < sbi->s_partitions && offset < table_len;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001433 i++, offset += gpm->partitionMapLength) {
1434 struct udf_part_map *map = &sbi->s_partmaps[i];
1435 gpm = (struct genericPartitionMap *)
1436 &(lvd->partitionMaps[offset]);
1437 type = gpm->partitionMapType;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001438 if (type == 1) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001439 struct genericPartitionMap1 *gpm1 =
1440 (struct genericPartitionMap1 *)gpm;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001441 map->s_partition_type = UDF_TYPE1_MAP15;
1442 map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum);
1443 map->s_partition_num = le16_to_cpu(gpm1->partitionNum);
1444 map->s_partition_func = NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001445 } else if (type == 2) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001446 struct udfPartitionMap2 *upm2 =
1447 (struct udfPartitionMap2 *)gpm;
1448 if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL,
1449 strlen(UDF_ID_VIRTUAL))) {
1450 u16 suf =
1451 le16_to_cpu(((__le16 *)upm2->partIdent.
1452 identSuffix)[0]);
Jan Karac82a1272008-04-08 01:16:32 +02001453 if (suf < 0x0200) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001454 map->s_partition_type =
1455 UDF_VIRTUAL_MAP15;
1456 map->s_partition_func =
1457 udf_get_pblock_virt15;
Jan Karac82a1272008-04-08 01:16:32 +02001458 } else {
Marcin Slusarz4b111112008-02-08 04:20:36 -08001459 map->s_partition_type =
1460 UDF_VIRTUAL_MAP20;
1461 map->s_partition_func =
1462 udf_get_pblock_virt20;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Marcin Slusarz4b111112008-02-08 04:20:36 -08001464 } else if (!strncmp(upm2->partIdent.ident,
1465 UDF_ID_SPARABLE,
1466 strlen(UDF_ID_SPARABLE))) {
Jan Karad759bfa42013-07-25 19:10:59 +02001467 ret = udf_load_sparable_map(sb, map,
1468 (struct sparablePartitionMap *)gpm);
1469 if (ret < 0)
Jan Kara1df2ae32012-06-27 21:23:07 +02001470 goto out_bh;
Jan Karabfb257a2008-04-08 20:37:21 +02001471 } else if (!strncmp(upm2->partIdent.ident,
1472 UDF_ID_METADATA,
1473 strlen(UDF_ID_METADATA))) {
1474 struct udf_meta_data *mdata =
1475 &map->s_type_specific.s_metadata;
1476 struct metadataPartitionMap *mdm =
1477 (struct metadataPartitionMap *)
1478 &(lvd->partitionMaps[offset]);
Steve Magnanifcbf7632017-10-12 08:48:41 -05001479 udf_debug("Parsing Logical vol part %d type %u id=%s\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001480 i, type, UDF_ID_METADATA);
Jan Karabfb257a2008-04-08 20:37:21 +02001481
1482 map->s_partition_type = UDF_METADATA_MAP25;
1483 map->s_partition_func = udf_get_pblock_meta25;
1484
1485 mdata->s_meta_file_loc =
1486 le32_to_cpu(mdm->metadataFileLoc);
1487 mdata->s_mirror_file_loc =
1488 le32_to_cpu(mdm->metadataMirrorFileLoc);
1489 mdata->s_bitmap_file_loc =
1490 le32_to_cpu(mdm->metadataBitmapFileLoc);
1491 mdata->s_alloc_unit_size =
1492 le32_to_cpu(mdm->allocUnitSize);
1493 mdata->s_align_unit_size =
1494 le16_to_cpu(mdm->alignUnitSize);
Jan Karaed47a7d2011-10-24 16:47:48 +02001495 if (mdm->flags & 0x01)
1496 mdata->s_flags |= MF_DUPLICATE_MD;
Jan Karabfb257a2008-04-08 20:37:21 +02001497
1498 udf_debug("Metadata Ident suffix=0x%x\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001499 le16_to_cpu(*(__le16 *)
1500 mdm->partIdent.identSuffix));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001501 udf_debug("Metadata part num=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001502 le16_to_cpu(mdm->partitionNum));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001503 udf_debug("Metadata part alloc unit size=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001504 le32_to_cpu(mdm->allocUnitSize));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001505 udf_debug("Metadata file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001506 le32_to_cpu(mdm->metadataFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001507 udf_debug("Mirror file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001508 le32_to_cpu(mdm->metadataMirrorFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001509 udf_debug("Bitmap file loc=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001510 le32_to_cpu(mdm->metadataBitmapFileLoc));
Steve Magnanifcbf7632017-10-12 08:48:41 -05001511 udf_debug("Flags: %d %u\n",
Jan Karaed47a7d2011-10-24 16:47:48 +02001512 mdata->s_flags, mdm->flags);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001513 } else {
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08001514 udf_debug("Unknown ident: %s\n",
1515 upm2->partIdent.ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 continue;
1517 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001518 map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum);
1519 map->s_partition_num = le16_to_cpu(upm2->partitionNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
Steve Magnanifcbf7632017-10-12 08:48:41 -05001521 udf_debug("Partition (%d:%u) type %u on volume %u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001522 i, map->s_partition_num, type, map->s_volumeseqnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001525 if (fileset) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001526 struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 *fileset = lelb_to_cpu(la->extLocation);
Steve Magnanifcbf7632017-10-12 08:48:41 -05001529 udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n",
Joe Perchesa983f362011-10-10 01:08:07 -07001530 fileset->logicalBlockNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001531 fileset->partitionReferenceNum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 if (lvd->integritySeqExt.extLength)
1534 udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt));
Jan Karad759bfa42013-07-25 19:10:59 +02001535 ret = 0;
Jan Karac0eb31e2008-04-01 16:50:35 +02001536out_bh:
1537 brelse(bh);
1538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541/*
Alden Tondettara47241c2016-04-25 19:27:56 -07001542 * Find the prevailing Logical Volume Integrity Descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 */
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02001544static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Alden Tondettara47241c2016-04-25 19:27:56 -07001546 struct buffer_head *bh, *final_bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 uint16_t ident;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001548 struct udf_sb_info *sbi = UDF_SB(sb);
1549 struct logicalVolIntegrityDesc *lvid;
Alden Tondettara47241c2016-04-25 19:27:56 -07001550 int indirections = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Alden Tondettara47241c2016-04-25 19:27:56 -07001552 while (++indirections <= UDF_MAX_LVID_NESTING) {
1553 final_bh = NULL;
1554 while (loc.extLength > 0 &&
1555 (bh = udf_read_tagged(sb, loc.extLocation,
1556 loc.extLocation, &ident))) {
1557 if (ident != TAG_IDENT_LVID) {
1558 brelse(bh);
1559 break;
1560 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001561
Alden Tondettara47241c2016-04-25 19:27:56 -07001562 brelse(final_bh);
1563 final_bh = bh;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001564
Alden Tondettara47241c2016-04-25 19:27:56 -07001565 loc.extLength -= sb->s_blocksize;
1566 loc.extLocation++;
1567 }
1568
1569 if (!final_bh)
1570 return;
1571
1572 brelse(sbi->s_lvid_bh);
1573 sbi->s_lvid_bh = final_bh;
1574
1575 lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data;
1576 if (lvid->nextIntegrityExt.extLength == 0)
1577 return;
1578
1579 loc = leea_to_cpu(lvid->nextIntegrityExt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
Alden Tondettara47241c2016-04-25 19:27:56 -07001581
1582 udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n",
1583 UDF_MAX_LVID_NESTING);
1584 brelse(sbi->s_lvid_bh);
1585 sbi->s_lvid_bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
Jan Kara7b78fd02018-02-09 15:56:34 +01001588/*
1589 * Step for reallocation of table of partition descriptor sequence numbers.
1590 * Must be power of 2.
1591 */
1592#define PART_DESC_ALLOC_STEP 32
1593
1594struct desc_seq_scan_data {
1595 struct udf_vds_record vds[VDS_POS_LENGTH];
1596 unsigned int size_part_descs;
1597 struct udf_vds_record *part_descs_loc;
1598};
1599
1600static struct udf_vds_record *handle_partition_descriptor(
1601 struct buffer_head *bh,
1602 struct desc_seq_scan_data *data)
1603{
1604 struct partitionDesc *desc = (struct partitionDesc *)bh->b_data;
1605 int partnum;
1606
1607 partnum = le16_to_cpu(desc->partitionNumber);
1608 if (partnum >= data->size_part_descs) {
1609 struct udf_vds_record *new_loc;
1610 unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP);
1611
1612 new_loc = kzalloc(sizeof(*new_loc) * new_size, GFP_KERNEL);
1613 if (!new_loc)
1614 return ERR_PTR(-ENOMEM);
1615 memcpy(new_loc, data->part_descs_loc,
1616 data->size_part_descs * sizeof(*new_loc));
1617 kfree(data->part_descs_loc);
1618 data->part_descs_loc = new_loc;
1619 data->size_part_descs = new_size;
1620 }
1621 return &(data->part_descs_loc[partnum]);
1622}
1623
1624
1625static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident,
1626 struct buffer_head *bh, struct desc_seq_scan_data *data)
Jan Kara18cf47812018-02-09 15:15:39 +01001627{
1628 switch (ident) {
1629 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001630 return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001631 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001632 return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001633 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001634 return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]);
Jan Kara18cf47812018-02-09 15:15:39 +01001635 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001636 return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]);
1637 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1638 return handle_partition_descriptor(bh, data);
Jan Kara18cf47812018-02-09 15:15:39 +01001639 }
1640 return NULL;
1641}
Vegard Nossume7a4eb82015-12-10 17:13:41 +01001642
1643/*
Jan Karad759bfa42013-07-25 19:10:59 +02001644 * Process a main/reserve volume descriptor sequence.
1645 * @block First block of first extent of the sequence.
1646 * @lastblock Lastblock of first extent of the sequence.
1647 * @fileset There we store extent containing root fileset
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 *
Jan Karad759bfa42013-07-25 19:10:59 +02001649 * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor
1650 * sequence
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
Jan Karad759bfa42013-07-25 19:10:59 +02001652static noinline int udf_process_sequence(
1653 struct super_block *sb,
1654 sector_t block, sector_t lastblock,
1655 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 struct buffer_head *bh = NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001658 struct udf_vds_record *curr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct generic_desc *gd;
1660 struct volDescPtr *vdp;
Fabian Frederick2b8f9422015-02-04 18:15:04 +01001661 bool done = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 uint32_t vdsn;
1663 uint16_t ident;
Jan Karad759bfa42013-07-25 19:10:59 +02001664 int ret;
Vegard Nossume7a4eb82015-12-10 17:13:41 +01001665 unsigned int indirections = 0;
Jan Kara7b78fd02018-02-09 15:56:34 +01001666 struct desc_seq_scan_data data;
1667 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jan Kara7b78fd02018-02-09 15:56:34 +01001669 memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH);
1670 data.size_part_descs = PART_DESC_ALLOC_STEP;
1671 data.part_descs_loc = kzalloc(sizeof(*data.part_descs_loc) *
1672 data.size_part_descs, GFP_KERNEL);
1673 if (!data.part_descs_loc)
1674 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Jan Karac0eb31e2008-04-01 16:50:35 +02001676 /*
1677 * Read the main descriptor sequence and find which descriptors
1678 * are in it.
1679 */
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001680 for (; (!done && block <= lastblock); block++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 bh = udf_read_tagged(sb, block, block, &ident);
Jan Kara67621672018-02-08 18:10:54 +01001683 if (!bh)
1684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* Process each descriptor (ISO 13346 3/8.3-8.4) */
1687 gd = (struct generic_desc *)bh->b_data;
1688 vdsn = le32_to_cpu(gd->volDescSeqNum);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001689 switch (ident) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001690 case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */
Jan Kara7b568cb2018-02-09 13:28:41 +01001691 if (++indirections > UDF_MAX_TD_NESTING) {
1692 udf_err(sb, "too many Volume Descriptor "
1693 "Pointers (max %u supported)\n",
1694 UDF_MAX_TD_NESTING);
1695 brelse(bh);
1696 return -EIO;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001697 }
Jan Kara7b568cb2018-02-09 13:28:41 +01001698
1699 vdp = (struct volDescPtr *)bh->b_data;
1700 block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation);
1701 lastblock = le32_to_cpu(
1702 vdp->nextVolDescSeqExt.extLength) >>
1703 sb->s_blocksize_bits;
1704 lastblock += block - 1;
1705 /* For loop is going to increment 'block' again */
1706 block--;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001707 break;
Jan Kara18cf47812018-02-09 15:15:39 +01001708 case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001709 case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */
Jan Kara18cf47812018-02-09 15:15:39 +01001710 case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */
1711 case TAG_IDENT_USD: /* ISO 13346 3/10.8 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001712 case TAG_IDENT_PD: /* ISO 13346 3/10.5 */
1713 curr = get_volume_descriptor_record(ident, bh, &data);
1714 if (IS_ERR(curr)) {
1715 brelse(bh);
1716 return PTR_ERR(curr);
1717 }
1718 /* Descriptor we don't care about? */
1719 if (!curr)
1720 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -08001721 if (vdsn >= curr->volDescSeqNum) {
1722 curr->volDescSeqNum = vdsn;
1723 curr->block = block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001724 }
1725 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07001726 case TAG_IDENT_TD: /* ISO 13346 3/10.9 */
Jan Kara7b568cb2018-02-09 13:28:41 +01001727 done = true;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07001728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07001730 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Jan Karac0eb31e2008-04-01 16:50:35 +02001732 /*
1733 * Now read interesting descriptors again and process them
1734 * in a suitable order
1735 */
Jan Kara7b78fd02018-02-09 15:56:34 +01001736 if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) {
Joe Perches78ace702011-10-10 01:08:05 -07001737 udf_err(sb, "Primary Volume Descriptor not found!\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001738 return -EAGAIN;
Jan Karac0eb31e2008-04-01 16:50:35 +02001739 }
Jan Kara7b78fd02018-02-09 15:56:34 +01001740 ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block);
Jan Karad759bfa42013-07-25 19:10:59 +02001741 if (ret < 0)
1742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Jan Kara7b78fd02018-02-09 15:56:34 +01001744 if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) {
Jan Karad759bfa42013-07-25 19:10:59 +02001745 ret = udf_load_logicalvol(sb,
Jan Kara7b78fd02018-02-09 15:56:34 +01001746 data.vds[VDS_POS_LOGICAL_VOL_DESC].block,
1747 fileset);
Jan Karad759bfa42013-07-25 19:10:59 +02001748 if (ret < 0)
1749 return ret;
1750 }
Marcin Slusarz165923f2008-02-10 11:33:08 +01001751
Jan Kara7b78fd02018-02-09 15:56:34 +01001752 /* Now handle prevailing Partition Descriptors */
1753 for (i = 0; i < data.size_part_descs; i++) {
1754 if (data.part_descs_loc[i].block) {
1755 ret = udf_load_partdesc(sb,
1756 data.part_descs_loc[i].block);
Jan Karad759bfa42013-07-25 19:10:59 +02001757 if (ret < 0)
1758 return ret;
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
1762 return 0;
1763}
1764
Jan Karad759bfa42013-07-25 19:10:59 +02001765/*
1766 * Load Volume Descriptor Sequence described by anchor in bh
1767 *
1768 * Returns <0 on error, 0 on success
1769 */
Jan Kara403460052009-03-19 16:21:38 +01001770static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh,
1771 struct kernel_lb_addr *fileset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Jan Kara403460052009-03-19 16:21:38 +01001773 struct anchorVolDescPtr *anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001774 sector_t main_s, main_e, reserve_s, reserve_e;
1775 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Jan Kara403460052009-03-19 16:21:38 +01001777 anchor = (struct anchorVolDescPtr *)bh->b_data;
1778
1779 /* Locate the main sequence */
1780 main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation);
1781 main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength);
1782 main_e = main_e >> sb->s_blocksize_bits;
Jan Kara91c9c9e2018-02-08 17:39:01 +01001783 main_e += main_s - 1;
Jan Kara403460052009-03-19 16:21:38 +01001784
1785 /* Locate the reserve sequence */
1786 reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation);
1787 reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength);
1788 reserve_e = reserve_e >> sb->s_blocksize_bits;
Jan Kara91c9c9e2018-02-08 17:39:01 +01001789 reserve_e += reserve_s - 1;
Jan Kara403460052009-03-19 16:21:38 +01001790
1791 /* Process the main & reserve sequences */
1792 /* responsible for finding the PartitionDesc(s) */
Jan Karad759bfa42013-07-25 19:10:59 +02001793 ret = udf_process_sequence(sb, main_s, main_e, fileset);
1794 if (ret != -EAGAIN)
1795 return ret;
Jan Karabff943a2012-06-27 22:27:05 +02001796 udf_sb_free_partitions(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001797 ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset);
1798 if (ret < 0) {
1799 udf_sb_free_partitions(sb);
1800 /* No sequence was OK, return -EIO */
1801 if (ret == -EAGAIN)
1802 ret = -EIO;
1803 }
1804 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Jan Kara403460052009-03-19 16:21:38 +01001807/*
1808 * Check whether there is an anchor block in the given block and
1809 * load Volume Descriptor Sequence if so.
Jan Karad759bfa42013-07-25 19:10:59 +02001810 *
1811 * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor
1812 * block
Jan Kara403460052009-03-19 16:21:38 +01001813 */
1814static int udf_check_anchor_block(struct super_block *sb, sector_t block,
1815 struct kernel_lb_addr *fileset)
1816{
1817 struct buffer_head *bh;
1818 uint16_t ident;
1819 int ret;
1820
1821 if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) &&
1822 udf_fixed_to_variable(block) >=
Fabian Frederick23bcda12017-01-06 21:54:41 +01001823 i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits)
Jan Karad759bfa42013-07-25 19:10:59 +02001824 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001825
1826 bh = udf_read_tagged(sb, block, block, &ident);
1827 if (!bh)
Jan Karad759bfa42013-07-25 19:10:59 +02001828 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001829 if (ident != TAG_IDENT_AVDP) {
1830 brelse(bh);
Jan Karad759bfa42013-07-25 19:10:59 +02001831 return -EAGAIN;
Jan Kara403460052009-03-19 16:21:38 +01001832 }
1833 ret = udf_load_sequence(sb, bh, fileset);
1834 brelse(bh);
1835 return ret;
1836}
1837
Jan Karad759bfa42013-07-25 19:10:59 +02001838/*
1839 * Search for an anchor volume descriptor pointer.
1840 *
1841 * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set
1842 * of anchors.
1843 */
1844static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock,
1845 struct kernel_lb_addr *fileset)
Jan Kara403460052009-03-19 16:21:38 +01001846{
1847 sector_t last[6];
1848 int i;
1849 struct udf_sb_info *sbi = UDF_SB(sb);
1850 int last_count = 0;
Jan Karad759bfa42013-07-25 19:10:59 +02001851 int ret;
Jan Kara403460052009-03-19 16:21:38 +01001852
1853 /* First try user provided anchor */
1854 if (sbi->s_anchor) {
Jan Karad759bfa42013-07-25 19:10:59 +02001855 ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset);
1856 if (ret != -EAGAIN)
1857 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001858 }
1859 /*
1860 * according to spec, anchor is in either:
1861 * block 256
1862 * lastblock-256
1863 * lastblock
1864 * however, if the disc isn't closed, it could be 512.
1865 */
Jan Karad759bfa42013-07-25 19:10:59 +02001866 ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset);
1867 if (ret != -EAGAIN)
1868 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001869 /*
1870 * The trouble is which block is the last one. Drives often misreport
1871 * this so we try various possibilities.
1872 */
Jan Karad759bfa42013-07-25 19:10:59 +02001873 last[last_count++] = *lastblock;
1874 if (*lastblock >= 1)
1875 last[last_count++] = *lastblock - 1;
1876 last[last_count++] = *lastblock + 1;
1877 if (*lastblock >= 2)
1878 last[last_count++] = *lastblock - 2;
1879 if (*lastblock >= 150)
1880 last[last_count++] = *lastblock - 150;
1881 if (*lastblock >= 152)
1882 last[last_count++] = *lastblock - 152;
Jan Kara403460052009-03-19 16:21:38 +01001883
1884 for (i = 0; i < last_count; i++) {
Fabian Frederick23bcda12017-01-06 21:54:41 +01001885 if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >>
Jan Kara403460052009-03-19 16:21:38 +01001886 sb->s_blocksize_bits)
1887 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001888 ret = udf_check_anchor_block(sb, last[i], fileset);
1889 if (ret != -EAGAIN) {
1890 if (!ret)
1891 *lastblock = last[i];
1892 return ret;
1893 }
Jan Kara403460052009-03-19 16:21:38 +01001894 if (last[i] < 256)
1895 continue;
Jan Karad759bfa42013-07-25 19:10:59 +02001896 ret = udf_check_anchor_block(sb, last[i] - 256, fileset);
1897 if (ret != -EAGAIN) {
1898 if (!ret)
1899 *lastblock = last[i];
1900 return ret;
1901 }
Jan Kara403460052009-03-19 16:21:38 +01001902 }
1903
1904 /* Finally try block 512 in case media is open */
Jan Karad759bfa42013-07-25 19:10:59 +02001905 return udf_check_anchor_block(sb, sbi->s_session + 512, fileset);
Jan Kara403460052009-03-19 16:21:38 +01001906}
1907
1908/*
1909 * Find an anchor volume descriptor and load Volume Descriptor Sequence from
1910 * area specified by it. The function expects sbi->s_lastblock to be the last
1911 * block on the media.
1912 *
Jan Karad759bfa42013-07-25 19:10:59 +02001913 * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor
1914 * was not found.
Jan Kara403460052009-03-19 16:21:38 +01001915 */
1916static int udf_find_anchor(struct super_block *sb,
1917 struct kernel_lb_addr *fileset)
1918{
Jan Kara403460052009-03-19 16:21:38 +01001919 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Karad759bfa42013-07-25 19:10:59 +02001920 sector_t lastblock = sbi->s_last_block;
1921 int ret;
Jan Kara403460052009-03-19 16:21:38 +01001922
Jan Karad759bfa42013-07-25 19:10:59 +02001923 ret = udf_scan_anchors(sb, &lastblock, fileset);
1924 if (ret != -EAGAIN)
Jan Kara403460052009-03-19 16:21:38 +01001925 goto out;
1926
1927 /* No anchor found? Try VARCONV conversion of block numbers */
1928 UDF_SET_FLAG(sb, UDF_FLAG_VARCONV);
Jan Karad759bfa42013-07-25 19:10:59 +02001929 lastblock = udf_variable_to_fixed(sbi->s_last_block);
Jan Kara403460052009-03-19 16:21:38 +01001930 /* Firstly, we try to not convert number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001931 ret = udf_scan_anchors(sb, &lastblock, fileset);
1932 if (ret != -EAGAIN)
Jan Kara403460052009-03-19 16:21:38 +01001933 goto out;
1934
Jan Karad759bfa42013-07-25 19:10:59 +02001935 lastblock = sbi->s_last_block;
Jan Kara403460052009-03-19 16:21:38 +01001936 /* Secondly, we try with converted number of the last block */
Jan Karad759bfa42013-07-25 19:10:59 +02001937 ret = udf_scan_anchors(sb, &lastblock, fileset);
1938 if (ret < 0) {
Jan Kara403460052009-03-19 16:21:38 +01001939 /* VARCONV didn't help. Clear it. */
1940 UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV);
Jan Kara403460052009-03-19 16:21:38 +01001941 }
1942out:
Jan Karad759bfa42013-07-25 19:10:59 +02001943 if (ret == 0)
1944 sbi->s_last_block = lastblock;
1945 return ret;
Jan Kara403460052009-03-19 16:21:38 +01001946}
1947
1948/*
1949 * Check Volume Structure Descriptor, find Anchor block and load Volume
Jan Karad759bfa42013-07-25 19:10:59 +02001950 * Descriptor Sequence.
1951 *
1952 * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor
1953 * block was not found.
Jan Kara403460052009-03-19 16:21:38 +01001954 */
1955static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt,
1956 int silent, struct kernel_lb_addr *fileset)
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001957{
1958 struct udf_sb_info *sbi = UDF_SB(sb);
Jan Kara403460052009-03-19 16:21:38 +01001959 loff_t nsr_off;
Jan Karad759bfa42013-07-25 19:10:59 +02001960 int ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001961
1962 if (!sb_set_blocksize(sb, uopt->blocksize)) {
1963 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001964 udf_warn(sb, "Bad block size\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001965 return -EINVAL;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001966 }
1967 sbi->s_last_block = uopt->lastblock;
Jan Kara403460052009-03-19 16:21:38 +01001968 if (!uopt->novrs) {
1969 /* Check that it is NSR02 compliant */
1970 nsr_off = udf_check_vsd(sb);
1971 if (!nsr_off) {
1972 if (!silent)
Joe Perches78ace702011-10-10 01:08:05 -07001973 udf_warn(sb, "No VRS found\n");
Fabian Frederick70f16ce2017-01-18 19:39:35 +01001974 return -EINVAL;
Jan Kara403460052009-03-19 16:21:38 +01001975 }
1976 if (nsr_off == -1)
Peter A. Felvegi44499602013-10-18 20:07:44 +02001977 udf_debug("Failed to read sector at offset %d. "
1978 "Assuming open disc. Skipping validity "
1979 "check\n", VSD_FIRST_SECTOR_OFFSET);
Jan Kara403460052009-03-19 16:21:38 +01001980 if (!sbi->s_last_block)
1981 sbi->s_last_block = udf_get_last_block(sb);
1982 } else {
1983 udf_debug("Validity check skipped because of novrs option\n");
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001984 }
Jan Kara403460052009-03-19 16:21:38 +01001985
1986 /* Look for anchor block and load Volume Descriptor Sequence */
1987 sbi->s_anchor = uopt->anchor;
Jan Karad759bfa42013-07-25 19:10:59 +02001988 ret = udf_find_anchor(sb, fileset);
1989 if (ret < 0) {
1990 if (!silent && ret == -EAGAIN)
Joe Perches78ace702011-10-10 01:08:05 -07001991 udf_warn(sb, "No anchor found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02001992 return ret;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001993 }
Jan Karad759bfa42013-07-25 19:10:59 +02001994 return 0;
Clemens Ladisch1197e4d2009-03-11 15:57:47 +01001995}
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997static void udf_open_lvid(struct super_block *sb)
1998{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08001999 struct udf_sb_info *sbi = UDF_SB(sb);
2000 struct buffer_head *bh = sbi->s_lvid_bh;
Marcin Slusarz165923f2008-02-10 11:33:08 +01002001 struct logicalVolIntegrityDesc *lvid;
2002 struct logicalVolIntegrityDescImpUse *lvidiu;
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08002003 struct timespec ts;
Jan Kara146bca72009-03-16 18:27:37 +01002004
Marcin Slusarz165923f2008-02-10 11:33:08 +01002005 if (!bh)
2006 return;
Jan Kara69d75672013-09-12 22:00:15 +02002007 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2008 lvidiu = udf_sb_lvidiu(sb);
2009 if (!lvidiu)
2010 return;
Jan Kara949f4a72010-10-20 18:49:20 +02002011
2012 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002013 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2014 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08002015 ktime_get_real_ts(&ts);
2016 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
Jan Kara146bca72009-03-16 18:27:37 +01002017 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Marcin Slusarz165923f2008-02-10 11:33:08 +01002019 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002020 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02002021 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01002022
2023 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
2024 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01002025 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02002026 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c972013-01-17 22:11:38 +01002027 /* Make opening of filesystem visible on the media immediately */
2028 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031static void udf_close_lvid(struct super_block *sb)
2032{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002033 struct udf_sb_info *sbi = UDF_SB(sb);
2034 struct buffer_head *bh = sbi->s_lvid_bh;
2035 struct logicalVolIntegrityDesc *lvid;
Marcin Slusarz165923f2008-02-10 11:33:08 +01002036 struct logicalVolIntegrityDescImpUse *lvidiu;
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08002037 struct timespec ts;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002038
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002039 if (!bh)
2040 return;
Jan Kara69d75672013-09-12 22:00:15 +02002041 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2042 lvidiu = udf_sb_lvidiu(sb);
2043 if (!lvidiu)
2044 return;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002045
Jan Kara949f4a72010-10-20 18:49:20 +02002046 mutex_lock(&sbi->s_alloc_mutex);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002047 lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
2048 lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
Deepa Dinamani88b50ce2017-01-02 19:20:55 -08002049 ktime_get_real_ts(&ts);
2050 udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002051 if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev))
2052 lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION);
2053 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev))
2054 lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev);
2055 if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev))
2056 lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev);
2057 lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Marcin Slusarz165923f2008-02-10 11:33:08 +01002059 lvid->descTag.descCRC = cpu_to_le16(
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002060 crc_itu_t(0, (char *)lvid + sizeof(struct tag),
Bob Copelandf845fce2008-04-17 09:47:48 +02002061 le16_to_cpu(lvid->descTag.descCRCLength)));
Marcin Slusarz165923f2008-02-10 11:33:08 +01002062
2063 lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag);
Jan Kara853a0c22011-12-23 11:53:07 +01002064 /*
2065 * We set buffer uptodate unconditionally here to avoid spurious
2066 * warnings from mark_buffer_dirty() when previous EIO has marked
2067 * the buffer as !uptodate
2068 */
2069 set_buffer_uptodate(bh);
Marcin Slusarz165923f2008-02-10 11:33:08 +01002070 mark_buffer_dirty(bh);
Jan Kara146bca72009-03-16 18:27:37 +01002071 sbi->s_lvid_dirty = 0;
Jan Kara949f4a72010-10-20 18:49:20 +02002072 mutex_unlock(&sbi->s_alloc_mutex);
Jan Kara9734c972013-01-17 22:11:38 +01002073 /* Make closing of filesystem visible on the media immediately */
2074 sync_dirty_buffer(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Jan Karad664b6a2010-10-20 18:28:46 +02002077u64 lvid_get_unique_id(struct super_block *sb)
2078{
2079 struct buffer_head *bh;
2080 struct udf_sb_info *sbi = UDF_SB(sb);
2081 struct logicalVolIntegrityDesc *lvid;
2082 struct logicalVolHeaderDesc *lvhd;
2083 u64 uniqueID;
2084 u64 ret;
2085
2086 bh = sbi->s_lvid_bh;
2087 if (!bh)
2088 return 0;
2089
2090 lvid = (struct logicalVolIntegrityDesc *)bh->b_data;
2091 lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse;
2092
2093 mutex_lock(&sbi->s_alloc_mutex);
2094 ret = uniqueID = le64_to_cpu(lvhd->uniqueID);
2095 if (!(++uniqueID & 0xFFFFFFFF))
2096 uniqueID += 16;
2097 lvhd->uniqueID = cpu_to_le64(uniqueID);
2098 mutex_unlock(&sbi->s_alloc_mutex);
2099 mark_buffer_dirty(bh);
2100
2101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104static int udf_fill_super(struct super_block *sb, void *options, int silent)
2105{
Jan Karad759bfa42013-07-25 19:10:59 +02002106 int ret = -EINVAL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002107 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 struct udf_options uopt;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002109 struct kernel_lb_addr rootdir, fileset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct udf_sb_info *sbi;
Jan Kara9181f8b2015-08-20 14:50:07 +02002111 bool lvid_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT);
Jan Kara116e5252018-02-22 10:39:52 +01002114 /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */
2115 uopt.uid = make_kuid(current_user_ns(), overflowuid);
2116 uopt.gid = make_kgid(current_user_ns(), overflowgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 uopt.umask = 0;
Marcin Slusarz87bc7302008-12-02 13:40:11 +01002118 uopt.fmode = UDF_INVALID_MODE;
2119 uopt.dmode = UDF_INVALID_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
Markus Elfring033c9da2017-08-15 16:45:44 +02002121 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Alessio Igor Bogani9db9f9e2010-11-16 18:40:49 +01002122 if (!sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return -ENOMEM;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sb->s_fs_info = sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Ingo Molnar1e7933d2006-03-23 03:00:44 -08002127 mutex_init(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Miklos Szeredi6da80892008-02-08 04:21:50 -08002129 if (!udf_parse_options((char *)options, &uopt, false))
Markus Elfringfdf26572014-11-18 18:29:10 +01002130 goto parse_options_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132 if (uopt.flags & (1 << UDF_FLAG_UTF8) &&
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002133 uopt.flags & (1 << UDF_FLAG_NLS_MAP)) {
Joe Perches8076c362011-10-10 01:08:03 -07002134 udf_err(sb, "utf8 cannot be combined with iocharset\n");
Markus Elfringfdf26572014-11-18 18:29:10 +01002135 goto parse_options_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137#ifdef CONFIG_UDF_NLS
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002138 if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 uopt.nls_map = load_nls_default();
2140 if (!uopt.nls_map)
2141 uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP);
2142 else
2143 udf_debug("Using default NLS map\n");
2144 }
2145#endif
2146 if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP)))
2147 uopt.flags |= (1 << UDF_FLAG_UTF8);
2148
2149 fileset.logicalBlockNum = 0xFFFFFFFF;
2150 fileset.partitionReferenceNum = 0xFFFF;
2151
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002152 sbi->s_flags = uopt.flags;
2153 sbi->s_uid = uopt.uid;
2154 sbi->s_gid = uopt.gid;
2155 sbi->s_umask = uopt.umask;
Marcin Slusarz7ac9bcd52008-11-16 20:52:19 +01002156 sbi->s_fmode = uopt.fmode;
2157 sbi->s_dmode = uopt.dmode;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002158 sbi->s_nls_map = uopt.nls_map;
Jan Karac03cad22010-10-20 22:17:28 +02002159 rwlock_init(&sbi->s_cred_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002161 if (uopt.session == 0xFFFFFFFF)
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002162 sbi->s_session = udf_get_last_session(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002164 sbi->s_session = uopt.session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002166 udf_debug("Multi-session=%d\n", sbi->s_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* Fill in the rest of the superblock */
2169 sb->s_op = &udf_sb_ops;
Rasmus Rohde221e5832008-04-30 17:22:06 +02002170 sb->s_export_op = &udf_export_ops;
Christoph Hellwig123e9ca2010-05-19 07:16:44 -04002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 sb->s_magic = UDF_SUPER_MAGIC;
2173 sb->s_time_gran = 1000;
2174
Jan Kara403460052009-03-19 16:21:38 +01002175 if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) {
2176 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
2177 } else {
Martin K. Petersene1defc42009-05-22 17:17:49 -04002178 uopt.blocksize = bdev_logical_block_size(sb->s_bdev);
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002179 while (uopt.blocksize <= 4096) {
Jan Kara403460052009-03-19 16:21:38 +01002180 ret = udf_load_vrs(sb, &uopt, silent, &fileset);
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002181 if (ret < 0) {
2182 if (!silent && ret != -EACCES) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002183 pr_notice("Scanning with blocksize %u failed\n",
Fabian Frederick70f16ce2017-01-18 19:39:35 +01002184 uopt.blocksize);
2185 }
2186 brelse(sbi->s_lvid_bh);
2187 sbi->s_lvid_bh = NULL;
2188 /*
2189 * EACCES is special - we want to propagate to
2190 * upper layers that we cannot handle RW mount.
2191 */
2192 if (ret == -EACCES)
2193 break;
2194 } else
2195 break;
2196
2197 uopt.blocksize <<= 1;
Jan Kara403460052009-03-19 16:21:38 +01002198 }
2199 }
Jan Karad759bfa42013-07-25 19:10:59 +02002200 if (ret < 0) {
2201 if (ret == -EAGAIN) {
2202 udf_warn(sb, "No partition found (1)\n");
2203 ret = -EINVAL;
2204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto error_out;
2206 }
2207
Steve Magnanifcbf7632017-10-12 08:48:41 -05002208 udf_debug("Lastblock=%u\n", sbi->s_last_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002210 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002211 struct logicalVolIntegrityDescImpUse *lvidiu =
Jan Kara69d75672013-09-12 22:00:15 +02002212 udf_sb_lvidiu(sb);
2213 uint16_t minUDFReadRev;
2214 uint16_t minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Jan Kara69d75672013-09-12 22:00:15 +02002216 if (!lvidiu) {
2217 ret = -EINVAL;
2218 goto error_out;
2219 }
2220 minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev);
2221 minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002222 if (minUDFReadRev > UDF_MAX_READ_VERSION) {
Joe Perches78ace702011-10-10 01:08:05 -07002223 udf_err(sb, "minUDFReadRev=%x (max is %x)\n",
Jan Kara69d75672013-09-12 22:00:15 +02002224 minUDFReadRev,
Joe Perches78ace702011-10-10 01:08:05 -07002225 UDF_MAX_READ_VERSION);
Jan Karad759bfa42013-07-25 19:10:59 +02002226 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto error_out;
Jan Karae729eac2013-07-25 16:15:16 +02002228 } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION &&
David Howellsbc98a422017-07-17 08:45:34 +01002229 !sb_rdonly(sb)) {
Jan Karae729eac2013-07-25 16:15:16 +02002230 ret = -EACCES;
2231 goto error_out;
2232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002234 sbi->s_udfrev = minUDFWriteRev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE)
2237 UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE);
2238 if (minUDFReadRev >= UDF_VERS_USE_STREAMS)
2239 UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS);
2240 }
2241
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002242 if (!sbi->s_partitions) {
Joe Perches78ace702011-10-10 01:08:05 -07002243 udf_warn(sb, "No partition found (2)\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002244 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 goto error_out;
2246 }
2247
Marcin Slusarz4b111112008-02-08 04:20:36 -08002248 if (sbi->s_partmaps[sbi->s_partition].s_partition_flags &
Jan Karae729eac2013-07-25 16:15:16 +02002249 UDF_PART_FLAG_READ_ONLY &&
David Howellsbc98a422017-07-17 08:45:34 +01002250 !sb_rdonly(sb)) {
Jan Karae729eac2013-07-25 16:15:16 +02002251 ret = -EACCES;
2252 goto error_out;
Peter Osterlundc1a26e72006-10-05 21:17:50 +02002253 }
Eric Sandeen39b3f6d2006-09-29 01:59:41 -07002254
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002255 if (udf_find_fileset(sb, &fileset, &rootdir)) {
Joe Perches78ace702011-10-10 01:08:05 -07002256 udf_warn(sb, "No fileset found\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002257 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto error_out;
2259 }
2260
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002261 if (!silent) {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002262 struct timestamp ts;
Marcin Slusarz56774802008-02-10 11:25:31 +01002263 udf_time_to_disk_stamp(&ts, sbi->s_record_time);
Joe Perches78ace702011-10-10 01:08:05 -07002264 udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n",
2265 sbi->s_volume_ident,
2266 le16_to_cpu(ts.year), ts.month, ts.day,
Marcin Slusarz56774802008-02-10 11:25:31 +01002267 ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
David Howellsbc98a422017-07-17 08:45:34 +01002269 if (!sb_rdonly(sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 udf_open_lvid(sb);
Jan Kara9181f8b2015-08-20 14:50:07 +02002271 lvid_open = true;
2272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* Assign the root inode */
2275 /* assign inodes by physical block number */
2276 /* perhaps it's not extensible enough, but for now ... */
Pekka Enberg97e961f2008-10-15 12:29:03 +02002277 inode = udf_iget(sb, &rootdir);
Jan Kara6d3d5e82014-09-04 16:15:51 +02002278 if (IS_ERR(inode)) {
Steve Magnanifcbf7632017-10-12 08:48:41 -05002279 udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002280 rootdir.logicalBlockNum, rootdir.partitionReferenceNum);
Jan Kara6d3d5e82014-09-04 16:15:51 +02002281 ret = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 goto error_out;
2283 }
2284
2285 /* Allocate a dentry for the root inode */
Al Viro48fde702012-01-08 22:15:13 -05002286 sb->s_root = d_make_root(inode);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002287 if (!sb->s_root) {
Joe Perches78ace702011-10-10 01:08:05 -07002288 udf_err(sb, "Couldn't allocate root dentry\n");
Jan Karad759bfa42013-07-25 19:10:59 +02002289 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto error_out;
2291 }
Jan Kara31170b62007-05-08 00:35:21 -07002292 sb->s_maxbytes = MAX_LFS_FILESIZE;
Al Viro8de52772012-02-06 12:45:27 -05002293 sb->s_max_links = UDF_MAX_LINKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002296error_out:
Markus Elfring0d454e42014-11-18 17:17:46 +01002297 iput(sbi->s_vat_inode);
Markus Elfringfdf26572014-11-18 18:29:10 +01002298parse_options_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299#ifdef CONFIG_UDF_NLS
2300 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002301 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#endif
Jan Kara9181f8b2015-08-20 14:50:07 +02002303 if (lvid_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002305 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002306 udf_sb_free_partitions(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 kfree(sbi);
2308 sb->s_fs_info = NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002309
Jan Karad759bfa42013-07-25 19:10:59 +02002310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311}
2312
Joe Perches8076c362011-10-10 01:08:03 -07002313void _udf_err(struct super_block *sb, const char *function,
2314 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Joe Perchesc2bff362011-10-10 01:08:06 -07002316 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 va_list args;
2318
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002320
2321 vaf.fmt = fmt;
2322 vaf.va = &args;
2323
2324 pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf);
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Joe Perchesa40ecd72011-10-10 01:08:04 -07002329void _udf_warn(struct super_block *sb, const char *function,
2330 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Joe Perchesc2bff362011-10-10 01:08:06 -07002332 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 va_list args;
2334
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002335 va_start(args, fmt);
Joe Perchesc2bff362011-10-10 01:08:06 -07002336
2337 vaf.fmt = fmt;
2338 vaf.va = &args;
2339
2340 pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf);
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 va_end(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002345static void udf_put_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002347 struct udf_sb_info *sbi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002349 sbi = UDF_SB(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +02002350
Markus Elfring0d454e42014-11-18 17:17:46 +01002351 iput(sbi->s_vat_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352#ifdef CONFIG_UDF_NLS
2353 if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP))
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002354 unload_nls(sbi->s_nls_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355#endif
David Howellsbc98a422017-07-17 08:45:34 +01002356 if (!sb_rdonly(sb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 udf_close_lvid(sb);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002358 brelse(sbi->s_lvid_bh);
Jan Karabff943a2012-06-27 22:27:05 +02002359 udf_sb_free_partitions(sb);
Fabian Frederickbbe48dd2015-01-10 19:13:32 +01002360 mutex_destroy(&sbi->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 kfree(sb->s_fs_info);
2362 sb->s_fs_info = NULL;
2363}
2364
Jan Kara146bca72009-03-16 18:27:37 +01002365static int udf_sync_fs(struct super_block *sb, int wait)
2366{
2367 struct udf_sb_info *sbi = UDF_SB(sb);
2368
2369 mutex_lock(&sbi->s_alloc_mutex);
2370 if (sbi->s_lvid_dirty) {
2371 /*
2372 * Blockdevice will be synced later so we don't have to submit
2373 * the buffer for IO
2374 */
2375 mark_buffer_dirty(sbi->s_lvid_bh);
Jan Kara146bca72009-03-16 18:27:37 +01002376 sbi->s_lvid_dirty = 0;
2377 }
2378 mutex_unlock(&sbi->s_alloc_mutex);
2379
2380 return 0;
2381}
2382
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002383static int udf_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
David Howells726c3342006-06-23 02:02:58 -07002385 struct super_block *sb = dentry->d_sb;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002386 struct udf_sb_info *sbi = UDF_SB(sb);
2387 struct logicalVolIntegrityDescImpUse *lvidiu;
Coly Li557f5a12009-01-20 01:36:55 +08002388 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002389
Jan Kara69d75672013-09-12 22:00:15 +02002390 lvidiu = udf_sb_lvidiu(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 buf->f_type = UDF_SUPER_MAGIC;
2392 buf->f_bsize = sb->s_blocksize;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002393 buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 buf->f_bfree = udf_count_free(sb);
2395 buf->f_bavail = buf->f_bfree;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002396 buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) +
2397 le32_to_cpu(lvidiu->numDirs)) : 0)
2398 + buf->f_bfree;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 buf->f_ffree = buf->f_bfree;
Andrew Gabbasov9fba7052016-01-15 02:44:21 -06002400 buf->f_namelen = UDF_NAME_LEN;
Coly Li557f5a12009-01-20 01:36:55 +08002401 buf->f_fsid.val[0] = (u32)id;
2402 buf->f_fsid.val[1] = (u32)(id >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 return 0;
2405}
2406
Marcin Slusarz4b111112008-02-08 04:20:36 -08002407static unsigned int udf_count_free_bitmap(struct super_block *sb,
2408 struct udf_bitmap *bitmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 struct buffer_head *bh = NULL;
2411 unsigned int accum = 0;
2412 int index;
Steve Magnanib490bdd2017-10-12 08:48:40 -05002413 udf_pblk_t block = 0, newblock;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002414 struct kernel_lb_addr loc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 uint32_t bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 uint8_t *ptr;
2417 uint16_t ident;
2418 struct spaceBitmapDesc *bm;
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 loc.logicalBlockNum = bitmap->s_extPosition;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002421 loc.partitionReferenceNum = UDF_SB(sb)->s_partition;
Pekka Enberg97e961f2008-10-15 12:29:03 +02002422 bh = udf_read_ptagged(sb, &loc, 0, &ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002424 if (!bh) {
Joe Perches78ace702011-10-10 01:08:05 -07002425 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto out;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002427 } else if (ident != TAG_IDENT_SBD) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002428 brelse(bh);
Joe Perches78ace702011-10-10 01:08:05 -07002429 udf_err(sb, "udf_count_free failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 goto out;
2431 }
2432
2433 bm = (struct spaceBitmapDesc *)bh->b_data;
2434 bytes = le32_to_cpu(bm->numOfBytes);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002435 index = sizeof(struct spaceBitmapDesc); /* offset in first block only */
2436 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002438 while (bytes > 0) {
Marcin Slusarz01b954a2008-02-02 22:37:07 +01002439 u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index);
2440 accum += bitmap_weight((const unsigned long *)(ptr + index),
2441 cur_bytes * 8);
2442 bytes -= cur_bytes;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002443 if (bytes) {
Jan Kara3bf25cb2007-05-08 00:35:16 -07002444 brelse(bh);
Pekka Enberg97e961f2008-10-15 12:29:03 +02002445 newblock = udf_get_lb_pblock(sb, &loc, ++block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 bh = udf_tread(sb, newblock);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002447 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 udf_debug("read failed\n");
2449 goto out;
2450 }
2451 index = 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002452 ptr = (uint8_t *)bh->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454 }
Jan Kara3bf25cb2007-05-08 00:35:16 -07002455 brelse(bh);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return accum;
2458}
2459
Marcin Slusarz4b111112008-02-08 04:20:36 -08002460static unsigned int udf_count_free_table(struct super_block *sb,
2461 struct inode *table)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
2463 unsigned int accum = 0;
Jan Karaff116fc2007-05-08 00:35:14 -07002464 uint32_t elen;
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +02002465 struct kernel_lb_addr eloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int8_t etype;
Jan Karaff116fc2007-05-08 00:35:14 -07002467 struct extent_position epos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Jan Karad1668fe2010-10-20 23:24:12 +02002469 mutex_lock(&UDF_SB(sb)->s_alloc_mutex);
Marcin Slusarzc0b34432008-02-08 04:20:42 -08002470 epos.block = UDF_I(table)->i_location;
Jan Karaff116fc2007-05-08 00:35:14 -07002471 epos.offset = sizeof(struct unallocSpaceEntry);
2472 epos.bh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002474 while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 accum += (elen >> table->i_sb->s_blocksize_bits);
Marcin Slusarz3a71fc52008-02-08 04:20:28 -08002476
Jan Kara3bf25cb2007-05-08 00:35:16 -07002477 brelse(epos.bh);
Jan Karad1668fe2010-10-20 23:24:12 +02002478 mutex_unlock(&UDF_SB(sb)->s_alloc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 return accum;
2481}
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -07002482
2483static unsigned int udf_count_free(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 unsigned int accum = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002486 struct udf_sb_info *sbi;
2487 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002489 sbi = UDF_SB(sb);
2490 if (sbi->s_lvid_bh) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002491 struct logicalVolIntegrityDesc *lvid =
2492 (struct logicalVolIntegrityDesc *)
2493 sbi->s_lvid_bh->b_data;
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002494 if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
Marcin Slusarz4b111112008-02-08 04:20:36 -08002495 accum = le32_to_cpu(
2496 lvid->freeSpaceTable[sbi->s_partition]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (accum == 0xFFFFFFFF)
2498 accum = 0;
2499 }
2500 }
2501
2502 if (accum)
2503 return accum;
2504
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002505 map = &sbi->s_partmaps[sbi->s_partition];
2506 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002507 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002508 map->s_uspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002510 if (map->s_partition_flags & UDF_PART_FLAG_FREED_BITMAP) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002511 accum += udf_count_free_bitmap(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002512 map->s_fspace.s_bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514 if (accum)
2515 return accum;
2516
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002517 if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002518 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002519 map->s_uspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002521 if (map->s_partition_flags & UDF_PART_FLAG_FREED_TABLE) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -07002522 accum += udf_count_free_table(sb,
Marcin Slusarz6c79e982008-02-08 04:20:30 -08002523 map->s_fspace.s_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
2526 return accum;
2527}
Fabian Frederick54bb60d2017-01-06 21:53:57 +01002528
2529MODULE_AUTHOR("Ben Fennema");
2530MODULE_DESCRIPTION("Universal Disk Format Filesystem");
2531MODULE_LICENSE("GPL");
2532module_init(init_udf_fs)
2533module_exit(exit_udf_fs)