Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2003,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_health.h"
23 :
24 : /*
25 : * Local function declarations.
26 : */
27 : static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
28 : int *indexp, struct xfs_buf **dbpp,
29 : struct xfs_dir3_icleaf_hdr *leafhdr);
30 : static void xfs_dir3_leaf_log_bests(struct xfs_da_args *args,
31 : struct xfs_buf *bp, int first, int last);
32 : static void xfs_dir3_leaf_log_tail(struct xfs_da_args *args,
33 : struct xfs_buf *bp);
34 :
35 : void
36 912903008 : xfs_dir2_leaf_hdr_from_disk(
37 : struct xfs_mount *mp,
38 : struct xfs_dir3_icleaf_hdr *to,
39 : struct xfs_dir2_leaf *from)
40 : {
41 912903008 : if (xfs_has_crc(mp)) {
42 912903008 : struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
43 :
44 912903008 : to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
45 912903008 : to->back = be32_to_cpu(from3->hdr.info.hdr.back);
46 912903008 : to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
47 912903008 : to->count = be16_to_cpu(from3->hdr.count);
48 912903008 : to->stale = be16_to_cpu(from3->hdr.stale);
49 912903008 : to->ents = from3->__ents;
50 :
51 912903008 : ASSERT(to->magic == XFS_DIR3_LEAF1_MAGIC ||
52 : to->magic == XFS_DIR3_LEAFN_MAGIC);
53 : } else {
54 0 : to->forw = be32_to_cpu(from->hdr.info.forw);
55 0 : to->back = be32_to_cpu(from->hdr.info.back);
56 0 : to->magic = be16_to_cpu(from->hdr.info.magic);
57 0 : to->count = be16_to_cpu(from->hdr.count);
58 0 : to->stale = be16_to_cpu(from->hdr.stale);
59 0 : to->ents = from->__ents;
60 :
61 0 : ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
62 : to->magic == XFS_DIR2_LEAFN_MAGIC);
63 : }
64 912903008 : }
65 :
66 : void
67 72063199 : xfs_dir2_leaf_hdr_to_disk(
68 : struct xfs_mount *mp,
69 : struct xfs_dir2_leaf *to,
70 : struct xfs_dir3_icleaf_hdr *from)
71 : {
72 72063199 : if (xfs_has_crc(mp)) {
73 72063199 : struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
74 :
75 72063199 : ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
76 : from->magic == XFS_DIR3_LEAFN_MAGIC);
77 :
78 72063199 : to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
79 72063199 : to3->hdr.info.hdr.back = cpu_to_be32(from->back);
80 72063199 : to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
81 72063199 : to3->hdr.count = cpu_to_be16(from->count);
82 144126398 : to3->hdr.stale = cpu_to_be16(from->stale);
83 : } else {
84 0 : ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
85 : from->magic == XFS_DIR2_LEAFN_MAGIC);
86 :
87 0 : to->hdr.info.forw = cpu_to_be32(from->forw);
88 0 : to->hdr.info.back = cpu_to_be32(from->back);
89 0 : to->hdr.info.magic = cpu_to_be16(from->magic);
90 0 : to->hdr.count = cpu_to_be16(from->count);
91 0 : to->hdr.stale = cpu_to_be16(from->stale);
92 : }
93 72063199 : }
94 :
95 : /*
96 : * Check the internal consistency of a leaf1 block.
97 : * Pop an assert if something is wrong.
98 : */
99 : #ifdef DEBUG
100 : static xfs_failaddr_t
101 28786304 : xfs_dir3_leaf1_check(
102 : struct xfs_inode *dp,
103 : struct xfs_buf *bp)
104 : {
105 28786304 : struct xfs_dir2_leaf *leaf = bp->b_addr;
106 28786304 : struct xfs_dir3_icleaf_hdr leafhdr;
107 :
108 28786304 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
109 :
110 28789324 : if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
111 28789324 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
112 28789324 : if (be64_to_cpu(leaf3->info.blkno) != xfs_buf_daddr(bp))
113 0 : return __this_address;
114 0 : } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
115 0 : return __this_address;
116 :
117 28789324 : return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
118 : }
119 :
120 : static inline void
121 28788523 : xfs_dir3_leaf_check(
122 : struct xfs_inode *dp,
123 : struct xfs_buf *bp)
124 : {
125 28788523 : xfs_failaddr_t fa;
126 :
127 28788523 : fa = xfs_dir3_leaf1_check(dp, bp);
128 28790439 : if (!fa)
129 : return;
130 0 : xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
131 0 : bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
132 : fa);
133 0 : ASSERT(0);
134 : }
135 : #else
136 : #define xfs_dir3_leaf_check(dp, bp)
137 : #endif
138 :
139 : xfs_failaddr_t
140 239786440 : xfs_dir3_leaf_check_int(
141 : struct xfs_mount *mp,
142 : struct xfs_dir3_icleaf_hdr *hdr,
143 : struct xfs_dir2_leaf *leaf,
144 : bool expensive_checking)
145 : {
146 239786440 : struct xfs_da_geometry *geo = mp->m_dir_geo;
147 239786440 : xfs_dir2_leaf_tail_t *ltp;
148 239786440 : int stale;
149 239786440 : int i;
150 239786440 : bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
151 : hdr->magic == XFS_DIR3_LEAF1_MAGIC);
152 :
153 239786440 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
154 :
155 : /*
156 : * XXX (dgc): This value is not restrictive enough.
157 : * Should factor in the size of the bests table as well.
158 : * We can deduce a value for that from i_disk_size.
159 : */
160 239786440 : if (hdr->count > geo->leaf_max_ents)
161 0 : return __this_address;
162 :
163 : /* Leaves and bests don't overlap in leaf format. */
164 239786440 : if (isleaf1 &&
165 28880306 : (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 0 : return __this_address;
167 :
168 239786440 : if (!expensive_checking)
169 : return NULL;
170 :
171 : /* Check hash value order, count stale entries. */
172 469620397 : for (i = stale = 0; i < hdr->count; i++) {
173 468244855 : if (i + 1 < hdr->count) {
174 933722092 : if (be32_to_cpu(hdr->ents[i].hashval) >
175 466861046 : be32_to_cpu(hdr->ents[i + 1].hashval))
176 0 : return __this_address;
177 : }
178 468244855 : if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
179 50577536 : stale++;
180 497975015 : if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
181 29730160 : be32_to_cpu(hdr->ents[i].address)) >=
182 29730160 : be32_to_cpu(ltp->bestcount))
183 0 : return __this_address;
184 : }
185 1375542 : if (hdr->stale != stale)
186 0 : return __this_address;
187 : return NULL;
188 : }
189 :
190 : /*
191 : * We verify the magic numbers before decoding the leaf header so that on debug
192 : * kernels we don't get assertion failures in xfs_dir3_leaf_hdr_from_disk() due
193 : * to incorrect magic numbers.
194 : */
195 : static xfs_failaddr_t
196 1375542 : xfs_dir3_leaf_verify(
197 : struct xfs_buf *bp)
198 : {
199 1375542 : struct xfs_mount *mp = bp->b_mount;
200 1375542 : struct xfs_dir3_icleaf_hdr leafhdr;
201 1375542 : xfs_failaddr_t fa;
202 :
203 1375542 : fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
204 1375542 : if (fa)
205 : return fa;
206 :
207 1375542 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
208 1375541 : return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
209 : }
210 :
211 : xfs_failaddr_t
212 169325139 : xfs_dir3_leaf_header_check(
213 : struct xfs_buf *bp,
214 : xfs_ino_t owner)
215 : {
216 169325139 : struct xfs_mount *mp = bp->b_mount;
217 :
218 169325139 : if (xfs_has_crc(mp)) {
219 169325139 : struct xfs_dir3_leaf *hdr3 = bp->b_addr;
220 :
221 169325139 : ASSERT(hdr3->hdr.info.hdr.magic ==
222 : cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
223 : hdr3->hdr.info.hdr.magic ==
224 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
225 :
226 169325139 : if (be64_to_cpu(hdr3->hdr.info.owner) != owner)
227 518 : return __this_address;
228 : }
229 :
230 : return NULL;
231 : }
232 :
233 : static void
234 78374 : xfs_dir3_leaf_read_verify(
235 : struct xfs_buf *bp)
236 : {
237 78374 : struct xfs_mount *mp = bp->b_mount;
238 78374 : xfs_failaddr_t fa;
239 :
240 156748 : if (xfs_has_crc(mp) &&
241 : !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
242 6 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
243 : else {
244 78368 : fa = xfs_dir3_leaf_verify(bp);
245 78368 : if (fa)
246 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
247 : }
248 78374 : }
249 :
250 : static void
251 758561 : xfs_dir3_leaf_write_verify(
252 : struct xfs_buf *bp)
253 : {
254 758561 : struct xfs_mount *mp = bp->b_mount;
255 758561 : struct xfs_buf_log_item *bip = bp->b_log_item;
256 758561 : struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
257 758561 : xfs_failaddr_t fa;
258 :
259 758561 : fa = xfs_dir3_leaf_verify(bp);
260 758561 : if (fa) {
261 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
262 0 : return;
263 : }
264 :
265 758561 : if (!xfs_has_crc(mp))
266 : return;
267 :
268 758561 : if (bip)
269 758561 : hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
270 :
271 758561 : xfs_buf_update_cksum(bp, XFS_DIR3_LEAF_CRC_OFF);
272 : }
273 :
274 : const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
275 : .name = "xfs_dir3_leaf1",
276 : .magic16 = { cpu_to_be16(XFS_DIR2_LEAF1_MAGIC),
277 : cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) },
278 : .verify_read = xfs_dir3_leaf_read_verify,
279 : .verify_write = xfs_dir3_leaf_write_verify,
280 : .verify_struct = xfs_dir3_leaf_verify,
281 : };
282 :
283 : const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
284 : .name = "xfs_dir3_leafn",
285 : .magic16 = { cpu_to_be16(XFS_DIR2_LEAFN_MAGIC),
286 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC) },
287 : .verify_read = xfs_dir3_leaf_read_verify,
288 : .verify_write = xfs_dir3_leaf_write_verify,
289 : .verify_struct = xfs_dir3_leaf_verify,
290 : };
291 :
292 : int
293 21591208 : xfs_dir3_leaf_read(
294 : struct xfs_trans *tp,
295 : struct xfs_inode *dp,
296 : xfs_ino_t owner,
297 : xfs_dablk_t fbno,
298 : struct xfs_buf **bpp)
299 : {
300 21591208 : xfs_failaddr_t fa;
301 21591208 : int err;
302 :
303 21591208 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
304 : &xfs_dir3_leaf1_buf_ops);
305 21594054 : if (err || !(*bpp))
306 : return err;
307 :
308 21593524 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
309 21595509 : if (fa) {
310 0 : __xfs_buf_mark_corrupt(*bpp, fa);
311 0 : xfs_trans_brelse(tp, *bpp);
312 0 : *bpp = NULL;
313 0 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
314 0 : return -EFSCORRUPTED;
315 : }
316 :
317 21595509 : if (tp)
318 11580909 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
319 : return 0;
320 : }
321 :
322 : int
323 2089933 : xfs_dir3_leafn_read(
324 : struct xfs_trans *tp,
325 : struct xfs_inode *dp,
326 : xfs_ino_t owner,
327 : xfs_dablk_t fbno,
328 : struct xfs_buf **bpp)
329 : {
330 2089933 : xfs_failaddr_t fa;
331 2089933 : int err;
332 :
333 2089933 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
334 : &xfs_dir3_leafn_buf_ops);
335 2089946 : if (err || !(*bpp))
336 : return err;
337 :
338 2089946 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
339 2089946 : if (fa) {
340 0 : __xfs_buf_mark_corrupt(*bpp, fa);
341 0 : xfs_trans_brelse(tp, *bpp);
342 0 : *bpp = NULL;
343 0 : xfs_dirattr_mark_sick(dp, XFS_DATA_FORK);
344 0 : return -EFSCORRUPTED;
345 : }
346 :
347 2089946 : if (tp)
348 2089946 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
349 : return 0;
350 : }
351 :
352 : /*
353 : * Initialize a new leaf block, leaf1 or leafn magic accepted.
354 : */
355 : static void
356 33807 : xfs_dir3_leaf_init(
357 : struct xfs_da_args *args,
358 : struct xfs_buf *bp,
359 : uint16_t type)
360 : {
361 33807 : struct xfs_mount *mp = args->dp->i_mount;
362 33807 : struct xfs_trans *tp = args->trans;
363 33807 : struct xfs_dir2_leaf *leaf = bp->b_addr;
364 :
365 33807 : ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
366 :
367 33807 : if (xfs_has_crc(mp)) {
368 33807 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
369 :
370 33807 : memset(leaf3, 0, sizeof(*leaf3));
371 :
372 33807 : leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
373 : ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
374 : : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
375 33807 : leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
376 33807 : leaf3->info.owner = cpu_to_be64(args->owner);
377 33807 : uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_meta_uuid);
378 : } else {
379 0 : memset(leaf, 0, sizeof(*leaf));
380 0 : leaf->hdr.info.magic = cpu_to_be16(type);
381 : }
382 :
383 : /*
384 : * If it's a leaf-format directory initialize the tail.
385 : * Caller is responsible for initialising the bests table.
386 : */
387 33807 : if (type == XFS_DIR2_LEAF1_MAGIC) {
388 4950 : struct xfs_dir2_leaf_tail *ltp;
389 :
390 4950 : ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
391 4950 : ltp->bestcount = 0;
392 4950 : bp->b_ops = &xfs_dir3_leaf1_buf_ops;
393 4950 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
394 : } else {
395 28857 : bp->b_ops = &xfs_dir3_leafn_buf_ops;
396 28857 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
397 : }
398 33806 : }
399 :
400 : int
401 33807 : xfs_dir3_leaf_get_buf(
402 : xfs_da_args_t *args,
403 : xfs_dir2_db_t bno,
404 : struct xfs_buf **bpp,
405 : uint16_t magic)
406 : {
407 33807 : struct xfs_inode *dp = args->dp;
408 33807 : struct xfs_trans *tp = args->trans;
409 33807 : struct xfs_buf *bp;
410 33807 : int error;
411 :
412 33807 : ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
413 33807 : ASSERT(bno >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET) &&
414 : bno < xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
415 :
416 33807 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
417 : &bp, XFS_DATA_FORK);
418 33807 : if (error)
419 : return error;
420 :
421 33807 : xfs_dir3_leaf_init(args, bp, magic);
422 33807 : xfs_dir3_leaf_log_header(args, bp);
423 33807 : if (magic == XFS_DIR2_LEAF1_MAGIC)
424 4950 : xfs_dir3_leaf_log_tail(args, bp);
425 33807 : *bpp = bp;
426 33807 : return 0;
427 : }
428 :
429 : /*
430 : * Convert a block form directory to a leaf form directory.
431 : */
432 : int /* error */
433 4952 : xfs_dir2_block_to_leaf(
434 : xfs_da_args_t *args, /* operation arguments */
435 : struct xfs_buf *dbp) /* input block's buffer */
436 : {
437 4952 : __be16 *bestsp; /* leaf's bestsp entries */
438 4952 : xfs_dablk_t blkno; /* leaf block's bno */
439 4952 : xfs_dir2_data_hdr_t *hdr; /* block header */
440 4952 : xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
441 4952 : xfs_dir2_block_tail_t *btp; /* block's tail */
442 4952 : xfs_inode_t *dp; /* incore directory inode */
443 4952 : int error; /* error return code */
444 4952 : struct xfs_buf *lbp; /* leaf block's buffer */
445 4952 : xfs_dir2_db_t ldb; /* leaf block's bno */
446 4952 : xfs_dir2_leaf_t *leaf; /* leaf structure */
447 4952 : xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
448 4952 : int needlog; /* need to log block header */
449 4952 : int needscan; /* need to rescan bestfree */
450 4952 : xfs_trans_t *tp; /* transaction pointer */
451 4952 : struct xfs_dir2_data_free *bf;
452 4952 : struct xfs_dir3_icleaf_hdr leafhdr;
453 :
454 4952 : trace_xfs_dir2_block_to_leaf(args);
455 :
456 4951 : dp = args->dp;
457 4951 : tp = args->trans;
458 : /*
459 : * Add the leaf block to the inode.
460 : * This interface will only put blocks in the leaf/node range.
461 : * Since that's empty now, we'll get the root (block 0 in range).
462 : */
463 4951 : if ((error = xfs_da_grow_inode(args, &blkno))) {
464 : return error;
465 : }
466 4949 : ldb = xfs_dir2_da_to_db(args->geo, blkno);
467 4949 : ASSERT(ldb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET));
468 : /*
469 : * Initialize the leaf block, get a buffer for it.
470 : */
471 4949 : error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
472 4951 : if (error)
473 : return error;
474 :
475 4951 : leaf = lbp->b_addr;
476 4951 : hdr = dbp->b_addr;
477 4951 : xfs_dir3_data_check(dp, dbp);
478 4951 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
479 4951 : blp = xfs_dir2_block_leaf_p(btp);
480 4951 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
481 :
482 : /*
483 : * Set the counts in the leaf header.
484 : */
485 4951 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
486 4950 : leafhdr.count = be32_to_cpu(btp->count);
487 4950 : leafhdr.stale = be32_to_cpu(btp->stale);
488 4950 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
489 4950 : xfs_dir3_leaf_log_header(args, lbp);
490 :
491 : /*
492 : * Could compact these but I think we always do the conversion
493 : * after squeezing out stale entries.
494 : */
495 9900 : memcpy(leafhdr.ents, blp,
496 : be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
497 4950 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
498 4950 : needscan = 0;
499 4950 : needlog = 1;
500 : /*
501 : * Make the space formerly occupied by the leaf entries and block
502 : * tail be free.
503 : */
504 4950 : xfs_dir2_data_make_free(args, dbp,
505 4950 : (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
506 4950 : (xfs_dir2_data_aoff_t)((char *)hdr + args->geo->blksize -
507 : (char *)blp),
508 : &needlog, &needscan);
509 : /*
510 : * Fix up the block header, make it a data block.
511 : */
512 4949 : dbp->b_ops = &xfs_dir3_data_buf_ops;
513 4949 : xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
514 4951 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
515 0 : hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
516 : else
517 4951 : hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
518 :
519 4951 : if (needscan)
520 3 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
521 : /*
522 : * Set up leaf tail and bests table.
523 : */
524 4951 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
525 4951 : ltp->bestcount = cpu_to_be32(1);
526 4951 : bestsp = xfs_dir2_leaf_bests_p(ltp);
527 4951 : bestsp[0] = bf[0].length;
528 : /*
529 : * Log the data header and leaf bests table.
530 : */
531 4951 : if (needlog)
532 4949 : xfs_dir2_data_log_header(args, dbp);
533 4951 : xfs_dir3_leaf_check(dp, lbp);
534 4950 : xfs_dir3_data_check(dp, dbp);
535 4951 : xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
536 4951 : return 0;
537 : }
538 :
539 : STATIC void
540 27176984 : xfs_dir3_leaf_find_stale(
541 : struct xfs_dir3_icleaf_hdr *leafhdr,
542 : struct xfs_dir2_leaf_entry *ents,
543 : int index,
544 : int *lowstale,
545 : int *highstale)
546 : {
547 : /*
548 : * Find the first stale entry before our index, if any.
549 : */
550 2253060481 : for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
551 2252239896 : if (ents[*lowstale].address ==
552 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
553 : break;
554 : }
555 :
556 : /*
557 : * Find the first stale entry at or after our index, if any.
558 : * Stop if the result would require moving more entries than using
559 : * lowstale.
560 : */
561 1241864856 : for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
562 1231444508 : if (ents[*highstale].address ==
563 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
564 : break;
565 1228021553 : if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
566 : break;
567 : }
568 27176984 : }
569 :
570 : struct xfs_dir2_leaf_entry *
571 37009299 : xfs_dir3_leaf_find_entry(
572 : struct xfs_dir3_icleaf_hdr *leafhdr,
573 : struct xfs_dir2_leaf_entry *ents,
574 : int index, /* leaf table position */
575 : int compact, /* need to compact leaves */
576 : int lowstale, /* index of prev stale leaf */
577 : int highstale, /* index of next stale leaf */
578 : int *lfloglow, /* low leaf logging index */
579 : int *lfloghigh) /* high leaf logging index */
580 : {
581 37009299 : if (!leafhdr->stale) {
582 9840334 : xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
583 :
584 : /*
585 : * Now we need to make room to insert the leaf entry.
586 : *
587 : * If there are no stale entries, just insert a hole at index.
588 : */
589 9840334 : lep = &ents[index];
590 9840334 : if (index < leafhdr->count)
591 18692420 : memmove(lep + 1, lep,
592 : (leafhdr->count - index) * sizeof(*lep));
593 :
594 : /*
595 : * Record low and high logging indices for the leaf.
596 : */
597 9840334 : *lfloglow = index;
598 9840334 : *lfloghigh = leafhdr->count++;
599 9840334 : return lep;
600 : }
601 :
602 : /*
603 : * There are stale entries.
604 : *
605 : * We will use one of them for the new entry. It's probably not at
606 : * the right location, so we'll have to shift some up or down first.
607 : *
608 : * If we didn't compact before, we need to find the nearest stale
609 : * entries before and after our insertion point.
610 : */
611 27168965 : if (compact == 0)
612 27165276 : xfs_dir3_leaf_find_stale(leafhdr, ents, index,
613 : &lowstale, &highstale);
614 :
615 : /*
616 : * If the low one is better, use it.
617 : */
618 27175967 : if (lowstale >= 0 &&
619 26356545 : (highstale == leafhdr->count ||
620 15934901 : index - lowstale - 1 < highstale - index)) {
621 23941598 : ASSERT(index - lowstale - 1 >= 0);
622 23941598 : ASSERT(ents[lowstale].address ==
623 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
624 :
625 : /*
626 : * Copy entries up to cover the stale entry and make room
627 : * for the new entry.
628 : */
629 23941598 : if (index - lowstale - 1 > 0) {
630 46262678 : memmove(&ents[lowstale], &ents[lowstale + 1],
631 : (index - lowstale - 1) *
632 : sizeof(xfs_dir2_leaf_entry_t));
633 : }
634 23941598 : *lfloglow = min(lowstale, *lfloglow);
635 23941598 : *lfloghigh = max(index - 1, *lfloghigh);
636 23941598 : leafhdr->stale--;
637 23941598 : return &ents[index - 1];
638 : }
639 :
640 : /*
641 : * The high one is better, so use that one.
642 : */
643 3234369 : ASSERT(highstale - index >= 0);
644 3234369 : ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
645 :
646 : /*
647 : * Copy entries down to cover the stale entry and make room for the
648 : * new entry.
649 : */
650 3234369 : if (highstale - index > 0) {
651 5122534 : memmove(&ents[index + 1], &ents[index],
652 : (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
653 : }
654 3234369 : *lfloglow = min(index, *lfloglow);
655 3234369 : *lfloghigh = max(highstale, *lfloghigh);
656 3234369 : leafhdr->stale--;
657 3234369 : return &ents[index];
658 : }
659 :
660 : /*
661 : * Add an entry to a leaf form directory.
662 : */
663 : int /* error */
664 4719410 : xfs_dir2_leaf_addname(
665 : struct xfs_da_args *args) /* operation arguments */
666 : {
667 4719410 : struct xfs_dir3_icleaf_hdr leafhdr;
668 4719410 : struct xfs_trans *tp = args->trans;
669 4719410 : __be16 *bestsp; /* freespace table in leaf */
670 4719410 : __be16 *tagp; /* end of data entry */
671 4719410 : struct xfs_buf *dbp; /* data block buffer */
672 4719410 : struct xfs_buf *lbp; /* leaf's buffer */
673 4719410 : struct xfs_dir2_leaf *leaf; /* leaf structure */
674 4719410 : struct xfs_inode *dp = args->dp; /* incore directory inode */
675 4719410 : struct xfs_dir2_data_hdr *hdr; /* data block header */
676 4719410 : struct xfs_dir2_data_entry *dep; /* data block entry */
677 4719410 : struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
678 4719410 : struct xfs_dir2_leaf_entry *ents;
679 4719410 : struct xfs_dir2_data_unused *dup; /* data unused entry */
680 4719410 : struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
681 4719410 : struct xfs_dir2_data_free *bf; /* bestfree table */
682 4719410 : int compact; /* need to compact leaves */
683 4719410 : int error; /* error return value */
684 4719410 : int grown; /* allocated new data block */
685 4719410 : int highstale = 0; /* index of next stale leaf */
686 4719410 : int i; /* temporary, index */
687 4719410 : int index; /* leaf table position */
688 4719410 : int length; /* length of new entry */
689 4719410 : int lfloglow; /* low leaf logging index */
690 4719410 : int lfloghigh; /* high leaf logging index */
691 4719410 : int lowstale = 0; /* index of prev stale leaf */
692 4719410 : int needbytes; /* leaf block bytes needed */
693 4719410 : int needlog; /* need to log data header */
694 4719410 : int needscan; /* need to rescan data free */
695 4719410 : xfs_dir2_db_t use_block; /* data block number */
696 :
697 4719410 : trace_xfs_dir2_leaf_addname(args);
698 :
699 4719494 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
700 : &lbp);
701 4719588 : if (error)
702 : return error;
703 :
704 : /*
705 : * Look up the entry by hash value and name.
706 : * We know it's not there, our caller has already done a lookup.
707 : * So the index is of the entry to insert in front of.
708 : * But if there are dup hash values the index is of the first of those.
709 : */
710 4719602 : index = xfs_dir2_leaf_search_hash(args, lbp);
711 4719498 : leaf = lbp->b_addr;
712 4719498 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
713 4719498 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
714 4719357 : ents = leafhdr.ents;
715 4719357 : bestsp = xfs_dir2_leaf_bests_p(ltp);
716 4719357 : length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
717 :
718 : /*
719 : * See if there are any entries with the same hash value
720 : * and space in their block for the new entry.
721 : * This is good because it puts multiple same-hash value entries
722 : * in a data block, improving the lookup of those entries.
723 : */
724 4719357 : for (use_block = -1, lep = &ents[index];
725 14982552 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
726 3104670 : index++, lep++) {
727 3117209 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
728 30037 : continue;
729 3087172 : i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
730 6174344 : ASSERT(i < be32_to_cpu(ltp->bestcount));
731 3087172 : ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
732 3087294 : if (be16_to_cpu(bestsp[i]) >= length) {
733 12638 : use_block = i;
734 12638 : break;
735 : }
736 : }
737 : /*
738 : * Didn't find a block yet, linear search all the data blocks.
739 : */
740 4719452 : if (use_block == -1) {
741 15749514 : for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
742 : /*
743 : * Remember a block we see that's missing.
744 : */
745 7869157 : if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
746 18625 : use_block == -1)
747 18550 : use_block = i;
748 7850607 : else if (be16_to_cpu(bestsp[i]) >= length) {
749 4701340 : use_block = i;
750 4701340 : break;
751 : }
752 : }
753 : }
754 : /*
755 : * How many bytes do we need in the leaf block?
756 : */
757 4719452 : needbytes = 0;
758 4719452 : if (!leafhdr.stale)
759 1112582 : needbytes += sizeof(xfs_dir2_leaf_entry_t);
760 4719452 : if (use_block == -1)
761 5600 : needbytes += sizeof(xfs_dir2_data_off_t);
762 :
763 : /*
764 : * Now kill use_block if it refers to a missing block, so we
765 : * can use it as an indication of allocation needed.
766 : */
767 4719452 : if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
768 75 : use_block = -1;
769 : /*
770 : * If we don't have enough free bytes but we can make enough
771 : * by compacting out stale entries, we'll do that.
772 : */
773 4719452 : if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
774 : leafhdr.stale > 1)
775 : compact = 1;
776 :
777 : /*
778 : * Otherwise if we don't have enough free bytes we need to
779 : * convert to node form.
780 : */
781 4719452 : else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
782 : /*
783 : * Just checking or no space reservation, give up.
784 : */
785 1693 : if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
786 1693 : args->total == 0) {
787 0 : xfs_trans_brelse(tp, lbp);
788 0 : return -ENOSPC;
789 : }
790 : /*
791 : * Convert to node form.
792 : */
793 1693 : error = xfs_dir2_leaf_to_node(args, lbp);
794 1693 : if (error)
795 : return error;
796 : /*
797 : * Then add the new entry.
798 : */
799 1693 : return xfs_dir2_node_addname(args);
800 : }
801 : /*
802 : * Otherwise it will fit without compaction.
803 : */
804 : else
805 : compact = 0;
806 : /*
807 : * If just checking, then it will fit unless we needed to allocate
808 : * a new data block.
809 : */
810 4717759 : if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
811 0 : xfs_trans_brelse(tp, lbp);
812 0 : return use_block == -1 ? -ENOSPC : 0;
813 : }
814 : /*
815 : * If no allocations are allowed, return now before we've
816 : * changed anything.
817 : */
818 4717759 : if (args->total == 0 && use_block == -1) {
819 0 : xfs_trans_brelse(tp, lbp);
820 0 : return -ENOSPC;
821 : }
822 : /*
823 : * Need to compact the leaf entries, removing stale ones.
824 : * Leave one stale entry behind - the one closest to our
825 : * insertion index - and we'll shift that one to our insertion
826 : * point later.
827 : */
828 4717759 : if (compact) {
829 0 : xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
830 : &highstale, &lfloglow, &lfloghigh);
831 : }
832 : /*
833 : * There are stale entries, so we'll need log-low and log-high
834 : * impossibly bad values later.
835 : */
836 4717759 : else if (leafhdr.stale) {
837 3606836 : lfloglow = leafhdr.count;
838 3606836 : lfloghigh = -1;
839 : }
840 : /*
841 : * If there was no data block space found, we need to allocate
842 : * a new one.
843 : */
844 4717759 : if (use_block == -1) {
845 : /*
846 : * Add the new data block.
847 : */
848 5664 : if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
849 : &use_block))) {
850 0 : xfs_trans_brelse(tp, lbp);
851 0 : return error;
852 : }
853 : /*
854 : * Initialize the block.
855 : */
856 5664 : if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
857 0 : xfs_trans_brelse(tp, lbp);
858 0 : return error;
859 : }
860 : /*
861 : * If we're adding a new data block on the end we need to
862 : * extend the bests table. Copy it up one entry.
863 : */
864 11328 : if (use_block >= be32_to_cpu(ltp->bestcount)) {
865 5589 : bestsp--;
866 11178 : memmove(&bestsp[0], &bestsp[1],
867 : be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
868 5589 : be32_add_cpu(<p->bestcount, 1);
869 5589 : xfs_dir3_leaf_log_tail(args, lbp);
870 5588 : xfs_dir3_leaf_log_bests(args, lbp, 0,
871 5588 : be32_to_cpu(ltp->bestcount) - 1);
872 : }
873 : /*
874 : * If we're filling in a previously empty block just log it.
875 : */
876 : else
877 75 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
878 5663 : hdr = dbp->b_addr;
879 5663 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
880 5663 : bestsp[use_block] = bf[0].length;
881 5663 : grown = 1;
882 : } else {
883 : /*
884 : * Already had space in some data block.
885 : * Just read that one in.
886 : */
887 4712095 : error = xfs_dir3_data_read(tp, dp, args->owner,
888 : xfs_dir2_db_to_da(args->geo, use_block), 0,
889 : &dbp);
890 4712222 : if (error) {
891 33 : xfs_trans_brelse(tp, lbp);
892 33 : return error;
893 : }
894 4712189 : hdr = dbp->b_addr;
895 4712189 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
896 4712189 : grown = 0;
897 : }
898 : /*
899 : * Point to the biggest freespace in our data block.
900 : */
901 9435604 : dup = (xfs_dir2_data_unused_t *)
902 4717802 : ((char *)hdr + be16_to_cpu(bf[0].offset));
903 4717802 : needscan = needlog = 0;
904 : /*
905 : * Mark the initial part of our freespace in use for the new entry.
906 : */
907 4717802 : error = xfs_dir2_data_use_free(args, dbp, dup,
908 : (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
909 : length, &needlog, &needscan);
910 4717506 : if (error) {
911 0 : xfs_trans_brelse(tp, lbp);
912 0 : return error;
913 : }
914 : /*
915 : * Initialize our new entry (at last).
916 : */
917 4717506 : dep = (xfs_dir2_data_entry_t *)dup;
918 4717506 : dep->inumber = cpu_to_be64(args->inumber);
919 4717506 : dep->namelen = args->namelen;
920 9435012 : memcpy(dep->name, args->name, dep->namelen);
921 4717506 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
922 4717525 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
923 4717591 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
924 : /*
925 : * Need to scan fix up the bestfree table.
926 : */
927 4717591 : if (needscan)
928 2471150 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
929 : /*
930 : * Need to log the data block's header.
931 : */
932 4717557 : if (needlog)
933 4717757 : xfs_dir2_data_log_header(args, dbp);
934 4717137 : xfs_dir2_data_log_entry(args, dbp, dep);
935 : /*
936 : * If the bests table needs to be changed, do it.
937 : * Log the change unless we've already done that.
938 : */
939 4717440 : if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
940 2632797 : bestsp[use_block] = bf[0].length;
941 2632797 : if (!grown)
942 2627146 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
943 : }
944 :
945 4717292 : lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
946 : highstale, &lfloglow, &lfloghigh);
947 :
948 : /*
949 : * Fill in the new leaf entry.
950 : */
951 4717594 : lep->hashval = cpu_to_be32(args->hashval);
952 4717594 : lep->address = cpu_to_be32(
953 : xfs_dir2_db_off_to_dataptr(args->geo, use_block,
954 : be16_to_cpu(*tagp)));
955 : /*
956 : * Log the leaf fields and give up the buffers.
957 : */
958 4717594 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
959 4717464 : xfs_dir3_leaf_log_header(args, lbp);
960 4717423 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
961 4717426 : xfs_dir3_leaf_check(dp, lbp);
962 4717572 : xfs_dir3_data_check(dp, dbp);
963 4717572 : return 0;
964 : }
965 :
966 : /*
967 : * Compact out any stale entries in the leaf.
968 : * Log the header and changed leaf entries, if any.
969 : */
970 : void
971 41454 : xfs_dir3_leaf_compact(
972 : xfs_da_args_t *args, /* operation arguments */
973 : struct xfs_dir3_icleaf_hdr *leafhdr,
974 : struct xfs_buf *bp) /* leaf buffer */
975 : {
976 41454 : int from; /* source leaf index */
977 41454 : xfs_dir2_leaf_t *leaf; /* leaf structure */
978 41454 : int loglow; /* first leaf entry to log */
979 41454 : int to; /* target leaf index */
980 41454 : struct xfs_inode *dp = args->dp;
981 :
982 41454 : leaf = bp->b_addr;
983 41454 : if (!leafhdr->stale)
984 : return;
985 :
986 : /*
987 : * Compress out the stale entries in place.
988 : */
989 14505686 : for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
990 14464232 : if (leafhdr->ents[from].address ==
991 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
992 6510310 : continue;
993 : /*
994 : * Only actually copy the entries that are different.
995 : */
996 7953922 : if (from > to) {
997 7299423 : if (loglow == -1)
998 40772 : loglow = to;
999 7299423 : leafhdr->ents[to] = leafhdr->ents[from];
1000 : }
1001 7953922 : to++;
1002 : }
1003 : /*
1004 : * Update and log the header, log the leaf entries.
1005 : */
1006 41454 : ASSERT(leafhdr->stale == from - to);
1007 41454 : leafhdr->count -= leafhdr->stale;
1008 41454 : leafhdr->stale = 0;
1009 :
1010 41454 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
1011 41454 : xfs_dir3_leaf_log_header(args, bp);
1012 41454 : if (loglow != -1)
1013 40772 : xfs_dir3_leaf_log_ents(args, leafhdr, bp, loglow, to - 1);
1014 : }
1015 :
1016 : /*
1017 : * Compact the leaf entries, removing stale ones.
1018 : * Leave one stale entry behind - the one closest to our
1019 : * insertion index - and the caller will shift that one to our insertion
1020 : * point later.
1021 : * Return new insertion index, where the remaining stale entry is,
1022 : * and leaf logging indices.
1023 : */
1024 : void
1025 3709 : xfs_dir3_leaf_compact_x1(
1026 : struct xfs_dir3_icleaf_hdr *leafhdr,
1027 : struct xfs_dir2_leaf_entry *ents,
1028 : int *indexp, /* insertion index */
1029 : int *lowstalep, /* out: stale entry before us */
1030 : int *highstalep, /* out: stale entry after us */
1031 : int *lowlogp, /* out: low log index */
1032 : int *highlogp) /* out: high log index */
1033 : {
1034 3709 : int from; /* source copy index */
1035 3709 : int highstale; /* stale entry at/after index */
1036 3709 : int index; /* insertion index */
1037 3709 : int keepstale; /* source index of kept stale */
1038 3709 : int lowstale; /* stale entry before index */
1039 3709 : int newindex=0; /* new insertion index */
1040 3709 : int to; /* destination copy index */
1041 :
1042 3709 : ASSERT(leafhdr->stale > 1);
1043 3709 : index = *indexp;
1044 :
1045 3709 : xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1046 :
1047 : /*
1048 : * Pick the better of lowstale and highstale.
1049 : */
1050 3709 : if (lowstale >= 0 &&
1051 3676 : (highstale == leafhdr->count ||
1052 2681 : index - lowstale <= highstale - index))
1053 : keepstale = lowstale;
1054 : else
1055 298 : keepstale = highstale;
1056 : /*
1057 : * Copy the entries in place, removing all the stale entries
1058 : * except keepstale.
1059 : */
1060 1873045 : for (from = to = 0; from < leafhdr->count; from++) {
1061 : /*
1062 : * Notice the new value of index.
1063 : */
1064 1869336 : if (index == from)
1065 3662 : newindex = to;
1066 1869336 : if (from != keepstale &&
1067 1865627 : ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1068 10307 : if (from == to)
1069 3709 : *lowlogp = to;
1070 10307 : continue;
1071 : }
1072 : /*
1073 : * Record the new keepstale value for the insertion.
1074 : */
1075 1859029 : if (from == keepstale)
1076 3709 : lowstale = highstale = to;
1077 : /*
1078 : * Copy only the entries that have moved.
1079 : */
1080 1859029 : if (from > to)
1081 1489822 : ents[to] = ents[from];
1082 1859029 : to++;
1083 : }
1084 3709 : ASSERT(from > to);
1085 : /*
1086 : * If the insertion point was past the last entry,
1087 : * set the new insertion point accordingly.
1088 : */
1089 3709 : if (index == from)
1090 47 : newindex = to;
1091 3709 : *indexp = newindex;
1092 : /*
1093 : * Adjust the leaf header values.
1094 : */
1095 3709 : leafhdr->count -= from - to;
1096 3709 : leafhdr->stale = 1;
1097 : /*
1098 : * Remember the low/high stale value only in the "right"
1099 : * direction.
1100 : */
1101 3709 : if (lowstale >= newindex)
1102 298 : lowstale = -1;
1103 : else
1104 3411 : highstale = leafhdr->count;
1105 3709 : *highlogp = leafhdr->count - 1;
1106 3709 : *lowstalep = lowstale;
1107 3709 : *highstalep = highstale;
1108 3709 : }
1109 :
1110 : /*
1111 : * Log the bests entries indicated from a leaf1 block.
1112 : */
1113 : static void
1114 4373448 : xfs_dir3_leaf_log_bests(
1115 : struct xfs_da_args *args,
1116 : struct xfs_buf *bp, /* leaf buffer */
1117 : int first, /* first entry to log */
1118 : int last) /* last entry to log */
1119 : {
1120 4373448 : __be16 *firstb; /* pointer to first entry */
1121 4373448 : __be16 *lastb; /* pointer to last entry */
1122 4373448 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1123 4373448 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1124 :
1125 4373448 : ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1126 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1127 :
1128 4373448 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1129 4373448 : firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1130 4373448 : lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1131 4373448 : xfs_trans_log_buf(args->trans, bp,
1132 4373448 : (uint)((char *)firstb - (char *)leaf),
1133 4373448 : (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1134 4372979 : }
1135 :
1136 : /*
1137 : * Log the leaf entries indicated from a leaf1 or leafn block.
1138 : */
1139 : void
1140 72011008 : xfs_dir3_leaf_log_ents(
1141 : struct xfs_da_args *args,
1142 : struct xfs_dir3_icleaf_hdr *hdr,
1143 : struct xfs_buf *bp,
1144 : int first,
1145 : int last)
1146 : {
1147 72011008 : xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1148 72011008 : xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1149 72011008 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1150 :
1151 72011008 : ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1152 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1153 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1154 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1155 :
1156 72011008 : firstlep = &hdr->ents[first];
1157 72011008 : lastlep = &hdr->ents[last];
1158 72011008 : xfs_trans_log_buf(args->trans, bp,
1159 72011008 : (uint)((char *)firstlep - (char *)leaf),
1160 72011008 : (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1161 72017387 : }
1162 :
1163 : /*
1164 : * Log the header of the leaf1 or leafn block.
1165 : */
1166 : void
1167 72097665 : xfs_dir3_leaf_log_header(
1168 : struct xfs_da_args *args,
1169 : struct xfs_buf *bp)
1170 : {
1171 72097665 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1172 :
1173 72097665 : ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1174 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1175 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1176 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1177 :
1178 72097665 : xfs_trans_log_buf(args->trans, bp,
1179 : (uint)((char *)&leaf->hdr - (char *)leaf),
1180 72097665 : args->geo->leaf_hdr_size - 1);
1181 72096762 : }
1182 :
1183 : /*
1184 : * Log the tail of the leaf1 block.
1185 : */
1186 : STATIC void
1187 14153 : xfs_dir3_leaf_log_tail(
1188 : struct xfs_da_args *args,
1189 : struct xfs_buf *bp)
1190 : {
1191 14153 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1192 14153 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1193 :
1194 14153 : ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1195 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1196 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1197 : leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1198 :
1199 14153 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1200 14153 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1201 : (uint)(args->geo->blksize - 1));
1202 14153 : }
1203 :
1204 : /*
1205 : * Look up the entry referred to by args in the leaf format directory.
1206 : * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1207 : * is also used by the node-format code.
1208 : */
1209 : int
1210 12047560 : xfs_dir2_leaf_lookup(
1211 : xfs_da_args_t *args) /* operation arguments */
1212 : {
1213 12047560 : struct xfs_buf *dbp; /* data block buffer */
1214 12047560 : xfs_dir2_data_entry_t *dep; /* data block entry */
1215 12047560 : xfs_inode_t *dp; /* incore directory inode */
1216 12047560 : int error; /* error return code */
1217 12047560 : int index; /* found entry index */
1218 12047560 : struct xfs_buf *lbp; /* leaf buffer */
1219 12047560 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1220 12047560 : xfs_trans_t *tp; /* transaction pointer */
1221 12047560 : struct xfs_dir3_icleaf_hdr leafhdr;
1222 :
1223 12047560 : trace_xfs_dir2_leaf_lookup(args);
1224 :
1225 : /*
1226 : * Look up name in the leaf block, returning both buffers and index.
1227 : */
1228 12047574 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1229 12047801 : if (error)
1230 : return error;
1231 :
1232 2382943 : tp = args->trans;
1233 2382943 : dp = args->dp;
1234 2382943 : xfs_dir3_leaf_check(dp, lbp);
1235 :
1236 : /*
1237 : * Get to the leaf entry and contained data entry address.
1238 : */
1239 2382951 : lep = &leafhdr.ents[index];
1240 :
1241 : /*
1242 : * Point to the data entry.
1243 : */
1244 4765902 : dep = (xfs_dir2_data_entry_t *)
1245 2382951 : ((char *)dbp->b_addr +
1246 2382951 : xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1247 : /*
1248 : * Return the found inode number & CI name if appropriate
1249 : */
1250 2382951 : args->inumber = be64_to_cpu(dep->inumber);
1251 2382951 : args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
1252 2382946 : error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1253 2382954 : xfs_trans_brelse(tp, dbp);
1254 2382968 : xfs_trans_brelse(tp, lbp);
1255 2382968 : return error;
1256 : }
1257 :
1258 : /*
1259 : * Look up name/hash in the leaf block.
1260 : * Fill in indexp with the found index, and dbpp with the data buffer.
1261 : * If not found dbpp will be NULL, and ENOENT comes back.
1262 : * lbpp will always be filled in with the leaf buffer unless there's an error.
1263 : */
1264 : static int /* error */
1265 16867724 : xfs_dir2_leaf_lookup_int(
1266 : xfs_da_args_t *args, /* operation arguments */
1267 : struct xfs_buf **lbpp, /* out: leaf buffer */
1268 : int *indexp, /* out: index in leaf block */
1269 : struct xfs_buf **dbpp, /* out: data buffer */
1270 : struct xfs_dir3_icleaf_hdr *leafhdr)
1271 : {
1272 16867724 : xfs_dir2_db_t curdb = -1; /* current data block number */
1273 16867724 : struct xfs_buf *dbp = NULL; /* data buffer */
1274 16867724 : xfs_dir2_data_entry_t *dep; /* data entry */
1275 16867724 : xfs_inode_t *dp; /* incore directory inode */
1276 16867724 : int error; /* error return code */
1277 16867724 : int index; /* index in leaf block */
1278 16867724 : struct xfs_buf *lbp; /* leaf buffer */
1279 16867724 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1280 16867724 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1281 16867724 : xfs_mount_t *mp; /* filesystem mount point */
1282 16867724 : xfs_dir2_db_t newdb; /* new data block number */
1283 16867724 : xfs_trans_t *tp; /* transaction pointer */
1284 16867724 : xfs_dir2_db_t cidb = -1; /* case match data block no. */
1285 16867724 : enum xfs_dacmp cmp; /* name compare result */
1286 :
1287 16867724 : dp = args->dp;
1288 16867724 : tp = args->trans;
1289 16867724 : mp = dp->i_mount;
1290 :
1291 16867724 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
1292 : &lbp);
1293 16869825 : if (error)
1294 : return error;
1295 :
1296 16869101 : *lbpp = lbp;
1297 16869101 : leaf = lbp->b_addr;
1298 16869101 : xfs_dir3_leaf_check(dp, lbp);
1299 16869022 : xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1300 :
1301 : /*
1302 : * Look for the first leaf entry with our hash value.
1303 : */
1304 16869046 : index = xfs_dir2_leaf_search_hash(args, lbp);
1305 : /*
1306 : * Loop over all the entries with the right hash value
1307 : * looking to match the name.
1308 : */
1309 16869067 : for (lep = &leafhdr->ents[index];
1310 46278336 : index < leafhdr->count &&
1311 88859962 : be32_to_cpu(lep->hashval) == args->hashval;
1312 29409269 : lep++, index++) {
1313 : /*
1314 : * Skip over stale leaf entries.
1315 : */
1316 36590588 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1317 3745383 : continue;
1318 : /*
1319 : * Get the new data block number.
1320 : */
1321 32845205 : newdb = xfs_dir2_dataptr_to_db(args->geo,
1322 0 : be32_to_cpu(lep->address));
1323 : /*
1324 : * If it's not the same as the old data block number,
1325 : * need to pitch the old one and read the new one.
1326 : */
1327 32845205 : if (newdb != curdb) {
1328 7493063 : if (dbp)
1329 260523 : xfs_trans_brelse(tp, dbp);
1330 7493063 : error = xfs_dir3_data_read(tp, dp, args->owner,
1331 : xfs_dir2_db_to_da(args->geo, newdb), 0,
1332 : &dbp);
1333 7492941 : if (error) {
1334 134 : xfs_trans_brelse(tp, lbp);
1335 134 : return error;
1336 : }
1337 : curdb = newdb;
1338 : }
1339 : /*
1340 : * Point to the data entry.
1341 : */
1342 65689898 : dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1343 65689898 : xfs_dir2_dataptr_to_off(args->geo,
1344 32844949 : be32_to_cpu(lep->address)));
1345 : /*
1346 : * Compare name and if it's an exact match, return the index
1347 : * and buffer. If it's the first case-insensitive match, store
1348 : * the index and buffer and continue looking for an exact match.
1349 : */
1350 32844949 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
1351 32845104 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1352 7204931 : args->cmpresult = cmp;
1353 7204931 : *indexp = index;
1354 : /* case exact match: return the current buffer. */
1355 7204931 : if (cmp == XFS_CMP_EXACT) {
1356 7181218 : *dbpp = dbp;
1357 7181218 : return 0;
1358 : }
1359 : cidb = curdb;
1360 : }
1361 : }
1362 9687748 : ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
1363 : /*
1364 : * Here, we can only be doing a lookup (not a rename or remove).
1365 : * If a case-insensitive match was found earlier, re-read the
1366 : * appropriate data block if required and return it.
1367 : */
1368 9687748 : if (args->cmpresult == XFS_CMP_CASE) {
1369 23688 : ASSERT(cidb != -1);
1370 23688 : if (cidb != curdb) {
1371 18301 : xfs_trans_brelse(tp, dbp);
1372 18301 : error = xfs_dir3_data_read(tp, dp, args->owner,
1373 : xfs_dir2_db_to_da(args->geo, cidb), 0,
1374 : &dbp);
1375 18301 : if (error) {
1376 0 : xfs_trans_brelse(tp, lbp);
1377 0 : return error;
1378 : }
1379 : }
1380 23688 : *dbpp = dbp;
1381 23688 : return 0;
1382 : }
1383 : /*
1384 : * No match found, return -ENOENT.
1385 : */
1386 9664060 : ASSERT(cidb == -1);
1387 9664060 : if (dbp)
1388 27412 : xfs_trans_brelse(tp, dbp);
1389 9664060 : xfs_trans_brelse(tp, lbp);
1390 9664060 : return -ENOENT;
1391 : }
1392 :
1393 : /*
1394 : * Remove an entry from a leaf format directory.
1395 : */
1396 : int /* error */
1397 4611395 : xfs_dir2_leaf_removename(
1398 : xfs_da_args_t *args) /* operation arguments */
1399 : {
1400 4611395 : struct xfs_da_geometry *geo = args->geo;
1401 4611395 : __be16 *bestsp; /* leaf block best freespace */
1402 4611395 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1403 4611395 : xfs_dir2_db_t db; /* data block number */
1404 4611395 : struct xfs_buf *dbp; /* data block buffer */
1405 4611395 : xfs_dir2_data_entry_t *dep; /* data entry structure */
1406 4611395 : xfs_inode_t *dp; /* incore directory inode */
1407 4611395 : int error; /* error return code */
1408 4611395 : xfs_dir2_db_t i; /* temporary data block # */
1409 4611395 : int index; /* index into leaf entries */
1410 4611395 : struct xfs_buf *lbp; /* leaf buffer */
1411 4611395 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1412 4611395 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1413 4611395 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1414 4611395 : int needlog; /* need to log data header */
1415 4611395 : int needscan; /* need to rescan data frees */
1416 4611395 : xfs_dir2_data_off_t oldbest; /* old value of best free */
1417 4611395 : struct xfs_dir2_data_free *bf; /* bestfree table */
1418 4611395 : struct xfs_dir3_icleaf_hdr leafhdr;
1419 :
1420 4611395 : trace_xfs_dir2_leaf_removename(args);
1421 :
1422 : /*
1423 : * Lookup the leaf entry, get the leaf and data blocks read in.
1424 : */
1425 4611352 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1426 4611439 : if (error)
1427 : return error;
1428 :
1429 4611438 : dp = args->dp;
1430 4611438 : leaf = lbp->b_addr;
1431 4611438 : hdr = dbp->b_addr;
1432 4611438 : xfs_dir3_data_check(dp, dbp);
1433 4611378 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1434 :
1435 : /*
1436 : * Point to the leaf entry, use that to point to the data entry.
1437 : */
1438 4611472 : lep = &leafhdr.ents[index];
1439 4611472 : db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1440 4611472 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1441 4611472 : xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
1442 4611472 : needscan = needlog = 0;
1443 4611472 : oldbest = be16_to_cpu(bf[0].length);
1444 4611472 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1445 4611472 : bestsp = xfs_dir2_leaf_bests_p(ltp);
1446 4611472 : if (be16_to_cpu(bestsp[db]) != oldbest) {
1447 0 : xfs_buf_mark_corrupt(lbp);
1448 0 : xfs_da_mark_sick(args);
1449 0 : return -EFSCORRUPTED;
1450 : }
1451 :
1452 : /*
1453 : * Mark the former data entry unused.
1454 : */
1455 4611472 : xfs_dir2_data_make_free(args, dbp,
1456 : (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1457 4611472 : xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1458 : &needscan);
1459 : /*
1460 : * We just mark the leaf entry stale by putting a null in it.
1461 : */
1462 4611465 : leafhdr.stale++;
1463 4611465 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1464 4611466 : xfs_dir3_leaf_log_header(args, lbp);
1465 :
1466 4611416 : lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1467 4611416 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, index, index);
1468 :
1469 : /*
1470 : * Scan the freespace in the data block again if necessary,
1471 : * log the data block header if necessary.
1472 : */
1473 4611414 : if (needscan)
1474 751029 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1475 4611416 : if (needlog)
1476 2837498 : xfs_dir2_data_log_header(args, dbp);
1477 : /*
1478 : * If the longest freespace in the data block has changed,
1479 : * put the new value in the bests table and log that.
1480 : */
1481 4611389 : if (be16_to_cpu(bf[0].length) != oldbest) {
1482 1732023 : bestsp[db] = bf[0].length;
1483 1732023 : xfs_dir3_leaf_log_bests(args, lbp, db, db);
1484 : }
1485 4611379 : xfs_dir3_data_check(dp, dbp);
1486 : /*
1487 : * If the data block is now empty then get rid of the data block.
1488 : */
1489 4611469 : if (be16_to_cpu(bf[0].length) ==
1490 4611469 : geo->blksize - geo->data_entry_offset) {
1491 5813 : ASSERT(db != geo->datablk);
1492 5813 : if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1493 : /*
1494 : * Nope, can't get rid of it because it caused
1495 : * allocation of a bmap btree block to do so.
1496 : * Just go on, returning success, leaving the
1497 : * empty block in place.
1498 : */
1499 0 : if (error == -ENOSPC && args->total == 0)
1500 0 : error = 0;
1501 0 : xfs_dir3_leaf_check(dp, lbp);
1502 0 : return error;
1503 : }
1504 5813 : dbp = NULL;
1505 : /*
1506 : * If this is the last data block then compact the
1507 : * bests table by getting rid of entries.
1508 : */
1509 5813 : if (db == be32_to_cpu(ltp->bestcount) - 1) {
1510 : /*
1511 : * Look for the last active entry (i).
1512 : */
1513 11929 : for (i = db - 1; i > 0; i--) {
1514 9839 : if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
1515 : break;
1516 : }
1517 : /*
1518 : * Copy the table down so inactive entries at the
1519 : * end are removed.
1520 : */
1521 7134 : memmove(&bestsp[db - i], bestsp,
1522 : (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1523 2378 : be32_add_cpu(<p->bestcount, -(db - i));
1524 2378 : xfs_dir3_leaf_log_tail(args, lbp);
1525 2378 : xfs_dir3_leaf_log_bests(args, lbp, 0,
1526 2378 : be32_to_cpu(ltp->bestcount) - 1);
1527 : } else
1528 3435 : bestsp[db] = cpu_to_be16(NULLDATAOFF);
1529 : }
1530 : /*
1531 : * If the data block was not the first one, drop it.
1532 : */
1533 4605656 : else if (db != geo->datablk)
1534 2209639 : dbp = NULL;
1535 :
1536 4611469 : xfs_dir3_leaf_check(dp, lbp);
1537 : /*
1538 : * See if we can convert to block form.
1539 : */
1540 4611457 : return xfs_dir2_leaf_to_block(args, lbp, dbp);
1541 : }
1542 :
1543 : /*
1544 : * Replace the inode number in a leaf format directory entry.
1545 : */
1546 : int /* error */
1547 210663 : xfs_dir2_leaf_replace(
1548 : xfs_da_args_t *args) /* operation arguments */
1549 : {
1550 210663 : struct xfs_buf *dbp; /* data block buffer */
1551 210663 : xfs_dir2_data_entry_t *dep; /* data block entry */
1552 210663 : xfs_inode_t *dp; /* incore directory inode */
1553 210663 : int error; /* error return code */
1554 210663 : int index; /* index of leaf entry */
1555 210663 : struct xfs_buf *lbp; /* leaf buffer */
1556 210663 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1557 210663 : xfs_trans_t *tp; /* transaction pointer */
1558 210663 : struct xfs_dir3_icleaf_hdr leafhdr;
1559 :
1560 210663 : trace_xfs_dir2_leaf_replace(args);
1561 :
1562 : /*
1563 : * Look up the entry.
1564 : */
1565 210663 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1566 210663 : if (error)
1567 : return error;
1568 :
1569 210656 : dp = args->dp;
1570 : /*
1571 : * Point to the leaf entry, get data address from it.
1572 : */
1573 210656 : lep = &leafhdr.ents[index];
1574 : /*
1575 : * Point to the data entry.
1576 : */
1577 421312 : dep = (xfs_dir2_data_entry_t *)
1578 210656 : ((char *)dbp->b_addr +
1579 210656 : xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1580 210656 : ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1581 : /*
1582 : * Put the new inode number in, log it.
1583 : */
1584 210656 : dep->inumber = cpu_to_be64(args->inumber);
1585 210656 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1586 210656 : tp = args->trans;
1587 210656 : xfs_dir2_data_log_entry(args, dbp, dep);
1588 210654 : xfs_dir3_leaf_check(dp, lbp);
1589 210656 : xfs_trans_brelse(tp, lbp);
1590 210656 : return 0;
1591 : }
1592 :
1593 : /*
1594 : * Return index in the leaf block (lbp) which is either the first
1595 : * one with this hash value, or if there are none, the insert point
1596 : * for that hash value.
1597 : */
1598 : int /* index value */
1599 166629004 : xfs_dir2_leaf_search_hash(
1600 : xfs_da_args_t *args, /* operation arguments */
1601 : struct xfs_buf *lbp) /* leaf buffer */
1602 : {
1603 166629004 : xfs_dahash_t hash=0; /* hash from this entry */
1604 166629004 : xfs_dahash_t hashwant; /* hash value looking for */
1605 166629004 : int high; /* high leaf index */
1606 166629004 : int low; /* low leaf index */
1607 166629004 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1608 166629004 : int mid=0; /* current leaf index */
1609 166629004 : struct xfs_dir3_icleaf_hdr leafhdr;
1610 :
1611 166629004 : xfs_dir2_leaf_hdr_from_disk(args->dp->i_mount, &leafhdr, lbp->b_addr);
1612 :
1613 : /*
1614 : * Note, the table cannot be empty, so we have to go through the loop.
1615 : * Binary search the leaf entries looking for our hash value.
1616 : */
1617 166637461 : for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
1618 166637461 : hashwant = args->hashval;
1619 1354409480 : low <= high; ) {
1620 1301051271 : mid = (low + high) >> 1;
1621 2602102542 : if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1622 : break;
1623 1187772019 : if (hash < hashwant)
1624 665535119 : low = mid + 1;
1625 : else
1626 522236900 : high = mid - 1;
1627 : }
1628 : /*
1629 : * Found one, back up through all the equal hash values.
1630 : */
1631 166637461 : if (hash == hashwant) {
1632 500202255 : while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
1633 : mid--;
1634 : }
1635 : }
1636 : /*
1637 : * Need to point to an entry higher than ours.
1638 : */
1639 53357073 : else if (hash < hashwant)
1640 24555670 : mid++;
1641 166637461 : return mid;
1642 : }
1643 :
1644 : /*
1645 : * Trim off a trailing data block. We know it's empty since the leaf
1646 : * freespace table says so.
1647 : */
1648 : int /* error */
1649 0 : xfs_dir2_leaf_trim_data(
1650 : xfs_da_args_t *args, /* operation arguments */
1651 : struct xfs_buf *lbp, /* leaf buffer */
1652 : xfs_dir2_db_t db) /* data block number */
1653 : {
1654 0 : struct xfs_da_geometry *geo = args->geo;
1655 0 : __be16 *bestsp; /* leaf bests table */
1656 0 : struct xfs_buf *dbp; /* data block buffer */
1657 0 : xfs_inode_t *dp; /* incore directory inode */
1658 0 : int error; /* error return value */
1659 0 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1660 0 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1661 0 : xfs_trans_t *tp; /* transaction pointer */
1662 :
1663 0 : dp = args->dp;
1664 0 : tp = args->trans;
1665 : /*
1666 : * Read the offending data block. We need its buffer.
1667 : */
1668 0 : error = xfs_dir3_data_read(tp, dp, args->owner,
1669 : xfs_dir2_db_to_da(geo, db), 0, &dbp);
1670 0 : if (error)
1671 : return error;
1672 :
1673 0 : leaf = lbp->b_addr;
1674 0 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1675 :
1676 : #ifdef DEBUG
1677 : {
1678 0 : struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
1679 0 : struct xfs_dir2_data_free *bf =
1680 0 : xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
1681 :
1682 0 : ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1683 : hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1684 0 : ASSERT(be16_to_cpu(bf[0].length) ==
1685 : geo->blksize - geo->data_entry_offset);
1686 0 : ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
1687 : }
1688 : #endif
1689 :
1690 : /*
1691 : * Get rid of the data block.
1692 : */
1693 0 : if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1694 0 : ASSERT(error != -ENOSPC);
1695 0 : xfs_trans_brelse(tp, dbp);
1696 0 : return error;
1697 : }
1698 : /*
1699 : * Eliminate the last bests entry from the table.
1700 : */
1701 0 : bestsp = xfs_dir2_leaf_bests_p(ltp);
1702 0 : be32_add_cpu(<p->bestcount, -1);
1703 0 : memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
1704 0 : xfs_dir3_leaf_log_tail(args, lbp);
1705 0 : xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1706 0 : return 0;
1707 : }
1708 :
1709 : static inline size_t
1710 1350 : xfs_dir3_leaf_size(
1711 : struct xfs_dir3_icleaf_hdr *hdr,
1712 : int counts)
1713 : {
1714 1350 : int entries;
1715 1350 : int hdrsize;
1716 :
1717 1350 : entries = hdr->count - hdr->stale;
1718 1350 : if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1719 : hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1720 : hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1721 : else
1722 1350 : hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1723 :
1724 1350 : return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1725 1350 : + counts * sizeof(xfs_dir2_data_off_t)
1726 1350 : + sizeof(xfs_dir2_leaf_tail_t);
1727 : }
1728 :
1729 : /*
1730 : * Convert node form directory to leaf form directory.
1731 : * The root of the node form dir needs to already be a LEAFN block.
1732 : * Just return if we can't do anything.
1733 : */
1734 : int /* error */
1735 30294741 : xfs_dir2_node_to_leaf(
1736 : xfs_da_state_t *state) /* directory operation state */
1737 : {
1738 30294741 : xfs_da_args_t *args; /* operation arguments */
1739 30294741 : xfs_inode_t *dp; /* incore directory inode */
1740 30294741 : int error; /* error return code */
1741 30294741 : struct xfs_buf *fbp; /* buffer for freespace block */
1742 30294741 : xfs_fileoff_t fo; /* freespace file offset */
1743 30294741 : struct xfs_buf *lbp; /* buffer for leaf block */
1744 30294741 : xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1745 30294741 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1746 30294741 : xfs_mount_t *mp; /* filesystem mount point */
1747 30294741 : int rval; /* successful free trim? */
1748 30294741 : xfs_trans_t *tp; /* transaction pointer */
1749 30294741 : struct xfs_dir3_icleaf_hdr leafhdr;
1750 30294741 : struct xfs_dir3_icfree_hdr freehdr;
1751 :
1752 : /*
1753 : * There's more than a leaf level in the btree, so there must
1754 : * be multiple leafn blocks. Give up.
1755 : */
1756 30294741 : if (state->path.active > 1)
1757 : return 0;
1758 22161 : args = state->args;
1759 :
1760 22161 : trace_xfs_dir2_node_to_leaf(args);
1761 :
1762 22161 : mp = state->mp;
1763 22161 : dp = args->dp;
1764 22161 : tp = args->trans;
1765 : /*
1766 : * Get the last offset in the file.
1767 : */
1768 22161 : if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1769 : return error;
1770 : }
1771 22161 : fo -= args->geo->fsbcount;
1772 : /*
1773 : * If there are freespace blocks other than the first one,
1774 : * take this opportunity to remove trailing empty freespace blocks
1775 : * that may have been left behind during no-space-reservation
1776 : * operations.
1777 : */
1778 22161 : while (fo > args->geo->freeblk) {
1779 2270 : if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1780 0 : return error;
1781 : }
1782 2270 : if (rval)
1783 0 : fo -= args->geo->fsbcount;
1784 : else
1785 : return 0;
1786 : }
1787 : /*
1788 : * Now find the block just before the freespace block.
1789 : */
1790 19891 : if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1791 : return error;
1792 : }
1793 : /*
1794 : * If it's not the single leaf block, give up.
1795 : */
1796 19891 : if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1797 : return 0;
1798 1350 : lbp = state->path.blk[0].bp;
1799 1350 : leaf = lbp->b_addr;
1800 1350 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1801 :
1802 1350 : ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1803 : leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1804 :
1805 : /*
1806 : * Read the freespace block.
1807 : */
1808 1350 : error = xfs_dir2_free_read(tp, dp, args->owner, args->geo->freeblk,
1809 : &fbp);
1810 1350 : if (error)
1811 : return error;
1812 1350 : xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
1813 :
1814 1350 : ASSERT(!freehdr.firstdb);
1815 :
1816 : /*
1817 : * Now see if the leafn and free data will fit in a leaf1.
1818 : * If not, release the buffer and give up.
1819 : */
1820 1350 : if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1821 115 : xfs_trans_brelse(tp, fbp);
1822 115 : return 0;
1823 : }
1824 :
1825 : /*
1826 : * If the leaf has any stale entries in it, compress them out.
1827 : */
1828 1235 : if (leafhdr.stale)
1829 133 : xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1830 :
1831 1235 : lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1832 1235 : xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1833 1235 : leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1834 : ? XFS_DIR2_LEAF1_MAGIC
1835 : : XFS_DIR3_LEAF1_MAGIC;
1836 :
1837 : /*
1838 : * Set up the leaf tail from the freespace block.
1839 : */
1840 1235 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1841 1235 : ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1842 :
1843 : /*
1844 : * Set up the leaf bests table.
1845 : */
1846 1235 : memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
1847 : freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1848 :
1849 1235 : xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1850 1235 : xfs_dir3_leaf_log_header(args, lbp);
1851 1235 : xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1852 1235 : xfs_dir3_leaf_log_tail(args, lbp);
1853 1235 : xfs_dir3_leaf_check(dp, lbp);
1854 :
1855 : /*
1856 : * Get rid of the freespace block.
1857 : */
1858 1235 : error = xfs_dir2_shrink_inode(args,
1859 : xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1860 : fbp);
1861 1235 : if (error) {
1862 : /*
1863 : * This can't fail here because it can only happen when
1864 : * punching out the middle of an extent, and this is an
1865 : * isolated block.
1866 : */
1867 0 : ASSERT(error != -ENOSPC);
1868 0 : return error;
1869 : }
1870 1235 : fbp = NULL;
1871 : /*
1872 : * Now see if we can convert the single-leaf directory
1873 : * down to a block form directory.
1874 : * This routine always kills the dabuf for the leaf, so
1875 : * eliminate it from the path.
1876 : */
1877 1235 : error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1878 1235 : state->path.blk[0].bp = NULL;
1879 1235 : return error;
1880 : }
|