Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * 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 Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 36 | * 12/06/98 blf really hosed things royally. vat/sparing support. sequenced |
| 37 | * vol descs. rewrote option handling based on isofs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * 12/20/98 find the free space bitmap (if it exists) |
| 39 | */ |
| 40 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 41 | #include "udfdecl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <linux/vfs.h> |
| 52 | #include <linux/vmalloc.h> |
Marcin Slusarz | dc5d39b | 2008-02-08 04:20:32 -0800 | [diff] [blame] | 53 | #include <linux/errno.h> |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 54 | #include <linux/mount.h> |
| 55 | #include <linux/seq_file.h> |
Marcin Slusarz | 01b954a | 2008-02-02 22:37:07 +0100 | [diff] [blame] | 56 | #include <linux/bitmap.h> |
Bob Copeland | f845fce | 2008-04-17 09:47:48 +0200 | [diff] [blame] | 57 | #include <linux/crc-itu-t.h> |
Jan Kara | 1df2ae3 | 2012-06-27 21:23:07 +0200 | [diff] [blame] | 58 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <asm/byteorder.h> |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #include "udf_sb.h" |
| 62 | #include "udf_i.h" |
| 63 | |
| 64 | #include <linux/init.h> |
Fabian Frederick | e973606 | 2014-06-18 19:38:24 +0200 | [diff] [blame] | 65 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Jan Kara | 4b8d425 | 2018-02-09 12:52:28 +0100 | [diff] [blame] | 67 | enum { |
| 68 | VDS_POS_PRIMARY_VOL_DESC, |
| 69 | VDS_POS_UNALLOC_SPACE_DESC, |
| 70 | VDS_POS_LOGICAL_VOL_DESC, |
Jan Kara | 4b8d425 | 2018-02-09 12:52:28 +0100 | [diff] [blame] | 71 | VDS_POS_IMP_USE_VOL_DESC, |
Jan Kara | 4b8d425 | 2018-02-09 12:52:28 +0100 | [diff] [blame] | 72 | VDS_POS_LENGTH |
| 73 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 75 | #define VSD_FIRST_SECTOR_OFFSET 32768 |
| 76 | #define VSD_MAX_SECTOR_OFFSET 0x800000 |
| 77 | |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 78 | /* |
| 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 Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 87 | enum { UDF_MAX_LINKS = 0xffff }; |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* These are the "meat" - everything else is stuffing */ |
| 90 | static int udf_fill_super(struct super_block *, void *, int); |
| 91 | static void udf_put_super(struct super_block *); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 92 | static int udf_sync_fs(struct super_block *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | static int udf_remount_fs(struct super_block *, int *, char *); |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 94 | static void udf_load_logicalvolint(struct super_block *, struct kernel_extent_ad); |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 95 | static int udf_find_fileset(struct super_block *, struct kernel_lb_addr *, |
| 96 | struct kernel_lb_addr *); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 97 | static void udf_load_fileset(struct super_block *, struct buffer_head *, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 98 | struct kernel_lb_addr *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | static void udf_open_lvid(struct super_block *); |
| 100 | static void udf_close_lvid(struct super_block *); |
| 101 | static unsigned int udf_count_free(struct super_block *); |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 102 | static int udf_statfs(struct dentry *, struct kstatfs *); |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 103 | static int udf_show_options(struct seq_file *, struct dentry *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 105 | struct logicalVolIntegrityDescImpUse *udf_sb_lvidiu(struct super_block *sb) |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 106 | { |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 107 | 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 Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 124 | return (struct logicalVolIntegrityDescImpUse *)&(lvid->impUse[offset]); |
| 125 | } |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | /* UDF filesystem type */ |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 128 | static struct dentry *udf_mount(struct file_system_type *fs_type, |
| 129 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 131 | return mount_bdev(fs_type, flags, dev_name, data, udf_fill_super); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static struct file_system_type udf_fstype = { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 135 | .owner = THIS_MODULE, |
| 136 | .name = "udf", |
Al Viro | 152a083 | 2010-07-25 00:46:55 +0400 | [diff] [blame] | 137 | .mount = udf_mount, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 138 | .kill_sb = kill_block_super, |
| 139 | .fs_flags = FS_REQUIRES_DEV, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | }; |
Eric W. Biederman | 3e64fe5 | 2013-03-11 07:05:42 -0700 | [diff] [blame] | 141 | MODULE_ALIAS_FS("udf"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 143 | static struct kmem_cache *udf_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | static struct inode *udf_alloc_inode(struct super_block *sb) |
| 146 | { |
| 147 | struct udf_inode_info *ei; |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 148 | ei = kmem_cache_alloc(udf_inode_cachep, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (!ei) |
| 150 | return NULL; |
Dan Bastone | 95f8797 | 2006-08-13 23:24:18 -0700 | [diff] [blame] | 151 | |
| 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 Bogani | 4d0fb62 | 2010-11-16 18:40:47 +0100 | [diff] [blame] | 157 | init_rwsem(&ei->i_data_sem); |
Namjae Jeon | 9960005 | 2013-01-19 11:17:14 +0900 | [diff] [blame] | 158 | ei->cached_extent.lstart = -1; |
| 159 | spin_lock_init(&ei->i_extent_cache_lock); |
Dan Bastone | 95f8797 | 2006-08-13 23:24:18 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | return &ei->vfs_inode; |
| 162 | } |
| 163 | |
Al Viro | a78bb38 | 2019-04-15 22:25:06 -0400 | [diff] [blame] | 164 | static void udf_free_in_core_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 165 | { |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 166 | kmem_cache_free(udf_inode_cachep, UDF_I(inode)); |
| 167 | } |
| 168 | |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 169 | static void init_once(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 171 | struct udf_inode_info *ei = (struct udf_inode_info *)foo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 173 | ei->i_ext.i_data = NULL; |
| 174 | inode_init_once(&ei->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Fabian Frederick | 53ea18d | 2014-02-01 15:45:18 +0800 | [diff] [blame] | 177 | static int __init init_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | udf_inode_cachep = kmem_cache_create("udf_inode_cache", |
| 180 | sizeof(struct udf_inode_info), |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 181 | 0, (SLAB_RECLAIM_ACCOUNT | |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 182 | SLAB_MEM_SPREAD | |
| 183 | SLAB_ACCOUNT), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 184 | init_once); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 185 | if (!udf_inode_cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | return -ENOMEM; |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static void destroy_inodecache(void) |
| 191 | { |
Kirill A. Shutemov | 8c0a853 | 2012-09-26 11:33:07 +1000 | [diff] [blame] | 192 | /* |
| 193 | * Make sure all delayed rcu free inodes are flushed before we |
| 194 | * destroy cache. |
| 195 | */ |
| 196 | rcu_barrier(); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 197 | kmem_cache_destroy(udf_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Superblock operations */ |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 201 | static const struct super_operations udf_sb_ops = { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 202 | .alloc_inode = udf_alloc_inode, |
Al Viro | a78bb38 | 2019-04-15 22:25:06 -0400 | [diff] [blame] | 203 | .free_inode = udf_free_in_core_inode, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 204 | .write_inode = udf_write_inode, |
Al Viro | 3aac2b6 | 2010-06-07 00:43:39 -0400 | [diff] [blame] | 205 | .evict_inode = udf_evict_inode, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 206 | .put_super = udf_put_super, |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 207 | .sync_fs = udf_sync_fs, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 208 | .statfs = udf_statfs, |
| 209 | .remount_fs = udf_remount_fs, |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 210 | .show_options = udf_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 213 | struct udf_options { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | unsigned char novrs; |
| 215 | unsigned int blocksize; |
| 216 | unsigned int session; |
| 217 | unsigned int lastblock; |
| 218 | unsigned int anchor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | unsigned int flags; |
Al Viro | faa1729 | 2011-07-26 03:18:29 -0400 | [diff] [blame] | 220 | umode_t umask; |
Eric W. Biederman | c2ba138 | 2012-02-10 12:20:35 -0800 | [diff] [blame] | 221 | kgid_t gid; |
| 222 | kuid_t uid; |
Al Viro | faa1729 | 2011-07-26 03:18:29 -0400 | [diff] [blame] | 223 | umode_t fmode; |
| 224 | umode_t dmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | struct nls_table *nls_map; |
| 226 | }; |
| 227 | |
| 228 | static int __init init_udf_fs(void) |
| 229 | { |
| 230 | int err; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | err = init_inodecache(); |
| 233 | if (err) |
| 234 | goto out1; |
| 235 | err = register_filesystem(&udf_fstype); |
| 236 | if (err) |
| 237 | goto out; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 238 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 240 | |
| 241 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | destroy_inodecache(); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 243 | |
| 244 | out1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return err; |
| 246 | } |
| 247 | |
| 248 | static void __exit exit_udf_fs(void) |
| 249 | { |
| 250 | unregister_filesystem(&udf_fstype); |
| 251 | destroy_inodecache(); |
| 252 | } |
| 253 | |
Marcin Slusarz | dc5d39b | 2008-02-08 04:20:32 -0800 | [diff] [blame] | 254 | static int udf_sb_alloc_partition_maps(struct super_block *sb, u32 count) |
| 255 | { |
| 256 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 257 | |
Markus Elfring | 033c9da | 2017-08-15 16:45:44 +0200 | [diff] [blame] | 258 | sbi->s_partmaps = kcalloc(count, sizeof(*sbi->s_partmaps), GFP_KERNEL); |
Marcin Slusarz | dc5d39b | 2008-02-08 04:20:32 -0800 | [diff] [blame] | 259 | if (!sbi->s_partmaps) { |
Marcin Slusarz | dc5d39b | 2008-02-08 04:20:32 -0800 | [diff] [blame] | 260 | sbi->s_partitions = 0; |
| 261 | return -ENOMEM; |
| 262 | } |
| 263 | |
| 264 | sbi->s_partitions = count; |
| 265 | return 0; |
| 266 | } |
| 267 | |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 268 | static void udf_sb_free_bitmap(struct udf_bitmap *bitmap) |
| 269 | { |
| 270 | int i; |
| 271 | int nr_groups = bitmap->s_nr_groups; |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 272 | |
| 273 | for (i = 0; i < nr_groups; i++) |
| 274 | if (bitmap->s_block_bitmap[i]) |
| 275 | brelse(bitmap->s_block_bitmap[i]); |
| 276 | |
Tetsuo Handa | 1d5cfdb | 2016-01-22 15:11:02 -0800 | [diff] [blame] | 277 | kvfree(bitmap); |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void udf_free_partition(struct udf_part_map *map) |
| 281 | { |
| 282 | int i; |
| 283 | struct udf_meta_data *mdata; |
| 284 | |
| 285 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) |
| 286 | iput(map->s_uspace.s_table); |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 287 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) |
| 288 | udf_sb_free_bitmap(map->s_uspace.s_bitmap); |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 289 | if (map->s_partition_type == UDF_SPARABLE_MAP15) |
| 290 | for (i = 0; i < 4; i++) |
| 291 | brelse(map->s_type_specific.s_sparing.s_spar_map[i]); |
| 292 | else if (map->s_partition_type == UDF_METADATA_MAP25) { |
| 293 | mdata = &map->s_type_specific.s_metadata; |
| 294 | iput(mdata->s_metadata_fe); |
| 295 | mdata->s_metadata_fe = NULL; |
| 296 | |
| 297 | iput(mdata->s_mirror_fe); |
| 298 | mdata->s_mirror_fe = NULL; |
| 299 | |
| 300 | iput(mdata->s_bitmap_fe); |
| 301 | mdata->s_bitmap_fe = NULL; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static void udf_sb_free_partitions(struct super_block *sb) |
| 306 | { |
| 307 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 308 | int i; |
Markus Elfring | ba2eb86 | 2017-08-15 17:03:06 +0200 | [diff] [blame] | 309 | |
| 310 | if (!sbi->s_partmaps) |
Namjae Jeon | 1b1baff | 2013-01-14 22:53:47 +0100 | [diff] [blame] | 311 | return; |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 312 | for (i = 0; i < sbi->s_partitions; i++) |
| 313 | udf_free_partition(&sbi->s_partmaps[i]); |
| 314 | kfree(sbi->s_partmaps); |
| 315 | sbi->s_partmaps = NULL; |
| 316 | } |
| 317 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 318 | static int udf_show_options(struct seq_file *seq, struct dentry *root) |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 319 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 320 | struct super_block *sb = root->d_sb; |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 321 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 322 | |
| 323 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) |
| 324 | seq_puts(seq, ",nostrict"); |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 325 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_BLOCKSIZE_SET)) |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 326 | seq_printf(seq, ",bs=%lu", sb->s_blocksize); |
| 327 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNHIDE)) |
| 328 | seq_puts(seq, ",unhide"); |
| 329 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UNDELETE)) |
| 330 | seq_puts(seq, ",undelete"); |
| 331 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_USE_AD_IN_ICB)) |
| 332 | seq_puts(seq, ",noadinicb"); |
| 333 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_USE_SHORT_AD)) |
| 334 | seq_puts(seq, ",shortad"); |
| 335 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_FORGET)) |
| 336 | seq_puts(seq, ",uid=forget"); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 337 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_FORGET)) |
| 338 | seq_puts(seq, ",gid=forget"); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 339 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UID_SET)) |
Eric W. Biederman | c2ba138 | 2012-02-10 12:20:35 -0800 | [diff] [blame] | 340 | seq_printf(seq, ",uid=%u", from_kuid(&init_user_ns, sbi->s_uid)); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 341 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_GID_SET)) |
Eric W. Biederman | c2ba138 | 2012-02-10 12:20:35 -0800 | [diff] [blame] | 342 | seq_printf(seq, ",gid=%u", from_kgid(&init_user_ns, sbi->s_gid)); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 343 | if (sbi->s_umask != 0) |
Al Viro | faa1729 | 2011-07-26 03:18:29 -0400 | [diff] [blame] | 344 | seq_printf(seq, ",umask=%ho", sbi->s_umask); |
Marcin Slusarz | 87bc730 | 2008-12-02 13:40:11 +0100 | [diff] [blame] | 345 | if (sbi->s_fmode != UDF_INVALID_MODE) |
Al Viro | faa1729 | 2011-07-26 03:18:29 -0400 | [diff] [blame] | 346 | seq_printf(seq, ",mode=%ho", sbi->s_fmode); |
Marcin Slusarz | 87bc730 | 2008-12-02 13:40:11 +0100 | [diff] [blame] | 347 | if (sbi->s_dmode != UDF_INVALID_MODE) |
Al Viro | faa1729 | 2011-07-26 03:18:29 -0400 | [diff] [blame] | 348 | seq_printf(seq, ",dmode=%ho", sbi->s_dmode); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 349 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_SESSION_SET)) |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 350 | seq_printf(seq, ",session=%d", sbi->s_session); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 351 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_LASTBLOCK_SET)) |
| 352 | seq_printf(seq, ",lastblock=%u", sbi->s_last_block); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 353 | if (sbi->s_anchor != 0) |
| 354 | seq_printf(seq, ",anchor=%u", sbi->s_anchor); |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 355 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_UTF8)) |
| 356 | seq_puts(seq, ",utf8"); |
| 357 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP) && sbi->s_nls_map) |
| 358 | seq_printf(seq, ",iocharset=%s", sbi->s_nls_map->charset); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* |
| 364 | * udf_parse_options |
| 365 | * |
| 366 | * PURPOSE |
| 367 | * Parse mount options. |
| 368 | * |
| 369 | * DESCRIPTION |
| 370 | * The following mount options are supported: |
| 371 | * |
| 372 | * gid= Set the default group. |
| 373 | * umask= Set the default umask. |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 374 | * mode= Set the default file permissions. |
| 375 | * dmode= Set the default directory permissions. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | * uid= Set the default user. |
| 377 | * bs= Set the block size. |
| 378 | * unhide Show otherwise hidden files. |
| 379 | * undelete Show deleted files in lists. |
| 380 | * adinicb Embed data in the inode (default) |
| 381 | * noadinicb Don't embed data in the inode |
| 382 | * shortad Use short ad's |
| 383 | * longad Use long ad's (default) |
| 384 | * nostrict Unset strict conformance |
| 385 | * iocharset= Set the NLS character set |
| 386 | * |
| 387 | * The remaining are for debugging and disaster recovery: |
| 388 | * |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 389 | * novrs Skip volume sequence recognition |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | * |
| 391 | * The following expect a offset from 0. |
| 392 | * |
| 393 | * session= Set the CDROM session (default= last session) |
| 394 | * anchor= Override standard anchor location. (default= 256) |
| 395 | * volume= Override the VolumeDesc location. (unused) |
| 396 | * partition= Override the PartitionDesc location. (unused) |
| 397 | * lastblock= Set the last block of the filesystem/ |
| 398 | * |
| 399 | * The following expect a offset from the partition root. |
| 400 | * |
| 401 | * fileset= Override the fileset block location. (unused) |
| 402 | * rootdir= Override the root directory location. (unused) |
| 403 | * WARNING: overriding the rootdir to a non-directory may |
| 404 | * yield highly unpredictable results. |
| 405 | * |
| 406 | * PRE-CONDITIONS |
| 407 | * options Pointer to mount options string. |
| 408 | * uopts Pointer to mount options variable. |
| 409 | * |
| 410 | * POST-CONDITIONS |
| 411 | * <return> 1 Mount options parsed okay. |
| 412 | * <return> 0 Error parsing mount options. |
| 413 | * |
| 414 | * HISTORY |
| 415 | * July 1, 1997 - Andrew E. Mileski |
| 416 | * Written, tested, and released. |
| 417 | */ |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | enum { |
| 420 | Opt_novrs, Opt_nostrict, Opt_bs, Opt_unhide, Opt_undelete, |
| 421 | Opt_noadinicb, Opt_adinicb, Opt_shortad, Opt_longad, |
| 422 | Opt_gid, Opt_uid, Opt_umask, Opt_session, Opt_lastblock, |
| 423 | Opt_anchor, Opt_volume, Opt_partition, Opt_fileset, |
| 424 | Opt_rootdir, Opt_utf8, Opt_iocharset, |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 425 | Opt_err, Opt_uforget, Opt_uignore, Opt_gforget, Opt_gignore, |
| 426 | Opt_fmode, Opt_dmode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | }; |
| 428 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 429 | static const match_table_t tokens = { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 430 | {Opt_novrs, "novrs"}, |
| 431 | {Opt_nostrict, "nostrict"}, |
| 432 | {Opt_bs, "bs=%u"}, |
| 433 | {Opt_unhide, "unhide"}, |
| 434 | {Opt_undelete, "undelete"}, |
| 435 | {Opt_noadinicb, "noadinicb"}, |
| 436 | {Opt_adinicb, "adinicb"}, |
| 437 | {Opt_shortad, "shortad"}, |
| 438 | {Opt_longad, "longad"}, |
| 439 | {Opt_uforget, "uid=forget"}, |
| 440 | {Opt_uignore, "uid=ignore"}, |
| 441 | {Opt_gforget, "gid=forget"}, |
| 442 | {Opt_gignore, "gid=ignore"}, |
| 443 | {Opt_gid, "gid=%u"}, |
| 444 | {Opt_uid, "uid=%u"}, |
| 445 | {Opt_umask, "umask=%o"}, |
| 446 | {Opt_session, "session=%u"}, |
| 447 | {Opt_lastblock, "lastblock=%u"}, |
| 448 | {Opt_anchor, "anchor=%u"}, |
| 449 | {Opt_volume, "volume=%u"}, |
| 450 | {Opt_partition, "partition=%u"}, |
| 451 | {Opt_fileset, "fileset=%u"}, |
| 452 | {Opt_rootdir, "rootdir=%u"}, |
| 453 | {Opt_utf8, "utf8"}, |
| 454 | {Opt_iocharset, "iocharset=%s"}, |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 455 | {Opt_fmode, "mode=%o"}, |
| 456 | {Opt_dmode, "dmode=%o"}, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 457 | {Opt_err, NULL} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | }; |
| 459 | |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 460 | static int udf_parse_options(char *options, struct udf_options *uopt, |
| 461 | bool remount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
| 463 | char *p; |
| 464 | int option; |
| 465 | |
| 466 | uopt->novrs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | uopt->session = 0xFFFFFFFF; |
| 468 | uopt->lastblock = 0; |
| 469 | uopt->anchor = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | if (!options) |
| 472 | return 1; |
| 473 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 474 | while ((p = strsep(&options, ",")) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | substring_t args[MAX_OPT_ARGS]; |
| 476 | int token; |
Fabian Frederick | 8c6915a | 2014-01-29 17:13:16 +0800 | [diff] [blame] | 477 | unsigned n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | if (!*p) |
| 479 | continue; |
| 480 | |
| 481 | token = match_token(p, tokens, args); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 482 | switch (token) { |
| 483 | case Opt_novrs: |
| 484 | uopt->novrs = 1; |
Clemens Ladisch | 4136801 | 2009-03-06 09:16:49 +0100 | [diff] [blame] | 485 | break; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 486 | case Opt_bs: |
| 487 | if (match_int(&args[0], &option)) |
| 488 | return 0; |
Fabian Frederick | 8c6915a | 2014-01-29 17:13:16 +0800 | [diff] [blame] | 489 | n = option; |
| 490 | if (n != 512 && n != 1024 && n != 2048 && n != 4096) |
| 491 | return 0; |
| 492 | uopt->blocksize = n; |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 493 | uopt->flags |= (1 << UDF_FLAG_BLOCKSIZE_SET); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 494 | break; |
| 495 | case Opt_unhide: |
| 496 | uopt->flags |= (1 << UDF_FLAG_UNHIDE); |
| 497 | break; |
| 498 | case Opt_undelete: |
| 499 | uopt->flags |= (1 << UDF_FLAG_UNDELETE); |
| 500 | break; |
| 501 | case Opt_noadinicb: |
| 502 | uopt->flags &= ~(1 << UDF_FLAG_USE_AD_IN_ICB); |
| 503 | break; |
| 504 | case Opt_adinicb: |
| 505 | uopt->flags |= (1 << UDF_FLAG_USE_AD_IN_ICB); |
| 506 | break; |
| 507 | case Opt_shortad: |
| 508 | uopt->flags |= (1 << UDF_FLAG_USE_SHORT_AD); |
| 509 | break; |
| 510 | case Opt_longad: |
| 511 | uopt->flags &= ~(1 << UDF_FLAG_USE_SHORT_AD); |
| 512 | break; |
| 513 | case Opt_gid: |
| 514 | if (match_int(args, &option)) |
| 515 | return 0; |
Eric W. Biederman | c2ba138 | 2012-02-10 12:20:35 -0800 | [diff] [blame] | 516 | uopt->gid = make_kgid(current_user_ns(), option); |
| 517 | if (!gid_valid(uopt->gid)) |
| 518 | return 0; |
Cyrill Gorcunov | ca76d2d | 2007-07-31 00:39:40 -0700 | [diff] [blame] | 519 | uopt->flags |= (1 << UDF_FLAG_GID_SET); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 520 | break; |
| 521 | case Opt_uid: |
| 522 | if (match_int(args, &option)) |
| 523 | return 0; |
Eric W. Biederman | c2ba138 | 2012-02-10 12:20:35 -0800 | [diff] [blame] | 524 | uopt->uid = make_kuid(current_user_ns(), option); |
| 525 | if (!uid_valid(uopt->uid)) |
| 526 | return 0; |
Cyrill Gorcunov | ca76d2d | 2007-07-31 00:39:40 -0700 | [diff] [blame] | 527 | uopt->flags |= (1 << UDF_FLAG_UID_SET); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 528 | break; |
| 529 | case Opt_umask: |
| 530 | if (match_octal(args, &option)) |
| 531 | return 0; |
| 532 | uopt->umask = option; |
| 533 | break; |
| 534 | case Opt_nostrict: |
| 535 | uopt->flags &= ~(1 << UDF_FLAG_STRICT); |
| 536 | break; |
| 537 | case Opt_session: |
| 538 | if (match_int(args, &option)) |
| 539 | return 0; |
| 540 | uopt->session = option; |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 541 | if (!remount) |
| 542 | uopt->flags |= (1 << UDF_FLAG_SESSION_SET); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 543 | break; |
| 544 | case Opt_lastblock: |
| 545 | if (match_int(args, &option)) |
| 546 | return 0; |
| 547 | uopt->lastblock = option; |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 548 | if (!remount) |
| 549 | uopt->flags |= (1 << UDF_FLAG_LASTBLOCK_SET); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 550 | break; |
| 551 | case Opt_anchor: |
| 552 | if (match_int(args, &option)) |
| 553 | return 0; |
| 554 | uopt->anchor = option; |
| 555 | break; |
| 556 | case Opt_volume: |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 557 | case Opt_partition: |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 558 | case Opt_fileset: |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 559 | case Opt_rootdir: |
Jan Kara | f0c4a81 | 2018-02-22 14:33:04 +0100 | [diff] [blame] | 560 | /* Ignored (never implemented properly) */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 561 | break; |
| 562 | case Opt_utf8: |
| 563 | uopt->flags |= (1 << UDF_FLAG_UTF8); |
| 564 | break; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 565 | case Opt_iocharset: |
Chengguang Xu | 785dffe | 2018-02-25 21:25:07 +0800 | [diff] [blame] | 566 | if (!remount) { |
| 567 | if (uopt->nls_map) |
| 568 | unload_nls(uopt->nls_map); |
Jan Kara | a768a9a | 2019-03-19 09:48:59 +0100 | [diff] [blame] | 569 | /* |
| 570 | * load_nls() failure is handled later in |
| 571 | * udf_fill_super() after all options are |
| 572 | * parsed. |
| 573 | */ |
Chengguang Xu | 785dffe | 2018-02-25 21:25:07 +0800 | [diff] [blame] | 574 | uopt->nls_map = load_nls(args[0].from); |
| 575 | uopt->flags |= (1 << UDF_FLAG_NLS_MAP); |
| 576 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 577 | break; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 578 | case Opt_uforget: |
| 579 | uopt->flags |= (1 << UDF_FLAG_UID_FORGET); |
| 580 | break; |
Jan Kara | 70260e4 | 2018-02-21 17:27:44 +0100 | [diff] [blame] | 581 | case Opt_uignore: |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 582 | case Opt_gignore: |
Jan Kara | 70260e4 | 2018-02-21 17:27:44 +0100 | [diff] [blame] | 583 | /* These options are superseeded by uid=<number> */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 584 | break; |
| 585 | case Opt_gforget: |
| 586 | uopt->flags |= (1 << UDF_FLAG_GID_FORGET); |
| 587 | break; |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 588 | case Opt_fmode: |
| 589 | if (match_octal(args, &option)) |
| 590 | return 0; |
| 591 | uopt->fmode = option & 0777; |
| 592 | break; |
| 593 | case Opt_dmode: |
| 594 | if (match_octal(args, &option)) |
| 595 | return 0; |
| 596 | uopt->dmode = option & 0777; |
| 597 | break; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 598 | default: |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 599 | pr_err("bad mount option \"%s\" or missing value\n", p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | } |
| 603 | return 1; |
| 604 | } |
| 605 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 606 | static int udf_remount_fs(struct super_block *sb, int *flags, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | struct udf_options uopt; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 609 | struct udf_sb_info *sbi = UDF_SB(sb); |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 610 | int error = 0; |
Jan Kara | a9ad01b | 2018-09-06 15:56:10 +0200 | [diff] [blame] | 611 | |
| 612 | if (!(*flags & SB_RDONLY) && UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT)) |
| 613 | return -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | |
Theodore Ts'o | 02b9984 | 2014-03-13 10:14:33 -0400 | [diff] [blame] | 615 | sync_filesystem(sb); |
Jan Kara | e729eac | 2013-07-25 16:15:16 +0200 | [diff] [blame] | 616 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 617 | uopt.flags = sbi->s_flags; |
| 618 | uopt.uid = sbi->s_uid; |
| 619 | uopt.gid = sbi->s_gid; |
| 620 | uopt.umask = sbi->s_umask; |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 621 | uopt.fmode = sbi->s_fmode; |
| 622 | uopt.dmode = sbi->s_dmode; |
Chengguang Xu | 785dffe | 2018-02-25 21:25:07 +0800 | [diff] [blame] | 623 | uopt.nls_map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 625 | if (!udf_parse_options(options, &uopt, true)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | return -EINVAL; |
| 627 | |
Jan Kara | c03cad2 | 2010-10-20 22:17:28 +0200 | [diff] [blame] | 628 | write_lock(&sbi->s_cred_lock); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 629 | sbi->s_flags = uopt.flags; |
| 630 | sbi->s_uid = uopt.uid; |
| 631 | sbi->s_gid = uopt.gid; |
| 632 | sbi->s_umask = uopt.umask; |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 633 | sbi->s_fmode = uopt.fmode; |
| 634 | sbi->s_dmode = uopt.dmode; |
Jan Kara | c03cad2 | 2010-10-20 22:17:28 +0200 | [diff] [blame] | 635 | write_unlock(&sbi->s_cred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 637 | if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb)) |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 638 | goto out_unlock; |
| 639 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 640 | if (*flags & SB_RDONLY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | udf_close_lvid(sb); |
Jan Kara | 3635046 | 2010-05-19 16:28:56 +0200 | [diff] [blame] | 642 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | udf_open_lvid(sb); |
| 644 | |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 645 | out_unlock: |
Christoph Hellwig | c79d967 | 2010-05-19 07:16:40 -0400 | [diff] [blame] | 646 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 649 | /* Check Volume Structure Descriptors (ECMA 167 2/9.1) */ |
| 650 | /* We also check any "CD-ROM Volume Descriptor Set" (ECMA 167 2/8.3.1) */ |
| 651 | static loff_t udf_check_vsd(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | { |
| 653 | struct volStructDesc *vsd = NULL; |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 654 | loff_t sector = VSD_FIRST_SECTOR_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | int sectorsize; |
| 656 | struct buffer_head *bh = NULL; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 657 | int nsr02 = 0; |
| 658 | int nsr03 = 0; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 659 | struct udf_sb_info *sbi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 661 | sbi = UDF_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | if (sb->s_blocksize < sizeof(struct volStructDesc)) |
| 663 | sectorsize = sizeof(struct volStructDesc); |
| 664 | else |
| 665 | sectorsize = sb->s_blocksize; |
| 666 | |
Jan Kara | abdc0eb | 2017-10-16 11:38:11 +0200 | [diff] [blame] | 667 | sector += (((loff_t)sbi->s_session) << sb->s_blocksize_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 669 | udf_debug("Starting at sector %u (%lu byte sectors)\n", |
Sebastian Manciulea | 706047a | 2008-04-14 17:13:01 +0200 | [diff] [blame] | 670 | (unsigned int)(sector >> sb->s_blocksize_bits), |
| 671 | sb->s_blocksize); |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 672 | /* Process the sequence (if applicable). The hard limit on the sector |
| 673 | * offset is arbitrary, hopefully large enough so that all valid UDF |
| 674 | * filesystems will be recognised. There is no mention of an upper |
| 675 | * bound to the size of the volume recognition area in the standard. |
| 676 | * The limit will prevent the code to read all the sectors of a |
| 677 | * specially crafted image (like a bluray disc full of CD001 sectors), |
| 678 | * potentially causing minutes or even hours of uninterruptible I/O |
| 679 | * activity. This actually happened with uninitialised SSD partitions |
| 680 | * (all 0xFF) before the check for the limit and all valid IDs were |
| 681 | * added */ |
| 682 | for (; !nsr02 && !nsr03 && sector < VSD_MAX_SECTOR_OFFSET; |
| 683 | sector += sectorsize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | /* Read a block */ |
| 685 | bh = udf_tread(sb, sector >> sb->s_blocksize_bits); |
| 686 | if (!bh) |
| 687 | break; |
| 688 | |
| 689 | /* Look for ISO descriptors */ |
| 690 | vsd = (struct volStructDesc *)(bh->b_data + |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 691 | (sector & (sb->s_blocksize - 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 693 | if (!strncmp(vsd->stdIdent, VSD_STD_ID_CD001, |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 694 | VSD_STD_ID_LEN)) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 695 | switch (vsd->structType) { |
| 696 | case 0: |
| 697 | udf_debug("ISO9660 Boot Record found\n"); |
| 698 | break; |
| 699 | case 1: |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 700 | udf_debug("ISO9660 Primary Volume Descriptor found\n"); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 701 | break; |
| 702 | case 2: |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 703 | udf_debug("ISO9660 Supplementary Volume Descriptor found\n"); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 704 | break; |
| 705 | case 3: |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 706 | udf_debug("ISO9660 Volume Partition Descriptor found\n"); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 707 | break; |
| 708 | case 255: |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 709 | udf_debug("ISO9660 Volume Descriptor Set Terminator found\n"); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 710 | break; |
| 711 | default: |
| 712 | udf_debug("ISO9660 VRS (%u) found\n", |
| 713 | vsd->structType); |
| 714 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 716 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BEA01, |
| 717 | VSD_STD_ID_LEN)) |
| 718 | ; /* nothing */ |
| 719 | else if (!strncmp(vsd->stdIdent, VSD_STD_ID_TEA01, |
| 720 | VSD_STD_ID_LEN)) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 721 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | break; |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 723 | } else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR02, |
| 724 | VSD_STD_ID_LEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | nsr02 = sector; |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 726 | else if (!strncmp(vsd->stdIdent, VSD_STD_ID_NSR03, |
| 727 | VSD_STD_ID_LEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | nsr03 = sector; |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 729 | else if (!strncmp(vsd->stdIdent, VSD_STD_ID_BOOT2, |
| 730 | VSD_STD_ID_LEN)) |
| 731 | ; /* nothing */ |
| 732 | else if (!strncmp(vsd->stdIdent, VSD_STD_ID_CDW02, |
| 733 | VSD_STD_ID_LEN)) |
| 734 | ; /* nothing */ |
| 735 | else { |
| 736 | /* invalid id : end of volume recognition area */ |
| 737 | brelse(bh); |
| 738 | break; |
| 739 | } |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 740 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | if (nsr03) |
| 744 | return nsr03; |
| 745 | else if (nsr02) |
| 746 | return nsr02; |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 747 | else if (!bh && sector - (sbi->s_session << sb->s_blocksize_bits) == |
| 748 | VSD_FIRST_SECTOR_OFFSET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return -1; |
| 750 | else |
| 751 | return 0; |
| 752 | } |
| 753 | |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 754 | static int udf_find_fileset(struct super_block *sb, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 755 | struct kernel_lb_addr *fileset, |
| 756 | struct kernel_lb_addr *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
| 758 | struct buffer_head *bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | uint16_t ident; |
| 760 | |
| 761 | if (fileset->logicalBlockNum != 0xFFFFFFFF || |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 762 | fileset->partitionReferenceNum != 0xFFFF) { |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 763 | bh = udf_read_ptagged(sb, fileset, 0, &ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 765 | if (!bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | return 1; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 767 | } else if (ident != TAG_IDENT_FSD) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 768 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return 1; |
| 770 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 771 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 772 | udf_debug("Fileset at block=%u, partition=%u\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 773 | fileset->logicalBlockNum, |
| 774 | fileset->partitionReferenceNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Jan Kara | 82c82ab | 2018-08-21 12:00:08 +0200 | [diff] [blame] | 776 | UDF_SB(sb)->s_partition = fileset->partitionReferenceNum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | udf_load_fileset(sb, bh, root); |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 778 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return 0; |
| 780 | } |
| 781 | return 1; |
| 782 | } |
| 783 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 784 | /* |
| 785 | * Load primary Volume Descriptor Sequence |
| 786 | * |
| 787 | * Return <0 on error, 0 on success. -EAGAIN is special meaning next sequence |
| 788 | * should be tried. |
| 789 | */ |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 790 | static int udf_load_pvoldesc(struct super_block *sb, sector_t block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | { |
| 792 | struct primaryVolDesc *pvoldesc; |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 793 | uint8_t *outstr; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 794 | struct buffer_head *bh; |
| 795 | uint16_t ident; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 796 | int ret = -ENOMEM; |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 797 | #ifdef UDFFS_DEBUG |
| 798 | struct timestamp *ts; |
| 799 | #endif |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 800 | |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 801 | outstr = kmalloc(128, GFP_NOFS); |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 802 | if (!outstr) |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 803 | return -ENOMEM; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 804 | |
| 805 | bh = udf_read_tagged(sb, block, block, &ident); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 806 | if (!bh) { |
| 807 | ret = -EAGAIN; |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 808 | goto out2; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 809 | } |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 810 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 811 | if (ident != TAG_IDENT_PVD) { |
| 812 | ret = -EIO; |
| 813 | goto out_bh; |
| 814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | |
| 816 | pvoldesc = (struct primaryVolDesc *)bh->b_data; |
| 817 | |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 818 | udf_disk_stamp_to_time(&UDF_SB(sb)->s_record_time, |
| 819 | pvoldesc->recordingDateAndTime); |
Marcin Slusarz | af15a29 | 2008-02-10 11:29:10 +0100 | [diff] [blame] | 820 | #ifdef UDFFS_DEBUG |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 821 | ts = &pvoldesc->recordingDateAndTime; |
| 822 | udf_debug("recording time %04u/%02u/%02u %02u:%02u (%x)\n", |
| 823 | le16_to_cpu(ts->year), ts->month, ts->day, ts->hour, |
| 824 | ts->minute, le16_to_cpu(ts->typeAndTimezone)); |
Marcin Slusarz | af15a29 | 2008-02-10 11:29:10 +0100 | [diff] [blame] | 825 | #endif |
Deepa Dinamani | 0220edd | 2018-05-10 08:26:17 -0700 | [diff] [blame] | 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Jan Kara | e966fc8d | 2018-04-16 15:44:19 +0200 | [diff] [blame] | 828 | ret = udf_dstrCS0toChar(sb, outstr, 31, pvoldesc->volIdent, 32); |
Jan Kara | b54e41f | 2018-11-16 13:43:17 +0100 | [diff] [blame] | 829 | if (ret < 0) { |
| 830 | strcpy(UDF_SB(sb)->s_volume_ident, "InvalidName"); |
| 831 | pr_warn("incorrect volume identification, setting to " |
| 832 | "'InvalidName'\n"); |
| 833 | } else { |
| 834 | strncpy(UDF_SB(sb)->s_volume_ident, outstr, ret); |
| 835 | } |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 836 | udf_debug("volIdent[] = '%s'\n", UDF_SB(sb)->s_volume_ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Jan Kara | e966fc8d | 2018-04-16 15:44:19 +0200 | [diff] [blame] | 838 | ret = udf_dstrCS0toChar(sb, outstr, 127, pvoldesc->volSetIdent, 128); |
Jan Kara | b54e41f | 2018-11-16 13:43:17 +0100 | [diff] [blame] | 839 | if (ret < 0) { |
| 840 | ret = 0; |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 841 | goto out_bh; |
Jan Kara | b54e41f | 2018-11-16 13:43:17 +0100 | [diff] [blame] | 842 | } |
Andrew Gabbasov | 9293fcf | 2016-01-15 02:44:22 -0600 | [diff] [blame] | 843 | outstr[ret] = 0; |
| 844 | udf_debug("volSetIdent[] = '%s'\n", outstr); |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 845 | |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 846 | ret = 0; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 847 | out_bh: |
| 848 | brelse(bh); |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 849 | out2: |
| 850 | kfree(outstr); |
Marcin Slusarz | ba9aadd | 2008-11-16 19:01:44 +0100 | [diff] [blame] | 851 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 854 | struct inode *udf_find_metadata_inode_efe(struct super_block *sb, |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 855 | u32 meta_file_loc, u32 partition_ref) |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 856 | { |
| 857 | struct kernel_lb_addr addr; |
| 858 | struct inode *metadata_fe; |
| 859 | |
| 860 | addr.logicalBlockNum = meta_file_loc; |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 861 | addr.partitionReferenceNum = partition_ref; |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 862 | |
Jan Kara | 6174c2e | 2014-10-09 12:52:16 +0200 | [diff] [blame] | 863 | metadata_fe = udf_iget_special(sb, &addr); |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 864 | |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 865 | if (IS_ERR(metadata_fe)) { |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 866 | udf_warn(sb, "metadata inode efe not found\n"); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 867 | return metadata_fe; |
| 868 | } |
| 869 | if (UDF_I(metadata_fe)->i_alloc_type != ICBTAG_FLAG_AD_SHORT) { |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 870 | udf_warn(sb, "metadata inode efe does not have short allocation descriptors!\n"); |
| 871 | iput(metadata_fe); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 872 | return ERR_PTR(-EIO); |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | return metadata_fe; |
| 876 | } |
| 877 | |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 878 | static int udf_load_metadata_files(struct super_block *sb, int partition, |
| 879 | int type1_index) |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 880 | { |
| 881 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 882 | struct udf_part_map *map; |
| 883 | struct udf_meta_data *mdata; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 884 | struct kernel_lb_addr addr; |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 885 | struct inode *fe; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 886 | |
| 887 | map = &sbi->s_partmaps[partition]; |
| 888 | mdata = &map->s_type_specific.s_metadata; |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 889 | mdata->s_phys_partition_ref = type1_index; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 890 | |
| 891 | /* metadata address */ |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 892 | udf_debug("Metadata file location: block = %u part = %u\n", |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 893 | mdata->s_meta_file_loc, mdata->s_phys_partition_ref); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 894 | |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 895 | fe = udf_find_metadata_inode_efe(sb, mdata->s_meta_file_loc, |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 896 | mdata->s_phys_partition_ref); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 897 | if (IS_ERR(fe)) { |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 898 | /* mirror file entry */ |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 899 | udf_debug("Mirror metadata file location: block = %u part = %u\n", |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 900 | mdata->s_mirror_file_loc, mdata->s_phys_partition_ref); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 901 | |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 902 | fe = udf_find_metadata_inode_efe(sb, mdata->s_mirror_file_loc, |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 903 | mdata->s_phys_partition_ref); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 904 | |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 905 | if (IS_ERR(fe)) { |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 906 | udf_err(sb, "Both metadata and mirror metadata inode efe can not found\n"); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 907 | return PTR_ERR(fe); |
Namjae Jeon | 3080a74 | 2011-10-23 19:28:32 +0900 | [diff] [blame] | 908 | } |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 909 | mdata->s_mirror_fe = fe; |
| 910 | } else |
| 911 | mdata->s_metadata_fe = fe; |
| 912 | |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 913 | |
| 914 | /* |
| 915 | * bitmap file entry |
| 916 | * Note: |
| 917 | * Load only if bitmap file location differs from 0xFFFFFFFF (DCN-5102) |
| 918 | */ |
| 919 | if (mdata->s_bitmap_file_loc != 0xFFFFFFFF) { |
| 920 | addr.logicalBlockNum = mdata->s_bitmap_file_loc; |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 921 | addr.partitionReferenceNum = mdata->s_phys_partition_ref; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 922 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 923 | udf_debug("Bitmap file location: block = %u part = %u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 924 | addr.logicalBlockNum, addr.partitionReferenceNum); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 925 | |
Jan Kara | 6174c2e | 2014-10-09 12:52:16 +0200 | [diff] [blame] | 926 | fe = udf_iget_special(sb, &addr); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 927 | if (IS_ERR(fe)) { |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 928 | if (sb_rdonly(sb)) |
Joe Perches | a40ecd7 | 2011-10-10 01:08:04 -0700 | [diff] [blame] | 929 | udf_warn(sb, "bitmap inode efe not found but it's ok since the disc is mounted read-only\n"); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 930 | else { |
Joe Perches | 8076c36 | 2011-10-10 01:08:03 -0700 | [diff] [blame] | 931 | udf_err(sb, "bitmap inode efe not found and attempted read-write mount\n"); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 932 | return PTR_ERR(fe); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 933 | } |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 934 | } else |
| 935 | mdata->s_bitmap_fe = fe; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | udf_debug("udf_load_metadata_files Ok\n"); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 939 | return 0; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 940 | } |
| 941 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 942 | static void udf_load_fileset(struct super_block *sb, struct buffer_head *bh, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 943 | struct kernel_lb_addr *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
| 945 | struct fileSetDesc *fset; |
| 946 | |
| 947 | fset = (struct fileSetDesc *)bh->b_data; |
| 948 | |
| 949 | *root = lelb_to_cpu(fset->rootDirectoryICB.extLocation); |
| 950 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 951 | UDF_SB(sb)->s_serial_number = le16_to_cpu(fset->descTag.tagSerialNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 953 | udf_debug("Rootdir at block=%u, partition=%u\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 954 | root->logicalBlockNum, root->partitionReferenceNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Marcin Slusarz | 883cb9d1 | 2008-02-08 04:20:34 -0800 | [diff] [blame] | 957 | int udf_compute_nr_groups(struct super_block *sb, u32 partition) |
| 958 | { |
| 959 | struct udf_part_map *map = &UDF_SB(sb)->s_partmaps[partition]; |
Julia Lawall | 8dee00b | 2008-02-14 16:15:45 +0100 | [diff] [blame] | 960 | return DIV_ROUND_UP(map->s_partition_len + |
| 961 | (sizeof(struct spaceBitmapDesc) << 3), |
| 962 | sb->s_blocksize * 8); |
Marcin Slusarz | 883cb9d1 | 2008-02-08 04:20:34 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 965 | static struct udf_bitmap *udf_sb_alloc_bitmap(struct super_block *sb, u32 index) |
| 966 | { |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 967 | struct udf_bitmap *bitmap; |
| 968 | int nr_groups; |
| 969 | int size; |
| 970 | |
Marcin Slusarz | 883cb9d1 | 2008-02-08 04:20:34 -0800 | [diff] [blame] | 971 | nr_groups = udf_compute_nr_groups(sb, index); |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 972 | size = sizeof(struct udf_bitmap) + |
| 973 | (sizeof(struct buffer_head *) * nr_groups); |
| 974 | |
| 975 | if (size <= PAGE_SIZE) |
Joe Perches | ed2ae6f | 2010-11-04 20:08:04 -0700 | [diff] [blame] | 976 | bitmap = kzalloc(size, GFP_KERNEL); |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 977 | else |
Joe Perches | ed2ae6f | 2010-11-04 20:08:04 -0700 | [diff] [blame] | 978 | bitmap = vzalloc(size); /* TODO: get rid of vzalloc */ |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 979 | |
Markus Elfring | ba2eb86 | 2017-08-15 17:03:06 +0200 | [diff] [blame] | 980 | if (!bitmap) |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 981 | return NULL; |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 982 | |
Marcin Slusarz | 66e1da3 | 2008-02-08 04:20:33 -0800 | [diff] [blame] | 983 | bitmap->s_nr_groups = nr_groups; |
| 984 | return bitmap; |
| 985 | } |
| 986 | |
Jan Kara | b085fbe | 2018-09-06 15:46:17 +0200 | [diff] [blame] | 987 | static int check_partition_desc(struct super_block *sb, |
| 988 | struct partitionDesc *p, |
| 989 | struct udf_part_map *map) |
| 990 | { |
| 991 | bool umap, utable, fmap, ftable; |
| 992 | struct partitionHeaderDesc *phd; |
| 993 | |
| 994 | switch (le32_to_cpu(p->accessType)) { |
| 995 | case PD_ACCESS_TYPE_READ_ONLY: |
| 996 | case PD_ACCESS_TYPE_WRITE_ONCE: |
| 997 | case PD_ACCESS_TYPE_REWRITABLE: |
| 998 | case PD_ACCESS_TYPE_NONE: |
| 999 | goto force_ro; |
| 1000 | } |
| 1001 | |
| 1002 | /* No Partition Header Descriptor? */ |
| 1003 | if (strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR02) && |
| 1004 | strcmp(p->partitionContents.ident, PD_PARTITION_CONTENTS_NSR03)) |
| 1005 | goto force_ro; |
| 1006 | |
| 1007 | phd = (struct partitionHeaderDesc *)p->partitionContentsUse; |
| 1008 | utable = phd->unallocSpaceTable.extLength; |
| 1009 | umap = phd->unallocSpaceBitmap.extLength; |
| 1010 | ftable = phd->freedSpaceTable.extLength; |
| 1011 | fmap = phd->freedSpaceBitmap.extLength; |
| 1012 | |
| 1013 | /* No allocation info? */ |
| 1014 | if (!utable && !umap && !ftable && !fmap) |
| 1015 | goto force_ro; |
| 1016 | |
| 1017 | /* We don't support blocks that require erasing before overwrite */ |
| 1018 | if (ftable || fmap) |
| 1019 | goto force_ro; |
| 1020 | /* UDF 2.60: 2.3.3 - no mixing of tables & bitmaps, no VAT. */ |
| 1021 | if (utable && umap) |
| 1022 | goto force_ro; |
| 1023 | |
| 1024 | if (map->s_partition_type == UDF_VIRTUAL_MAP15 || |
| 1025 | map->s_partition_type == UDF_VIRTUAL_MAP20) |
| 1026 | goto force_ro; |
| 1027 | |
| 1028 | return 0; |
| 1029 | force_ro: |
| 1030 | if (!sb_rdonly(sb)) |
| 1031 | return -EACCES; |
| 1032 | UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT); |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1036 | static int udf_fill_partdesc_info(struct super_block *sb, |
| 1037 | struct partitionDesc *p, int p_index) |
| 1038 | { |
| 1039 | struct udf_part_map *map; |
| 1040 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1041 | struct partitionHeaderDesc *phd; |
Jan Kara | b085fbe | 2018-09-06 15:46:17 +0200 | [diff] [blame] | 1042 | int err; |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1043 | |
| 1044 | map = &sbi->s_partmaps[p_index]; |
| 1045 | |
| 1046 | map->s_partition_len = le32_to_cpu(p->partitionLength); /* blocks */ |
| 1047 | map->s_partition_root = le32_to_cpu(p->partitionStartingLocation); |
| 1048 | |
| 1049 | if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_READ_ONLY)) |
| 1050 | map->s_partition_flags |= UDF_PART_FLAG_READ_ONLY; |
| 1051 | if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_WRITE_ONCE)) |
| 1052 | map->s_partition_flags |= UDF_PART_FLAG_WRITE_ONCE; |
| 1053 | if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_REWRITABLE)) |
| 1054 | map->s_partition_flags |= UDF_PART_FLAG_REWRITABLE; |
| 1055 | if (p->accessType == cpu_to_le32(PD_ACCESS_TYPE_OVERWRITABLE)) |
| 1056 | map->s_partition_flags |= UDF_PART_FLAG_OVERWRITABLE; |
| 1057 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1058 | udf_debug("Partition (%d type %x) starts at physical %u, block length %u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1059 | p_index, map->s_partition_type, |
| 1060 | map->s_partition_root, map->s_partition_len); |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1061 | |
Jan Kara | b085fbe | 2018-09-06 15:46:17 +0200 | [diff] [blame] | 1062 | err = check_partition_desc(sb, p, map); |
| 1063 | if (err) |
| 1064 | return err; |
| 1065 | |
| 1066 | /* |
| 1067 | * Skip loading allocation info it we cannot ever write to the fs. |
| 1068 | * This is a correctness thing as we may have decided to force ro mount |
| 1069 | * to avoid allocation info we don't support. |
| 1070 | */ |
| 1071 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_RW_INCOMPAT)) |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1072 | return 0; |
| 1073 | |
| 1074 | phd = (struct partitionHeaderDesc *)p->partitionContentsUse; |
| 1075 | if (phd->unallocSpaceTable.extLength) { |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 1076 | struct kernel_lb_addr loc = { |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1077 | .logicalBlockNum = le32_to_cpu( |
| 1078 | phd->unallocSpaceTable.extPosition), |
| 1079 | .partitionReferenceNum = p_index, |
| 1080 | }; |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1081 | struct inode *inode; |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1082 | |
Jan Kara | 6174c2e | 2014-10-09 12:52:16 +0200 | [diff] [blame] | 1083 | inode = udf_iget_special(sb, &loc); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1084 | if (IS_ERR(inode)) { |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1085 | udf_debug("cannot load unallocSpaceTable (part %d)\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1086 | p_index); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1087 | return PTR_ERR(inode); |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1088 | } |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1089 | map->s_uspace.s_table = inode; |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1090 | map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_TABLE; |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1091 | udf_debug("unallocSpaceTable (part %d) @ %lu\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1092 | p_index, map->s_uspace.s_table->i_ino); |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | if (phd->unallocSpaceBitmap.extLength) { |
| 1096 | struct udf_bitmap *bitmap = udf_sb_alloc_bitmap(sb, p_index); |
| 1097 | if (!bitmap) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1098 | return -ENOMEM; |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1099 | map->s_uspace.s_bitmap = bitmap; |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1100 | bitmap->s_extPosition = le32_to_cpu( |
| 1101 | phd->unallocSpaceBitmap.extPosition); |
| 1102 | map->s_partition_flags |= UDF_PART_FLAG_UNALLOC_BITMAP; |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1103 | udf_debug("unallocSpaceBitmap (part %d) @ %u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1104 | p_index, bitmap->s_extPosition); |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1110 | static void udf_find_vat_block(struct super_block *sb, int p_index, |
| 1111 | int type1_index, sector_t start_block) |
| 1112 | { |
| 1113 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1114 | struct udf_part_map *map = &sbi->s_partmaps[p_index]; |
| 1115 | sector_t vat_block; |
| 1116 | struct kernel_lb_addr ino; |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1117 | struct inode *inode; |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1118 | |
| 1119 | /* |
| 1120 | * VAT file entry is in the last recorded block. Some broken disks have |
| 1121 | * it a few blocks before so try a bit harder... |
| 1122 | */ |
| 1123 | ino.partitionReferenceNum = type1_index; |
| 1124 | for (vat_block = start_block; |
| 1125 | vat_block >= map->s_partition_root && |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1126 | vat_block >= start_block - 3; vat_block--) { |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1127 | ino.logicalBlockNum = vat_block - map->s_partition_root; |
Jan Kara | 6174c2e | 2014-10-09 12:52:16 +0200 | [diff] [blame] | 1128 | inode = udf_iget_special(sb, &ino); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 1129 | if (!IS_ERR(inode)) { |
| 1130 | sbi->s_vat_inode = inode; |
| 1131 | break; |
| 1132 | } |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1136 | static int udf_load_vat(struct super_block *sb, int p_index, int type1_index) |
| 1137 | { |
| 1138 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1139 | struct udf_part_map *map = &sbi->s_partmaps[p_index]; |
Jan Kara | fa5e081 | 2008-04-08 02:08:53 +0200 | [diff] [blame] | 1140 | struct buffer_head *bh = NULL; |
| 1141 | struct udf_inode_info *vati; |
| 1142 | uint32_t pos; |
| 1143 | struct virtualAllocationTable20 *vat20; |
Fabian Frederick | 23bcda1 | 2017-01-06 21:54:41 +0100 | [diff] [blame] | 1144 | sector_t blocks = i_size_read(sb->s_bdev->bd_inode) >> |
| 1145 | sb->s_blocksize_bits; |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1146 | |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1147 | udf_find_vat_block(sb, p_index, type1_index, sbi->s_last_block); |
Jan Kara | 4bf17af | 2009-07-14 19:30:23 +0200 | [diff] [blame] | 1148 | if (!sbi->s_vat_inode && |
| 1149 | sbi->s_last_block != blocks - 1) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1150 | pr_notice("Failed to read VAT inode from the last recorded block (%lu), retrying with the last block of the device (%lu).\n", |
| 1151 | (unsigned long)sbi->s_last_block, |
| 1152 | (unsigned long)blocks - 1); |
Jan Kara | e971b0b | 2009-11-30 19:47:55 +0100 | [diff] [blame] | 1153 | udf_find_vat_block(sb, p_index, type1_index, blocks - 1); |
Jan Kara | 4bf17af | 2009-07-14 19:30:23 +0200 | [diff] [blame] | 1154 | } |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1155 | if (!sbi->s_vat_inode) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1156 | return -EIO; |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1157 | |
| 1158 | if (map->s_partition_type == UDF_VIRTUAL_MAP15) { |
Sebastian Manciulea | 47c9358 | 2008-04-14 17:06:36 +0200 | [diff] [blame] | 1159 | map->s_type_specific.s_virtual.s_start_offset = 0; |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1160 | map->s_type_specific.s_virtual.s_num_entries = |
| 1161 | (sbi->s_vat_inode->i_size - 36) >> 2; |
| 1162 | } else if (map->s_partition_type == UDF_VIRTUAL_MAP20) { |
Jan Kara | fa5e081 | 2008-04-08 02:08:53 +0200 | [diff] [blame] | 1163 | vati = UDF_I(sbi->s_vat_inode); |
| 1164 | if (vati->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) { |
| 1165 | pos = udf_block_map(sbi->s_vat_inode, 0); |
| 1166 | bh = sb_bread(sb, pos); |
| 1167 | if (!bh) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1168 | return -EIO; |
Jan Kara | fa5e081 | 2008-04-08 02:08:53 +0200 | [diff] [blame] | 1169 | vat20 = (struct virtualAllocationTable20 *)bh->b_data; |
| 1170 | } else { |
| 1171 | vat20 = (struct virtualAllocationTable20 *) |
| 1172 | vati->i_ext.i_data; |
| 1173 | } |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1174 | |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1175 | map->s_type_specific.s_virtual.s_start_offset = |
Sebastian Manciulea | 47c9358 | 2008-04-14 17:06:36 +0200 | [diff] [blame] | 1176 | le16_to_cpu(vat20->lengthHeader); |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1177 | map->s_type_specific.s_virtual.s_num_entries = |
| 1178 | (sbi->s_vat_inode->i_size - |
| 1179 | map->s_type_specific.s_virtual. |
| 1180 | s_start_offset) >> 2; |
| 1181 | brelse(bh); |
| 1182 | } |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1186 | /* |
| 1187 | * Load partition descriptor block |
| 1188 | * |
| 1189 | * Returns <0 on error, 0 on success, -EAGAIN is special - try next descriptor |
| 1190 | * sequence. |
| 1191 | */ |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1192 | static int udf_load_partdesc(struct super_block *sb, sector_t block) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | { |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1194 | struct buffer_head *bh; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1195 | struct partitionDesc *p; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1196 | struct udf_part_map *map; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1197 | struct udf_sb_info *sbi = UDF_SB(sb); |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1198 | int i, type1_idx; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1199 | uint16_t partitionNumber; |
| 1200 | uint16_t ident; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1201 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1203 | bh = udf_read_tagged(sb, block, block, &ident); |
| 1204 | if (!bh) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1205 | return -EAGAIN; |
| 1206 | if (ident != TAG_IDENT_PD) { |
| 1207 | ret = 0; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1208 | goto out_bh; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1209 | } |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1210 | |
| 1211 | p = (struct partitionDesc *)bh->b_data; |
| 1212 | partitionNumber = le16_to_cpu(p->partitionNumber); |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1213 | |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 1214 | /* First scan for TYPE1 and SPARABLE partitions */ |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1215 | for (i = 0; i < sbi->s_partitions; i++) { |
| 1216 | map = &sbi->s_partmaps[i]; |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1217 | udf_debug("Searching map: (%u == %u)\n", |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1218 | map->s_partition_num, partitionNumber); |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1219 | if (map->s_partition_num == partitionNumber && |
| 1220 | (map->s_partition_type == UDF_TYPE1_MAP15 || |
| 1221 | map->s_partition_type == UDF_SPARABLE_MAP15)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | break; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1223 | } |
| 1224 | |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1225 | if (i >= sbi->s_partitions) { |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1226 | udf_debug("Partition (%u) not found in partition map\n", |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1227 | partitionNumber); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1228 | ret = 0; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1229 | goto out_bh; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1230 | } |
| 1231 | |
Jan Kara | 3fb38df | 2008-04-02 12:26:36 +0200 | [diff] [blame] | 1232 | ret = udf_fill_partdesc_info(sb, p, i); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1233 | if (ret < 0) |
| 1234 | goto out_bh; |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1235 | |
| 1236 | /* |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1237 | * Now rescan for VIRTUAL or METADATA partitions when SPARABLE and |
| 1238 | * PHYSICAL partitions are already set up |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1239 | */ |
| 1240 | type1_idx = i; |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 1241 | #ifdef UDFFS_DEBUG |
| 1242 | map = NULL; /* supress 'maybe used uninitialized' warning */ |
| 1243 | #endif |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1244 | for (i = 0; i < sbi->s_partitions; i++) { |
| 1245 | map = &sbi->s_partmaps[i]; |
| 1246 | |
| 1247 | if (map->s_partition_num == partitionNumber && |
| 1248 | (map->s_partition_type == UDF_VIRTUAL_MAP15 || |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1249 | map->s_partition_type == UDF_VIRTUAL_MAP20 || |
| 1250 | map->s_partition_type == UDF_METADATA_MAP25)) |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1254 | if (i >= sbi->s_partitions) { |
| 1255 | ret = 0; |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1256 | goto out_bh; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1257 | } |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1258 | |
| 1259 | ret = udf_fill_partdesc_info(sb, p, i); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1260 | if (ret < 0) |
Jan Kara | 38b74a5 | 2008-04-02 16:01:35 +0200 | [diff] [blame] | 1261 | goto out_bh; |
| 1262 | |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1263 | if (map->s_partition_type == UDF_METADATA_MAP25) { |
Alden Tondettar | 7888824 | 2016-05-18 14:09:19 -0700 | [diff] [blame] | 1264 | ret = udf_load_metadata_files(sb, i, type1_idx); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1265 | if (ret < 0) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1266 | udf_err(sb, "error loading MetaData partition map %d\n", |
| 1267 | i); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1268 | goto out_bh; |
| 1269 | } |
| 1270 | } else { |
Jan Kara | e729eac | 2013-07-25 16:15:16 +0200 | [diff] [blame] | 1271 | /* |
| 1272 | * If we have a partition with virtual map, we don't handle |
| 1273 | * writing to it (we overwrite blocks instead of relocating |
| 1274 | * them). |
| 1275 | */ |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 1276 | if (!sb_rdonly(sb)) { |
Jan Kara | e729eac | 2013-07-25 16:15:16 +0200 | [diff] [blame] | 1277 | ret = -EACCES; |
| 1278 | goto out_bh; |
| 1279 | } |
Jan Kara | a9ad01b | 2018-09-06 15:56:10 +0200 | [diff] [blame] | 1280 | UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1281 | ret = udf_load_vat(sb, i, type1_idx); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1282 | if (ret < 0) |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1283 | goto out_bh; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1284 | } |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1285 | ret = 0; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1286 | out_bh: |
Jan Kara | 2e0838f | 2008-04-01 18:08:51 +0200 | [diff] [blame] | 1287 | /* In case loading failed, we handle cleanup in udf_fill_super */ |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1288 | brelse(bh); |
| 1289 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Jan Kara | 1df2ae3 | 2012-06-27 21:23:07 +0200 | [diff] [blame] | 1292 | static int udf_load_sparable_map(struct super_block *sb, |
| 1293 | struct udf_part_map *map, |
| 1294 | struct sparablePartitionMap *spm) |
| 1295 | { |
| 1296 | uint32_t loc; |
| 1297 | uint16_t ident; |
| 1298 | struct sparingTable *st; |
| 1299 | struct udf_sparing_data *sdata = &map->s_type_specific.s_sparing; |
| 1300 | int i; |
| 1301 | struct buffer_head *bh; |
| 1302 | |
| 1303 | map->s_partition_type = UDF_SPARABLE_MAP15; |
| 1304 | sdata->s_packet_len = le16_to_cpu(spm->packetLength); |
| 1305 | if (!is_power_of_2(sdata->s_packet_len)) { |
| 1306 | udf_err(sb, "error loading logical volume descriptor: " |
| 1307 | "Invalid packet length %u\n", |
| 1308 | (unsigned)sdata->s_packet_len); |
| 1309 | return -EIO; |
| 1310 | } |
| 1311 | if (spm->numSparingTables > 4) { |
| 1312 | udf_err(sb, "error loading logical volume descriptor: " |
| 1313 | "Too many sparing tables (%d)\n", |
| 1314 | (int)spm->numSparingTables); |
| 1315 | return -EIO; |
| 1316 | } |
| 1317 | |
| 1318 | for (i = 0; i < spm->numSparingTables; i++) { |
| 1319 | loc = le32_to_cpu(spm->locSparingTable[i]); |
| 1320 | bh = udf_read_tagged(sb, loc, loc, &ident); |
| 1321 | if (!bh) |
| 1322 | continue; |
| 1323 | |
| 1324 | st = (struct sparingTable *)bh->b_data; |
| 1325 | if (ident != 0 || |
| 1326 | strncmp(st->sparingIdent.ident, UDF_ID_SPARING, |
| 1327 | strlen(UDF_ID_SPARING)) || |
| 1328 | sizeof(*st) + le16_to_cpu(st->reallocationTableLen) > |
| 1329 | sb->s_blocksize) { |
| 1330 | brelse(bh); |
| 1331 | continue; |
| 1332 | } |
| 1333 | |
| 1334 | sdata->s_spar_map[i] = bh; |
| 1335 | } |
| 1336 | map->s_partition_func = udf_get_pblock_spar15; |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1340 | static int udf_load_logicalvol(struct super_block *sb, sector_t block, |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 1341 | struct kernel_lb_addr *fileset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | { |
| 1343 | struct logicalVolDesc *lvd; |
Jan Kara | 1df2ae3 | 2012-06-27 21:23:07 +0200 | [diff] [blame] | 1344 | int i, offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | uint8_t type; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1346 | struct udf_sb_info *sbi = UDF_SB(sb); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1347 | struct genericPartitionMap *gpm; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1348 | uint16_t ident; |
| 1349 | struct buffer_head *bh; |
Jan Kara | adee11b | 2012-06-27 20:20:22 +0200 | [diff] [blame] | 1350 | unsigned int table_len; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1351 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1353 | bh = udf_read_tagged(sb, block, block, &ident); |
| 1354 | if (!bh) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1355 | return -EAGAIN; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1356 | BUG_ON(ident != TAG_IDENT_LVD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | lvd = (struct logicalVolDesc *)bh->b_data; |
Jan Kara | adee11b | 2012-06-27 20:20:22 +0200 | [diff] [blame] | 1358 | table_len = le32_to_cpu(lvd->mapTableLength); |
Jan Kara | 57b9655 | 2012-07-10 17:58:04 +0200 | [diff] [blame] | 1359 | if (table_len > sb->s_blocksize - sizeof(*lvd)) { |
Jan Kara | adee11b | 2012-06-27 20:20:22 +0200 | [diff] [blame] | 1360 | udf_err(sb, "error loading logical volume descriptor: " |
| 1361 | "Partition table too long (%u > %lu)\n", table_len, |
| 1362 | sb->s_blocksize - sizeof(*lvd)); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1363 | ret = -EIO; |
Jan Kara | adee11b | 2012-06-27 20:20:22 +0200 | [diff] [blame] | 1364 | goto out_bh; |
| 1365 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Jan Kara | cb14d34 | 2012-06-27 20:08:44 +0200 | [diff] [blame] | 1367 | ret = udf_sb_alloc_partition_maps(sb, le32_to_cpu(lvd->numPartitionMaps)); |
| 1368 | if (ret) |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1369 | goto out_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1371 | for (i = 0, offset = 0; |
Jan Kara | adee11b | 2012-06-27 20:20:22 +0200 | [diff] [blame] | 1372 | i < sbi->s_partitions && offset < table_len; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1373 | i++, offset += gpm->partitionMapLength) { |
| 1374 | struct udf_part_map *map = &sbi->s_partmaps[i]; |
| 1375 | gpm = (struct genericPartitionMap *) |
| 1376 | &(lvd->partitionMaps[offset]); |
| 1377 | type = gpm->partitionMapType; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1378 | if (type == 1) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1379 | struct genericPartitionMap1 *gpm1 = |
| 1380 | (struct genericPartitionMap1 *)gpm; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1381 | map->s_partition_type = UDF_TYPE1_MAP15; |
| 1382 | map->s_volumeseqnum = le16_to_cpu(gpm1->volSeqNum); |
| 1383 | map->s_partition_num = le16_to_cpu(gpm1->partitionNum); |
| 1384 | map->s_partition_func = NULL; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1385 | } else if (type == 2) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1386 | struct udfPartitionMap2 *upm2 = |
| 1387 | (struct udfPartitionMap2 *)gpm; |
| 1388 | if (!strncmp(upm2->partIdent.ident, UDF_ID_VIRTUAL, |
| 1389 | strlen(UDF_ID_VIRTUAL))) { |
| 1390 | u16 suf = |
| 1391 | le16_to_cpu(((__le16 *)upm2->partIdent. |
| 1392 | identSuffix)[0]); |
Jan Kara | c82a127 | 2008-04-08 01:16:32 +0200 | [diff] [blame] | 1393 | if (suf < 0x0200) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1394 | map->s_partition_type = |
| 1395 | UDF_VIRTUAL_MAP15; |
| 1396 | map->s_partition_func = |
| 1397 | udf_get_pblock_virt15; |
Jan Kara | c82a127 | 2008-04-08 01:16:32 +0200 | [diff] [blame] | 1398 | } else { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1399 | map->s_partition_type = |
| 1400 | UDF_VIRTUAL_MAP20; |
| 1401 | map->s_partition_func = |
| 1402 | udf_get_pblock_virt20; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1404 | } else if (!strncmp(upm2->partIdent.ident, |
| 1405 | UDF_ID_SPARABLE, |
| 1406 | strlen(UDF_ID_SPARABLE))) { |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1407 | ret = udf_load_sparable_map(sb, map, |
| 1408 | (struct sparablePartitionMap *)gpm); |
| 1409 | if (ret < 0) |
Jan Kara | 1df2ae3 | 2012-06-27 21:23:07 +0200 | [diff] [blame] | 1410 | goto out_bh; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1411 | } else if (!strncmp(upm2->partIdent.ident, |
| 1412 | UDF_ID_METADATA, |
| 1413 | strlen(UDF_ID_METADATA))) { |
| 1414 | struct udf_meta_data *mdata = |
| 1415 | &map->s_type_specific.s_metadata; |
| 1416 | struct metadataPartitionMap *mdm = |
| 1417 | (struct metadataPartitionMap *) |
| 1418 | &(lvd->partitionMaps[offset]); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1419 | udf_debug("Parsing Logical vol part %d type %u id=%s\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1420 | i, type, UDF_ID_METADATA); |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1421 | |
| 1422 | map->s_partition_type = UDF_METADATA_MAP25; |
| 1423 | map->s_partition_func = udf_get_pblock_meta25; |
| 1424 | |
| 1425 | mdata->s_meta_file_loc = |
| 1426 | le32_to_cpu(mdm->metadataFileLoc); |
| 1427 | mdata->s_mirror_file_loc = |
| 1428 | le32_to_cpu(mdm->metadataMirrorFileLoc); |
| 1429 | mdata->s_bitmap_file_loc = |
| 1430 | le32_to_cpu(mdm->metadataBitmapFileLoc); |
| 1431 | mdata->s_alloc_unit_size = |
| 1432 | le32_to_cpu(mdm->allocUnitSize); |
| 1433 | mdata->s_align_unit_size = |
| 1434 | le16_to_cpu(mdm->alignUnitSize); |
Jan Kara | ed47a7d | 2011-10-24 16:47:48 +0200 | [diff] [blame] | 1435 | if (mdm->flags & 0x01) |
| 1436 | mdata->s_flags |= MF_DUPLICATE_MD; |
Jan Kara | bfb257a | 2008-04-08 20:37:21 +0200 | [diff] [blame] | 1437 | |
| 1438 | udf_debug("Metadata Ident suffix=0x%x\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1439 | le16_to_cpu(*(__le16 *) |
| 1440 | mdm->partIdent.identSuffix)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1441 | udf_debug("Metadata part num=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1442 | le16_to_cpu(mdm->partitionNum)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1443 | udf_debug("Metadata part alloc unit size=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1444 | le32_to_cpu(mdm->allocUnitSize)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1445 | udf_debug("Metadata file loc=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1446 | le32_to_cpu(mdm->metadataFileLoc)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1447 | udf_debug("Mirror file loc=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1448 | le32_to_cpu(mdm->metadataMirrorFileLoc)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1449 | udf_debug("Bitmap file loc=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1450 | le32_to_cpu(mdm->metadataBitmapFileLoc)); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1451 | udf_debug("Flags: %d %u\n", |
Jan Kara | ed47a7d | 2011-10-24 16:47:48 +0200 | [diff] [blame] | 1452 | mdata->s_flags, mdm->flags); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1453 | } else { |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 1454 | udf_debug("Unknown ident: %s\n", |
| 1455 | upm2->partIdent.ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | continue; |
| 1457 | } |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1458 | map->s_volumeseqnum = le16_to_cpu(upm2->volSeqNum); |
| 1459 | map->s_partition_num = le16_to_cpu(upm2->partitionNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1461 | udf_debug("Partition (%d:%u) type %u on volume %u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1462 | i, map->s_partition_num, type, map->s_volumeseqnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1465 | if (fileset) { |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 1466 | struct long_ad *la = (struct long_ad *)&(lvd->logicalVolContentsUse[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
| 1468 | *fileset = lelb_to_cpu(la->extLocation); |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 1469 | udf_debug("FileSet found in LogicalVolDesc at block=%u, partition=%u\n", |
Joe Perches | a983f36 | 2011-10-10 01:08:07 -0700 | [diff] [blame] | 1470 | fileset->logicalBlockNum, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 1471 | fileset->partitionReferenceNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | if (lvd->integritySeqExt.extLength) |
| 1474 | udf_load_logicalvolint(sb, leea_to_cpu(lvd->integritySeqExt)); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1475 | ret = 0; |
Steve Magnani | 4f5edd8 | 2019-02-11 08:24:38 -0600 | [diff] [blame] | 1476 | |
| 1477 | if (!sbi->s_lvid_bh) { |
| 1478 | /* We can't generate unique IDs without a valid LVID */ |
| 1479 | if (sb_rdonly(sb)) { |
| 1480 | UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT); |
| 1481 | } else { |
| 1482 | udf_warn(sb, "Damaged or missing LVID, forcing " |
| 1483 | "readonly mount\n"); |
| 1484 | ret = -EACCES; |
| 1485 | } |
| 1486 | } |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1487 | out_bh: |
| 1488 | brelse(bh); |
| 1489 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1493 | * Find the prevailing Logical Volume Integrity Descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | */ |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 1495 | static void udf_load_logicalvolint(struct super_block *sb, struct kernel_extent_ad loc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | { |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1497 | struct buffer_head *bh, *final_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | uint16_t ident; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1499 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1500 | struct logicalVolIntegrityDesc *lvid; |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1501 | int indirections = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1503 | while (++indirections <= UDF_MAX_LVID_NESTING) { |
| 1504 | final_bh = NULL; |
| 1505 | while (loc.extLength > 0 && |
| 1506 | (bh = udf_read_tagged(sb, loc.extLocation, |
| 1507 | loc.extLocation, &ident))) { |
| 1508 | if (ident != TAG_IDENT_LVID) { |
| 1509 | brelse(bh); |
| 1510 | break; |
| 1511 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1512 | |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1513 | brelse(final_bh); |
| 1514 | final_bh = bh; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1515 | |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1516 | loc.extLength -= sb->s_blocksize; |
| 1517 | loc.extLocation++; |
| 1518 | } |
| 1519 | |
| 1520 | if (!final_bh) |
| 1521 | return; |
| 1522 | |
| 1523 | brelse(sbi->s_lvid_bh); |
| 1524 | sbi->s_lvid_bh = final_bh; |
| 1525 | |
| 1526 | lvid = (struct logicalVolIntegrityDesc *)final_bh->b_data; |
| 1527 | if (lvid->nextIntegrityExt.extLength == 0) |
| 1528 | return; |
| 1529 | |
| 1530 | loc = leea_to_cpu(lvid->nextIntegrityExt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
Alden Tondettar | a47241c | 2016-04-25 19:27:56 -0700 | [diff] [blame] | 1532 | |
| 1533 | udf_warn(sb, "Too many LVID indirections (max %u), ignoring.\n", |
| 1534 | UDF_MAX_LVID_NESTING); |
| 1535 | brelse(sbi->s_lvid_bh); |
| 1536 | sbi->s_lvid_bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1539 | /* |
| 1540 | * Step for reallocation of table of partition descriptor sequence numbers. |
| 1541 | * Must be power of 2. |
| 1542 | */ |
| 1543 | #define PART_DESC_ALLOC_STEP 32 |
| 1544 | |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1545 | struct part_desc_seq_scan_data { |
| 1546 | struct udf_vds_record rec; |
| 1547 | u32 partnum; |
| 1548 | }; |
| 1549 | |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1550 | struct desc_seq_scan_data { |
| 1551 | struct udf_vds_record vds[VDS_POS_LENGTH]; |
| 1552 | unsigned int size_part_descs; |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1553 | unsigned int num_part_descs; |
| 1554 | struct part_desc_seq_scan_data *part_descs_loc; |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1555 | }; |
| 1556 | |
| 1557 | static struct udf_vds_record *handle_partition_descriptor( |
| 1558 | struct buffer_head *bh, |
| 1559 | struct desc_seq_scan_data *data) |
| 1560 | { |
| 1561 | struct partitionDesc *desc = (struct partitionDesc *)bh->b_data; |
| 1562 | int partnum; |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1563 | int i; |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1564 | |
| 1565 | partnum = le16_to_cpu(desc->partitionNumber); |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1566 | for (i = 0; i < data->num_part_descs; i++) |
| 1567 | if (partnum == data->part_descs_loc[i].partnum) |
| 1568 | return &(data->part_descs_loc[i].rec); |
| 1569 | if (data->num_part_descs >= data->size_part_descs) { |
| 1570 | struct part_desc_seq_scan_data *new_loc; |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1571 | unsigned int new_size = ALIGN(partnum, PART_DESC_ALLOC_STEP); |
| 1572 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1573 | new_loc = kcalloc(new_size, sizeof(*new_loc), GFP_KERNEL); |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1574 | if (!new_loc) |
| 1575 | return ERR_PTR(-ENOMEM); |
| 1576 | memcpy(new_loc, data->part_descs_loc, |
| 1577 | data->size_part_descs * sizeof(*new_loc)); |
| 1578 | kfree(data->part_descs_loc); |
| 1579 | data->part_descs_loc = new_loc; |
| 1580 | data->size_part_descs = new_size; |
| 1581 | } |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1582 | return &(data->part_descs_loc[data->num_part_descs++].rec); |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | static struct udf_vds_record *get_volume_descriptor_record(uint16_t ident, |
| 1587 | struct buffer_head *bh, struct desc_seq_scan_data *data) |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1588 | { |
| 1589 | switch (ident) { |
| 1590 | case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1591 | return &(data->vds[VDS_POS_PRIMARY_VOL_DESC]); |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1592 | case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1593 | return &(data->vds[VDS_POS_IMP_USE_VOL_DESC]); |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1594 | case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1595 | return &(data->vds[VDS_POS_LOGICAL_VOL_DESC]); |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1596 | case TAG_IDENT_USD: /* ISO 13346 3/10.8 */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1597 | return &(data->vds[VDS_POS_UNALLOC_SPACE_DESC]); |
| 1598 | case TAG_IDENT_PD: /* ISO 13346 3/10.5 */ |
| 1599 | return handle_partition_descriptor(bh, data); |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1600 | } |
| 1601 | return NULL; |
| 1602 | } |
Vegard Nossum | e7a4eb8 | 2015-12-10 17:13:41 +0100 | [diff] [blame] | 1603 | |
| 1604 | /* |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1605 | * Process a main/reserve volume descriptor sequence. |
| 1606 | * @block First block of first extent of the sequence. |
| 1607 | * @lastblock Lastblock of first extent of the sequence. |
| 1608 | * @fileset There we store extent containing root fileset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | * |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1610 | * Returns <0 on error, 0 on success. -EAGAIN is special - try next descriptor |
| 1611 | * sequence |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1613 | static noinline int udf_process_sequence( |
| 1614 | struct super_block *sb, |
| 1615 | sector_t block, sector_t lastblock, |
| 1616 | struct kernel_lb_addr *fileset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | { |
| 1618 | struct buffer_head *bh = NULL; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1619 | struct udf_vds_record *curr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | struct generic_desc *gd; |
| 1621 | struct volDescPtr *vdp; |
Fabian Frederick | 2b8f942 | 2015-02-04 18:15:04 +0100 | [diff] [blame] | 1622 | bool done = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | uint32_t vdsn; |
| 1624 | uint16_t ident; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1625 | int ret; |
Vegard Nossum | e7a4eb8 | 2015-12-10 17:13:41 +0100 | [diff] [blame] | 1626 | unsigned int indirections = 0; |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1627 | struct desc_seq_scan_data data; |
| 1628 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1630 | memset(data.vds, 0, sizeof(struct udf_vds_record) * VDS_POS_LENGTH); |
| 1631 | data.size_part_descs = PART_DESC_ALLOC_STEP; |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1632 | data.num_part_descs = 0; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1633 | data.part_descs_loc = kcalloc(data.size_part_descs, |
| 1634 | sizeof(*data.part_descs_loc), |
| 1635 | GFP_KERNEL); |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1636 | if (!data.part_descs_loc) |
| 1637 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1639 | /* |
| 1640 | * Read the main descriptor sequence and find which descriptors |
| 1641 | * are in it. |
| 1642 | */ |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1643 | for (; (!done && block <= lastblock); block++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | bh = udf_read_tagged(sb, block, block, &ident); |
Jan Kara | 6762167 | 2018-02-08 18:10:54 +0100 | [diff] [blame] | 1645 | if (!bh) |
| 1646 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* Process each descriptor (ISO 13346 3/8.3-8.4) */ |
| 1649 | gd = (struct generic_desc *)bh->b_data; |
| 1650 | vdsn = le32_to_cpu(gd->volDescSeqNum); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1651 | switch (ident) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 1652 | case TAG_IDENT_VDP: /* ISO 13346 3/10.3 */ |
Jan Kara | 7b568cb | 2018-02-09 13:28:41 +0100 | [diff] [blame] | 1653 | if (++indirections > UDF_MAX_TD_NESTING) { |
| 1654 | udf_err(sb, "too many Volume Descriptor " |
| 1655 | "Pointers (max %u supported)\n", |
| 1656 | UDF_MAX_TD_NESTING); |
| 1657 | brelse(bh); |
| 1658 | return -EIO; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1659 | } |
Jan Kara | 7b568cb | 2018-02-09 13:28:41 +0100 | [diff] [blame] | 1660 | |
| 1661 | vdp = (struct volDescPtr *)bh->b_data; |
| 1662 | block = le32_to_cpu(vdp->nextVolDescSeqExt.extLocation); |
| 1663 | lastblock = le32_to_cpu( |
| 1664 | vdp->nextVolDescSeqExt.extLength) >> |
| 1665 | sb->s_blocksize_bits; |
| 1666 | lastblock += block - 1; |
| 1667 | /* For loop is going to increment 'block' again */ |
| 1668 | block--; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1669 | break; |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1670 | case TAG_IDENT_PVD: /* ISO 13346 3/10.1 */ |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 1671 | case TAG_IDENT_IUVD: /* ISO 13346 3/10.4 */ |
Jan Kara | 18cf4781 | 2018-02-09 15:15:39 +0100 | [diff] [blame] | 1672 | case TAG_IDENT_LVD: /* ISO 13346 3/10.6 */ |
| 1673 | case TAG_IDENT_USD: /* ISO 13346 3/10.8 */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1674 | case TAG_IDENT_PD: /* ISO 13346 3/10.5 */ |
| 1675 | curr = get_volume_descriptor_record(ident, bh, &data); |
| 1676 | if (IS_ERR(curr)) { |
| 1677 | brelse(bh); |
| 1678 | return PTR_ERR(curr); |
| 1679 | } |
| 1680 | /* Descriptor we don't care about? */ |
| 1681 | if (!curr) |
| 1682 | break; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 1683 | if (vdsn >= curr->volDescSeqNum) { |
| 1684 | curr->volDescSeqNum = vdsn; |
| 1685 | curr->block = block; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1686 | } |
| 1687 | break; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 1688 | case TAG_IDENT_TD: /* ISO 13346 3/10.9 */ |
Jan Kara | 7b568cb | 2018-02-09 13:28:41 +0100 | [diff] [blame] | 1689 | done = true; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 1690 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | } |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 1692 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | } |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1694 | /* |
| 1695 | * Now read interesting descriptors again and process them |
| 1696 | * in a suitable order |
| 1697 | */ |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1698 | if (!data.vds[VDS_POS_PRIMARY_VOL_DESC].block) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1699 | udf_err(sb, "Primary Volume Descriptor not found!\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1700 | return -EAGAIN; |
Jan Kara | c0eb31e | 2008-04-01 16:50:35 +0200 | [diff] [blame] | 1701 | } |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1702 | ret = udf_load_pvoldesc(sb, data.vds[VDS_POS_PRIMARY_VOL_DESC].block); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1703 | if (ret < 0) |
| 1704 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1706 | if (data.vds[VDS_POS_LOGICAL_VOL_DESC].block) { |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1707 | ret = udf_load_logicalvol(sb, |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1708 | data.vds[VDS_POS_LOGICAL_VOL_DESC].block, |
| 1709 | fileset); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1710 | if (ret < 0) |
| 1711 | return ret; |
| 1712 | } |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1713 | |
Jan Kara | 7b78fd0 | 2018-02-09 15:56:34 +0100 | [diff] [blame] | 1714 | /* Now handle prevailing Partition Descriptors */ |
Jan Kara | ee4af50 | 2018-08-21 14:52:34 +0200 | [diff] [blame] | 1715 | for (i = 0; i < data.num_part_descs; i++) { |
| 1716 | ret = udf_load_partdesc(sb, data.part_descs_loc[i].rec.block); |
| 1717 | if (ret < 0) |
| 1718 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1724 | /* |
| 1725 | * Load Volume Descriptor Sequence described by anchor in bh |
| 1726 | * |
| 1727 | * Returns <0 on error, 0 on success |
| 1728 | */ |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1729 | static int udf_load_sequence(struct super_block *sb, struct buffer_head *bh, |
| 1730 | struct kernel_lb_addr *fileset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | { |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1732 | struct anchorVolDescPtr *anchor; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1733 | sector_t main_s, main_e, reserve_s, reserve_e; |
| 1734 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1736 | anchor = (struct anchorVolDescPtr *)bh->b_data; |
| 1737 | |
| 1738 | /* Locate the main sequence */ |
| 1739 | main_s = le32_to_cpu(anchor->mainVolDescSeqExt.extLocation); |
| 1740 | main_e = le32_to_cpu(anchor->mainVolDescSeqExt.extLength); |
| 1741 | main_e = main_e >> sb->s_blocksize_bits; |
Jan Kara | 91c9c9e | 2018-02-08 17:39:01 +0100 | [diff] [blame] | 1742 | main_e += main_s - 1; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* Locate the reserve sequence */ |
| 1745 | reserve_s = le32_to_cpu(anchor->reserveVolDescSeqExt.extLocation); |
| 1746 | reserve_e = le32_to_cpu(anchor->reserveVolDescSeqExt.extLength); |
| 1747 | reserve_e = reserve_e >> sb->s_blocksize_bits; |
Jan Kara | 91c9c9e | 2018-02-08 17:39:01 +0100 | [diff] [blame] | 1748 | reserve_e += reserve_s - 1; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* Process the main & reserve sequences */ |
| 1751 | /* responsible for finding the PartitionDesc(s) */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1752 | ret = udf_process_sequence(sb, main_s, main_e, fileset); |
| 1753 | if (ret != -EAGAIN) |
| 1754 | return ret; |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 1755 | udf_sb_free_partitions(sb); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1756 | ret = udf_process_sequence(sb, reserve_s, reserve_e, fileset); |
| 1757 | if (ret < 0) { |
| 1758 | udf_sb_free_partitions(sb); |
| 1759 | /* No sequence was OK, return -EIO */ |
| 1760 | if (ret == -EAGAIN) |
| 1761 | ret = -EIO; |
| 1762 | } |
| 1763 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | } |
| 1765 | |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1766 | /* |
| 1767 | * Check whether there is an anchor block in the given block and |
| 1768 | * load Volume Descriptor Sequence if so. |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1769 | * |
| 1770 | * Returns <0 on error, 0 on success, -EAGAIN is special - try next anchor |
| 1771 | * block |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1772 | */ |
| 1773 | static int udf_check_anchor_block(struct super_block *sb, sector_t block, |
| 1774 | struct kernel_lb_addr *fileset) |
| 1775 | { |
| 1776 | struct buffer_head *bh; |
| 1777 | uint16_t ident; |
| 1778 | int ret; |
| 1779 | |
| 1780 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_VARCONV) && |
| 1781 | udf_fixed_to_variable(block) >= |
Fabian Frederick | 23bcda1 | 2017-01-06 21:54:41 +0100 | [diff] [blame] | 1782 | i_size_read(sb->s_bdev->bd_inode) >> sb->s_blocksize_bits) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1783 | return -EAGAIN; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1784 | |
| 1785 | bh = udf_read_tagged(sb, block, block, &ident); |
| 1786 | if (!bh) |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1787 | return -EAGAIN; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1788 | if (ident != TAG_IDENT_AVDP) { |
| 1789 | brelse(bh); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1790 | return -EAGAIN; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1791 | } |
| 1792 | ret = udf_load_sequence(sb, bh, fileset); |
| 1793 | brelse(bh); |
| 1794 | return ret; |
| 1795 | } |
| 1796 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1797 | /* |
| 1798 | * Search for an anchor volume descriptor pointer. |
| 1799 | * |
| 1800 | * Returns < 0 on error, 0 on success. -EAGAIN is special - try next set |
| 1801 | * of anchors. |
| 1802 | */ |
| 1803 | static int udf_scan_anchors(struct super_block *sb, sector_t *lastblock, |
| 1804 | struct kernel_lb_addr *fileset) |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1805 | { |
| 1806 | sector_t last[6]; |
| 1807 | int i; |
| 1808 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1809 | int last_count = 0; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1810 | int ret; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* First try user provided anchor */ |
| 1813 | if (sbi->s_anchor) { |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1814 | ret = udf_check_anchor_block(sb, sbi->s_anchor, fileset); |
| 1815 | if (ret != -EAGAIN) |
| 1816 | return ret; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1817 | } |
| 1818 | /* |
| 1819 | * according to spec, anchor is in either: |
| 1820 | * block 256 |
| 1821 | * lastblock-256 |
| 1822 | * lastblock |
| 1823 | * however, if the disc isn't closed, it could be 512. |
| 1824 | */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1825 | ret = udf_check_anchor_block(sb, sbi->s_session + 256, fileset); |
| 1826 | if (ret != -EAGAIN) |
| 1827 | return ret; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1828 | /* |
| 1829 | * The trouble is which block is the last one. Drives often misreport |
| 1830 | * this so we try various possibilities. |
| 1831 | */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1832 | last[last_count++] = *lastblock; |
| 1833 | if (*lastblock >= 1) |
| 1834 | last[last_count++] = *lastblock - 1; |
| 1835 | last[last_count++] = *lastblock + 1; |
| 1836 | if (*lastblock >= 2) |
| 1837 | last[last_count++] = *lastblock - 2; |
| 1838 | if (*lastblock >= 150) |
| 1839 | last[last_count++] = *lastblock - 150; |
| 1840 | if (*lastblock >= 152) |
| 1841 | last[last_count++] = *lastblock - 152; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1842 | |
| 1843 | for (i = 0; i < last_count; i++) { |
Fabian Frederick | 23bcda1 | 2017-01-06 21:54:41 +0100 | [diff] [blame] | 1844 | if (last[i] >= i_size_read(sb->s_bdev->bd_inode) >> |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1845 | sb->s_blocksize_bits) |
| 1846 | continue; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1847 | ret = udf_check_anchor_block(sb, last[i], fileset); |
| 1848 | if (ret != -EAGAIN) { |
| 1849 | if (!ret) |
| 1850 | *lastblock = last[i]; |
| 1851 | return ret; |
| 1852 | } |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1853 | if (last[i] < 256) |
| 1854 | continue; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1855 | ret = udf_check_anchor_block(sb, last[i] - 256, fileset); |
| 1856 | if (ret != -EAGAIN) { |
| 1857 | if (!ret) |
| 1858 | *lastblock = last[i]; |
| 1859 | return ret; |
| 1860 | } |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | /* Finally try block 512 in case media is open */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1864 | return udf_check_anchor_block(sb, sbi->s_session + 512, fileset); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Find an anchor volume descriptor and load Volume Descriptor Sequence from |
| 1869 | * area specified by it. The function expects sbi->s_lastblock to be the last |
| 1870 | * block on the media. |
| 1871 | * |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1872 | * Return <0 on error, 0 if anchor found. -EAGAIN is special meaning anchor |
| 1873 | * was not found. |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1874 | */ |
| 1875 | static int udf_find_anchor(struct super_block *sb, |
| 1876 | struct kernel_lb_addr *fileset) |
| 1877 | { |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1878 | struct udf_sb_info *sbi = UDF_SB(sb); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1879 | sector_t lastblock = sbi->s_last_block; |
| 1880 | int ret; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1881 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1882 | ret = udf_scan_anchors(sb, &lastblock, fileset); |
| 1883 | if (ret != -EAGAIN) |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1884 | goto out; |
| 1885 | |
| 1886 | /* No anchor found? Try VARCONV conversion of block numbers */ |
| 1887 | UDF_SET_FLAG(sb, UDF_FLAG_VARCONV); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1888 | lastblock = udf_variable_to_fixed(sbi->s_last_block); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1889 | /* Firstly, we try to not convert number of the last block */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1890 | ret = udf_scan_anchors(sb, &lastblock, fileset); |
| 1891 | if (ret != -EAGAIN) |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1892 | goto out; |
| 1893 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1894 | lastblock = sbi->s_last_block; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1895 | /* Secondly, we try with converted number of the last block */ |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1896 | ret = udf_scan_anchors(sb, &lastblock, fileset); |
| 1897 | if (ret < 0) { |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1898 | /* VARCONV didn't help. Clear it. */ |
| 1899 | UDF_CLEAR_FLAG(sb, UDF_FLAG_VARCONV); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1900 | } |
| 1901 | out: |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1902 | if (ret == 0) |
| 1903 | sbi->s_last_block = lastblock; |
| 1904 | return ret; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Check Volume Structure Descriptor, find Anchor block and load Volume |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1909 | * Descriptor Sequence. |
| 1910 | * |
| 1911 | * Returns < 0 on error, 0 on success. -EAGAIN is special meaning anchor |
| 1912 | * block was not found. |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1913 | */ |
| 1914 | static int udf_load_vrs(struct super_block *sb, struct udf_options *uopt, |
| 1915 | int silent, struct kernel_lb_addr *fileset) |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1916 | { |
| 1917 | struct udf_sb_info *sbi = UDF_SB(sb); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1918 | loff_t nsr_off; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1919 | int ret; |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1920 | |
| 1921 | if (!sb_set_blocksize(sb, uopt->blocksize)) { |
| 1922 | if (!silent) |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1923 | udf_warn(sb, "Bad block size\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1924 | return -EINVAL; |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1925 | } |
| 1926 | sbi->s_last_block = uopt->lastblock; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1927 | if (!uopt->novrs) { |
| 1928 | /* Check that it is NSR02 compliant */ |
| 1929 | nsr_off = udf_check_vsd(sb); |
| 1930 | if (!nsr_off) { |
| 1931 | if (!silent) |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1932 | udf_warn(sb, "No VRS found\n"); |
Fabian Frederick | 70f16ce | 2017-01-18 19:39:35 +0100 | [diff] [blame] | 1933 | return -EINVAL; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1934 | } |
| 1935 | if (nsr_off == -1) |
Peter A. Felvegi | 4449960 | 2013-10-18 20:07:44 +0200 | [diff] [blame] | 1936 | udf_debug("Failed to read sector at offset %d. " |
| 1937 | "Assuming open disc. Skipping validity " |
| 1938 | "check\n", VSD_FIRST_SECTOR_OFFSET); |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1939 | if (!sbi->s_last_block) |
| 1940 | sbi->s_last_block = udf_get_last_block(sb); |
| 1941 | } else { |
| 1942 | udf_debug("Validity check skipped because of novrs option\n"); |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1943 | } |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 1944 | |
| 1945 | /* Look for anchor block and load Volume Descriptor Sequence */ |
| 1946 | sbi->s_anchor = uopt->anchor; |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1947 | ret = udf_find_anchor(sb, fileset); |
| 1948 | if (ret < 0) { |
| 1949 | if (!silent && ret == -EAGAIN) |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 1950 | udf_warn(sb, "No anchor found\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1951 | return ret; |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1952 | } |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 1953 | return 0; |
Clemens Ladisch | 1197e4d | 2009-03-11 15:57:47 +0100 | [diff] [blame] | 1954 | } |
| 1955 | |
Steve Magnani | ebbd5e9 | 2019-02-08 11:34:54 -0600 | [diff] [blame] | 1956 | static void udf_finalize_lvid(struct logicalVolIntegrityDesc *lvid) |
| 1957 | { |
| 1958 | struct timespec64 ts; |
| 1959 | |
| 1960 | ktime_get_real_ts64(&ts); |
| 1961 | udf_time_to_disk_stamp(&lvid->recordingDateAndTime, ts); |
| 1962 | lvid->descTag.descCRC = cpu_to_le16( |
| 1963 | crc_itu_t(0, (char *)lvid + sizeof(struct tag), |
| 1964 | le16_to_cpu(lvid->descTag.descCRCLength))); |
| 1965 | lvid->descTag.tagChecksum = udf_tag_checksum(&lvid->descTag); |
| 1966 | } |
| 1967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | static void udf_open_lvid(struct super_block *sb) |
| 1969 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 1970 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 1971 | struct buffer_head *bh = sbi->s_lvid_bh; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1972 | struct logicalVolIntegrityDesc *lvid; |
| 1973 | struct logicalVolIntegrityDescImpUse *lvidiu; |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 1974 | |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1975 | if (!bh) |
| 1976 | return; |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 1977 | lvid = (struct logicalVolIntegrityDesc *)bh->b_data; |
| 1978 | lvidiu = udf_sb_lvidiu(sb); |
| 1979 | if (!lvidiu) |
| 1980 | return; |
Jan Kara | 949f4a7 | 2010-10-20 18:49:20 +0200 | [diff] [blame] | 1981 | |
| 1982 | mutex_lock(&sbi->s_alloc_mutex); |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1983 | lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; |
| 1984 | lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; |
Jan Kara | b72e632 | 2018-02-27 18:55:31 +0100 | [diff] [blame] | 1985 | if (le32_to_cpu(lvid->integrityType) == LVID_INTEGRITY_TYPE_CLOSE) |
| 1986 | lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_OPEN); |
| 1987 | else |
| 1988 | UDF_SET_FLAG(sb, UDF_FLAG_INCONSISTENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
Steve Magnani | ebbd5e9 | 2019-02-08 11:34:54 -0600 | [diff] [blame] | 1990 | udf_finalize_lvid(lvid); |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 1991 | mark_buffer_dirty(bh); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 1992 | sbi->s_lvid_dirty = 0; |
Jan Kara | 949f4a7 | 2010-10-20 18:49:20 +0200 | [diff] [blame] | 1993 | mutex_unlock(&sbi->s_alloc_mutex); |
Jan Kara | 9734c971 | 2013-01-17 22:11:38 +0100 | [diff] [blame] | 1994 | /* Make opening of filesystem visible on the media immediately */ |
| 1995 | sync_dirty_buffer(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | static void udf_close_lvid(struct super_block *sb) |
| 1999 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2000 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 2001 | struct buffer_head *bh = sbi->s_lvid_bh; |
| 2002 | struct logicalVolIntegrityDesc *lvid; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 2003 | struct logicalVolIntegrityDescImpUse *lvidiu; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2004 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2005 | if (!bh) |
| 2006 | return; |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 2007 | lvid = (struct logicalVolIntegrityDesc *)bh->b_data; |
| 2008 | lvidiu = udf_sb_lvidiu(sb); |
| 2009 | if (!lvidiu) |
| 2010 | return; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2011 | |
Jan Kara | 949f4a7 | 2010-10-20 18:49:20 +0200 | [diff] [blame] | 2012 | mutex_lock(&sbi->s_alloc_mutex); |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 2013 | lvidiu->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX; |
| 2014 | lvidiu->impIdent.identSuffix[1] = UDF_OS_ID_LINUX; |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 2015 | if (UDF_MAX_WRITE_VERSION > le16_to_cpu(lvidiu->maxUDFWriteRev)) |
| 2016 | lvidiu->maxUDFWriteRev = cpu_to_le16(UDF_MAX_WRITE_VERSION); |
| 2017 | if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFReadRev)) |
| 2018 | lvidiu->minUDFReadRev = cpu_to_le16(sbi->s_udfrev); |
| 2019 | if (sbi->s_udfrev > le16_to_cpu(lvidiu->minUDFWriteRev)) |
| 2020 | lvidiu->minUDFWriteRev = cpu_to_le16(sbi->s_udfrev); |
Jan Kara | b72e632 | 2018-02-27 18:55:31 +0100 | [diff] [blame] | 2021 | if (!UDF_QUERY_FLAG(sb, UDF_FLAG_INCONSISTENT)) |
| 2022 | lvid->integrityType = cpu_to_le32(LVID_INTEGRITY_TYPE_CLOSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | |
Jan Kara | 853a0c2 | 2011-12-23 11:53:07 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * We set buffer uptodate unconditionally here to avoid spurious |
| 2026 | * warnings from mark_buffer_dirty() when previous EIO has marked |
| 2027 | * the buffer as !uptodate |
| 2028 | */ |
| 2029 | set_buffer_uptodate(bh); |
Steve Magnani | ebbd5e9 | 2019-02-08 11:34:54 -0600 | [diff] [blame] | 2030 | udf_finalize_lvid(lvid); |
Marcin Slusarz | 165923f | 2008-02-10 11:33:08 +0100 | [diff] [blame] | 2031 | mark_buffer_dirty(bh); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 2032 | sbi->s_lvid_dirty = 0; |
Jan Kara | 949f4a7 | 2010-10-20 18:49:20 +0200 | [diff] [blame] | 2033 | mutex_unlock(&sbi->s_alloc_mutex); |
Jan Kara | 9734c971 | 2013-01-17 22:11:38 +0100 | [diff] [blame] | 2034 | /* Make closing of filesystem visible on the media immediately */ |
| 2035 | sync_dirty_buffer(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | } |
| 2037 | |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 2038 | u64 lvid_get_unique_id(struct super_block *sb) |
| 2039 | { |
| 2040 | struct buffer_head *bh; |
| 2041 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 2042 | struct logicalVolIntegrityDesc *lvid; |
| 2043 | struct logicalVolHeaderDesc *lvhd; |
| 2044 | u64 uniqueID; |
| 2045 | u64 ret; |
| 2046 | |
| 2047 | bh = sbi->s_lvid_bh; |
| 2048 | if (!bh) |
| 2049 | return 0; |
| 2050 | |
| 2051 | lvid = (struct logicalVolIntegrityDesc *)bh->b_data; |
| 2052 | lvhd = (struct logicalVolHeaderDesc *)lvid->logicalVolContentsUse; |
| 2053 | |
| 2054 | mutex_lock(&sbi->s_alloc_mutex); |
| 2055 | ret = uniqueID = le64_to_cpu(lvhd->uniqueID); |
| 2056 | if (!(++uniqueID & 0xFFFFFFFF)) |
| 2057 | uniqueID += 16; |
| 2058 | lvhd->uniqueID = cpu_to_le64(uniqueID); |
Steve Magnani | e8b4274 | 2019-02-08 11:34:55 -0600 | [diff] [blame] | 2059 | udf_updated_lvid(sb); |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 2060 | mutex_unlock(&sbi->s_alloc_mutex); |
Jan Kara | d664b6a | 2010-10-20 18:28:46 +0200 | [diff] [blame] | 2061 | |
| 2062 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
| 2064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | static int udf_fill_super(struct super_block *sb, void *options, int silent) |
| 2066 | { |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2067 | int ret = -EINVAL; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2068 | struct inode *inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | struct udf_options uopt; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 2070 | struct kernel_lb_addr rootdir, fileset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | struct udf_sb_info *sbi; |
Jan Kara | 9181f8b | 2015-08-20 14:50:07 +0200 | [diff] [blame] | 2072 | bool lvid_open = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | |
| 2074 | uopt.flags = (1 << UDF_FLAG_USE_AD_IN_ICB) | (1 << UDF_FLAG_STRICT); |
Jan Kara | 116e525 | 2018-02-22 10:39:52 +0100 | [diff] [blame] | 2075 | /* By default we'll use overflow[ug]id when UDF inode [ug]id == -1 */ |
| 2076 | uopt.uid = make_kuid(current_user_ns(), overflowuid); |
| 2077 | uopt.gid = make_kgid(current_user_ns(), overflowgid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | uopt.umask = 0; |
Marcin Slusarz | 87bc730 | 2008-12-02 13:40:11 +0100 | [diff] [blame] | 2079 | uopt.fmode = UDF_INVALID_MODE; |
| 2080 | uopt.dmode = UDF_INVALID_MODE; |
Chengguang Xu | 785dffe | 2018-02-25 21:25:07 +0800 | [diff] [blame] | 2081 | uopt.nls_map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | |
Markus Elfring | 033c9da | 2017-08-15 16:45:44 +0200 | [diff] [blame] | 2083 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); |
Alessio Igor Bogani | 9db9f9e | 2010-11-16 18:40:49 +0100 | [diff] [blame] | 2084 | if (!sbi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | return -ENOMEM; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | sb->s_fs_info = sbi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | |
Ingo Molnar | 1e7933d | 2006-03-23 03:00:44 -0800 | [diff] [blame] | 2089 | mutex_init(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | |
Miklos Szeredi | 6da8089 | 2008-02-08 04:21:50 -0800 | [diff] [blame] | 2091 | if (!udf_parse_options((char *)options, &uopt, false)) |
Markus Elfring | fdf2657 | 2014-11-18 18:29:10 +0100 | [diff] [blame] | 2092 | goto parse_options_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | |
| 2094 | if (uopt.flags & (1 << UDF_FLAG_UTF8) && |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2095 | uopt.flags & (1 << UDF_FLAG_NLS_MAP)) { |
Joe Perches | 8076c36 | 2011-10-10 01:08:03 -0700 | [diff] [blame] | 2096 | udf_err(sb, "utf8 cannot be combined with iocharset\n"); |
Markus Elfring | fdf2657 | 2014-11-18 18:29:10 +0100 | [diff] [blame] | 2097 | goto parse_options_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2099 | if ((uopt.flags & (1 << UDF_FLAG_NLS_MAP)) && !uopt.nls_map) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | uopt.nls_map = load_nls_default(); |
| 2101 | if (!uopt.nls_map) |
| 2102 | uopt.flags &= ~(1 << UDF_FLAG_NLS_MAP); |
| 2103 | else |
| 2104 | udf_debug("Using default NLS map\n"); |
| 2105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | if (!(uopt.flags & (1 << UDF_FLAG_NLS_MAP))) |
| 2107 | uopt.flags |= (1 << UDF_FLAG_UTF8); |
| 2108 | |
| 2109 | fileset.logicalBlockNum = 0xFFFFFFFF; |
| 2110 | fileset.partitionReferenceNum = 0xFFFF; |
| 2111 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2112 | sbi->s_flags = uopt.flags; |
| 2113 | sbi->s_uid = uopt.uid; |
| 2114 | sbi->s_gid = uopt.gid; |
| 2115 | sbi->s_umask = uopt.umask; |
Marcin Slusarz | 7ac9bcd5 | 2008-11-16 20:52:19 +0100 | [diff] [blame] | 2116 | sbi->s_fmode = uopt.fmode; |
| 2117 | sbi->s_dmode = uopt.dmode; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2118 | sbi->s_nls_map = uopt.nls_map; |
Jan Kara | c03cad2 | 2010-10-20 22:17:28 +0200 | [diff] [blame] | 2119 | rwlock_init(&sbi->s_cred_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2121 | if (uopt.session == 0xFFFFFFFF) |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2122 | sbi->s_session = udf_get_last_session(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | else |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2124 | sbi->s_session = uopt.session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2126 | udf_debug("Multi-session=%d\n", sbi->s_session); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | /* Fill in the rest of the superblock */ |
| 2129 | sb->s_op = &udf_sb_ops; |
Rasmus Rohde | 221e583 | 2008-04-30 17:22:06 +0200 | [diff] [blame] | 2130 | sb->s_export_op = &udf_export_ops; |
Christoph Hellwig | 123e9ca | 2010-05-19 07:16:44 -0400 | [diff] [blame] | 2131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | sb->s_magic = UDF_SUPER_MAGIC; |
| 2133 | sb->s_time_gran = 1000; |
| 2134 | |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 2135 | if (uopt.flags & (1 << UDF_FLAG_BLOCKSIZE_SET)) { |
| 2136 | ret = udf_load_vrs(sb, &uopt, silent, &fileset); |
| 2137 | } else { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 2138 | uopt.blocksize = bdev_logical_block_size(sb->s_bdev); |
Fabian Frederick | 70f16ce | 2017-01-18 19:39:35 +0100 | [diff] [blame] | 2139 | while (uopt.blocksize <= 4096) { |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 2140 | ret = udf_load_vrs(sb, &uopt, silent, &fileset); |
Fabian Frederick | 70f16ce | 2017-01-18 19:39:35 +0100 | [diff] [blame] | 2141 | if (ret < 0) { |
| 2142 | if (!silent && ret != -EACCES) { |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 2143 | pr_notice("Scanning with blocksize %u failed\n", |
Fabian Frederick | 70f16ce | 2017-01-18 19:39:35 +0100 | [diff] [blame] | 2144 | uopt.blocksize); |
| 2145 | } |
| 2146 | brelse(sbi->s_lvid_bh); |
| 2147 | sbi->s_lvid_bh = NULL; |
| 2148 | /* |
| 2149 | * EACCES is special - we want to propagate to |
| 2150 | * upper layers that we cannot handle RW mount. |
| 2151 | */ |
| 2152 | if (ret == -EACCES) |
| 2153 | break; |
| 2154 | } else |
| 2155 | break; |
| 2156 | |
| 2157 | uopt.blocksize <<= 1; |
Jan Kara | 40346005 | 2009-03-19 16:21:38 +0100 | [diff] [blame] | 2158 | } |
| 2159 | } |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2160 | if (ret < 0) { |
| 2161 | if (ret == -EAGAIN) { |
| 2162 | udf_warn(sb, "No partition found (1)\n"); |
| 2163 | ret = -EINVAL; |
| 2164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | goto error_out; |
| 2166 | } |
| 2167 | |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 2168 | udf_debug("Lastblock=%u\n", sbi->s_last_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2170 | if (sbi->s_lvid_bh) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2171 | struct logicalVolIntegrityDescImpUse *lvidiu = |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 2172 | udf_sb_lvidiu(sb); |
| 2173 | uint16_t minUDFReadRev; |
| 2174 | uint16_t minUDFWriteRev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 2176 | if (!lvidiu) { |
| 2177 | ret = -EINVAL; |
| 2178 | goto error_out; |
| 2179 | } |
| 2180 | minUDFReadRev = le16_to_cpu(lvidiu->minUDFReadRev); |
| 2181 | minUDFWriteRev = le16_to_cpu(lvidiu->minUDFWriteRev); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2182 | if (minUDFReadRev > UDF_MAX_READ_VERSION) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2183 | udf_err(sb, "minUDFReadRev=%x (max is %x)\n", |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 2184 | minUDFReadRev, |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2185 | UDF_MAX_READ_VERSION); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2186 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | goto error_out; |
Jan Kara | a9ad01b | 2018-09-06 15:56:10 +0200 | [diff] [blame] | 2188 | } else if (minUDFWriteRev > UDF_MAX_WRITE_VERSION) { |
| 2189 | if (!sb_rdonly(sb)) { |
| 2190 | ret = -EACCES; |
| 2191 | goto error_out; |
| 2192 | } |
| 2193 | UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT); |
Jan Kara | e729eac | 2013-07-25 16:15:16 +0200 | [diff] [blame] | 2194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2196 | sbi->s_udfrev = minUDFWriteRev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
| 2198 | if (minUDFReadRev >= UDF_VERS_USE_EXTENDED_FE) |
| 2199 | UDF_SET_FLAG(sb, UDF_FLAG_USE_EXTENDED_FE); |
| 2200 | if (minUDFReadRev >= UDF_VERS_USE_STREAMS) |
| 2201 | UDF_SET_FLAG(sb, UDF_FLAG_USE_STREAMS); |
| 2202 | } |
| 2203 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2204 | if (!sbi->s_partitions) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2205 | udf_warn(sb, "No partition found (2)\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2206 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | goto error_out; |
| 2208 | } |
| 2209 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2210 | if (sbi->s_partmaps[sbi->s_partition].s_partition_flags & |
Jan Kara | a9ad01b | 2018-09-06 15:56:10 +0200 | [diff] [blame] | 2211 | UDF_PART_FLAG_READ_ONLY) { |
| 2212 | if (!sb_rdonly(sb)) { |
| 2213 | ret = -EACCES; |
| 2214 | goto error_out; |
| 2215 | } |
| 2216 | UDF_SET_FLAG(sb, UDF_FLAG_RW_INCOMPAT); |
Peter Osterlund | c1a26e7 | 2006-10-05 21:17:50 +0200 | [diff] [blame] | 2217 | } |
Eric Sandeen | 39b3f6d | 2006-09-29 01:59:41 -0700 | [diff] [blame] | 2218 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2219 | if (udf_find_fileset(sb, &fileset, &rootdir)) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2220 | udf_warn(sb, "No fileset found\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2221 | ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | goto error_out; |
| 2223 | } |
| 2224 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2225 | if (!silent) { |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 2226 | struct timestamp ts; |
Marcin Slusarz | 5677480 | 2008-02-10 11:25:31 +0100 | [diff] [blame] | 2227 | udf_time_to_disk_stamp(&ts, sbi->s_record_time); |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2228 | udf_info("Mounting volume '%s', timestamp %04u/%02u/%02u %02u:%02u (%x)\n", |
| 2229 | sbi->s_volume_ident, |
| 2230 | le16_to_cpu(ts.year), ts.month, ts.day, |
Marcin Slusarz | 5677480 | 2008-02-10 11:25:31 +0100 | [diff] [blame] | 2231 | ts.hour, ts.minute, le16_to_cpu(ts.typeAndTimezone)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2233 | if (!sb_rdonly(sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | udf_open_lvid(sb); |
Jan Kara | 9181f8b | 2015-08-20 14:50:07 +0200 | [diff] [blame] | 2235 | lvid_open = true; |
| 2236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
| 2238 | /* Assign the root inode */ |
| 2239 | /* assign inodes by physical block number */ |
| 2240 | /* perhaps it's not extensible enough, but for now ... */ |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 2241 | inode = udf_iget(sb, &rootdir); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 2242 | if (IS_ERR(inode)) { |
Steve Magnani | fcbf763 | 2017-10-12 08:48:41 -0500 | [diff] [blame] | 2243 | udf_err(sb, "Error in udf_iget, block=%u, partition=%u\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2244 | rootdir.logicalBlockNum, rootdir.partitionReferenceNum); |
Jan Kara | 6d3d5e8 | 2014-09-04 16:15:51 +0200 | [diff] [blame] | 2245 | ret = PTR_ERR(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | goto error_out; |
| 2247 | } |
| 2248 | |
| 2249 | /* Allocate a dentry for the root inode */ |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 2250 | sb->s_root = d_make_root(inode); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2251 | if (!sb->s_root) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2252 | udf_err(sb, "Couldn't allocate root dentry\n"); |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2253 | ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | goto error_out; |
| 2255 | } |
Jan Kara | 31170b6 | 2007-05-08 00:35:21 -0700 | [diff] [blame] | 2256 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 2257 | sb->s_max_links = UDF_MAX_LINKS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | return 0; |
| 2259 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2260 | error_out: |
Markus Elfring | 0d454e4 | 2014-11-18 17:17:46 +0100 | [diff] [blame] | 2261 | iput(sbi->s_vat_inode); |
Markus Elfring | fdf2657 | 2014-11-18 18:29:10 +0100 | [diff] [blame] | 2262 | parse_options_failure: |
Chengguang Xu | 785dffe | 2018-02-25 21:25:07 +0800 | [diff] [blame] | 2263 | if (uopt.nls_map) |
| 2264 | unload_nls(uopt.nls_map); |
Jan Kara | 9181f8b | 2015-08-20 14:50:07 +0200 | [diff] [blame] | 2265 | if (lvid_open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | udf_close_lvid(sb); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2267 | brelse(sbi->s_lvid_bh); |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 2268 | udf_sb_free_partitions(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | kfree(sbi); |
| 2270 | sb->s_fs_info = NULL; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2271 | |
Jan Kara | d759bfa4 | 2013-07-25 19:10:59 +0200 | [diff] [blame] | 2272 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | } |
| 2274 | |
Joe Perches | 8076c36 | 2011-10-10 01:08:03 -0700 | [diff] [blame] | 2275 | void _udf_err(struct super_block *sb, const char *function, |
| 2276 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | { |
Joe Perches | c2bff36 | 2011-10-10 01:08:06 -0700 | [diff] [blame] | 2278 | struct va_format vaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | va_list args; |
| 2280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | va_start(args, fmt); |
Joe Perches | c2bff36 | 2011-10-10 01:08:06 -0700 | [diff] [blame] | 2282 | |
| 2283 | vaf.fmt = fmt; |
| 2284 | vaf.va = &args; |
| 2285 | |
| 2286 | pr_err("error (device %s): %s: %pV", sb->s_id, function, &vaf); |
| 2287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | va_end(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Joe Perches | a40ecd7 | 2011-10-10 01:08:04 -0700 | [diff] [blame] | 2291 | void _udf_warn(struct super_block *sb, const char *function, |
| 2292 | const char *fmt, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | { |
Joe Perches | c2bff36 | 2011-10-10 01:08:06 -0700 | [diff] [blame] | 2294 | struct va_format vaf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | va_list args; |
| 2296 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2297 | va_start(args, fmt); |
Joe Perches | c2bff36 | 2011-10-10 01:08:06 -0700 | [diff] [blame] | 2298 | |
| 2299 | vaf.fmt = fmt; |
| 2300 | vaf.va = &args; |
| 2301 | |
| 2302 | pr_warn("warning (device %s): %s: %pV", sb->s_id, function, &vaf); |
| 2303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | va_end(args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2307 | static void udf_put_super(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2308 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2309 | struct udf_sb_info *sbi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2311 | sbi = UDF_SB(sb); |
Christoph Hellwig | 6cfd014 | 2009-05-05 15:40:36 +0200 | [diff] [blame] | 2312 | |
Markus Elfring | 0d454e4 | 2014-11-18 17:17:46 +0100 | [diff] [blame] | 2313 | iput(sbi->s_vat_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | if (UDF_QUERY_FLAG(sb, UDF_FLAG_NLS_MAP)) |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2315 | unload_nls(sbi->s_nls_map); |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 2316 | if (!sb_rdonly(sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | udf_close_lvid(sb); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2318 | brelse(sbi->s_lvid_bh); |
Jan Kara | bff943a | 2012-06-27 22:27:05 +0200 | [diff] [blame] | 2319 | udf_sb_free_partitions(sb); |
Fabian Frederick | bbe48dd | 2015-01-10 19:13:32 +0100 | [diff] [blame] | 2320 | mutex_destroy(&sbi->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | kfree(sb->s_fs_info); |
| 2322 | sb->s_fs_info = NULL; |
| 2323 | } |
| 2324 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 2325 | static int udf_sync_fs(struct super_block *sb, int wait) |
| 2326 | { |
| 2327 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 2328 | |
| 2329 | mutex_lock(&sbi->s_alloc_mutex); |
| 2330 | if (sbi->s_lvid_dirty) { |
Steve Magnani | e8b4274 | 2019-02-08 11:34:55 -0600 | [diff] [blame] | 2331 | struct buffer_head *bh = sbi->s_lvid_bh; |
Jan Kara | 52b9666 | 2019-02-19 14:59:43 +0100 | [diff] [blame] | 2332 | struct logicalVolIntegrityDesc *lvid; |
Steve Magnani | e8b4274 | 2019-02-08 11:34:55 -0600 | [diff] [blame] | 2333 | |
Jan Kara | 52b9666 | 2019-02-19 14:59:43 +0100 | [diff] [blame] | 2334 | lvid = (struct logicalVolIntegrityDesc *)bh->b_data; |
| 2335 | udf_finalize_lvid(lvid); |
Steve Magnani | e8b4274 | 2019-02-08 11:34:55 -0600 | [diff] [blame] | 2336 | |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 2337 | /* |
| 2338 | * Blockdevice will be synced later so we don't have to submit |
| 2339 | * the buffer for IO |
| 2340 | */ |
Steve Magnani | e8b4274 | 2019-02-08 11:34:55 -0600 | [diff] [blame] | 2341 | mark_buffer_dirty(bh); |
Jan Kara | 146bca7 | 2009-03-16 18:27:37 +0100 | [diff] [blame] | 2342 | sbi->s_lvid_dirty = 0; |
| 2343 | } |
| 2344 | mutex_unlock(&sbi->s_alloc_mutex); |
| 2345 | |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2349 | static int udf_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2351 | struct super_block *sb = dentry->d_sb; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2352 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 2353 | struct logicalVolIntegrityDescImpUse *lvidiu; |
Coly Li | 557f5a1 | 2009-01-20 01:36:55 +0800 | [diff] [blame] | 2354 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2355 | |
Jan Kara | 69d7567 | 2013-09-12 22:00:15 +0200 | [diff] [blame] | 2356 | lvidiu = udf_sb_lvidiu(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | buf->f_type = UDF_SUPER_MAGIC; |
| 2358 | buf->f_bsize = sb->s_blocksize; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2359 | buf->f_blocks = sbi->s_partmaps[sbi->s_partition].s_partition_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | buf->f_bfree = udf_count_free(sb); |
| 2361 | buf->f_bavail = buf->f_bfree; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2362 | buf->f_files = (lvidiu != NULL ? (le32_to_cpu(lvidiu->numFiles) + |
| 2363 | le32_to_cpu(lvidiu->numDirs)) : 0) |
| 2364 | + buf->f_bfree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | buf->f_ffree = buf->f_bfree; |
Andrew Gabbasov | 9fba705 | 2016-01-15 02:44:21 -0600 | [diff] [blame] | 2366 | buf->f_namelen = UDF_NAME_LEN; |
Coly Li | 557f5a1 | 2009-01-20 01:36:55 +0800 | [diff] [blame] | 2367 | buf->f_fsid.val[0] = (u32)id; |
| 2368 | buf->f_fsid.val[1] = (u32)(id >> 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2373 | static unsigned int udf_count_free_bitmap(struct super_block *sb, |
| 2374 | struct udf_bitmap *bitmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | { |
| 2376 | struct buffer_head *bh = NULL; |
| 2377 | unsigned int accum = 0; |
| 2378 | int index; |
Steve Magnani | b490bdd | 2017-10-12 08:48:40 -0500 | [diff] [blame] | 2379 | udf_pblk_t block = 0, newblock; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 2380 | struct kernel_lb_addr loc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | uint32_t bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | uint8_t *ptr; |
| 2383 | uint16_t ident; |
| 2384 | struct spaceBitmapDesc *bm; |
| 2385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | loc.logicalBlockNum = bitmap->s_extPosition; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2387 | loc.partitionReferenceNum = UDF_SB(sb)->s_partition; |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 2388 | bh = udf_read_ptagged(sb, &loc, 0, &ident); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2390 | if (!bh) { |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2391 | udf_err(sb, "udf_count_free failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | goto out; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2393 | } else if (ident != TAG_IDENT_SBD) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 2394 | brelse(bh); |
Joe Perches | 78ace70c | 2011-10-10 01:08:05 -0700 | [diff] [blame] | 2395 | udf_err(sb, "udf_count_free failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | goto out; |
| 2397 | } |
| 2398 | |
| 2399 | bm = (struct spaceBitmapDesc *)bh->b_data; |
| 2400 | bytes = le32_to_cpu(bm->numOfBytes); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2401 | index = sizeof(struct spaceBitmapDesc); /* offset in first block only */ |
| 2402 | ptr = (uint8_t *)bh->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2404 | while (bytes > 0) { |
Marcin Slusarz | 01b954a | 2008-02-02 22:37:07 +0100 | [diff] [blame] | 2405 | u32 cur_bytes = min_t(u32, bytes, sb->s_blocksize - index); |
| 2406 | accum += bitmap_weight((const unsigned long *)(ptr + index), |
| 2407 | cur_bytes * 8); |
| 2408 | bytes -= cur_bytes; |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2409 | if (bytes) { |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 2410 | brelse(bh); |
Pekka Enberg | 97e961f | 2008-10-15 12:29:03 +0200 | [diff] [blame] | 2411 | newblock = udf_get_lb_pblock(sb, &loc, ++block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | bh = udf_tread(sb, newblock); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2413 | if (!bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | udf_debug("read failed\n"); |
| 2415 | goto out; |
| 2416 | } |
| 2417 | index = 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2418 | ptr = (uint8_t *)bh->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2419 | } |
| 2420 | } |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 2421 | brelse(bh); |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2422 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | return accum; |
| 2424 | } |
| 2425 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2426 | static unsigned int udf_count_free_table(struct super_block *sb, |
| 2427 | struct inode *table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | { |
| 2429 | unsigned int accum = 0; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 2430 | uint32_t elen; |
Pekka Enberg | 5ca4e4b | 2008-10-15 12:28:03 +0200 | [diff] [blame] | 2431 | struct kernel_lb_addr eloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | int8_t etype; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 2433 | struct extent_position epos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | |
Jan Kara | d1668fe | 2010-10-20 23:24:12 +0200 | [diff] [blame] | 2435 | mutex_lock(&UDF_SB(sb)->s_alloc_mutex); |
Marcin Slusarz | c0b3443 | 2008-02-08 04:20:42 -0800 | [diff] [blame] | 2436 | epos.block = UDF_I(table)->i_location; |
Jan Kara | ff116fc | 2007-05-08 00:35:14 -0700 | [diff] [blame] | 2437 | epos.offset = sizeof(struct unallocSpaceEntry); |
| 2438 | epos.bh = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 2440 | while ((etype = udf_next_aext(table, &epos, &eloc, &elen, 1)) != -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | accum += (elen >> table->i_sb->s_blocksize_bits); |
Marcin Slusarz | 3a71fc5 | 2008-02-08 04:20:28 -0800 | [diff] [blame] | 2442 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 2443 | brelse(epos.bh); |
Jan Kara | d1668fe | 2010-10-20 23:24:12 +0200 | [diff] [blame] | 2444 | mutex_unlock(&UDF_SB(sb)->s_alloc_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | |
| 2446 | return accum; |
| 2447 | } |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 2448 | |
| 2449 | static unsigned int udf_count_free(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | { |
| 2451 | unsigned int accum = 0; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2452 | struct udf_sb_info *sbi; |
| 2453 | struct udf_part_map *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2455 | sbi = UDF_SB(sb); |
| 2456 | if (sbi->s_lvid_bh) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2457 | struct logicalVolIntegrityDesc *lvid = |
| 2458 | (struct logicalVolIntegrityDesc *) |
| 2459 | sbi->s_lvid_bh->b_data; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2460 | if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 2461 | accum = le32_to_cpu( |
| 2462 | lvid->freeSpaceTable[sbi->s_partition]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | if (accum == 0xFFFFFFFF) |
| 2464 | accum = 0; |
| 2465 | } |
| 2466 | } |
| 2467 | |
| 2468 | if (accum) |
| 2469 | return accum; |
| 2470 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2471 | map = &sbi->s_partmaps[sbi->s_partition]; |
| 2472 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2473 | accum += udf_count_free_bitmap(sb, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2474 | map->s_uspace.s_bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | if (accum) |
| 2477 | return accum; |
| 2478 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2479 | if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_TABLE) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 2480 | accum += udf_count_free_table(sb, |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 2481 | map->s_uspace.s_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | return accum; |
| 2484 | } |
Fabian Frederick | 54bb60d | 2017-01-06 21:53:57 +0100 | [diff] [blame] | 2485 | |
| 2486 | MODULE_AUTHOR("Ben Fennema"); |
| 2487 | MODULE_DESCRIPTION("Universal Disk Format Filesystem"); |
| 2488 | MODULE_LICENSE("GPL"); |
| 2489 | module_init(init_udf_fs) |
| 2490 | module_exit(exit_udf_fs) |