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 34639808 : xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
42 : {
43 34639808 : return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
44 34639808 : (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 34635507 : xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
52 : {
53 34635507 : 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 316871461 : xfs_dir3_leafn_check(
62 : struct xfs_inode *dp,
63 : struct xfs_buf *bp)
64 : {
65 316871461 : struct xfs_dir2_leaf *leaf = bp->b_addr;
66 316871461 : struct xfs_dir3_icleaf_hdr leafhdr;
67 :
68 316871461 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
69 :
70 316834396 : if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
71 316834396 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
72 316834396 : 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 316834396 : return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
78 : }
79 :
80 : static inline void
81 317299477 : xfs_dir3_leaf_check(
82 : struct xfs_inode *dp,
83 : struct xfs_buf *bp)
84 : {
85 317299477 : xfs_failaddr_t fa;
86 :
87 317299477 : fa = xfs_dir3_leafn_check(dp, bp);
88 316960580 : 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 208641 : xfs_dir3_free_verify(
101 : struct xfs_buf *bp)
102 : {
103 208641 : struct xfs_mount *mp = bp->b_mount;
104 208641 : struct xfs_dir2_free_hdr *hdr = bp->b_addr;
105 :
106 208641 : if (!xfs_verify_magic(bp, hdr->magic))
107 0 : return __this_address;
108 :
109 208641 : if (xfs_has_crc(mp)) {
110 208641 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
111 :
112 208641 : if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
113 0 : return __this_address;
114 208641 : if (be64_to_cpu(hdr3->blkno) != xfs_buf_daddr(bp))
115 0 : return __this_address;
116 208641 : 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 33969 : xfs_dir3_free_read_verify(
127 : struct xfs_buf *bp)
128 : {
129 33969 : struct xfs_mount *mp = bp->b_mount;
130 33969 : xfs_failaddr_t fa;
131 :
132 67938 : 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 33959 : fa = xfs_dir3_free_verify(bp);
137 33959 : if (fa)
138 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
139 : }
140 33969 : }
141 :
142 : static void
143 148759 : xfs_dir3_free_write_verify(
144 : struct xfs_buf *bp)
145 : {
146 148759 : struct xfs_mount *mp = bp->b_mount;
147 148759 : struct xfs_buf_log_item *bip = bp->b_log_item;
148 148759 : struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
149 148759 : xfs_failaddr_t fa;
150 :
151 148759 : fa = xfs_dir3_free_verify(bp);
152 148759 : if (fa) {
153 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
154 0 : return;
155 : }
156 :
157 148759 : if (!xfs_has_crc(mp))
158 : return;
159 :
160 148759 : if (bip)
161 148759 : hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
162 :
163 148759 : 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 75080666 : xfs_dir3_free_header_check(
178 : struct xfs_buf *bp,
179 : xfs_ino_t owner,
180 : xfs_dablk_t fbno)
181 : {
182 75080666 : struct xfs_mount *mp = bp->b_mount;
183 75080666 : int maxbests = mp->m_dir_geo->free_max_bests;
184 75080666 : unsigned int firstdb;
185 :
186 75080666 : firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
187 75059838 : xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
188 : maxbests;
189 75072525 : if (xfs_has_crc(mp)) {
190 75072525 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
191 :
192 75072525 : if (be32_to_cpu(hdr3->firstdb) != firstdb)
193 0 : return __this_address;
194 75072525 : if (be32_to_cpu(hdr3->nvalid) > maxbests)
195 0 : return __this_address;
196 75072525 : if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
197 0 : return __this_address;
198 75072525 : 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 74921799 : __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 74921799 : xfs_failaddr_t fa;
223 74921799 : int err;
224 :
225 74921799 : err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
226 : &xfs_dir3_free_buf_ops);
227 75097249 : if (err || !*bpp)
228 : return err;
229 :
230 : /* Check things that we can't do in the verifier. */
231 75097083 : fa = xfs_dir3_free_header_check(*bpp, owner, fbno);
232 75068566 : 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 75068566 : if (tp)
242 75068566 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
243 :
244 : return 0;
245 : }
246 :
247 : void
248 111430352 : 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 111430352 : if (xfs_has_crc(mp)) {
254 111430352 : struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
255 :
256 111430352 : to->magic = be32_to_cpu(from3->hdr.hdr.magic);
257 111430352 : to->firstdb = be32_to_cpu(from3->hdr.firstdb);
258 111430352 : to->nvalid = be32_to_cpu(from3->hdr.nvalid);
259 111430352 : to->nused = be32_to_cpu(from3->hdr.nused);
260 111430352 : to->bests = from3->bests;
261 :
262 111430352 : 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 111430352 : }
273 :
274 : static void
275 272278 : 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 272278 : if (xfs_has_crc(mp)) {
281 272278 : struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
282 :
283 272278 : ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
284 :
285 272278 : to3->hdr.hdr.magic = cpu_to_be32(from->magic);
286 272278 : to3->hdr.firstdb = cpu_to_be32(from->firstdb);
287 272278 : to3->hdr.nvalid = cpu_to_be32(from->nvalid);
288 272278 : 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 272278 : }
298 :
299 : int
300 34056 : 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 34056 : 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 49226380 : return __xfs_dir3_free_read(tp, dp, owner, fbno, XFS_DABUF_MAP_HOLE_OK,
319 : bpp);
320 : }
321 :
322 : static int
323 10236 : xfs_dir3_free_get_buf(
324 : xfs_da_args_t *args,
325 : xfs_dir2_db_t fbno,
326 : struct xfs_buf **bpp)
327 : {
328 10236 : struct xfs_trans *tp = args->trans;
329 10236 : struct xfs_inode *dp = args->dp;
330 10236 : struct xfs_mount *mp = dp->i_mount;
331 10236 : struct xfs_buf *bp;
332 10236 : int error;
333 10236 : struct xfs_dir3_icfree_hdr hdr;
334 :
335 10236 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
336 : &bp, XFS_DATA_FORK);
337 10238 : if (error)
338 : return error;
339 :
340 10238 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
341 10236 : 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 10236 : memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
348 10236 : memset(&hdr, 0, sizeof(hdr));
349 :
350 10236 : if (xfs_has_crc(mp)) {
351 10236 : struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
352 :
353 10236 : hdr.magic = XFS_DIR3_FREE_MAGIC;
354 :
355 10236 : hdr3->hdr.blkno = cpu_to_be64(xfs_buf_daddr(bp));
356 10236 : hdr3->hdr.owner = cpu_to_be64(args->owner);
357 10236 : uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
358 : } else
359 0 : hdr.magic = XFS_DIR2_FREE_MAGIC;
360 10236 : xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
361 10236 : *bpp = bp;
362 10236 : return 0;
363 : }
364 :
365 : /*
366 : * Log entries from a freespace block.
367 : */
368 : STATIC void
369 56947074 : 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 56947074 : struct xfs_dir2_free *free = bp->b_addr;
377 :
378 56947074 : ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
379 : free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
380 56947074 : xfs_trans_log_buf(args->trans, bp,
381 56947074 : (char *)&hdr->bests[first] - (char *)free,
382 56947074 : (char *)&hdr->bests[last] - (char *)free +
383 : sizeof(hdr->bests[0]) - 1);
384 56995679 : }
385 :
386 : /*
387 : * Log header from a freespace block.
388 : */
389 : static void
390 262042 : xfs_dir2_free_log_header(
391 : struct xfs_da_args *args,
392 : struct xfs_buf *bp)
393 : {
394 : #ifdef DEBUG
395 262042 : xfs_dir2_free_t *free; /* freespace structure */
396 :
397 262042 : free = bp->b_addr;
398 262042 : 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 262042 : xfs_trans_log_buf(args->trans, bp, 0,
402 262042 : args->geo->free_hdr_size - 1);
403 262074 : }
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 10216 : xfs_dir2_leaf_to_node(
412 : xfs_da_args_t *args, /* operation arguments */
413 : struct xfs_buf *lbp) /* leaf buffer */
414 : {
415 10216 : xfs_inode_t *dp; /* incore directory inode */
416 10216 : int error; /* error return value */
417 10216 : struct xfs_buf *fbp; /* freespace buffer */
418 10216 : xfs_dir2_db_t fdb; /* freespace block number */
419 10216 : __be16 *from; /* pointer to freespace entry */
420 10216 : int i; /* leaf freespace index */
421 10216 : xfs_dir2_leaf_t *leaf; /* leaf structure */
422 10216 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
423 10216 : int n; /* count of live freespc ents */
424 10216 : xfs_dir2_data_off_t off; /* freespace entry value */
425 10216 : xfs_trans_t *tp; /* transaction pointer */
426 10216 : struct xfs_dir3_icfree_hdr freehdr;
427 :
428 10216 : trace_xfs_dir2_leaf_to_node(args);
429 :
430 10215 : dp = args->dp;
431 10215 : tp = args->trans;
432 : /*
433 : * Add a freespace block to the directory.
434 : */
435 10215 : if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
436 : return error;
437 : }
438 10215 : 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 10214 : error = xfs_dir3_free_get_buf(args, fdb, &fbp);
443 10215 : if (error)
444 : return error;
445 :
446 10214 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
447 10214 : leaf = lbp->b_addr;
448 10214 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
449 20428 : if (be32_to_cpu(ltp->bestcount) >
450 10214 : (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 10214 : from = xfs_dir2_leaf_bests_p(ltp);
461 42120 : for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
462 31906 : off = be16_to_cpu(*from);
463 31906 : if (off != NULLDATAOFF)
464 31906 : n++;
465 31906 : freehdr.bests[i] = cpu_to_be16(off);
466 : }
467 :
468 : /*
469 : * Now initialize the freespace block header.
470 : */
471 10214 : freehdr.nused = n;
472 10214 : freehdr.nvalid = be32_to_cpu(ltp->bestcount);
473 :
474 10214 : xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
475 10213 : xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
476 10216 : 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 10215 : 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 10215 : leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
488 10215 : lbp->b_ops = &xfs_dir3_leafn_buf_ops;
489 10215 : xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
490 10214 : xfs_dir3_leaf_log_header(args, lbp);
491 10216 : xfs_dir3_leaf_check(dp, lbp);
492 10216 : 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 48576647 : 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 48576647 : struct xfs_dir3_icleaf_hdr leafhdr;
506 48576647 : struct xfs_inode *dp = args->dp;
507 48576647 : struct xfs_dir2_leaf *leaf = bp->b_addr;
508 48576647 : struct xfs_dir2_leaf_entry *lep;
509 48576647 : struct xfs_dir2_leaf_entry *ents;
510 48576647 : int compact; /* compacting stale leaves */
511 48576647 : int highstale = 0; /* next stale entry */
512 48576647 : int lfloghigh; /* high leaf entry logging */
513 48576647 : int lfloglow; /* low leaf entry logging */
514 48576647 : int lowstale = 0; /* previous stale entry */
515 :
516 48576647 : trace_xfs_dir2_leafn_add(args, index);
517 :
518 48454446 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
519 48399792 : 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 48399792 : 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 48399792 : if (leafhdr.count == args->geo->leaf_max_ents) {
539 93920 : if (!leafhdr.stale)
540 : return -ENOSPC;
541 8347 : compact = leafhdr.stale > 1;
542 : } else
543 : compact = 0;
544 48314219 : ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
545 48314219 : ASSERT(index == leafhdr.count ||
546 : be32_to_cpu(ents[index].hashval) >= args->hashval);
547 :
548 48314219 : 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 48314219 : if (compact)
556 3191 : xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
557 : &highstale, &lfloglow, &lfloghigh);
558 48311028 : else if (leafhdr.stale) {
559 : /*
560 : * Set impossible logging indices for this case.
561 : */
562 22667635 : lfloglow = leafhdr.count;
563 22667635 : lfloghigh = -1;
564 : }
565 :
566 : /*
567 : * Insert the new entry, log everything.
568 : */
569 48314219 : lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
570 : highstale, &lfloglow, &lfloghigh);
571 :
572 48503936 : lep->hashval = cpu_to_be32(args->hashval);
573 48503936 : lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
574 : args->blkno, args->index));
575 :
576 48471740 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
577 48371032 : xfs_dir3_leaf_log_header(args, bp);
578 48545161 : xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
579 48543995 : xfs_dir3_leaf_check(dp, bp);
580 48543995 : return 0;
581 : }
582 :
583 : #ifdef DEBUG
584 : static void
585 2287093 : xfs_dir2_free_hdr_check(
586 : struct xfs_inode *dp,
587 : struct xfs_buf *bp,
588 : xfs_dir2_db_t db)
589 : {
590 2287093 : struct xfs_dir3_icfree_hdr hdr;
591 :
592 2287093 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
593 :
594 2287093 : ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
595 2287093 : ASSERT(hdr.firstdb <= db);
596 2287093 : ASSERT(db < hdr.firstdb + hdr.nvalid);
597 2287093 : }
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 315265781 : 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 315265781 : struct xfs_dir3_icleaf_hdr leafhdr;
613 :
614 315265781 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
615 :
616 315080578 : 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 315080578 : if (count)
622 90737320 : *count = leafhdr.count;
623 315080578 : if (!leafhdr.count)
624 : return 0;
625 315080578 : 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 49524613 : 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 49524613 : struct xfs_buf *curbp = NULL; /* current data/free buffer */
640 49524613 : xfs_dir2_db_t curdb = -1; /* current data block number */
641 49524613 : xfs_dir2_db_t curfdb = -1; /* current free block number */
642 49524613 : xfs_inode_t *dp; /* incore directory inode */
643 49524613 : int error; /* error return value */
644 49524613 : int fi; /* free entry index */
645 49524613 : xfs_dir2_free_t *free = NULL; /* free block structure */
646 49524613 : int index; /* leaf entry index */
647 49524613 : xfs_dir2_leaf_t *leaf; /* leaf structure */
648 49524613 : int length; /* length of new data entry */
649 49524613 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
650 49524613 : xfs_mount_t *mp; /* filesystem mount point */
651 49524613 : xfs_dir2_db_t newdb; /* new data block number */
652 49524613 : xfs_dir2_db_t newfdb; /* new free block number */
653 49524613 : xfs_trans_t *tp; /* transaction pointer */
654 49524613 : struct xfs_dir3_icleaf_hdr leafhdr;
655 :
656 49524613 : dp = args->dp;
657 49524613 : tp = args->trans;
658 49524613 : mp = dp->i_mount;
659 49524613 : leaf = bp->b_addr;
660 49524613 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
661 :
662 49526204 : xfs_dir3_leaf_check(dp, bp);
663 49504653 : ASSERT(leafhdr.count > 0);
664 :
665 : /*
666 : * Look up the hash value in the leaf entries.
667 : */
668 49504653 : index = xfs_dir2_leaf_search_hash(args, bp);
669 : /*
670 : * Do we have a buffer coming in?
671 : */
672 49641356 : if (state->extravalid) {
673 : /* If so, it's a free block buffer, get the block number. */
674 1163604 : curbp = state->extrablk.bp;
675 1163604 : curfdb = state->extrablk.blkno;
676 1163604 : free = curbp->b_addr;
677 1163604 : ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
678 : free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
679 : }
680 49641356 : length = xfs_dir2_data_entsize(mp, args->namelen);
681 : /*
682 : * Loop over leaf entries with the right hash value.
683 : */
684 49641356 : for (lep = &leafhdr.ents[index];
685 178378055 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
686 128786107 : lep++, index++) {
687 : /*
688 : * Skip stale leaf entries.
689 : */
690 130203845 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
691 1026520 : continue;
692 : /*
693 : * Pull the data block number from the entry.
694 : */
695 129177325 : 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 129050712 : if (newdb != curdb) {
706 11050636 : struct xfs_dir3_icfree_hdr freehdr;
707 :
708 11050636 : curdb = newdb;
709 : /*
710 : * Convert the data block to the free block
711 : * holding its freespace information.
712 : */
713 11050636 : 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 11050636 : if (newfdb != curfdb) {
718 : /*
719 : * If we had one before, drop it.
720 : */
721 2287091 : if (curbp)
722 356457 : xfs_trans_brelse(tp, curbp);
723 :
724 2287091 : error = xfs_dir2_free_read(tp, dp, args->owner,
725 : xfs_dir2_db_to_da(args->geo,
726 : newfdb),
727 : &curbp);
728 2287093 : if (error)
729 0 : return error;
730 2287093 : free = curbp->b_addr;
731 :
732 2287093 : xfs_dir2_free_hdr_check(dp, curbp, curdb);
733 : }
734 : /*
735 : * Get the index for our entry.
736 : */
737 11050638 : fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
738 : /*
739 : * If it has room, return it.
740 : */
741 11050638 : xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
742 11050638 : 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 11050638 : curfdb = newfdb;
751 11050638 : if (be16_to_cpu(freehdr.bests[fi]) >= length)
752 1285693 : goto out;
753 : }
754 : }
755 : /* Didn't find any space */
756 : fi = -1;
757 49459903 : out:
758 49459903 : ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
759 49459903 : if (curbp) {
760 : /* Giving back a free block. */
761 3094240 : state->extravalid = 1;
762 3094240 : state->extrablk.bp = curbp;
763 3094240 : state->extrablk.index = fi;
764 3094240 : 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 3094240 : state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
772 : } else {
773 46365663 : state->extravalid = 0;
774 : }
775 : /*
776 : * Return the index, that will be the insertion point.
777 : */
778 49459903 : *indexp = index;
779 49459903 : 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 174130406 : 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 174130406 : struct xfs_buf *curbp = NULL; /* current data/free buffer */
794 174130406 : xfs_dir2_db_t curdb = -1; /* current data block number */
795 174130406 : xfs_dir2_data_entry_t *dep; /* data block entry */
796 174130406 : xfs_inode_t *dp; /* incore directory inode */
797 174130406 : int error; /* error return value */
798 174130406 : int index; /* leaf entry index */
799 174130406 : xfs_dir2_leaf_t *leaf; /* leaf structure */
800 174130406 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
801 174130406 : xfs_mount_t *mp; /* filesystem mount point */
802 174130406 : xfs_dir2_db_t newdb; /* new data block number */
803 174130406 : xfs_trans_t *tp; /* transaction pointer */
804 174130406 : enum xfs_dacmp cmp; /* comparison result */
805 174130406 : struct xfs_dir3_icleaf_hdr leafhdr;
806 :
807 174130406 : dp = args->dp;
808 174130406 : tp = args->trans;
809 174130406 : mp = dp->i_mount;
810 174130406 : leaf = bp->b_addr;
811 174130406 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
812 :
813 173703482 : xfs_dir3_leaf_check(dp, bp);
814 173748180 : 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 173748180 : index = xfs_dir2_leaf_search_hash(args, bp);
824 : /*
825 : * Do we have a buffer coming in?
826 : */
827 174673579 : if (state->extravalid) {
828 6096139 : curbp = state->extrablk.bp;
829 6096139 : curdb = state->extrablk.blkno;
830 : }
831 : /*
832 : * Loop over leaf entries with the right hash value.
833 : */
834 174673579 : for (lep = &leafhdr.ents[index];
835 2604119732 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
836 2429446153 : lep++, index++) {
837 : /*
838 : * Skip stale leaf entries.
839 : */
840 2568412921 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
841 457652526 : continue;
842 : /*
843 : * Pull the data block number from the entry.
844 : */
845 2110760395 : 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 2109728671 : 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 195039664 : if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
859 5268720 : curdb != state->extrablk.blkno))
860 53401262 : 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 195061663 : 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 195054983 : error = xfs_dir3_data_read(tp, dp, args->owner,
871 : xfs_dir2_db_to_da(args->geo,
872 : newdb),
873 : 0, &curbp);
874 195592154 : if (error)
875 455 : return error;
876 : }
877 195598379 : xfs_dir3_data_check(dp, curbp);
878 195598379 : curdb = newdb;
879 : }
880 : /*
881 : * Point to the data entry.
882 : */
883 2110549983 : dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
884 2110549983 : xfs_dir2_dataptr_to_off(args->geo,
885 2110549983 : 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 2110549983 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
892 2110399418 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
893 : /* If there is a CI match block, drop it */
894 138961838 : if (args->cmpresult != XFS_CMP_DIFFERENT &&
895 0 : curdb != state->extrablk.blkno)
896 0 : xfs_trans_brelse(tp, state->extrablk.bp);
897 138961838 : args->cmpresult = cmp;
898 138961838 : args->inumber = be64_to_cpu(dep->inumber);
899 138961838 : args->filetype = xfs_dir2_data_get_ftype(mp, dep);
900 138897543 : *indexp = index;
901 138897543 : state->extravalid = 1;
902 138897543 : state->extrablk.bp = curbp;
903 138897543 : state->extrablk.blkno = curdb;
904 138897543 : state->extrablk.index = (int)((char *)dep -
905 138897543 : (char *)curbp->b_addr);
906 138897543 : state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
907 138897543 : curbp->b_ops = &xfs_dir3_data_buf_ops;
908 138897543 : xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
909 138918979 : if (cmp == XFS_CMP_EXACT)
910 : return -EEXIST;
911 : }
912 : }
913 35706811 : ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
914 35706811 : if (curbp) {
915 8638488 : if (args->cmpresult == XFS_CMP_DIFFERENT) {
916 : /* Giving back last used data block. */
917 7363880 : state->extravalid = 1;
918 7363880 : state->extrablk.bp = curbp;
919 7363880 : state->extrablk.index = -1;
920 7363880 : state->extrablk.blkno = curdb;
921 7363880 : state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
922 7363880 : curbp->b_ops = &xfs_dir3_data_buf_ops;
923 7363880 : 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 27068323 : state->extravalid = 0;
931 : }
932 35706811 : *indexp = index;
933 35706811 : 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 223873966 : 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 223873966 : if (args->op_flags & XFS_DA_OP_ADDNAME)
949 49585667 : return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
950 : state);
951 174288299 : 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 145448 : 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 145448 : int stale; /* count stale leaves copied */
972 :
973 145448 : trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
974 :
975 : /*
976 : * Silently return if nothing to do.
977 : */
978 145448 : 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 145442 : if (start_d < dhdr->count) {
987 37076 : memmove(&dents[start_d + count], &dents[start_d],
988 : (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
989 18538 : xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
990 18538 : 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 145445 : 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 290890 : memcpy(&dents[start_d], &sents[start_s],
1010 : count * sizeof(xfs_dir2_leaf_entry_t));
1011 145445 : 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 145447 : 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 145446 : shdr->count -= count;
1028 145446 : shdr->stale -= stale;
1029 145446 : dhdr->count += count;
1030 145446 : 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 231020 : 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 231020 : struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1044 231020 : struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1045 231020 : struct xfs_dir2_leaf_entry *ents1;
1046 231020 : struct xfs_dir2_leaf_entry *ents2;
1047 231020 : struct xfs_dir3_icleaf_hdr hdr1;
1048 231020 : struct xfs_dir3_icleaf_hdr hdr2;
1049 :
1050 231020 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1051 231021 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1052 231024 : ents1 = hdr1.ents;
1053 231024 : ents2 = hdr2.ents;
1054 :
1055 231024 : if (hdr1.count > 0 && hdr2.count > 0 &&
1056 145431 : (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1057 126895 : be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1058 126895 : be32_to_cpu(ents1[hdr1.count - 1].hashval)))
1059 18538 : 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 85576 : 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 85576 : xfs_da_args_t *args; /* operation arguments */
1077 85576 : int count; /* count (& direction) leaves */
1078 85576 : int isleft; /* new goes in left leaf */
1079 85576 : xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1080 85576 : xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1081 85576 : int mid; /* midpoint leaf index */
1082 : #if defined(DEBUG) || defined(XFS_WARN)
1083 85576 : int oldstale; /* old count of stale leaves */
1084 : #endif
1085 85576 : int oldsum; /* old total leaf count */
1086 85576 : int swap_blocks; /* swapped leaf blocks */
1087 85576 : struct xfs_dir2_leaf_entry *ents1;
1088 85576 : struct xfs_dir2_leaf_entry *ents2;
1089 85576 : struct xfs_dir3_icleaf_hdr hdr1;
1090 85576 : struct xfs_dir3_icleaf_hdr hdr2;
1091 85576 : struct xfs_inode *dp = state->args->dp;
1092 :
1093 85576 : args = state->args;
1094 : /*
1095 : * If the block order is wrong, swap the arguments.
1096 : */
1097 85576 : swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1098 85576 : if (swap_blocks)
1099 0 : swap(blk1, blk2);
1100 :
1101 85576 : leaf1 = blk1->bp->b_addr;
1102 85576 : leaf2 = blk2->bp->b_addr;
1103 85576 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1104 85576 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
1105 85573 : ents1 = hdr1.ents;
1106 85573 : ents2 = hdr2.ents;
1107 :
1108 85573 : oldsum = hdr1.count + hdr2.count;
1109 : #if defined(DEBUG) || defined(XFS_WARN)
1110 85573 : oldstale = hdr1.stale + hdr2.stale;
1111 : #endif
1112 85573 : 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 85573 : 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 85573 : count = hdr1.count - mid + (isleft == 0);
1139 85573 : if (count > 0)
1140 85573 : 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 85574 : ASSERT(hdr1.count + hdr2.count == oldsum);
1149 85574 : ASSERT(hdr1.stale + hdr2.stale == oldstale);
1150 :
1151 : /* log the changes made when moving the entries */
1152 85574 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1153 85573 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
1154 85574 : xfs_dir3_leaf_log_header(args, blk1->bp);
1155 85577 : xfs_dir3_leaf_log_header(args, blk2->bp);
1156 :
1157 85575 : xfs_dir3_leaf_check(dp, blk1->bp);
1158 85576 : xfs_dir3_leaf_check(dp, blk2->bp);
1159 :
1160 : /*
1161 : * Mark whether we're inserting into the old or new leaf.
1162 : */
1163 85577 : if (hdr1.count < hdr2.count)
1164 0 : state->inleaf = swap_blocks;
1165 85577 : else if (hdr1.count > hdr2.count)
1166 0 : state->inleaf = !swap_blocks;
1167 : else
1168 85577 : state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
1169 : /*
1170 : * Adjust the expected index for insertion.
1171 : */
1172 85577 : if (!state->inleaf)
1173 56512 : 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 85577 : 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 85577 : }
1187 :
1188 : static int
1189 23415830 : 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 23415830 : int logfree = 0;
1199 23415830 : struct xfs_dir3_icfree_hdr freehdr;
1200 23415830 : struct xfs_inode *dp = args->dp;
1201 :
1202 23415830 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1203 23415946 : if (hdr) {
1204 : /*
1205 : * Data block is not empty, just set the free entry to the new
1206 : * value.
1207 : */
1208 23333602 : freehdr.bests[findex] = cpu_to_be16(longest);
1209 23333602 : xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
1210 23333602 : return 0;
1211 : }
1212 :
1213 : /* One less used entry in the free table. */
1214 82344 : 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 82344 : if (findex == freehdr.nvalid - 1) {
1222 1975 : int i; /* free entry index */
1223 :
1224 42750 : for (i = findex - 1; i >= 0; i--) {
1225 42739 : if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
1226 : break;
1227 : }
1228 1975 : freehdr.nvalid = i + 1;
1229 1975 : logfree = 0;
1230 : } else {
1231 : /* Not the last entry, just punch it out. */
1232 80369 : freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
1233 80369 : logfree = 1;
1234 : }
1235 :
1236 82344 : xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
1237 82343 : 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 82344 : 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 82333 : if (logfree)
1261 80369 : 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 42107577 : 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 42107577 : struct xfs_da_geometry *geo = args->geo;
1279 42107577 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1280 42107577 : xfs_dir2_db_t db; /* data block number */
1281 42107577 : struct xfs_buf *dbp; /* data block buffer */
1282 42107577 : xfs_dir2_data_entry_t *dep; /* data block entry */
1283 42107577 : xfs_inode_t *dp; /* incore directory inode */
1284 42107577 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1285 42107577 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1286 42107577 : int longest; /* longest data free entry */
1287 42107577 : int off; /* data block entry offset */
1288 42107577 : int needlog; /* need to log data header */
1289 42107577 : int needscan; /* need to rescan data frees */
1290 42107577 : xfs_trans_t *tp; /* transaction pointer */
1291 42107577 : struct xfs_dir2_data_free *bf; /* bestfree table */
1292 42107577 : struct xfs_dir3_icleaf_hdr leafhdr;
1293 :
1294 42107577 : trace_xfs_dir2_leafn_remove(args, index);
1295 :
1296 42096474 : dp = args->dp;
1297 42096474 : tp = args->trans;
1298 42096474 : leaf = bp->b_addr;
1299 42096474 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1300 :
1301 : /*
1302 : * Point to the entry we're removing.
1303 : */
1304 42108186 : lep = &leafhdr.ents[index];
1305 :
1306 : /*
1307 : * Extract the data block and offset from the entry.
1308 : */
1309 42108186 : db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1310 42111234 : ASSERT(dblk->blkno == db);
1311 42111234 : off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
1312 42111234 : ASSERT(dblk->index == off);
1313 :
1314 : /*
1315 : * Kill the leaf entry by marking it stale.
1316 : * Log the leaf block changes.
1317 : */
1318 42111234 : leafhdr.stale++;
1319 42111234 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1320 42109155 : xfs_dir3_leaf_log_header(args, bp);
1321 :
1322 42121177 : lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1323 42121177 : 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 42127123 : dbp = dblk->bp;
1330 42127123 : hdr = dbp->b_addr;
1331 42127123 : dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1332 42127123 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1333 42107788 : longest = be16_to_cpu(bf[0].length);
1334 42107788 : needlog = needscan = 0;
1335 42107788 : xfs_dir2_data_make_free(args, dbp, off,
1336 42107788 : 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 42093585 : if (needscan)
1343 3193370 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1344 42093789 : if (needlog)
1345 30607925 : xfs_dir2_data_log_header(args, dbp);
1346 42105579 : 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 42139451 : if (longest < be16_to_cpu(bf[0].length)) {
1352 23420720 : int error; /* error return value */
1353 23420720 : struct xfs_buf *fbp; /* freeblock buffer */
1354 23420720 : xfs_dir2_db_t fdb; /* freeblock block number */
1355 23420720 : int findex; /* index in freeblock entries */
1356 23420720 : 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 23420720 : fdb = xfs_dir2_db_to_fdb(geo, db);
1363 23418949 : error = xfs_dir2_free_read(tp, dp, args->owner,
1364 : xfs_dir2_db_to_da(geo, fdb), &fbp);
1365 23417101 : if (error)
1366 12 : return error;
1367 23417089 : free = fbp->b_addr;
1368 : #ifdef DEBUG
1369 : {
1370 23417089 : struct xfs_dir3_icfree_hdr freehdr;
1371 :
1372 23417089 : xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
1373 23415823 : 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 23415371 : findex = xfs_dir2_db_to_fdindex(geo, db);
1381 23415447 : 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 23415447 : if (longest == geo->blksize - geo->data_entry_offset) {
1387 : /*
1388 : * Try to punch out the data block.
1389 : */
1390 82344 : error = xfs_dir2_shrink_inode(args, db, dbp);
1391 82344 : if (error == 0) {
1392 82344 : dblk->bp = NULL;
1393 82344 : 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 23415447 : error = xfs_dir3_data_block_free(args, hdr, free,
1408 : fdb, findex, fbp, longest);
1409 23417229 : if (error)
1410 : return error;
1411 : }
1412 :
1413 42135960 : 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 42136831 : *rval = (geo->leaf_hdr_size +
1419 42136831 : (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
1420 42136831 : geo->magicpct;
1421 42136831 : return 0;
1422 : }
1423 :
1424 : /*
1425 : * Split the leaf entries in the old block into old and new blocks.
1426 : */
1427 : int /* error */
1428 85573 : 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 85573 : xfs_da_args_t *args; /* operation arguments */
1434 85573 : xfs_dablk_t blkno; /* new leaf block number */
1435 85573 : int error; /* error return value */
1436 85573 : struct xfs_inode *dp;
1437 :
1438 : /*
1439 : * Allocate space for a new leaf node.
1440 : */
1441 85573 : args = state->args;
1442 85573 : dp = args->dp;
1443 85573 : ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1444 85573 : error = xfs_da_grow_inode(args, &blkno);
1445 85575 : if (error) {
1446 : return error;
1447 : }
1448 : /*
1449 : * Initialize the new leaf block.
1450 : */
1451 85575 : error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
1452 : &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1453 85577 : if (error)
1454 : return error;
1455 :
1456 85577 : newblk->blkno = blkno;
1457 85577 : 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 85577 : xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1463 85573 : error = xfs_da3_blk_link(state, oldblk, newblk);
1464 85576 : if (error) {
1465 : return error;
1466 : }
1467 : /*
1468 : * Insert the new entry in the correct block.
1469 : */
1470 85576 : if (state->inleaf)
1471 29063 : error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1472 : else
1473 56513 : 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 85577 : oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1478 85576 : newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
1479 85576 : xfs_dir3_leaf_check(dp, oldblk->bp);
1480 85577 : xfs_dir3_leaf_check(dp, newblk->bp);
1481 85577 : 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 2816897 : xfs_dir2_leafn_toosmall(
1495 : xfs_da_state_t *state, /* btree cursor */
1496 : int *action) /* resulting action to take */
1497 : {
1498 2816897 : xfs_da_state_blk_t *blk; /* leaf block */
1499 2816897 : xfs_dablk_t blkno; /* leaf block number */
1500 2816897 : struct xfs_buf *bp; /* leaf buffer */
1501 2816897 : int bytes; /* bytes in use */
1502 2816897 : int count; /* leaf live entry count */
1503 2816897 : int error; /* error return value */
1504 2816897 : int forward; /* sibling block direction */
1505 2816897 : int i; /* sibling counter */
1506 2816897 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1507 2816897 : int rval; /* result from path_shift */
1508 2816897 : struct xfs_dir3_icleaf_hdr leafhdr;
1509 2816897 : struct xfs_dir2_leaf_entry *ents;
1510 2816897 : 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 2816897 : blk = &state->path.blk[state->path.active - 1];
1518 2816883 : leaf = blk->bp->b_addr;
1519 2816883 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
1520 2816845 : ents = leafhdr.ents;
1521 2816845 : xfs_dir3_leaf_check(dp, blk->bp);
1522 :
1523 2816863 : count = leafhdr.count - leafhdr.stale;
1524 2816863 : bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
1525 2816863 : 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 2816863 : 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 19 : forward = (leafhdr.forw != 0);
1544 38 : memcpy(&state->altpath, &state->path, sizeof(state->path));
1545 19 : error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1546 : &rval);
1547 19 : if (error)
1548 : return error;
1549 19 : *action = rval ? 2 : 0;
1550 19 : 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 2816844 : forward = leafhdr.forw < leafhdr.back;
1560 8371626 : for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
1561 5614581 : struct xfs_dir3_icleaf_hdr hdr2;
1562 :
1563 5614581 : blkno = forward ? leafhdr.forw : leafhdr.back;
1564 5614581 : if (blkno == 0)
1565 1951520 : continue;
1566 : /*
1567 : * Read the sibling leaf block.
1568 : */
1569 3663061 : error = xfs_dir3_leafn_read(state->args->trans, dp,
1570 3663061 : state->args->owner, blkno, &bp);
1571 3663172 : if (error)
1572 0 : return error;
1573 :
1574 : /*
1575 : * Count bytes in the two blocks combined.
1576 : */
1577 3663172 : count = leafhdr.count - leafhdr.stale;
1578 3663172 : bytes = state->args->geo->blksize -
1579 3663172 : (state->args->geo->blksize >> 2);
1580 :
1581 3663172 : leaf = bp->b_addr;
1582 3663172 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
1583 3663167 : ents = hdr2.ents;
1584 3663167 : count += hdr2.count - hdr2.stale;
1585 3663167 : bytes -= count * sizeof(ents[0]);
1586 :
1587 : /*
1588 : * Fits with at least 25% to spare.
1589 : */
1590 3663167 : if (bytes >= 0)
1591 : break;
1592 3603291 : xfs_trans_brelse(state->args->trans, bp);
1593 : }
1594 : /*
1595 : * Didn't like either block, give up.
1596 : */
1597 2816921 : if (i >= 2) {
1598 2757045 : *action = 0;
1599 2757045 : 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 119752 : memcpy(&state->altpath, &state->path, sizeof(state->path));
1607 59876 : if (blkno < blk->blkno)
1608 24306 : error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
1609 : &rval);
1610 : else
1611 35570 : error = xfs_da3_path_shift(state, &state->path, forward, 0,
1612 : &rval);
1613 59876 : if (error) {
1614 : return error;
1615 : }
1616 59876 : *action = rval ? 0 : 1;
1617 59876 : 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 59876 : 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 59876 : xfs_da_args_t *args; /* operation arguments */
1631 59876 : xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1632 59876 : xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1633 59876 : struct xfs_dir3_icleaf_hdr savehdr;
1634 59876 : struct xfs_dir3_icleaf_hdr drophdr;
1635 59876 : struct xfs_dir2_leaf_entry *sents;
1636 59876 : struct xfs_dir2_leaf_entry *dents;
1637 59876 : struct xfs_inode *dp = state->args->dp;
1638 :
1639 59876 : args = state->args;
1640 59876 : ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1641 59876 : ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1642 59876 : drop_leaf = drop_blk->bp->b_addr;
1643 59876 : save_leaf = save_blk->bp->b_addr;
1644 :
1645 59876 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1646 59876 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
1647 59876 : sents = savehdr.ents;
1648 59876 : dents = drophdr.ents;
1649 :
1650 : /*
1651 : * If there are any stale leaf entries, take this opportunity
1652 : * to purge them.
1653 : */
1654 59876 : if (drophdr.stale)
1655 53913 : xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1656 59876 : if (savehdr.stale)
1657 59676 : 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 59876 : drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
1663 59876 : if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
1664 18538 : xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1665 : save_blk->bp, &savehdr, sents, 0,
1666 18538 : drophdr.count);
1667 : else
1668 41338 : xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1669 : save_blk->bp, &savehdr, sents,
1670 41338 : savehdr.count, drophdr.count);
1671 59876 : save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1672 :
1673 : /* log the changes made when moving the entries */
1674 59876 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1675 59876 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
1676 59876 : xfs_dir3_leaf_log_header(args, save_blk->bp);
1677 59876 : xfs_dir3_leaf_log_header(args, drop_blk->bp);
1678 :
1679 59876 : xfs_dir3_leaf_check(dp, save_blk->bp);
1680 59876 : xfs_dir3_leaf_check(dp, drop_blk->bp);
1681 59876 : }
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 169516 : 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 169516 : struct xfs_inode *dp = args->dp;
1698 169516 : struct xfs_trans *tp = args->trans;
1699 169516 : struct xfs_mount *mp = dp->i_mount;
1700 169516 : struct xfs_dir2_data_free *bf;
1701 169516 : xfs_dir2_db_t fbno;
1702 169516 : struct xfs_buf *fbp;
1703 169516 : struct xfs_buf *dbp;
1704 169516 : int error;
1705 :
1706 : /* Not allowed to allocate, return failure. */
1707 169516 : if (args->total == 0)
1708 : return -ENOSPC;
1709 :
1710 : /* Allocate and initialize the new data block. */
1711 169516 : error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1712 169520 : if (error)
1713 : return error;
1714 169517 : error = xfs_dir3_data_init(args, *dbno, &dbp);
1715 169513 : if (error)
1716 : return error;
1717 :
1718 : /*
1719 : * Get the freespace block corresponding to the data block
1720 : * that was just allocated.
1721 : */
1722 169513 : fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
1723 169485 : error = xfs_dir2_free_try_read(tp, dp, args->owner,
1724 : xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1725 169516 : 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 169516 : if (!fbp) {
1733 22 : error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1734 22 : if (error)
1735 : return error;
1736 :
1737 22 : 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 22 : error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1759 22 : if (error)
1760 : return error;
1761 22 : xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
1762 :
1763 : /* Remember the first slot as our empty slot. */
1764 22 : hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1765 22 : XFS_DIR2_FREE_OFFSET)) *
1766 22 : args->geo->free_max_bests;
1767 : } else {
1768 169494 : 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 169510 : *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 169504 : if (*findex >= hdr->nvalid) {
1776 167871 : ASSERT(*findex < args->geo->free_max_bests);
1777 167871 : hdr->nvalid = *findex + 1;
1778 167871 : 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 169504 : if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1786 169506 : hdr->nused++;
1787 169506 : xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
1788 169483 : xfs_dir2_free_log_header(args, fbp);
1789 : }
1790 :
1791 : /* Update the freespace value for the new block in the table. */
1792 169516 : bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
1793 169506 : hdr->bests[*findex] = bf[0].length;
1794 :
1795 169506 : *dbpp = dbp;
1796 169506 : *fbpp = fbp;
1797 169506 : return 0;
1798 : }
1799 :
1800 : static int
1801 48172751 : 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 48172751 : struct xfs_inode *dp = args->dp;
1811 48172751 : struct xfs_trans *tp = args->trans;
1812 48172751 : struct xfs_buf *fbp = NULL;
1813 48172751 : xfs_dir2_db_t firstfbno;
1814 48172751 : xfs_dir2_db_t lastfbno;
1815 48172751 : xfs_dir2_db_t ifbno = -1;
1816 48172751 : xfs_dir2_db_t dbno = -1;
1817 48172751 : xfs_dir2_db_t fbno;
1818 48172751 : xfs_fileoff_t fo;
1819 48172751 : int findex = 0;
1820 48172751 : 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 48172751 : if (fblk) {
1828 1930636 : fbp = fblk->bp;
1829 1930636 : findex = fblk->index;
1830 1930636 : xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
1831 1930636 : if (findex >= 0) {
1832 : /* caller already found the freespace for us. */
1833 355905 : ASSERT(findex < hdr->nvalid);
1834 355905 : ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1835 355905 : ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
1836 355905 : dbno = hdr->firstdb + findex;
1837 355905 : 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 1574731 : ifbno = fblk->blkno;
1845 1574731 : xfs_trans_brelse(tp, fbp);
1846 1574731 : fbp = NULL;
1847 1574731 : 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 47816846 : error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1855 48170645 : if (error)
1856 : return error;
1857 48170973 : lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1858 48077483 : firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1859 :
1860 50338564 : for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
1861 : /* If it's ifbno we already looked at it. */
1862 50169050 : if (fbno == ifbno)
1863 1108086 : 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 49060964 : error = xfs_dir2_free_try_read(tp, dp, args->owner,
1871 : xfs_dir2_db_to_da(args->geo, fbno),
1872 : &fbp);
1873 49137286 : if (error)
1874 132 : return error;
1875 49137154 : if (!fbp)
1876 0 : continue;
1877 :
1878 49137154 : 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 2434191878 : for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
1882 2433036086 : if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1883 2425881694 : be16_to_cpu(hdr->bests[findex]) >= length) {
1884 47895902 : dbno = hdr->firstdb + findex;
1885 47895902 : goto found_block;
1886 : }
1887 : }
1888 :
1889 : /* Didn't find free space, go on to next free block */
1890 1155792 : xfs_trans_brelse(tp, fbp);
1891 : }
1892 :
1893 169514 : found_block:
1894 48421321 : *dbnop = dbno;
1895 48421321 : *fbpp = fbp;
1896 48421321 : *findexp = findex;
1897 48421321 : 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 48279694 : 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 48279694 : struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1911 48279694 : struct xfs_dir2_data_entry *dep; /* data entry pointer */
1912 48279694 : struct xfs_dir2_data_hdr *hdr; /* data block header */
1913 48279694 : struct xfs_dir2_data_free *bf;
1914 48279694 : struct xfs_trans *tp = args->trans;
1915 48279694 : struct xfs_inode *dp = args->dp;
1916 48279694 : struct xfs_dir3_icfree_hdr freehdr;
1917 48279694 : struct xfs_buf *dbp; /* data block buffer */
1918 48279694 : struct xfs_buf *fbp; /* freespace buffer */
1919 48279694 : xfs_dir2_data_aoff_t aoff;
1920 48279694 : xfs_dir2_db_t dbno; /* data block number */
1921 48279694 : int error; /* error return value */
1922 48279694 : int findex; /* freespace entry index */
1923 48279694 : int length; /* length of the new entry */
1924 48279694 : int logfree = 0; /* need to log free entry */
1925 48279694 : int needlog = 0; /* need to log data header */
1926 48279694 : int needscan = 0; /* need to rescan data frees */
1927 48279694 : __be16 *tagp; /* data entry tag pointer */
1928 :
1929 48279694 : length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
1930 48279694 : error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1931 : &findex, length);
1932 48338259 : 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 48338127 : 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 48338127 : if (dbno == -1) {
1950 : /* we're going to have to log the free block index later */
1951 169518 : logfree = 1;
1952 169518 : error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1953 : &freehdr, &findex);
1954 : } else {
1955 : /* Read the data block in. */
1956 48168609 : error = xfs_dir3_data_read(tp, dp, args->owner,
1957 : xfs_dir2_db_to_da(args->geo, dbno), 0, &dbp);
1958 : }
1959 48540547 : if (error)
1960 : return error;
1961 :
1962 : /* setup for data block up now */
1963 48540462 : hdr = dbp->b_addr;
1964 48540462 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1965 48487101 : ASSERT(be16_to_cpu(bf[0].length) >= length);
1966 :
1967 : /* Point to the existing unused space. */
1968 48487101 : dup = (xfs_dir2_data_unused_t *)
1969 48487101 : ((char *)hdr + be16_to_cpu(bf[0].offset));
1970 :
1971 : /* Mark the first part of the unused space, inuse for us. */
1972 48487101 : aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1973 48487101 : error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1974 : &needlog, &needscan);
1975 48351771 : 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 48351771 : dep = (xfs_dir2_data_entry_t *)dup;
1982 48351771 : dep->inumber = cpu_to_be64(args->inumber);
1983 48351771 : dep->namelen = args->namelen;
1984 96703542 : memcpy(dep->name, args->name, dep->namelen);
1985 48351771 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1986 48220898 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
1987 48181829 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
1988 48181829 : xfs_dir2_data_log_entry(args, dbp, dep);
1989 :
1990 : /* Rescan the freespace and log the data block if needed. */
1991 48386164 : if (needscan)
1992 14295095 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1993 48396323 : if (needlog)
1994 48397332 : xfs_dir2_data_log_header(args, dbp);
1995 :
1996 : /* If the freespace block entry is now wrong, update it. */
1997 48525138 : if (freehdr.bests[findex] != bf[0].length) {
1998 33570463 : freehdr.bests[findex] = bf[0].length;
1999 33570463 : logfree = 1;
2000 : }
2001 :
2002 : /* Log the freespace entry if needed. */
2003 48525138 : if (logfree)
2004 33570463 : xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
2005 :
2006 : /* Return the data block and offset in args. */
2007 48493976 : args->blkno = (xfs_dablk_t)dbno;
2008 48493976 : args->index = be16_to_cpu(*tagp);
2009 48493976 : return 0;
2010 : }
2011 :
2012 : /*
2013 : * Top-level node form directory addname routine.
2014 : */
2015 : int /* error */
2016 48403295 : xfs_dir2_node_addname(
2017 : xfs_da_args_t *args) /* operation arguments */
2018 : {
2019 48403295 : xfs_da_state_blk_t *blk; /* leaf block for insert */
2020 48403295 : int error; /* error return value */
2021 48403295 : int rval; /* sub-return value */
2022 48403295 : xfs_da_state_t *state; /* btree cursor */
2023 :
2024 48403295 : trace_xfs_dir2_node_addname(args);
2025 :
2026 : /*
2027 : * Allocate and initialize the state (btree cursor).
2028 : */
2029 48210395 : 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 48483550 : error = xfs_da3_node_lookup_int(state, &rval);
2035 48279503 : if (error)
2036 8 : rval = error;
2037 48279503 : 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 48279495 : rval = xfs_dir2_node_addname_int(args,
2045 48279495 : state->extravalid ? &state->extrablk : NULL);
2046 48506916 : if (rval) {
2047 217 : goto done;
2048 : }
2049 48506699 : blk = &state->path.blk[state->path.active - 1];
2050 48506959 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2051 : /*
2052 : * Add the new leaf entry.
2053 : */
2054 48506959 : rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2055 48496266 : if (rval == 0) {
2056 : /*
2057 : * It worked, fix the hash values up the btree.
2058 : */
2059 48410696 : if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2060 48409844 : xfs_da3_fixhashpath(state, &state->path);
2061 : } else {
2062 : /*
2063 : * It didn't work, we need to split the leaf block.
2064 : */
2065 85570 : 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 85570 : rval = xfs_da3_split(state);
2073 : }
2074 48412348 : done:
2075 48412348 : xfs_da_state_free(state);
2076 48408739 : 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 125651602 : xfs_dir2_node_lookup(
2086 : xfs_da_args_t *args) /* operation arguments */
2087 : {
2088 125651602 : int error; /* error return value */
2089 125651602 : int i; /* btree level */
2090 125651602 : int rval; /* operation return value */
2091 125651602 : xfs_da_state_t *state; /* btree cursor */
2092 :
2093 125651602 : trace_xfs_dir2_node_lookup(args);
2094 :
2095 : /*
2096 : * Allocate and initialize the btree cursor.
2097 : */
2098 124844206 : state = xfs_da_state_alloc(args);
2099 :
2100 : /*
2101 : * Fill in the path to the entry in the cursor.
2102 : */
2103 125725463 : error = xfs_da3_node_lookup_int(state, &rval);
2104 125484976 : if (error)
2105 1196 : rval = error;
2106 125483780 : 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 390787502 : for (i = 0; i < state->path.active; i++) {
2119 264818750 : xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2120 265658869 : state->path.blk[i].bp = NULL;
2121 : }
2122 : /*
2123 : * Release the data block if we have it.
2124 : */
2125 125968752 : if (state->extravalid && state->extrablk.bp) {
2126 98548299 : xfs_trans_brelse(args->trans, state->extrablk.bp);
2127 98330538 : state->extrablk.bp = NULL;
2128 : }
2129 125750991 : xfs_da_state_free(state);
2130 125423862 : return rval;
2131 : }
2132 :
2133 : /*
2134 : * Remove an entry from a node-format directory.
2135 : */
2136 : int /* error */
2137 42102306 : xfs_dir2_node_removename(
2138 : struct xfs_da_args *args) /* operation arguments */
2139 : {
2140 42102306 : struct xfs_da_state_blk *blk; /* leaf block */
2141 42102306 : int error; /* error return value */
2142 42102306 : int rval; /* operation return value */
2143 42102306 : struct xfs_da_state *state; /* btree cursor */
2144 :
2145 42102306 : trace_xfs_dir2_node_removename(args);
2146 :
2147 : /*
2148 : * Allocate and initialize the btree cursor.
2149 : */
2150 42074328 : state = xfs_da_state_alloc(args);
2151 :
2152 : /* Look up the entry we're deleting, set up the cursor. */
2153 42119376 : error = xfs_da3_node_lookup_int(state, &rval);
2154 42121057 : if (error)
2155 2 : goto out_free;
2156 :
2157 : /* Didn't find it, upper layer screwed up. */
2158 42121055 : if (rval != -EEXIST) {
2159 1 : error = rval;
2160 1 : goto out_free;
2161 : }
2162 :
2163 42121054 : blk = &state->path.blk[state->path.active - 1];
2164 42118726 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2165 42118726 : ASSERT(state->extravalid);
2166 : /*
2167 : * Remove the leaf and data entries.
2168 : * Extrablk refers to the data block.
2169 : */
2170 42118726 : error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2171 : &state->extrablk, &rval);
2172 42134954 : if (error)
2173 12 : goto out_free;
2174 : /*
2175 : * Fix the hash values up the btree.
2176 : */
2177 42134942 : xfs_da3_fixhashpath(state, &state->path);
2178 : /*
2179 : * If we need to join leaf blocks, do it.
2180 : */
2181 42130865 : if (rval && state->path.active > 1)
2182 2816937 : error = xfs_da3_join(state);
2183 : /*
2184 : * If no errors so far, try conversion to leaf format.
2185 : */
2186 42130852 : if (!error)
2187 42130852 : error = xfs_dir2_node_to_leaf(state);
2188 0 : out_free:
2189 42124769 : xfs_da_state_free(state);
2190 42129003 : return error;
2191 : }
2192 :
2193 : /*
2194 : * Replace an entry's inode number in a node-format directory.
2195 : */
2196 : int /* error */
2197 674047 : xfs_dir2_node_replace(
2198 : xfs_da_args_t *args) /* operation arguments */
2199 : {
2200 674047 : xfs_da_state_blk_t *blk; /* leaf block */
2201 674047 : xfs_dir2_data_hdr_t *hdr; /* data block header */
2202 674047 : xfs_dir2_data_entry_t *dep; /* data entry changed */
2203 674047 : int error; /* error return value */
2204 674047 : int i; /* btree level */
2205 674047 : xfs_ino_t inum; /* new inode number */
2206 674047 : int ftype; /* new file type */
2207 674047 : int rval; /* internal return value */
2208 674047 : xfs_da_state_t *state; /* btree cursor */
2209 :
2210 674047 : trace_xfs_dir2_node_replace(args);
2211 :
2212 : /*
2213 : * Allocate and initialize the btree cursor.
2214 : */
2215 674026 : 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 674057 : inum = args->inumber;
2222 674057 : ftype = args->filetype;
2223 :
2224 : /*
2225 : * Lookup the entry to change in the btree.
2226 : */
2227 674057 : error = xfs_da3_node_lookup_int(state, &rval);
2228 674059 : if (error) {
2229 179 : 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 674059 : if (rval == -EEXIST) {
2236 673880 : struct xfs_dir3_icleaf_hdr leafhdr;
2237 :
2238 : /*
2239 : * Find the leaf entry.
2240 : */
2241 673880 : blk = &state->path.blk[state->path.active - 1];
2242 673879 : ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2243 673879 : ASSERT(state->extravalid);
2244 :
2245 673879 : xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2246 673879 : blk->bp->b_addr);
2247 : /*
2248 : * Point to the data entry.
2249 : */
2250 673872 : hdr = state->extrablk.bp->b_addr;
2251 673872 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2252 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2253 673872 : dep = (xfs_dir2_data_entry_t *)
2254 : ((char *)hdr +
2255 673872 : xfs_dir2_dataptr_to_off(args->geo,
2256 673872 : be32_to_cpu(leafhdr.ents[blk->index].address)));
2257 673872 : ASSERT(inum != be64_to_cpu(dep->inumber));
2258 : /*
2259 : * Fill in the new inode number and log the entry.
2260 : */
2261 673872 : dep->inumber = cpu_to_be64(inum);
2262 673872 : xfs_dir2_data_put_ftype(state->mp, dep, ftype);
2263 673870 : xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
2264 673863 : rval = 0;
2265 : }
2266 : /*
2267 : * Didn't find it, and we're holding a data block. Drop it.
2268 : */
2269 179 : 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 2021802 : for (i = 0; i < state->path.active; i++) {
2277 1347737 : xfs_trans_brelse(args->trans, state->path.blk[i].bp);
2278 1347757 : state->path.blk[i].bp = NULL;
2279 : }
2280 674065 : xfs_da_state_free(state);
2281 674046 : 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 8852 : 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 8852 : struct xfs_buf *bp; /* freespace buffer */
2295 8852 : xfs_inode_t *dp; /* incore directory inode */
2296 8852 : int error; /* error return code */
2297 8852 : xfs_dir2_free_t *free; /* freespace structure */
2298 8852 : xfs_trans_t *tp; /* transaction pointer */
2299 8852 : struct xfs_dir3_icfree_hdr freehdr;
2300 :
2301 8852 : dp = args->dp;
2302 8852 : tp = args->trans;
2303 :
2304 8852 : *rvalp = 0;
2305 :
2306 : /*
2307 : * Read the freespace block.
2308 : */
2309 8852 : error = xfs_dir2_free_try_read(tp, dp, args->owner, fo, &bp);
2310 8852 : 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 8852 : if (!bp)
2317 : return 0;
2318 8852 : free = bp->b_addr;
2319 8852 : 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 8852 : if (freehdr.nused > 0) {
2325 8852 : xfs_trans_brelse(tp, bp);
2326 8852 : 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 : }
|