Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 LT |
24 | #include "xfs_trans.h" |
25 | #include "xfs_sb.h" | |
26 | #include "xfs_ag.h" | |
1da177e4 LT |
27 | #include "xfs_dir2.h" |
28 | #include "xfs_dmapi.h" | |
29 | #include "xfs_mount.h" | |
a844f451 | 30 | #include "xfs_da_btree.h" |
1da177e4 LT |
31 | #include "xfs_bmap_btree.h" |
32 | #include "xfs_attr_sf.h" | |
1da177e4 LT |
33 | #include "xfs_dir2_sf.h" |
34 | #include "xfs_dinode.h" | |
35 | #include "xfs_inode.h" | |
36 | #include "xfs_bmap.h" | |
1da177e4 LT |
37 | #include "xfs_dir2_data.h" |
38 | #include "xfs_dir2_leaf.h" | |
39 | #include "xfs_dir2_block.h" | |
40 | #include "xfs_dir2_node.h" | |
41 | #include "xfs_dir2_trace.h" | |
42 | #include "xfs_error.h" | |
1da177e4 LT |
43 | |
44 | /* | |
45 | * Local function declarations. | |
46 | */ | |
47 | #ifdef DEBUG | |
48 | static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp); | |
49 | #else | |
50 | #define xfs_dir2_leaf_check(dp, bp) | |
51 | #endif | |
52 | static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp, | |
53 | int *indexp, xfs_dabuf_t **dbpp); | |
ba0f32d4 CH |
54 | static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp, |
55 | int first, int last); | |
6add2c42 | 56 | static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp); |
ba0f32d4 | 57 | |
1da177e4 LT |
58 | |
59 | /* | |
60 | * Convert a block form directory to a leaf form directory. | |
61 | */ | |
62 | int /* error */ | |
63 | xfs_dir2_block_to_leaf( | |
64 | xfs_da_args_t *args, /* operation arguments */ | |
65 | xfs_dabuf_t *dbp) /* input block's buffer */ | |
66 | { | |
68b3a102 | 67 | __be16 *bestsp; /* leaf's bestsp entries */ |
1da177e4 LT |
68 | xfs_dablk_t blkno; /* leaf block's bno */ |
69 | xfs_dir2_block_t *block; /* block structure */ | |
70 | xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ | |
71 | xfs_dir2_block_tail_t *btp; /* block's tail */ | |
72 | xfs_inode_t *dp; /* incore directory inode */ | |
73 | int error; /* error return code */ | |
74 | xfs_dabuf_t *lbp; /* leaf block's buffer */ | |
75 | xfs_dir2_db_t ldb; /* leaf block's bno */ | |
76 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
77 | xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ | |
78 | xfs_mount_t *mp; /* filesystem mount point */ | |
79 | int needlog; /* need to log block header */ | |
80 | int needscan; /* need to rescan bestfree */ | |
81 | xfs_trans_t *tp; /* transaction pointer */ | |
82 | ||
83 | xfs_dir2_trace_args_b("block_to_leaf", args, dbp); | |
84 | dp = args->dp; | |
85 | mp = dp->i_mount; | |
86 | tp = args->trans; | |
87 | /* | |
88 | * Add the leaf block to the inode. | |
89 | * This interface will only put blocks in the leaf/node range. | |
90 | * Since that's empty now, we'll get the root (block 0 in range). | |
91 | */ | |
92 | if ((error = xfs_da_grow_inode(args, &blkno))) { | |
93 | return error; | |
94 | } | |
bbaaf538 | 95 | ldb = xfs_dir2_da_to_db(mp, blkno); |
1da177e4 LT |
96 | ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp)); |
97 | /* | |
98 | * Initialize the leaf block, get a buffer for it. | |
99 | */ | |
100 | if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) { | |
101 | return error; | |
102 | } | |
103 | ASSERT(lbp != NULL); | |
104 | leaf = lbp->data; | |
105 | block = dbp->data; | |
106 | xfs_dir2_data_check(dp, dbp); | |
bbaaf538 CH |
107 | btp = xfs_dir2_block_tail_p(mp, block); |
108 | blp = xfs_dir2_block_leaf_p(btp); | |
1da177e4 LT |
109 | /* |
110 | * Set the counts in the leaf header. | |
111 | */ | |
a818e5de NS |
112 | leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count)); |
113 | leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale)); | |
1da177e4 LT |
114 | /* |
115 | * Could compact these but I think we always do the conversion | |
116 | * after squeezing out stale entries. | |
117 | */ | |
e922fffa | 118 | memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t)); |
a818e5de | 119 | xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1); |
1da177e4 LT |
120 | needscan = 0; |
121 | needlog = 1; | |
122 | /* | |
123 | * Make the space formerly occupied by the leaf entries and block | |
124 | * tail be free. | |
125 | */ | |
126 | xfs_dir2_data_make_free(tp, dbp, | |
127 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)block), | |
128 | (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize - | |
129 | (char *)blp), | |
130 | &needlog, &needscan); | |
131 | /* | |
132 | * Fix up the block header, make it a data block. | |
133 | */ | |
70e73f59 | 134 | block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
1da177e4 | 135 | if (needscan) |
ef497f8a | 136 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog); |
1da177e4 LT |
137 | /* |
138 | * Set up leaf tail and bests table. | |
139 | */ | |
bbaaf538 | 140 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
afbcb3f9 | 141 | ltp->bestcount = cpu_to_be32(1); |
bbaaf538 | 142 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
70e73f59 | 143 | bestsp[0] = block->hdr.bestfree[0].length; |
1da177e4 LT |
144 | /* |
145 | * Log the data header and leaf bests table. | |
146 | */ | |
147 | if (needlog) | |
148 | xfs_dir2_data_log_header(tp, dbp); | |
149 | xfs_dir2_leaf_check(dp, lbp); | |
150 | xfs_dir2_data_check(dp, dbp); | |
151 | xfs_dir2_leaf_log_bests(tp, lbp, 0, 0); | |
152 | xfs_da_buf_done(lbp); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | /* | |
157 | * Add an entry to a leaf form directory. | |
158 | */ | |
159 | int /* error */ | |
160 | xfs_dir2_leaf_addname( | |
161 | xfs_da_args_t *args) /* operation arguments */ | |
162 | { | |
68b3a102 | 163 | __be16 *bestsp; /* freespace table in leaf */ |
1da177e4 LT |
164 | int compact; /* need to compact leaves */ |
165 | xfs_dir2_data_t *data; /* data block structure */ | |
166 | xfs_dabuf_t *dbp; /* data block buffer */ | |
167 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
168 | xfs_inode_t *dp; /* incore directory inode */ | |
169 | xfs_dir2_data_unused_t *dup; /* data unused entry */ | |
170 | int error; /* error return value */ | |
171 | int grown; /* allocated new data block */ | |
172 | int highstale; /* index of next stale leaf */ | |
173 | int i; /* temporary, index */ | |
174 | int index; /* leaf table position */ | |
175 | xfs_dabuf_t *lbp; /* leaf's buffer */ | |
176 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
177 | int length; /* length of new entry */ | |
178 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ | |
179 | int lfloglow; /* low leaf logging index */ | |
180 | int lfloghigh; /* high leaf logging index */ | |
181 | int lowstale; /* index of prev stale leaf */ | |
182 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ | |
183 | xfs_mount_t *mp; /* filesystem mount point */ | |
184 | int needbytes; /* leaf block bytes needed */ | |
185 | int needlog; /* need to log data header */ | |
186 | int needscan; /* need to rescan data free */ | |
3d693c6e | 187 | __be16 *tagp; /* end of data entry */ |
1da177e4 LT |
188 | xfs_trans_t *tp; /* transaction pointer */ |
189 | xfs_dir2_db_t use_block; /* data block number */ | |
190 | ||
191 | xfs_dir2_trace_args("leaf_addname", args); | |
192 | dp = args->dp; | |
193 | tp = args->trans; | |
194 | mp = dp->i_mount; | |
195 | /* | |
196 | * Read the leaf block. | |
197 | */ | |
198 | error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, | |
199 | XFS_DATA_FORK); | |
200 | if (error) { | |
201 | return error; | |
202 | } | |
203 | ASSERT(lbp != NULL); | |
204 | /* | |
205 | * Look up the entry by hash value and name. | |
206 | * We know it's not there, our caller has already done a lookup. | |
207 | * So the index is of the entry to insert in front of. | |
208 | * But if there are dup hash values the index is of the first of those. | |
209 | */ | |
210 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
211 | leaf = lbp->data; | |
bbaaf538 CH |
212 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
213 | bestsp = xfs_dir2_leaf_bests_p(ltp); | |
214 | length = xfs_dir2_data_entsize(args->namelen); | |
1da177e4 LT |
215 | /* |
216 | * See if there are any entries with the same hash value | |
217 | * and space in their block for the new entry. | |
218 | * This is good because it puts multiple same-hash value entries | |
219 | * in a data block, improving the lookup of those entries. | |
220 | */ | |
221 | for (use_block = -1, lep = &leaf->ents[index]; | |
3c1f9c15 | 222 | index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval; |
1da177e4 | 223 | index++, lep++) { |
3c1f9c15 | 224 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 | 225 | continue; |
bbaaf538 | 226 | i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
afbcb3f9 | 227 | ASSERT(i < be32_to_cpu(ltp->bestcount)); |
68b3a102 NS |
228 | ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF); |
229 | if (be16_to_cpu(bestsp[i]) >= length) { | |
1da177e4 LT |
230 | use_block = i; |
231 | break; | |
232 | } | |
233 | } | |
234 | /* | |
235 | * Didn't find a block yet, linear search all the data blocks. | |
236 | */ | |
237 | if (use_block == -1) { | |
afbcb3f9 | 238 | for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) { |
1da177e4 LT |
239 | /* |
240 | * Remember a block we see that's missing. | |
241 | */ | |
68b3a102 | 242 | if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1) |
1da177e4 | 243 | use_block = i; |
68b3a102 | 244 | else if (be16_to_cpu(bestsp[i]) >= length) { |
1da177e4 LT |
245 | use_block = i; |
246 | break; | |
247 | } | |
248 | } | |
249 | } | |
250 | /* | |
251 | * How many bytes do we need in the leaf block? | |
252 | */ | |
253 | needbytes = | |
254 | (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) + | |
255 | (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0])); | |
256 | /* | |
257 | * Now kill use_block if it refers to a missing block, so we | |
258 | * can use it as an indication of allocation needed. | |
259 | */ | |
68b3a102 | 260 | if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF) |
1da177e4 LT |
261 | use_block = -1; |
262 | /* | |
263 | * If we don't have enough free bytes but we can make enough | |
264 | * by compacting out stale entries, we'll do that. | |
265 | */ | |
6a178100 BN |
266 | if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] < |
267 | needbytes && be16_to_cpu(leaf->hdr.stale) > 1) { | |
1da177e4 LT |
268 | compact = 1; |
269 | } | |
270 | /* | |
271 | * Otherwise if we don't have enough free bytes we need to | |
272 | * convert to node form. | |
273 | */ | |
6a178100 BN |
274 | else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu( |
275 | leaf->hdr.count)] < needbytes) { | |
1da177e4 LT |
276 | /* |
277 | * Just checking or no space reservation, give up. | |
278 | */ | |
6a178100 BN |
279 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || |
280 | args->total == 0) { | |
1da177e4 LT |
281 | xfs_da_brelse(tp, lbp); |
282 | return XFS_ERROR(ENOSPC); | |
283 | } | |
284 | /* | |
285 | * Convert to node form. | |
286 | */ | |
287 | error = xfs_dir2_leaf_to_node(args, lbp); | |
288 | xfs_da_buf_done(lbp); | |
289 | if (error) | |
290 | return error; | |
291 | /* | |
292 | * Then add the new entry. | |
293 | */ | |
294 | return xfs_dir2_node_addname(args); | |
295 | } | |
296 | /* | |
297 | * Otherwise it will fit without compaction. | |
298 | */ | |
299 | else | |
300 | compact = 0; | |
301 | /* | |
302 | * If just checking, then it will fit unless we needed to allocate | |
303 | * a new data block. | |
304 | */ | |
6a178100 | 305 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { |
1da177e4 LT |
306 | xfs_da_brelse(tp, lbp); |
307 | return use_block == -1 ? XFS_ERROR(ENOSPC) : 0; | |
308 | } | |
309 | /* | |
310 | * If no allocations are allowed, return now before we've | |
311 | * changed anything. | |
312 | */ | |
313 | if (args->total == 0 && use_block == -1) { | |
314 | xfs_da_brelse(tp, lbp); | |
315 | return XFS_ERROR(ENOSPC); | |
316 | } | |
317 | /* | |
318 | * Need to compact the leaf entries, removing stale ones. | |
319 | * Leave one stale entry behind - the one closest to our | |
320 | * insertion index - and we'll shift that one to our insertion | |
321 | * point later. | |
322 | */ | |
323 | if (compact) { | |
324 | xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale, | |
325 | &lfloglow, &lfloghigh); | |
326 | } | |
327 | /* | |
328 | * There are stale entries, so we'll need log-low and log-high | |
329 | * impossibly bad values later. | |
330 | */ | |
a818e5de NS |
331 | else if (be16_to_cpu(leaf->hdr.stale)) { |
332 | lfloglow = be16_to_cpu(leaf->hdr.count); | |
1da177e4 LT |
333 | lfloghigh = -1; |
334 | } | |
335 | /* | |
336 | * If there was no data block space found, we need to allocate | |
337 | * a new one. | |
338 | */ | |
339 | if (use_block == -1) { | |
340 | /* | |
341 | * Add the new data block. | |
342 | */ | |
343 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, | |
344 | &use_block))) { | |
345 | xfs_da_brelse(tp, lbp); | |
346 | return error; | |
347 | } | |
348 | /* | |
349 | * Initialize the block. | |
350 | */ | |
351 | if ((error = xfs_dir2_data_init(args, use_block, &dbp))) { | |
352 | xfs_da_brelse(tp, lbp); | |
353 | return error; | |
354 | } | |
355 | /* | |
356 | * If we're adding a new data block on the end we need to | |
357 | * extend the bests table. Copy it up one entry. | |
358 | */ | |
afbcb3f9 | 359 | if (use_block >= be32_to_cpu(ltp->bestcount)) { |
1da177e4 LT |
360 | bestsp--; |
361 | memmove(&bestsp[0], &bestsp[1], | |
afbcb3f9 | 362 | be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0])); |
413d57c9 | 363 | be32_add_cpu(<p->bestcount, 1); |
1da177e4 | 364 | xfs_dir2_leaf_log_tail(tp, lbp); |
afbcb3f9 | 365 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
1da177e4 LT |
366 | } |
367 | /* | |
368 | * If we're filling in a previously empty block just log it. | |
369 | */ | |
370 | else | |
371 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); | |
372 | data = dbp->data; | |
70e73f59 | 373 | bestsp[use_block] = data->hdr.bestfree[0].length; |
1da177e4 LT |
374 | grown = 1; |
375 | } | |
376 | /* | |
377 | * Already had space in some data block. | |
378 | * Just read that one in. | |
379 | */ | |
380 | else { | |
381 | if ((error = | |
bbaaf538 | 382 | xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, use_block), |
1da177e4 LT |
383 | -1, &dbp, XFS_DATA_FORK))) { |
384 | xfs_da_brelse(tp, lbp); | |
385 | return error; | |
386 | } | |
387 | data = dbp->data; | |
388 | grown = 0; | |
389 | } | |
390 | xfs_dir2_data_check(dp, dbp); | |
391 | /* | |
392 | * Point to the biggest freespace in our data block. | |
393 | */ | |
394 | dup = (xfs_dir2_data_unused_t *) | |
70e73f59 | 395 | ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset)); |
ad354eb3 | 396 | ASSERT(be16_to_cpu(dup->length) >= length); |
1da177e4 LT |
397 | needscan = needlog = 0; |
398 | /* | |
399 | * Mark the initial part of our freespace in use for the new entry. | |
400 | */ | |
401 | xfs_dir2_data_use_free(tp, dbp, dup, | |
402 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length, | |
403 | &needlog, &needscan); | |
404 | /* | |
405 | * Initialize our new entry (at last). | |
406 | */ | |
407 | dep = (xfs_dir2_data_entry_t *)dup; | |
ff9901c1 | 408 | dep->inumber = cpu_to_be64(args->inumber); |
1da177e4 LT |
409 | dep->namelen = args->namelen; |
410 | memcpy(dep->name, args->name, dep->namelen); | |
bbaaf538 | 411 | tagp = xfs_dir2_data_entry_tag_p(dep); |
3d693c6e | 412 | *tagp = cpu_to_be16((char *)dep - (char *)data); |
1da177e4 LT |
413 | /* |
414 | * Need to scan fix up the bestfree table. | |
415 | */ | |
416 | if (needscan) | |
ef497f8a | 417 | xfs_dir2_data_freescan(mp, data, &needlog); |
1da177e4 LT |
418 | /* |
419 | * Need to log the data block's header. | |
420 | */ | |
421 | if (needlog) | |
422 | xfs_dir2_data_log_header(tp, dbp); | |
423 | xfs_dir2_data_log_entry(tp, dbp, dep); | |
424 | /* | |
425 | * If the bests table needs to be changed, do it. | |
426 | * Log the change unless we've already done that. | |
427 | */ | |
68b3a102 | 428 | if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) { |
70e73f59 | 429 | bestsp[use_block] = data->hdr.bestfree[0].length; |
1da177e4 LT |
430 | if (!grown) |
431 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); | |
432 | } | |
433 | /* | |
434 | * Now we need to make room to insert the leaf entry. | |
435 | * If there are no stale entries, we just insert a hole at index. | |
436 | */ | |
437 | if (!leaf->hdr.stale) { | |
438 | /* | |
439 | * lep is still good as the index leaf entry. | |
440 | */ | |
a818e5de | 441 | if (index < be16_to_cpu(leaf->hdr.count)) |
1da177e4 | 442 | memmove(lep + 1, lep, |
a818e5de | 443 | (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep)); |
1da177e4 LT |
444 | /* |
445 | * Record low and high logging indices for the leaf. | |
446 | */ | |
447 | lfloglow = index; | |
a818e5de | 448 | lfloghigh = be16_to_cpu(leaf->hdr.count); |
413d57c9 | 449 | be16_add_cpu(&leaf->hdr.count, 1); |
1da177e4 LT |
450 | } |
451 | /* | |
452 | * There are stale entries. | |
453 | * We will use one of them for the new entry. | |
454 | * It's probably not at the right location, so we'll have to | |
455 | * shift some up or down first. | |
456 | */ | |
457 | else { | |
458 | /* | |
459 | * If we didn't compact before, we need to find the nearest | |
460 | * stale entries before and after our insertion point. | |
461 | */ | |
462 | if (compact == 0) { | |
463 | /* | |
464 | * Find the first stale entry before the insertion | |
465 | * point, if any. | |
466 | */ | |
467 | for (lowstale = index - 1; | |
468 | lowstale >= 0 && | |
3c1f9c15 | 469 | be32_to_cpu(leaf->ents[lowstale].address) != |
1da177e4 LT |
470 | XFS_DIR2_NULL_DATAPTR; |
471 | lowstale--) | |
472 | continue; | |
473 | /* | |
474 | * Find the next stale entry at or after the insertion | |
475 | * point, if any. Stop if we go so far that the | |
476 | * lowstale entry would be better. | |
477 | */ | |
478 | for (highstale = index; | |
a818e5de | 479 | highstale < be16_to_cpu(leaf->hdr.count) && |
3c1f9c15 | 480 | be32_to_cpu(leaf->ents[highstale].address) != |
1da177e4 LT |
481 | XFS_DIR2_NULL_DATAPTR && |
482 | (lowstale < 0 || | |
483 | index - lowstale - 1 >= highstale - index); | |
484 | highstale++) | |
485 | continue; | |
486 | } | |
487 | /* | |
488 | * If the low one is better, use it. | |
489 | */ | |
490 | if (lowstale >= 0 && | |
a818e5de | 491 | (highstale == be16_to_cpu(leaf->hdr.count) || |
1da177e4 LT |
492 | index - lowstale - 1 < highstale - index)) { |
493 | ASSERT(index - lowstale - 1 >= 0); | |
3c1f9c15 | 494 | ASSERT(be32_to_cpu(leaf->ents[lowstale].address) == |
1da177e4 LT |
495 | XFS_DIR2_NULL_DATAPTR); |
496 | /* | |
497 | * Copy entries up to cover the stale entry | |
498 | * and make room for the new entry. | |
499 | */ | |
500 | if (index - lowstale - 1 > 0) | |
501 | memmove(&leaf->ents[lowstale], | |
502 | &leaf->ents[lowstale + 1], | |
503 | (index - lowstale - 1) * sizeof(*lep)); | |
504 | lep = &leaf->ents[index - 1]; | |
505 | lfloglow = MIN(lowstale, lfloglow); | |
506 | lfloghigh = MAX(index - 1, lfloghigh); | |
507 | } | |
508 | /* | |
509 | * The high one is better, so use that one. | |
510 | */ | |
511 | else { | |
512 | ASSERT(highstale - index >= 0); | |
3c1f9c15 | 513 | ASSERT(be32_to_cpu(leaf->ents[highstale].address) == |
1da177e4 LT |
514 | XFS_DIR2_NULL_DATAPTR); |
515 | /* | |
c41564b5 | 516 | * Copy entries down to cover the stale entry |
1da177e4 LT |
517 | * and make room for the new entry. |
518 | */ | |
519 | if (highstale - index > 0) | |
520 | memmove(&leaf->ents[index + 1], | |
521 | &leaf->ents[index], | |
522 | (highstale - index) * sizeof(*lep)); | |
523 | lep = &leaf->ents[index]; | |
524 | lfloglow = MIN(index, lfloglow); | |
525 | lfloghigh = MAX(highstale, lfloghigh); | |
526 | } | |
413d57c9 | 527 | be16_add_cpu(&leaf->hdr.stale, -1); |
1da177e4 LT |
528 | } |
529 | /* | |
530 | * Fill in the new leaf entry. | |
531 | */ | |
3c1f9c15 | 532 | lep->hashval = cpu_to_be32(args->hashval); |
bbaaf538 | 533 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block, |
3d693c6e | 534 | be16_to_cpu(*tagp))); |
1da177e4 LT |
535 | /* |
536 | * Log the leaf fields and give up the buffers. | |
537 | */ | |
538 | xfs_dir2_leaf_log_header(tp, lbp); | |
539 | xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh); | |
540 | xfs_dir2_leaf_check(dp, lbp); | |
541 | xfs_da_buf_done(lbp); | |
542 | xfs_dir2_data_check(dp, dbp); | |
543 | xfs_da_buf_done(dbp); | |
544 | return 0; | |
545 | } | |
546 | ||
547 | #ifdef DEBUG | |
548 | /* | |
549 | * Check the internal consistency of a leaf1 block. | |
550 | * Pop an assert if something is wrong. | |
551 | */ | |
3180e66d | 552 | STATIC void |
1da177e4 LT |
553 | xfs_dir2_leaf_check( |
554 | xfs_inode_t *dp, /* incore directory inode */ | |
555 | xfs_dabuf_t *bp) /* leaf's buffer */ | |
556 | { | |
557 | int i; /* leaf index */ | |
558 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
559 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ | |
560 | xfs_mount_t *mp; /* filesystem mount point */ | |
561 | int stale; /* count of stale leaves */ | |
562 | ||
563 | leaf = bp->data; | |
564 | mp = dp->i_mount; | |
89da0544 | 565 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
1da177e4 LT |
566 | /* |
567 | * This value is not restrictive enough. | |
568 | * Should factor in the size of the bests table as well. | |
569 | * We can deduce a value for that from di_size. | |
570 | */ | |
bbaaf538 CH |
571 | ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp)); |
572 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); | |
1da177e4 LT |
573 | /* |
574 | * Leaves and bests don't overlap. | |
575 | */ | |
a818e5de | 576 | ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <= |
bbaaf538 | 577 | (char *)xfs_dir2_leaf_bests_p(ltp)); |
1da177e4 LT |
578 | /* |
579 | * Check hash value order, count stale entries. | |
580 | */ | |
a818e5de NS |
581 | for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) { |
582 | if (i + 1 < be16_to_cpu(leaf->hdr.count)) | |
3c1f9c15 NS |
583 | ASSERT(be32_to_cpu(leaf->ents[i].hashval) <= |
584 | be32_to_cpu(leaf->ents[i + 1].hashval)); | |
585 | if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR) | |
1da177e4 LT |
586 | stale++; |
587 | } | |
a818e5de | 588 | ASSERT(be16_to_cpu(leaf->hdr.stale) == stale); |
1da177e4 LT |
589 | } |
590 | #endif /* DEBUG */ | |
591 | ||
592 | /* | |
593 | * Compact out any stale entries in the leaf. | |
594 | * Log the header and changed leaf entries, if any. | |
595 | */ | |
596 | void | |
597 | xfs_dir2_leaf_compact( | |
598 | xfs_da_args_t *args, /* operation arguments */ | |
599 | xfs_dabuf_t *bp) /* leaf buffer */ | |
600 | { | |
601 | int from; /* source leaf index */ | |
602 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
603 | int loglow; /* first leaf entry to log */ | |
604 | int to; /* target leaf index */ | |
605 | ||
606 | leaf = bp->data; | |
607 | if (!leaf->hdr.stale) { | |
608 | return; | |
609 | } | |
610 | /* | |
611 | * Compress out the stale entries in place. | |
612 | */ | |
a818e5de | 613 | for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) { |
3c1f9c15 | 614 | if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
615 | continue; |
616 | /* | |
617 | * Only actually copy the entries that are different. | |
618 | */ | |
619 | if (from > to) { | |
620 | if (loglow == -1) | |
621 | loglow = to; | |
622 | leaf->ents[to] = leaf->ents[from]; | |
623 | } | |
624 | to++; | |
625 | } | |
626 | /* | |
627 | * Update and log the header, log the leaf entries. | |
628 | */ | |
a818e5de | 629 | ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to); |
413d57c9 | 630 | be16_add_cpu(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale))); |
1da177e4 LT |
631 | leaf->hdr.stale = 0; |
632 | xfs_dir2_leaf_log_header(args->trans, bp); | |
633 | if (loglow != -1) | |
634 | xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1); | |
635 | } | |
636 | ||
637 | /* | |
638 | * Compact the leaf entries, removing stale ones. | |
639 | * Leave one stale entry behind - the one closest to our | |
640 | * insertion index - and the caller will shift that one to our insertion | |
641 | * point later. | |
642 | * Return new insertion index, where the remaining stale entry is, | |
643 | * and leaf logging indices. | |
644 | */ | |
645 | void | |
646 | xfs_dir2_leaf_compact_x1( | |
647 | xfs_dabuf_t *bp, /* leaf buffer */ | |
648 | int *indexp, /* insertion index */ | |
649 | int *lowstalep, /* out: stale entry before us */ | |
650 | int *highstalep, /* out: stale entry after us */ | |
651 | int *lowlogp, /* out: low log index */ | |
652 | int *highlogp) /* out: high log index */ | |
653 | { | |
654 | int from; /* source copy index */ | |
655 | int highstale; /* stale entry at/after index */ | |
656 | int index; /* insertion index */ | |
657 | int keepstale; /* source index of kept stale */ | |
658 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
659 | int lowstale; /* stale entry before index */ | |
660 | int newindex=0; /* new insertion index */ | |
661 | int to; /* destination copy index */ | |
662 | ||
663 | leaf = bp->data; | |
a818e5de | 664 | ASSERT(be16_to_cpu(leaf->hdr.stale) > 1); |
1da177e4 LT |
665 | index = *indexp; |
666 | /* | |
667 | * Find the first stale entry before our index, if any. | |
668 | */ | |
669 | for (lowstale = index - 1; | |
670 | lowstale >= 0 && | |
3c1f9c15 | 671 | be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR; |
1da177e4 LT |
672 | lowstale--) |
673 | continue; | |
674 | /* | |
675 | * Find the first stale entry at or after our index, if any. | |
676 | * Stop if the answer would be worse than lowstale. | |
677 | */ | |
678 | for (highstale = index; | |
a818e5de | 679 | highstale < be16_to_cpu(leaf->hdr.count) && |
3c1f9c15 | 680 | be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR && |
1da177e4 LT |
681 | (lowstale < 0 || index - lowstale > highstale - index); |
682 | highstale++) | |
683 | continue; | |
684 | /* | |
685 | * Pick the better of lowstale and highstale. | |
686 | */ | |
687 | if (lowstale >= 0 && | |
a818e5de | 688 | (highstale == be16_to_cpu(leaf->hdr.count) || |
1da177e4 LT |
689 | index - lowstale <= highstale - index)) |
690 | keepstale = lowstale; | |
691 | else | |
692 | keepstale = highstale; | |
693 | /* | |
694 | * Copy the entries in place, removing all the stale entries | |
695 | * except keepstale. | |
696 | */ | |
a818e5de | 697 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { |
1da177e4 LT |
698 | /* |
699 | * Notice the new value of index. | |
700 | */ | |
701 | if (index == from) | |
702 | newindex = to; | |
703 | if (from != keepstale && | |
3c1f9c15 | 704 | be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) { |
1da177e4 LT |
705 | if (from == to) |
706 | *lowlogp = to; | |
707 | continue; | |
708 | } | |
709 | /* | |
710 | * Record the new keepstale value for the insertion. | |
711 | */ | |
712 | if (from == keepstale) | |
713 | lowstale = highstale = to; | |
714 | /* | |
715 | * Copy only the entries that have moved. | |
716 | */ | |
717 | if (from > to) | |
718 | leaf->ents[to] = leaf->ents[from]; | |
719 | to++; | |
720 | } | |
721 | ASSERT(from > to); | |
722 | /* | |
723 | * If the insertion point was past the last entry, | |
724 | * set the new insertion point accordingly. | |
725 | */ | |
726 | if (index == from) | |
727 | newindex = to; | |
728 | *indexp = newindex; | |
729 | /* | |
730 | * Adjust the leaf header values. | |
731 | */ | |
413d57c9 | 732 | be16_add_cpu(&leaf->hdr.count, -(from - to)); |
a818e5de | 733 | leaf->hdr.stale = cpu_to_be16(1); |
1da177e4 LT |
734 | /* |
735 | * Remember the low/high stale value only in the "right" | |
736 | * direction. | |
737 | */ | |
738 | if (lowstale >= newindex) | |
739 | lowstale = -1; | |
740 | else | |
a818e5de NS |
741 | highstale = be16_to_cpu(leaf->hdr.count); |
742 | *highlogp = be16_to_cpu(leaf->hdr.count) - 1; | |
1da177e4 LT |
743 | *lowstalep = lowstale; |
744 | *highstalep = highstale; | |
745 | } | |
746 | ||
747 | /* | |
748 | * Getdents (readdir) for leaf and node directories. | |
749 | * This reads the data blocks only, so is the same for both forms. | |
750 | */ | |
751 | int /* error */ | |
752 | xfs_dir2_leaf_getdents( | |
1da177e4 | 753 | xfs_inode_t *dp, /* incore directory inode */ |
051e7cd4 CH |
754 | void *dirent, |
755 | size_t bufsize, | |
756 | xfs_off_t *offset, | |
757 | filldir_t filldir) | |
1da177e4 LT |
758 | { |
759 | xfs_dabuf_t *bp; /* data block buffer */ | |
760 | int byteoff; /* offset in current block */ | |
761 | xfs_dir2_db_t curdb; /* db for current block */ | |
762 | xfs_dir2_off_t curoff; /* current overall offset */ | |
763 | xfs_dir2_data_t *data; /* data block structure */ | |
764 | xfs_dir2_data_entry_t *dep; /* data entry */ | |
765 | xfs_dir2_data_unused_t *dup; /* unused entry */ | |
f6d75cbe | 766 | int error = 0; /* error return value */ |
1da177e4 LT |
767 | int i; /* temporary loop index */ |
768 | int j; /* temporary loop index */ | |
769 | int length; /* temporary length value */ | |
770 | xfs_bmbt_irec_t *map; /* map vector for blocks */ | |
771 | xfs_extlen_t map_blocks; /* number of fsbs in map */ | |
772 | xfs_dablk_t map_off; /* last mapped file offset */ | |
773 | int map_size; /* total entries in *map */ | |
774 | int map_valid; /* valid entries in *map */ | |
775 | xfs_mount_t *mp; /* filesystem mount point */ | |
776 | xfs_dir2_off_t newoff; /* new curoff after new blk */ | |
777 | int nmap; /* mappings to ask xfs_bmapi */ | |
f6d75cbe | 778 | char *ptr = NULL; /* pointer to current data */ |
1da177e4 LT |
779 | int ra_current; /* number of read-ahead blks */ |
780 | int ra_index; /* *map index for read-ahead */ | |
781 | int ra_offset; /* map entry offset for ra */ | |
782 | int ra_want; /* readahead count wanted */ | |
783 | ||
784 | /* | |
785 | * If the offset is at or past the largest allowed value, | |
051e7cd4 | 786 | * give up right away. |
1da177e4 | 787 | */ |
051e7cd4 | 788 | if (*offset >= XFS_DIR2_MAX_DATAPTR) |
1da177e4 | 789 | return 0; |
051e7cd4 | 790 | |
1da177e4 | 791 | mp = dp->i_mount; |
051e7cd4 | 792 | |
1da177e4 LT |
793 | /* |
794 | * Set up to bmap a number of blocks based on the caller's | |
795 | * buffer size, the directory block size, and the filesystem | |
796 | * block size. | |
797 | */ | |
051e7cd4 | 798 | map_size = howmany(bufsize + mp->m_dirblksize, mp->m_sb.sb_blocksize); |
1da177e4 LT |
799 | map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP); |
800 | map_valid = ra_index = ra_offset = ra_current = map_blocks = 0; | |
801 | bp = NULL; | |
051e7cd4 | 802 | |
1da177e4 LT |
803 | /* |
804 | * Inside the loop we keep the main offset value as a byte offset | |
805 | * in the directory file. | |
806 | */ | |
051e7cd4 CH |
807 | curoff = xfs_dir2_dataptr_to_byte(mp, *offset); |
808 | ||
1da177e4 LT |
809 | /* |
810 | * Force this conversion through db so we truncate the offset | |
811 | * down to get the start of the data block. | |
812 | */ | |
bbaaf538 | 813 | map_off = xfs_dir2_db_to_da(mp, xfs_dir2_byte_to_db(mp, curoff)); |
1da177e4 LT |
814 | /* |
815 | * Loop over directory entries until we reach the end offset. | |
816 | * Get more blocks and readahead as necessary. | |
817 | */ | |
818 | while (curoff < XFS_DIR2_LEAF_OFFSET) { | |
819 | /* | |
820 | * If we have no buffer, or we're off the end of the | |
821 | * current buffer, need to get another one. | |
822 | */ | |
823 | if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) { | |
824 | /* | |
825 | * If we have a buffer, we need to release it and | |
826 | * take it out of the mapping. | |
827 | */ | |
828 | if (bp) { | |
051e7cd4 | 829 | xfs_da_brelse(NULL, bp); |
1da177e4 LT |
830 | bp = NULL; |
831 | map_blocks -= mp->m_dirblkfsbs; | |
832 | /* | |
833 | * Loop to get rid of the extents for the | |
834 | * directory block. | |
835 | */ | |
836 | for (i = mp->m_dirblkfsbs; i > 0; ) { | |
837 | j = MIN((int)map->br_blockcount, i); | |
838 | map->br_blockcount -= j; | |
839 | map->br_startblock += j; | |
840 | map->br_startoff += j; | |
841 | /* | |
842 | * If mapping is done, pitch it from | |
843 | * the table. | |
844 | */ | |
845 | if (!map->br_blockcount && --map_valid) | |
846 | memmove(&map[0], &map[1], | |
847 | sizeof(map[0]) * | |
848 | map_valid); | |
849 | i -= j; | |
850 | } | |
851 | } | |
852 | /* | |
853 | * Recalculate the readahead blocks wanted. | |
854 | */ | |
051e7cd4 | 855 | ra_want = howmany(bufsize + mp->m_dirblksize, |
1da177e4 | 856 | mp->m_sb.sb_blocksize) - 1; |
051e7cd4 | 857 | |
1da177e4 LT |
858 | /* |
859 | * If we don't have as many as we want, and we haven't | |
860 | * run out of data blocks, get some more mappings. | |
861 | */ | |
862 | if (1 + ra_want > map_blocks && | |
863 | map_off < | |
bbaaf538 | 864 | xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) { |
1da177e4 LT |
865 | /* |
866 | * Get more bmaps, fill in after the ones | |
867 | * we already have in the table. | |
868 | */ | |
869 | nmap = map_size - map_valid; | |
051e7cd4 | 870 | error = xfs_bmapi(NULL, dp, |
1da177e4 | 871 | map_off, |
bbaaf538 | 872 | xfs_dir2_byte_to_da(mp, |
1da177e4 LT |
873 | XFS_DIR2_LEAF_OFFSET) - map_off, |
874 | XFS_BMAPI_METADATA, NULL, 0, | |
3e57ecf6 | 875 | &map[map_valid], &nmap, NULL, NULL); |
1da177e4 LT |
876 | /* |
877 | * Don't know if we should ignore this or | |
878 | * try to return an error. | |
879 | * The trouble with returning errors | |
880 | * is that readdir will just stop without | |
881 | * actually passing the error through. | |
882 | */ | |
883 | if (error) | |
884 | break; /* XXX */ | |
885 | /* | |
886 | * If we got all the mappings we asked for, | |
887 | * set the final map offset based on the | |
888 | * last bmap value received. | |
889 | * Otherwise, we've reached the end. | |
890 | */ | |
891 | if (nmap == map_size - map_valid) | |
892 | map_off = | |
893 | map[map_valid + nmap - 1].br_startoff + | |
894 | map[map_valid + nmap - 1].br_blockcount; | |
895 | else | |
896 | map_off = | |
bbaaf538 | 897 | xfs_dir2_byte_to_da(mp, |
1da177e4 LT |
898 | XFS_DIR2_LEAF_OFFSET); |
899 | /* | |
900 | * Look for holes in the mapping, and | |
901 | * eliminate them. Count up the valid blocks. | |
902 | */ | |
903 | for (i = map_valid; i < map_valid + nmap; ) { | |
904 | if (map[i].br_startblock == | |
905 | HOLESTARTBLOCK) { | |
906 | nmap--; | |
907 | length = map_valid + nmap - i; | |
908 | if (length) | |
909 | memmove(&map[i], | |
910 | &map[i + 1], | |
911 | sizeof(map[i]) * | |
912 | length); | |
913 | } else { | |
914 | map_blocks += | |
915 | map[i].br_blockcount; | |
916 | i++; | |
917 | } | |
918 | } | |
919 | map_valid += nmap; | |
920 | } | |
921 | /* | |
922 | * No valid mappings, so no more data blocks. | |
923 | */ | |
924 | if (!map_valid) { | |
bbaaf538 | 925 | curoff = xfs_dir2_da_to_byte(mp, map_off); |
1da177e4 LT |
926 | break; |
927 | } | |
928 | /* | |
929 | * Read the directory block starting at the first | |
930 | * mapping. | |
931 | */ | |
bbaaf538 | 932 | curdb = xfs_dir2_da_to_db(mp, map->br_startoff); |
051e7cd4 | 933 | error = xfs_da_read_buf(NULL, dp, map->br_startoff, |
1da177e4 LT |
934 | map->br_blockcount >= mp->m_dirblkfsbs ? |
935 | XFS_FSB_TO_DADDR(mp, map->br_startblock) : | |
936 | -1, | |
937 | &bp, XFS_DATA_FORK); | |
938 | /* | |
939 | * Should just skip over the data block instead | |
940 | * of giving up. | |
941 | */ | |
942 | if (error) | |
943 | break; /* XXX */ | |
944 | /* | |
945 | * Adjust the current amount of read-ahead: we just | |
946 | * read a block that was previously ra. | |
947 | */ | |
948 | if (ra_current) | |
949 | ra_current -= mp->m_dirblkfsbs; | |
950 | /* | |
951 | * Do we need more readahead? | |
952 | */ | |
953 | for (ra_index = ra_offset = i = 0; | |
954 | ra_want > ra_current && i < map_blocks; | |
955 | i += mp->m_dirblkfsbs) { | |
956 | ASSERT(ra_index < map_valid); | |
957 | /* | |
958 | * Read-ahead a contiguous directory block. | |
959 | */ | |
960 | if (i > ra_current && | |
961 | map[ra_index].br_blockcount >= | |
962 | mp->m_dirblkfsbs) { | |
963 | xfs_baread(mp->m_ddev_targp, | |
964 | XFS_FSB_TO_DADDR(mp, | |
965 | map[ra_index].br_startblock + | |
966 | ra_offset), | |
967 | (int)BTOBB(mp->m_dirblksize)); | |
968 | ra_current = i; | |
969 | } | |
970 | /* | |
971 | * Read-ahead a non-contiguous directory block. | |
972 | * This doesn't use our mapping, but this | |
973 | * is a very rare case. | |
974 | */ | |
975 | else if (i > ra_current) { | |
051e7cd4 | 976 | (void)xfs_da_reada_buf(NULL, dp, |
1da177e4 LT |
977 | map[ra_index].br_startoff + |
978 | ra_offset, XFS_DATA_FORK); | |
979 | ra_current = i; | |
980 | } | |
981 | /* | |
982 | * Advance offset through the mapping table. | |
983 | */ | |
984 | for (j = 0; j < mp->m_dirblkfsbs; j++) { | |
985 | /* | |
986 | * The rest of this extent but not | |
987 | * more than a dir block. | |
988 | */ | |
989 | length = MIN(mp->m_dirblkfsbs, | |
990 | (int)(map[ra_index].br_blockcount - | |
991 | ra_offset)); | |
992 | j += length; | |
993 | ra_offset += length; | |
994 | /* | |
995 | * Advance to the next mapping if | |
996 | * this one is used up. | |
997 | */ | |
998 | if (ra_offset == | |
999 | map[ra_index].br_blockcount) { | |
1000 | ra_offset = 0; | |
1001 | ra_index++; | |
1002 | } | |
1003 | } | |
1004 | } | |
1005 | /* | |
1006 | * Having done a read, we need to set a new offset. | |
1007 | */ | |
bbaaf538 | 1008 | newoff = xfs_dir2_db_off_to_byte(mp, curdb, 0); |
1da177e4 LT |
1009 | /* |
1010 | * Start of the current block. | |
1011 | */ | |
1012 | if (curoff < newoff) | |
1013 | curoff = newoff; | |
1014 | /* | |
1015 | * Make sure we're in the right block. | |
1016 | */ | |
1017 | else if (curoff > newoff) | |
bbaaf538 | 1018 | ASSERT(xfs_dir2_byte_to_db(mp, curoff) == |
1da177e4 LT |
1019 | curdb); |
1020 | data = bp->data; | |
1021 | xfs_dir2_data_check(dp, bp); | |
1022 | /* | |
1023 | * Find our position in the block. | |
1024 | */ | |
1025 | ptr = (char *)&data->u; | |
bbaaf538 | 1026 | byteoff = xfs_dir2_byte_to_off(mp, curoff); |
1da177e4 LT |
1027 | /* |
1028 | * Skip past the header. | |
1029 | */ | |
1030 | if (byteoff == 0) | |
1031 | curoff += (uint)sizeof(data->hdr); | |
1032 | /* | |
1033 | * Skip past entries until we reach our offset. | |
1034 | */ | |
1035 | else { | |
1036 | while ((char *)ptr - (char *)data < byteoff) { | |
1037 | dup = (xfs_dir2_data_unused_t *)ptr; | |
1038 | ||
ad354eb3 | 1039 | if (be16_to_cpu(dup->freetag) |
1da177e4 LT |
1040 | == XFS_DIR2_DATA_FREE_TAG) { |
1041 | ||
ad354eb3 | 1042 | length = be16_to_cpu(dup->length); |
1da177e4 LT |
1043 | ptr += length; |
1044 | continue; | |
1045 | } | |
1046 | dep = (xfs_dir2_data_entry_t *)ptr; | |
1047 | length = | |
bbaaf538 | 1048 | xfs_dir2_data_entsize(dep->namelen); |
1da177e4 LT |
1049 | ptr += length; |
1050 | } | |
1051 | /* | |
1052 | * Now set our real offset. | |
1053 | */ | |
1054 | curoff = | |
bbaaf538 CH |
1055 | xfs_dir2_db_off_to_byte(mp, |
1056 | xfs_dir2_byte_to_db(mp, curoff), | |
1da177e4 LT |
1057 | (char *)ptr - (char *)data); |
1058 | if (ptr >= (char *)data + mp->m_dirblksize) { | |
1059 | continue; | |
1060 | } | |
1061 | } | |
1062 | } | |
1063 | /* | |
1064 | * We have a pointer to an entry. | |
1065 | * Is it a live one? | |
1066 | */ | |
1067 | dup = (xfs_dir2_data_unused_t *)ptr; | |
1068 | /* | |
1069 | * No, it's unused, skip over it. | |
1070 | */ | |
ad354eb3 NS |
1071 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
1072 | length = be16_to_cpu(dup->length); | |
1da177e4 LT |
1073 | ptr += length; |
1074 | curoff += length; | |
1075 | continue; | |
1076 | } | |
1077 | ||
1da177e4 | 1078 | dep = (xfs_dir2_data_entry_t *)ptr; |
051e7cd4 | 1079 | length = xfs_dir2_data_entsize(dep->namelen); |
1da177e4 | 1080 | |
051e7cd4 | 1081 | if (filldir(dirent, dep->name, dep->namelen, |
15440319 | 1082 | xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff, |
a19d9f88 | 1083 | be64_to_cpu(dep->inumber), DT_UNKNOWN)) |
1da177e4 | 1084 | break; |
051e7cd4 | 1085 | |
1da177e4 LT |
1086 | /* |
1087 | * Advance to next entry in the block. | |
1088 | */ | |
1089 | ptr += length; | |
1090 | curoff += length; | |
051e7cd4 | 1091 | bufsize -= length; |
1da177e4 LT |
1092 | } |
1093 | ||
1094 | /* | |
1095 | * All done. Set output offset value to current offset. | |
1096 | */ | |
bbaaf538 | 1097 | if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR)) |
15440319 | 1098 | *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff; |
1da177e4 | 1099 | else |
15440319 | 1100 | *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff; |
f0e2d93c | 1101 | kmem_free(map); |
1da177e4 | 1102 | if (bp) |
051e7cd4 | 1103 | xfs_da_brelse(NULL, bp); |
1da177e4 LT |
1104 | return error; |
1105 | } | |
1106 | ||
1107 | /* | |
1108 | * Initialize a new leaf block, leaf1 or leafn magic accepted. | |
1109 | */ | |
1110 | int | |
1111 | xfs_dir2_leaf_init( | |
1112 | xfs_da_args_t *args, /* operation arguments */ | |
1113 | xfs_dir2_db_t bno, /* directory block number */ | |
1114 | xfs_dabuf_t **bpp, /* out: leaf buffer */ | |
1115 | int magic) /* magic number for block */ | |
1116 | { | |
1117 | xfs_dabuf_t *bp; /* leaf buffer */ | |
1118 | xfs_inode_t *dp; /* incore directory inode */ | |
1119 | int error; /* error return code */ | |
1120 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1121 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1122 | xfs_mount_t *mp; /* filesystem mount point */ | |
1123 | xfs_trans_t *tp; /* transaction pointer */ | |
1124 | ||
1125 | dp = args->dp; | |
1126 | ASSERT(dp != NULL); | |
1127 | tp = args->trans; | |
1128 | mp = dp->i_mount; | |
1129 | ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) && | |
1130 | bno < XFS_DIR2_FREE_FIRSTDB(mp)); | |
1131 | /* | |
1132 | * Get the buffer for the block. | |
1133 | */ | |
bbaaf538 | 1134 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp, |
1da177e4 LT |
1135 | XFS_DATA_FORK); |
1136 | if (error) { | |
1137 | return error; | |
1138 | } | |
1139 | ASSERT(bp != NULL); | |
1140 | leaf = bp->data; | |
1141 | /* | |
1142 | * Initialize the header. | |
1143 | */ | |
89da0544 | 1144 | leaf->hdr.info.magic = cpu_to_be16(magic); |
1da177e4 LT |
1145 | leaf->hdr.info.forw = 0; |
1146 | leaf->hdr.info.back = 0; | |
1147 | leaf->hdr.count = 0; | |
1148 | leaf->hdr.stale = 0; | |
1149 | xfs_dir2_leaf_log_header(tp, bp); | |
1150 | /* | |
1151 | * If it's a leaf-format directory initialize the tail. | |
1152 | * In this case our caller has the real bests table to copy into | |
1153 | * the block. | |
1154 | */ | |
1155 | if (magic == XFS_DIR2_LEAF1_MAGIC) { | |
bbaaf538 | 1156 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
1da177e4 LT |
1157 | ltp->bestcount = 0; |
1158 | xfs_dir2_leaf_log_tail(tp, bp); | |
1159 | } | |
1160 | *bpp = bp; | |
1161 | return 0; | |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Log the bests entries indicated from a leaf1 block. | |
1166 | */ | |
ba0f32d4 | 1167 | static void |
1da177e4 LT |
1168 | xfs_dir2_leaf_log_bests( |
1169 | xfs_trans_t *tp, /* transaction pointer */ | |
1170 | xfs_dabuf_t *bp, /* leaf buffer */ | |
1171 | int first, /* first entry to log */ | |
1172 | int last) /* last entry to log */ | |
1173 | { | |
68b3a102 NS |
1174 | __be16 *firstb; /* pointer to first entry */ |
1175 | __be16 *lastb; /* pointer to last entry */ | |
1da177e4 LT |
1176 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
1177 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1178 | ||
1179 | leaf = bp->data; | |
89da0544 | 1180 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
bbaaf538 CH |
1181 | ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf); |
1182 | firstb = xfs_dir2_leaf_bests_p(ltp) + first; | |
1183 | lastb = xfs_dir2_leaf_bests_p(ltp) + last; | |
1da177e4 LT |
1184 | xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf), |
1185 | (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); | |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | * Log the leaf entries indicated from a leaf1 or leafn block. | |
1190 | */ | |
1191 | void | |
1192 | xfs_dir2_leaf_log_ents( | |
1193 | xfs_trans_t *tp, /* transaction pointer */ | |
1194 | xfs_dabuf_t *bp, /* leaf buffer */ | |
1195 | int first, /* first entry to log */ | |
1196 | int last) /* last entry to log */ | |
1197 | { | |
1198 | xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ | |
1199 | xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ | |
1200 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1201 | ||
1202 | leaf = bp->data; | |
89da0544 NS |
1203 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC || |
1204 | be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); | |
1da177e4 LT |
1205 | firstlep = &leaf->ents[first]; |
1206 | lastlep = &leaf->ents[last]; | |
1207 | xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf), | |
1208 | (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); | |
1209 | } | |
1210 | ||
1211 | /* | |
1212 | * Log the header of the leaf1 or leafn block. | |
1213 | */ | |
1214 | void | |
1215 | xfs_dir2_leaf_log_header( | |
1216 | xfs_trans_t *tp, /* transaction pointer */ | |
1217 | xfs_dabuf_t *bp) /* leaf buffer */ | |
1218 | { | |
1219 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1220 | ||
1221 | leaf = bp->data; | |
89da0544 NS |
1222 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC || |
1223 | be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); | |
1da177e4 LT |
1224 | xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf), |
1225 | (uint)(sizeof(leaf->hdr) - 1)); | |
1226 | } | |
1227 | ||
1228 | /* | |
1229 | * Log the tail of the leaf1 block. | |
1230 | */ | |
ba0f32d4 | 1231 | STATIC void |
1da177e4 LT |
1232 | xfs_dir2_leaf_log_tail( |
1233 | xfs_trans_t *tp, /* transaction pointer */ | |
1234 | xfs_dabuf_t *bp) /* leaf buffer */ | |
1235 | { | |
1236 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1237 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1238 | xfs_mount_t *mp; /* filesystem mount point */ | |
1239 | ||
1240 | mp = tp->t_mountp; | |
1241 | leaf = bp->data; | |
89da0544 | 1242 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
bbaaf538 | 1243 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
1da177e4 LT |
1244 | xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf), |
1245 | (uint)(mp->m_dirblksize - 1)); | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * Look up the entry referred to by args in the leaf format directory. | |
1250 | * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which | |
1251 | * is also used by the node-format code. | |
1252 | */ | |
1253 | int | |
1254 | xfs_dir2_leaf_lookup( | |
1255 | xfs_da_args_t *args) /* operation arguments */ | |
1256 | { | |
1257 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1258 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
1259 | xfs_inode_t *dp; /* incore directory inode */ | |
1260 | int error; /* error return code */ | |
1261 | int index; /* found entry index */ | |
1262 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1263 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1264 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1265 | xfs_trans_t *tp; /* transaction pointer */ | |
1266 | ||
1267 | xfs_dir2_trace_args("leaf_lookup", args); | |
1268 | /* | |
1269 | * Look up name in the leaf block, returning both buffers and index. | |
1270 | */ | |
1271 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { | |
1272 | return error; | |
1273 | } | |
1274 | tp = args->trans; | |
1275 | dp = args->dp; | |
1276 | xfs_dir2_leaf_check(dp, lbp); | |
1277 | leaf = lbp->data; | |
1278 | /* | |
1279 | * Get to the leaf entry and contained data entry address. | |
1280 | */ | |
1281 | lep = &leaf->ents[index]; | |
1282 | /* | |
1283 | * Point to the data entry. | |
1284 | */ | |
1285 | dep = (xfs_dir2_data_entry_t *) | |
1286 | ((char *)dbp->data + | |
bbaaf538 | 1287 | xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address))); |
1da177e4 | 1288 | /* |
384f3ced | 1289 | * Return the found inode number & CI name if appropriate |
1da177e4 | 1290 | */ |
ff9901c1 | 1291 | args->inumber = be64_to_cpu(dep->inumber); |
384f3ced | 1292 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
1da177e4 LT |
1293 | xfs_da_brelse(tp, dbp); |
1294 | xfs_da_brelse(tp, lbp); | |
384f3ced | 1295 | return XFS_ERROR(error); |
1da177e4 LT |
1296 | } |
1297 | ||
1298 | /* | |
1299 | * Look up name/hash in the leaf block. | |
1300 | * Fill in indexp with the found index, and dbpp with the data buffer. | |
1301 | * If not found dbpp will be NULL, and ENOENT comes back. | |
1302 | * lbpp will always be filled in with the leaf buffer unless there's an error. | |
1303 | */ | |
1304 | static int /* error */ | |
1305 | xfs_dir2_leaf_lookup_int( | |
1306 | xfs_da_args_t *args, /* operation arguments */ | |
1307 | xfs_dabuf_t **lbpp, /* out: leaf buffer */ | |
1308 | int *indexp, /* out: index in leaf block */ | |
1309 | xfs_dabuf_t **dbpp) /* out: data buffer */ | |
1310 | { | |
07fe4dd4 BN |
1311 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
1312 | xfs_dabuf_t *dbp = NULL; /* data buffer */ | |
1da177e4 LT |
1313 | xfs_dir2_data_entry_t *dep; /* data entry */ |
1314 | xfs_inode_t *dp; /* incore directory inode */ | |
1315 | int error; /* error return code */ | |
1316 | int index; /* index in leaf block */ | |
1317 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1318 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1319 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1320 | xfs_mount_t *mp; /* filesystem mount point */ | |
1321 | xfs_dir2_db_t newdb; /* new data block number */ | |
1322 | xfs_trans_t *tp; /* transaction pointer */ | |
07fe4dd4 | 1323 | xfs_dir2_db_t cidb = -1; /* case match data block no. */ |
5163f95a | 1324 | enum xfs_dacmp cmp; /* name compare result */ |
1da177e4 LT |
1325 | |
1326 | dp = args->dp; | |
1327 | tp = args->trans; | |
1328 | mp = dp->i_mount; | |
1329 | /* | |
1330 | * Read the leaf block into the buffer. | |
1331 | */ | |
07fe4dd4 BN |
1332 | error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, |
1333 | XFS_DATA_FORK); | |
1334 | if (error) | |
1da177e4 | 1335 | return error; |
1da177e4 LT |
1336 | *lbpp = lbp; |
1337 | leaf = lbp->data; | |
1338 | xfs_dir2_leaf_check(dp, lbp); | |
1339 | /* | |
1340 | * Look for the first leaf entry with our hash value. | |
1341 | */ | |
1342 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
1343 | /* | |
1344 | * Loop over all the entries with the right hash value | |
1345 | * looking to match the name. | |
1346 | */ | |
07fe4dd4 | 1347 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
5163f95a BN |
1348 | be32_to_cpu(lep->hashval) == args->hashval; |
1349 | lep++, index++) { | |
1da177e4 LT |
1350 | /* |
1351 | * Skip over stale leaf entries. | |
1352 | */ | |
3c1f9c15 | 1353 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
1354 | continue; |
1355 | /* | |
1356 | * Get the new data block number. | |
1357 | */ | |
bbaaf538 | 1358 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
1da177e4 LT |
1359 | /* |
1360 | * If it's not the same as the old data block number, | |
1361 | * need to pitch the old one and read the new one. | |
1362 | */ | |
1363 | if (newdb != curdb) { | |
07fe4dd4 | 1364 | if (dbp) |
1da177e4 | 1365 | xfs_da_brelse(tp, dbp); |
5163f95a BN |
1366 | error = xfs_da_read_buf(tp, dp, |
1367 | xfs_dir2_db_to_da(mp, newdb), | |
1368 | -1, &dbp, XFS_DATA_FORK); | |
1369 | if (error) { | |
1da177e4 LT |
1370 | xfs_da_brelse(tp, lbp); |
1371 | return error; | |
1372 | } | |
1373 | xfs_dir2_data_check(dp, dbp); | |
1374 | curdb = newdb; | |
1375 | } | |
1376 | /* | |
1377 | * Point to the data entry. | |
1378 | */ | |
5163f95a BN |
1379 | dep = (xfs_dir2_data_entry_t *)((char *)dbp->data + |
1380 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address))); | |
1da177e4 | 1381 | /* |
5163f95a BN |
1382 | * Compare name and if it's an exact match, return the index |
1383 | * and buffer. If it's the first case-insensitive match, store | |
1384 | * the index and buffer and continue looking for an exact match. | |
1da177e4 | 1385 | */ |
5163f95a BN |
1386 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
1387 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | |
1388 | args->cmpresult = cmp; | |
1da177e4 | 1389 | *indexp = index; |
07fe4dd4 | 1390 | /* case exact match: return the current buffer. */ |
5163f95a | 1391 | if (cmp == XFS_CMP_EXACT) { |
5163f95a BN |
1392 | *dbpp = dbp; |
1393 | return 0; | |
1394 | } | |
07fe4dd4 | 1395 | cidb = curdb; |
1da177e4 LT |
1396 | } |
1397 | } | |
6a178100 | 1398 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
5163f95a | 1399 | /* |
07fe4dd4 BN |
1400 | * Here, we can only be doing a lookup (not a rename or remove). |
1401 | * If a case-insensitive match was found earlier, re-read the | |
1402 | * appropriate data block if required and return it. | |
5163f95a BN |
1403 | */ |
1404 | if (args->cmpresult == XFS_CMP_CASE) { | |
07fe4dd4 BN |
1405 | ASSERT(cidb != -1); |
1406 | if (cidb != curdb) { | |
5163f95a | 1407 | xfs_da_brelse(tp, dbp); |
07fe4dd4 BN |
1408 | error = xfs_da_read_buf(tp, dp, |
1409 | xfs_dir2_db_to_da(mp, cidb), | |
1410 | -1, &dbp, XFS_DATA_FORK); | |
1411 | if (error) { | |
1412 | xfs_da_brelse(tp, lbp); | |
1413 | return error; | |
1414 | } | |
1415 | } | |
1416 | *dbpp = dbp; | |
5163f95a BN |
1417 | return 0; |
1418 | } | |
1da177e4 LT |
1419 | /* |
1420 | * No match found, return ENOENT. | |
1421 | */ | |
07fe4dd4 | 1422 | ASSERT(cidb == -1); |
1da177e4 LT |
1423 | if (dbp) |
1424 | xfs_da_brelse(tp, dbp); | |
1425 | xfs_da_brelse(tp, lbp); | |
1426 | return XFS_ERROR(ENOENT); | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * Remove an entry from a leaf format directory. | |
1431 | */ | |
1432 | int /* error */ | |
1433 | xfs_dir2_leaf_removename( | |
1434 | xfs_da_args_t *args) /* operation arguments */ | |
1435 | { | |
68b3a102 | 1436 | __be16 *bestsp; /* leaf block best freespace */ |
1da177e4 LT |
1437 | xfs_dir2_data_t *data; /* data block structure */ |
1438 | xfs_dir2_db_t db; /* data block number */ | |
1439 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1440 | xfs_dir2_data_entry_t *dep; /* data entry structure */ | |
1441 | xfs_inode_t *dp; /* incore directory inode */ | |
1442 | int error; /* error return code */ | |
1443 | xfs_dir2_db_t i; /* temporary data block # */ | |
1444 | int index; /* index into leaf entries */ | |
1445 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1446 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1447 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1448 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1449 | xfs_mount_t *mp; /* filesystem mount point */ | |
1450 | int needlog; /* need to log data header */ | |
1451 | int needscan; /* need to rescan data frees */ | |
1452 | xfs_dir2_data_off_t oldbest; /* old value of best free */ | |
1453 | xfs_trans_t *tp; /* transaction pointer */ | |
1454 | ||
1455 | xfs_dir2_trace_args("leaf_removename", args); | |
1456 | /* | |
1457 | * Lookup the leaf entry, get the leaf and data blocks read in. | |
1458 | */ | |
1459 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { | |
1460 | return error; | |
1461 | } | |
1462 | dp = args->dp; | |
1463 | tp = args->trans; | |
1464 | mp = dp->i_mount; | |
1465 | leaf = lbp->data; | |
1466 | data = dbp->data; | |
1467 | xfs_dir2_data_check(dp, dbp); | |
1468 | /* | |
1469 | * Point to the leaf entry, use that to point to the data entry. | |
1470 | */ | |
1471 | lep = &leaf->ents[index]; | |
bbaaf538 | 1472 | db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
1da177e4 | 1473 | dep = (xfs_dir2_data_entry_t *) |
bbaaf538 | 1474 | ((char *)data + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address))); |
1da177e4 | 1475 | needscan = needlog = 0; |
70e73f59 | 1476 | oldbest = be16_to_cpu(data->hdr.bestfree[0].length); |
bbaaf538 CH |
1477 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
1478 | bestsp = xfs_dir2_leaf_bests_p(ltp); | |
68b3a102 | 1479 | ASSERT(be16_to_cpu(bestsp[db]) == oldbest); |
1da177e4 LT |
1480 | /* |
1481 | * Mark the former data entry unused. | |
1482 | */ | |
1483 | xfs_dir2_data_make_free(tp, dbp, | |
1484 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)data), | |
bbaaf538 | 1485 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
1da177e4 LT |
1486 | /* |
1487 | * We just mark the leaf entry stale by putting a null in it. | |
1488 | */ | |
413d57c9 | 1489 | be16_add_cpu(&leaf->hdr.stale, 1); |
1da177e4 | 1490 | xfs_dir2_leaf_log_header(tp, lbp); |
3c1f9c15 | 1491 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
1da177e4 LT |
1492 | xfs_dir2_leaf_log_ents(tp, lbp, index, index); |
1493 | /* | |
1494 | * Scan the freespace in the data block again if necessary, | |
1495 | * log the data block header if necessary. | |
1496 | */ | |
1497 | if (needscan) | |
ef497f8a | 1498 | xfs_dir2_data_freescan(mp, data, &needlog); |
1da177e4 LT |
1499 | if (needlog) |
1500 | xfs_dir2_data_log_header(tp, dbp); | |
1501 | /* | |
1502 | * If the longest freespace in the data block has changed, | |
1503 | * put the new value in the bests table and log that. | |
1504 | */ | |
70e73f59 NS |
1505 | if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) { |
1506 | bestsp[db] = data->hdr.bestfree[0].length; | |
1da177e4 LT |
1507 | xfs_dir2_leaf_log_bests(tp, lbp, db, db); |
1508 | } | |
1509 | xfs_dir2_data_check(dp, dbp); | |
1510 | /* | |
1511 | * If the data block is now empty then get rid of the data block. | |
1512 | */ | |
70e73f59 | 1513 | if (be16_to_cpu(data->hdr.bestfree[0].length) == |
1da177e4 LT |
1514 | mp->m_dirblksize - (uint)sizeof(data->hdr)) { |
1515 | ASSERT(db != mp->m_dirdatablk); | |
1516 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { | |
1517 | /* | |
1518 | * Nope, can't get rid of it because it caused | |
1519 | * allocation of a bmap btree block to do so. | |
1520 | * Just go on, returning success, leaving the | |
1521 | * empty block in place. | |
1522 | */ | |
1523 | if (error == ENOSPC && args->total == 0) { | |
1524 | xfs_da_buf_done(dbp); | |
1525 | error = 0; | |
1526 | } | |
1527 | xfs_dir2_leaf_check(dp, lbp); | |
1528 | xfs_da_buf_done(lbp); | |
1529 | return error; | |
1530 | } | |
1531 | dbp = NULL; | |
1532 | /* | |
1533 | * If this is the last data block then compact the | |
1534 | * bests table by getting rid of entries. | |
1535 | */ | |
afbcb3f9 | 1536 | if (db == be32_to_cpu(ltp->bestcount) - 1) { |
1da177e4 LT |
1537 | /* |
1538 | * Look for the last active entry (i). | |
1539 | */ | |
1540 | for (i = db - 1; i > 0; i--) { | |
68b3a102 | 1541 | if (be16_to_cpu(bestsp[i]) != NULLDATAOFF) |
1da177e4 LT |
1542 | break; |
1543 | } | |
1544 | /* | |
1545 | * Copy the table down so inactive entries at the | |
1546 | * end are removed. | |
1547 | */ | |
1548 | memmove(&bestsp[db - i], bestsp, | |
afbcb3f9 | 1549 | (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp)); |
413d57c9 | 1550 | be32_add_cpu(<p->bestcount, -(db - i)); |
1da177e4 | 1551 | xfs_dir2_leaf_log_tail(tp, lbp); |
afbcb3f9 | 1552 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
1da177e4 | 1553 | } else |
68b3a102 | 1554 | bestsp[db] = cpu_to_be16(NULLDATAOFF); |
1da177e4 LT |
1555 | } |
1556 | /* | |
1557 | * If the data block was not the first one, drop it. | |
1558 | */ | |
1559 | else if (db != mp->m_dirdatablk && dbp != NULL) { | |
1560 | xfs_da_buf_done(dbp); | |
1561 | dbp = NULL; | |
1562 | } | |
1563 | xfs_dir2_leaf_check(dp, lbp); | |
1564 | /* | |
1565 | * See if we can convert to block form. | |
1566 | */ | |
1567 | return xfs_dir2_leaf_to_block(args, lbp, dbp); | |
1568 | } | |
1569 | ||
1570 | /* | |
1571 | * Replace the inode number in a leaf format directory entry. | |
1572 | */ | |
1573 | int /* error */ | |
1574 | xfs_dir2_leaf_replace( | |
1575 | xfs_da_args_t *args) /* operation arguments */ | |
1576 | { | |
1577 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1578 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
1579 | xfs_inode_t *dp; /* incore directory inode */ | |
1580 | int error; /* error return code */ | |
1581 | int index; /* index of leaf entry */ | |
1582 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1583 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1584 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1585 | xfs_trans_t *tp; /* transaction pointer */ | |
1586 | ||
1587 | xfs_dir2_trace_args("leaf_replace", args); | |
1588 | /* | |
1589 | * Look up the entry. | |
1590 | */ | |
1591 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { | |
1592 | return error; | |
1593 | } | |
1594 | dp = args->dp; | |
1595 | leaf = lbp->data; | |
1596 | /* | |
1597 | * Point to the leaf entry, get data address from it. | |
1598 | */ | |
1599 | lep = &leaf->ents[index]; | |
1600 | /* | |
1601 | * Point to the data entry. | |
1602 | */ | |
1603 | dep = (xfs_dir2_data_entry_t *) | |
1604 | ((char *)dbp->data + | |
bbaaf538 | 1605 | xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address))); |
ff9901c1 | 1606 | ASSERT(args->inumber != be64_to_cpu(dep->inumber)); |
1da177e4 LT |
1607 | /* |
1608 | * Put the new inode number in, log it. | |
1609 | */ | |
ff9901c1 | 1610 | dep->inumber = cpu_to_be64(args->inumber); |
1da177e4 LT |
1611 | tp = args->trans; |
1612 | xfs_dir2_data_log_entry(tp, dbp, dep); | |
1613 | xfs_da_buf_done(dbp); | |
1614 | xfs_dir2_leaf_check(dp, lbp); | |
1615 | xfs_da_brelse(tp, lbp); | |
1616 | return 0; | |
1617 | } | |
1618 | ||
1619 | /* | |
1620 | * Return index in the leaf block (lbp) which is either the first | |
1621 | * one with this hash value, or if there are none, the insert point | |
1622 | * for that hash value. | |
1623 | */ | |
1624 | int /* index value */ | |
1625 | xfs_dir2_leaf_search_hash( | |
1626 | xfs_da_args_t *args, /* operation arguments */ | |
1627 | xfs_dabuf_t *lbp) /* leaf buffer */ | |
1628 | { | |
1629 | xfs_dahash_t hash=0; /* hash from this entry */ | |
1630 | xfs_dahash_t hashwant; /* hash value looking for */ | |
1631 | int high; /* high leaf index */ | |
1632 | int low; /* low leaf index */ | |
1633 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1634 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1635 | int mid=0; /* current leaf index */ | |
1636 | ||
1637 | leaf = lbp->data; | |
1638 | #ifndef __KERNEL__ | |
1639 | if (!leaf->hdr.count) | |
1640 | return 0; | |
1641 | #endif | |
1642 | /* | |
1643 | * Note, the table cannot be empty, so we have to go through the loop. | |
1644 | * Binary search the leaf entries looking for our hash value. | |
1645 | */ | |
a818e5de | 1646 | for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1, |
1da177e4 LT |
1647 | hashwant = args->hashval; |
1648 | low <= high; ) { | |
1649 | mid = (low + high) >> 1; | |
3c1f9c15 | 1650 | if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant) |
1da177e4 LT |
1651 | break; |
1652 | if (hash < hashwant) | |
1653 | low = mid + 1; | |
1654 | else | |
1655 | high = mid - 1; | |
1656 | } | |
1657 | /* | |
1658 | * Found one, back up through all the equal hash values. | |
1659 | */ | |
1660 | if (hash == hashwant) { | |
3c1f9c15 | 1661 | while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) { |
1da177e4 LT |
1662 | mid--; |
1663 | } | |
1664 | } | |
1665 | /* | |
1666 | * Need to point to an entry higher than ours. | |
1667 | */ | |
1668 | else if (hash < hashwant) | |
1669 | mid++; | |
1670 | return mid; | |
1671 | } | |
1672 | ||
1673 | /* | |
1674 | * Trim off a trailing data block. We know it's empty since the leaf | |
1675 | * freespace table says so. | |
1676 | */ | |
1677 | int /* error */ | |
1678 | xfs_dir2_leaf_trim_data( | |
1679 | xfs_da_args_t *args, /* operation arguments */ | |
1680 | xfs_dabuf_t *lbp, /* leaf buffer */ | |
1681 | xfs_dir2_db_t db) /* data block number */ | |
1682 | { | |
68b3a102 | 1683 | __be16 *bestsp; /* leaf bests table */ |
1da177e4 LT |
1684 | #ifdef DEBUG |
1685 | xfs_dir2_data_t *data; /* data block structure */ | |
1686 | #endif | |
1687 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1688 | xfs_inode_t *dp; /* incore directory inode */ | |
1689 | int error; /* error return value */ | |
1690 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1691 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1692 | xfs_mount_t *mp; /* filesystem mount point */ | |
1693 | xfs_trans_t *tp; /* transaction pointer */ | |
1694 | ||
1695 | dp = args->dp; | |
1696 | mp = dp->i_mount; | |
1697 | tp = args->trans; | |
1698 | /* | |
1699 | * Read the offending data block. We need its buffer. | |
1700 | */ | |
bbaaf538 | 1701 | if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp, |
1da177e4 LT |
1702 | XFS_DATA_FORK))) { |
1703 | return error; | |
1704 | } | |
1705 | #ifdef DEBUG | |
1706 | data = dbp->data; | |
70e73f59 | 1707 | ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC); |
1da177e4 LT |
1708 | #endif |
1709 | /* this seems to be an error | |
1710 | * data is only valid if DEBUG is defined? | |
1711 | * RMC 09/08/1999 | |
1712 | */ | |
1713 | ||
1714 | leaf = lbp->data; | |
bbaaf538 | 1715 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
70e73f59 | 1716 | ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) == |
1da177e4 | 1717 | mp->m_dirblksize - (uint)sizeof(data->hdr)); |
afbcb3f9 | 1718 | ASSERT(db == be32_to_cpu(ltp->bestcount) - 1); |
1da177e4 LT |
1719 | /* |
1720 | * Get rid of the data block. | |
1721 | */ | |
1722 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { | |
1723 | ASSERT(error != ENOSPC); | |
1724 | xfs_da_brelse(tp, dbp); | |
1725 | return error; | |
1726 | } | |
1727 | /* | |
1728 | * Eliminate the last bests entry from the table. | |
1729 | */ | |
bbaaf538 | 1730 | bestsp = xfs_dir2_leaf_bests_p(ltp); |
413d57c9 | 1731 | be32_add_cpu(<p->bestcount, -1); |
afbcb3f9 | 1732 | memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp)); |
1da177e4 | 1733 | xfs_dir2_leaf_log_tail(tp, lbp); |
afbcb3f9 | 1734 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
1da177e4 LT |
1735 | return 0; |
1736 | } | |
1737 | ||
1738 | /* | |
1739 | * Convert node form directory to leaf form directory. | |
1740 | * The root of the node form dir needs to already be a LEAFN block. | |
1741 | * Just return if we can't do anything. | |
1742 | */ | |
1743 | int /* error */ | |
1744 | xfs_dir2_node_to_leaf( | |
1745 | xfs_da_state_t *state) /* directory operation state */ | |
1746 | { | |
1747 | xfs_da_args_t *args; /* operation arguments */ | |
1748 | xfs_inode_t *dp; /* incore directory inode */ | |
1749 | int error; /* error return code */ | |
1750 | xfs_dabuf_t *fbp; /* buffer for freespace block */ | |
1751 | xfs_fileoff_t fo; /* freespace file offset */ | |
1752 | xfs_dir2_free_t *free; /* freespace structure */ | |
1753 | xfs_dabuf_t *lbp; /* buffer for leaf block */ | |
1754 | xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ | |
1755 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1756 | xfs_mount_t *mp; /* filesystem mount point */ | |
1757 | int rval; /* successful free trim? */ | |
1758 | xfs_trans_t *tp; /* transaction pointer */ | |
1759 | ||
1760 | /* | |
1761 | * There's more than a leaf level in the btree, so there must | |
1762 | * be multiple leafn blocks. Give up. | |
1763 | */ | |
1764 | if (state->path.active > 1) | |
1765 | return 0; | |
1766 | args = state->args; | |
1767 | xfs_dir2_trace_args("node_to_leaf", args); | |
1768 | mp = state->mp; | |
1769 | dp = args->dp; | |
1770 | tp = args->trans; | |
1771 | /* | |
1772 | * Get the last offset in the file. | |
1773 | */ | |
1774 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) { | |
1775 | return error; | |
1776 | } | |
1777 | fo -= mp->m_dirblkfsbs; | |
1778 | /* | |
1779 | * If there are freespace blocks other than the first one, | |
1780 | * take this opportunity to remove trailing empty freespace blocks | |
1781 | * that may have been left behind during no-space-reservation | |
1782 | * operations. | |
1783 | */ | |
1784 | while (fo > mp->m_dirfreeblk) { | |
1785 | if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { | |
1786 | return error; | |
1787 | } | |
1788 | if (rval) | |
1789 | fo -= mp->m_dirblkfsbs; | |
1790 | else | |
1791 | return 0; | |
1792 | } | |
1793 | /* | |
1794 | * Now find the block just before the freespace block. | |
1795 | */ | |
1796 | if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { | |
1797 | return error; | |
1798 | } | |
1799 | /* | |
1800 | * If it's not the single leaf block, give up. | |
1801 | */ | |
1802 | if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize) | |
1803 | return 0; | |
1804 | lbp = state->path.blk[0].bp; | |
1805 | leaf = lbp->data; | |
89da0544 | 1806 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
1da177e4 LT |
1807 | /* |
1808 | * Read the freespace block. | |
1809 | */ | |
1810 | if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp, | |
1811 | XFS_DATA_FORK))) { | |
1812 | return error; | |
1813 | } | |
1814 | free = fbp->data; | |
0ba962ef | 1815 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
1da177e4 LT |
1816 | ASSERT(!free->hdr.firstdb); |
1817 | /* | |
1818 | * Now see if the leafn and free data will fit in a leaf1. | |
1819 | * If not, release the buffer and give up. | |
1820 | */ | |
1821 | if ((uint)sizeof(leaf->hdr) + | |
a818e5de | 1822 | (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) + |
0ba962ef | 1823 | be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) + |
1da177e4 LT |
1824 | (uint)sizeof(leaf->tail) > |
1825 | mp->m_dirblksize) { | |
1826 | xfs_da_brelse(tp, fbp); | |
1827 | return 0; | |
1828 | } | |
1829 | /* | |
1830 | * If the leaf has any stale entries in it, compress them out. | |
1831 | * The compact routine will log the header. | |
1832 | */ | |
a818e5de | 1833 | if (be16_to_cpu(leaf->hdr.stale)) |
1da177e4 LT |
1834 | xfs_dir2_leaf_compact(args, lbp); |
1835 | else | |
1836 | xfs_dir2_leaf_log_header(tp, lbp); | |
89da0544 | 1837 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC); |
1da177e4 LT |
1838 | /* |
1839 | * Set up the leaf tail from the freespace block. | |
1840 | */ | |
bbaaf538 | 1841 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
0ba962ef | 1842 | ltp->bestcount = free->hdr.nvalid; |
1da177e4 LT |
1843 | /* |
1844 | * Set up the leaf bests table. | |
1845 | */ | |
bbaaf538 | 1846 | memcpy(xfs_dir2_leaf_bests_p(ltp), free->bests, |
afbcb3f9 NS |
1847 | be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0])); |
1848 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); | |
1da177e4 LT |
1849 | xfs_dir2_leaf_log_tail(tp, lbp); |
1850 | xfs_dir2_leaf_check(dp, lbp); | |
1851 | /* | |
1852 | * Get rid of the freespace block. | |
1853 | */ | |
1854 | error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp); | |
1855 | if (error) { | |
1856 | /* | |
1857 | * This can't fail here because it can only happen when | |
1858 | * punching out the middle of an extent, and this is an | |
1859 | * isolated block. | |
1860 | */ | |
1861 | ASSERT(error != ENOSPC); | |
1862 | return error; | |
1863 | } | |
1864 | fbp = NULL; | |
1865 | /* | |
1866 | * Now see if we can convert the single-leaf directory | |
1867 | * down to a block form directory. | |
1868 | * This routine always kills the dabuf for the leaf, so | |
1869 | * eliminate it from the path. | |
1870 | */ | |
1871 | error = xfs_dir2_leaf_to_block(args, lbp, NULL); | |
1872 | state->path.blk[0].bp = NULL; | |
1873 | return error; | |
1874 | } |