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