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