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