Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
cbc8adf8 | 4 | * Copyright (c) 2013 Red Hat, Inc. |
7b718769 | 5 | * All Rights Reserved. |
1da177e4 | 6 | */ |
1da177e4 | 7 | #include "xfs.h" |
a844f451 | 8 | #include "xfs_fs.h" |
5467b34b | 9 | #include "xfs_shared.h" |
a4fbe6ab | 10 | #include "xfs_format.h" |
239880ef DC |
11 | #include "xfs_log_format.h" |
12 | #include "xfs_trans_resv.h" | |
1da177e4 | 13 | #include "xfs_mount.h" |
1da177e4 LT |
14 | #include "xfs_inode.h" |
15 | #include "xfs_bmap.h" | |
2b9ab5ab | 16 | #include "xfs_dir2.h" |
57926640 | 17 | #include "xfs_dir2_priv.h" |
1da177e4 | 18 | #include "xfs_error.h" |
0b1b213f | 19 | #include "xfs_trace.h" |
239880ef | 20 | #include "xfs_trans.h" |
cbc8adf8 | 21 | #include "xfs_buf_item.h" |
a45086e2 | 22 | #include "xfs_log.h" |
1da177e4 LT |
23 | |
24 | /* | |
25 | * Function declarations. | |
26 | */ | |
1d9025e5 DC |
27 | static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args, |
28 | int index); | |
1da177e4 LT |
29 | static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, |
30 | xfs_da_state_blk_t *blk1, | |
31 | xfs_da_state_blk_t *blk2); | |
1d9025e5 | 32 | static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp, |
1da177e4 LT |
33 | int index, xfs_da_state_blk_t *dblk, |
34 | int *rval); | |
1da177e4 | 35 | |
24df33b4 DC |
36 | /* |
37 | * Check internal consistency of a leafn block. | |
38 | */ | |
39 | #ifdef DEBUG | |
a6a781a5 | 40 | static xfs_failaddr_t |
24df33b4 | 41 | xfs_dir3_leafn_check( |
4141956a | 42 | struct xfs_inode *dp, |
24df33b4 DC |
43 | struct xfs_buf *bp) |
44 | { | |
45 | struct xfs_dir2_leaf *leaf = bp->b_addr; | |
46 | struct xfs_dir3_icleaf_hdr leafhdr; | |
47 | ||
51842556 | 48 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
24df33b4 DC |
49 | |
50 | if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) { | |
51 | struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr; | |
52 | if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn) | |
a6a781a5 | 53 | return __this_address; |
24df33b4 | 54 | } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC) |
a6a781a5 | 55 | return __this_address; |
24df33b4 | 56 | |
5ba30919 | 57 | return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf); |
24df33b4 | 58 | } |
a6a781a5 DW |
59 | |
60 | static inline void | |
61 | xfs_dir3_leaf_check( | |
62 | struct xfs_inode *dp, | |
63 | struct xfs_buf *bp) | |
64 | { | |
65 | xfs_failaddr_t fa; | |
66 | ||
67 | fa = xfs_dir3_leafn_check(dp, bp); | |
68 | if (!fa) | |
69 | return; | |
70 | xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, | |
2551a530 DW |
71 | bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, |
72 | fa); | |
a6a781a5 DW |
73 | ASSERT(0); |
74 | } | |
24df33b4 | 75 | #else |
4141956a | 76 | #define xfs_dir3_leaf_check(dp, bp) |
24df33b4 DC |
77 | #endif |
78 | ||
a6a781a5 | 79 | static xfs_failaddr_t |
cbc8adf8 | 80 | xfs_dir3_free_verify( |
2025207c DC |
81 | struct xfs_buf *bp) |
82 | { | |
dbd329f1 | 83 | struct xfs_mount *mp = bp->b_mount; |
2025207c | 84 | struct xfs_dir2_free_hdr *hdr = bp->b_addr; |
2025207c | 85 | |
39708c20 BF |
86 | if (!xfs_verify_magic(bp, hdr->magic)) |
87 | return __this_address; | |
88 | ||
cbc8adf8 DC |
89 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
90 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; | |
91 | ||
ce748eaa | 92 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) |
a6a781a5 | 93 | return __this_address; |
cbc8adf8 | 94 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
a6a781a5 | 95 | return __this_address; |
a45086e2 | 96 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) |
a6a781a5 | 97 | return __this_address; |
2025207c | 98 | } |
cbc8adf8 DC |
99 | |
100 | /* XXX: should bounds check the xfs_dir3_icfree_hdr here */ | |
101 | ||
a6a781a5 | 102 | return NULL; |
612cfbfe | 103 | } |
2025207c | 104 | |
612cfbfe | 105 | static void |
cbc8adf8 | 106 | xfs_dir3_free_read_verify( |
612cfbfe DC |
107 | struct xfs_buf *bp) |
108 | { | |
dbd329f1 | 109 | struct xfs_mount *mp = bp->b_mount; |
bc1a09b8 | 110 | xfs_failaddr_t fa; |
cbc8adf8 | 111 | |
ce5028cf ES |
112 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
113 | !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF)) | |
bc1a09b8 DW |
114 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
115 | else { | |
116 | fa = xfs_dir3_free_verify(bp); | |
117 | if (fa) | |
118 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
119 | } | |
612cfbfe DC |
120 | } |
121 | ||
1813dd64 | 122 | static void |
cbc8adf8 | 123 | xfs_dir3_free_write_verify( |
612cfbfe DC |
124 | struct xfs_buf *bp) |
125 | { | |
dbd329f1 | 126 | struct xfs_mount *mp = bp->b_mount; |
fb1755a6 | 127 | struct xfs_buf_log_item *bip = bp->b_log_item; |
cbc8adf8 | 128 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
bc1a09b8 | 129 | xfs_failaddr_t fa; |
cbc8adf8 | 130 | |
bc1a09b8 DW |
131 | fa = xfs_dir3_free_verify(bp); |
132 | if (fa) { | |
133 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); | |
cbc8adf8 DC |
134 | return; |
135 | } | |
136 | ||
137 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
138 | return; | |
139 | ||
140 | if (bip) | |
141 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
142 | ||
f1dbcd7e | 143 | xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF); |
2025207c DC |
144 | } |
145 | ||
d75afeb3 | 146 | const struct xfs_buf_ops xfs_dir3_free_buf_ops = { |
233135b7 | 147 | .name = "xfs_dir3_free", |
39708c20 BF |
148 | .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC), |
149 | cpu_to_be32(XFS_DIR3_FREE_MAGIC) }, | |
cbc8adf8 DC |
150 | .verify_read = xfs_dir3_free_read_verify, |
151 | .verify_write = xfs_dir3_free_write_verify, | |
b5572597 | 152 | .verify_struct = xfs_dir3_free_verify, |
1813dd64 DC |
153 | }; |
154 | ||
de14c5f5 | 155 | /* Everything ok in the free block header? */ |
bc1a09b8 | 156 | static xfs_failaddr_t |
de14c5f5 DW |
157 | xfs_dir3_free_header_check( |
158 | struct xfs_inode *dp, | |
159 | xfs_dablk_t fbno, | |
160 | struct xfs_buf *bp) | |
161 | { | |
162 | struct xfs_mount *mp = dp->i_mount; | |
163 | unsigned int firstdb; | |
164 | int maxbests; | |
165 | ||
166 | maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo); | |
167 | firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) - | |
168 | xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) * | |
169 | maxbests; | |
170 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
171 | struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; | |
172 | ||
173 | if (be32_to_cpu(hdr3->firstdb) != firstdb) | |
a6a781a5 | 174 | return __this_address; |
de14c5f5 | 175 | if (be32_to_cpu(hdr3->nvalid) > maxbests) |
a6a781a5 | 176 | return __this_address; |
de14c5f5 | 177 | if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused)) |
a6a781a5 | 178 | return __this_address; |
de14c5f5 DW |
179 | } else { |
180 | struct xfs_dir2_free_hdr *hdr = bp->b_addr; | |
181 | ||
182 | if (be32_to_cpu(hdr->firstdb) != firstdb) | |
a6a781a5 | 183 | return __this_address; |
de14c5f5 | 184 | if (be32_to_cpu(hdr->nvalid) > maxbests) |
a6a781a5 | 185 | return __this_address; |
de14c5f5 | 186 | if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused)) |
a6a781a5 | 187 | return __this_address; |
de14c5f5 | 188 | } |
a6a781a5 | 189 | return NULL; |
de14c5f5 | 190 | } |
612cfbfe | 191 | |
2025207c | 192 | static int |
cbc8adf8 | 193 | __xfs_dir3_free_read( |
2025207c DC |
194 | struct xfs_trans *tp, |
195 | struct xfs_inode *dp, | |
196 | xfs_dablk_t fbno, | |
197 | xfs_daddr_t mappedbno, | |
198 | struct xfs_buf **bpp) | |
199 | { | |
bc1a09b8 | 200 | xfs_failaddr_t fa; |
d75afeb3 DC |
201 | int err; |
202 | ||
203 | err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp, | |
cbc8adf8 | 204 | XFS_DATA_FORK, &xfs_dir3_free_buf_ops); |
de14c5f5 DW |
205 | if (err || !*bpp) |
206 | return err; | |
207 | ||
208 | /* Check things that we can't do in the verifier. */ | |
bc1a09b8 DW |
209 | fa = xfs_dir3_free_header_check(dp, fbno, *bpp); |
210 | if (fa) { | |
211 | xfs_verifier_error(*bpp, -EFSCORRUPTED, fa); | |
de14c5f5 DW |
212 | xfs_trans_brelse(tp, *bpp); |
213 | return -EFSCORRUPTED; | |
214 | } | |
d75afeb3 DC |
215 | |
216 | /* try read returns without an error or *bpp if it lands in a hole */ | |
de14c5f5 | 217 | if (tp) |
61fe135c | 218 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF); |
de14c5f5 DW |
219 | |
220 | return 0; | |
2025207c DC |
221 | } |
222 | ||
5ba30919 CH |
223 | void |
224 | xfs_dir2_free_hdr_from_disk( | |
225 | struct xfs_mount *mp, | |
226 | struct xfs_dir3_icfree_hdr *to, | |
227 | struct xfs_dir2_free *from) | |
228 | { | |
229 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
230 | struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from; | |
231 | ||
232 | to->magic = be32_to_cpu(from3->hdr.hdr.magic); | |
233 | to->firstdb = be32_to_cpu(from3->hdr.firstdb); | |
234 | to->nvalid = be32_to_cpu(from3->hdr.nvalid); | |
235 | to->nused = be32_to_cpu(from3->hdr.nused); | |
a84f3d5c | 236 | to->bests = from3->bests; |
5ba30919 CH |
237 | |
238 | ASSERT(to->magic == XFS_DIR3_FREE_MAGIC); | |
239 | } else { | |
240 | to->magic = be32_to_cpu(from->hdr.magic); | |
241 | to->firstdb = be32_to_cpu(from->hdr.firstdb); | |
242 | to->nvalid = be32_to_cpu(from->hdr.nvalid); | |
243 | to->nused = be32_to_cpu(from->hdr.nused); | |
a84f3d5c CH |
244 | to->bests = from->bests; |
245 | ||
5ba30919 CH |
246 | ASSERT(to->magic == XFS_DIR2_FREE_MAGIC); |
247 | } | |
248 | } | |
249 | ||
200dada7 CH |
250 | static void |
251 | xfs_dir2_free_hdr_to_disk( | |
252 | struct xfs_mount *mp, | |
253 | struct xfs_dir2_free *to, | |
254 | struct xfs_dir3_icfree_hdr *from) | |
255 | { | |
256 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
257 | struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to; | |
258 | ||
259 | ASSERT(from->magic == XFS_DIR3_FREE_MAGIC); | |
260 | ||
261 | to3->hdr.hdr.magic = cpu_to_be32(from->magic); | |
262 | to3->hdr.firstdb = cpu_to_be32(from->firstdb); | |
263 | to3->hdr.nvalid = cpu_to_be32(from->nvalid); | |
264 | to3->hdr.nused = cpu_to_be32(from->nused); | |
265 | } else { | |
266 | ASSERT(from->magic == XFS_DIR2_FREE_MAGIC); | |
267 | ||
268 | to->hdr.magic = cpu_to_be32(from->magic); | |
269 | to->hdr.firstdb = cpu_to_be32(from->firstdb); | |
270 | to->hdr.nvalid = cpu_to_be32(from->nvalid); | |
271 | to->hdr.nused = cpu_to_be32(from->nused); | |
272 | } | |
273 | } | |
274 | ||
2025207c DC |
275 | int |
276 | xfs_dir2_free_read( | |
277 | struct xfs_trans *tp, | |
278 | struct xfs_inode *dp, | |
279 | xfs_dablk_t fbno, | |
280 | struct xfs_buf **bpp) | |
281 | { | |
cbc8adf8 | 282 | return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp); |
2025207c DC |
283 | } |
284 | ||
285 | static int | |
286 | xfs_dir2_free_try_read( | |
287 | struct xfs_trans *tp, | |
288 | struct xfs_inode *dp, | |
289 | xfs_dablk_t fbno, | |
290 | struct xfs_buf **bpp) | |
291 | { | |
cbc8adf8 DC |
292 | return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp); |
293 | } | |
294 | ||
cbc8adf8 DC |
295 | static int |
296 | xfs_dir3_free_get_buf( | |
2998ab1d | 297 | xfs_da_args_t *args, |
cbc8adf8 DC |
298 | xfs_dir2_db_t fbno, |
299 | struct xfs_buf **bpp) | |
300 | { | |
2998ab1d DC |
301 | struct xfs_trans *tp = args->trans; |
302 | struct xfs_inode *dp = args->dp; | |
cbc8adf8 DC |
303 | struct xfs_mount *mp = dp->i_mount; |
304 | struct xfs_buf *bp; | |
305 | int error; | |
306 | struct xfs_dir3_icfree_hdr hdr; | |
307 | ||
2998ab1d | 308 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno), |
cbc8adf8 DC |
309 | -1, &bp, XFS_DATA_FORK); |
310 | if (error) | |
311 | return error; | |
312 | ||
61fe135c | 313 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF); |
cbc8adf8 DC |
314 | bp->b_ops = &xfs_dir3_free_buf_ops; |
315 | ||
316 | /* | |
317 | * Initialize the new block to be empty, and remember | |
318 | * its first slot as our empty slot. | |
319 | */ | |
e400d27d DC |
320 | memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr)); |
321 | memset(&hdr, 0, sizeof(hdr)); | |
322 | ||
cbc8adf8 DC |
323 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
324 | struct xfs_dir3_free_hdr *hdr3 = bp->b_addr; | |
325 | ||
326 | hdr.magic = XFS_DIR3_FREE_MAGIC; | |
e400d27d | 327 | |
cbc8adf8 DC |
328 | hdr3->hdr.blkno = cpu_to_be64(bp->b_bn); |
329 | hdr3->hdr.owner = cpu_to_be64(dp->i_ino); | |
ce748eaa | 330 | uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid); |
e400d27d DC |
331 | } else |
332 | hdr.magic = XFS_DIR2_FREE_MAGIC; | |
200dada7 | 333 | xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr); |
cbc8adf8 DC |
334 | *bpp = bp; |
335 | return 0; | |
2025207c DC |
336 | } |
337 | ||
1da177e4 LT |
338 | /* |
339 | * Log entries from a freespace block. | |
340 | */ | |
5d77c0dc | 341 | STATIC void |
1da177e4 | 342 | xfs_dir2_free_log_bests( |
bc85178a | 343 | struct xfs_da_args *args, |
a84f3d5c | 344 | struct xfs_dir3_icfree_hdr *hdr, |
1d9025e5 | 345 | struct xfs_buf *bp, |
1da177e4 LT |
346 | int first, /* first entry to log */ |
347 | int last) /* last entry to log */ | |
348 | { | |
a84f3d5c | 349 | struct xfs_dir2_free *free = bp->b_addr; |
1da177e4 | 350 | |
cbc8adf8 DC |
351 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
352 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); | |
bc85178a | 353 | xfs_trans_log_buf(args->trans, bp, |
a84f3d5c CH |
354 | (char *)&hdr->bests[first] - (char *)free, |
355 | (char *)&hdr->bests[last] - (char *)free + | |
356 | sizeof(hdr->bests[0]) - 1); | |
1da177e4 LT |
357 | } |
358 | ||
359 | /* | |
360 | * Log header from a freespace block. | |
361 | */ | |
362 | static void | |
363 | xfs_dir2_free_log_header( | |
bc85178a | 364 | struct xfs_da_args *args, |
1d9025e5 | 365 | struct xfs_buf *bp) |
1da177e4 | 366 | { |
836a94ad | 367 | #ifdef DEBUG |
1da177e4 LT |
368 | xfs_dir2_free_t *free; /* freespace structure */ |
369 | ||
1d9025e5 | 370 | free = bp->b_addr; |
cbc8adf8 DC |
371 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
372 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); | |
836a94ad | 373 | #endif |
bc85178a | 374 | xfs_trans_log_buf(args->trans, bp, 0, |
ed1d612f | 375 | args->geo->free_hdr_size - 1); |
1da177e4 LT |
376 | } |
377 | ||
378 | /* | |
379 | * Convert a leaf-format directory to a node-format directory. | |
380 | * We need to change the magic number of the leaf block, and copy | |
381 | * the freespace table out of the leaf block into its own block. | |
382 | */ | |
383 | int /* error */ | |
384 | xfs_dir2_leaf_to_node( | |
385 | xfs_da_args_t *args, /* operation arguments */ | |
1d9025e5 | 386 | struct xfs_buf *lbp) /* leaf buffer */ |
1da177e4 LT |
387 | { |
388 | xfs_inode_t *dp; /* incore directory inode */ | |
389 | int error; /* error return value */ | |
1d9025e5 | 390 | struct xfs_buf *fbp; /* freespace buffer */ |
1da177e4 | 391 | xfs_dir2_db_t fdb; /* freespace block number */ |
68b3a102 | 392 | __be16 *from; /* pointer to freespace entry */ |
1da177e4 LT |
393 | int i; /* leaf freespace index */ |
394 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
395 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ | |
1da177e4 LT |
396 | int n; /* count of live freespc ents */ |
397 | xfs_dir2_data_off_t off; /* freespace entry value */ | |
1da177e4 | 398 | xfs_trans_t *tp; /* transaction pointer */ |
cbc8adf8 | 399 | struct xfs_dir3_icfree_hdr freehdr; |
1da177e4 | 400 | |
0b1b213f CH |
401 | trace_xfs_dir2_leaf_to_node(args); |
402 | ||
1da177e4 | 403 | dp = args->dp; |
1da177e4 LT |
404 | tp = args->trans; |
405 | /* | |
406 | * Add a freespace block to the directory. | |
407 | */ | |
408 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { | |
409 | return error; | |
410 | } | |
30028030 | 411 | ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET)); |
1da177e4 LT |
412 | /* |
413 | * Get the buffer for the new freespace block. | |
414 | */ | |
2998ab1d | 415 | error = xfs_dir3_free_get_buf(args, fdb, &fbp); |
b0f539de | 416 | if (error) |
1da177e4 | 417 | return error; |
b0f539de | 418 | |
a84f3d5c | 419 | xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr); |
1d9025e5 | 420 | leaf = lbp->b_addr; |
8f66193c | 421 | ltp = xfs_dir2_leaf_tail_p(args->geo, leaf); |
3f883f5b | 422 | if (be32_to_cpu(ltp->bestcount) > |
a5155b87 DW |
423 | (uint)dp->i_d.di_size / args->geo->blksize) { |
424 | xfs_buf_corruption_error(lbp); | |
3f883f5b | 425 | return -EFSCORRUPTED; |
a5155b87 | 426 | } |
cbc8adf8 | 427 | |
1da177e4 LT |
428 | /* |
429 | * Copy freespace entries from the leaf block to the new block. | |
430 | * Count active entries. | |
431 | */ | |
cbc8adf8 | 432 | from = xfs_dir2_leaf_bests_p(ltp); |
a84f3d5c CH |
433 | for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) { |
434 | off = be16_to_cpu(*from); | |
435 | if (off != NULLDATAOFF) | |
1da177e4 | 436 | n++; |
a84f3d5c | 437 | freehdr.bests[i] = cpu_to_be16(off); |
1da177e4 | 438 | } |
b0f539de | 439 | |
1da177e4 | 440 | /* |
cbc8adf8 | 441 | * Now initialize the freespace block header. |
1da177e4 | 442 | */ |
cbc8adf8 DC |
443 | freehdr.nused = n; |
444 | freehdr.nvalid = be32_to_cpu(ltp->bestcount); | |
445 | ||
200dada7 | 446 | xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr); |
a84f3d5c | 447 | xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1); |
bc85178a | 448 | xfs_dir2_free_log_header(args, fbp); |
cbc8adf8 | 449 | |
24df33b4 DC |
450 | /* |
451 | * Converting the leaf to a leafnode is just a matter of changing the | |
452 | * magic number and the ops. Do the change directly to the buffer as | |
453 | * it's less work (and less code) than decoding the header to host | |
454 | * format and back again. | |
455 | */ | |
456 | if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)) | |
457 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); | |
458 | else | |
459 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC); | |
460 | lbp->b_ops = &xfs_dir3_leafn_buf_ops; | |
61fe135c | 461 | xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF); |
bc85178a | 462 | xfs_dir3_leaf_log_header(args, lbp); |
4141956a | 463 | xfs_dir3_leaf_check(dp, lbp); |
1da177e4 LT |
464 | return 0; |
465 | } | |
466 | ||
467 | /* | |
468 | * Add a leaf entry to a leaf block in a node-form directory. | |
469 | * The other work necessary is done from the caller. | |
470 | */ | |
471 | static int /* error */ | |
472 | xfs_dir2_leafn_add( | |
1d9025e5 | 473 | struct xfs_buf *bp, /* leaf buffer */ |
79622c7c | 474 | struct xfs_da_args *args, /* operation arguments */ |
1da177e4 LT |
475 | int index) /* insertion pt for new entry */ |
476 | { | |
79622c7c DW |
477 | struct xfs_dir3_icleaf_hdr leafhdr; |
478 | struct xfs_inode *dp = args->dp; | |
479 | struct xfs_dir2_leaf *leaf = bp->b_addr; | |
480 | struct xfs_dir2_leaf_entry *lep; | |
481 | struct xfs_dir2_leaf_entry *ents; | |
1da177e4 | 482 | int compact; /* compacting stale leaves */ |
79622c7c | 483 | int highstale = 0; /* next stale entry */ |
1da177e4 LT |
484 | int lfloghigh; /* high leaf entry logging */ |
485 | int lfloglow; /* low leaf entry logging */ | |
79622c7c | 486 | int lowstale = 0; /* previous stale entry */ |
1da177e4 | 487 | |
0b1b213f CH |
488 | trace_xfs_dir2_leafn_add(args, index); |
489 | ||
51842556 | 490 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
787b0893 | 491 | ents = leafhdr.ents; |
1da177e4 LT |
492 | |
493 | /* | |
494 | * Quick check just to make sure we are not going to index | |
495 | * into other peoples memory | |
496 | */ | |
a5155b87 DW |
497 | if (index < 0) { |
498 | xfs_buf_corruption_error(bp); | |
2451337d | 499 | return -EFSCORRUPTED; |
a5155b87 | 500 | } |
1da177e4 LT |
501 | |
502 | /* | |
503 | * If there are already the maximum number of leaf entries in | |
504 | * the block, if there are no stale entries it won't fit. | |
505 | * Caller will do a split. If there are stale entries we'll do | |
506 | * a compact. | |
507 | */ | |
508 | ||
478c7835 | 509 | if (leafhdr.count == args->geo->leaf_max_ents) { |
24df33b4 | 510 | if (!leafhdr.stale) |
2451337d | 511 | return -ENOSPC; |
24df33b4 | 512 | compact = leafhdr.stale > 1; |
1da177e4 LT |
513 | } else |
514 | compact = 0; | |
24df33b4 DC |
515 | ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval); |
516 | ASSERT(index == leafhdr.count || | |
517 | be32_to_cpu(ents[index].hashval) >= args->hashval); | |
1da177e4 | 518 | |
6a178100 | 519 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) |
1da177e4 LT |
520 | return 0; |
521 | ||
522 | /* | |
523 | * Compact out all but one stale leaf entry. Leaves behind | |
524 | * the entry closest to index. | |
525 | */ | |
24df33b4 DC |
526 | if (compact) |
527 | xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale, | |
528 | &highstale, &lfloglow, &lfloghigh); | |
529 | else if (leafhdr.stale) { | |
530 | /* | |
531 | * Set impossible logging indices for this case. | |
532 | */ | |
533 | lfloglow = leafhdr.count; | |
1da177e4 LT |
534 | lfloghigh = -1; |
535 | } | |
4fb44c82 | 536 | |
1da177e4 LT |
537 | /* |
538 | * Insert the new entry, log everything. | |
539 | */ | |
24df33b4 | 540 | lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale, |
4fb44c82 CH |
541 | highstale, &lfloglow, &lfloghigh); |
542 | ||
3c1f9c15 | 543 | lep->hashval = cpu_to_be32(args->hashval); |
30028030 | 544 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo, |
3c1f9c15 | 545 | args->blkno, args->index)); |
24df33b4 | 546 | |
163fbbb3 | 547 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); |
bc85178a | 548 | xfs_dir3_leaf_log_header(args, bp); |
787b0893 | 549 | xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh); |
4141956a | 550 | xfs_dir3_leaf_check(dp, bp); |
1da177e4 LT |
551 | return 0; |
552 | } | |
553 | ||
554 | #ifdef DEBUG | |
cbc8adf8 DC |
555 | static void |
556 | xfs_dir2_free_hdr_check( | |
01ba43b8 | 557 | struct xfs_inode *dp, |
cbc8adf8 DC |
558 | struct xfs_buf *bp, |
559 | xfs_dir2_db_t db) | |
560 | { | |
561 | struct xfs_dir3_icfree_hdr hdr; | |
562 | ||
5ba30919 | 563 | xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr); |
cbc8adf8 | 564 | |
8f66193c DC |
565 | ASSERT((hdr.firstdb % |
566 | dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0); | |
cbc8adf8 DC |
567 | ASSERT(hdr.firstdb <= db); |
568 | ASSERT(db < hdr.firstdb + hdr.nvalid); | |
569 | } | |
570 | #else | |
01ba43b8 | 571 | #define xfs_dir2_free_hdr_check(dp, bp, db) |
1da177e4 LT |
572 | #endif /* DEBUG */ |
573 | ||
574 | /* | |
575 | * Return the last hash value in the leaf. | |
576 | * Stale entries are ok. | |
577 | */ | |
578 | xfs_dahash_t /* hash value */ | |
8e8877e6 | 579 | xfs_dir2_leaf_lasthash( |
4141956a | 580 | struct xfs_inode *dp, |
1d9025e5 | 581 | struct xfs_buf *bp, /* leaf buffer */ |
1da177e4 LT |
582 | int *count) /* count of entries in leaf */ |
583 | { | |
24df33b4 DC |
584 | struct xfs_dir3_icleaf_hdr leafhdr; |
585 | ||
787b0893 | 586 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr); |
24df33b4 DC |
587 | |
588 | ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC || | |
8e8877e6 DW |
589 | leafhdr.magic == XFS_DIR3_LEAFN_MAGIC || |
590 | leafhdr.magic == XFS_DIR2_LEAF1_MAGIC || | |
591 | leafhdr.magic == XFS_DIR3_LEAF1_MAGIC); | |
1da177e4 | 592 | |
1da177e4 | 593 | if (count) |
24df33b4 DC |
594 | *count = leafhdr.count; |
595 | if (!leafhdr.count) | |
1da177e4 | 596 | return 0; |
787b0893 | 597 | return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval); |
1da177e4 LT |
598 | } |
599 | ||
600 | /* | |
f9f6dce0 BN |
601 | * Look up a leaf entry for space to add a name in a node-format leaf block. |
602 | * The extrablk in state is a freespace block. | |
1da177e4 | 603 | */ |
f9f6dce0 BN |
604 | STATIC int |
605 | xfs_dir2_leafn_lookup_for_addname( | |
1d9025e5 | 606 | struct xfs_buf *bp, /* leaf buffer */ |
1da177e4 LT |
607 | xfs_da_args_t *args, /* operation arguments */ |
608 | int *indexp, /* out: leaf entry index */ | |
609 | xfs_da_state_t *state) /* state to fill in */ | |
610 | { | |
1d9025e5 | 611 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
f9f6dce0 BN |
612 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
613 | xfs_dir2_db_t curfdb = -1; /* current free block number */ | |
1da177e4 LT |
614 | xfs_inode_t *dp; /* incore directory inode */ |
615 | int error; /* error return value */ | |
616 | int fi; /* free entry index */ | |
f9f6dce0 | 617 | xfs_dir2_free_t *free = NULL; /* free block structure */ |
1da177e4 LT |
618 | int index; /* leaf entry index */ |
619 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
f9f6dce0 | 620 | int length; /* length of new data entry */ |
1da177e4 LT |
621 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
622 | xfs_mount_t *mp; /* filesystem mount point */ | |
623 | xfs_dir2_db_t newdb; /* new data block number */ | |
624 | xfs_dir2_db_t newfdb; /* new free block number */ | |
625 | xfs_trans_t *tp; /* transaction pointer */ | |
24df33b4 | 626 | struct xfs_dir3_icleaf_hdr leafhdr; |
1da177e4 LT |
627 | |
628 | dp = args->dp; | |
629 | tp = args->trans; | |
630 | mp = dp->i_mount; | |
1d9025e5 | 631 | leaf = bp->b_addr; |
51842556 | 632 | xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); |
24df33b4 | 633 | |
4141956a | 634 | xfs_dir3_leaf_check(dp, bp); |
24df33b4 DC |
635 | ASSERT(leafhdr.count > 0); |
636 | ||
1da177e4 LT |
637 | /* |
638 | * Look up the hash value in the leaf entries. | |
639 | */ | |
640 | index = xfs_dir2_leaf_search_hash(args, bp); | |
641 | /* | |
642 | * Do we have a buffer coming in? | |
643 | */ | |
f9f6dce0 BN |
644 | if (state->extravalid) { |
645 | /* If so, it's a free block buffer, get the block number. */ | |
1da177e4 | 646 | curbp = state->extrablk.bp; |
f9f6dce0 | 647 | curfdb = state->extrablk.blkno; |
1d9025e5 | 648 | free = curbp->b_addr; |
cbc8adf8 DC |
649 | ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) || |
650 | free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC)); | |
1da177e4 | 651 | } |
9d23fc85 | 652 | length = dp->d_ops->data_entsize(args->namelen); |
1da177e4 LT |
653 | /* |
654 | * Loop over leaf entries with the right hash value. | |
655 | */ | |
787b0893 | 656 | for (lep = &leafhdr.ents[index]; |
24df33b4 DC |
657 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
658 | lep++, index++) { | |
1da177e4 LT |
659 | /* |
660 | * Skip stale leaf entries. | |
661 | */ | |
3c1f9c15 | 662 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
1da177e4 LT |
663 | continue; |
664 | /* | |
665 | * Pull the data block number from the entry. | |
666 | */ | |
30028030 DC |
667 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
668 | be32_to_cpu(lep->address)); | |
1da177e4 LT |
669 | /* |
670 | * For addname, we're looking for a place to put the new entry. | |
671 | * We want to use a data block with an entry of equal | |
672 | * hash value to ours if there is one with room. | |
f9f6dce0 BN |
673 | * |
674 | * If this block isn't the data block we already have | |
675 | * in hand, take a look at it. | |
1da177e4 | 676 | */ |
f9f6dce0 | 677 | if (newdb != curdb) { |
a84f3d5c | 678 | struct xfs_dir3_icfree_hdr freehdr; |
cbc8adf8 | 679 | |
f9f6dce0 | 680 | curdb = newdb; |
1da177e4 | 681 | /* |
f9f6dce0 BN |
682 | * Convert the data block to the free block |
683 | * holding its freespace information. | |
1da177e4 | 684 | */ |
8f66193c | 685 | newfdb = dp->d_ops->db_to_fdb(args->geo, newdb); |
1da177e4 | 686 | /* |
f9f6dce0 | 687 | * If it's not the one we have in hand, read it in. |
1da177e4 | 688 | */ |
f9f6dce0 | 689 | if (newfdb != curfdb) { |
1da177e4 | 690 | /* |
f9f6dce0 | 691 | * If we had one before, drop it. |
1da177e4 LT |
692 | */ |
693 | if (curbp) | |
1d9025e5 | 694 | xfs_trans_brelse(tp, curbp); |
2025207c DC |
695 | |
696 | error = xfs_dir2_free_read(tp, dp, | |
2998ab1d DC |
697 | xfs_dir2_db_to_da(args->geo, |
698 | newfdb), | |
2025207c | 699 | &curbp); |
f9f6dce0 | 700 | if (error) |
1da177e4 | 701 | return error; |
1d9025e5 | 702 | free = curbp->b_addr; |
cbc8adf8 | 703 | |
01ba43b8 | 704 | xfs_dir2_free_hdr_check(dp, curbp, curdb); |
1da177e4 LT |
705 | } |
706 | /* | |
f9f6dce0 | 707 | * Get the index for our entry. |
1da177e4 | 708 | */ |
8f66193c | 709 | fi = dp->d_ops->db_to_fdindex(args->geo, curdb); |
1da177e4 | 710 | /* |
f9f6dce0 | 711 | * If it has room, return it. |
1da177e4 | 712 | */ |
a84f3d5c CH |
713 | xfs_dir2_free_hdr_from_disk(mp, &freehdr, free); |
714 | if (unlikely( | |
715 | freehdr.bests[fi] == cpu_to_be16(NULLDATAOFF))) { | |
f9f6dce0 BN |
716 | XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", |
717 | XFS_ERRLEVEL_LOW, mp); | |
718 | if (curfdb != newfdb) | |
1d9025e5 | 719 | xfs_trans_brelse(tp, curbp); |
2451337d | 720 | return -EFSCORRUPTED; |
1da177e4 | 721 | } |
f9f6dce0 | 722 | curfdb = newfdb; |
a84f3d5c | 723 | if (be16_to_cpu(freehdr.bests[fi]) >= length) |
f9f6dce0 | 724 | goto out; |
1da177e4 LT |
725 | } |
726 | } | |
f9f6dce0 BN |
727 | /* Didn't find any space */ |
728 | fi = -1; | |
729 | out: | |
6a178100 | 730 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); |
f9f6dce0 BN |
731 | if (curbp) { |
732 | /* Giving back a free block. */ | |
733 | state->extravalid = 1; | |
734 | state->extrablk.bp = curbp; | |
735 | state->extrablk.index = fi; | |
736 | state->extrablk.blkno = curfdb; | |
cbc8adf8 DC |
737 | |
738 | /* | |
739 | * Important: this magic number is not in the buffer - it's for | |
740 | * buffer type information and therefore only the free/data type | |
741 | * matters here, not whether CRCs are enabled or not. | |
742 | */ | |
f9f6dce0 BN |
743 | state->extrablk.magic = XFS_DIR2_FREE_MAGIC; |
744 | } else { | |
745 | state->extravalid = 0; | |
746 | } | |
1da177e4 | 747 | /* |
f9f6dce0 | 748 | * Return the index, that will be the insertion point. |
1da177e4 | 749 | */ |
f9f6dce0 | 750 | *indexp = index; |
2451337d | 751 | return -ENOENT; |
f9f6dce0 BN |
752 | } |
753 | ||
754 | /* | |
755 | * Look up a leaf entry in a node-format leaf block. | |
756 | * The extrablk in state a data block. | |
757 | */ | |
758 | STATIC int | |
759 | xfs_dir2_leafn_lookup_for_entry( | |
1d9025e5 | 760 | struct xfs_buf *bp, /* leaf buffer */ |
f9f6dce0 BN |
761 | xfs_da_args_t *args, /* operation arguments */ |
762 | int *indexp, /* out: leaf entry index */ | |
763 | xfs_da_state_t *state) /* state to fill in */ | |
764 | { | |
1d9025e5 | 765 | struct xfs_buf *curbp = NULL; /* current data/free buffer */ |
f9f6dce0 BN |
766 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
767 | xfs_dir2_data_entry_t *dep; /* data block entry */ | |
768 | xfs_inode_t *dp; /* incore directory inode */ | |
769 | int error; /* error return value */ | |
f9f6dce0 BN |
770 | int index; /* leaf entry index */ |
771 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
772 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
773 | xfs_mount_t *mp; /* filesystem mount point */ | |
774 | xfs_dir2_db_t newdb; /* new data block number */ | |
775 | xfs_trans_t *tp; /* transaction pointer */ | |
5163f95a | 776 | enum xfs_dacmp cmp; /* comparison result */ |
24df33b4 | 777 | struct xfs_dir3_icleaf_hdr leafhdr; |
f9f6dce0 BN |
778 | |
779 | dp = args->dp; | |
780 | tp = args->trans; | |
781 | mp = dp->i_mount; | |
1d9025e5 | 782 | leaf = bp->b_addr; |
51842556 | 783 | xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf); |
24df33b4 | 784 | |
4141956a | 785 | xfs_dir3_leaf_check(dp, bp); |
a5155b87 DW |
786 | if (leafhdr.count <= 0) { |
787 | xfs_buf_corruption_error(bp); | |
858b44dc | 788 | return -EFSCORRUPTED; |
a5155b87 | 789 | } |
24df33b4 | 790 | |
f9f6dce0 BN |
791 | /* |
792 | * Look up the hash value in the leaf entries. | |
793 | */ | |
794 | index = xfs_dir2_leaf_search_hash(args, bp); | |
795 | /* | |
796 | * Do we have a buffer coming in? | |
797 | */ | |
798 | if (state->extravalid) { | |
799 | curbp = state->extrablk.bp; | |
800 | curdb = state->extrablk.blkno; | |
801 | } | |
802 | /* | |
803 | * Loop over leaf entries with the right hash value. | |
804 | */ | |
787b0893 | 805 | for (lep = &leafhdr.ents[index]; |
24df33b4 DC |
806 | index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval; |
807 | lep++, index++) { | |
f9f6dce0 BN |
808 | /* |
809 | * Skip stale leaf entries. | |
810 | */ | |
811 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) | |
812 | continue; | |
1da177e4 | 813 | /* |
f9f6dce0 BN |
814 | * Pull the data block number from the entry. |
815 | */ | |
30028030 DC |
816 | newdb = xfs_dir2_dataptr_to_db(args->geo, |
817 | be32_to_cpu(lep->address)); | |
f9f6dce0 BN |
818 | /* |
819 | * Not adding a new entry, so we really want to find | |
820 | * the name given to us. | |
821 | * | |
822 | * If it's a different data block, go get it. | |
1da177e4 | 823 | */ |
f9f6dce0 BN |
824 | if (newdb != curdb) { |
825 | /* | |
90bb7ab0 BN |
826 | * If we had a block before that we aren't saving |
827 | * for a CI name, drop it | |
f9f6dce0 | 828 | */ |
90bb7ab0 BN |
829 | if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT || |
830 | curdb != state->extrablk.blkno)) | |
1d9025e5 | 831 | xfs_trans_brelse(tp, curbp); |
f9f6dce0 | 832 | /* |
90bb7ab0 BN |
833 | * If needing the block that is saved with a CI match, |
834 | * use it otherwise read in the new data block. | |
f9f6dce0 | 835 | */ |
90bb7ab0 BN |
836 | if (args->cmpresult != XFS_CMP_DIFFERENT && |
837 | newdb == state->extrablk.blkno) { | |
838 | ASSERT(state->extravalid); | |
839 | curbp = state->extrablk.bp; | |
840 | } else { | |
33363fee | 841 | error = xfs_dir3_data_read(tp, dp, |
2998ab1d DC |
842 | xfs_dir2_db_to_da(args->geo, |
843 | newdb), | |
e4813572 | 844 | -1, &curbp); |
90bb7ab0 BN |
845 | if (error) |
846 | return error; | |
847 | } | |
33363fee | 848 | xfs_dir3_data_check(dp, curbp); |
f9f6dce0 | 849 | curdb = newdb; |
1da177e4 LT |
850 | } |
851 | /* | |
f9f6dce0 | 852 | * Point to the data entry. |
1da177e4 | 853 | */ |
1d9025e5 | 854 | dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr + |
30028030 DC |
855 | xfs_dir2_dataptr_to_off(args->geo, |
856 | be32_to_cpu(lep->address))); | |
f9f6dce0 | 857 | /* |
5163f95a BN |
858 | * Compare the entry and if it's an exact match, return |
859 | * EEXIST immediately. If it's the first case-insensitive | |
90bb7ab0 | 860 | * match, store the block & inode number and continue looking. |
f9f6dce0 | 861 | */ |
5163f95a BN |
862 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); |
863 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | |
90bb7ab0 BN |
864 | /* If there is a CI match block, drop it */ |
865 | if (args->cmpresult != XFS_CMP_DIFFERENT && | |
866 | curdb != state->extrablk.blkno) | |
1d9025e5 | 867 | xfs_trans_brelse(tp, state->extrablk.bp); |
5163f95a | 868 | args->cmpresult = cmp; |
f9f6dce0 | 869 | args->inumber = be64_to_cpu(dep->inumber); |
9d23fc85 | 870 | args->filetype = dp->d_ops->data_get_ftype(dep); |
90bb7ab0 BN |
871 | *indexp = index; |
872 | state->extravalid = 1; | |
873 | state->extrablk.bp = curbp; | |
874 | state->extrablk.blkno = curdb; | |
875 | state->extrablk.index = (int)((char *)dep - | |
1d9025e5 | 876 | (char *)curbp->b_addr); |
90bb7ab0 | 877 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
33363fee | 878 | curbp->b_ops = &xfs_dir3_data_buf_ops; |
61fe135c | 879 | xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); |
5163f95a | 880 | if (cmp == XFS_CMP_EXACT) |
2451337d | 881 | return -EEXIST; |
1da177e4 LT |
882 | } |
883 | } | |
24df33b4 | 884 | ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT)); |
f9f6dce0 | 885 | if (curbp) { |
90bb7ab0 BN |
886 | if (args->cmpresult == XFS_CMP_DIFFERENT) { |
887 | /* Giving back last used data block. */ | |
888 | state->extravalid = 1; | |
889 | state->extrablk.bp = curbp; | |
890 | state->extrablk.index = -1; | |
891 | state->extrablk.blkno = curdb; | |
892 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; | |
33363fee | 893 | curbp->b_ops = &xfs_dir3_data_buf_ops; |
61fe135c | 894 | xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF); |
90bb7ab0 BN |
895 | } else { |
896 | /* If the curbp is not the CI match block, drop it */ | |
897 | if (state->extrablk.bp != curbp) | |
1d9025e5 | 898 | xfs_trans_brelse(tp, curbp); |
90bb7ab0 | 899 | } |
f9f6dce0 BN |
900 | } else { |
901 | state->extravalid = 0; | |
902 | } | |
1da177e4 | 903 | *indexp = index; |
2451337d | 904 | return -ENOENT; |
f9f6dce0 BN |
905 | } |
906 | ||
907 | /* | |
908 | * Look up a leaf entry in a node-format leaf block. | |
909 | * If this is an addname then the extrablk in state is a freespace block, | |
910 | * otherwise it's a data block. | |
911 | */ | |
912 | int | |
913 | xfs_dir2_leafn_lookup_int( | |
1d9025e5 | 914 | struct xfs_buf *bp, /* leaf buffer */ |
f9f6dce0 BN |
915 | xfs_da_args_t *args, /* operation arguments */ |
916 | int *indexp, /* out: leaf entry index */ | |
917 | xfs_da_state_t *state) /* state to fill in */ | |
918 | { | |
6a178100 | 919 | if (args->op_flags & XFS_DA_OP_ADDNAME) |
f9f6dce0 BN |
920 | return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, |
921 | state); | |
922 | return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); | |
1da177e4 LT |
923 | } |
924 | ||
925 | /* | |
926 | * Move count leaf entries from source to destination leaf. | |
927 | * Log entries and headers. Stale entries are preserved. | |
928 | */ | |
929 | static void | |
24df33b4 DC |
930 | xfs_dir3_leafn_moveents( |
931 | xfs_da_args_t *args, /* operation arguments */ | |
932 | struct xfs_buf *bp_s, /* source */ | |
933 | struct xfs_dir3_icleaf_hdr *shdr, | |
934 | struct xfs_dir2_leaf_entry *sents, | |
935 | int start_s,/* source leaf index */ | |
936 | struct xfs_buf *bp_d, /* destination */ | |
937 | struct xfs_dir3_icleaf_hdr *dhdr, | |
938 | struct xfs_dir2_leaf_entry *dents, | |
939 | int start_d,/* destination leaf index */ | |
940 | int count) /* count of leaves to copy */ | |
1da177e4 | 941 | { |
24df33b4 | 942 | int stale; /* count stale leaves copied */ |
1da177e4 | 943 | |
0b1b213f CH |
944 | trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count); |
945 | ||
1da177e4 LT |
946 | /* |
947 | * Silently return if nothing to do. | |
948 | */ | |
24df33b4 | 949 | if (count == 0) |
1da177e4 | 950 | return; |
24df33b4 | 951 | |
1da177e4 LT |
952 | /* |
953 | * If the destination index is not the end of the current | |
954 | * destination leaf entries, open up a hole in the destination | |
955 | * to hold the new entries. | |
956 | */ | |
24df33b4 DC |
957 | if (start_d < dhdr->count) { |
958 | memmove(&dents[start_d + count], &dents[start_d], | |
959 | (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t)); | |
787b0893 | 960 | xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count, |
24df33b4 | 961 | count + dhdr->count - 1); |
1da177e4 LT |
962 | } |
963 | /* | |
964 | * If the source has stale leaves, count the ones in the copy range | |
965 | * so we can update the header correctly. | |
966 | */ | |
24df33b4 | 967 | if (shdr->stale) { |
1da177e4 LT |
968 | int i; /* temp leaf index */ |
969 | ||
970 | for (i = start_s, stale = 0; i < start_s + count; i++) { | |
24df33b4 DC |
971 | if (sents[i].address == |
972 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | |
1da177e4 LT |
973 | stale++; |
974 | } | |
975 | } else | |
976 | stale = 0; | |
977 | /* | |
978 | * Copy the leaf entries from source to destination. | |
979 | */ | |
24df33b4 | 980 | memcpy(&dents[start_d], &sents[start_s], |
1da177e4 | 981 | count * sizeof(xfs_dir2_leaf_entry_t)); |
787b0893 | 982 | xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1); |
24df33b4 | 983 | |
1da177e4 LT |
984 | /* |
985 | * If there are source entries after the ones we copied, | |
986 | * delete the ones we copied by sliding the next ones down. | |
987 | */ | |
24df33b4 DC |
988 | if (start_s + count < shdr->count) { |
989 | memmove(&sents[start_s], &sents[start_s + count], | |
1da177e4 | 990 | count * sizeof(xfs_dir2_leaf_entry_t)); |
787b0893 CH |
991 | xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s, |
992 | start_s + count - 1); | |
1da177e4 | 993 | } |
24df33b4 | 994 | |
1da177e4 LT |
995 | /* |
996 | * Update the headers and log them. | |
997 | */ | |
24df33b4 DC |
998 | shdr->count -= count; |
999 | shdr->stale -= stale; | |
1000 | dhdr->count += count; | |
1001 | dhdr->stale += stale; | |
1da177e4 LT |
1002 | } |
1003 | ||
1004 | /* | |
1005 | * Determine the sort order of two leaf blocks. | |
1006 | * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. | |
1007 | */ | |
1008 | int /* sort order */ | |
1009 | xfs_dir2_leafn_order( | |
4141956a | 1010 | struct xfs_inode *dp, |
24df33b4 DC |
1011 | struct xfs_buf *leaf1_bp, /* leaf1 buffer */ |
1012 | struct xfs_buf *leaf2_bp) /* leaf2 buffer */ | |
1da177e4 | 1013 | { |
24df33b4 DC |
1014 | struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr; |
1015 | struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr; | |
1016 | struct xfs_dir2_leaf_entry *ents1; | |
1017 | struct xfs_dir2_leaf_entry *ents2; | |
1018 | struct xfs_dir3_icleaf_hdr hdr1; | |
1019 | struct xfs_dir3_icleaf_hdr hdr2; | |
1020 | ||
51842556 CH |
1021 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); |
1022 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); | |
787b0893 CH |
1023 | ents1 = hdr1.ents; |
1024 | ents2 = hdr2.ents; | |
24df33b4 DC |
1025 | |
1026 | if (hdr1.count > 0 && hdr2.count > 0 && | |
1027 | (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) || | |
1028 | be32_to_cpu(ents2[hdr2.count - 1].hashval) < | |
1029 | be32_to_cpu(ents1[hdr1.count - 1].hashval))) | |
1da177e4 LT |
1030 | return 1; |
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * Rebalance leaf entries between two leaf blocks. | |
1036 | * This is actually only called when the second block is new, | |
1037 | * though the code deals with the general case. | |
1038 | * A new entry will be inserted in one of the blocks, and that | |
1039 | * entry is taken into account when balancing. | |
1040 | */ | |
1041 | static void | |
1042 | xfs_dir2_leafn_rebalance( | |
1043 | xfs_da_state_t *state, /* btree cursor */ | |
1044 | xfs_da_state_blk_t *blk1, /* first btree block */ | |
1045 | xfs_da_state_blk_t *blk2) /* second btree block */ | |
1046 | { | |
1047 | xfs_da_args_t *args; /* operation arguments */ | |
1048 | int count; /* count (& direction) leaves */ | |
1049 | int isleft; /* new goes in left leaf */ | |
1050 | xfs_dir2_leaf_t *leaf1; /* first leaf structure */ | |
1051 | xfs_dir2_leaf_t *leaf2; /* second leaf structure */ | |
1052 | int mid; /* midpoint leaf index */ | |
742ae1e3 | 1053 | #if defined(DEBUG) || defined(XFS_WARN) |
1da177e4 LT |
1054 | int oldstale; /* old count of stale leaves */ |
1055 | #endif | |
1056 | int oldsum; /* old total leaf count */ | |
e4e542a6 | 1057 | int swap_blocks; /* swapped leaf blocks */ |
24df33b4 DC |
1058 | struct xfs_dir2_leaf_entry *ents1; |
1059 | struct xfs_dir2_leaf_entry *ents2; | |
1060 | struct xfs_dir3_icleaf_hdr hdr1; | |
1061 | struct xfs_dir3_icleaf_hdr hdr2; | |
4141956a | 1062 | struct xfs_inode *dp = state->args->dp; |
1da177e4 LT |
1063 | |
1064 | args = state->args; | |
1065 | /* | |
1066 | * If the block order is wrong, swap the arguments. | |
1067 | */ | |
e4e542a6 GS |
1068 | swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp); |
1069 | if (swap_blocks) | |
1070 | swap(blk1, blk2); | |
1da177e4 | 1071 | |
1d9025e5 DC |
1072 | leaf1 = blk1->bp->b_addr; |
1073 | leaf2 = blk2->bp->b_addr; | |
51842556 CH |
1074 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1); |
1075 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2); | |
787b0893 CH |
1076 | ents1 = hdr1.ents; |
1077 | ents2 = hdr2.ents; | |
24df33b4 DC |
1078 | |
1079 | oldsum = hdr1.count + hdr2.count; | |
742ae1e3 | 1080 | #if defined(DEBUG) || defined(XFS_WARN) |
24df33b4 | 1081 | oldstale = hdr1.stale + hdr2.stale; |
1da177e4 LT |
1082 | #endif |
1083 | mid = oldsum >> 1; | |
24df33b4 | 1084 | |
1da177e4 LT |
1085 | /* |
1086 | * If the old leaf count was odd then the new one will be even, | |
1087 | * so we need to divide the new count evenly. | |
1088 | */ | |
1089 | if (oldsum & 1) { | |
1090 | xfs_dahash_t midhash; /* middle entry hash value */ | |
1091 | ||
24df33b4 DC |
1092 | if (mid >= hdr1.count) |
1093 | midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval); | |
1da177e4 | 1094 | else |
24df33b4 | 1095 | midhash = be32_to_cpu(ents1[mid].hashval); |
1da177e4 LT |
1096 | isleft = args->hashval <= midhash; |
1097 | } | |
1098 | /* | |
1099 | * If the old count is even then the new count is odd, so there's | |
1100 | * no preferred side for the new entry. | |
1101 | * Pick the left one. | |
1102 | */ | |
1103 | else | |
1104 | isleft = 1; | |
1105 | /* | |
1106 | * Calculate moved entry count. Positive means left-to-right, | |
1107 | * negative means right-to-left. Then move the entries. | |
1108 | */ | |
24df33b4 | 1109 | count = hdr1.count - mid + (isleft == 0); |
1da177e4 | 1110 | if (count > 0) |
24df33b4 DC |
1111 | xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1, |
1112 | hdr1.count - count, blk2->bp, | |
1113 | &hdr2, ents2, 0, count); | |
1da177e4 | 1114 | else if (count < 0) |
24df33b4 DC |
1115 | xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0, |
1116 | blk1->bp, &hdr1, ents1, | |
1117 | hdr1.count, count); | |
1118 | ||
1119 | ASSERT(hdr1.count + hdr2.count == oldsum); | |
1120 | ASSERT(hdr1.stale + hdr2.stale == oldstale); | |
1121 | ||
1122 | /* log the changes made when moving the entries */ | |
163fbbb3 CH |
1123 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1); |
1124 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2); | |
bc85178a DC |
1125 | xfs_dir3_leaf_log_header(args, blk1->bp); |
1126 | xfs_dir3_leaf_log_header(args, blk2->bp); | |
24df33b4 | 1127 | |
4141956a DC |
1128 | xfs_dir3_leaf_check(dp, blk1->bp); |
1129 | xfs_dir3_leaf_check(dp, blk2->bp); | |
24df33b4 | 1130 | |
1da177e4 LT |
1131 | /* |
1132 | * Mark whether we're inserting into the old or new leaf. | |
1133 | */ | |
24df33b4 | 1134 | if (hdr1.count < hdr2.count) |
e4e542a6 | 1135 | state->inleaf = swap_blocks; |
24df33b4 | 1136 | else if (hdr1.count > hdr2.count) |
e4e542a6 | 1137 | state->inleaf = !swap_blocks; |
1da177e4 | 1138 | else |
e4e542a6 | 1139 | state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count); |
1da177e4 LT |
1140 | /* |
1141 | * Adjust the expected index for insertion. | |
1142 | */ | |
1143 | if (!state->inleaf) | |
24df33b4 | 1144 | blk2->index = blk1->index - hdr1.count; |
f9f6dce0 BN |
1145 | |
1146 | /* | |
1147 | * Finally sanity check just to make sure we are not returning a | |
1148 | * negative index | |
1da177e4 | 1149 | */ |
4141956a | 1150 | if (blk2->index < 0) { |
1da177e4 LT |
1151 | state->inleaf = 1; |
1152 | blk2->index = 0; | |
4141956a | 1153 | xfs_alert(dp->i_mount, |
08e96e1a | 1154 | "%s: picked the wrong leaf? reverting original leaf: blk1->index %d", |
0b932ccc | 1155 | __func__, blk1->index); |
1da177e4 LT |
1156 | } |
1157 | } | |
1158 | ||
2025207c | 1159 | static int |
cbc8adf8 | 1160 | xfs_dir3_data_block_free( |
2025207c DC |
1161 | xfs_da_args_t *args, |
1162 | struct xfs_dir2_data_hdr *hdr, | |
1163 | struct xfs_dir2_free *free, | |
1164 | xfs_dir2_db_t fdb, | |
1165 | int findex, | |
1166 | struct xfs_buf *fbp, | |
1167 | int longest) | |
1168 | { | |
2025207c | 1169 | int logfree = 0; |
cbc8adf8 | 1170 | struct xfs_dir3_icfree_hdr freehdr; |
01ba43b8 | 1171 | struct xfs_inode *dp = args->dp; |
2025207c | 1172 | |
5ba30919 | 1173 | xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); |
cbc8adf8 | 1174 | if (hdr) { |
2025207c | 1175 | /* |
cbc8adf8 DC |
1176 | * Data block is not empty, just set the free entry to the new |
1177 | * value. | |
2025207c | 1178 | */ |
a84f3d5c CH |
1179 | freehdr.bests[findex] = cpu_to_be16(longest); |
1180 | xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); | |
cbc8adf8 DC |
1181 | return 0; |
1182 | } | |
2025207c | 1183 | |
cbc8adf8 DC |
1184 | /* One less used entry in the free table. */ |
1185 | freehdr.nused--; | |
2025207c | 1186 | |
cbc8adf8 DC |
1187 | /* |
1188 | * If this was the last entry in the table, we can trim the table size | |
1189 | * back. There might be other entries at the end referring to | |
1190 | * non-existent data blocks, get those too. | |
1191 | */ | |
1192 | if (findex == freehdr.nvalid - 1) { | |
1193 | int i; /* free entry index */ | |
1194 | ||
1195 | for (i = findex - 1; i >= 0; i--) { | |
a84f3d5c | 1196 | if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF)) |
cbc8adf8 | 1197 | break; |
2025207c | 1198 | } |
cbc8adf8 DC |
1199 | freehdr.nvalid = i + 1; |
1200 | logfree = 0; | |
2025207c | 1201 | } else { |
cbc8adf8 | 1202 | /* Not the last entry, just punch it out. */ |
a84f3d5c | 1203 | freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF); |
cbc8adf8 DC |
1204 | logfree = 1; |
1205 | } | |
1206 | ||
200dada7 | 1207 | xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr); |
bc85178a | 1208 | xfs_dir2_free_log_header(args, fbp); |
cbc8adf8 DC |
1209 | |
1210 | /* | |
1211 | * If there are no useful entries left in the block, get rid of the | |
1212 | * block if we can. | |
1213 | */ | |
1214 | if (!freehdr.nused) { | |
1215 | int error; | |
1216 | ||
1217 | error = xfs_dir2_shrink_inode(args, fdb, fbp); | |
1218 | if (error == 0) { | |
1219 | fbp = NULL; | |
1220 | logfree = 0; | |
2451337d | 1221 | } else if (error != -ENOSPC || args->total != 0) |
cbc8adf8 | 1222 | return error; |
2025207c | 1223 | /* |
cbc8adf8 DC |
1224 | * It's possible to get ENOSPC if there is no |
1225 | * space reservation. In this case some one | |
1226 | * else will eventually get rid of this block. | |
2025207c | 1227 | */ |
2025207c DC |
1228 | } |
1229 | ||
1230 | /* Log the free entry that changed, unless we got rid of it. */ | |
1231 | if (logfree) | |
a84f3d5c | 1232 | xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); |
2025207c DC |
1233 | return 0; |
1234 | } | |
1235 | ||
1da177e4 LT |
1236 | /* |
1237 | * Remove an entry from a node directory. | |
1238 | * This removes the leaf entry and the data entry, | |
1239 | * and updates the free block if necessary. | |
1240 | */ | |
1241 | static int /* error */ | |
1242 | xfs_dir2_leafn_remove( | |
1243 | xfs_da_args_t *args, /* operation arguments */ | |
1d9025e5 | 1244 | struct xfs_buf *bp, /* leaf buffer */ |
1da177e4 LT |
1245 | int index, /* leaf entry index */ |
1246 | xfs_da_state_blk_t *dblk, /* data block */ | |
1247 | int *rval) /* resulting block needs join */ | |
1248 | { | |
c2066e26 | 1249 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 | 1250 | xfs_dir2_db_t db; /* data block number */ |
1d9025e5 | 1251 | struct xfs_buf *dbp; /* data block buffer */ |
1da177e4 LT |
1252 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
1253 | xfs_inode_t *dp; /* incore directory inode */ | |
1254 | xfs_dir2_leaf_t *leaf; /* leaf structure */ | |
1255 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ | |
1256 | int longest; /* longest data free entry */ | |
1257 | int off; /* data block entry offset */ | |
1da177e4 LT |
1258 | int needlog; /* need to log data header */ |
1259 | int needscan; /* need to rescan data frees */ | |
1260 | xfs_trans_t *tp; /* transaction pointer */ | |
33363fee | 1261 | struct xfs_dir2_data_free *bf; /* bestfree table */ |
24df33b4 | 1262 | struct xfs_dir3_icleaf_hdr leafhdr; |
1da177e4 | 1263 | |
0b1b213f CH |
1264 | trace_xfs_dir2_leafn_remove(args, index); |
1265 | ||
1da177e4 LT |
1266 | dp = args->dp; |
1267 | tp = args->trans; | |
1d9025e5 | 1268 | leaf = bp->b_addr; |
51842556 | 1269 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
24df33b4 | 1270 | |
1da177e4 LT |
1271 | /* |
1272 | * Point to the entry we're removing. | |
1273 | */ | |
787b0893 | 1274 | lep = &leafhdr.ents[index]; |
24df33b4 | 1275 | |
1da177e4 LT |
1276 | /* |
1277 | * Extract the data block and offset from the entry. | |
1278 | */ | |
30028030 | 1279 | db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address)); |
1da177e4 | 1280 | ASSERT(dblk->blkno == db); |
30028030 | 1281 | off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)); |
1da177e4 | 1282 | ASSERT(dblk->index == off); |
24df33b4 | 1283 | |
1da177e4 LT |
1284 | /* |
1285 | * Kill the leaf entry by marking it stale. | |
1286 | * Log the leaf block changes. | |
1287 | */ | |
24df33b4 | 1288 | leafhdr.stale++; |
163fbbb3 | 1289 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr); |
bc85178a | 1290 | xfs_dir3_leaf_log_header(args, bp); |
24df33b4 | 1291 | |
3c1f9c15 | 1292 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
787b0893 | 1293 | xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index); |
24df33b4 | 1294 | |
1da177e4 LT |
1295 | /* |
1296 | * Make the data entry free. Keep track of the longest freespace | |
1297 | * in the data block in case it changes. | |
1298 | */ | |
1299 | dbp = dblk->bp; | |
1d9025e5 | 1300 | hdr = dbp->b_addr; |
c2066e26 | 1301 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + off); |
2ca98774 | 1302 | bf = dp->d_ops->data_bestfree_p(hdr); |
33363fee | 1303 | longest = be16_to_cpu(bf[0].length); |
1da177e4 | 1304 | needlog = needscan = 0; |
bc85178a | 1305 | xfs_dir2_data_make_free(args, dbp, off, |
9d23fc85 | 1306 | dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan); |
1da177e4 LT |
1307 | /* |
1308 | * Rescan the data block freespaces for bestfree. | |
1309 | * Log the data block header if needed. | |
1310 | */ | |
1311 | if (needscan) | |
9d23fc85 | 1312 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
1da177e4 | 1313 | if (needlog) |
bc85178a | 1314 | xfs_dir2_data_log_header(args, dbp); |
33363fee | 1315 | xfs_dir3_data_check(dp, dbp); |
1da177e4 LT |
1316 | /* |
1317 | * If the longest data block freespace changes, need to update | |
1318 | * the corresponding freeblock entry. | |
1319 | */ | |
33363fee | 1320 | if (longest < be16_to_cpu(bf[0].length)) { |
1da177e4 | 1321 | int error; /* error return value */ |
1d9025e5 | 1322 | struct xfs_buf *fbp; /* freeblock buffer */ |
1da177e4 LT |
1323 | xfs_dir2_db_t fdb; /* freeblock block number */ |
1324 | int findex; /* index in freeblock entries */ | |
1325 | xfs_dir2_free_t *free; /* freeblock structure */ | |
1da177e4 LT |
1326 | |
1327 | /* | |
1328 | * Convert the data block number to a free block, | |
1329 | * read in the free block. | |
1330 | */ | |
8f66193c | 1331 | fdb = dp->d_ops->db_to_fdb(args->geo, db); |
2998ab1d DC |
1332 | error = xfs_dir2_free_read(tp, dp, |
1333 | xfs_dir2_db_to_da(args->geo, fdb), | |
2025207c | 1334 | &fbp); |
4bb20a83 | 1335 | if (error) |
1da177e4 | 1336 | return error; |
1d9025e5 | 1337 | free = fbp->b_addr; |
cbc8adf8 DC |
1338 | #ifdef DEBUG |
1339 | { | |
1340 | struct xfs_dir3_icfree_hdr freehdr; | |
5ba30919 CH |
1341 | |
1342 | xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); | |
8f66193c | 1343 | ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) * |
30028030 DC |
1344 | (fdb - xfs_dir2_byte_to_db(args->geo, |
1345 | XFS_DIR2_FREE_OFFSET))); | |
cbc8adf8 DC |
1346 | } |
1347 | #endif | |
1da177e4 LT |
1348 | /* |
1349 | * Calculate which entry we need to fix. | |
1350 | */ | |
8f66193c | 1351 | findex = dp->d_ops->db_to_fdindex(args->geo, db); |
33363fee | 1352 | longest = be16_to_cpu(bf[0].length); |
1da177e4 LT |
1353 | /* |
1354 | * If the data block is now empty we can get rid of it | |
1355 | * (usually). | |
1356 | */ | |
8f66193c | 1357 | if (longest == args->geo->blksize - |
1c9a5b2e | 1358 | dp->d_ops->data_entry_offset) { |
1da177e4 LT |
1359 | /* |
1360 | * Try to punch out the data block. | |
1361 | */ | |
1362 | error = xfs_dir2_shrink_inode(args, db, dbp); | |
1363 | if (error == 0) { | |
1364 | dblk->bp = NULL; | |
c2066e26 | 1365 | hdr = NULL; |
1da177e4 LT |
1366 | } |
1367 | /* | |
1368 | * We can get ENOSPC if there's no space reservation. | |
1369 | * In this case just drop the buffer and some one else | |
1370 | * will eventually get rid of the empty block. | |
1371 | */ | |
2451337d | 1372 | else if (!(error == -ENOSPC && args->total == 0)) |
1da177e4 LT |
1373 | return error; |
1374 | } | |
1375 | /* | |
1376 | * If we got rid of the data block, we can eliminate that entry | |
1377 | * in the free block. | |
1378 | */ | |
cbc8adf8 | 1379 | error = xfs_dir3_data_block_free(args, hdr, free, |
2025207c DC |
1380 | fdb, findex, fbp, longest); |
1381 | if (error) | |
1382 | return error; | |
1da177e4 | 1383 | } |
2025207c | 1384 | |
4141956a | 1385 | xfs_dir3_leaf_check(dp, bp); |
1da177e4 | 1386 | /* |
9da096fd | 1387 | * Return indication of whether this leaf block is empty enough |
1da177e4 LT |
1388 | * to justify trying to join it with a neighbor. |
1389 | */ | |
545910bc | 1390 | *rval = (args->geo->leaf_hdr_size + |
787b0893 | 1391 | (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) < |
ed358c00 | 1392 | args->geo->magicpct; |
1da177e4 LT |
1393 | return 0; |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * Split the leaf entries in the old block into old and new blocks. | |
1398 | */ | |
1399 | int /* error */ | |
1400 | xfs_dir2_leafn_split( | |
1401 | xfs_da_state_t *state, /* btree cursor */ | |
1402 | xfs_da_state_blk_t *oldblk, /* original block */ | |
1403 | xfs_da_state_blk_t *newblk) /* newly created block */ | |
1404 | { | |
1405 | xfs_da_args_t *args; /* operation arguments */ | |
1406 | xfs_dablk_t blkno; /* new leaf block number */ | |
1407 | int error; /* error return value */ | |
4141956a | 1408 | struct xfs_inode *dp; |
1da177e4 LT |
1409 | |
1410 | /* | |
1411 | * Allocate space for a new leaf node. | |
1412 | */ | |
1413 | args = state->args; | |
4141956a | 1414 | dp = args->dp; |
1da177e4 LT |
1415 | ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); |
1416 | error = xfs_da_grow_inode(args, &blkno); | |
1417 | if (error) { | |
1418 | return error; | |
1419 | } | |
1420 | /* | |
1421 | * Initialize the new leaf block. | |
1422 | */ | |
2998ab1d | 1423 | error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno), |
24df33b4 DC |
1424 | &newblk->bp, XFS_DIR2_LEAFN_MAGIC); |
1425 | if (error) | |
1da177e4 | 1426 | return error; |
24df33b4 | 1427 | |
1da177e4 LT |
1428 | newblk->blkno = blkno; |
1429 | newblk->magic = XFS_DIR2_LEAFN_MAGIC; | |
1430 | /* | |
1431 | * Rebalance the entries across the two leaves, link the new | |
1432 | * block into the leaves. | |
1433 | */ | |
1434 | xfs_dir2_leafn_rebalance(state, oldblk, newblk); | |
f5ea1100 | 1435 | error = xfs_da3_blk_link(state, oldblk, newblk); |
1da177e4 LT |
1436 | if (error) { |
1437 | return error; | |
1438 | } | |
1439 | /* | |
1440 | * Insert the new entry in the correct block. | |
1441 | */ | |
1442 | if (state->inleaf) | |
1443 | error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); | |
1444 | else | |
1445 | error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); | |
1446 | /* | |
1447 | * Update last hashval in each block since we added the name. | |
1448 | */ | |
8e8877e6 DW |
1449 | oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL); |
1450 | newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL); | |
4141956a DC |
1451 | xfs_dir3_leaf_check(dp, oldblk->bp); |
1452 | xfs_dir3_leaf_check(dp, newblk->bp); | |
1da177e4 LT |
1453 | return error; |
1454 | } | |
1455 | ||
1456 | /* | |
1457 | * Check a leaf block and its neighbors to see if the block should be | |
1458 | * collapsed into one or the other neighbor. Always keep the block | |
1459 | * with the smaller block number. | |
1460 | * If the current block is over 50% full, don't try to join it, return 0. | |
1461 | * If the block is empty, fill in the state structure and return 2. | |
1462 | * If it can be collapsed, fill in the state structure and return 1. | |
1463 | * If nothing can be done, return 0. | |
1464 | */ | |
1465 | int /* error */ | |
1466 | xfs_dir2_leafn_toosmall( | |
1467 | xfs_da_state_t *state, /* btree cursor */ | |
1468 | int *action) /* resulting action to take */ | |
1469 | { | |
1470 | xfs_da_state_blk_t *blk; /* leaf block */ | |
1471 | xfs_dablk_t blkno; /* leaf block number */ | |
1d9025e5 | 1472 | struct xfs_buf *bp; /* leaf buffer */ |
1da177e4 LT |
1473 | int bytes; /* bytes in use */ |
1474 | int count; /* leaf live entry count */ | |
1475 | int error; /* error return value */ | |
1476 | int forward; /* sibling block direction */ | |
1477 | int i; /* sibling counter */ | |
1da177e4 LT |
1478 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
1479 | int rval; /* result from path_shift */ | |
24df33b4 DC |
1480 | struct xfs_dir3_icleaf_hdr leafhdr; |
1481 | struct xfs_dir2_leaf_entry *ents; | |
4141956a | 1482 | struct xfs_inode *dp = state->args->dp; |
1da177e4 LT |
1483 | |
1484 | /* | |
1485 | * Check for the degenerate case of the block being over 50% full. | |
1486 | * If so, it's not worth even looking to see if we might be able | |
1487 | * to coalesce with a sibling. | |
1488 | */ | |
1489 | blk = &state->path.blk[state->path.active - 1]; | |
24df33b4 | 1490 | leaf = blk->bp->b_addr; |
51842556 | 1491 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf); |
787b0893 | 1492 | ents = leafhdr.ents; |
4141956a | 1493 | xfs_dir3_leaf_check(dp, blk->bp); |
24df33b4 DC |
1494 | |
1495 | count = leafhdr.count - leafhdr.stale; | |
545910bc | 1496 | bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]); |
b2a21e7a | 1497 | if (bytes > (state->args->geo->blksize >> 1)) { |
1da177e4 LT |
1498 | /* |
1499 | * Blk over 50%, don't try to join. | |
1500 | */ | |
1501 | *action = 0; | |
1502 | return 0; | |
1503 | } | |
1504 | /* | |
1505 | * Check for the degenerate case of the block being empty. | |
1506 | * If the block is empty, we'll simply delete it, no need to | |
1507 | * coalesce it with a sibling block. We choose (arbitrarily) | |
1508 | * to merge with the forward block unless it is NULL. | |
1509 | */ | |
1510 | if (count == 0) { | |
1511 | /* | |
1512 | * Make altpath point to the block we want to keep and | |
1513 | * path point to the block we want to drop (this one). | |
1514 | */ | |
24df33b4 | 1515 | forward = (leafhdr.forw != 0); |
1da177e4 | 1516 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
f5ea1100 | 1517 | error = xfs_da3_path_shift(state, &state->altpath, forward, 0, |
1da177e4 LT |
1518 | &rval); |
1519 | if (error) | |
1520 | return error; | |
1521 | *action = rval ? 2 : 0; | |
1522 | return 0; | |
1523 | } | |
1524 | /* | |
1525 | * Examine each sibling block to see if we can coalesce with | |
1526 | * at least 25% free space to spare. We need to figure out | |
1527 | * whether to merge with the forward or the backward block. | |
1528 | * We prefer coalescing with the lower numbered sibling so as | |
1529 | * to shrink a directory over time. | |
1530 | */ | |
24df33b4 | 1531 | forward = leafhdr.forw < leafhdr.back; |
1da177e4 | 1532 | for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { |
24df33b4 DC |
1533 | struct xfs_dir3_icleaf_hdr hdr2; |
1534 | ||
1535 | blkno = forward ? leafhdr.forw : leafhdr.back; | |
1da177e4 LT |
1536 | if (blkno == 0) |
1537 | continue; | |
1538 | /* | |
1539 | * Read the sibling leaf block. | |
1540 | */ | |
4141956a | 1541 | error = xfs_dir3_leafn_read(state->args->trans, dp, |
e6f7667c | 1542 | blkno, -1, &bp); |
4bb20a83 | 1543 | if (error) |
1da177e4 | 1544 | return error; |
e6f7667c | 1545 | |
1da177e4 LT |
1546 | /* |
1547 | * Count bytes in the two blocks combined. | |
1548 | */ | |
24df33b4 | 1549 | count = leafhdr.count - leafhdr.stale; |
b2a21e7a DC |
1550 | bytes = state->args->geo->blksize - |
1551 | (state->args->geo->blksize >> 2); | |
24df33b4 | 1552 | |
1d9025e5 | 1553 | leaf = bp->b_addr; |
51842556 | 1554 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf); |
787b0893 | 1555 | ents = hdr2.ents; |
24df33b4 DC |
1556 | count += hdr2.count - hdr2.stale; |
1557 | bytes -= count * sizeof(ents[0]); | |
1558 | ||
1da177e4 LT |
1559 | /* |
1560 | * Fits with at least 25% to spare. | |
1561 | */ | |
1562 | if (bytes >= 0) | |
1563 | break; | |
1d9025e5 | 1564 | xfs_trans_brelse(state->args->trans, bp); |
1da177e4 LT |
1565 | } |
1566 | /* | |
1567 | * Didn't like either block, give up. | |
1568 | */ | |
1569 | if (i >= 2) { | |
1570 | *action = 0; | |
1571 | return 0; | |
1572 | } | |
1d9025e5 | 1573 | |
1da177e4 LT |
1574 | /* |
1575 | * Make altpath point to the block we want to keep (the lower | |
1576 | * numbered block) and path point to the block we want to drop. | |
1577 | */ | |
1578 | memcpy(&state->altpath, &state->path, sizeof(state->path)); | |
1579 | if (blkno < blk->blkno) | |
f5ea1100 | 1580 | error = xfs_da3_path_shift(state, &state->altpath, forward, 0, |
1da177e4 LT |
1581 | &rval); |
1582 | else | |
f5ea1100 | 1583 | error = xfs_da3_path_shift(state, &state->path, forward, 0, |
1da177e4 LT |
1584 | &rval); |
1585 | if (error) { | |
1586 | return error; | |
1587 | } | |
1588 | *action = rval ? 0 : 1; | |
1589 | return 0; | |
1590 | } | |
1591 | ||
1592 | /* | |
1593 | * Move all the leaf entries from drop_blk to save_blk. | |
1594 | * This is done as part of a join operation. | |
1595 | */ | |
1596 | void | |
1597 | xfs_dir2_leafn_unbalance( | |
1598 | xfs_da_state_t *state, /* cursor */ | |
1599 | xfs_da_state_blk_t *drop_blk, /* dead block */ | |
1600 | xfs_da_state_blk_t *save_blk) /* surviving block */ | |
1601 | { | |
1602 | xfs_da_args_t *args; /* operation arguments */ | |
1603 | xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ | |
1604 | xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ | |
24df33b4 DC |
1605 | struct xfs_dir3_icleaf_hdr savehdr; |
1606 | struct xfs_dir3_icleaf_hdr drophdr; | |
1607 | struct xfs_dir2_leaf_entry *sents; | |
1608 | struct xfs_dir2_leaf_entry *dents; | |
4141956a | 1609 | struct xfs_inode *dp = state->args->dp; |
1da177e4 LT |
1610 | |
1611 | args = state->args; | |
1612 | ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1613 | ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1d9025e5 DC |
1614 | drop_leaf = drop_blk->bp->b_addr; |
1615 | save_leaf = save_blk->bp->b_addr; | |
24df33b4 | 1616 | |
51842556 CH |
1617 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf); |
1618 | xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf); | |
787b0893 CH |
1619 | sents = savehdr.ents; |
1620 | dents = drophdr.ents; | |
24df33b4 | 1621 | |
1da177e4 LT |
1622 | /* |
1623 | * If there are any stale leaf entries, take this opportunity | |
1624 | * to purge them. | |
1625 | */ | |
24df33b4 DC |
1626 | if (drophdr.stale) |
1627 | xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp); | |
1628 | if (savehdr.stale) | |
1629 | xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp); | |
1630 | ||
1da177e4 LT |
1631 | /* |
1632 | * Move the entries from drop to the appropriate end of save. | |
1633 | */ | |
24df33b4 | 1634 | drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval); |
4141956a | 1635 | if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp)) |
24df33b4 DC |
1636 | xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, |
1637 | save_blk->bp, &savehdr, sents, 0, | |
1638 | drophdr.count); | |
1da177e4 | 1639 | else |
24df33b4 DC |
1640 | xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0, |
1641 | save_blk->bp, &savehdr, sents, | |
1642 | savehdr.count, drophdr.count); | |
1643 | save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval); | |
1644 | ||
1645 | /* log the changes made when moving the entries */ | |
163fbbb3 CH |
1646 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr); |
1647 | xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr); | |
bc85178a DC |
1648 | xfs_dir3_leaf_log_header(args, save_blk->bp); |
1649 | xfs_dir3_leaf_log_header(args, drop_blk->bp); | |
24df33b4 | 1650 | |
4141956a DC |
1651 | xfs_dir3_leaf_check(dp, save_blk->bp); |
1652 | xfs_dir3_leaf_check(dp, drop_blk->bp); | |
1da177e4 LT |
1653 | } |
1654 | ||
a07258a6 DC |
1655 | /* |
1656 | * Add a new data block to the directory at the free space index that the caller | |
1657 | * has specified. | |
1658 | */ | |
1659 | static int | |
1660 | xfs_dir2_node_add_datablk( | |
1661 | struct xfs_da_args *args, | |
1662 | struct xfs_da_state_blk *fblk, | |
1663 | xfs_dir2_db_t *dbno, | |
1664 | struct xfs_buf **dbpp, | |
1665 | struct xfs_buf **fbpp, | |
195b0a44 | 1666 | struct xfs_dir3_icfree_hdr *hdr, |
a07258a6 DC |
1667 | int *findex) |
1668 | { | |
1669 | struct xfs_inode *dp = args->dp; | |
1670 | struct xfs_trans *tp = args->trans; | |
1671 | struct xfs_mount *mp = dp->i_mount; | |
a07258a6 | 1672 | struct xfs_dir2_data_free *bf; |
a07258a6 DC |
1673 | xfs_dir2_db_t fbno; |
1674 | struct xfs_buf *fbp; | |
1675 | struct xfs_buf *dbp; | |
a07258a6 DC |
1676 | int error; |
1677 | ||
1678 | /* Not allowed to allocate, return failure. */ | |
0e822255 | 1679 | if (args->total == 0) |
a07258a6 DC |
1680 | return -ENOSPC; |
1681 | ||
1682 | /* Allocate and initialize the new data block. */ | |
1683 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno); | |
1684 | if (error) | |
1685 | return error; | |
1686 | error = xfs_dir3_data_init(args, *dbno, &dbp); | |
1687 | if (error) | |
1688 | return error; | |
1689 | ||
1690 | /* | |
1691 | * Get the freespace block corresponding to the data block | |
1692 | * that was just allocated. | |
1693 | */ | |
1694 | fbno = dp->d_ops->db_to_fdb(args->geo, *dbno); | |
1695 | error = xfs_dir2_free_try_read(tp, dp, | |
1696 | xfs_dir2_db_to_da(args->geo, fbno), &fbp); | |
1697 | if (error) | |
1698 | return error; | |
1699 | ||
1700 | /* | |
1701 | * If there wasn't a freespace block, the read will | |
1702 | * return a NULL fbp. Allocate and initialize a new one. | |
1703 | */ | |
1704 | if (!fbp) { | |
1705 | error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno); | |
1706 | if (error) | |
1707 | return error; | |
1708 | ||
1709 | if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) { | |
1710 | xfs_alert(mp, | |
1711 | "%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld", | |
1712 | __func__, (unsigned long long)dp->i_ino, | |
1713 | (long long)dp->d_ops->db_to_fdb(args->geo, *dbno), | |
1714 | (long long)*dbno, (long long)fbno); | |
1715 | if (fblk) { | |
1716 | xfs_alert(mp, | |
1717 | " fblk "PTR_FMT" blkno %llu index %d magic 0x%x", | |
1718 | fblk, (unsigned long long)fblk->blkno, | |
1719 | fblk->index, fblk->magic); | |
1720 | } else { | |
1721 | xfs_alert(mp, " ... fblk is NULL"); | |
1722 | } | |
1723 | XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp); | |
1724 | return -EFSCORRUPTED; | |
1725 | } | |
1726 | ||
1727 | /* Get a buffer for the new block. */ | |
1728 | error = xfs_dir3_free_get_buf(args, fbno, &fbp); | |
1729 | if (error) | |
1730 | return error; | |
a84f3d5c | 1731 | xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); |
a07258a6 DC |
1732 | |
1733 | /* Remember the first slot as our empty slot. */ | |
195b0a44 | 1734 | hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo, |
a07258a6 DC |
1735 | XFS_DIR2_FREE_OFFSET)) * |
1736 | dp->d_ops->free_max_bests(args->geo); | |
1737 | } else { | |
a84f3d5c | 1738 | xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr); |
a07258a6 DC |
1739 | } |
1740 | ||
1741 | /* Set the freespace block index from the data block number. */ | |
1742 | *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno); | |
1743 | ||
1744 | /* Extend the freespace table if the new data block is off the end. */ | |
195b0a44 | 1745 | if (*findex >= hdr->nvalid) { |
a07258a6 | 1746 | ASSERT(*findex < dp->d_ops->free_max_bests(args->geo)); |
195b0a44 | 1747 | hdr->nvalid = *findex + 1; |
a84f3d5c | 1748 | hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF); |
a07258a6 DC |
1749 | } |
1750 | ||
1751 | /* | |
1752 | * If this entry was for an empty data block (this should always be | |
1753 | * true) then update the header. | |
1754 | */ | |
a84f3d5c | 1755 | if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) { |
195b0a44 CH |
1756 | hdr->nused++; |
1757 | xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr); | |
a07258a6 DC |
1758 | xfs_dir2_free_log_header(args, fbp); |
1759 | } | |
1760 | ||
1761 | /* Update the freespace value for the new block in the table. */ | |
195b0a44 | 1762 | bf = dp->d_ops->data_bestfree_p(dbp->b_addr); |
a84f3d5c | 1763 | hdr->bests[*findex] = bf[0].length; |
a07258a6 DC |
1764 | |
1765 | *dbpp = dbp; | |
1766 | *fbpp = fbp; | |
1767 | return 0; | |
1768 | } | |
1769 | ||
0e822255 DC |
1770 | static int |
1771 | xfs_dir2_node_find_freeblk( | |
1772 | struct xfs_da_args *args, | |
1773 | struct xfs_da_state_blk *fblk, | |
1774 | xfs_dir2_db_t *dbnop, | |
1775 | struct xfs_buf **fbpp, | |
195b0a44 | 1776 | struct xfs_dir3_icfree_hdr *hdr, |
0e822255 DC |
1777 | int *findexp, |
1778 | int length) | |
1da177e4 | 1779 | { |
0e822255 DC |
1780 | struct xfs_inode *dp = args->dp; |
1781 | struct xfs_trans *tp = args->trans; | |
1782 | struct xfs_buf *fbp = NULL; | |
756c6f0f | 1783 | xfs_dir2_db_t firstfbno; |
0e822255 DC |
1784 | xfs_dir2_db_t lastfbno; |
1785 | xfs_dir2_db_t ifbno = -1; | |
1786 | xfs_dir2_db_t dbno = -1; | |
756c6f0f | 1787 | xfs_dir2_db_t fbno; |
0e822255 | 1788 | xfs_fileoff_t fo; |
610125ab | 1789 | int findex = 0; |
0e822255 | 1790 | int error; |
1da177e4 | 1791 | |
1da177e4 LT |
1792 | /* |
1793 | * If we came in with a freespace block that means that lookup | |
1794 | * found an entry with our hash value. This is the freespace | |
1795 | * block for that data entry. | |
1796 | */ | |
1797 | if (fblk) { | |
1798 | fbp = fblk->bp; | |
1da177e4 | 1799 | findex = fblk->index; |
a84f3d5c | 1800 | xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); |
1da177e4 | 1801 | if (findex >= 0) { |
0e822255 | 1802 | /* caller already found the freespace for us. */ |
195b0a44 | 1803 | ASSERT(findex < hdr->nvalid); |
a84f3d5c CH |
1804 | ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF); |
1805 | ASSERT(be16_to_cpu(hdr->bests[findex]) >= length); | |
195b0a44 | 1806 | dbno = hdr->firstdb + findex; |
a07258a6 | 1807 | goto found_block; |
1da177e4 | 1808 | } |
0e822255 | 1809 | |
cbc8adf8 | 1810 | /* |
0e822255 DC |
1811 | * The data block looked at didn't have enough room. |
1812 | * We'll start at the beginning of the freespace entries. | |
cbc8adf8 | 1813 | */ |
0e822255 | 1814 | ifbno = fblk->blkno; |
610125ab DC |
1815 | xfs_trans_brelse(tp, fbp); |
1816 | fbp = NULL; | |
1817 | fblk->bp = NULL; | |
1da177e4 | 1818 | } |
cbc8adf8 | 1819 | |
1da177e4 | 1820 | /* |
0e822255 | 1821 | * If we don't have a data block yet, we're going to scan the freespace |
610125ab | 1822 | * data for a data block with enough free space in it. |
1da177e4 | 1823 | */ |
0e822255 DC |
1824 | error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK); |
1825 | if (error) | |
1826 | return error; | |
1827 | lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo); | |
756c6f0f | 1828 | firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET); |
0e822255 | 1829 | |
756c6f0f | 1830 | for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) { |
610125ab DC |
1831 | /* If it's ifbno we already looked at it. */ |
1832 | if (fbno == ifbno) | |
1833 | continue; | |
1834 | ||
1da177e4 | 1835 | /* |
610125ab DC |
1836 | * Read the block. There can be holes in the freespace blocks, |
1837 | * so this might not succeed. This should be really rare, so | |
1838 | * there's no reason to avoid it. | |
1da177e4 | 1839 | */ |
610125ab DC |
1840 | error = xfs_dir2_free_try_read(tp, dp, |
1841 | xfs_dir2_db_to_da(args->geo, fbno), | |
1842 | &fbp); | |
1843 | if (error) | |
1844 | return error; | |
1845 | if (!fbp) | |
1846 | continue; | |
1847 | ||
a84f3d5c | 1848 | xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr); |
610125ab DC |
1849 | |
1850 | /* Scan the free entry array for a large enough free space. */ | |
195b0a44 | 1851 | for (findex = hdr->nvalid - 1; findex >= 0; findex--) { |
a84f3d5c CH |
1852 | if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF && |
1853 | be16_to_cpu(hdr->bests[findex]) >= length) { | |
195b0a44 | 1854 | dbno = hdr->firstdb + findex; |
610125ab | 1855 | goto found_block; |
1da177e4 LT |
1856 | } |
1857 | } | |
610125ab DC |
1858 | |
1859 | /* Didn't find free space, go on to next free block */ | |
1860 | xfs_trans_brelse(tp, fbp); | |
1da177e4 | 1861 | } |
610125ab | 1862 | |
0e822255 DC |
1863 | found_block: |
1864 | *dbnop = dbno; | |
1865 | *fbpp = fbp; | |
1866 | *findexp = findex; | |
1867 | return 0; | |
1868 | } | |
1869 | ||
0e822255 DC |
1870 | /* |
1871 | * Add the data entry for a node-format directory name addition. | |
1872 | * The leaf entry is added in xfs_dir2_leafn_add. | |
1873 | * We may enter with a freespace block that the lookup found. | |
1874 | */ | |
1875 | static int | |
1876 | xfs_dir2_node_addname_int( | |
1877 | struct xfs_da_args *args, /* operation arguments */ | |
1878 | struct xfs_da_state_blk *fblk) /* optional freespace block */ | |
1879 | { | |
1880 | struct xfs_dir2_data_unused *dup; /* data unused entry pointer */ | |
1881 | struct xfs_dir2_data_entry *dep; /* data entry pointer */ | |
1882 | struct xfs_dir2_data_hdr *hdr; /* data block header */ | |
1883 | struct xfs_dir2_data_free *bf; | |
0e822255 DC |
1884 | struct xfs_trans *tp = args->trans; |
1885 | struct xfs_inode *dp = args->dp; | |
195b0a44 | 1886 | struct xfs_dir3_icfree_hdr freehdr; |
0e822255 DC |
1887 | struct xfs_buf *dbp; /* data block buffer */ |
1888 | struct xfs_buf *fbp; /* freespace buffer */ | |
1889 | xfs_dir2_data_aoff_t aoff; | |
1890 | xfs_dir2_db_t dbno; /* data block number */ | |
1891 | int error; /* error return value */ | |
1892 | int findex; /* freespace entry index */ | |
1893 | int length; /* length of the new entry */ | |
1894 | int logfree = 0; /* need to log free entry */ | |
1895 | int needlog = 0; /* need to log data header */ | |
1896 | int needscan = 0; /* need to rescan data frees */ | |
1897 | __be16 *tagp; /* data entry tag pointer */ | |
0e822255 DC |
1898 | |
1899 | length = dp->d_ops->data_entsize(args->namelen); | |
195b0a44 CH |
1900 | error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr, |
1901 | &findex, length); | |
0e822255 DC |
1902 | if (error) |
1903 | return error; | |
1904 | ||
1905 | /* | |
1906 | * Now we know if we must allocate blocks, so if we are checking whether | |
1907 | * we can insert without allocation then we can return now. | |
1908 | */ | |
1909 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) { | |
1910 | if (dbno == -1) | |
1911 | return -ENOSPC; | |
1912 | return 0; | |
1913 | } | |
a07258a6 | 1914 | |
1da177e4 LT |
1915 | /* |
1916 | * If we don't have a data block, we need to allocate one and make | |
1917 | * the freespace entries refer to it. | |
1918 | */ | |
a07258a6 | 1919 | if (dbno == -1) { |
a07258a6 | 1920 | /* we're going to have to log the free block index later */ |
1da177e4 | 1921 | logfree = 1; |
0e822255 | 1922 | error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp, |
195b0a44 | 1923 | &freehdr, &findex); |
a07258a6 | 1924 | } else { |
a07258a6 | 1925 | /* Read the data block in. */ |
2998ab1d DC |
1926 | error = xfs_dir3_data_read(tp, dp, |
1927 | xfs_dir2_db_to_da(args->geo, dbno), | |
e4813572 | 1928 | -1, &dbp); |
1da177e4 | 1929 | } |
0e822255 DC |
1930 | if (error) |
1931 | return error; | |
a07258a6 DC |
1932 | |
1933 | /* setup for data block up now */ | |
1934 | hdr = dbp->b_addr; | |
1935 | bf = dp->d_ops->data_bestfree_p(hdr); | |
33363fee | 1936 | ASSERT(be16_to_cpu(bf[0].length) >= length); |
a07258a6 DC |
1937 | |
1938 | /* Point to the existing unused space. */ | |
1da177e4 | 1939 | dup = (xfs_dir2_data_unused_t *) |
33363fee | 1940 | ((char *)hdr + be16_to_cpu(bf[0].offset)); |
a07258a6 DC |
1941 | |
1942 | /* Mark the first part of the unused space, inuse for us. */ | |
6915ef35 DW |
1943 | aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
1944 | error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length, | |
1945 | &needlog, &needscan); | |
1946 | if (error) { | |
1947 | xfs_trans_brelse(tp, dbp); | |
1948 | return error; | |
1949 | } | |
a07258a6 DC |
1950 | |
1951 | /* Fill in the new entry and log it. */ | |
1da177e4 | 1952 | dep = (xfs_dir2_data_entry_t *)dup; |
ff9901c1 | 1953 | dep->inumber = cpu_to_be64(args->inumber); |
1da177e4 LT |
1954 | dep->namelen = args->namelen; |
1955 | memcpy(dep->name, args->name, dep->namelen); | |
9d23fc85 DC |
1956 | dp->d_ops->data_put_ftype(dep, args->filetype); |
1957 | tagp = dp->d_ops->data_entry_tag_p(dep); | |
c2066e26 | 1958 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); |
bc85178a | 1959 | xfs_dir2_data_log_entry(args, dbp, dep); |
a07258a6 DC |
1960 | |
1961 | /* Rescan the freespace and log the data block if needed. */ | |
1da177e4 | 1962 | if (needscan) |
9d23fc85 | 1963 | xfs_dir2_data_freescan(dp, hdr, &needlog); |
1da177e4 | 1964 | if (needlog) |
bc85178a | 1965 | xfs_dir2_data_log_header(args, dbp); |
a07258a6 DC |
1966 | |
1967 | /* If the freespace block entry is now wrong, update it. */ | |
a84f3d5c CH |
1968 | if (freehdr.bests[findex] != bf[0].length) { |
1969 | freehdr.bests[findex] = bf[0].length; | |
1da177e4 LT |
1970 | logfree = 1; |
1971 | } | |
a07258a6 DC |
1972 | |
1973 | /* Log the freespace entry if needed. */ | |
1da177e4 | 1974 | if (logfree) |
a84f3d5c | 1975 | xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex); |
a07258a6 DC |
1976 | |
1977 | /* Return the data block and offset in args. */ | |
1da177e4 | 1978 | args->blkno = (xfs_dablk_t)dbno; |
3d693c6e | 1979 | args->index = be16_to_cpu(*tagp); |
1da177e4 LT |
1980 | return 0; |
1981 | } | |
1982 | ||
aee7754b DC |
1983 | /* |
1984 | * Top-level node form directory addname routine. | |
1985 | */ | |
1986 | int /* error */ | |
1987 | xfs_dir2_node_addname( | |
1988 | xfs_da_args_t *args) /* operation arguments */ | |
1989 | { | |
1990 | xfs_da_state_blk_t *blk; /* leaf block for insert */ | |
1991 | int error; /* error return value */ | |
1992 | int rval; /* sub-return value */ | |
1993 | xfs_da_state_t *state; /* btree cursor */ | |
1994 | ||
1995 | trace_xfs_dir2_node_addname(args); | |
1996 | ||
1997 | /* | |
1998 | * Allocate and initialize the state (btree cursor). | |
1999 | */ | |
2000 | state = xfs_da_state_alloc(); | |
2001 | state->args = args; | |
2002 | state->mp = args->dp->i_mount; | |
2003 | /* | |
2004 | * Look up the name. We're not supposed to find it, but | |
2005 | * this gives us the insertion point. | |
2006 | */ | |
2007 | error = xfs_da3_node_lookup_int(state, &rval); | |
2008 | if (error) | |
2009 | rval = error; | |
2010 | if (rval != -ENOENT) { | |
2011 | goto done; | |
2012 | } | |
2013 | /* | |
2014 | * Add the data entry to a data block. | |
2015 | * Extravalid is set to a freeblock found by lookup. | |
2016 | */ | |
2017 | rval = xfs_dir2_node_addname_int(args, | |
2018 | state->extravalid ? &state->extrablk : NULL); | |
2019 | if (rval) { | |
2020 | goto done; | |
2021 | } | |
2022 | blk = &state->path.blk[state->path.active - 1]; | |
2023 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
2024 | /* | |
2025 | * Add the new leaf entry. | |
2026 | */ | |
2027 | rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); | |
2028 | if (rval == 0) { | |
2029 | /* | |
2030 | * It worked, fix the hash values up the btree. | |
2031 | */ | |
2032 | if (!(args->op_flags & XFS_DA_OP_JUSTCHECK)) | |
2033 | xfs_da3_fixhashpath(state, &state->path); | |
2034 | } else { | |
2035 | /* | |
2036 | * It didn't work, we need to split the leaf block. | |
2037 | */ | |
2038 | if (args->total == 0) { | |
2039 | ASSERT(rval == -ENOSPC); | |
2040 | goto done; | |
2041 | } | |
2042 | /* | |
2043 | * Split the leaf block and insert the new entry. | |
2044 | */ | |
2045 | rval = xfs_da3_split(state); | |
2046 | } | |
2047 | done: | |
2048 | xfs_da_state_free(state); | |
2049 | return rval; | |
2050 | } | |
2051 | ||
1da177e4 LT |
2052 | /* |
2053 | * Lookup an entry in a node-format directory. | |
f5ea1100 | 2054 | * All the real work happens in xfs_da3_node_lookup_int. |
1da177e4 LT |
2055 | * The only real output is the inode number of the entry. |
2056 | */ | |
2057 | int /* error */ | |
2058 | xfs_dir2_node_lookup( | |
2059 | xfs_da_args_t *args) /* operation arguments */ | |
2060 | { | |
2061 | int error; /* error return value */ | |
2062 | int i; /* btree level */ | |
2063 | int rval; /* operation return value */ | |
2064 | xfs_da_state_t *state; /* btree cursor */ | |
2065 | ||
0b1b213f CH |
2066 | trace_xfs_dir2_node_lookup(args); |
2067 | ||
1da177e4 LT |
2068 | /* |
2069 | * Allocate and initialize the btree cursor. | |
2070 | */ | |
2071 | state = xfs_da_state_alloc(); | |
2072 | state->args = args; | |
2073 | state->mp = args->dp->i_mount; | |
1da177e4 LT |
2074 | /* |
2075 | * Fill in the path to the entry in the cursor. | |
2076 | */ | |
f5ea1100 | 2077 | error = xfs_da3_node_lookup_int(state, &rval); |
1da177e4 LT |
2078 | if (error) |
2079 | rval = error; | |
2451337d DC |
2080 | else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) { |
2081 | /* If a CI match, dup the actual name and return -EEXIST */ | |
384f3ced BN |
2082 | xfs_dir2_data_entry_t *dep; |
2083 | ||
1d9025e5 DC |
2084 | dep = (xfs_dir2_data_entry_t *) |
2085 | ((char *)state->extrablk.bp->b_addr + | |
2086 | state->extrablk.index); | |
384f3ced BN |
2087 | rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen); |
2088 | } | |
1da177e4 LT |
2089 | /* |
2090 | * Release the btree blocks and leaf block. | |
2091 | */ | |
2092 | for (i = 0; i < state->path.active; i++) { | |
1d9025e5 | 2093 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
1da177e4 LT |
2094 | state->path.blk[i].bp = NULL; |
2095 | } | |
2096 | /* | |
2097 | * Release the data block if we have it. | |
2098 | */ | |
2099 | if (state->extravalid && state->extrablk.bp) { | |
1d9025e5 | 2100 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
1da177e4 LT |
2101 | state->extrablk.bp = NULL; |
2102 | } | |
2103 | xfs_da_state_free(state); | |
2104 | return rval; | |
2105 | } | |
2106 | ||
2107 | /* | |
2108 | * Remove an entry from a node-format directory. | |
2109 | */ | |
2110 | int /* error */ | |
2111 | xfs_dir2_node_removename( | |
3a8c9208 | 2112 | struct xfs_da_args *args) /* operation arguments */ |
1da177e4 | 2113 | { |
3a8c9208 | 2114 | struct xfs_da_state_blk *blk; /* leaf block */ |
1da177e4 LT |
2115 | int error; /* error return value */ |
2116 | int rval; /* operation return value */ | |
3a8c9208 | 2117 | struct xfs_da_state *state; /* btree cursor */ |
1da177e4 | 2118 | |
0b1b213f CH |
2119 | trace_xfs_dir2_node_removename(args); |
2120 | ||
1da177e4 LT |
2121 | /* |
2122 | * Allocate and initialize the btree cursor. | |
2123 | */ | |
2124 | state = xfs_da_state_alloc(); | |
2125 | state->args = args; | |
2126 | state->mp = args->dp->i_mount; | |
3a8c9208 MT |
2127 | |
2128 | /* Look up the entry we're deleting, set up the cursor. */ | |
f5ea1100 | 2129 | error = xfs_da3_node_lookup_int(state, &rval); |
5163f95a | 2130 | if (error) |
3a8c9208 MT |
2131 | goto out_free; |
2132 | ||
2133 | /* Didn't find it, upper layer screwed up. */ | |
2451337d | 2134 | if (rval != -EEXIST) { |
3a8c9208 MT |
2135 | error = rval; |
2136 | goto out_free; | |
1da177e4 | 2137 | } |
3a8c9208 | 2138 | |
1da177e4 LT |
2139 | blk = &state->path.blk[state->path.active - 1]; |
2140 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
2141 | ASSERT(state->extravalid); | |
2142 | /* | |
2143 | * Remove the leaf and data entries. | |
2144 | * Extrablk refers to the data block. | |
2145 | */ | |
2146 | error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, | |
2147 | &state->extrablk, &rval); | |
5163f95a | 2148 | if (error) |
3a8c9208 | 2149 | goto out_free; |
1da177e4 LT |
2150 | /* |
2151 | * Fix the hash values up the btree. | |
2152 | */ | |
f5ea1100 | 2153 | xfs_da3_fixhashpath(state, &state->path); |
1da177e4 LT |
2154 | /* |
2155 | * If we need to join leaf blocks, do it. | |
2156 | */ | |
2157 | if (rval && state->path.active > 1) | |
f5ea1100 | 2158 | error = xfs_da3_join(state); |
1da177e4 LT |
2159 | /* |
2160 | * If no errors so far, try conversion to leaf format. | |
2161 | */ | |
2162 | if (!error) | |
2163 | error = xfs_dir2_node_to_leaf(state); | |
3a8c9208 | 2164 | out_free: |
1da177e4 LT |
2165 | xfs_da_state_free(state); |
2166 | return error; | |
2167 | } | |
2168 | ||
2169 | /* | |
2170 | * Replace an entry's inode number in a node-format directory. | |
2171 | */ | |
2172 | int /* error */ | |
2173 | xfs_dir2_node_replace( | |
2174 | xfs_da_args_t *args) /* operation arguments */ | |
2175 | { | |
2176 | xfs_da_state_blk_t *blk; /* leaf block */ | |
c2066e26 | 2177 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
1da177e4 LT |
2178 | xfs_dir2_data_entry_t *dep; /* data entry changed */ |
2179 | int error; /* error return value */ | |
2180 | int i; /* btree level */ | |
2181 | xfs_ino_t inum; /* new inode number */ | |
03754234 | 2182 | int ftype; /* new file type */ |
1da177e4 LT |
2183 | int rval; /* internal return value */ |
2184 | xfs_da_state_t *state; /* btree cursor */ | |
2185 | ||
0b1b213f CH |
2186 | trace_xfs_dir2_node_replace(args); |
2187 | ||
1da177e4 LT |
2188 | /* |
2189 | * Allocate and initialize the btree cursor. | |
2190 | */ | |
2191 | state = xfs_da_state_alloc(); | |
2192 | state->args = args; | |
2193 | state->mp = args->dp->i_mount; | |
03754234 JK |
2194 | |
2195 | /* | |
2196 | * We have to save new inode number and ftype since | |
2197 | * xfs_da3_node_lookup_int() is going to overwrite them | |
2198 | */ | |
1da177e4 | 2199 | inum = args->inumber; |
03754234 JK |
2200 | ftype = args->filetype; |
2201 | ||
1da177e4 LT |
2202 | /* |
2203 | * Lookup the entry to change in the btree. | |
2204 | */ | |
f5ea1100 | 2205 | error = xfs_da3_node_lookup_int(state, &rval); |
1da177e4 LT |
2206 | if (error) { |
2207 | rval = error; | |
2208 | } | |
2209 | /* | |
2210 | * It should be found, since the vnodeops layer has looked it up | |
2211 | * and locked it. But paranoia is good. | |
2212 | */ | |
2451337d | 2213 | if (rval == -EEXIST) { |
787b0893 CH |
2214 | struct xfs_dir3_icleaf_hdr leafhdr; |
2215 | ||
1da177e4 LT |
2216 | /* |
2217 | * Find the leaf entry. | |
2218 | */ | |
2219 | blk = &state->path.blk[state->path.active - 1]; | |
2220 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); | |
1da177e4 | 2221 | ASSERT(state->extravalid); |
787b0893 CH |
2222 | |
2223 | xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr, | |
2224 | blk->bp->b_addr); | |
1da177e4 LT |
2225 | /* |
2226 | * Point to the data entry. | |
2227 | */ | |
1d9025e5 | 2228 | hdr = state->extrablk.bp->b_addr; |
33363fee DC |
2229 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
2230 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC)); | |
1da177e4 | 2231 | dep = (xfs_dir2_data_entry_t *) |
c2066e26 | 2232 | ((char *)hdr + |
30028030 | 2233 | xfs_dir2_dataptr_to_off(args->geo, |
787b0893 | 2234 | be32_to_cpu(leafhdr.ents[blk->index].address))); |
ff9901c1 | 2235 | ASSERT(inum != be64_to_cpu(dep->inumber)); |
1da177e4 LT |
2236 | /* |
2237 | * Fill in the new inode number and log the entry. | |
2238 | */ | |
ff9901c1 | 2239 | dep->inumber = cpu_to_be64(inum); |
03754234 | 2240 | args->dp->d_ops->data_put_ftype(dep, ftype); |
bc85178a | 2241 | xfs_dir2_data_log_entry(args, state->extrablk.bp, dep); |
1da177e4 LT |
2242 | rval = 0; |
2243 | } | |
2244 | /* | |
2245 | * Didn't find it, and we're holding a data block. Drop it. | |
2246 | */ | |
2247 | else if (state->extravalid) { | |
1d9025e5 | 2248 | xfs_trans_brelse(args->trans, state->extrablk.bp); |
1da177e4 LT |
2249 | state->extrablk.bp = NULL; |
2250 | } | |
2251 | /* | |
2252 | * Release all the buffers in the cursor. | |
2253 | */ | |
2254 | for (i = 0; i < state->path.active; i++) { | |
1d9025e5 | 2255 | xfs_trans_brelse(args->trans, state->path.blk[i].bp); |
1da177e4 LT |
2256 | state->path.blk[i].bp = NULL; |
2257 | } | |
2258 | xfs_da_state_free(state); | |
2259 | return rval; | |
2260 | } | |
2261 | ||
2262 | /* | |
2263 | * Trim off a trailing empty freespace block. | |
2264 | * Return (in rvalp) 1 if we did it, 0 if not. | |
2265 | */ | |
2266 | int /* error */ | |
2267 | xfs_dir2_node_trim_free( | |
2268 | xfs_da_args_t *args, /* operation arguments */ | |
2269 | xfs_fileoff_t fo, /* free block number */ | |
2270 | int *rvalp) /* out: did something */ | |
2271 | { | |
1d9025e5 | 2272 | struct xfs_buf *bp; /* freespace buffer */ |
1da177e4 LT |
2273 | xfs_inode_t *dp; /* incore directory inode */ |
2274 | int error; /* error return code */ | |
2275 | xfs_dir2_free_t *free; /* freespace structure */ | |
1da177e4 | 2276 | xfs_trans_t *tp; /* transaction pointer */ |
cbc8adf8 | 2277 | struct xfs_dir3_icfree_hdr freehdr; |
1da177e4 LT |
2278 | |
2279 | dp = args->dp; | |
1da177e4 | 2280 | tp = args->trans; |
355cced4 CH |
2281 | |
2282 | *rvalp = 0; | |
2283 | ||
1da177e4 LT |
2284 | /* |
2285 | * Read the freespace block. | |
2286 | */ | |
2025207c | 2287 | error = xfs_dir2_free_try_read(tp, dp, fo, &bp); |
4bb20a83 | 2288 | if (error) |
1da177e4 | 2289 | return error; |
1da177e4 LT |
2290 | /* |
2291 | * There can be holes in freespace. If fo is a hole, there's | |
2292 | * nothing to do. | |
2293 | */ | |
2025207c | 2294 | if (!bp) |
1da177e4 | 2295 | return 0; |
1d9025e5 | 2296 | free = bp->b_addr; |
5ba30919 | 2297 | xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free); |
cbc8adf8 | 2298 | |
1da177e4 LT |
2299 | /* |
2300 | * If there are used entries, there's nothing to do. | |
2301 | */ | |
cbc8adf8 | 2302 | if (freehdr.nused > 0) { |
1d9025e5 | 2303 | xfs_trans_brelse(tp, bp); |
1da177e4 LT |
2304 | return 0; |
2305 | } | |
2306 | /* | |
2307 | * Blow the block away. | |
2308 | */ | |
2998ab1d DC |
2309 | error = xfs_dir2_shrink_inode(args, |
2310 | xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp); | |
2311 | if (error) { | |
1da177e4 LT |
2312 | /* |
2313 | * Can't fail with ENOSPC since that only happens with no | |
2314 | * space reservation, when breaking up an extent into two | |
2315 | * pieces. This is the last block of an extent. | |
2316 | */ | |
2451337d | 2317 | ASSERT(error != -ENOSPC); |
1d9025e5 | 2318 | xfs_trans_brelse(tp, bp); |
1da177e4 LT |
2319 | return error; |
2320 | } | |
2321 | /* | |
2322 | * Return that we succeeded. | |
2323 | */ | |
2324 | *rvalp = 1; | |
2325 | return 0; | |
2326 | } |