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