blob: 2d5b254ad9e275352eae070e729a30b6af2f1a14 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hpfs/anode.c
3 *
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5 *
6 * handling HPFS anode tree that contains file allocation info
7 */
8
9#include "hpfs_fn.h"
10
11/* Find a sector in allocation tree */
12
13secno hpfs_bplus_lookup(struct super_block *s, struct inode *inode,
14 struct bplus_header *btree, unsigned sec,
15 struct buffer_head *bh)
16{
17 anode_secno a = -1;
18 struct anode *anode;
19 int i;
20 int c1, c2 = 0;
21 go_down:
22 if (hpfs_sb(s)->sb_chk) if (hpfs_stop_cycles(s, a, &c1, &c2, "hpfs_bplus_lookup")) return -1;
Al Viroddc19e62012-04-17 15:59:35 -040023 if (bp_internal(btree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 for (i = 0; i < btree->n_used_nodes; i++)
Mikulas Patocka0b697602011-05-08 20:44:26 +020025 if (le32_to_cpu(btree->u.internal[i].file_secno) > sec) {
26 a = le32_to_cpu(btree->u.internal[i].down);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 brelse(bh);
28 if (!(anode = hpfs_map_anode(s, a, &bh))) return -1;
29 btree = &anode->btree;
30 goto go_down;
31 }
32 hpfs_error(s, "sector %08x not found in internal anode %08x", sec, a);
33 brelse(bh);
34 return -1;
35 }
36 for (i = 0; i < btree->n_used_nodes; i++)
Mikulas Patocka0b697602011-05-08 20:44:26 +020037 if (le32_to_cpu(btree->u.external[i].file_secno) <= sec &&
38 le32_to_cpu(btree->u.external[i].file_secno) + le32_to_cpu(btree->u.external[i].length) > sec) {
39 a = le32_to_cpu(btree->u.external[i].disk_secno) + sec - le32_to_cpu(btree->u.external[i].file_secno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, a, 1, "data")) {
41 brelse(bh);
42 return -1;
43 }
44 if (inode) {
45 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
Mikulas Patocka0b697602011-05-08 20:44:26 +020046 hpfs_inode->i_file_sec = le32_to_cpu(btree->u.external[i].file_secno);
47 hpfs_inode->i_disk_sec = le32_to_cpu(btree->u.external[i].disk_secno);
48 hpfs_inode->i_n_secs = le32_to_cpu(btree->u.external[i].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
50 brelse(bh);
51 return a;
52 }
53 hpfs_error(s, "sector %08x not found in external anode %08x", sec, a);
54 brelse(bh);
55 return -1;
56}
57
58/* Add a sector to tree */
59
60secno hpfs_add_sector_to_btree(struct super_block *s, secno node, int fnod, unsigned fsecno)
61{
62 struct bplus_header *btree;
63 struct anode *anode = NULL, *ranode = NULL;
64 struct fnode *fnode;
65 anode_secno a, na = -1, ra, up = -1;
66 secno se;
67 struct buffer_head *bh, *bh1, *bh2;
68 int n;
69 unsigned fs;
70 int c1, c2 = 0;
71 if (fnod) {
72 if (!(fnode = hpfs_map_fnode(s, node, &bh))) return -1;
73 btree = &fnode->btree;
74 } else {
75 if (!(anode = hpfs_map_anode(s, node, &bh))) return -1;
76 btree = &anode->btree;
77 }
78 a = node;
79 go_down:
80 if ((n = btree->n_used_nodes - 1) < -!!fnod) {
81 hpfs_error(s, "anode %08x has no entries", a);
82 brelse(bh);
83 return -1;
84 }
Al Viroddc19e62012-04-17 15:59:35 -040085 if (bp_internal(btree)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +020086 a = le32_to_cpu(btree->u.internal[n].down);
87 btree->u.internal[n].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 mark_buffer_dirty(bh);
89 brelse(bh);
90 if (hpfs_sb(s)->sb_chk)
91 if (hpfs_stop_cycles(s, a, &c1, &c2, "hpfs_add_sector_to_btree #1")) return -1;
92 if (!(anode = hpfs_map_anode(s, a, &bh))) return -1;
93 btree = &anode->btree;
94 goto go_down;
95 }
96 if (n >= 0) {
Mikulas Patocka0b697602011-05-08 20:44:26 +020097 if (le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length) != fsecno) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 hpfs_error(s, "allocated size %08x, trying to add sector %08x, %cnode %08x",
Mikulas Patocka0b697602011-05-08 20:44:26 +020099 le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length), fsecno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 fnod?'f':'a', node);
101 brelse(bh);
102 return -1;
103 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200104 if (hpfs_alloc_if_possible(s, se = le32_to_cpu(btree->u.external[n].disk_secno) + le32_to_cpu(btree->u.external[n].length))) {
Wei Yongjun32daab92012-10-04 17:14:39 -0700105 le32_add_cpu(&btree->u.external[n].length, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 mark_buffer_dirty(bh);
107 brelse(bh);
108 return se;
109 }
110 } else {
111 if (fsecno) {
112 hpfs_error(s, "empty file %08x, trying to add sector %08x", node, fsecno);
113 brelse(bh);
114 return -1;
115 }
116 se = !fnod ? node : (node + 16384) & ~16383;
117 }
Mikulas Patocka7d23ce32011-05-08 20:43:06 +0200118 if (!(se = hpfs_alloc_sector(s, se, 1, fsecno*ALLOC_M>ALLOC_FWD_MAX ? ALLOC_FWD_MAX : fsecno*ALLOC_M<ALLOC_FWD_MIN ? ALLOC_FWD_MIN : fsecno*ALLOC_M))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 brelse(bh);
120 return -1;
121 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200122 fs = n < 0 ? 0 : le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (!btree->n_free_nodes) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200124 up = a != node ? le32_to_cpu(anode->up) : -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!(anode = hpfs_alloc_anode(s, a, &na, &bh1))) {
126 brelse(bh);
127 hpfs_free_sectors(s, se, 1);
128 return -1;
129 }
130 if (a == node && fnod) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200131 anode->up = cpu_to_le32(node);
Al Viroddc19e62012-04-17 15:59:35 -0400132 anode->btree.flags |= BP_fnode_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 anode->btree.n_used_nodes = btree->n_used_nodes;
134 anode->btree.first_free = btree->first_free;
135 anode->btree.n_free_nodes = 40 - anode->btree.n_used_nodes;
136 memcpy(&anode->u, &btree->u, btree->n_used_nodes * 12);
Al Viroddc19e62012-04-17 15:59:35 -0400137 btree->flags |= BP_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 btree->n_free_nodes = 11;
139 btree->n_used_nodes = 1;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200140 btree->first_free = cpu_to_le16((char *)&(btree->u.internal[1]) - (char *)btree);
141 btree->u.internal[0].file_secno = cpu_to_le32(-1);
142 btree->u.internal[0].down = cpu_to_le32(na);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 mark_buffer_dirty(bh);
144 } else if (!(ranode = hpfs_alloc_anode(s, /*a*/0, &ra, &bh2))) {
145 brelse(bh);
146 brelse(bh1);
147 hpfs_free_sectors(s, se, 1);
148 hpfs_free_sectors(s, na, 1);
149 return -1;
150 }
151 brelse(bh);
152 bh = bh1;
153 btree = &anode->btree;
154 }
155 btree->n_free_nodes--; n = btree->n_used_nodes++;
Wei Yongjun32daab92012-10-04 17:14:39 -0700156 le16_add_cpu(&btree->first_free, 12);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200157 btree->u.external[n].disk_secno = cpu_to_le32(se);
158 btree->u.external[n].file_secno = cpu_to_le32(fs);
159 btree->u.external[n].length = cpu_to_le32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mark_buffer_dirty(bh);
161 brelse(bh);
162 if ((a == node && fnod) || na == -1) return se;
163 c2 = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200164 while (up != (anode_secno)-1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct anode *new_anode;
166 if (hpfs_sb(s)->sb_chk)
167 if (hpfs_stop_cycles(s, up, &c1, &c2, "hpfs_add_sector_to_btree #2")) return -1;
168 if (up != node || !fnod) {
169 if (!(anode = hpfs_map_anode(s, up, &bh))) return -1;
170 btree = &anode->btree;
171 } else {
172 if (!(fnode = hpfs_map_fnode(s, up, &bh))) return -1;
173 btree = &fnode->btree;
174 }
175 if (btree->n_free_nodes) {
176 btree->n_free_nodes--; n = btree->n_used_nodes++;
Wei Yongjun32daab92012-10-04 17:14:39 -0700177 le16_add_cpu(&btree->first_free, 8);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200178 btree->u.internal[n].file_secno = cpu_to_le32(-1);
179 btree->u.internal[n].down = cpu_to_le32(na);
180 btree->u.internal[n-1].file_secno = cpu_to_le32(fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mark_buffer_dirty(bh);
182 brelse(bh);
183 brelse(bh2);
184 hpfs_free_sectors(s, ra, 1);
185 if ((anode = hpfs_map_anode(s, na, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200186 anode->up = cpu_to_le32(up);
Al Viroddc19e62012-04-17 15:59:35 -0400187 if (up == node && fnod)
188 anode->btree.flags |= BP_fnode_parent;
189 else
190 anode->btree.flags &= ~BP_fnode_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mark_buffer_dirty(bh);
192 brelse(bh);
193 }
194 return se;
195 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200196 up = up != node ? le32_to_cpu(anode->up) : -1;
197 btree->u.internal[btree->n_used_nodes - 1].file_secno = cpu_to_le32(/*fs*/-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mark_buffer_dirty(bh);
199 brelse(bh);
200 a = na;
201 if ((new_anode = hpfs_alloc_anode(s, a, &na, &bh))) {
202 anode = new_anode;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200203 /*anode->up = cpu_to_le32(up != -1 ? up : ra);*/
Al Viroddc19e62012-04-17 15:59:35 -0400204 anode->btree.flags |= BP_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 anode->btree.n_used_nodes = 1;
206 anode->btree.n_free_nodes = 59;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200207 anode->btree.first_free = cpu_to_le16(16);
208 anode->btree.u.internal[0].down = cpu_to_le32(a);
209 anode->btree.u.internal[0].file_secno = cpu_to_le32(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 mark_buffer_dirty(bh);
211 brelse(bh);
212 if ((anode = hpfs_map_anode(s, a, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200213 anode->up = cpu_to_le32(na);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mark_buffer_dirty(bh);
215 brelse(bh);
216 }
217 } else na = a;
218 }
219 if ((anode = hpfs_map_anode(s, na, &bh))) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200220 anode->up = cpu_to_le32(node);
Al Viroddc19e62012-04-17 15:59:35 -0400221 if (fnod)
222 anode->btree.flags |= BP_fnode_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 mark_buffer_dirty(bh);
224 brelse(bh);
225 }
226 if (!fnod) {
227 if (!(anode = hpfs_map_anode(s, node, &bh))) {
228 brelse(bh2);
229 return -1;
230 }
231 btree = &anode->btree;
232 } else {
233 if (!(fnode = hpfs_map_fnode(s, node, &bh))) {
234 brelse(bh2);
235 return -1;
236 }
237 btree = &fnode->btree;
238 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200239 ranode->up = cpu_to_le32(node);
240 memcpy(&ranode->btree, btree, le16_to_cpu(btree->first_free));
Al Viroddc19e62012-04-17 15:59:35 -0400241 if (fnod)
242 ranode->btree.flags |= BP_fnode_parent;
243 ranode->btree.n_free_nodes = (bp_internal(&ranode->btree) ? 60 : 40) - ranode->btree.n_used_nodes;
244 if (bp_internal(&ranode->btree)) for (n = 0; n < ranode->btree.n_used_nodes; n++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct anode *unode;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200246 if ((unode = hpfs_map_anode(s, le32_to_cpu(ranode->u.internal[n].down), &bh1))) {
247 unode->up = cpu_to_le32(ra);
Al Viroddc19e62012-04-17 15:59:35 -0400248 unode->btree.flags &= ~BP_fnode_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 mark_buffer_dirty(bh1);
250 brelse(bh1);
251 }
252 }
Al Viroddc19e62012-04-17 15:59:35 -0400253 btree->flags |= BP_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 btree->n_free_nodes = fnod ? 10 : 58;
255 btree->n_used_nodes = 2;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200256 btree->first_free = cpu_to_le16((char *)&btree->u.internal[2] - (char *)btree);
257 btree->u.internal[0].file_secno = cpu_to_le32(fs);
258 btree->u.internal[0].down = cpu_to_le32(ra);
259 btree->u.internal[1].file_secno = cpu_to_le32(-1);
260 btree->u.internal[1].down = cpu_to_le32(na);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 mark_buffer_dirty(bh);
262 brelse(bh);
263 mark_buffer_dirty(bh2);
264 brelse(bh2);
265 return se;
266}
267
268/*
269 * Remove allocation tree. Recursion would look much nicer but
270 * I want to avoid it because it can cause stack overflow.
271 */
272
273void hpfs_remove_btree(struct super_block *s, struct bplus_header *btree)
274{
275 struct bplus_header *btree1 = btree;
276 struct anode *anode = NULL;
277 anode_secno ano = 0, oano;
278 struct buffer_head *bh;
279 int level = 0;
280 int pos = 0;
281 int i;
282 int c1, c2 = 0;
283 int d1, d2;
284 go_down:
285 d2 = 0;
Al Viroddc19e62012-04-17 15:59:35 -0400286 while (bp_internal(btree1)) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200287 ano = le32_to_cpu(btree1->u.internal[pos].down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (level) brelse(bh);
289 if (hpfs_sb(s)->sb_chk)
290 if (hpfs_stop_cycles(s, ano, &d1, &d2, "hpfs_remove_btree #1"))
291 return;
292 if (!(anode = hpfs_map_anode(s, ano, &bh))) return;
293 btree1 = &anode->btree;
294 level++;
295 pos = 0;
296 }
297 for (i = 0; i < btree1->n_used_nodes; i++)
Mikulas Patocka0b697602011-05-08 20:44:26 +0200298 hpfs_free_sectors(s, le32_to_cpu(btree1->u.external[i].disk_secno), le32_to_cpu(btree1->u.external[i].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 go_up:
300 if (!level) return;
301 brelse(bh);
302 if (hpfs_sb(s)->sb_chk)
303 if (hpfs_stop_cycles(s, ano, &c1, &c2, "hpfs_remove_btree #2")) return;
304 hpfs_free_sectors(s, ano, 1);
305 oano = ano;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200306 ano = le32_to_cpu(anode->up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (--level) {
308 if (!(anode = hpfs_map_anode(s, ano, &bh))) return;
309 btree1 = &anode->btree;
310 } else btree1 = btree;
311 for (i = 0; i < btree1->n_used_nodes; i++) {
Mikulas Patocka0b697602011-05-08 20:44:26 +0200312 if (le32_to_cpu(btree1->u.internal[i].down) == oano) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if ((pos = i + 1) < btree1->n_used_nodes)
314 goto go_down;
315 else
316 goto go_up;
317 }
318 }
319 hpfs_error(s,
320 "reference to anode %08x not found in anode %08x "
321 "(probably bad up pointer)",
322 oano, level ? ano : -1);
323 if (level)
324 brelse(bh);
325}
326
327/* Just a wrapper around hpfs_bplus_lookup .. used for reading eas */
328
329static secno anode_lookup(struct super_block *s, anode_secno a, unsigned sec)
330{
331 struct anode *anode;
332 struct buffer_head *bh;
333 if (!(anode = hpfs_map_anode(s, a, &bh))) return -1;
334 return hpfs_bplus_lookup(s, NULL, &anode->btree, sec, bh);
335}
336
337int hpfs_ea_read(struct super_block *s, secno a, int ano, unsigned pos,
338 unsigned len, char *buf)
339{
340 struct buffer_head *bh;
341 char *data;
342 secno sec;
343 unsigned l;
344 while (len) {
345 if (ano) {
346 if ((sec = anode_lookup(s, a, pos >> 9)) == -1)
347 return -1;
348 } else sec = a + (pos >> 9);
349 if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, sec, 1, "ea #1")) return -1;
350 if (!(data = hpfs_map_sector(s, sec, &bh, (len - 1) >> 9)))
351 return -1;
352 l = 0x200 - (pos & 0x1ff); if (l > len) l = len;
353 memcpy(buf, data + (pos & 0x1ff), l);
354 brelse(bh);
355 buf += l; pos += l; len -= l;
356 }
357 return 0;
358}
359
360int hpfs_ea_write(struct super_block *s, secno a, int ano, unsigned pos,
Al Viro7e7742e2010-01-31 17:09:29 -0500361 unsigned len, const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct buffer_head *bh;
364 char *data;
365 secno sec;
366 unsigned l;
367 while (len) {
368 if (ano) {
369 if ((sec = anode_lookup(s, a, pos >> 9)) == -1)
370 return -1;
371 } else sec = a + (pos >> 9);
372 if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, sec, 1, "ea #2")) return -1;
373 if (!(data = hpfs_map_sector(s, sec, &bh, (len - 1) >> 9)))
374 return -1;
375 l = 0x200 - (pos & 0x1ff); if (l > len) l = len;
376 memcpy(data + (pos & 0x1ff), buf, l);
377 mark_buffer_dirty(bh);
378 brelse(bh);
379 buf += l; pos += l; len -= l;
380 }
381 return 0;
382}
383
384void hpfs_ea_remove(struct super_block *s, secno a, int ano, unsigned len)
385{
386 struct anode *anode;
387 struct buffer_head *bh;
388 if (ano) {
389 if (!(anode = hpfs_map_anode(s, a, &bh))) return;
390 hpfs_remove_btree(s, &anode->btree);
391 brelse(bh);
392 hpfs_free_sectors(s, a, 1);
393 } else hpfs_free_sectors(s, a, (len + 511) >> 9);
394}
395
396/* Truncate allocation tree. Doesn't join anodes - I hope it doesn't matter */
397
398void hpfs_truncate_btree(struct super_block *s, secno f, int fno, unsigned secs)
399{
400 struct fnode *fnode;
401 struct anode *anode;
402 struct buffer_head *bh;
403 struct bplus_header *btree;
404 anode_secno node = f;
405 int i, j, nodes;
406 int c1, c2 = 0;
407 if (fno) {
408 if (!(fnode = hpfs_map_fnode(s, f, &bh))) return;
409 btree = &fnode->btree;
410 } else {
411 if (!(anode = hpfs_map_anode(s, f, &bh))) return;
412 btree = &anode->btree;
413 }
414 if (!secs) {
415 hpfs_remove_btree(s, btree);
416 if (fno) {
417 btree->n_free_nodes = 8;
418 btree->n_used_nodes = 0;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200419 btree->first_free = cpu_to_le16(8);
Al Viroddc19e62012-04-17 15:59:35 -0400420 btree->flags &= ~BP_internal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 mark_buffer_dirty(bh);
422 } else hpfs_free_sectors(s, f, 1);
423 brelse(bh);
424 return;
425 }
Al Viroddc19e62012-04-17 15:59:35 -0400426 while (bp_internal(btree)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 nodes = btree->n_used_nodes + btree->n_free_nodes;
428 for (i = 0; i < btree->n_used_nodes; i++)
Mikulas Patocka0b697602011-05-08 20:44:26 +0200429 if (le32_to_cpu(btree->u.internal[i].file_secno) >= secs) goto f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 brelse(bh);
431 hpfs_error(s, "internal btree %08x doesn't end with -1", node);
432 return;
433 f:
434 for (j = i + 1; j < btree->n_used_nodes; j++)
Mikulas Patocka0b697602011-05-08 20:44:26 +0200435 hpfs_ea_remove(s, le32_to_cpu(btree->u.internal[j].down), 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 btree->n_used_nodes = i + 1;
437 btree->n_free_nodes = nodes - btree->n_used_nodes;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200438 btree->first_free = cpu_to_le16(8 + 8 * btree->n_used_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mark_buffer_dirty(bh);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200440 if (btree->u.internal[i].file_secno == cpu_to_le32(secs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 brelse(bh);
442 return;
443 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200444 node = le32_to_cpu(btree->u.internal[i].down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 brelse(bh);
446 if (hpfs_sb(s)->sb_chk)
447 if (hpfs_stop_cycles(s, node, &c1, &c2, "hpfs_truncate_btree"))
448 return;
449 if (!(anode = hpfs_map_anode(s, node, &bh))) return;
450 btree = &anode->btree;
451 }
452 nodes = btree->n_used_nodes + btree->n_free_nodes;
453 for (i = 0; i < btree->n_used_nodes; i++)
Mikulas Patocka0b697602011-05-08 20:44:26 +0200454 if (le32_to_cpu(btree->u.external[i].file_secno) + le32_to_cpu(btree->u.external[i].length) >= secs) goto ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 brelse(bh);
456 return;
457 ff:
Mikulas Patocka0b697602011-05-08 20:44:26 +0200458 if (secs <= le32_to_cpu(btree->u.external[i].file_secno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 hpfs_error(s, "there is an allocation error in file %08x, sector %08x", f, secs);
460 if (i) i--;
461 }
Mikulas Patocka0b697602011-05-08 20:44:26 +0200462 else if (le32_to_cpu(btree->u.external[i].file_secno) + le32_to_cpu(btree->u.external[i].length) > secs) {
463 hpfs_free_sectors(s, le32_to_cpu(btree->u.external[i].disk_secno) + secs -
464 le32_to_cpu(btree->u.external[i].file_secno), le32_to_cpu(btree->u.external[i].length)
465 - secs + le32_to_cpu(btree->u.external[i].file_secno)); /* I hope gcc optimizes this :-) */
466 btree->u.external[i].length = cpu_to_le32(secs - le32_to_cpu(btree->u.external[i].file_secno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 for (j = i + 1; j < btree->n_used_nodes; j++)
Mikulas Patocka0b697602011-05-08 20:44:26 +0200469 hpfs_free_sectors(s, le32_to_cpu(btree->u.external[j].disk_secno), le32_to_cpu(btree->u.external[j].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 btree->n_used_nodes = i + 1;
471 btree->n_free_nodes = nodes - btree->n_used_nodes;
Mikulas Patocka0b697602011-05-08 20:44:26 +0200472 btree->first_free = cpu_to_le16(8 + 12 * btree->n_used_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 mark_buffer_dirty(bh);
474 brelse(bh);
475}
476
477/* Remove file or directory and it's eas - note that directory must
478 be empty when this is called. */
479
480void hpfs_remove_fnode(struct super_block *s, fnode_secno fno)
481{
482 struct buffer_head *bh;
483 struct fnode *fnode;
484 struct extended_attribute *ea;
485 struct extended_attribute *ea_end;
486 if (!(fnode = hpfs_map_fnode(s, fno, &bh))) return;
Al Viroc4c99542012-04-06 14:30:07 -0400487 if (!fnode_is_dir(fnode)) hpfs_remove_btree(s, &fnode->btree);
Mikulas Patocka0b697602011-05-08 20:44:26 +0200488 else hpfs_remove_dtree(s, le32_to_cpu(fnode->u.external[0].disk_secno));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 ea_end = fnode_end_ea(fnode);
490 for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea))
Al Viro4085e152012-04-06 13:21:09 -0400491 if (ea_indirect(ea))
492 hpfs_ea_remove(s, ea_sec(ea), ea_in_anode(ea), ea_len(ea));
Al Viroc4c99542012-04-06 14:30:07 -0400493 hpfs_ea_ext_remove(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 brelse(bh);
495 hpfs_free_sectors(s, fno, 1);
496}