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 | { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 34 | if (partition >= UDF_SB_NUMPARTS(sb)) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 35 | udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n", |
| 36 | block, partition, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | return 0xFFFFFFFF; |
| 38 | } |
| 39 | if (UDF_SB_PARTFUNC(sb, partition)) |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 40 | return UDF_SB_PARTFUNC(sb, partition)(sb, block, partition, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | else |
| 42 | return UDF_SB_PARTROOT(sb, partition) + block + offset; |
| 43 | } |
| 44 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 45 | 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] | 46 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | struct buffer_head *bh = NULL; |
| 49 | uint32_t newblock; |
| 50 | uint32_t index; |
| 51 | uint32_t loc; |
| 52 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 53 | index = (sb->s_blocksize - UDF_SB_TYPEVIRT(sb,partition).s_start_offset) / sizeof(uint32_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 55 | if (block > UDF_SB_TYPEVIRT(sb,partition).s_num_entries) { |
| 56 | udf_debug("Trying to access block beyond end of VAT (%d max %d)\n", |
| 57 | block, UDF_SB_TYPEVIRT(sb,partition).s_num_entries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return 0xFFFFFFFF; |
| 59 | } |
| 60 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 61 | if (block >= index) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | block -= index; |
| 63 | newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t))); |
| 64 | index = block % (sb->s_blocksize / sizeof(uint32_t)); |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 65 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | newblock = 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 67 | index = UDF_SB_TYPEVIRT(sb,partition).s_start_offset / sizeof(uint32_t) + block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | loc = udf_block_map(UDF_SB_VAT(sb), newblock); |
| 71 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 72 | if (!(bh = sb_bread(sb, loc))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | 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] | 74 | sb, block, partition, loc, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | return 0xFFFFFFFF; |
| 76 | } |
| 77 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 78 | loc = le32_to_cpu(((__le32 *)bh->b_data)[index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Jan Kara | 3bf25cb | 2007-05-08 00:35:16 -0700 | [diff] [blame] | 80 | brelse(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 82 | if (UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum == partition) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | udf_debug("recursive call to udf_get_pblock!\n"); |
| 84 | return 0xFFFFFFFF; |
| 85 | } |
| 86 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 87 | return udf_get_pblock(sb, loc, |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 88 | UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum, |
| 89 | offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 92 | inline uint32_t udf_get_pblock_virt20(struct super_block * sb, uint32_t block, |
| 93 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | return udf_get_pblock_virt15(sb, block, partition, offset); |
| 96 | } |
| 97 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 98 | uint32_t udf_get_pblock_spar15(struct super_block * sb, uint32_t block, |
| 99 | uint16_t partition, uint32_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | int i; |
| 102 | struct sparingTable *st = NULL; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 103 | uint32_t packet = (block + offset) & ~(UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 105 | for (i = 0; i < 4; i++) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 106 | if (UDF_SB_TYPESPAR(sb,partition).s_spar_map[i] != NULL) { |
| 107 | st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,partition).s_spar_map[i]->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 112 | if (st) { |
| 113 | for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 114 | if (le32_to_cpu(st->mapEntry[i].origLocation) >= 0xFFFFFFF0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | break; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 116 | } else if (le32_to_cpu(st->mapEntry[i].origLocation) == packet) { |
| 117 | return le32_to_cpu(st->mapEntry[i].mappedLocation) + |
| 118 | ((block + offset) & (UDF_SB_TYPESPAR(sb,partition).s_packet_len - 1)); |
| 119 | } else if (le32_to_cpu(st->mapEntry[i].origLocation) > packet) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | break; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 124 | |
| 125 | return UDF_SB_PARTROOT(sb,partition) + block + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block) |
| 129 | { |
| 130 | struct udf_sparing_data *sdata; |
| 131 | struct sparingTable *st = NULL; |
| 132 | struct sparingEntry mapEntry; |
| 133 | uint32_t packet; |
| 134 | int i, j, k, l; |
| 135 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 136 | for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 137 | if (old_block > UDF_SB_PARTROOT(sb,i) && |
| 138 | old_block < UDF_SB_PARTROOT(sb,i) + UDF_SB_PARTLEN(sb,i)) { |
| 139 | sdata = &UDF_SB_TYPESPAR(sb,i); |
| 140 | packet = (old_block - UDF_SB_PARTROOT(sb,i)) & ~(sdata->s_packet_len - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 142 | for (j = 0; j < 4; j++) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 143 | if (UDF_SB_TYPESPAR(sb,i).s_spar_map[j] != NULL) { |
| 144 | st = (struct sparingTable *)sdata->s_spar_map[j]->b_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if (!st) |
| 150 | return 1; |
| 151 | |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 152 | for (k = 0; k < le16_to_cpu(st->reallocationTableLen); k++) { |
| 153 | if (le32_to_cpu(st->mapEntry[k].origLocation) == 0xFFFFFFFF) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 154 | for (; j < 4; j++) { |
| 155 | if (sdata->s_spar_map[j]) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 156 | st = (struct sparingTable *)sdata->s_spar_map[j]->b_data; |
| 157 | st->mapEntry[k].origLocation = cpu_to_le32(packet); |
| 158 | udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry)); |
| 159 | mark_buffer_dirty(sdata->s_spar_map[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 162 | *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) + |
| 163 | ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 165 | } else if (le32_to_cpu(st->mapEntry[k].origLocation) == packet) { |
| 166 | *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) + |
| 167 | ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return 0; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 169 | } else if (le32_to_cpu(st->mapEntry[k].origLocation) > packet) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | break; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 173 | |
| 174 | for (l = k; l < le16_to_cpu(st->reallocationTableLen); l++) { |
| 175 | if (le32_to_cpu(st->mapEntry[l].origLocation) == 0xFFFFFFFF) { |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 176 | for (; j < 4; j++) { |
| 177 | if (sdata->s_spar_map[j]) { |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 178 | st = (struct sparingTable *)sdata->s_spar_map[j]->b_data; |
| 179 | mapEntry = st->mapEntry[l]; |
| 180 | mapEntry.origLocation = cpu_to_le32(packet); |
| 181 | memmove(&st->mapEntry[k + 1], &st->mapEntry[k], (l - k) * sizeof(struct sparingEntry)); |
| 182 | st->mapEntry[k] = mapEntry; |
| 183 | udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry)); |
| 184 | mark_buffer_dirty(sdata->s_spar_map[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 187 | *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) + |
| 188 | ((old_block - UDF_SB_PARTROOT(sb,i)) & (sdata->s_packet_len - 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | return 1; |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 194 | } /* if old_block */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Cyrill Gorcunov | 28de794 | 2007-07-21 04:37:18 -0700 | [diff] [blame] | 196 | |
Cyrill Gorcunov | cb00ea3 | 2007-07-19 01:47:43 -0700 | [diff] [blame] | 197 | if (i == UDF_SB_NUMPARTS(sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* outside of partitions */ |
| 199 | /* for now, fail =) */ |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |