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