xref: /openbmc/linux/fs/udf/partition.c (revision 017cdefe)
1 /*
2  * partition.c
3  *
4  * PURPOSE
5  *      Partition handling routines for the OSTA-UDF(tm) filesystem.
6  *
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  *
17  * 12/06/98 blf  Created file.
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/mutex.h>
28 
29 uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
30 			uint16_t partition, uint32_t offset)
31 {
32 	struct udf_sb_info *sbi = UDF_SB(sb);
33 	struct udf_part_map *map;
34 	if (partition >= sbi->s_partitions) {
35 		udf_debug("block=%u, partition=%u, offset=%u: invalid partition\n",
36 			  block, partition, offset);
37 		return 0xFFFFFFFF;
38 	}
39 	map = &sbi->s_partmaps[partition];
40 	if (map->s_partition_func)
41 		return map->s_partition_func(sb, block, partition, offset);
42 	else
43 		return map->s_partition_root + block + offset;
44 }
45 
46 uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
47 			       uint16_t partition, uint32_t offset)
48 {
49 	struct buffer_head *bh = NULL;
50 	uint32_t newblock;
51 	uint32_t index;
52 	uint32_t loc;
53 	struct udf_sb_info *sbi = UDF_SB(sb);
54 	struct udf_part_map *map;
55 	struct udf_virtual_data *vdata;
56 	struct udf_inode_info *iinfo = UDF_I(sbi->s_vat_inode);
57 	int err;
58 
59 	map = &sbi->s_partmaps[partition];
60 	vdata = &map->s_type_specific.s_virtual;
61 
62 	if (block > vdata->s_num_entries) {
63 		udf_debug("Trying to access block beyond end of VAT (%u max %u)\n",
64 			  block, vdata->s_num_entries);
65 		return 0xFFFFFFFF;
66 	}
67 
68 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
69 		loc = le32_to_cpu(((__le32 *)(iinfo->i_data +
70 			vdata->s_start_offset))[block]);
71 		goto translate;
72 	}
73 	index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
74 	if (block >= index) {
75 		block -= index;
76 		newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
77 		index = block % (sb->s_blocksize / sizeof(uint32_t));
78 	} else {
79 		newblock = 0;
80 		index = vdata->s_start_offset / sizeof(uint32_t) + block;
81 	}
82 
83 	bh = udf_bread(sbi->s_vat_inode, newblock, 0, &err);
84 	if (!bh) {
85 		udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%u,%u)\n",
86 			  sb, block, partition);
87 		return 0xFFFFFFFF;
88 	}
89 
90 	loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
91 
92 	brelse(bh);
93 
94 translate:
95 	if (iinfo->i_location.partitionReferenceNum == partition) {
96 		udf_debug("recursive call to udf_get_pblock!\n");
97 		return 0xFFFFFFFF;
98 	}
99 
100 	return udf_get_pblock(sb, loc,
101 			      iinfo->i_location.partitionReferenceNum,
102 			      offset);
103 }
104 
105 inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
106 				      uint16_t partition, uint32_t offset)
107 {
108 	return udf_get_pblock_virt15(sb, block, partition, offset);
109 }
110 
111 uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
112 			       uint16_t partition, uint32_t offset)
113 {
114 	int i;
115 	struct sparingTable *st = NULL;
116 	struct udf_sb_info *sbi = UDF_SB(sb);
117 	struct udf_part_map *map;
118 	uint32_t packet;
119 	struct udf_sparing_data *sdata;
120 
121 	map = &sbi->s_partmaps[partition];
122 	sdata = &map->s_type_specific.s_sparing;
123 	packet = (block + offset) & ~(sdata->s_packet_len - 1);
124 
125 	for (i = 0; i < 4; i++) {
126 		if (sdata->s_spar_map[i] != NULL) {
127 			st = (struct sparingTable *)
128 					sdata->s_spar_map[i]->b_data;
129 			break;
130 		}
131 	}
132 
133 	if (st) {
134 		for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
135 			struct sparingEntry *entry = &st->mapEntry[i];
136 			u32 origLoc = le32_to_cpu(entry->origLocation);
137 			if (origLoc >= 0xFFFFFFF0)
138 				break;
139 			else if (origLoc == packet)
140 				return le32_to_cpu(entry->mappedLocation) +
141 					((block + offset) &
142 						(sdata->s_packet_len - 1));
143 			else if (origLoc > packet)
144 				break;
145 		}
146 	}
147 
148 	return map->s_partition_root + block + offset;
149 }
150 
151 int udf_relocate_blocks(struct super_block *sb, long old_block, long *new_block)
152 {
153 	struct udf_sparing_data *sdata;
154 	struct sparingTable *st = NULL;
155 	struct sparingEntry mapEntry;
156 	uint32_t packet;
157 	int i, j, k, l;
158 	struct udf_sb_info *sbi = UDF_SB(sb);
159 	u16 reallocationTableLen;
160 	struct buffer_head *bh;
161 	int ret = 0;
162 
163 	mutex_lock(&sbi->s_alloc_mutex);
164 	for (i = 0; i < sbi->s_partitions; i++) {
165 		struct udf_part_map *map = &sbi->s_partmaps[i];
166 		if (old_block > map->s_partition_root &&
167 		    old_block < map->s_partition_root + map->s_partition_len) {
168 			sdata = &map->s_type_specific.s_sparing;
169 			packet = (old_block - map->s_partition_root) &
170 						~(sdata->s_packet_len - 1);
171 
172 			for (j = 0; j < 4; j++)
173 				if (sdata->s_spar_map[j] != NULL) {
174 					st = (struct sparingTable *)
175 						sdata->s_spar_map[j]->b_data;
176 					break;
177 				}
178 
179 			if (!st) {
180 				ret = 1;
181 				goto out;
182 			}
183 
184 			reallocationTableLen =
185 					le16_to_cpu(st->reallocationTableLen);
186 			for (k = 0; k < reallocationTableLen; k++) {
187 				struct sparingEntry *entry = &st->mapEntry[k];
188 				u32 origLoc = le32_to_cpu(entry->origLocation);
189 
190 				if (origLoc == 0xFFFFFFFF) {
191 					for (; j < 4; j++) {
192 						int len;
193 						bh = sdata->s_spar_map[j];
194 						if (!bh)
195 							continue;
196 
197 						st = (struct sparingTable *)
198 								bh->b_data;
199 						entry->origLocation =
200 							cpu_to_le32(packet);
201 						len =
202 						  sizeof(struct sparingTable) +
203 						  reallocationTableLen *
204 						  sizeof(struct sparingEntry);
205 						udf_update_tag((char *)st, len);
206 						mark_buffer_dirty(bh);
207 					}
208 					*new_block = le32_to_cpu(
209 							entry->mappedLocation) +
210 						     ((old_block -
211 							map->s_partition_root) &
212 						     (sdata->s_packet_len - 1));
213 					ret = 0;
214 					goto out;
215 				} else if (origLoc == packet) {
216 					*new_block = le32_to_cpu(
217 							entry->mappedLocation) +
218 						     ((old_block -
219 							map->s_partition_root) &
220 						     (sdata->s_packet_len - 1));
221 					ret = 0;
222 					goto out;
223 				} else if (origLoc > packet)
224 					break;
225 			}
226 
227 			for (l = k; l < reallocationTableLen; l++) {
228 				struct sparingEntry *entry = &st->mapEntry[l];
229 				u32 origLoc = le32_to_cpu(entry->origLocation);
230 
231 				if (origLoc != 0xFFFFFFFF)
232 					continue;
233 
234 				for (; j < 4; j++) {
235 					bh = sdata->s_spar_map[j];
236 					if (!bh)
237 						continue;
238 
239 					st = (struct sparingTable *)bh->b_data;
240 					mapEntry = st->mapEntry[l];
241 					mapEntry.origLocation =
242 							cpu_to_le32(packet);
243 					memmove(&st->mapEntry[k + 1],
244 						&st->mapEntry[k],
245 						(l - k) *
246 						sizeof(struct sparingEntry));
247 					st->mapEntry[k] = mapEntry;
248 					udf_update_tag((char *)st,
249 						sizeof(struct sparingTable) +
250 						reallocationTableLen *
251 						sizeof(struct sparingEntry));
252 					mark_buffer_dirty(bh);
253 				}
254 				*new_block =
255 					le32_to_cpu(
256 					      st->mapEntry[k].mappedLocation) +
257 					((old_block - map->s_partition_root) &
258 					 (sdata->s_packet_len - 1));
259 				ret = 0;
260 				goto out;
261 			}
262 
263 			ret = 1;
264 			goto out;
265 		} /* if old_block */
266 	}
267 
268 	if (i == sbi->s_partitions) {
269 		/* outside of partitions */
270 		/* for now, fail =) */
271 		ret = 1;
272 	}
273 
274 out:
275 	mutex_unlock(&sbi->s_alloc_mutex);
276 	return ret;
277 }
278 
279 static uint32_t udf_try_read_meta(struct inode *inode, uint32_t block,
280 					uint16_t partition, uint32_t offset)
281 {
282 	struct super_block *sb = inode->i_sb;
283 	struct udf_part_map *map;
284 	struct kernel_lb_addr eloc;
285 	uint32_t elen;
286 	sector_t ext_offset;
287 	struct extent_position epos = {};
288 	uint32_t phyblock;
289 
290 	if (inode_bmap(inode, block, &epos, &eloc, &elen, &ext_offset) !=
291 						(EXT_RECORDED_ALLOCATED >> 30))
292 		phyblock = 0xFFFFFFFF;
293 	else {
294 		map = &UDF_SB(sb)->s_partmaps[partition];
295 		/* map to sparable/physical partition desc */
296 		phyblock = udf_get_pblock(sb, eloc.logicalBlockNum,
297 			map->s_type_specific.s_metadata.s_phys_partition_ref,
298 			ext_offset + offset);
299 	}
300 
301 	brelse(epos.bh);
302 	return phyblock;
303 }
304 
305 uint32_t udf_get_pblock_meta25(struct super_block *sb, uint32_t block,
306 				uint16_t partition, uint32_t offset)
307 {
308 	struct udf_sb_info *sbi = UDF_SB(sb);
309 	struct udf_part_map *map;
310 	struct udf_meta_data *mdata;
311 	uint32_t retblk;
312 	struct inode *inode;
313 
314 	udf_debug("READING from METADATA\n");
315 
316 	map = &sbi->s_partmaps[partition];
317 	mdata = &map->s_type_specific.s_metadata;
318 	inode = mdata->s_metadata_fe ? : mdata->s_mirror_fe;
319 
320 	if (!inode)
321 		return 0xFFFFFFFF;
322 
323 	retblk = udf_try_read_meta(inode, block, partition, offset);
324 	if (retblk == 0xFFFFFFFF && mdata->s_metadata_fe) {
325 		udf_warn(sb, "error reading from METADATA, trying to read from MIRROR\n");
326 		if (!(mdata->s_flags & MF_MIRROR_FE_LOADED)) {
327 			mdata->s_mirror_fe = udf_find_metadata_inode_efe(sb,
328 				mdata->s_mirror_file_loc,
329 				mdata->s_phys_partition_ref);
330 			if (IS_ERR(mdata->s_mirror_fe))
331 				mdata->s_mirror_fe = NULL;
332 			mdata->s_flags |= MF_MIRROR_FE_LOADED;
333 		}
334 
335 		inode = mdata->s_mirror_fe;
336 		if (!inode)
337 			return 0xFFFFFFFF;
338 		retblk = udf_try_read_meta(inode, block, partition, offset);
339 	}
340 
341 	return retblk;
342 }
343