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 : xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
42 : {
43 14161332 : return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
44 3 : (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 : xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
52 : {
53 14163332 : 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 209635523 : xfs_dir3_leafn_check(
62 : struct xfs_inode *dp,
63 : struct xfs_buf *bp)
64 : {
65 209635523 : struct xfs_dir2_leaf *leaf = bp->b_addr;
66 209635523 : struct xfs_dir3_icleaf_hdr leafhdr;
67 :
68 209635523 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
69 :
70 209633908 : if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
71 209633908 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
72 209633908 : 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 209633908 : return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
78 : }
79 :
80 : static inline void
81 209632933 : xfs_dir3_leaf_check(
82 : struct xfs_inode *dp,
83 : struct xfs_buf *bp)
84 : {
85 209632933 : xfs_failaddr_t fa;
86 :
87 209632933 : fa = xfs_dir3_leafn_check(dp, bp);
88 209638321 : 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 185372 : xfs_dir3_free_verify(
101 : struct xfs_buf *bp)
102 : {
103 185372 : struct xfs_mount *mp = bp->b_mount;
104 185372 : struct xfs_dir2_free_hdr *hdr = bp->b_addr;
105 :
106 185372 : if (!xfs_verify_magic(bp, hdr->magic))
107 0 : return __this_address;
108 :
109 185372 : if (xfs_has_crc(mp)) {
110 185372 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
111 :
112 185372 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
113 0 : return __this_address;
114 185372 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
115 0 : return __this_address;
116 185372 : 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 28695 : xfs_dir3_free_read_verify(
127 : struct xfs_buf *bp)
128 : {
129 28695 : struct xfs_mount *mp = bp->b_mount;
130 28695 : xfs_failaddr_t fa;
131 :
132 57390 : if (xfs_has_crc(mp) &&
133 : !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
134 2 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
135 : else {
136 28693 : fa = xfs_dir3_free_verify(bp);
137 28693 : if (fa)
138 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
139 : }
140 28695 : }
141 :
142 : static void
143 140052 : xfs_dir3_free_write_verify(
144 : struct xfs_buf *bp)
145 : {
146 140052 : struct xfs_mount *mp = bp->b_mount;
147 140052 : struct xfs_buf_log_item *bip = bp->b_log_item;
148 140052 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
149 140052 : xfs_failaddr_t fa;
150 :
151 140052 : fa = xfs_dir3_free_verify(bp);
152 140052 : if (fa) {
153 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
154 0 : return;
155 : }
156 :
157 140052 : if (!xfs_has_crc(mp))
158 : return;
159 :
160 140052 : if (bip)
161 140052 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
162 :
163 140052 : 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 45276663 : xfs_dir3_free_header_check(
178 : struct xfs_buf *bp,
179 : xfs_ino_t owner,
180 : xfs_dablk_t fbno)
181 : {
182 45276663 : struct xfs_mount *mp = bp->b_mount;
183 45276663 : int maxbests = mp->m_dir_geo->free_max_bests;
184 45276663 : unsigned int firstdb;
185 :
186 45276663 : firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
187 : xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
188 : maxbests;
189 45276663 : if (xfs_has_crc(mp)) {
190 45276663 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
191 :
192 90553326 : if (be32_to_cpu(hdr3->firstdb) != firstdb)
193 0 : return __this_address;
194 90562070 : if (be32_to_cpu(hdr3->nvalid) > maxbests)
195 0 : return __this_address;
196 90562070 : if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
197 0 : return __this_address;
198 45281035 : 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 45277205 : __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 45277205 : xfs_failaddr_t fa;
223 45277205 : int err;
224 :
225 45277205 : err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
226 : &xfs_dir3_free_buf_ops);
227 45282791 : if (err || !*bpp)
228 : return err;
229 :
230 : /* Check things that we can't do in the verifier. */
231 45282634 : fa = xfs_dir3_free_header_check(*bpp, owner, fbno);
232 45281835 : 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 45281835 : if (tp)
242 45281835 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
243 :
244 : return 0;
245 : }
246 :
247 : void
248 59668438 : 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 59668438 : if (xfs_has_crc(mp)) {
254 59668438 : struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
255 :
256 59668438 : to->magic = be32_to_cpu(from3->hdr.hdr.magic);
257 59668438 : to->firstdb = be32_to_cpu(from3->hdr.firstdb);
258 59668438 : to->nvalid = be32_to_cpu(from3->hdr.nvalid);
259 59668438 : to->nused = be32_to_cpu(from3->hdr.nused);
260 59668438 : to->bests = from3->bests;
261 :
262 59668438 : 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 59668438 : }
273 :
274 : static void
275 53103 : 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 53103 : if (xfs_has_crc(mp)) {
281 53103 : struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
282 :
283 53103 : ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
284 :
285 53103 : to3->hdr.hdr.magic = cpu_to_be32(from->magic);
286 53103 : to3->hdr.firstdb = cpu_to_be32(from->firstdb);
287 53103 : to3->hdr.nvalid = cpu_to_be32(from->nvalid);
288 106206 : 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 53103 : }
298 :
299 : int
300 17977 : 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 12742363 : 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 32532118 : return __xfs_dir3_free_read(tp, dp, owner, fbno, XFS_DABUF_MAP_HOLE_OK,
319 : bpp);
320 : }
321 :
322 : static int
323 1696 : xfs_dir3_free_get_buf(
324 : xfs_da_args_t *args,
325 : xfs_dir2_db_t fbno,
326 : struct xfs_buf **bpp)
327 : {
328 1696 : struct xfs_trans *tp = args->trans;
329 1696 : struct xfs_inode *dp = args->dp;
330 1696 : struct xfs_mount *mp = dp->i_mount;
331 1696 : struct xfs_buf *bp;
332 1696 : int error;
333 1696 : struct xfs_dir3_icfree_hdr hdr;
334 :
335 1696 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
336 : &bp, XFS_DATA_FORK);
337 1696 : if (error)
338 : return error;
339 :
340 1696 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
341 1696 : 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 1696 : memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
348 1696 : memset(&hdr, 0, sizeof(hdr));
349 :
350 1696 : if (xfs_has_crc(mp)) {
351 1696 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
352 :
353 1696 : hdr.magic = XFS_DIR3_FREE_MAGIC;
354 :
355 1696 : hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
356 1696 : hdr3->hdr.owner = cpu_to_be64(args->owner);
357 1696 : uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
358 : } else
359 0 : hdr.magic = XFS_DIR2_FREE_MAGIC;
360 1696 : xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
361 1696 : *bpp = bp;
362 1696 : return 0;
363 : }
364 :
365 : /*
366 : * Log entries from a freespace block.
367 : */
368 : STATIC void
369 28445631 : 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 28445631 : struct xfs_dir2_free *free = bp->b_addr;
377 :
378 28445631 : ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
379 : free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
380 28445631 : xfs_trans_log_buf(args->trans, bp,
381 28445631 : (char *)&hdr->bests[first] - (char *)free,
382 28445631 : (char *)&hdr->bests[last] - (char *)free +
383 : sizeof(hdr->bests[0]) - 1);
384 28445510 : }
385 :
386 : /*
387 : * Log header from a freespace block.
388 : */
389 : static void
390 51407 : xfs_dir2_free_log_header(
391 : struct xfs_da_args *args,
392 : struct xfs_buf *bp)
393 : {
394 : #ifdef DEBUG
395 51407 : xfs_dir2_free_t *free; /* freespace structure */
396 :
397 51407 : free = bp->b_addr;
398 51407 : 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 51407 : xfs_trans_log_buf(args->trans, bp, 0,
402 51407 : args->geo->free_hdr_size - 1);
403 51407 : }
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 1693 : xfs_dir2_leaf_to_node(
412 : xfs_da_args_t *args, /* operation arguments */
413 : struct xfs_buf *lbp) /* leaf buffer */
414 : {
415 1693 : xfs_inode_t *dp; /* incore directory inode */
416 1693 : int error; /* error return value */
417 1693 : struct xfs_buf *fbp; /* freespace buffer */
418 1693 : xfs_dir2_db_t fdb; /* freespace block number */
419 1693 : __be16 *from; /* pointer to freespace entry */
420 1693 : int i; /* leaf freespace index */
421 1693 : xfs_dir2_leaf_t *leaf; /* leaf structure */
422 1693 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
423 1693 : int n; /* count of live freespc ents */
424 1693 : xfs_dir2_data_off_t off; /* freespace entry value */
425 1693 : xfs_trans_t *tp; /* transaction pointer */
426 1693 : struct xfs_dir3_icfree_hdr freehdr;
427 :
428 1693 : trace_xfs_dir2_leaf_to_node(args);
429 :
430 1693 : dp = args->dp;
431 1693 : tp = args->trans;
432 : /*
433 : * Add a freespace block to the directory.
434 : */
435 1693 : if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
436 : return error;
437 : }
438 1693 : 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 1693 : error = xfs_dir3_free_get_buf(args, fdb, &fbp);
443 1693 : if (error)
444 : return error;
445 :
446 1693 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
447 1693 : leaf = lbp->b_addr;
448 1693 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
449 1693 : if (be32_to_cpu(ltp->bestcount) >
450 1693 : (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 1693 : from = xfs_dir2_leaf_bests_p(ltp);
461 14022 : for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
462 5318 : off = be16_to_cpu(*from);
463 5318 : if (off != NULLDATAOFF)
464 5278 : n++;
465 10636 : freehdr.bests[i] = cpu_to_be16(off);
466 : }
467 :
468 : /*
469 : * Now initialize the freespace block header.
470 : */
471 1693 : freehdr.nused = n;
472 1693 : freehdr.nvalid = be32_to_cpu(ltp->bestcount);
473 :
474 1693 : xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
475 1693 : xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
476 1693 : 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 1693 : 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 1693 : leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
488 1693 : lbp->b_ops = &xfs_dir3_leafn_buf_ops;
489 1693 : xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
490 1693 : xfs_dir3_leaf_log_header(args, lbp);
491 1693 : xfs_dir3_leaf_check(dp, lbp);
492 1693 : 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 32325950 : 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 32325950 : struct xfs_dir3_icleaf_hdr leafhdr;
506 32325950 : struct xfs_inode *dp = args->dp;
507 32325950 : struct xfs_dir2_leaf *leaf = bp->b_addr;
508 32325950 : struct xfs_dir2_leaf_entry *lep;
509 32325950 : struct xfs_dir2_leaf_entry *ents;
510 32325950 : int compact; /* compacting stale leaves */
511 32325950 : int highstale = 0; /* next stale entry */
512 32325950 : int lfloghigh; /* high leaf entry logging */
513 32325950 : int lfloglow; /* low leaf entry logging */
514 32325950 : int lowstale = 0; /* previous stale entry */
515 :
516 32325950 : trace_xfs_dir2_leafn_add(args, index);
517 :
518 32328710 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
519 32327046 : 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 32327046 : 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 32327046 : if (leafhdr.count == args->geo->leaf_max_ents) {
539 38102 : if (!leafhdr.stale)
540 : return -ENOSPC;
541 9246 : compact = leafhdr.stale > 1;
542 : } else
543 : compact = 0;
544 64577131 : ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
545 62447262 : ASSERT(index == leafhdr.count ||
546 : be32_to_cpu(ents[index].hashval) >= args->hashval);
547 :
548 32298190 : 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 32298190 : if (compact)
556 3709 : xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
557 : &highstale, &lfloglow, &lfloghigh);
558 32294481 : else if (leafhdr.stale) {
559 : /*
560 : * Set impossible logging indices for this case.
561 : */
562 23563713 : lfloglow = leafhdr.count;
563 23563713 : lfloghigh = -1;
564 : }
565 :
566 : /*
567 : * Insert the new entry, log everything.
568 : */
569 32298190 : lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
570 : highstale, &lfloglow, &lfloghigh);
571 :
572 32298664 : lep->hashval = cpu_to_be32(args->hashval);
573 32298664 : lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
574 : args->blkno, args->index));
575 :
576 32298664 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
577 32297146 : xfs_dir3_leaf_log_header(args, bp);
578 32298252 : xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
579 32297686 : xfs_dir3_leaf_check(dp, bp);
580 32297686 : return 0;
581 : }
582 :
583 : #ifdef DEBUG
584 : static void
585 257985 : xfs_dir2_free_hdr_check(
586 : struct xfs_inode *dp,
587 : struct xfs_buf *bp,
588 : xfs_dir2_db_t db)
589 : {
590 257985 : struct xfs_dir3_icfree_hdr hdr;
591 :
592 257985 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
593 :
594 257985 : ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
595 257985 : ASSERT(hdr.firstdb <= db);
596 257985 : ASSERT(db < hdr.firstdb + hdr.nvalid);
597 257985 : }
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 208218776 : 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 208218776 : struct xfs_dir3_icleaf_hdr leafhdr;
613 :
614 208218776 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
615 :
616 208229260 : 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 208229260 : if (count)
622 62763894 : *count = leafhdr.count;
623 208229260 : if (!leafhdr.count)
624 : return 0;
625 208229260 : 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 32457149 : 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 32457149 : struct xfs_buf *curbp = NULL; /* current data/free buffer */
640 32457149 : xfs_dir2_db_t curdb = -1; /* current data block number */
641 32457149 : xfs_dir2_db_t curfdb = -1; /* current free block number */
642 32457149 : xfs_inode_t *dp; /* incore directory inode */
643 32457149 : int error; /* error return value */
644 32457149 : int fi; /* free entry index */
645 32457149 : xfs_dir2_free_t *free = NULL; /* free block structure */
646 32457149 : int index; /* leaf entry index */
647 32457149 : xfs_dir2_leaf_t *leaf; /* leaf structure */
648 32457149 : int length; /* length of new data entry */
649 32457149 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
650 32457149 : xfs_mount_t *mp; /* filesystem mount point */
651 32457149 : xfs_dir2_db_t newdb; /* new data block number */
652 32457149 : xfs_dir2_db_t newfdb; /* new free block number */
653 32457149 : xfs_trans_t *tp; /* transaction pointer */
654 32457149 : struct xfs_dir3_icleaf_hdr leafhdr;
655 :
656 32457149 : dp = args->dp;
657 32457149 : tp = args->trans;
658 32457149 : mp = dp->i_mount;
659 32457149 : leaf = bp->b_addr;
660 32457149 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
661 :
662 32456890 : xfs_dir3_leaf_check(dp, bp);
663 32454198 : ASSERT(leafhdr.count > 0);
664 :
665 : /*
666 : * Look up the hash value in the leaf entries.
667 : */
668 32454198 : index = xfs_dir2_leaf_search_hash(args, bp);
669 : /*
670 : * Do we have a buffer coming in?
671 : */
672 32455534 : if (state->extravalid) {
673 : /* If so, it's a free block buffer, get the block number. */
674 155978 : curbp = state->extrablk.bp;
675 155978 : curfdb = state->extrablk.blkno;
676 155978 : free = curbp->b_addr;
677 155978 : ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
678 : free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
679 : }
680 32455534 : length = xfs_dir2_data_entsize(mp, args->namelen);
681 : /*
682 : * Loop over leaf entries with the right hash value.
683 : */
684 32455534 : for (lep = &leafhdr.ents[index];
685 112103161 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
686 24686048 : lep++, index++) {
687 : /*
688 : * Skip stale leaf entries.
689 : */
690 24866900 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
691 260405 : continue;
692 : /*
693 : * Pull the data block number from the entry.
694 : */
695 24606495 : newdb = xfs_dir2_dataptr_to_db(args->geo,
696 0 : 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 24606495 : if (newdb != curdb) {
706 1659640 : struct xfs_dir3_icfree_hdr freehdr;
707 :
708 1659640 : curdb = newdb;
709 : /*
710 : * Convert the data block to the free block
711 : * holding its freespace information.
712 : */
713 1659640 : 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 1659640 : if (newfdb != curfdb) {
718 : /*
719 : * If we had one before, drop it.
720 : */
721 256596 : if (curbp)
722 0 : xfs_trans_brelse(tp, curbp);
723 :
724 256596 : error = xfs_dir2_free_read(tp, dp, args->owner,
725 : xfs_dir2_db_to_da(args->geo,
726 : newfdb),
727 : &curbp);
728 257985 : if (error)
729 0 : return error;
730 257985 : free = curbp->b_addr;
731 :
732 257985 : xfs_dir2_free_hdr_check(dp, curbp, curdb);
733 : }
734 : /*
735 : * Get the index for our entry.
736 : */
737 1661029 : fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
738 : /*
739 : * If it has room, return it.
740 : */
741 1661029 : xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
742 1661029 : 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 1661029 : curfdb = newfdb;
751 1661029 : if (be16_to_cpu(freehdr.bests[fi]) >= length)
752 182752 : goto out;
753 : }
754 : }
755 : /* Didn't find any space */
756 : fi = -1;
757 32457434 : out:
758 32457434 : ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
759 32457434 : if (curbp) {
760 : /* Giving back a free block. */
761 413963 : state->extravalid = 1;
762 413963 : state->extrablk.bp = curbp;
763 413963 : state->extrablk.index = fi;
764 413963 : 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 413963 : state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
772 : } else {
773 32043471 : state->extravalid = 0;
774 : }
775 : /*
776 : * Return the index, that will be the insertion point.
777 : */
778 32457434 : *indexp = index;
779 32457434 : 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 112594062 : 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 112594062 : struct xfs_buf *curbp = NULL; /* current data/free buffer */
794 112594062 : xfs_dir2_db_t curdb = -1; /* current data block number */
795 112594062 : xfs_dir2_data_entry_t *dep; /* data block entry */
796 112594062 : xfs_inode_t *dp; /* incore directory inode */
797 112594062 : int error; /* error return value */
798 112594062 : int index; /* leaf entry index */
799 112594062 : xfs_dir2_leaf_t *leaf; /* leaf structure */
800 112594062 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
801 112594062 : xfs_mount_t *mp; /* filesystem mount point */
802 112594062 : xfs_dir2_db_t newdb; /* new data block number */
803 112594062 : xfs_trans_t *tp; /* transaction pointer */
804 112594062 : enum xfs_dacmp cmp; /* comparison result */
805 112594062 : struct xfs_dir3_icleaf_hdr leafhdr;
806 :
807 112594062 : dp = args->dp;
808 112594062 : tp = args->trans;
809 112594062 : mp = dp->i_mount;
810 112594062 : leaf = bp->b_addr;
811 112594062 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
812 :
813 112594472 : xfs_dir3_leaf_check(dp, bp);
814 112594816 : 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 112594816 : index = xfs_dir2_leaf_search_hash(args, bp);
824 : /*
825 : * Do we have a buffer coming in?
826 : */
827 112594388 : if (state->extravalid) {
828 813407 : curbp = state->extrablk.bp;
829 813407 : curdb = state->extrablk.blkno;
830 : }
831 : /*
832 : * Loop over leaf entries with the right hash value.
833 : */
834 112594388 : for (lep = &leafhdr.ents[index];
835 1057521602 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
836 416903971 : lep++, index++) {
837 : /*
838 : * Skip stale leaf entries.
839 : */
840 521069063 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
841 76547649 : continue;
842 : /*
843 : * Pull the data block number from the entry.
844 : */
845 444521414 : newdb = xfs_dir2_dataptr_to_db(args->geo,
846 0 : 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 444521414 : 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 111896173 : if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
859 709770 : curdb != state->extrablk.blkno))
860 7213387 : 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 111895437 : if (args->cmpresult != XFS_CMP_DIFFERENT &&
866 709770 : newdb == state->extrablk.blkno) {
867 1199 : ASSERT(state->extravalid);
868 1199 : curbp = state->extrablk.bp;
869 : } else {
870 111894238 : error = xfs_dir3_data_read(tp, dp, args->owner,
871 : xfs_dir2_db_to_da(args->geo,
872 : newdb),
873 : 0, &curbp);
874 111890210 : if (error)
875 2563 : return error;
876 : }
877 111888846 : xfs_dir3_data_check(dp, curbp);
878 111888846 : curdb = newdb;
879 : }
880 : /*
881 : * Point to the data entry.
882 : */
883 889038342 : dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
884 889038342 : xfs_dir2_dataptr_to_off(args->geo,
885 444519171 : 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 444519171 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
892 444517938 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
893 : /* If there is a CI match block, drop it */
894 104219680 : if (args->cmpresult != XFS_CMP_DIFFERENT &&
895 0 : curdb != state->extrablk.blkno)
896 0 : xfs_trans_brelse(tp, state->extrablk.bp);
897 104219680 : args->cmpresult = cmp;
898 104219680 : args->inumber = be64_to_cpu(dep->inumber);
899 104219680 : args->filetype = xfs_dir2_data_get_ftype(mp, dep);
900 104221301 : *indexp = index;
901 104221301 : state->extravalid = 1;
902 104221301 : state->extrablk.bp = curbp;
903 104221301 : state->extrablk.blkno = curdb;
904 104221301 : state->extrablk.index = (int)((char *)dep -
905 104221301 : (char *)curbp->b_addr);
906 104221301 : state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
907 104221301 : curbp->b_ops = &xfs_dir3_data_buf_ops;
908 104221301 : xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
909 104221442 : if (cmp == XFS_CMP_EXACT)
910 : return -EEXIST;
911 : }
912 : }
913 8429102 : ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
914 8429102 : if (curbp) {
915 1166887 : if (args->cmpresult == XFS_CMP_DIFFERENT) {
916 : /* Giving back last used data block. */
917 988848 : state->extravalid = 1;
918 988848 : state->extrablk.bp = curbp;
919 988848 : state->extrablk.index = -1;
920 988848 : state->extrablk.blkno = curdb;
921 988848 : state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
922 988848 : curbp->b_ops = &xfs_dir3_data_buf_ops;
923 988848 : 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 178039 : if (state->extrablk.bp != curbp)
927 161004 : xfs_trans_brelse(tp, curbp);
928 : }
929 : } else {
930 7262215 : state->extravalid = 0;
931 : }
932 8429102 : *indexp = index;
933 8429102 : 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 145051915 : 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 145051915 : if (args->op_flags & XFS_DA_OP_ADDNAME)
949 32455855 : return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
950 : state);
951 112596060 : 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 50764 : 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 50764 : int stale; /* count stale leaves copied */
972 :
973 50764 : trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
974 :
975 : /*
976 : * Silently return if nothing to do.
977 : */
978 50764 : 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 50764 : if (start_d < dhdr->count) {
987 6168 : memmove(&dents[start_d + count], &dents[start_d],
988 : (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
989 3084 : xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
990 3084 : 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 50764 : 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 101528 : memcpy(&dents[start_d], &sents[start_s],
1010 : count * sizeof(xfs_dir2_leaf_entry_t));
1011 50764 : 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 50764 : 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 50764 : shdr->count -= count;
1028 50764 : shdr->stale -= stale;
1029 50764 : dhdr->count += count;
1030 50764 : 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 79621 : 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 79621 : struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1044 79621 : struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1045 79621 : struct xfs_dir2_leaf_entry *ents1;
1046 79621 : struct xfs_dir2_leaf_entry *ents2;
1047 79621 : struct xfs_dir3_icleaf_hdr hdr1;
1048 79621 : struct xfs_dir3_icleaf_hdr hdr2;
1049 :
1050 79621 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1051 79621 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1052 79621 : ents1 = hdr1.ents;
1053 79621 : ents2 = hdr2.ents;
1054 :
1055 130385 : if (hdr1.count > 0 && hdr2.count > 0 &&
1056 149209 : (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1057 47681 : be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1058 47681 : be32_to_cpu(ents1[hdr1.count - 1].hashval)))
1059 3084 : 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 28857 : 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 28857 : xfs_da_args_t *args; /* operation arguments */
1077 28857 : int count; /* count (& direction) leaves */
1078 28857 : int isleft; /* new goes in left leaf */
1079 28857 : xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1080 28857 : xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1081 28857 : int mid; /* midpoint leaf index */
1082 : #if defined(DEBUG) || defined(XFS_WARN)
1083 28857 : int oldstale; /* old count of stale leaves */
1084 : #endif
1085 28857 : int oldsum; /* old total leaf count */
1086 28857 : int swap_blocks; /* swapped leaf blocks */
1087 28857 : struct xfs_dir2_leaf_entry *ents1;
1088 28857 : struct xfs_dir2_leaf_entry *ents2;
1089 28857 : struct xfs_dir3_icleaf_hdr hdr1;
1090 28857 : struct xfs_dir3_icleaf_hdr hdr2;
1091 28857 : struct xfs_inode *dp = state->args->dp;
1092 :
1093 28857 : args = state->args;
1094 : /*
1095 : * If the block order is wrong, swap the arguments.
1096 : */
1097 28857 : swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1098 28857 : if (swap_blocks)
1099 0 : swap(blk1, blk2);
1100 :
1101 28857 : leaf1 = blk1->bp->b_addr;
1102 28857 : leaf2 = blk2->bp->b_addr;
1103 28857 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1104 28857 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1105 28857 : ents1 = hdr1.ents;
1106 28857 : ents2 = hdr2.ents;
1107 :
1108 28857 : oldsum = hdr1.count + hdr2.count;
1109 : #if defined(DEBUG) || defined(XFS_WARN)
1110 28857 : oldstale = hdr1.stale + hdr2.stale;
1111 : #endif
1112 28857 : 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 28857 : 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 28857 : count = hdr1.count - mid + (isleft == 0);
1139 28857 : if (count > 0)
1140 28857 : 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 28857 : ASSERT(hdr1.count + hdr2.count == oldsum);
1149 28857 : ASSERT(hdr1.stale + hdr2.stale == oldstale);
1150 :
1151 : /* log the changes made when moving the entries */
1152 28857 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1153 28857 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
1154 28857 : xfs_dir3_leaf_log_header(args, blk1->bp);
1155 28857 : xfs_dir3_leaf_log_header(args, blk2->bp);
1156 :
1157 28857 : xfs_dir3_leaf_check(dp, blk1->bp);
1158 28857 : xfs_dir3_leaf_check(dp, blk2->bp);
1159 :
1160 : /*
1161 : * Mark whether we're inserting into the old or new leaf.
1162 : */
1163 28857 : if (hdr1.count < hdr2.count)
1164 0 : state->inleaf = swap_blocks;
1165 28857 : else if (hdr1.count > hdr2.count)
1166 0 : state->inleaf = !swap_blocks;
1167 : else
1168 28857 : state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1169 : /*
1170 : * Adjust the expected index for insertion.
1171 : */
1172 28857 : if (!state->inleaf)
1173 22547 : 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 28857 : 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 28857 : }
1187 :
1188 : static int
1189 12468195 : 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 12468195 : int logfree = 0;
1199 12468195 : struct xfs_dir3_icfree_hdr freehdr;
1200 12468195 : struct xfs_inode *dp = args->dp;
1201 :
1202 12468195 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1203 12468360 : if (hdr) {
1204 : /*
1205 : * Data block is not empty, just set the free entry to the new
1206 : * value.
1207 : */
1208 12452545 : freehdr.bests[findex] = cpu_to_be16(longest);
1209 12452545 : xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1210 12452545 : return 0;
1211 : }
1212 :
1213 : /* One less used entry in the free table. */
1214 15815 : 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 15815 : if (findex == freehdr.nvalid - 1) {
1222 494 : int i; /* free entry index */
1223 :
1224 8716 : for (i = findex - 1; i >= 0; i--) {
1225 8714 : if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
1226 : break;
1227 : }
1228 494 : freehdr.nvalid = i + 1;
1229 494 : logfree = 0;
1230 : } else {
1231 : /* Not the last entry, just punch it out. */
1232 15321 : freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
1233 15321 : logfree = 1;
1234 : }
1235 :
1236 15815 : xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
1237 15815 : 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 15815 : if (!freehdr.nused) {
1244 2 : int error;
1245 :
1246 2 : error = xfs_dir2_shrink_inode(args, fdb, fbp);
1247 2 : 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 15813 : if (logfree)
1261 15321 : 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 30295596 : 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 30295596 : struct xfs_da_geometry *geo = args->geo;
1279 30295596 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1280 30295596 : xfs_dir2_db_t db; /* data block number */
1281 30295596 : struct xfs_buf *dbp; /* data block buffer */
1282 30295596 : xfs_dir2_data_entry_t *dep; /* data block entry */
1283 30295596 : xfs_inode_t *dp; /* incore directory inode */
1284 30295596 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1285 30295596 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1286 30295596 : int longest; /* longest data free entry */
1287 30295596 : int off; /* data block entry offset */
1288 30295596 : int needlog; /* need to log data header */
1289 30295596 : int needscan; /* need to rescan data frees */
1290 30295596 : xfs_trans_t *tp; /* transaction pointer */
1291 30295596 : struct xfs_dir2_data_free *bf; /* bestfree table */
1292 30295596 : struct xfs_dir3_icleaf_hdr leafhdr;
1293 :
1294 30295596 : trace_xfs_dir2_leafn_remove(args, index);
1295 :
1296 30295872 : dp = args->dp;
1297 30295872 : tp = args->trans;
1298 30295872 : leaf = bp->b_addr;
1299 30295872 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1300 :
1301 : /*
1302 : * Point to the entry we're removing.
1303 : */
1304 30295942 : lep = &leafhdr.ents[index];
1305 :
1306 : /*
1307 : * Extract the data block and offset from the entry.
1308 : */
1309 30295942 : db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1310 30295942 : ASSERT(dblk->blkno == db);
1311 30295942 : off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
1312 30295942 : ASSERT(dblk->index == off);
1313 :
1314 : /*
1315 : * Kill the leaf entry by marking it stale.
1316 : * Log the leaf block changes.
1317 : */
1318 30295942 : leafhdr.stale++;
1319 30295942 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1320 30295107 : xfs_dir3_leaf_log_header(args, bp);
1321 :
1322 30295524 : lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1323 30295524 : 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 30295518 : dbp = dblk->bp;
1330 30295518 : hdr = dbp->b_addr;
1331 30295518 : dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1332 30295518 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1333 30294429 : longest = be16_to_cpu(bf[0].length);
1334 30294429 : needlog = needscan = 0;
1335 30294429 : xfs_dir2_data_make_free(args, dbp, off,
1336 30294429 : 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 30294305 : if (needscan)
1343 3214249 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1344 30294312 : if (needlog)
1345 18916081 : xfs_dir2_data_log_header(args, dbp);
1346 30294014 : 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 30294924 : if (longest < be16_to_cpu(bf[0].length)) {
1352 12468301 : int error; /* error return value */
1353 12468301 : struct xfs_buf *fbp; /* freeblock buffer */
1354 12468301 : xfs_dir2_db_t fdb; /* freeblock block number */
1355 12468301 : int findex; /* index in freeblock entries */
1356 12468301 : 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 12468301 : fdb = xfs_dir2_db_to_fdb(geo, db);
1363 12468301 : error = xfs_dir2_free_read(tp, dp, args->owner,
1364 : xfs_dir2_db_to_da(geo, fdb), &fbp);
1365 12468376 : if (error)
1366 2 : return error;
1367 12468374 : free = fbp->b_addr;
1368 : #ifdef DEBUG
1369 : {
1370 12468374 : struct xfs_dir3_icfree_hdr freehdr;
1371 :
1372 12468374 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1373 12468404 : 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 12468404 : findex = xfs_dir2_db_to_fdindex(geo, db);
1381 12468404 : 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 12468404 : if (longest == geo->blksize - geo->data_entry_offset) {
1387 : /*
1388 : * Try to punch out the data block.
1389 : */
1390 15815 : error = xfs_dir2_shrink_inode(args, db, dbp);
1391 15815 : if (error == 0) {
1392 15815 : dblk->bp = NULL;
1393 15815 : 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 12468404 : error = xfs_dir3_data_block_free(args, hdr, free,
1408 : fdb, findex, fbp, longest);
1409 12468323 : if (error)
1410 : return error;
1411 : }
1412 :
1413 30294946 : 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 30295065 : *rval = (geo->leaf_hdr_size +
1419 30295065 : (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
1420 30295065 : geo->magicpct;
1421 30295065 : return 0;
1422 : }
1423 :
1424 : /*
1425 : * Split the leaf entries in the old block into old and new blocks.
1426 : */
1427 : int /* error */
1428 28856 : 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 28856 : xfs_da_args_t *args; /* operation arguments */
1434 28856 : xfs_dablk_t blkno; /* new leaf block number */
1435 28856 : int error; /* error return value */
1436 28856 : struct xfs_inode *dp;
1437 :
1438 : /*
1439 : * Allocate space for a new leaf node.
1440 : */
1441 28856 : args = state->args;
1442 28856 : dp = args->dp;
1443 28856 : ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1444 28856 : error = xfs_da_grow_inode(args, &blkno);
1445 28857 : if (error) {
1446 : return error;
1447 : }
1448 : /*
1449 : * Initialize the new leaf block.
1450 : */
1451 28857 : error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1452 : &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1453 28857 : if (error)
1454 : return error;
1455 :
1456 28857 : newblk->blkno = blkno;
1457 28857 : 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 28857 : xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1463 28857 : error = xfs_da3_blk_link(state, oldblk, newblk);
1464 28857 : if (error) {
1465 : return error;
1466 : }
1467 : /*
1468 : * Insert the new entry in the correct block.
1469 : */
1470 28857 : if (state->inleaf)
1471 6310 : error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1472 : else
1473 22547 : 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 28857 : oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1478 28857 : newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1479 28857 : xfs_dir3_leaf_check(dp, oldblk->bp);
1480 28857 : xfs_dir3_leaf_check(dp, newblk->bp);
1481 28857 : 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 1880284 : xfs_dir2_leafn_toosmall(
1495 : xfs_da_state_t *state, /* btree cursor */
1496 : int *action) /* resulting action to take */
1497 : {
1498 1880284 : xfs_da_state_blk_t *blk; /* leaf block */
1499 1880284 : xfs_dablk_t blkno; /* leaf block number */
1500 1880284 : struct xfs_buf *bp; /* leaf buffer */
1501 1880284 : int bytes; /* bytes in use */
1502 1880284 : int count; /* leaf live entry count */
1503 1880284 : int error; /* error return value */
1504 1880284 : int forward; /* sibling block direction */
1505 1880284 : int i; /* sibling counter */
1506 1880284 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1507 1880284 : int rval; /* result from path_shift */
1508 1880284 : struct xfs_dir3_icleaf_hdr leafhdr;
1509 1880284 : struct xfs_dir2_leaf_entry *ents;
1510 1880284 : 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 1880284 : blk = &state->path.blk[state->path.active - 1];
1518 1880284 : leaf = blk->bp->b_addr;
1519 1880284 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1520 1880291 : ents = leafhdr.ents;
1521 1880291 : xfs_dir3_leaf_check(dp, blk->bp);
1522 :
1523 1880282 : count = leafhdr.count - leafhdr.stale;
1524 1880282 : bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
1525 1880282 : 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 1880282 : 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 0 : forward = (leafhdr.forw != 0);
1544 0 : memcpy(&state->altpath, &state->path, sizeof(state->path));
1545 0 : error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1546 : &rval);
1547 0 : if (error)
1548 : return error;
1549 0 : *action = rval ? 2 : 0;
1550 0 : 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 1880282 : forward = leafhdr.forw < leafhdr.back;
1560 5612841 : for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1561 3754454 : struct xfs_dir3_icleaf_hdr hdr2;
1562 :
1563 3754454 : blkno = forward ? leafhdr.forw : leafhdr.back;
1564 3754454 : if (blkno == 0)
1565 1664516 : continue;
1566 : /*
1567 : * Read the sibling leaf block.
1568 : */
1569 2089938 : error = xfs_dir3_leafn_read(state->args->trans, dp,
1570 : state->args->owner, blkno, &bp);
1571 2089946 : if (error)
1572 0 : return error;
1573 :
1574 : /*
1575 : * Count bytes in the two blocks combined.
1576 : */
1577 2089946 : count = leafhdr.count - leafhdr.stale;
1578 2089946 : bytes = state->args->geo->blksize -
1579 2089946 : (state->args->geo->blksize >> 2);
1580 :
1581 2089946 : leaf = bp->b_addr;
1582 2089946 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
1583 2089949 : ents = hdr2.ents;
1584 2089949 : count += hdr2.count - hdr2.stale;
1585 2089949 : bytes -= count * sizeof(ents[0]);
1586 :
1587 : /*
1588 : * Fits with at least 25% to spare.
1589 : */
1590 2089949 : if (bytes >= 0)
1591 : break;
1592 2068042 : xfs_trans_brelse(state->args->trans, bp);
1593 : }
1594 : /*
1595 : * Didn't like either block, give up.
1596 : */
1597 1880294 : if (i >= 2) {
1598 1858387 : *action = 0;
1599 1858387 : 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 43814 : memcpy(&state->altpath, &state->path, sizeof(state->path));
1607 21907 : if (blkno < blk->blkno)
1608 6744 : error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1609 : &rval);
1610 : else
1611 15163 : error = xfs_da3_path_shift(state, &state->path, forward, 0,
1612 : &rval);
1613 21907 : if (error) {
1614 : return error;
1615 : }
1616 21907 : *action = rval ? 0 : 1;
1617 21907 : 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 21907 : 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 21907 : xfs_da_args_t *args; /* operation arguments */
1631 21907 : xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1632 21907 : xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1633 21907 : struct xfs_dir3_icleaf_hdr savehdr;
1634 21907 : struct xfs_dir3_icleaf_hdr drophdr;
1635 21907 : struct xfs_dir2_leaf_entry *sents;
1636 21907 : struct xfs_dir2_leaf_entry *dents;
1637 21907 : struct xfs_inode *dp = state->args->dp;
1638 :
1639 21907 : args = state->args;
1640 21907 : ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1641 21907 : ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1642 21907 : drop_leaf = drop_blk->bp->b_addr;
1643 21907 : save_leaf = save_blk->bp->b_addr;
1644 :
1645 21907 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1646 21907 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
1647 21907 : sents = savehdr.ents;
1648 21907 : dents = drophdr.ents;
1649 :
1650 : /*
1651 : * If there are any stale leaf entries, take this opportunity
1652 : * to purge them.
1653 : */
1654 21907 : if (drophdr.stale)
1655 19492 : xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1656 21907 : if (savehdr.stale)
1657 21829 : 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 21907 : drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1663 21907 : if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1664 3084 : xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1665 : save_blk->bp, &savehdr, sents, 0,
1666 3084 : drophdr.count);
1667 : else
1668 18823 : xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1669 : save_blk->bp, &savehdr, sents,
1670 18823 : savehdr.count, drophdr.count);
1671 21907 : save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1672 :
1673 : /* log the changes made when moving the entries */
1674 21907 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1675 21907 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
1676 21907 : xfs_dir3_leaf_log_header(args, save_blk->bp);
1677 21907 : xfs_dir3_leaf_log_header(args, drop_blk->bp);
1678 :
1679 21907 : xfs_dir3_leaf_check(dp, save_blk->bp);
1680 21907 : xfs_dir3_leaf_check(dp, drop_blk->bp);
1681 21907 : }
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 33900 : 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 33900 : struct xfs_inode *dp = args->dp;
1698 33900 : struct xfs_trans *tp = args->trans;
1699 33900 : struct xfs_mount *mp = dp->i_mount;
1700 33900 : struct xfs_dir2_data_free *bf;
1701 33900 : xfs_dir2_db_t fbno;
1702 33900 : struct xfs_buf *fbp;
1703 33900 : struct xfs_buf *dbp;
1704 33900 : int error;
1705 :
1706 : /* Not allowed to allocate, return failure. */
1707 33900 : if (args->total == 0)
1708 : return -ENOSPC;
1709 :
1710 : /* Allocate and initialize the new data block. */
1711 33900 : error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1712 33899 : if (error)
1713 : return error;
1714 33898 : error = xfs_dir3_data_init(args, *dbno, &dbp);
1715 33899 : if (error)
1716 : return error;
1717 :
1718 : /*
1719 : * Get the freespace block corresponding to the data block
1720 : * that was just allocated.
1721 : */
1722 33899 : fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
1723 33899 : error = xfs_dir2_free_try_read(tp, dp, args->owner,
1724 : xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1725 33898 : 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 33898 : if (!fbp) {
1733 3 : error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1734 3 : if (error)
1735 : return error;
1736 :
1737 3 : 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 3 : error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1759 3 : if (error)
1760 : return error;
1761 3 : xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1762 :
1763 : /* Remember the first slot as our empty slot. */
1764 3 : hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1765 3 : XFS_DIR2_FREE_OFFSET)) *
1766 3 : args->geo->free_max_bests;
1767 : } else {
1768 33895 : 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 33899 : *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 33899 : if (*findex >= hdr->nvalid) {
1776 33179 : ASSERT(*findex < args->geo->free_max_bests);
1777 33179 : hdr->nvalid = *findex + 1;
1778 33179 : 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 33899 : if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1786 33899 : hdr->nused++;
1787 33899 : xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
1788 33899 : xfs_dir2_free_log_header(args, fbp);
1789 : }
1790 :
1791 : /* Update the freespace value for the new block in the table. */
1792 33899 : bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
1793 33899 : hdr->bests[*findex] = bf[0].length;
1794 :
1795 33899 : *dbpp = dbp;
1796 33899 : *fbpp = fbp;
1797 33899 : return 0;
1798 : }
1799 :
1800 : static int
1801 32299327 : 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 32299327 : struct xfs_inode *dp = args->dp;
1811 32299327 : struct xfs_trans *tp = args->trans;
1812 32299327 : struct xfs_buf *fbp = NULL;
1813 32299327 : xfs_dir2_db_t firstfbno;
1814 32299327 : xfs_dir2_db_t lastfbno;
1815 32299327 : xfs_dir2_db_t ifbno = -1;
1816 32299327 : xfs_dir2_db_t dbno = -1;
1817 32299327 : xfs_dir2_db_t fbno;
1818 32299327 : xfs_fileoff_t fo;
1819 32299327 : int findex = 0;
1820 32299327 : 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 32299327 : if (fblk) {
1828 257985 : fbp = fblk->bp;
1829 257985 : findex = fblk->index;
1830 257985 : xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1831 257985 : if (findex >= 0) {
1832 : /* caller already found the freespace for us. */
1833 58772 : ASSERT(findex < hdr->nvalid);
1834 58772 : ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1835 58772 : ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
1836 58772 : dbno = hdr->firstdb + findex;
1837 58772 : 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 199213 : ifbno = fblk->blkno;
1845 199213 : xfs_trans_brelse(tp, fbp);
1846 199213 : fbp = NULL;
1847 199213 : 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 32240555 : error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1855 32240377 : if (error)
1856 : return error;
1857 32240377 : lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1858 32240377 : firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1859 :
1860 32659143 : for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1861 : /* If it's ifbno we already looked at it. */
1862 32625243 : if (fbno == ifbno)
1863 127024 : 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 32498219 : error = xfs_dir2_free_try_read(tp, dp, args->owner,
1871 : xfs_dir2_db_to_da(args->geo, fbno),
1872 : &fbp);
1873 32501357 : if (error)
1874 152 : return error;
1875 32501205 : if (!fbp)
1876 0 : continue;
1877 :
1878 32501205 : 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 500657105 : for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
1882 997601984 : if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1883 497236621 : be16_to_cpu(hdr->bests[findex]) >= length) {
1884 32207905 : dbno = hdr->firstdb + findex;
1885 32207905 : goto found_block;
1886 : }
1887 : }
1888 :
1889 : /* Didn't find free space, go on to next free block */
1890 291742 : xfs_trans_brelse(tp, fbp);
1891 : }
1892 :
1893 33900 : found_block:
1894 32300577 : *dbnop = dbno;
1895 32300577 : *fbpp = fbp;
1896 32300577 : *findexp = findex;
1897 32300577 : 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 32300418 : 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 32300418 : struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1911 32300418 : struct xfs_dir2_data_entry *dep; /* data entry pointer */
1912 32300418 : struct xfs_dir2_data_hdr *hdr; /* data block header */
1913 32300418 : struct xfs_dir2_data_free *bf;
1914 32300418 : struct xfs_trans *tp = args->trans;
1915 32300418 : struct xfs_inode *dp = args->dp;
1916 32300418 : struct xfs_dir3_icfree_hdr freehdr;
1917 32300418 : struct xfs_buf *dbp; /* data block buffer */
1918 32300418 : struct xfs_buf *fbp; /* freespace buffer */
1919 32300418 : xfs_dir2_data_aoff_t aoff;
1920 32300418 : xfs_dir2_db_t dbno; /* data block number */
1921 32300418 : int error; /* error return value */
1922 32300418 : int findex; /* freespace entry index */
1923 32300418 : int length; /* length of the new entry */
1924 32300418 : int logfree = 0; /* need to log free entry */
1925 32300418 : int needlog = 0; /* need to log data header */
1926 32300418 : int needscan = 0; /* need to rescan data frees */
1927 32300418 : __be16 *tagp; /* data entry tag pointer */
1928 :
1929 32300418 : length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
1930 32300418 : error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1931 : &findex, length);
1932 32300554 : 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 32300402 : 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 32300402 : if (dbno == -1) {
1950 : /* we're going to have to log the free block index later */
1951 33900 : logfree = 1;
1952 33900 : error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1953 : &freehdr, &findex);
1954 : } else {
1955 : /* Read the data block in. */
1956 32266502 : error = xfs_dir3_data_read(tp, dp, args->owner,
1957 : xfs_dir2_db_to_da(args->geo, dbno), 0, &dbp);
1958 : }
1959 32301413 : if (error)
1960 : return error;
1961 :
1962 : /* setup for data block up now */
1963 32301314 : hdr = dbp->b_addr;
1964 32301314 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1965 32301317 : ASSERT(be16_to_cpu(bf[0].length) >= length);
1966 :
1967 : /* Point to the existing unused space. */
1968 64602634 : dup = (xfs_dir2_data_unused_t *)
1969 32301317 : ((char *)hdr + be16_to_cpu(bf[0].offset));
1970 :
1971 : /* Mark the first part of the unused space, inuse for us. */
1972 32301317 : aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1973 32301317 : error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1974 : &needlog, &needscan);
1975 32299702 : 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 32299702 : dep = (xfs_dir2_data_entry_t *)dup;
1982 32299702 : dep->inumber = cpu_to_be64(args->inumber);
1983 32299702 : dep->namelen = args->namelen;
1984 64599404 : memcpy(dep->name, args->name, dep->namelen);
1985 32299702 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1986 32299873 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
1987 32300299 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1988 32300299 : xfs_dir2_data_log_entry(args, dbp, dep);
1989 :
1990 : /* Rescan the freespace and log the data block if needed. */
1991 32299135 : if (needscan)
1992 15473513 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1993 32300343 : if (needlog)
1994 32297809 : xfs_dir2_data_log_header(args, dbp);
1995 :
1996 : /* If the freespace block entry is now wrong, update it. */
1997 32299437 : if (freehdr.bests[findex] != bf[0].length) {
1998 15976492 : freehdr.bests[findex] = bf[0].length;
1999 15976492 : logfree = 1;
2000 : }
2001 :
2002 : /* Log the freespace entry if needed. */
2003 32299437 : if (logfree)
2004 15976492 : xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
2005 :
2006 : /* Return the data block and offset in args. */
2007 32297382 : args->blkno = (xfs_dablk_t)dbno;
2008 32297382 : args->index = be16_to_cpu(*tagp);
2009 32297382 : return 0;
2010 : }
2011 :
2012 : /*
2013 : * Top-level node form directory addname routine.
2014 : */
2015 : int /* error */
2016 32298816 : xfs_dir2_node_addname(
2017 : xfs_da_args_t *args) /* operation arguments */
2018 : {
2019 32298816 : xfs_da_state_blk_t *blk; /* leaf block for insert */
2020 32298816 : int error; /* error return value */
2021 32298816 : int rval; /* sub-return value */
2022 32298816 : xfs_da_state_t *state; /* btree cursor */
2023 :
2024 32298816 : trace_xfs_dir2_node_addname(args);
2025 :
2026 : /*
2027 : * Allocate and initialize the state (btree cursor).
2028 : */
2029 32301086 : 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 32297000 : error = xfs_da3_node_lookup_int(state, &rval);
2035 32298350 : if (error)
2036 5 : rval = error;
2037 32298350 : if (rval != -ENOENT) {
2038 5 : 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 32298345 : rval = xfs_dir2_node_addname_int(args,
2045 32298345 : state->extravalid ? &state->extrablk : NULL);
2046 32298600 : if (rval) {
2047 251 : goto done;
2048 : }
2049 32298349 : blk = &state->path.blk[state->path.active - 1];
2050 32298349 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2051 : /*
2052 : * Add the new leaf entry.
2053 : */
2054 32298349 : rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2055 32298713 : if (rval == 0) {
2056 : /*
2057 : * It worked, fix the hash values up the btree.
2058 : */
2059 32269857 : if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2060 32268971 : xfs_da3_fixhashpath(state, &state->path);
2061 : } else {
2062 : /*
2063 : * It didn't work, we need to split the leaf block.
2064 : */
2065 28856 : 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 28856 : rval = xfs_da3_split(state);
2073 : }
2074 32299220 : done:
2075 32299220 : xfs_da_state_free(state);
2076 32299267 : 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 80967963 : xfs_dir2_node_lookup(
2086 : xfs_da_args_t *args) /* operation arguments */
2087 : {
2088 80967963 : int error; /* error return value */
2089 80967963 : int i; /* btree level */
2090 80967963 : int rval; /* operation return value */
2091 80967963 : xfs_da_state_t *state; /* btree cursor */
2092 :
2093 80967963 : trace_xfs_dir2_node_lookup(args);
2094 :
2095 : /*
2096 : * Allocate and initialize the btree cursor.
2097 : */
2098 80968160 : state = xfs_da_state_alloc(args);
2099 :
2100 : /*
2101 : * Fill in the path to the entry in the cursor.
2102 : */
2103 80964707 : error = xfs_da3_node_lookup_int(state, &rval);
2104 80973509 : if (error)
2105 11713 : rval = error;
2106 80961796 : else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2107 : /* If a CI match, dup the actual name and return -EEXIST */
2108 58413 : xfs_dir2_data_entry_t *dep;
2109 :
2110 58413 : dep = (xfs_dir2_data_entry_t *)
2111 58413 : ((char *)state->extrablk.bp->b_addr +
2112 58413 : state->extrablk.index);
2113 58413 : rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2114 : }
2115 : /*
2116 : * Release the btree blocks and leaf block.
2117 : */
2118 244512834 : for (i = 0; i < state->path.active; i++) {
2119 163534830 : xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2120 163539325 : state->path.blk[i].bp = NULL;
2121 : }
2122 : /*
2123 : * Release the data block if we have it.
2124 : */
2125 80978004 : if (state->extravalid && state->extrablk.bp) {
2126 73713941 : xfs_trans_brelse(args->trans, state->extrablk.bp);
2127 73714557 : state->extrablk.bp = NULL;
2128 : }
2129 80978620 : xfs_da_state_free(state);
2130 80977310 : return rval;
2131 : }
2132 :
2133 : /*
2134 : * Remove an entry from a node-format directory.
2135 : */
2136 : int /* error */
2137 30294251 : xfs_dir2_node_removename(
2138 : struct xfs_da_args *args) /* operation arguments */
2139 : {
2140 30294251 : struct xfs_da_state_blk *blk; /* leaf block */
2141 30294251 : int error; /* error return value */
2142 30294251 : int rval; /* operation return value */
2143 30294251 : struct xfs_da_state *state; /* btree cursor */
2144 :
2145 30294251 : trace_xfs_dir2_node_removename(args);
2146 :
2147 : /*
2148 : * Allocate and initialize the btree cursor.
2149 : */
2150 30295967 : state = xfs_da_state_alloc(args);
2151 :
2152 : /* Look up the entry we're deleting, set up the cursor. */
2153 30294741 : error = xfs_da3_node_lookup_int(state, &rval);
2154 30295269 : if (error)
2155 3 : goto out_free;
2156 :
2157 : /* Didn't find it, upper layer screwed up. */
2158 30295266 : if (rval != -EEXIST) {
2159 0 : error = rval;
2160 0 : goto out_free;
2161 : }
2162 :
2163 30295266 : blk = &state->path.blk[state->path.active - 1];
2164 30295266 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2165 30295266 : ASSERT(state->extravalid);
2166 : /*
2167 : * Remove the leaf and data entries.
2168 : * Extrablk refers to the data block.
2169 : */
2170 30295266 : error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2171 : &state->extrablk, &rval);
2172 30293886 : if (error)
2173 2 : goto out_free;
2174 : /*
2175 : * Fix the hash values up the btree.
2176 : */
2177 30293884 : xfs_da3_fixhashpath(state, &state->path);
2178 : /*
2179 : * If we need to join leaf blocks, do it.
2180 : */
2181 30295258 : if (rval && state->path.active > 1)
2182 1880288 : error = xfs_da3_join(state);
2183 : /*
2184 : * If no errors so far, try conversion to leaf format.
2185 : */
2186 30295262 : if (!error)
2187 30295262 : error = xfs_dir2_node_to_leaf(state);
2188 0 : out_free:
2189 30295195 : xfs_da_state_free(state);
2190 30295730 : return error;
2191 : }
2192 :
2193 : /*
2194 : * Replace an entry's inode number in a node-format directory.
2195 : */
2196 : int /* error */
2197 508863 : xfs_dir2_node_replace(
2198 : xfs_da_args_t *args) /* operation arguments */
2199 : {
2200 508863 : xfs_da_state_blk_t *blk; /* leaf block */
2201 508863 : xfs_dir2_data_hdr_t *hdr; /* data block header */
2202 508863 : xfs_dir2_data_entry_t *dep; /* data entry changed */
2203 508863 : int error; /* error return value */
2204 508863 : int i; /* btree level */
2205 508863 : xfs_ino_t inum; /* new inode number */
2206 508863 : int ftype; /* new file type */
2207 508863 : int rval; /* internal return value */
2208 508863 : xfs_da_state_t *state; /* btree cursor */
2209 :
2210 508863 : trace_xfs_dir2_node_replace(args);
2211 :
2212 : /*
2213 : * Allocate and initialize the btree cursor.
2214 : */
2215 508860 : 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 508861 : inum = args->inumber;
2222 508861 : ftype = args->filetype;
2223 :
2224 : /*
2225 : * Lookup the entry to change in the btree.
2226 : */
2227 508861 : error = xfs_da3_node_lookup_int(state, &rval);
2228 508863 : if (error) {
2229 107 : 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 508863 : if (rval == -EEXIST) {
2236 508755 : struct xfs_dir3_icleaf_hdr leafhdr;
2237 :
2238 : /*
2239 : * Find the leaf entry.
2240 : */
2241 508755 : blk = &state->path.blk[state->path.active - 1];
2242 508755 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2243 508755 : ASSERT(state->extravalid);
2244 :
2245 508755 : xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2246 508755 : blk->bp->b_addr);
2247 : /*
2248 : * Point to the data entry.
2249 : */
2250 508755 : hdr = state->extrablk.bp->b_addr;
2251 508755 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2252 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2253 1017510 : dep = (xfs_dir2_data_entry_t *)
2254 : ((char *)hdr +
2255 1017510 : xfs_dir2_dataptr_to_off(args->geo,
2256 508755 : be32_to_cpu(leafhdr.ents[blk->index].address)));
2257 508755 : ASSERT(inum != be64_to_cpu(dep->inumber));
2258 : /*
2259 : * Fill in the new inode number and log the entry.
2260 : */
2261 508755 : dep->inumber = cpu_to_be64(inum);
2262 508755 : xfs_dir2_data_put_ftype(state->mp, dep, ftype);
2263 508755 : xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2264 508753 : rval = 0;
2265 : }
2266 : /*
2267 : * Didn't find it, and we're holding a data block. Drop it.
2268 : */
2269 108 : 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 1526372 : for (i = 0; i < state->path.active; i++) {
2277 1017508 : xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2278 1017511 : state->path.blk[i].bp = NULL;
2279 : }
2280 508864 : xfs_da_state_free(state);
2281 508864 : 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 2270 : 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 2270 : struct xfs_buf *bp; /* freespace buffer */
2295 2270 : xfs_inode_t *dp; /* incore directory inode */
2296 2270 : int error; /* error return code */
2297 2270 : xfs_dir2_free_t *free; /* freespace structure */
2298 2270 : xfs_trans_t *tp; /* transaction pointer */
2299 2270 : struct xfs_dir3_icfree_hdr freehdr;
2300 :
2301 2270 : dp = args->dp;
2302 2270 : tp = args->trans;
2303 :
2304 2270 : *rvalp = 0;
2305 :
2306 : /*
2307 : * Read the freespace block.
2308 : */
2309 2270 : error = xfs_dir2_free_try_read(tp, dp, args->owner, fo, &bp);
2310 2270 : 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 2270 : if (!bp)
2317 : return 0;
2318 2270 : free = bp->b_addr;
2319 2270 : 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 2270 : if (freehdr.nused > 0) {
2325 2270 : xfs_trans_brelse(tp, bp);
2326 2270 : 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 : }
|