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