partition.c 7.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * partition.c
 *
 * PURPOSE
 *      Partition handling routines for the OSTA-UDF(tm) filesystem.
 *
 * COPYRIGHT
 *      This file is distributed under the terms of the GNU General Public
 *      License (GPL). Copies of the GPL can be obtained from:
 *              ftp://prep.ai.mit.edu/pub/gnu/GPL
 *      Each contributing author retains all rights to their own work.
 *
 *  (C) 1998-2001 Ben Fennema
 *
 * HISTORY
 *
 * 12/06/98 blf  Created file. 
 *
 */

#include "udfdecl.h"
#include "udf_sb.h"
#include "udf_i.h"

#include <linux/fs.h>
#include <linux/string.h>
#include <linux/udf_fs.h>
#include <linux/slab.h>
#include <linux/buffer_head.h>

31 32
inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
			       uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
33
{
34 35 36 37
	if (partition >= UDF_SB_NUMPARTS(sb)) {
		udf_debug
		    ("block=%d, partition=%d, offset=%d: invalid partition\n",
		     block, partition, offset);
L
Linus Torvalds 已提交
38 39 40
		return 0xFFFFFFFF;
	}
	if (UDF_SB_PARTFUNC(sb, partition))
41 42
		return UDF_SB_PARTFUNC(sb, partition) (sb, block, partition,
						       offset);
L
Linus Torvalds 已提交
43 44 45 46
	else
		return UDF_SB_PARTROOT(sb, partition) + block + offset;
}

47 48
uint32_t udf_get_pblock_virt15(struct super_block * sb, uint32_t block,
			       uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
49 50 51 52 53 54
{
	struct buffer_head *bh = NULL;
	uint32_t newblock;
	uint32_t index;
	uint32_t loc;

55 56 57
	index =
	    (sb->s_blocksize -
	     UDF_SB_TYPEVIRT(sb, partition).s_start_offset) / sizeof(uint32_t);
L
Linus Torvalds 已提交
58

59 60 61 62
	if (block > UDF_SB_TYPEVIRT(sb, partition).s_num_entries) {
		udf_debug
		    ("Trying to access block beyond end of VAT (%d max %d)\n",
		     block, UDF_SB_TYPEVIRT(sb, partition).s_num_entries);
L
Linus Torvalds 已提交
63 64 65
		return 0xFFFFFFFF;
	}

66
	if (block >= index) {
L
Linus Torvalds 已提交
67 68 69
		block -= index;
		newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
		index = block % (sb->s_blocksize / sizeof(uint32_t));
70
	} else {
L
Linus Torvalds 已提交
71
		newblock = 0;
72 73 74 75
		index =
		    UDF_SB_TYPEVIRT(sb,
				    partition).s_start_offset /
		    sizeof(uint32_t) + block;
L
Linus Torvalds 已提交
76 77 78 79
	}

	loc = udf_block_map(UDF_SB_VAT(sb), newblock);

80
	if (!(bh = sb_bread(sb, loc))) {
L
Linus Torvalds 已提交
81
		udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
82
			  sb, block, partition, loc, index);
L
Linus Torvalds 已提交
83 84 85
		return 0xFFFFFFFF;
	}

86
	loc = le32_to_cpu(((__le32 *) bh->b_data)[index]);
L
Linus Torvalds 已提交
87

J
Jan Kara 已提交
88
	brelse(bh);
L
Linus Torvalds 已提交
89

90
	if (UDF_I_LOCATION(UDF_SB_VAT(sb)).partitionReferenceNum == partition) {
L
Linus Torvalds 已提交
91 92 93 94
		udf_debug("recursive call to udf_get_pblock!\n");
		return 0xFFFFFFFF;
	}

95 96 97
	return udf_get_pblock(sb, loc,
			      UDF_I_LOCATION(UDF_SB_VAT(sb)).
			      partitionReferenceNum, offset);
L
Linus Torvalds 已提交
98 99
}

100 101
inline uint32_t udf_get_pblock_virt20(struct super_block * sb, uint32_t block,
				      uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
102 103 104 105
{
	return udf_get_pblock_virt15(sb, block, partition, offset);
}

106 107
uint32_t udf_get_pblock_spar15(struct super_block * sb, uint32_t block,
			       uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
108 109 110
{
	int i;
	struct sparingTable *st = NULL;
111 112 113
	uint32_t packet =
	    (block + offset) & ~(UDF_SB_TYPESPAR(sb, partition).s_packet_len -
				 1);
L
Linus Torvalds 已提交
114

115 116 117 118 119
	for (i = 0; i < 4; i++) {
		if (UDF_SB_TYPESPAR(sb, partition).s_spar_map[i] != NULL) {
			st = (struct sparingTable *)UDF_SB_TYPESPAR(sb,
								    partition).
			    s_spar_map[i]->b_data;
L
Linus Torvalds 已提交
120 121 122 123
			break;
		}
	}

124 125 126 127
	if (st) {
		for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
			if (le32_to_cpu(st->mapEntry[i].origLocation) >=
			    0xFFFFFFF0)
L
Linus Torvalds 已提交
128
				break;
129 130 131 132 133 134 135 136 137 138 139 140
			else if (le32_to_cpu(st->mapEntry[i].origLocation) ==
				 packet) {
				return le32_to_cpu(st->mapEntry[i].
						   mappedLocation) + ((block +
								       offset) &
								      (UDF_SB_TYPESPAR
								       (sb,
									partition).
								       s_packet_len
								       - 1));
			} else if (le32_to_cpu(st->mapEntry[i].origLocation) >
				   packet)
L
Linus Torvalds 已提交
141 142 143
				break;
		}
	}
144
	return UDF_SB_PARTROOT(sb, partition) + block + offset;
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154
}

int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
{
	struct udf_sparing_data *sdata;
	struct sparingTable *st = NULL;
	struct sparingEntry mapEntry;
	uint32_t packet;
	int i, j, k, l;

155 156 157
	for (i = 0; i < UDF_SB_NUMPARTS(sb); i++) {
		if (old_block > UDF_SB_PARTROOT(sb, i) &&
		    old_block < UDF_SB_PARTROOT(sb, i) + UDF_SB_PARTLEN(sb, i))
L
Linus Torvalds 已提交
158
		{
159 160 161 162 163 164 165 166 167 168 169
			sdata = &UDF_SB_TYPESPAR(sb, i);
			packet =
			    (old_block -
			     UDF_SB_PARTROOT(sb,
					     i)) & ~(sdata->s_packet_len - 1);

			for (j = 0; j < 4; j++) {
				if (UDF_SB_TYPESPAR(sb, i).s_spar_map[j] !=
				    NULL) {
					st = (struct sparingTable *)sdata->
					    s_spar_map[j]->b_data;
L
Linus Torvalds 已提交
170 171 172 173 174 175 176
					break;
				}
			}

			if (!st)
				return 1;

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
			for (k = 0; k < le16_to_cpu(st->reallocationTableLen);
			     k++) {
				if (le32_to_cpu(st->mapEntry[k].origLocation) ==
				    0xFFFFFFFF) {
					for (; j < 4; j++) {
						if (sdata->s_spar_map[j]) {
							st = (struct
							      sparingTable *)
							    sdata->
							    s_spar_map[j]->
							    b_data;
							st->mapEntry[k].
							    origLocation =
							    cpu_to_le32(packet);
							udf_update_tag((char *)
								       st,
								       sizeof
								       (struct
									sparingTable)
								       +
								       le16_to_cpu
								       (st->
									reallocationTableLen)
								       *
								       sizeof
								       (struct
									sparingEntry));
							mark_buffer_dirty
							    (sdata->
							     s_spar_map[j]);
L
Linus Torvalds 已提交
207 208
						}
					}
209 210 211 212 213 214 215 216
					*new_block =
					    le32_to_cpu(st->mapEntry[k].
							mappedLocation) +
					    ((old_block -
					      UDF_SB_PARTROOT(sb,
							      i)) & (sdata->
								     s_packet_len
								     - 1));
L
Linus Torvalds 已提交
217
					return 0;
218 219 220 221 222 223 224 225 226 227 228 229
				} else
				    if (le32_to_cpu
					(st->mapEntry[k].origLocation) ==
					packet) {
					*new_block =
					    le32_to_cpu(st->mapEntry[k].
							mappedLocation) +
					    ((old_block -
					      UDF_SB_PARTROOT(sb,
							      i)) & (sdata->
								     s_packet_len
								     - 1));
L
Linus Torvalds 已提交
230
					return 0;
231 232 233
				} else
				    if (le32_to_cpu
					(st->mapEntry[k].origLocation) > packet)
L
Linus Torvalds 已提交
234 235
					break;
			}
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
			for (l = k; l < le16_to_cpu(st->reallocationTableLen);
			     l++) {
				if (le32_to_cpu(st->mapEntry[l].origLocation) ==
				    0xFFFFFFFF) {
					for (; j < 4; j++) {
						if (sdata->s_spar_map[j]) {
							st = (struct
							      sparingTable *)
							    sdata->
							    s_spar_map[j]->
							    b_data;
							mapEntry =
							    st->mapEntry[l];
							mapEntry.origLocation =
							    cpu_to_le32(packet);
							memmove(&st->
								mapEntry[k + 1],
								&st->
								mapEntry[k],
								(l -
								 k) *
								sizeof(struct
								       sparingEntry));
							st->mapEntry[k] =
							    mapEntry;
							udf_update_tag((char *)
								       st,
								       sizeof
								       (struct
									sparingTable)
								       +
								       le16_to_cpu
								       (st->
									reallocationTableLen)
								       *
								       sizeof
								       (struct
									sparingEntry));
							mark_buffer_dirty
							    (sdata->
							     s_spar_map[j]);
L
Linus Torvalds 已提交
277 278
						}
					}
279 280 281 282 283 284 285 286
					*new_block =
					    le32_to_cpu(st->mapEntry[k].
							mappedLocation) +
					    ((old_block -
					      UDF_SB_PARTROOT(sb,
							      i)) & (sdata->
								     s_packet_len
								     - 1));
L
Linus Torvalds 已提交
287 288 289 290 291 292
					return 0;
				}
			}
			return 1;
		}
	}
293
	if (i == UDF_SB_NUMPARTS(sb)) {
L
Linus Torvalds 已提交
294 295 296 297 298 299 300
		/* outside of partitions */
		/* for now, fail =) */
		return 1;
	}

	return 0;
}