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