]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/udf/partition.c
307c9c33d184938b3d9a4b81a8d60b489a8ed3b6
[linux-2.6-omap-h63xx.git] / fs / udf / partition.c
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/slab.h>
28 #include <linux/buffer_head.h>
29
30 inline uint32_t udf_get_pblock(struct super_block *sb, uint32_t block,
31                                uint16_t partition, uint32_t offset)
32 {
33         struct udf_sb_info *sbi = UDF_SB(sb);
34         struct udf_part_map *map;
35         if (partition >= sbi->s_partitions) {
36                 udf_debug("block=%d, partition=%d, offset=%d: "
37                           "invalid partition\n", block, partition, offset);
38                 return 0xFFFFFFFF;
39         }
40         map = &sbi->s_partmaps[partition];
41         if (map->s_partition_func)
42                 return map->s_partition_func(sb, block, partition, offset);
43         else
44                 return map->s_partition_root + block + offset;
45 }
46
47 uint32_t udf_get_pblock_virt15(struct super_block *sb, uint32_t block,
48                                uint16_t partition, uint32_t offset)
49 {
50         struct buffer_head *bh = NULL;
51         uint32_t newblock;
52         uint32_t index;
53         uint32_t loc;
54         struct udf_sb_info *sbi = UDF_SB(sb);
55         struct udf_part_map *map;
56         struct udf_virtual_data *vdata;
57         struct udf_inode_info *iinfo;
58
59         map = &sbi->s_partmaps[partition];
60         vdata = &map->s_type_specific.s_virtual;
61         index = (sb->s_blocksize - vdata->s_start_offset) / sizeof(uint32_t);
62
63         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);
66                 return 0xFFFFFFFF;
67         }
68
69         if (block >= index) {
70                 block -= index;
71                 newblock = 1 + (block / (sb->s_blocksize / sizeof(uint32_t)));
72                 index = block % (sb->s_blocksize / sizeof(uint32_t));
73         } else {
74                 newblock = 0;
75                 index = vdata->s_start_offset / sizeof(uint32_t) + block;
76         }
77
78         loc = udf_block_map(sbi->s_vat_inode, newblock);
79
80         bh = sb_bread(sb, loc);
81         if (!bh) {
82                 udf_debug("get_pblock(UDF_VIRTUAL_MAP:%p,%d,%d) VAT: %d[%d]\n",
83                           sb, block, partition, loc, index);
84                 return 0xFFFFFFFF;
85         }
86
87         loc = le32_to_cpu(((__le32 *)bh->b_data)[index]);
88
89         brelse(bh);
90
91         iinfo = UDF_I(sbi->s_vat_inode);
92         if (iinfo->i_location.partitionReferenceNum == partition) {
93                 udf_debug("recursive call to udf_get_pblock!\n");
94                 return 0xFFFFFFFF;
95         }
96
97         return udf_get_pblock(sb, loc,
98                               iinfo->i_location.partitionReferenceNum,
99                               offset);
100 }
101
102 inline uint32_t udf_get_pblock_virt20(struct super_block *sb, uint32_t block,
103                                       uint16_t partition, uint32_t offset)
104 {
105         return udf_get_pblock_virt15(sb, block, partition, offset);
106 }
107
108 uint32_t udf_get_pblock_spar15(struct super_block *sb, uint32_t block,
109                                uint16_t partition, uint32_t offset)
110 {
111         int i;
112         struct sparingTable *st = NULL;
113         struct udf_sb_info *sbi = UDF_SB(sb);
114         struct udf_part_map *map;
115         uint32_t packet;
116         struct udf_sparing_data *sdata;
117
118         map = &sbi->s_partmaps[partition];
119         sdata = &map->s_type_specific.s_sparing;
120         packet = (block + offset) & ~(sdata->s_packet_len - 1);
121
122         for (i = 0; i < 4; i++) {
123                 if (sdata->s_spar_map[i] != NULL) {
124                         st = (struct sparingTable *)
125                                         sdata->s_spar_map[i]->b_data;
126                         break;
127                 }
128         }
129
130         if (st) {
131                 for (i = 0; i < le16_to_cpu(st->reallocationTableLen); i++) {
132                         struct sparingEntry *entry = &st->mapEntry[i];
133                         u32 origLoc = le32_to_cpu(entry->origLocation);
134                         if (origLoc >= 0xFFFFFFF0)
135                                 break;
136                         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)
141                                 break;
142                 }
143         }
144
145         return map->s_partition_root + block + offset;
146 }
147
148 int 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;
155         struct udf_sb_info *sbi = UDF_SB(sb);
156         u16 reallocationTableLen;
157         struct buffer_head *bh;
158
159         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;
164                         packet = (old_block - map->s_partition_root) &
165                                                 ~(sdata->s_packet_len - 1);
166
167                         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;
171                                         break;
172                                 }
173
174                         if (!st)
175                                 return 1;
176
177                         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) {
184                                         for (; j < 4; j++) {
185                                                 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);
200                                         }
201                                         *new_block = le32_to_cpu(
202                                                         entry->mappedLocation) +
203                                                      ((old_block -
204                                                         map->s_partition_root) &
205                                                      (sdata->s_packet_len - 1));
206                                         return 0;
207                                 } 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));
213                                         return 0;
214                                 } else if (origLoc > packet)
215                                         break;
216                         }
217
218                         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);
244                                 }
245                                 *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;
251                         }
252
253                         return 1;
254                 } /* if old_block */
255         }
256
257         if (i == sbi->s_partitions) {
258                 /* outside of partitions */
259                 /* for now, fail =) */
260                 return 1;
261         }
262
263         return 0;
264 }