Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * partition.c |
| 3 | * |
| 4 | * PURPOSE |
| 5 | * Partition handling routines for the OSTA-UDF(tm) filesystem. |
| 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * COPYRIGHT |
| 8 | * This file is distributed under the terms of the GNU General Public |
| 9 | * License (GPL). Copies of the GPL can be obtained from: |
| 10 | * ftp://prep.ai.mit.edu/pub/gnu/GPL |
| 11 | * Each contributing author retains all rights to their own work. |
| 12 | * |
| 13 | * (C) 1998-2001 Ben Fennema |
| 14 | * |
| 15 | * HISTORY |
| 16 | * |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 17 | * 12/06/98 blf Created file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include "udfdecl.h" |
| 22 | #include "udf_sb.h" |
| 23 | #include "udf_i.h" |
| 24 | |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/udf_fs.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/buffer_head.h> |
| 30 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 31 | inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block, |
| 32 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 34 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 35 | struct udf_part_map *map; |
| 36 | if (partition >= sbi->s_partitions) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 37 | udf_debug("block=%d, partition=%d, offset=%d: " |
| 38 | "invalid partition\n", block, partition, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | return 0xFFFFFFFF; |
| 40 | } |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 41 | map = &sbi->s_partmaps[partition]; |
| 42 | if (map->s_partition_func) |
| 43 | return map->s_partition_func(sb, block, partition, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | else |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 45 | return map->s_partition_root + block + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 48 | uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 49 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | struct buffer_head *bh = NULL; |
| 52 | uint32_t newblock; |
| 53 | uint32_t index; |
| 54 | uint32_t loc; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 55 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 56 | struct udf_part_map *map; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 57 | struct udf_virtual_data *vdata; |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 58 | struct udf_inode_info *iinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 60 | map = &sbi->s_partmaps[partition]; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 61 | vdata = &map->s_type_specific.s_virtual; |
| 62 | index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 64 | if (block > vdata->s_num_entries) { |
| 65 | udf_debug("Trying to access block beyond end of VAT " |
| 66 | "(%d max %d)\n", block, vdata->s_num_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | return 0xFFFFFFFF; |
| 68 | } |
| 69 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 70 | if (block >= index) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | block -= index; |
| 72 | newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t))); |
| 73 | index = block % (sb->s_blocksize / sizeof(uint32_t)); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 74 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | newblock = 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 76 | index = vdata->s_start_offset / sizeof(uint32_t) + block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 79 | loc = udf_block_map(sbi->s_vat_inode, newblock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 81 | bh = sb_bread(sb, loc); |
| 82 | if (!bh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n", |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 84 | sb, block, partition, loc, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return 0xFFFFFFFF; |
| 86 | } |
| 87 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 88 | loc = le32_to_cpu(((__le32 *)bh->b_data)[index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 90 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 92 | iinfo = UDF_I(sbi->s_vat_inode); |
| 93 | if (iinfo->i_location.partitionReferenceNum == partition) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | udf_debug("recursive call to udf_get_pblock!\n"); |
| 95 | return 0xFFFFFFFF; |
| 96 | } |
| 97 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 98 | return udf_get_pblock(sb, loc, |
Marcin Slusarz | 48d6d8f | 2008-02-08 04:20:44 -0800 | [diff] [blame^] | 99 | iinfo->i_location.partitionReferenceNum, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 100 | offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 103 | inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 104 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | return udf_get_pblock_virt15(sb, block, partition, offset); |
| 107 | } |
| 108 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 109 | uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block, |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 110 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | int i; |
| 113 | struct sparingTable *st = NULL; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 114 | struct udf_sb_info *sbi = UDF_SB(sb); |
| 115 | struct udf_part_map *map; |
| 116 | uint32_t packet; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 117 | struct udf_sparing_data *sdata; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 118 | |
| 119 | map = &sbi->s_partmaps[partition]; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 120 | sdata = &map->s_type_specific.s_sparing; |
| 121 | packet = (block + offset) & ~(sdata->s_packet_len - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 123 | for (i = 0; i < 4; i++) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 124 | if (sdata->s_spar_map[i] != NULL) { |
| 125 | st = (struct sparingTable *) |
| 126 | sdata->s_spar_map[i]->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 131 | if (st) { |
| 132 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 133 | struct sparingEntry *entry = &st->mapEntry[i]; |
| 134 | u32 origLoc = le32_to_cpu(entry->origLocation); |
| 135 | if (origLoc >= 0xFFFFFFF0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | break; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 137 | else if (origLoc == packet) |
| 138 | return le32_to_cpu(entry->mappedLocation) + |
| 139 | ((block + offset) & |
| 140 | (sdata->s_packet_len - 1)); |
| 141 | else if (origLoc > packet) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | break; |
| 143 | } |
| 144 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 145 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 146 | return map->s_partition_root + block + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) |
| 150 | { |
| 151 | struct udf_sparing_data *sdata; |
| 152 | struct sparingTable *st = NULL; |
| 153 | struct sparingEntry mapEntry; |
| 154 | uint32_t packet; |
| 155 | int i, j, k, l; |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 156 | struct udf_sb_info *sbi = UDF_SB(sb); |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 157 | u16 reallocationTableLen; |
| 158 | struct buffer_head *bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 160 | for (i = 0; i < sbi->s_partitions; i++) { |
| 161 | struct udf_part_map *map = &sbi->s_partmaps[i]; |
| 162 | if (old_block > map->s_partition_root && |
| 163 | old_block < map->s_partition_root + map->s_partition_len) { |
| 164 | sdata = &map->s_type_specific.s_sparing; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 165 | packet = (old_block - map->s_partition_root) & |
| 166 | ~(sdata->s_packet_len - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 168 | for (j = 0; j < 4; j++) |
| 169 | if (sdata->s_spar_map[j] != NULL) { |
| 170 | st = (struct sparingTable *) |
| 171 | sdata->s_spar_map[j]->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | break; |
| 173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | if (!st) |
| 176 | return 1; |
| 177 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 178 | reallocationTableLen = |
| 179 | le16_to_cpu(st->reallocationTableLen); |
| 180 | for (k = 0; k < reallocationTableLen; k++) { |
| 181 | struct sparingEntry *entry = &st->mapEntry[k]; |
| 182 | u32 origLoc = le32_to_cpu(entry->origLocation); |
| 183 | |
| 184 | if (origLoc == 0xFFFFFFFF) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 185 | for (; j < 4; j++) { |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 186 | int len; |
| 187 | bh = sdata->s_spar_map[j]; |
| 188 | if (!bh) |
| 189 | continue; |
| 190 | |
| 191 | st = (struct sparingTable *) |
| 192 | bh->b_data; |
| 193 | entry->origLocation = |
| 194 | cpu_to_le32(packet); |
| 195 | len = |
| 196 | sizeof(struct sparingTable) + |
| 197 | reallocationTableLen * |
| 198 | sizeof(struct sparingEntry); |
| 199 | udf_update_tag((char *)st, len); |
| 200 | mark_buffer_dirty(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 202 | *new_block = le32_to_cpu( |
| 203 | entry->mappedLocation) + |
| 204 | ((old_block - |
| 205 | map->s_partition_root) & |
| 206 | (sdata->s_packet_len - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 208 | } else if (origLoc == packet) { |
| 209 | *new_block = le32_to_cpu( |
| 210 | entry->mappedLocation) + |
| 211 | ((old_block - |
| 212 | map->s_partition_root) & |
| 213 | (sdata->s_packet_len - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | return 0; |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 215 | } else if (origLoc > packet) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | break; |
| 217 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 218 | |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 219 | for (l = k; l < reallocationTableLen; l++) { |
| 220 | struct sparingEntry *entry = &st->mapEntry[l]; |
| 221 | u32 origLoc = le32_to_cpu(entry->origLocation); |
| 222 | |
| 223 | if (origLoc != 0xFFFFFFFF) |
| 224 | continue; |
| 225 | |
| 226 | for (; j < 4; j++) { |
| 227 | bh = sdata->s_spar_map[j]; |
| 228 | if (!bh) |
| 229 | continue; |
| 230 | |
| 231 | st = (struct sparingTable *)bh->b_data; |
| 232 | mapEntry = st->mapEntry[l]; |
| 233 | mapEntry.origLocation = |
| 234 | cpu_to_le32(packet); |
| 235 | memmove(&st->mapEntry[k + 1], |
| 236 | &st->mapEntry[k], |
| 237 | (l - k) * |
| 238 | sizeof(struct sparingEntry)); |
| 239 | st->mapEntry[k] = mapEntry; |
| 240 | udf_update_tag((char *)st, |
| 241 | sizeof(struct sparingTable) + |
| 242 | reallocationTableLen * |
| 243 | sizeof(struct sparingEntry)); |
| 244 | mark_buffer_dirty(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
Marcin Slusarz | 4b11111 | 2008-02-08 04:20:36 -0800 | [diff] [blame] | 246 | *new_block = |
| 247 | le32_to_cpu( |
| 248 | st->mapEntry[k].mappedLocation) + |
| 249 | ((old_block - map->s_partition_root) & |
| 250 | (sdata->s_packet_len - 1)); |
| 251 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 1; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 255 | } /* if old_block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 257 | |
Marcin Slusarz | 6c79e98 | 2008-02-08 04:20:30 -0800 | [diff] [blame] | 258 | if (i == sbi->s_partitions) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | /* outside of partitions */ |
| 260 | /* for now, fail =) */ |
| 261 | return 1; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |