partition.c 6.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
 *
17
 * 12/06/98 blf  Created file.
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 */

#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
{
M
Marcin Slusarz 已提交
34 35 36
	struct udf_sb_info *sbi = UDF_SB(sb);
	struct udf_part_map *map;
	if (partition >= sbi->s_partitions) {
37 38
		udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
			  block, partition, offset);
L
Linus Torvalds 已提交
39 40
		return 0xFFFFFFFF;
	}
M
Marcin Slusarz 已提交
41 42 43
	map = &sbi->s_partmaps[partition];
	if (map->s_partition_func)
		return map->s_partition_func(sb, block, partition, offset);
L
Linus Torvalds 已提交
44
	else
M
Marcin Slusarz 已提交
45
		return map->s_partition_root + block + offset;
L
Linus Torvalds 已提交
46 47
}

48
uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
49
			       uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
50 51 52 53 54
{
	struct buffer_head *bh = NULL;
	uint32_t newblock;
	uint32_t index;
	uint32_t loc;
M
Marcin Slusarz 已提交
55 56
	struct udf_sb_info *sbi = UDF_SB(sb);
	struct udf_part_map *map;
L
Linus Torvalds 已提交
57

M
Marcin Slusarz 已提交
58 59
	map = &sbi->s_partmaps[partition];
	index = (sb->s_blocksize - map->s_type_specific.s_virtual.s_start_offset) / sizeof(uint32_t);
L
Linus Torvalds 已提交
60

M
Marcin Slusarz 已提交
61
	if (block > map->s_type_specific.s_virtual.s_num_entries) {
62
		udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
M
Marcin Slusarz 已提交
63
			  block, map->s_type_specific.s_virtual.s_num_entries);
L
Linus Torvalds 已提交
64 65 66
		return 0xFFFFFFFF;
	}

67
	if (block >= index) {
L
Linus Torvalds 已提交
68 69 70
		block -= index;
		newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
		index = block % (sb->s_blocksize / sizeof(uint32_t));
71
	} else {
L
Linus Torvalds 已提交
72
		newblock = 0;
M
Marcin Slusarz 已提交
73
		index = map->s_type_specific.s_virtual.s_start_offset / sizeof(uint32_t) + block;
L
Linus Torvalds 已提交
74 75
	}

M
Marcin Slusarz 已提交
76
	loc = udf_block_map(sbi->s_vat_inode, newblock);
L
Linus Torvalds 已提交
77

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

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

J
Jan Kara 已提交
86
	brelse(bh);
L
Linus Torvalds 已提交
87

M
Marcin Slusarz 已提交
88
	if (UDF_I_LOCATION(sbi->s_vat_inode).partitionReferenceNum == partition) {
L
Linus Torvalds 已提交
89 90 91 92
		udf_debug("recursive call to udf_get_pblock!\n");
		return 0xFFFFFFFF;
	}

93
	return udf_get_pblock(sb, loc,
M
Marcin Slusarz 已提交
94
			      UDF_I_LOCATION(sbi->s_vat_inode).partitionReferenceNum,
95
			      offset);
L
Linus Torvalds 已提交
96 97
}

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

M
Marcin Slusarz 已提交
104
uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
105
			       uint16_t partition, uint32_t offset)
L
Linus Torvalds 已提交
106 107 108
{
	int i;
	struct sparingTable *st = NULL;
M
Marcin Slusarz 已提交
109 110 111 112 113 114
	struct udf_sb_info *sbi = UDF_SB(sb);
	struct udf_part_map *map;
	uint32_t packet;

	map = &sbi->s_partmaps[partition];
	packet = (block + offset) & ~(map->s_type_specific.s_sparing.s_packet_len - 1);
L
Linus Torvalds 已提交
115

116
	for (i = 0; i < 4; i++) {
M
Marcin Slusarz 已提交
117 118
		if (map->s_type_specific.s_sparing.s_spar_map[i] != NULL) {
			st = (struct sparingTable *)map->s_type_specific.s_sparing.s_spar_map[i]->b_data;
L
Linus Torvalds 已提交
119 120 121 122
			break;
		}
	}

123 124
	if (st) {
		for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
125
			if (le32_to_cpu(st->mapEntry[i].origLocation) >= 0xFFFFFFF0) {
L
Linus Torvalds 已提交
126
				break;
127 128
			} else if (le32_to_cpu(st->mapEntry[i].origLocation) == packet) {
				return le32_to_cpu(st->mapEntry[i].mappedLocation) +
M
Marcin Slusarz 已提交
129
					((block + offset) & (map->s_type_specific.s_sparing.s_packet_len - 1));
130
			} else if (le32_to_cpu(st->mapEntry[i].origLocation) > packet) {
L
Linus Torvalds 已提交
131
				break;
132
			}
L
Linus Torvalds 已提交
133 134
		}
	}
135

M
Marcin Slusarz 已提交
136
	return map->s_partition_root + block + offset;
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145
}

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;
M
Marcin Slusarz 已提交
146
	struct udf_sb_info *sbi = UDF_SB(sb);
L
Linus Torvalds 已提交
147

M
Marcin Slusarz 已提交
148 149 150 151 152 153
	for (i = 0; i < sbi->s_partitions; i++) {
		struct udf_part_map *map = &sbi->s_partmaps[i];
		if (old_block > map->s_partition_root &&
		    old_block < map->s_partition_root + map->s_partition_len) {
			sdata = &map->s_type_specific.s_sparing;
			packet = (old_block - map->s_partition_root) & ~(sdata->s_packet_len - 1);
154 155

			for (j = 0; j < 4; j++) {
M
Marcin Slusarz 已提交
156
				if (map->s_type_specific.s_sparing.s_spar_map[j] != NULL) {
157
					st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
L
Linus Torvalds 已提交
158 159 160 161 162 163 164
					break;
				}
			}

			if (!st)
				return 1;

165 166
			for (k = 0; k < le16_to_cpu(st->reallocationTableLen); k++) {
				if (le32_to_cpu(st->mapEntry[k].origLocation) == 0xFFFFFFFF) {
167 168
					for (; j < 4; j++) {
						if (sdata->s_spar_map[j]) {
169 170 171 172
							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 已提交
173 174
						}
					}
175
					*new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
M
Marcin Slusarz 已提交
176
						((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
L
Linus Torvalds 已提交
177
					return 0;
178 179
				} else if (le32_to_cpu(st->mapEntry[k].origLocation) == packet) {
					*new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
M
Marcin Slusarz 已提交
180
						((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
L
Linus Torvalds 已提交
181
					return 0;
182
				} else if (le32_to_cpu(st->mapEntry[k].origLocation) > packet) {
L
Linus Torvalds 已提交
183
					break;
184
				}
L
Linus Torvalds 已提交
185
			}
186 187 188

			for (l = k; l < le16_to_cpu(st->reallocationTableLen); l++) {
				if (le32_to_cpu(st->mapEntry[l].origLocation) == 0xFFFFFFFF) {
189 190
					for (; j < 4; j++) {
						if (sdata->s_spar_map[j]) {
191 192 193 194 195 196 197
							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 已提交
198 199
						}
					}
200
					*new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
M
Marcin Slusarz 已提交
201
						((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
L
Linus Torvalds 已提交
202 203 204
					return 0;
				}
			}
205

L
Linus Torvalds 已提交
206
			return 1;
207
		} /* if old_block */
L
Linus Torvalds 已提交
208
	}
209

M
Marcin Slusarz 已提交
210
	if (i == sbi->s_partitions) {
L
Linus Torvalds 已提交
211 212 213 214 215 216 217
		/* outside of partitions */
		/* for now, fail =) */
		return 1;
	}

	return 0;
}