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