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