Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 13 | secno 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 Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 23 | if (bp_internal(btree)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | for (i = 0; i < btree->n_used_nodes; i++) |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 25 | if (le32_to_cpu(btree->u.internal[i].file_secno) > sec) { |
| 26 | a = le32_to_cpu(btree->u.internal[i].down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 37 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 46 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 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 | |
| 60 | secno 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 Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 85 | if (bp_internal(btree)) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 86 | a = le32_to_cpu(btree->u.internal[n].down); |
| 87 | btree->u.internal[n].file_secno = cpu_to_le32(-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 97 | if (le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length) != fsecno) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | hpfs_error(s, "allocated size %08x, trying to add sector %08x, %cnode %08x", |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 99 | le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length), fsecno, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | fnod?'f':'a', node); |
| 101 | brelse(bh); |
| 102 | return -1; |
| 103 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 104 | if (hpfs_alloc_if_possible(s, se = le32_to_cpu(btree->u.external[n].disk_secno) + le32_to_cpu(btree->u.external[n].length))) { |
| 105 | btree->u.external[n].length = cpu_to_le32(le32_to_cpu(btree->u.external[n].length) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | 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 Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 118 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | brelse(bh); |
| 120 | return -1; |
| 121 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 122 | fs = n < 0 ? 0 : le32_to_cpu(btree->u.external[n].file_secno) + le32_to_cpu(btree->u.external[n].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | if (!btree->n_free_nodes) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 124 | up = a != node ? le32_to_cpu(anode->up) : -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 131 | anode->up = cpu_to_le32(node); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 132 | anode->btree.flags |= BP_fnode_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | 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 Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 137 | btree->flags |= BP_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | btree->n_free_nodes = 11; |
| 139 | btree->n_used_nodes = 1; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 140 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | 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++; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 156 | btree->first_free = cpu_to_le16(le16_to_cpu(btree->first_free) + 12); |
| 157 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | mark_buffer_dirty(bh); |
| 161 | brelse(bh); |
| 162 | if ((a == node && fnod) || na == -1) return se; |
| 163 | c2 = 0; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 164 | while (up != (anode_secno)-1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | 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++; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 177 | btree->first_free = cpu_to_le16(le16_to_cpu(btree->first_free) + 8); |
| 178 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 186 | anode->up = cpu_to_le32(up); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 187 | if (up == node && fnod) |
| 188 | anode->btree.flags |= BP_fnode_parent; |
| 189 | else |
| 190 | anode->btree.flags &= ~BP_fnode_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | mark_buffer_dirty(bh); |
| 192 | brelse(bh); |
| 193 | } |
| 194 | return se; |
| 195 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 196 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 203 | /*anode->up = cpu_to_le32(up != -1 ? up : ra);*/ |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 204 | anode->btree.flags |= BP_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | anode->btree.n_used_nodes = 1; |
| 206 | anode->btree.n_free_nodes = 59; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 207 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | mark_buffer_dirty(bh); |
| 211 | brelse(bh); |
| 212 | if ((anode = hpfs_map_anode(s, a, &bh))) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 213 | anode->up = cpu_to_le32(na); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | mark_buffer_dirty(bh); |
| 215 | brelse(bh); |
| 216 | } |
| 217 | } else na = a; |
| 218 | } |
| 219 | if ((anode = hpfs_map_anode(s, na, &bh))) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 220 | anode->up = cpu_to_le32(node); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 221 | if (fnod) |
| 222 | anode->btree.flags |= BP_fnode_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 239 | ranode->up = cpu_to_le32(node); |
| 240 | memcpy(&ranode->btree, btree, le16_to_cpu(btree->first_free)); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 241 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | struct anode *unode; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 246 | if ((unode = hpfs_map_anode(s, le32_to_cpu(ranode->u.internal[n].down), &bh1))) { |
| 247 | unode->up = cpu_to_le32(ra); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 248 | unode->btree.flags &= ~BP_fnode_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | mark_buffer_dirty(bh1); |
| 250 | brelse(bh1); |
| 251 | } |
| 252 | } |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 253 | btree->flags |= BP_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | btree->n_free_nodes = fnod ? 10 : 58; |
| 255 | btree->n_used_nodes = 2; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 256 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | 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 | |
| 273 | void 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 Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 286 | while (bp_internal(btree1)) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 287 | ano = le32_to_cpu(btree1->u.internal[pos].down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 298 | hpfs_free_sectors(s, le32_to_cpu(btree1->u.external[i].disk_secno), le32_to_cpu(btree1->u.external[i].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 306 | ano = le32_to_cpu(anode->up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 312 | if (le32_to_cpu(btree1->u.internal[i].down) == oano) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | 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 | |
| 329 | static 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 | |
| 337 | int 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 | |
| 360 | int hpfs_ea_write(struct super_block *s, secno a, int ano, unsigned pos, |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 361 | unsigned len, const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
| 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 | |
| 384 | void 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 | |
| 398 | void 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 419 | btree->first_free = cpu_to_le16(8); |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 420 | btree->flags &= ~BP_internal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | mark_buffer_dirty(bh); |
| 422 | } else hpfs_free_sectors(s, f, 1); |
| 423 | brelse(bh); |
| 424 | return; |
| 425 | } |
Al Viro | ddc19e6 | 2012-04-17 15:59:35 -0400 | [diff] [blame^] | 426 | while (bp_internal(btree)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | nodes = btree->n_used_nodes + btree->n_free_nodes; |
| 428 | for (i = 0; i < btree->n_used_nodes; i++) |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 429 | if (le32_to_cpu(btree->u.internal[i].file_secno) >= secs) goto f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 435 | hpfs_ea_remove(s, le32_to_cpu(btree->u.internal[j].down), 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | btree->n_used_nodes = i + 1; |
| 437 | btree->n_free_nodes = nodes - btree->n_used_nodes; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 438 | btree->first_free = cpu_to_le16(8 + 8 * btree->n_used_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | mark_buffer_dirty(bh); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 440 | if (btree->u.internal[i].file_secno == cpu_to_le32(secs)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | brelse(bh); |
| 442 | return; |
| 443 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 444 | node = le32_to_cpu(btree->u.internal[i].down); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | 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 Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 454 | if (le32_to_cpu(btree->u.external[i].file_secno) + le32_to_cpu(btree->u.external[i].length) >= secs) goto ff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | brelse(bh); |
| 456 | return; |
| 457 | ff: |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 458 | if (secs <= le32_to_cpu(btree->u.external[i].file_secno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | hpfs_error(s, "there is an allocation error in file %08x, sector %08x", f, secs); |
| 460 | if (i) i--; |
| 461 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 462 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | for (j = i + 1; j < btree->n_used_nodes; j++) |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 469 | hpfs_free_sectors(s, le32_to_cpu(btree->u.external[j].disk_secno), le32_to_cpu(btree->u.external[j].length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | btree->n_used_nodes = i + 1; |
| 471 | btree->n_free_nodes = nodes - btree->n_used_nodes; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 472 | btree->first_free = cpu_to_le16(8 + 12 * btree->n_used_nodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | 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 | |
| 480 | void 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 Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 487 | if (!fnode_is_dir(fnode)) hpfs_remove_btree(s, &fnode->btree); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 488 | else hpfs_remove_dtree(s, le32_to_cpu(fnode->u.external[0].disk_secno)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | ea_end = fnode_end_ea(fnode); |
| 490 | for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea)) |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 491 | if (ea_indirect(ea)) |
| 492 | hpfs_ea_remove(s, ea_sec(ea), ea_in_anode(ea), ea_len(ea)); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 493 | hpfs_ea_ext_remove(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | brelse(bh); |
| 495 | hpfs_free_sectors(s, fno, 1); |
| 496 | } |