blob: eeb4714b36410f9da676d905bcd3b7f23d2e667a [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>
27#include <linux/udf_fs.h>
28#include <linux/slab.h>
29#include <linux/buffer_head.h>
30
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070031inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
32 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Marcin Slusarz6c79e982008-02-08 04:20:30 -080034 struct udf_sb_info *sbi = UDF_SB(sb);
35 struct udf_part_map *map;
36 if (partition >= sbi->s_partitions) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070037 udf_debug("block=%d, partition=%d, offset=%d: invalid partition\n",
38 block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return 0xFFFFFFFF;
40 }
Marcin Slusarz6c79e982008-02-08 04:20:30 -080041 map = &sbi->s_partmaps[partition];
42 if (map->s_partition_func)
43 return map->s_partition_func(sb, block, partition, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 else
Marcin Slusarz6c79e982008-02-08 04:20:30 -080045 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070048uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070049 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 struct buffer_head *bh = NULL;
52 uint32_t newblock;
53 uint32_t index;
54 uint32_t loc;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080055 struct udf_sb_info *sbi = UDF_SB(sb);
56 struct udf_part_map *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Marcin Slusarz6c79e982008-02-08 04:20:30 -080058 map = &sbi->s_partmaps[partition];
59 index = (sb->s_blocksize - map->s_type_specific.s_virtual.s_start_offset) / sizeof(uint32_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Marcin Slusarz6c79e982008-02-08 04:20:30 -080061 if (block > map->s_type_specific.s_virtual.s_num_entries) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070062 udf_debug("Trying to access block beyond end of VAT (%d max %d)\n",
Marcin Slusarz6c79e982008-02-08 04:20:30 -080063 block, map->s_type_specific.s_virtual.s_num_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return 0xFFFFFFFF;
65 }
66
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070067 if (block >= index) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 block -= index;
69 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
70 index = block % (sb->s_blocksize / sizeof(uint32_t));
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 newblock = 0;
Marcin Slusarz6c79e982008-02-08 04:20:30 -080073 index = map->s_type_specific.s_virtual.s_start_offset / sizeof(uint32_t) + block;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
Marcin Slusarz6c79e982008-02-08 04:20:30 -080076 loc = udf_block_map(sbi->s_vat_inode, newblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070078 if (!(bh = sb_bread(sb, loc))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070080 sb, block, partition, loc, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0xFFFFFFFF;
82 }
83
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070084 loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jan Kara3bf25cb2007-05-08 00:35:16 -070086 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Marcin Slusarz6c79e982008-02-08 04:20:30 -080088 if (UDF_I_LOCATION(sbi->s_vat_inode).partitionReferenceNum == partition) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 udf_debug("recursive call to udf_get_pblock!\n");
90 return 0xFFFFFFFF;
91 }
92
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070093 return udf_get_pblock(sb, loc,
Marcin Slusarz6c79e982008-02-08 04:20:30 -080094 UDF_I_LOCATION(sbi->s_vat_inode).partitionReferenceNum,
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070095 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070098inline uint32_t udf_get_pblock_virt20(struct super_block * sb, uint32_t block,
99 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 return udf_get_pblock_virt15(sb, block, partition, offset);
102}
103
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800104uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700105 uint16_t partition, uint32_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int i;
108 struct sparingTable *st = NULL;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800109 struct udf_sb_info *sbi = UDF_SB(sb);
110 struct udf_part_map *map;
111 uint32_t packet;
112
113 map = &sbi->s_partmaps[partition];
114 packet = (block + offset) & ~(map->s_type_specific.s_sparing.s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 for (i = 0; i < 4; i++) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800117 if (map->s_type_specific.s_sparing.s_spar_map[i] != NULL) {
118 st = (struct sparingTable *)map->s_type_specific.s_sparing.s_spar_map[i]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120 }
121 }
122
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700123 if (st) {
124 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700125 if (le32_to_cpu(st->mapEntry[i].origLocation) >= 0xFFFFFFF0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700127 } else if (le32_to_cpu(st->mapEntry[i].origLocation) == packet) {
128 return le32_to_cpu(st->mapEntry[i].mappedLocation) +
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800129 ((block + offset) & (map->s_type_specific.s_sparing.s_packet_len - 1));
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700130 } else if (le32_to_cpu(st->mapEntry[i].origLocation) > packet) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700135
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800136 return map->s_partition_root + block + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
140{
141 struct udf_sparing_data *sdata;
142 struct sparingTable *st = NULL;
143 struct sparingEntry mapEntry;
144 uint32_t packet;
145 int i, j, k, l;
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800146 struct udf_sb_info *sbi = UDF_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800148 for (i = 0; i < sbi->s_partitions; i++) {
149 struct udf_part_map *map = &sbi->s_partmaps[i];
150 if (old_block > map->s_partition_root &&
151 old_block < map->s_partition_root + map->s_partition_len) {
152 sdata = &map->s_type_specific.s_sparing;
153 packet = (old_block - map->s_partition_root) & ~(sdata->s_packet_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 for (j = 0; j < 4; j++) {
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800156 if (map->s_type_specific.s_sparing.s_spar_map[j] != NULL) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700157 st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 }
160 }
161
162 if (!st)
163 return 1;
164
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700165 for (k = 0; k < le16_to_cpu(st->reallocationTableLen); k++) {
166 if (le32_to_cpu(st->mapEntry[k].origLocation) == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700167 for (; j < 4; j++) {
168 if (sdata->s_spar_map[j]) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700169 st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
170 st->mapEntry[k].origLocation = cpu_to_le32(packet);
171 udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
172 mark_buffer_dirty(sdata->s_spar_map[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700175 *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800176 ((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700178 } else if (le32_to_cpu(st->mapEntry[k].origLocation) == packet) {
179 *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800180 ((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700182 } else if (le32_to_cpu(st->mapEntry[k].origLocation) > packet) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700186
187 for (l = k; l < le16_to_cpu(st->reallocationTableLen); l++) {
188 if (le32_to_cpu(st->mapEntry[l].origLocation) == 0xFFFFFFFF) {
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700189 for (; j < 4; j++) {
190 if (sdata->s_spar_map[j]) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700191 st = (struct sparingTable *)sdata->s_spar_map[j]->b_data;
192 mapEntry = st->mapEntry[l];
193 mapEntry.origLocation = cpu_to_le32(packet);
194 memmove(&st->mapEntry[k + 1], &st->mapEntry[k], (l - k) * sizeof(struct sparingEntry));
195 st->mapEntry[k] = mapEntry;
196 udf_update_tag((char *)st, sizeof(struct sparingTable) + le16_to_cpu(st->reallocationTableLen) * sizeof(struct sparingEntry));
197 mark_buffer_dirty(sdata->s_spar_map[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700200 *new_block = le32_to_cpu(st->mapEntry[k].mappedLocation) +
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800201 ((old_block - map->s_partition_root) & (sdata->s_packet_len - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203 }
204 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 1;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700207 } /* if old_block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700209
Marcin Slusarz6c79e982008-02-08 04:20:30 -0800210 if (i == sbi->s_partitions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* outside of partitions */
212 /* for now, fail =) */
213 return 1;
214 }
215
216 return 0;
217}