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