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