blob: 307c9c33d184938b3d9a4b81a8d60b489a8ed3b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * partition.c
3 *
4 * PURPOSE
5 * Partition handling routines for the OSTA-UDF(tm) filesystem.
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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 Gorcunov28de7942007-07-21 04:37:18 -070017 * 12/06/98 blf Created file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
28#include <linux/buffer_head.h>
29
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070030inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
31 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Marcin Slusarz6c79e982008-02-08 04:20:30 -080033 struct udf_sb_info *sbi = UDF_SB(sb);
34 struct udf_part_map *map;
35 if (partition >= sbi->s_partitions) {
Marcin Slusarz4b111112008-02-08 04:20:36 -080036 udf_debug("block=%d, partition=%d, offset=%d: "
37 "invalid partition\n", block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return 0xFFFFFFFF;
39 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -080040 map = &sbi->s_partmaps[partition];
41 if (map->s_partition_func)
42 return map->s_partition_func(sb, block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -080044 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070047uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070048 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct buffer_head *bh = NULL;
51 uint32_t newblock;
52 uint32_t index;
53 uint32_t loc;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080054 struct udf_sb_info *sbi = UDF_SB(sb);
55 struct udf_part_map *map;
Marcin Slusarz4b111112008-02-08 04:20:36 -080056 struct udf_virtual_data *vdata;
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080057 struct udf_inode_info *iinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Marcin Slusarz6c79e982008-02-08 04:20:30 -080059 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -080060 vdata = &map->s_type_specific.s_virtual;
61 index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Marcin Slusarz4b111112008-02-08 04:20:36 -080063 if (block > vdata->s_num_entries) {
64 udf_debug("Trying to access block beyond end of VAT "
65 "(%d max %d)\n", block, vdata->s_num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0xFFFFFFFF;
67 }
68
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070069 if (block >= index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 block -= index;
71 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
72 index = block % (sb->s_blocksize / sizeof(uint32_t));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070073 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 newblock = 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -080075 index = vdata->s_start_offset / sizeof(uint32_t) + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 }
77
Marcin Slusarz6c79e982008-02-08 04:20:30 -080078 loc = udf_block_map(sbi->s_vat_inode, newblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Marcin Slusarz4b111112008-02-08 04:20:36 -080080 bh = sb_bread(sb, loc);
81 if (!bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070083 sb, block, partition, loc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0xFFFFFFFF;
85 }
86
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070087 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Jan Kara3bf25cb2007-05-08 00:35:16 -070089 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080091 iinfo = UDF_I(sbi->s_vat_inode);
92 if (iinfo->i_location.partitionReferenceNum == partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 udf_debug("recursive call to udf_get_pblock!\n");
94 return 0xFFFFFFFF;
95 }
96
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070097 return udf_get_pblock(sb, loc,
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080098 iinfo->i_location.partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070099 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Marcin Slusarz4b111112008-02-08 04:20:36 -0800102inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 return udf_get_pblock_virt15(sb, block, partition, offset);
106}
107
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800108uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 int i;
112 struct sparingTable *st = NULL;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800113 struct udf_sb_info *sbi = UDF_SB(sb);
114 struct udf_part_map *map;
115 uint32_t packet;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800116 struct udf_sparing_data *sdata;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800117
118 map = &sbi->s_partmaps[partition];
Marcin Slusarz4b111112008-02-08 04:20:36 -0800119 sdata = &map->s_type_specific.s_sparing;
120 packet = (block + offset) & ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700122 for (i = 0; i < 4; i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800123 if (sdata->s_spar_map[i] != NULL) {
124 st = (struct sparingTable *)
125 sdata->s_spar_map[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 break;
127 }
128 }
129
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700130 if (st) {
131 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800132 struct sparingEntry *entry = &st->mapEntry[i];
133 u32 origLoc = le32_to_cpu(entry->origLocation);
134 if (origLoc >= 0xFFFFFFF0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 break;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800136 else if (origLoc == packet)
137 return le32_to_cpu(entry->mappedLocation) +
138 ((block + offset) &
139 (sdata->s_packet_len - 1));
140 else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
143 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700144
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800145 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
149{
150 struct udf_sparing_data *sdata;
151 struct sparingTable *st = NULL;
152 struct sparingEntry mapEntry;
153 uint32_t packet;
154 int i, j, k, l;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800155 struct udf_sb_info *sbi = UDF_SB(sb);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800156 u16 reallocationTableLen;
157 struct buffer_head *bh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800159 for (i = 0; i < sbi->s_partitions; i++) {
160 struct udf_part_map *map = &sbi->s_partmaps[i];
161 if (old_block > map->s_partition_root &&
162 old_block < map->s_partition_root + map->s_partition_len) {
163 sdata = &map->s_type_specific.s_sparing;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800164 packet = (old_block - map->s_partition_root) &
165 ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Marcin Slusarz4b111112008-02-08 04:20:36 -0800167 for (j = 0; j < 4; j++)
168 if (sdata->s_spar_map[j] != NULL) {
169 st = (struct sparingTable *)
170 sdata->s_spar_map[j]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (!st)
175 return 1;
176
Marcin Slusarz4b111112008-02-08 04:20:36 -0800177 reallocationTableLen =
178 le16_to_cpu(st->reallocationTableLen);
179 for (k = 0; k < reallocationTableLen; k++) {
180 struct sparingEntry *entry = &st->mapEntry[k];
181 u32 origLoc = le32_to_cpu(entry->origLocation);
182
183 if (origLoc == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700184 for (; j < 4; j++) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 int len;
186 bh = sdata->s_spar_map[j];
187 if (!bh)
188 continue;
189
190 st = (struct sparingTable *)
191 bh->b_data;
192 entry->origLocation =
193 cpu_to_le32(packet);
194 len =
195 sizeof(struct sparingTable) +
196 reallocationTableLen *
197 sizeof(struct sparingEntry);
198 udf_update_tag((char *)st, len);
199 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800201 *new_block = le32_to_cpu(
202 entry->mappedLocation) +
203 ((old_block -
204 map->s_partition_root) &
205 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800207 } else if (origLoc == packet) {
208 *new_block = le32_to_cpu(
209 entry->mappedLocation) +
210 ((old_block -
211 map->s_partition_root) &
212 (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return 0;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800214 } else if (origLoc > packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700217
Marcin Slusarz4b111112008-02-08 04:20:36 -0800218 for (l = k; l < reallocationTableLen; l++) {
219 struct sparingEntry *entry = &st->mapEntry[l];
220 u32 origLoc = le32_to_cpu(entry->origLocation);
221
222 if (origLoc != 0xFFFFFFFF)
223 continue;
224
225 for (; j < 4; j++) {
226 bh = sdata->s_spar_map[j];
227 if (!bh)
228 continue;
229
230 st = (struct sparingTable *)bh->b_data;
231 mapEntry = st->mapEntry[l];
232 mapEntry.origLocation =
233 cpu_to_le32(packet);
234 memmove(&st->mapEntry[k + 1],
235 &st->mapEntry[k],
236 (l - k) *
237 sizeof(struct sparingEntry));
238 st->mapEntry[k] = mapEntry;
239 udf_update_tag((char *)st,
240 sizeof(struct sparingTable) +
241 reallocationTableLen *
242 sizeof(struct sparingEntry));
243 mark_buffer_dirty(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800245 *new_block =
246 le32_to_cpu(
247 st->mapEntry[k].mappedLocation) +
248 ((old_block - map->s_partition_root) &
249 (sdata->s_packet_len - 1));
250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700254 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700256
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800257 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* outside of partitions */
259 /* for now, fail =) */
260 return 1;
261 }
262
263 return 0;
264}