]>
Commit | Line | Data |
---|---|---|
2bd0ea18 | 1 | /* |
0d3e0b37 | 2 | * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved. |
5000d01d | 3 | * |
2bd0ea18 NS |
4 | * This program is free software; you can redistribute it and/or modify it |
5 | * under the terms of version 2 of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
5000d01d | 7 | * |
2bd0ea18 NS |
8 | * This program is distributed in the hope that it would be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
5000d01d | 11 | * |
2bd0ea18 NS |
12 | * Further, this software is distributed without any warranty that it is |
13 | * free of the rightful claim of any third person regarding infringement | |
dfc130f3 | 14 | * or the like. Any license provided herein, whether implied or |
2bd0ea18 NS |
15 | * otherwise, applies only to this software file. Patent licenses, if |
16 | * any, provided herein do not apply to combinations of this program with | |
17 | * other software, or any other product whatsoever. | |
5000d01d | 18 | * |
2bd0ea18 NS |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write the Free Software Foundation, Inc., 59 | |
21 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. | |
5000d01d | 22 | * |
2bd0ea18 NS |
23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
24 | * Mountain View, CA 94043, or: | |
5000d01d SL |
25 | * |
26 | * http://www.sgi.com | |
27 | * | |
28 | * For further information regarding this notice, see: | |
29 | * | |
2bd0ea18 NS |
30 | * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ |
31 | */ | |
32 | ||
33 | /* | |
34 | * xfs_dir2_leaf.c | |
35 | * XFS directory version 2 implementation - single leaf form | |
36 | * see xfs_dir2_leaf.h for data structures. | |
37 | * These directories have multiple XFS_DIR2_DATA blocks and one | |
38 | * XFS_DIR2_LEAF1 block containing the hash table and freespace map. | |
39 | */ | |
40 | ||
41 | #include <xfs.h> | |
42 | ||
f302e9e4 NS |
43 | static void xfs_dir2_leaf_log_bests(xfs_trans_t *, xfs_dabuf_t *, int, int); |
44 | static void xfs_dir2_leaf_log_tail(xfs_trans_t *, xfs_dabuf_t *); | |
2bd0ea18 NS |
45 | |
46 | /* | |
47 | * Convert a block form directory to a leaf form directory. | |
48 | */ | |
49 | int /* error */ | |
50 | xfs_dir2_block_to_leaf( | |
51 | xfs_da_args_t *args, /* operation arguments */ | |
52 | xfs_dabuf_t *dbp) /* input block's buffer */ | |
53 | { | |
54 | xfs_dir2_data_off_t *bestsp; /* leaf's bestsp entries */ | |
55 | xfs_dablk_t blkno; /* leaf block's bno */ | |
56 | xfs_dir2_block_t *block; /* block structure */ | |
57 | xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ | |
58 | xfs_dir2_block_tail_t *btp; /* block's tail */ | |
59 | xfs_inode_t *dp; /* incore directory inode */ | |
60 | int error; /* error return code */ | |
61 | xfs_dabuf_t *lbp; /* leaf block's buffer */ | |
62 | xfs_dir2_db_t ldb; /* leaf block's bno */ | |
63 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
64 | xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ | |
65 | xfs_mount_t *mp; /* filesystem mount point */ | |
66 | int needlog; /* need to log block header */ | |
67 | int needscan; /* need to rescan bestfree */ | |
68 | xfs_trans_t *tp; /* transaction pointer */ | |
69 | ||
70 | xfs_dir2_trace_args_b("block_to_leaf", args, dbp); | |
71 | dp = args->dp; | |
72 | mp = dp->i_mount; | |
73 | tp = args->trans; | |
74 | /* | |
75 | * Add the leaf block to the inode. | |
76 | * This interface will only put blocks in the leaf/node range. | |
77 | * Since that's empty now, we'll get the root (block 0 in range). | |
78 | */ | |
0e266570 | 79 | if ((error = xfs_da_grow_inode(args, &blkno))) { |
2bd0ea18 NS |
80 | return error; |
81 | } | |
82 | ldb = XFS_DIR2_DA_TO_DB(mp, blkno); | |
83 | ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp)); | |
84 | /* | |
85 | * Initialize the leaf block, get a buffer for it. | |
86 | */ | |
0e266570 | 87 | if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) { |
2bd0ea18 NS |
88 | return error; |
89 | } | |
90 | ASSERT(lbp != NULL); | |
91 | leaf = lbp->data; | |
92 | block = dbp->data; | |
93 | xfs_dir2_data_check(dp, dbp); | |
94 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, block); | |
46eca962 | 95 | blp = XFS_DIR2_BLOCK_LEAF_P(btp); |
2bd0ea18 NS |
96 | /* |
97 | * Set the counts in the leaf header. | |
98 | */ | |
99 | INT_COPY(leaf->hdr.count, btp->count, ARCH_CONVERT); /* INT_: type change */ | |
100 | INT_COPY(leaf->hdr.stale, btp->stale, ARCH_CONVERT); /* INT_: type change */ | |
101 | /* | |
102 | * Could compact these but I think we always do the conversion | |
103 | * after squeezing out stale entries. | |
104 | */ | |
32181a02 | 105 | memcpy(leaf->ents, blp, INT_GET(btp->count, ARCH_CONVERT) * sizeof(xfs_dir2_leaf_entry_t)); |
2bd0ea18 NS |
106 | xfs_dir2_leaf_log_ents(tp, lbp, 0, INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1); |
107 | needscan = 0; | |
108 | needlog = 1; | |
109 | /* | |
110 | * Make the space formerly occupied by the leaf entries and block | |
111 | * tail be free. | |
112 | */ | |
113 | xfs_dir2_data_make_free(tp, dbp, | |
114 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)block), | |
115 | (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize - | |
116 | (char *)blp), | |
117 | &needlog, &needscan); | |
118 | /* | |
119 | * Fix up the block header, make it a data block. | |
120 | */ | |
121 | INT_SET(block->hdr.magic, ARCH_CONVERT, XFS_DIR2_DATA_MAGIC); | |
122 | if (needscan) | |
123 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog, | |
124 | NULL); | |
125 | /* | |
126 | * Set up leaf tail and bests table. | |
127 | */ | |
128 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
129 | INT_SET(ltp->bestcount, ARCH_CONVERT, 1); | |
46eca962 | 130 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
2bd0ea18 NS |
131 | INT_COPY(bestsp[0], block->hdr.bestfree[0].length, ARCH_CONVERT); |
132 | /* | |
133 | * Log the data header and leaf bests table. | |
134 | */ | |
135 | if (needlog) | |
136 | xfs_dir2_data_log_header(tp, dbp); | |
137 | xfs_dir2_leaf_check(dp, lbp); | |
138 | xfs_dir2_data_check(dp, dbp); | |
139 | xfs_dir2_leaf_log_bests(tp, lbp, 0, 0); | |
140 | xfs_da_buf_done(lbp); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Add an entry to a leaf form directory. | |
146 | */ | |
147 | int /* error */ | |
148 | xfs_dir2_leaf_addname( | |
149 | xfs_da_args_t *args) /* operation arguments */ | |
150 | { | |
151 | xfs_dir2_data_off_t *bestsp; /* freespace table in leaf */ | |
152 | int compact; /* need to compact leaves */ | |
153 | xfs_dir2_data_t *data; /* data block structure */ | |
154 | xfs_dabuf_t *dbp; /* data block buffer */ | |
155 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
156 | xfs_inode_t *dp; /* incore directory inode */ | |
157 | xfs_dir2_data_unused_t *dup; /* data unused entry */ | |
158 | int error; /* error return value */ | |
159 | int grown; /* allocated new data block */ | |
160 | int highstale; /* index of next stale leaf */ | |
161 | int i; /* temporary, index */ | |
162 | int index; /* leaf table position */ | |
163 | xfs_dabuf_t *lbp; /* leaf's buffer */ | |
164 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
165 | int length; /* length of new entry */ | |
166 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ | |
167 | int lfloglow; /* low leaf logging index */ | |
168 | int lfloghigh; /* high leaf logging index */ | |
169 | int lowstale; /* index of prev stale leaf */ | |
170 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ | |
171 | xfs_mount_t *mp; /* filesystem mount point */ | |
172 | int needbytes; /* leaf block bytes needed */ | |
173 | int needlog; /* need to log data header */ | |
174 | int needscan; /* need to rescan data free */ | |
175 | xfs_dir2_data_off_t *tagp; /* end of data entry */ | |
176 | xfs_trans_t *tp; /* transaction pointer */ | |
177 | xfs_dir2_db_t use_block; /* data block number */ | |
178 | ||
179 | xfs_dir2_trace_args("leaf_addname", args); | |
180 | dp = args->dp; | |
181 | tp = args->trans; | |
182 | mp = dp->i_mount; | |
183 | /* | |
184 | * Read the leaf block. | |
185 | */ | |
186 | error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, | |
187 | XFS_DATA_FORK); | |
188 | if (error) { | |
2bd0ea18 NS |
189 | return error; |
190 | } | |
191 | ASSERT(lbp != NULL); | |
192 | /* | |
193 | * Look up the entry by hash value and name. | |
194 | * We know it's not there, our caller has already done a lookup. | |
195 | * So the index is of the entry to insert in front of. | |
196 | * But if there are dup hash values the index is of the first of those. | |
197 | */ | |
198 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
199 | leaf = lbp->data; | |
200 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
46eca962 | 201 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
2bd0ea18 NS |
202 | length = XFS_DIR2_DATA_ENTSIZE(args->namelen); |
203 | /* | |
204 | * See if there are any entries with the same hash value | |
205 | * and space in their block for the new entry. | |
206 | * This is good because it puts multiple same-hash value entries | |
207 | * in a data block, improving the lookup of those entries. | |
208 | */ | |
209 | for (use_block = -1, lep = &leaf->ents[index]; | |
210 | index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval; | |
211 | index++, lep++) { | |
212 | if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) | |
213 | continue; | |
214 | i = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT)); | |
215 | ASSERT(i < INT_GET(ltp->bestcount, ARCH_CONVERT)); | |
216 | ASSERT(INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF); | |
217 | if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) { | |
218 | use_block = i; | |
219 | break; | |
220 | } | |
221 | } | |
222 | /* | |
223 | * Didn't find a block yet, linear search all the data blocks. | |
224 | */ | |
225 | if (use_block == -1) { | |
226 | for (i = 0; i < INT_GET(ltp->bestcount, ARCH_CONVERT); i++) { | |
227 | /* | |
228 | * Remember a block we see that's missing. | |
229 | */ | |
230 | if (INT_GET(bestsp[i], ARCH_CONVERT) == NULLDATAOFF && use_block == -1) | |
231 | use_block = i; | |
232 | else if (INT_GET(bestsp[i], ARCH_CONVERT) >= length) { | |
233 | use_block = i; | |
234 | break; | |
235 | } | |
236 | } | |
237 | } | |
238 | /* | |
239 | * How many bytes do we need in the leaf block? | |
240 | */ | |
241 | needbytes = | |
46eca962 | 242 | (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) + |
2bd0ea18 NS |
243 | (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0])); |
244 | /* | |
245 | * Now kill use_block if it refers to a missing block, so we | |
246 | * can use it as an indication of allocation needed. | |
247 | */ | |
248 | if (use_block != -1 && INT_GET(bestsp[use_block], ARCH_CONVERT) == NULLDATAOFF) | |
249 | use_block = -1; | |
250 | /* | |
251 | * If we don't have enough free bytes but we can make enough | |
252 | * by compacting out stale entries, we'll do that. | |
253 | */ | |
254 | if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] < needbytes && | |
255 | INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1) { | |
2bd0ea18 NS |
256 | compact = 1; |
257 | } | |
258 | /* | |
259 | * Otherwise if we don't have enough free bytes we need to | |
260 | * convert to node form. | |
261 | */ | |
262 | else if ((char *)bestsp - (char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] < | |
263 | needbytes) { | |
2bd0ea18 NS |
264 | /* |
265 | * Just checking or no space reservation, give up. | |
266 | */ | |
267 | if (args->justcheck || args->total == 0) { | |
268 | xfs_da_brelse(tp, lbp); | |
269 | return XFS_ERROR(ENOSPC); | |
270 | } | |
271 | /* | |
272 | * Convert to node form. | |
273 | */ | |
274 | error = xfs_dir2_leaf_to_node(args, lbp); | |
275 | xfs_da_buf_done(lbp); | |
276 | if (error) | |
277 | return error; | |
278 | /* | |
279 | * Then add the new entry. | |
280 | */ | |
281 | return xfs_dir2_node_addname(args); | |
282 | } | |
283 | /* | |
284 | * Otherwise it will fit without compaction. | |
285 | */ | |
286 | else | |
287 | compact = 0; | |
288 | /* | |
289 | * If just checking, then it will fit unless we needed to allocate | |
290 | * a new data block. | |
291 | */ | |
292 | if (args->justcheck) { | |
293 | xfs_da_brelse(tp, lbp); | |
294 | return use_block == -1 ? XFS_ERROR(ENOSPC) : 0; | |
295 | } | |
296 | /* | |
297 | * If no allocations are allowed, return now before we've | |
298 | * changed anything. | |
299 | */ | |
300 | if (args->total == 0 && use_block == -1) { | |
2bd0ea18 NS |
301 | xfs_da_brelse(tp, lbp); |
302 | return XFS_ERROR(ENOSPC); | |
303 | } | |
304 | /* | |
305 | * Need to compact the leaf entries, removing stale ones. | |
306 | * Leave one stale entry behind - the one closest to our | |
307 | * insertion index - and we'll shift that one to our insertion | |
308 | * point later. | |
309 | */ | |
310 | if (compact) { | |
2bd0ea18 NS |
311 | xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale, |
312 | &lfloglow, &lfloghigh); | |
313 | } | |
314 | /* | |
315 | * There are stale entries, so we'll need log-low and log-high | |
316 | * impossibly bad values later. | |
317 | */ | |
318 | else if (INT_GET(leaf->hdr.stale, ARCH_CONVERT)) { | |
319 | lfloglow = INT_GET(leaf->hdr.count, ARCH_CONVERT); | |
320 | lfloghigh = -1; | |
321 | } | |
322 | /* | |
323 | * If there was no data block space found, we need to allocate | |
324 | * a new one. | |
325 | */ | |
326 | if (use_block == -1) { | |
2bd0ea18 NS |
327 | /* |
328 | * Add the new data block. | |
329 | */ | |
0e266570 NS |
330 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, |
331 | &use_block))) { | |
2bd0ea18 NS |
332 | xfs_da_brelse(tp, lbp); |
333 | return error; | |
334 | } | |
335 | /* | |
336 | * Initialize the block. | |
337 | */ | |
0e266570 | 338 | if ((error = xfs_dir2_data_init(args, use_block, &dbp))) { |
2bd0ea18 NS |
339 | xfs_da_brelse(tp, lbp); |
340 | return error; | |
341 | } | |
342 | /* | |
343 | * If we're adding a new data block on the end we need to | |
344 | * extend the bests table. Copy it up one entry. | |
345 | */ | |
346 | if (use_block >= INT_GET(ltp->bestcount, ARCH_CONVERT)) { | |
347 | bestsp--; | |
32181a02 | 348 | memmove(&bestsp[0], &bestsp[1], |
2bd0ea18 NS |
349 | INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(bestsp[0])); |
350 | INT_MOD(ltp->bestcount, ARCH_CONVERT, +1); | |
351 | xfs_dir2_leaf_log_tail(tp, lbp); | |
352 | xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1); | |
353 | } | |
354 | /* | |
355 | * If we're filling in a previously empty block just log it. | |
356 | */ | |
357 | else | |
358 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); | |
359 | data = dbp->data; | |
360 | INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT); | |
361 | grown = 1; | |
362 | } | |
363 | /* | |
364 | * Already had space in some data block. | |
365 | * Just read that one in. | |
366 | */ | |
367 | else { | |
0e266570 | 368 | if ((error = |
2bd0ea18 | 369 | xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, use_block), |
0e266570 | 370 | -1, &dbp, XFS_DATA_FORK))) { |
2bd0ea18 NS |
371 | xfs_da_brelse(tp, lbp); |
372 | return error; | |
373 | } | |
374 | data = dbp->data; | |
375 | grown = 0; | |
376 | } | |
377 | xfs_dir2_data_check(dp, dbp); | |
378 | /* | |
379 | * Point to the biggest freespace in our data block. | |
380 | */ | |
381 | dup = (xfs_dir2_data_unused_t *) | |
382 | ((char *)data + INT_GET(data->hdr.bestfree[0].offset, ARCH_CONVERT)); | |
383 | ASSERT(INT_GET(dup->length, ARCH_CONVERT) >= length); | |
384 | needscan = needlog = 0; | |
385 | /* | |
386 | * Mark the initial part of our freespace in use for the new entry. | |
387 | */ | |
388 | xfs_dir2_data_use_free(tp, dbp, dup, | |
389 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length, | |
390 | &needlog, &needscan); | |
391 | /* | |
392 | * Initialize our new entry (at last). | |
393 | */ | |
394 | dep = (xfs_dir2_data_entry_t *)dup; | |
395 | INT_SET(dep->inumber, ARCH_CONVERT, args->inumber); | |
396 | dep->namelen = args->namelen; | |
32181a02 | 397 | memcpy(dep->name, args->name, dep->namelen); |
2bd0ea18 NS |
398 | tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep); |
399 | INT_SET(*tagp, ARCH_CONVERT, (xfs_dir2_data_off_t)((char *)dep - (char *)data)); | |
400 | /* | |
401 | * Need to scan fix up the bestfree table. | |
402 | */ | |
403 | if (needscan) | |
404 | xfs_dir2_data_freescan(mp, data, &needlog, NULL); | |
405 | /* | |
406 | * Need to log the data block's header. | |
407 | */ | |
408 | if (needlog) | |
409 | xfs_dir2_data_log_header(tp, dbp); | |
410 | xfs_dir2_data_log_entry(tp, dbp, dep); | |
411 | /* | |
412 | * If the bests table needs to be changed, do it. | |
413 | * Log the change unless we've already done that. | |
414 | */ | |
415 | if (INT_GET(bestsp[use_block], ARCH_CONVERT) != INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT)) { | |
416 | INT_COPY(bestsp[use_block], data->hdr.bestfree[0].length, ARCH_CONVERT); | |
417 | if (!grown) | |
418 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); | |
419 | } | |
420 | /* | |
421 | * Now we need to make room to insert the leaf entry. | |
422 | * If there are no stale entries, we just insert a hole at index. | |
423 | */ | |
46eca962 | 424 | if (!leaf->hdr.stale) { |
2bd0ea18 NS |
425 | /* |
426 | * lep is still good as the index leaf entry. | |
427 | */ | |
428 | if (index < INT_GET(leaf->hdr.count, ARCH_CONVERT)) | |
32181a02 | 429 | memmove(lep + 1, lep, |
2bd0ea18 NS |
430 | (INT_GET(leaf->hdr.count, ARCH_CONVERT) - index) * sizeof(*lep)); |
431 | /* | |
432 | * Record low and high logging indices for the leaf. | |
433 | */ | |
434 | lfloglow = index; | |
435 | lfloghigh = INT_GET(leaf->hdr.count, ARCH_CONVERT); | |
436 | INT_MOD(leaf->hdr.count, ARCH_CONVERT, +1); | |
437 | } | |
438 | /* | |
439 | * There are stale entries. | |
440 | * We will use one of them for the new entry. | |
441 | * It's probably not at the right location, so we'll have to | |
442 | * shift some up or down first. | |
443 | */ | |
444 | else { | |
445 | /* | |
446 | * If we didn't compact before, we need to find the nearest | |
447 | * stale entries before and after our insertion point. | |
448 | */ | |
449 | if (compact == 0) { | |
450 | /* | |
451 | * Find the first stale entry before the insertion | |
452 | * point, if any. | |
453 | */ | |
454 | for (lowstale = index - 1; | |
455 | lowstale >= 0 && | |
456 | INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) != | |
457 | XFS_DIR2_NULL_DATAPTR; | |
458 | lowstale--) | |
459 | continue; | |
460 | /* | |
461 | * Find the next stale entry at or after the insertion | |
462 | * point, if any. Stop if we go so far that the | |
463 | * lowstale entry would be better. | |
464 | */ | |
465 | for (highstale = index; | |
466 | highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) && | |
467 | INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) != | |
468 | XFS_DIR2_NULL_DATAPTR && | |
469 | (lowstale < 0 || | |
470 | index - lowstale - 1 >= highstale - index); | |
471 | highstale++) | |
472 | continue; | |
473 | } | |
474 | /* | |
475 | * If the low one is better, use it. | |
476 | */ | |
477 | if (lowstale >= 0 && | |
478 | (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) || | |
479 | index - lowstale - 1 < highstale - index)) { | |
480 | ASSERT(index - lowstale - 1 >= 0); | |
481 | ASSERT(INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) == | |
482 | XFS_DIR2_NULL_DATAPTR); | |
483 | /* | |
484 | * Copy entries up to cover the stale entry | |
485 | * and make room for the new entry. | |
486 | */ | |
487 | if (index - lowstale - 1 > 0) | |
32181a02 NS |
488 | memmove(&leaf->ents[lowstale], |
489 | &leaf->ents[lowstale + 1], | |
2bd0ea18 NS |
490 | (index - lowstale - 1) * sizeof(*lep)); |
491 | lep = &leaf->ents[index - 1]; | |
492 | lfloglow = MIN(lowstale, lfloglow); | |
493 | lfloghigh = MAX(index - 1, lfloghigh); | |
494 | } | |
495 | /* | |
496 | * The high one is better, so use that one. | |
497 | */ | |
498 | else { | |
499 | ASSERT(highstale - index >= 0); | |
500 | ASSERT(INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) == | |
501 | XFS_DIR2_NULL_DATAPTR); | |
502 | /* | |
503 | * Copy entries down to copver the stale entry | |
504 | * and make room for the new entry. | |
505 | */ | |
506 | if (highstale - index > 0) | |
32181a02 NS |
507 | memmove(&leaf->ents[index + 1], |
508 | &leaf->ents[index], | |
2bd0ea18 NS |
509 | (highstale - index) * sizeof(*lep)); |
510 | lep = &leaf->ents[index]; | |
511 | lfloglow = MIN(index, lfloglow); | |
512 | lfloghigh = MAX(highstale, lfloghigh); | |
513 | } | |
514 | INT_MOD(leaf->hdr.stale, ARCH_CONVERT, -1); | |
515 | } | |
516 | /* | |
517 | * Fill in the new leaf entry. | |
518 | */ | |
519 | INT_SET(lep->hashval, ARCH_CONVERT, args->hashval); | |
520 | INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_DB_OFF_TO_DATAPTR(mp, use_block, INT_GET(*tagp, ARCH_CONVERT))); | |
521 | /* | |
522 | * Log the leaf fields and give up the buffers. | |
523 | */ | |
524 | xfs_dir2_leaf_log_header(tp, lbp); | |
525 | xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh); | |
526 | xfs_dir2_leaf_check(dp, lbp); | |
527 | xfs_da_buf_done(lbp); | |
528 | xfs_dir2_data_check(dp, dbp); | |
529 | xfs_da_buf_done(dbp); | |
530 | return 0; | |
531 | } | |
532 | ||
2bd0ea18 NS |
533 | #ifdef DEBUG |
534 | /* | |
535 | * Check the internal consistency of a leaf1 block. | |
536 | * Pop an assert if something is wrong. | |
537 | */ | |
538 | void | |
539 | xfs_dir2_leaf_check( | |
540 | xfs_inode_t *dp, /* incore directory inode */ | |
541 | xfs_dabuf_t *bp) /* leaf's buffer */ | |
542 | { | |
543 | int i; /* leaf index */ | |
544 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
545 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ | |
546 | xfs_mount_t *mp; /* filesystem mount point */ | |
547 | int stale; /* count of stale leaves */ | |
548 | ||
549 | leaf = bp->data; | |
550 | mp = dp->i_mount; | |
551 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC); | |
552 | /* | |
553 | * This value is not restrictive enough. | |
554 | * Should factor in the size of the bests table as well. | |
555 | * We can deduce a value for that from di_size. | |
556 | */ | |
557 | ASSERT(INT_GET(leaf->hdr.count, ARCH_CONVERT) <= XFS_DIR2_MAX_LEAF_ENTS(mp)); | |
558 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
559 | /* | |
560 | * Leaves and bests don't overlap. | |
561 | */ | |
562 | ASSERT((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] <= | |
46eca962 | 563 | (char *)XFS_DIR2_LEAF_BESTS_P(ltp)); |
2bd0ea18 NS |
564 | /* |
565 | * Check hash value order, count stale entries. | |
566 | */ | |
567 | for (i = stale = 0; i < INT_GET(leaf->hdr.count, ARCH_CONVERT); i++) { | |
568 | if (i + 1 < INT_GET(leaf->hdr.count, ARCH_CONVERT)) | |
569 | ASSERT(INT_GET(leaf->ents[i].hashval, ARCH_CONVERT) <= | |
570 | INT_GET(leaf->ents[i + 1].hashval, ARCH_CONVERT)); | |
571 | if (INT_GET(leaf->ents[i].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) | |
572 | stale++; | |
573 | } | |
574 | ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == stale); | |
575 | } | |
576 | #endif /* DEBUG */ | |
577 | ||
578 | /* | |
579 | * Compact out any stale entries in the leaf. | |
580 | * Log the header and changed leaf entries, if any. | |
581 | */ | |
582 | void | |
583 | xfs_dir2_leaf_compact( | |
584 | xfs_da_args_t *args, /* operation arguments */ | |
585 | xfs_dabuf_t *bp) /* leaf buffer */ | |
586 | { | |
587 | int from; /* source leaf index */ | |
dfc130f3 | 588 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
2bd0ea18 NS |
589 | int loglow; /* first leaf entry to log */ |
590 | int to; /* target leaf index */ | |
591 | ||
592 | leaf = bp->data; | |
46eca962 | 593 | if (!leaf->hdr.stale) { |
2bd0ea18 NS |
594 | return; |
595 | } | |
596 | /* | |
597 | * Compress out the stale entries in place. | |
598 | */ | |
599 | for (from = to = 0, loglow = -1; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) { | |
600 | if (INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) | |
601 | continue; | |
602 | /* | |
603 | * Only actually copy the entries that are different. | |
604 | */ | |
605 | if (from > to) { | |
606 | if (loglow == -1) | |
607 | loglow = to; | |
608 | leaf->ents[to] = leaf->ents[from]; | |
609 | } | |
610 | to++; | |
611 | } | |
612 | /* | |
613 | * Update and log the header, log the leaf entries. | |
614 | */ | |
615 | ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) == from - to); | |
616 | INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(INT_GET(leaf->hdr.stale, ARCH_CONVERT))); | |
46eca962 | 617 | leaf->hdr.stale = 0; |
2bd0ea18 NS |
618 | xfs_dir2_leaf_log_header(args->trans, bp); |
619 | if (loglow != -1) | |
620 | xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1); | |
621 | } | |
622 | ||
623 | /* | |
624 | * Compact the leaf entries, removing stale ones. | |
625 | * Leave one stale entry behind - the one closest to our | |
626 | * insertion index - and the caller will shift that one to our insertion | |
627 | * point later. | |
628 | * Return new insertion index, where the remaining stale entry is, | |
629 | * and leaf logging indices. | |
630 | */ | |
631 | void | |
632 | xfs_dir2_leaf_compact_x1( | |
633 | xfs_dabuf_t *bp, /* leaf buffer */ | |
634 | int *indexp, /* insertion index */ | |
635 | int *lowstalep, /* out: stale entry before us */ | |
636 | int *highstalep, /* out: stale entry after us */ | |
637 | int *lowlogp, /* out: low log index */ | |
638 | int *highlogp) /* out: high log index */ | |
639 | { | |
640 | int from; /* source copy index */ | |
641 | int highstale; /* stale entry at/after index */ | |
642 | int index; /* insertion index */ | |
643 | int keepstale; /* source index of kept stale */ | |
dfc130f3 | 644 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
2bd0ea18 | 645 | int lowstale; /* stale entry before index */ |
0e266570 | 646 | int newindex=0; /* new insertion index */ |
2bd0ea18 NS |
647 | int to; /* destination copy index */ |
648 | ||
649 | leaf = bp->data; | |
650 | ASSERT(INT_GET(leaf->hdr.stale, ARCH_CONVERT) > 1); | |
651 | index = *indexp; | |
652 | /* | |
653 | * Find the first stale entry before our index, if any. | |
654 | */ | |
655 | for (lowstale = index - 1; | |
656 | lowstale >= 0 && | |
657 | INT_GET(leaf->ents[lowstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR; | |
658 | lowstale--) | |
659 | continue; | |
660 | /* | |
661 | * Find the first stale entry at or after our index, if any. | |
662 | * Stop if the answer would be worse than lowstale. | |
663 | */ | |
664 | for (highstale = index; | |
665 | highstale < INT_GET(leaf->hdr.count, ARCH_CONVERT) && | |
666 | INT_GET(leaf->ents[highstale].address, ARCH_CONVERT) != XFS_DIR2_NULL_DATAPTR && | |
667 | (lowstale < 0 || index - lowstale > highstale - index); | |
668 | highstale++) | |
669 | continue; | |
670 | /* | |
671 | * Pick the better of lowstale and highstale. | |
672 | */ | |
673 | if (lowstale >= 0 && | |
674 | (highstale == INT_GET(leaf->hdr.count, ARCH_CONVERT) || | |
675 | index - lowstale <= highstale - index)) | |
676 | keepstale = lowstale; | |
677 | else | |
678 | keepstale = highstale; | |
679 | /* | |
680 | * Copy the entries in place, removing all the stale entries | |
681 | * except keepstale. | |
682 | */ | |
683 | for (from = to = 0; from < INT_GET(leaf->hdr.count, ARCH_CONVERT); from++) { | |
684 | /* | |
685 | * Notice the new value of index. | |
686 | */ | |
687 | if (index == from) | |
688 | newindex = to; | |
689 | if (from != keepstale && | |
690 | INT_GET(leaf->ents[from].address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) { | |
691 | if (from == to) | |
692 | *lowlogp = to; | |
693 | continue; | |
694 | } | |
695 | /* | |
696 | * Record the new keepstale value for the insertion. | |
697 | */ | |
698 | if (from == keepstale) | |
699 | lowstale = highstale = to; | |
700 | /* | |
701 | * Copy only the entries that have moved. | |
702 | */ | |
703 | if (from > to) | |
704 | leaf->ents[to] = leaf->ents[from]; | |
705 | to++; | |
706 | } | |
707 | ASSERT(from > to); | |
708 | /* | |
709 | * If the insertion point was past the last entry, | |
710 | * set the new insertion point accordingly. | |
711 | */ | |
712 | if (index == from) | |
713 | newindex = to; | |
714 | *indexp = newindex; | |
715 | /* | |
716 | * Adjust the leaf header values. | |
717 | */ | |
718 | INT_MOD(leaf->hdr.count, ARCH_CONVERT, -(from - to)); | |
719 | INT_SET(leaf->hdr.stale, ARCH_CONVERT, 1); | |
720 | /* | |
721 | * Remember the low/high stale value only in the "right" | |
722 | * direction. | |
723 | */ | |
724 | if (lowstale >= newindex) | |
725 | lowstale = -1; | |
726 | else | |
727 | highstale = INT_GET(leaf->hdr.count, ARCH_CONVERT); | |
728 | *highlogp = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1; | |
729 | *lowstalep = lowstale; | |
730 | *highstalep = highstale; | |
731 | } | |
732 | ||
733 | /* | |
734 | * Initialize a new leaf block, leaf1 or leafn magic accepted. | |
735 | */ | |
736 | int | |
737 | xfs_dir2_leaf_init( | |
738 | xfs_da_args_t *args, /* operation arguments */ | |
739 | xfs_dir2_db_t bno, /* directory block number */ | |
740 | xfs_dabuf_t **bpp, /* out: leaf buffer */ | |
741 | int magic) /* magic number for block */ | |
742 | { | |
743 | xfs_dabuf_t *bp; /* leaf buffer */ | |
744 | xfs_inode_t *dp; /* incore directory inode */ | |
745 | int error; /* error return code */ | |
746 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
747 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
748 | xfs_mount_t *mp; /* filesystem mount point */ | |
749 | xfs_trans_t *tp; /* transaction pointer */ | |
750 | ||
751 | dp = args->dp; | |
752 | ASSERT(dp != NULL); | |
753 | tp = args->trans; | |
754 | mp = dp->i_mount; | |
755 | ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) && | |
756 | bno < XFS_DIR2_FREE_FIRSTDB(mp)); | |
757 | /* | |
758 | * Get the buffer for the block. | |
759 | */ | |
760 | error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, bno), -1, &bp, | |
761 | XFS_DATA_FORK); | |
762 | if (error) { | |
2bd0ea18 NS |
763 | return error; |
764 | } | |
765 | ASSERT(bp != NULL); | |
766 | leaf = bp->data; | |
767 | /* | |
768 | * Initialize the header. | |
769 | */ | |
770 | INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, magic); | |
46eca962 NS |
771 | leaf->hdr.info.forw = 0; |
772 | leaf->hdr.info.back = 0; | |
773 | leaf->hdr.count = 0; | |
774 | leaf->hdr.stale = 0; | |
2bd0ea18 NS |
775 | xfs_dir2_leaf_log_header(tp, bp); |
776 | /* | |
777 | * If it's a leaf-format directory initialize the tail. | |
778 | * In this case our caller has the real bests table to copy into | |
779 | * the block. | |
780 | */ | |
781 | if (magic == XFS_DIR2_LEAF1_MAGIC) { | |
782 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
46eca962 | 783 | ltp->bestcount = 0; |
2bd0ea18 NS |
784 | xfs_dir2_leaf_log_tail(tp, bp); |
785 | } | |
786 | *bpp = bp; | |
787 | return 0; | |
788 | } | |
789 | ||
790 | /* | |
791 | * Log the bests entries indicated from a leaf1 block. | |
792 | */ | |
793 | void | |
794 | xfs_dir2_leaf_log_bests( | |
795 | xfs_trans_t *tp, /* transaction pointer */ | |
796 | xfs_dabuf_t *bp, /* leaf buffer */ | |
797 | int first, /* first entry to log */ | |
798 | int last) /* last entry to log */ | |
799 | { | |
800 | xfs_dir2_data_off_t *firstb; /* pointer to first entry */ | |
801 | xfs_dir2_data_off_t *lastb; /* pointer to last entry */ | |
802 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
803 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
804 | ||
805 | leaf = bp->data; | |
806 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC); | |
807 | ltp = XFS_DIR2_LEAF_TAIL_P(tp->t_mountp, leaf); | |
46eca962 NS |
808 | firstb = XFS_DIR2_LEAF_BESTS_P(ltp) + first; |
809 | lastb = XFS_DIR2_LEAF_BESTS_P(ltp) + last; | |
2bd0ea18 NS |
810 | xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf), |
811 | (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); | |
812 | } | |
813 | ||
814 | /* | |
815 | * Log the leaf entries indicated from a leaf1 or leafn block. | |
816 | */ | |
817 | void | |
818 | xfs_dir2_leaf_log_ents( | |
819 | xfs_trans_t *tp, /* transaction pointer */ | |
820 | xfs_dabuf_t *bp, /* leaf buffer */ | |
821 | int first, /* first entry to log */ | |
822 | int last) /* last entry to log */ | |
823 | { | |
824 | xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ | |
825 | xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ | |
826 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
827 | ||
828 | leaf = bp->data; | |
829 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC || | |
830 | INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC); | |
831 | firstlep = &leaf->ents[first]; | |
832 | lastlep = &leaf->ents[last]; | |
833 | xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf), | |
834 | (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); | |
835 | } | |
836 | ||
837 | /* | |
838 | * Log the header of the leaf1 or leafn block. | |
839 | */ | |
840 | void | |
841 | xfs_dir2_leaf_log_header( | |
842 | xfs_trans_t *tp, /* transaction pointer */ | |
843 | xfs_dabuf_t *bp) /* leaf buffer */ | |
844 | { | |
845 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
846 | ||
847 | leaf = bp->data; | |
848 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC || | |
849 | INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC); | |
850 | xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf), | |
851 | (uint)(sizeof(leaf->hdr) - 1)); | |
852 | } | |
853 | ||
854 | /* | |
855 | * Log the tail of the leaf1 block. | |
856 | */ | |
f302e9e4 | 857 | static void |
2bd0ea18 NS |
858 | xfs_dir2_leaf_log_tail( |
859 | xfs_trans_t *tp, /* transaction pointer */ | |
860 | xfs_dabuf_t *bp) /* leaf buffer */ | |
861 | { | |
862 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
863 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
864 | xfs_mount_t *mp; /* filesystem mount point */ | |
865 | ||
866 | mp = tp->t_mountp; | |
867 | leaf = bp->data; | |
868 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAF1_MAGIC); | |
869 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
870 | xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf), | |
871 | (uint)(mp->m_dirblksize - 1)); | |
872 | } | |
873 | ||
874 | /* | |
875 | * Look up the entry referred to by args in the leaf format directory. | |
876 | * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which | |
877 | * is also used by the node-format code. | |
878 | */ | |
879 | int | |
880 | xfs_dir2_leaf_lookup( | |
881 | xfs_da_args_t *args) /* operation arguments */ | |
882 | { | |
883 | xfs_dabuf_t *dbp; /* data block buffer */ | |
884 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
885 | xfs_inode_t *dp; /* incore directory inode */ | |
886 | int error; /* error return code */ | |
887 | int index; /* found entry index */ | |
888 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
889 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
890 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
891 | xfs_trans_t *tp; /* transaction pointer */ | |
892 | ||
893 | xfs_dir2_trace_args("leaf_lookup", args); | |
894 | /* | |
895 | * Look up name in the leaf block, returning both buffers and index. | |
896 | */ | |
0e266570 | 897 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
2bd0ea18 NS |
898 | return error; |
899 | } | |
900 | tp = args->trans; | |
901 | dp = args->dp; | |
902 | xfs_dir2_leaf_check(dp, lbp); | |
903 | leaf = lbp->data; | |
904 | /* | |
905 | * Get to the leaf entry and contained data entry address. | |
906 | */ | |
907 | lep = &leaf->ents[index]; | |
908 | /* | |
909 | * Point to the data entry. | |
910 | */ | |
911 | dep = (xfs_dir2_data_entry_t *) | |
912 | ((char *)dbp->data + | |
913 | XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT))); | |
914 | /* | |
915 | * Return the found inode number. | |
916 | */ | |
917 | args->inumber = INT_GET(dep->inumber, ARCH_CONVERT); | |
918 | xfs_da_brelse(tp, dbp); | |
919 | xfs_da_brelse(tp, lbp); | |
920 | return XFS_ERROR(EEXIST); | |
921 | } | |
922 | ||
923 | /* | |
924 | * Look up name/hash in the leaf block. | |
925 | * Fill in indexp with the found index, and dbpp with the data buffer. | |
926 | * If not found dbpp will be NULL, and ENOENT comes back. | |
927 | * lbpp will always be filled in with the leaf buffer unless there's an error. | |
928 | */ | |
929 | STATIC int /* error */ | |
930 | xfs_dir2_leaf_lookup_int( | |
931 | xfs_da_args_t *args, /* operation arguments */ | |
932 | xfs_dabuf_t **lbpp, /* out: leaf buffer */ | |
933 | int *indexp, /* out: index in leaf block */ | |
934 | xfs_dabuf_t **dbpp) /* out: data buffer */ | |
935 | { | |
936 | xfs_dir2_db_t curdb; /* current data block number */ | |
937 | xfs_dabuf_t *dbp; /* data buffer */ | |
938 | xfs_dir2_data_entry_t *dep; /* data entry */ | |
939 | xfs_inode_t *dp; /* incore directory inode */ | |
940 | int error; /* error return code */ | |
941 | int index; /* index in leaf block */ | |
942 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
943 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
944 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
945 | xfs_mount_t *mp; /* filesystem mount point */ | |
946 | xfs_dir2_db_t newdb; /* new data block number */ | |
947 | xfs_trans_t *tp; /* transaction pointer */ | |
948 | ||
949 | dp = args->dp; | |
950 | tp = args->trans; | |
951 | mp = dp->i_mount; | |
952 | /* | |
953 | * Read the leaf block into the buffer. | |
954 | */ | |
0e266570 | 955 | if ((error = |
2bd0ea18 | 956 | xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, |
0e266570 | 957 | XFS_DATA_FORK))) { |
2bd0ea18 NS |
958 | return error; |
959 | } | |
960 | *lbpp = lbp; | |
961 | leaf = lbp->data; | |
962 | xfs_dir2_leaf_check(dp, lbp); | |
963 | /* | |
964 | * Look for the first leaf entry with our hash value. | |
965 | */ | |
966 | index = xfs_dir2_leaf_search_hash(args, lbp); | |
967 | /* | |
968 | * Loop over all the entries with the right hash value | |
969 | * looking to match the name. | |
970 | */ | |
971 | for (lep = &leaf->ents[index], dbp = NULL, curdb = -1; | |
972 | index < INT_GET(leaf->hdr.count, ARCH_CONVERT) && INT_GET(lep->hashval, ARCH_CONVERT) == args->hashval; | |
973 | lep++, index++) { | |
974 | /* | |
975 | * Skip over stale leaf entries. | |
976 | */ | |
977 | if (INT_GET(lep->address, ARCH_CONVERT) == XFS_DIR2_NULL_DATAPTR) | |
978 | continue; | |
979 | /* | |
980 | * Get the new data block number. | |
981 | */ | |
982 | newdb = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT)); | |
983 | /* | |
984 | * If it's not the same as the old data block number, | |
985 | * need to pitch the old one and read the new one. | |
986 | */ | |
987 | if (newdb != curdb) { | |
988 | if (dbp) | |
989 | xfs_da_brelse(tp, dbp); | |
0e266570 | 990 | if ((error = |
2bd0ea18 NS |
991 | xfs_da_read_buf(tp, dp, |
992 | XFS_DIR2_DB_TO_DA(mp, newdb), -1, &dbp, | |
0e266570 | 993 | XFS_DATA_FORK))) { |
2bd0ea18 NS |
994 | xfs_da_brelse(tp, lbp); |
995 | return error; | |
996 | } | |
997 | xfs_dir2_data_check(dp, dbp); | |
998 | curdb = newdb; | |
999 | } | |
1000 | /* | |
1001 | * Point to the data entry. | |
1002 | */ | |
1003 | dep = (xfs_dir2_data_entry_t *) | |
1004 | ((char *)dbp->data + | |
1005 | XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT))); | |
1006 | /* | |
1007 | * If it matches then return it. | |
1008 | */ | |
1009 | if (dep->namelen == args->namelen && | |
1010 | dep->name[0] == args->name[0] && | |
32181a02 | 1011 | memcmp(dep->name, args->name, args->namelen) == 0) { |
2bd0ea18 NS |
1012 | *dbpp = dbp; |
1013 | *indexp = index; | |
1014 | return 0; | |
1015 | } | |
1016 | } | |
1017 | /* | |
1018 | * No match found, return ENOENT. | |
1019 | */ | |
1020 | ASSERT(args->oknoent); | |
1021 | if (dbp) | |
1022 | xfs_da_brelse(tp, dbp); | |
1023 | xfs_da_brelse(tp, lbp); | |
1024 | return XFS_ERROR(ENOENT); | |
1025 | } | |
1026 | ||
1027 | /* | |
1028 | * Remove an entry from a leaf format directory. | |
1029 | */ | |
1030 | int /* error */ | |
1031 | xfs_dir2_leaf_removename( | |
1032 | xfs_da_args_t *args) /* operation arguments */ | |
1033 | { | |
1034 | xfs_dir2_data_off_t *bestsp; /* leaf block best freespace */ | |
1035 | xfs_dir2_data_t *data; /* data block structure */ | |
1036 | xfs_dir2_db_t db; /* data block number */ | |
1037 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1038 | xfs_dir2_data_entry_t *dep; /* data entry structure */ | |
1039 | xfs_inode_t *dp; /* incore directory inode */ | |
1040 | int error; /* error return code */ | |
1041 | xfs_dir2_db_t i; /* temporary data block # */ | |
1042 | int index; /* index into leaf entries */ | |
1043 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1044 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1045 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1046 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1047 | xfs_mount_t *mp; /* filesystem mount point */ | |
1048 | int needlog; /* need to log data header */ | |
1049 | int needscan; /* need to rescan data frees */ | |
1050 | xfs_dir2_data_off_t oldbest; /* old value of best free */ | |
1051 | xfs_trans_t *tp; /* transaction pointer */ | |
1052 | ||
1053 | xfs_dir2_trace_args("leaf_removename", args); | |
1054 | /* | |
1055 | * Lookup the leaf entry, get the leaf and data blocks read in. | |
1056 | */ | |
0e266570 | 1057 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
2bd0ea18 NS |
1058 | return error; |
1059 | } | |
1060 | dp = args->dp; | |
1061 | tp = args->trans; | |
1062 | mp = dp->i_mount; | |
1063 | leaf = lbp->data; | |
1064 | data = dbp->data; | |
1065 | xfs_dir2_data_check(dp, dbp); | |
1066 | /* | |
1067 | * Point to the leaf entry, use that to point to the data entry. | |
1068 | */ | |
1069 | lep = &leaf->ents[index]; | |
1070 | db = XFS_DIR2_DATAPTR_TO_DB(mp, INT_GET(lep->address, ARCH_CONVERT)); | |
1071 | dep = (xfs_dir2_data_entry_t *) | |
1072 | ((char *)data + XFS_DIR2_DATAPTR_TO_OFF(mp, INT_GET(lep->address, ARCH_CONVERT))); | |
1073 | needscan = needlog = 0; | |
1074 | oldbest = INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT); | |
1075 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
46eca962 | 1076 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
2bd0ea18 NS |
1077 | ASSERT(INT_GET(bestsp[db], ARCH_CONVERT) == oldbest); |
1078 | /* | |
1079 | * Mark the former data entry unused. | |
1080 | */ | |
1081 | xfs_dir2_data_make_free(tp, dbp, | |
1082 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)data), | |
1083 | XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan); | |
1084 | /* | |
1085 | * We just mark the leaf entry stale by putting a null in it. | |
1086 | */ | |
1087 | INT_MOD(leaf->hdr.stale, ARCH_CONVERT, +1); | |
1088 | xfs_dir2_leaf_log_header(tp, lbp); | |
1089 | INT_SET(lep->address, ARCH_CONVERT, XFS_DIR2_NULL_DATAPTR); | |
1090 | xfs_dir2_leaf_log_ents(tp, lbp, index, index); | |
1091 | /* | |
1092 | * Scan the freespace in the data block again if necessary, | |
1093 | * log the data block header if necessary. | |
1094 | */ | |
1095 | if (needscan) | |
1096 | xfs_dir2_data_freescan(mp, data, &needlog, NULL); | |
1097 | if (needlog) | |
1098 | xfs_dir2_data_log_header(tp, dbp); | |
1099 | /* | |
1100 | * If the longest freespace in the data block has changed, | |
1101 | * put the new value in the bests table and log that. | |
1102 | */ | |
1103 | if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) != oldbest) { | |
1104 | INT_COPY(bestsp[db], data->hdr.bestfree[0].length, ARCH_CONVERT); | |
1105 | xfs_dir2_leaf_log_bests(tp, lbp, db, db); | |
1106 | } | |
1107 | xfs_dir2_data_check(dp, dbp); | |
1108 | /* | |
1109 | * If the data block is now empty then get rid of the data block. | |
1110 | */ | |
1111 | if (INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) == | |
1112 | mp->m_dirblksize - (uint)sizeof(data->hdr)) { | |
2bd0ea18 | 1113 | ASSERT(db != mp->m_dirdatablk); |
0e266570 | 1114 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
2bd0ea18 NS |
1115 | /* |
1116 | * Nope, can't get rid of it because it caused | |
1117 | * allocation of a bmap btree block to do so. | |
1118 | * Just go on, returning success, leaving the | |
1119 | * empty block in place. | |
1120 | */ | |
1121 | if (error == ENOSPC && args->total == 0) { | |
1122 | xfs_da_buf_done(dbp); | |
1123 | error = 0; | |
1124 | } | |
1125 | xfs_dir2_leaf_check(dp, lbp); | |
1126 | xfs_da_buf_done(lbp); | |
1127 | return error; | |
1128 | } | |
1129 | dbp = NULL; | |
1130 | /* | |
1131 | * If this is the last data block then compact the | |
1132 | * bests table by getting rid of entries. | |
1133 | */ | |
1134 | if (db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1) { | |
1135 | /* | |
1136 | * Look for the last active entry (i). | |
1137 | */ | |
1138 | for (i = db - 1; i > 0; i--) { | |
1139 | if (INT_GET(bestsp[i], ARCH_CONVERT) != NULLDATAOFF) | |
1140 | break; | |
1141 | } | |
1142 | /* | |
1143 | * Copy the table down so inactive entries at the | |
1144 | * end are removed. | |
1145 | */ | |
32181a02 | 1146 | memmove(&bestsp[db - i], bestsp, |
2bd0ea18 NS |
1147 | (INT_GET(ltp->bestcount, ARCH_CONVERT) - (db - i)) * sizeof(*bestsp)); |
1148 | INT_MOD(ltp->bestcount, ARCH_CONVERT, -(db - i)); | |
1149 | xfs_dir2_leaf_log_tail(tp, lbp); | |
1150 | xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1); | |
1151 | } else | |
1152 | INT_SET(bestsp[db], ARCH_CONVERT, NULLDATAOFF); | |
1153 | } | |
1154 | /* | |
1155 | * If the data block was not the first one, drop it. | |
1156 | */ | |
1157 | else if (db != mp->m_dirdatablk && dbp != NULL) { | |
1158 | xfs_da_buf_done(dbp); | |
1159 | dbp = NULL; | |
1160 | } | |
1161 | xfs_dir2_leaf_check(dp, lbp); | |
1162 | /* | |
1163 | * See if we can convert to block form. | |
1164 | */ | |
1165 | return xfs_dir2_leaf_to_block(args, lbp, dbp); | |
1166 | } | |
1167 | ||
1168 | /* | |
1169 | * Replace the inode number in a leaf format directory entry. | |
1170 | */ | |
1171 | int /* error */ | |
1172 | xfs_dir2_leaf_replace( | |
1173 | xfs_da_args_t *args) /* operation arguments */ | |
1174 | { | |
1175 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1176 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
1177 | xfs_inode_t *dp; /* incore directory inode */ | |
1178 | int error; /* error return code */ | |
1179 | int index; /* index of leaf entry */ | |
1180 | xfs_dabuf_t *lbp; /* leaf buffer */ | |
1181 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1182 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1183 | xfs_trans_t *tp; /* transaction pointer */ | |
1184 | ||
1185 | xfs_dir2_trace_args("leaf_replace", args); | |
1186 | /* | |
1187 | * Look up the entry. | |
1188 | */ | |
0e266570 | 1189 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
2bd0ea18 NS |
1190 | return error; |
1191 | } | |
1192 | dp = args->dp; | |
1193 | leaf = lbp->data; | |
1194 | /* | |
1195 | * Point to the leaf entry, get data address from it. | |
1196 | */ | |
1197 | lep = &leaf->ents[index]; | |
1198 | /* | |
1199 | * Point to the data entry. | |
1200 | */ | |
1201 | dep = (xfs_dir2_data_entry_t *) | |
1202 | ((char *)dbp->data + | |
1203 | XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, INT_GET(lep->address, ARCH_CONVERT))); | |
1204 | ASSERT(args->inumber != INT_GET(dep->inumber, ARCH_CONVERT)); | |
1205 | /* | |
1206 | * Put the new inode number in, log it. | |
1207 | */ | |
1208 | INT_SET(dep->inumber, ARCH_CONVERT, args->inumber); | |
1209 | tp = args->trans; | |
1210 | xfs_dir2_data_log_entry(tp, dbp, dep); | |
1211 | xfs_da_buf_done(dbp); | |
1212 | xfs_dir2_leaf_check(dp, lbp); | |
1213 | xfs_da_brelse(tp, lbp); | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | /* | |
1218 | * Return index in the leaf block (lbp) which is either the first | |
1219 | * one with this hash value, or if there are none, the insert point | |
1220 | * for that hash value. | |
1221 | */ | |
1222 | int /* index value */ | |
1223 | xfs_dir2_leaf_search_hash( | |
1224 | xfs_da_args_t *args, /* operation arguments */ | |
1225 | xfs_dabuf_t *lbp) /* leaf buffer */ | |
1226 | { | |
0e266570 | 1227 | xfs_dahash_t hash=0; /* hash from this entry */ |
2bd0ea18 NS |
1228 | xfs_dahash_t hashwant; /* hash value looking for */ |
1229 | int high; /* high leaf index */ | |
1230 | int low; /* low leaf index */ | |
1231 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1232 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
0e266570 | 1233 | int mid=0; /* current leaf index */ |
2bd0ea18 NS |
1234 | |
1235 | leaf = lbp->data; | |
1236 | #ifndef __KERNEL__ | |
46eca962 | 1237 | if (!leaf->hdr.count) |
2bd0ea18 NS |
1238 | return 0; |
1239 | #endif | |
1240 | /* | |
1241 | * Note, the table cannot be empty, so we have to go through the loop. | |
1242 | * Binary search the leaf entries looking for our hash value. | |
1243 | */ | |
1244 | for (lep = leaf->ents, low = 0, high = INT_GET(leaf->hdr.count, ARCH_CONVERT) - 1, | |
1245 | hashwant = args->hashval; | |
1246 | low <= high; ) { | |
1247 | mid = (low + high) >> 1; | |
1248 | if ((hash = INT_GET(lep[mid].hashval, ARCH_CONVERT)) == hashwant) | |
1249 | break; | |
1250 | if (hash < hashwant) | |
1251 | low = mid + 1; | |
1252 | else | |
1253 | high = mid - 1; | |
1254 | } | |
1255 | /* | |
1256 | * Found one, back up through all the equal hash values. | |
1257 | */ | |
1258 | if (hash == hashwant) { | |
1259 | while (mid > 0 && INT_GET(lep[mid - 1].hashval, ARCH_CONVERT) == hashwant) { | |
2bd0ea18 NS |
1260 | mid--; |
1261 | } | |
1262 | } | |
1263 | /* | |
1264 | * Need to point to an entry higher than ours. | |
1265 | */ | |
1266 | else if (hash < hashwant) | |
1267 | mid++; | |
1268 | return mid; | |
1269 | } | |
1270 | ||
1271 | /* | |
1272 | * Trim off a trailing data block. We know it's empty since the leaf | |
1273 | * freespace table says so. | |
1274 | */ | |
1275 | int /* error */ | |
1276 | xfs_dir2_leaf_trim_data( | |
1277 | xfs_da_args_t *args, /* operation arguments */ | |
1278 | xfs_dabuf_t *lbp, /* leaf buffer */ | |
1279 | xfs_dir2_db_t db) /* data block number */ | |
1280 | { | |
1281 | xfs_dir2_data_off_t *bestsp; /* leaf bests table */ | |
1282 | #ifdef DEBUG | |
1283 | xfs_dir2_data_t *data; /* data block structure */ | |
1284 | #endif | |
1285 | xfs_dabuf_t *dbp; /* data block buffer */ | |
1286 | xfs_inode_t *dp; /* incore directory inode */ | |
1287 | int error; /* error return value */ | |
1288 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1289 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1290 | xfs_mount_t *mp; /* filesystem mount point */ | |
1291 | xfs_trans_t *tp; /* transaction pointer */ | |
1292 | ||
1293 | dp = args->dp; | |
1294 | mp = dp->i_mount; | |
1295 | tp = args->trans; | |
1296 | /* | |
1297 | * Read the offending data block. We need its buffer. | |
1298 | */ | |
0e266570 NS |
1299 | if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, db), -1, &dbp, |
1300 | XFS_DATA_FORK))) { | |
2bd0ea18 NS |
1301 | return error; |
1302 | } | |
1303 | #ifdef DEBUG | |
1304 | data = dbp->data; | |
1305 | ASSERT(INT_GET(data->hdr.magic, ARCH_CONVERT) == XFS_DIR2_DATA_MAGIC); | |
1306 | #endif | |
5000d01d | 1307 | /* this seems to be an error |
2bd0ea18 NS |
1308 | * data is only valid if DEBUG is defined? |
1309 | * RMC 09/08/1999 | |
1310 | */ | |
1311 | ||
1312 | leaf = lbp->data; | |
1313 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
1314 | ASSERT(INT_GET(data->hdr.bestfree[0].length, ARCH_CONVERT) == | |
1315 | mp->m_dirblksize - (uint)sizeof(data->hdr)); | |
1316 | ASSERT(db == INT_GET(ltp->bestcount, ARCH_CONVERT) - 1); | |
1317 | /* | |
1318 | * Get rid of the data block. | |
1319 | */ | |
0e266570 | 1320 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
2bd0ea18 NS |
1321 | ASSERT(error != ENOSPC); |
1322 | xfs_da_brelse(tp, dbp); | |
1323 | return error; | |
1324 | } | |
1325 | /* | |
1326 | * Eliminate the last bests entry from the table. | |
1327 | */ | |
46eca962 | 1328 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
2bd0ea18 | 1329 | INT_MOD(ltp->bestcount, ARCH_CONVERT, -1); |
32181a02 | 1330 | memmove(&bestsp[1], &bestsp[0], INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(*bestsp)); |
2bd0ea18 NS |
1331 | xfs_dir2_leaf_log_tail(tp, lbp); |
1332 | xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1); | |
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | /* | |
1337 | * Convert node form directory to leaf form directory. | |
1338 | * The root of the node form dir needs to already be a LEAFN block. | |
1339 | * Just return if we can't do anything. | |
1340 | */ | |
1341 | int /* error */ | |
1342 | xfs_dir2_node_to_leaf( | |
1343 | xfs_da_state_t *state) /* directory operation state */ | |
1344 | { | |
1345 | xfs_da_args_t *args; /* operation arguments */ | |
1346 | xfs_inode_t *dp; /* incore directory inode */ | |
1347 | int error; /* error return code */ | |
1348 | xfs_dabuf_t *fbp; /* buffer for freespace block */ | |
1349 | xfs_fileoff_t fo; /* freespace file offset */ | |
1350 | xfs_dir2_free_t *free; /* freespace structure */ | |
1351 | xfs_dabuf_t *lbp; /* buffer for leaf block */ | |
1352 | xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ | |
1353 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1354 | xfs_mount_t *mp; /* filesystem mount point */ | |
1355 | int rval; /* successful free trim? */ | |
1356 | xfs_trans_t *tp; /* transaction pointer */ | |
1357 | ||
1358 | /* | |
1359 | * There's more than a leaf level in the btree, so there must | |
1360 | * be multiple leafn blocks. Give up. | |
1361 | */ | |
1362 | if (state->path.active > 1) | |
1363 | return 0; | |
1364 | args = state->args; | |
1365 | xfs_dir2_trace_args("node_to_leaf", args); | |
1366 | mp = state->mp; | |
1367 | dp = args->dp; | |
1368 | tp = args->trans; | |
1369 | /* | |
1370 | * Get the last offset in the file. | |
1371 | */ | |
0e266570 | 1372 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) { |
2bd0ea18 NS |
1373 | return error; |
1374 | } | |
1375 | fo -= mp->m_dirblkfsbs; | |
1376 | /* | |
1377 | * If there are freespace blocks other than the first one, | |
1378 | * take this opportunity to remove trailing empty freespace blocks | |
1379 | * that may have been left behind during no-space-reservation | |
1380 | * operations. | |
1381 | */ | |
1382 | while (fo > mp->m_dirfreeblk) { | |
0e266570 | 1383 | if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { |
2bd0ea18 NS |
1384 | return error; |
1385 | } | |
1386 | if (rval) | |
1387 | fo -= mp->m_dirblkfsbs; | |
1388 | else | |
1389 | return 0; | |
1390 | } | |
1391 | /* | |
1392 | * Now find the block just before the freespace block. | |
1393 | */ | |
0e266570 | 1394 | if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { |
2bd0ea18 NS |
1395 | return error; |
1396 | } | |
1397 | /* | |
1398 | * If it's not the single leaf block, give up. | |
1399 | */ | |
1400 | if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize) | |
1401 | return 0; | |
1402 | lbp = state->path.blk[0].bp; | |
1403 | leaf = lbp->data; | |
1404 | ASSERT(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC); | |
1405 | /* | |
1406 | * Read the freespace block. | |
1407 | */ | |
0e266570 NS |
1408 | if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp, |
1409 | XFS_DATA_FORK))) { | |
2bd0ea18 NS |
1410 | return error; |
1411 | } | |
1412 | free = fbp->data; | |
1413 | ASSERT(INT_GET(free->hdr.magic, ARCH_CONVERT) == XFS_DIR2_FREE_MAGIC); | |
46eca962 | 1414 | ASSERT(!free->hdr.firstdb); |
2bd0ea18 NS |
1415 | /* |
1416 | * Now see if the leafn and free data will fit in a leaf1. | |
1417 | * If not, release the buffer and give up. | |
1418 | */ | |
1419 | if ((uint)sizeof(leaf->hdr) + | |
1420 | (INT_GET(leaf->hdr.count, ARCH_CONVERT) - INT_GET(leaf->hdr.stale, ARCH_CONVERT)) * (uint)sizeof(leaf->ents[0]) + | |
1421 | INT_GET(free->hdr.nvalid, ARCH_CONVERT) * (uint)sizeof(leaf->bests[0]) + | |
1422 | (uint)sizeof(leaf->tail) > | |
1423 | mp->m_dirblksize) { | |
1424 | xfs_da_brelse(tp, fbp); | |
1425 | return 0; | |
1426 | } | |
1427 | /* | |
1428 | * If the leaf has any stale entries in it, compress them out. | |
1429 | * The compact routine will log the header. | |
1430 | */ | |
1431 | if (INT_GET(leaf->hdr.stale, ARCH_CONVERT)) | |
1432 | xfs_dir2_leaf_compact(args, lbp); | |
1433 | else | |
1434 | xfs_dir2_leaf_log_header(tp, lbp); | |
1435 | INT_SET(leaf->hdr.info.magic, ARCH_CONVERT, XFS_DIR2_LEAF1_MAGIC); | |
1436 | /* | |
1437 | * Set up the leaf tail from the freespace block. | |
1438 | */ | |
1439 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); | |
1440 | INT_COPY(ltp->bestcount, free->hdr.nvalid, ARCH_CONVERT); | |
1441 | /* | |
1442 | * Set up the leaf bests table. | |
1443 | */ | |
46eca962 | 1444 | memcpy(XFS_DIR2_LEAF_BESTS_P(ltp), free->bests, |
2bd0ea18 NS |
1445 | INT_GET(ltp->bestcount, ARCH_CONVERT) * sizeof(leaf->bests[0])); |
1446 | xfs_dir2_leaf_log_bests(tp, lbp, 0, INT_GET(ltp->bestcount, ARCH_CONVERT) - 1); | |
1447 | xfs_dir2_leaf_log_tail(tp, lbp); | |
1448 | xfs_dir2_leaf_check(dp, lbp); | |
1449 | /* | |
1450 | * Get rid of the freespace block. | |
1451 | */ | |
1452 | error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp); | |
1453 | if (error) { | |
2bd0ea18 NS |
1454 | /* |
1455 | * This can't fail here because it can only happen when | |
1456 | * punching out the middle of an extent, and this is an | |
1457 | * isolated block. | |
1458 | */ | |
1459 | ASSERT(error != ENOSPC); | |
1460 | return error; | |
1461 | } | |
1462 | fbp = NULL; | |
1463 | /* | |
1464 | * Now see if we can convert the single-leaf directory | |
1465 | * down to a block form directory. | |
1466 | * This routine always kills the dabuf for the leaf, so | |
1467 | * eliminate it from the path. | |
1468 | */ | |
1469 | error = xfs_dir2_leaf_to_block(args, lbp, NULL); | |
1470 | state->path.blk[0].bp = NULL; | |
1471 | return error; | |
1472 | } |