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 845150001 : 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 845150001 : if (xfs_has_crc(mp)) {
42 845150001 : struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
43 :
44 845150001 : to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
45 845150001 : to->back = be32_to_cpu(from3->hdr.info.hdr.back);
46 845150001 : to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
47 845150001 : to->count = be16_to_cpu(from3->hdr.count);
48 845150001 : to->stale = be16_to_cpu(from3->hdr.stale);
49 845150001 : to->ents = from3->__ents;
50 :
51 845150001 : 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 845150001 : }
65 :
66 : void
67 68656811 : 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 68656811 : if (xfs_has_crc(mp)) {
73 68656811 : struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
74 :
75 68656811 : ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
76 : from->magic == XFS_DIR3_LEAFN_MAGIC);
77 :
78 68656811 : to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
79 68656811 : to3->hdr.info.hdr.back = cpu_to_be32(from->back);
80 68656811 : to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
81 68656811 : to3->hdr.count = cpu_to_be16(from->count);
82 137313622 : 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 68656811 : }
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 23842757 : xfs_dir3_leaf1_check(
102 : struct xfs_inode *dp,
103 : struct xfs_buf *bp)
104 : {
105 23842757 : struct xfs_dir2_leaf *leaf = bp->b_addr;
106 23842757 : struct xfs_dir3_icleaf_hdr leafhdr;
107 :
108 23842757 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
109 :
110 23842183 : if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
111 23842183 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
112 23842183 : 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 23842183 : return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
118 : }
119 :
120 : static inline void
121 23841869 : xfs_dir3_leaf_check(
122 : struct xfs_inode *dp,
123 : struct xfs_buf *bp)
124 : {
125 23841869 : xfs_failaddr_t fa;
126 :
127 23841869 : fa = xfs_dir3_leaf1_check(dp, bp);
128 23843071 : 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 223798097 : 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 223798097 : struct xfs_da_geometry *geo = mp->m_dir_geo;
147 223798097 : xfs_dir2_leaf_tail_t *ltp;
148 223798097 : int stale;
149 223798097 : int i;
150 223798097 : bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
151 : hdr->magic == XFS_DIR3_LEAF1_MAGIC);
152 :
153 223798097 : 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 223798097 : if (hdr->count > geo->leaf_max_ents)
161 0 : return __this_address;
162 :
163 : /* Leaves and bests don't overlap in leaf format. */
164 223798097 : if (isleaf1 &&
165 23941654 : (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 0 : return __this_address;
167 :
168 223798097 : if (!expensive_checking)
169 : return NULL;
170 :
171 : /* Check hash value order, count stale entries. */
172 653645086 : for (i = stale = 0; i < hdr->count; i++) {
173 651772793 : if (i + 1 < hdr->count) {
174 1299785430 : if (be32_to_cpu(hdr->ents[i].hashval) >
175 649892715 : be32_to_cpu(hdr->ents[i + 1].hashval))
176 0 : return __this_address;
177 : }
178 651772793 : if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
179 61882247 : stale++;
180 688396941 : if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
181 36624148 : be32_to_cpu(hdr->ents[i].address)) >=
182 36624148 : be32_to_cpu(ltp->bestcount))
183 0 : return __this_address;
184 : }
185 1872293 : 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 1872292 : xfs_dir3_leaf_verify(
197 : struct xfs_buf *bp)
198 : {
199 1872292 : struct xfs_mount *mp = bp->b_mount;
200 1872292 : struct xfs_dir3_icleaf_hdr leafhdr;
201 1872292 : xfs_failaddr_t fa;
202 :
203 1872292 : fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
204 1872293 : if (fa)
205 : return fa;
206 :
207 1872292 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
208 1872292 : return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
209 : }
210 :
211 : xfs_failaddr_t
212 156039717 : xfs_dir3_leaf_header_check(
213 : struct xfs_buf *bp,
214 : xfs_ino_t owner)
215 : {
216 156039717 : struct xfs_mount *mp = bp->b_mount;
217 :
218 156039717 : if (xfs_has_crc(mp)) {
219 156039717 : struct xfs_dir3_leaf *hdr3 = bp->b_addr;
220 :
221 156039717 : 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 156039717 : if (be64_to_cpu(hdr3->hdr.info.owner) != owner)
227 0 : return __this_address;
228 : }
229 :
230 : return NULL;
231 : }
232 :
233 : static void
234 82463 : xfs_dir3_leaf_read_verify(
235 : struct xfs_buf *bp)
236 : {
237 82463 : struct xfs_mount *mp = bp->b_mount;
238 82463 : xfs_failaddr_t fa;
239 :
240 164926 : 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 82457 : fa = xfs_dir3_leaf_verify(bp);
245 82457 : if (fa)
246 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
247 : }
248 82463 : }
249 :
250 : static void
251 1355925 : xfs_dir3_leaf_write_verify(
252 : struct xfs_buf *bp)
253 : {
254 1355925 : struct xfs_mount *mp = bp->b_mount;
255 1355925 : struct xfs_buf_log_item *bip = bp->b_log_item;
256 1355925 : struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
257 1355925 : xfs_failaddr_t fa;
258 :
259 1355925 : fa = xfs_dir3_leaf_verify(bp);
260 1355925 : if (fa) {
261 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
262 0 : return;
263 : }
264 :
265 1355925 : if (!xfs_has_crc(mp))
266 : return;
267 :
268 1355925 : if (bip)
269 1355925 : hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
270 :
271 1355925 : 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 18110333 : 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 18110333 : xfs_failaddr_t fa;
301 18110333 : int err;
302 :
303 18110333 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
304 : &xfs_dir3_leaf1_buf_ops);
305 18110947 : if (err || !(*bpp))
306 : return err;
307 :
308 18110484 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
309 18112082 : 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 18112082 : if (tp)
318 9344825 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
319 : return 0;
320 : }
321 :
322 : int
323 1809517 : 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 1809517 : xfs_failaddr_t fa;
331 1809517 : int err;
332 :
333 1809517 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
334 : &xfs_dir3_leafn_buf_ops);
335 1809535 : if (err || !(*bpp))
336 : return err;
337 :
338 1809535 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
339 1809533 : 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 1809533 : if (tp)
348 1809533 : 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 38885 : xfs_dir3_leaf_init(
357 : struct xfs_da_args *args,
358 : struct xfs_buf *bp,
359 : uint16_t type)
360 : {
361 38885 : struct xfs_mount *mp = args->dp->i_mount;
362 38885 : struct xfs_trans *tp = args->trans;
363 38885 : struct xfs_dir2_leaf *leaf = bp->b_addr;
364 :
365 38885 : ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
366 :
367 38885 : if (xfs_has_crc(mp)) {
368 38885 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
369 :
370 38885 : memset(leaf3, 0, sizeof(*leaf3));
371 :
372 38885 : 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 38885 : leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
376 38885 : leaf3->info.owner = cpu_to_be64(args->owner);
377 38885 : 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 38885 : if (type == XFS_DIR2_LEAF1_MAGIC) {
388 5409 : struct xfs_dir2_leaf_tail *ltp;
389 :
390 5409 : ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
391 5409 : ltp->bestcount = 0;
392 5409 : bp->b_ops = &xfs_dir3_leaf1_buf_ops;
393 5409 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
394 : } else {
395 33476 : bp->b_ops = &xfs_dir3_leafn_buf_ops;
396 33476 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
397 : }
398 38885 : }
399 :
400 : int
401 38885 : 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 38885 : struct xfs_inode *dp = args->dp;
408 38885 : struct xfs_trans *tp = args->trans;
409 38885 : struct xfs_buf *bp;
410 38885 : int error;
411 :
412 38885 : ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
413 38885 : 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 38885 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
417 : &bp, XFS_DATA_FORK);
418 38885 : if (error)
419 : return error;
420 :
421 38885 : xfs_dir3_leaf_init(args, bp, magic);
422 38885 : xfs_dir3_leaf_log_header(args, bp);
423 38885 : if (magic == XFS_DIR2_LEAF1_MAGIC)
424 5409 : xfs_dir3_leaf_log_tail(args, bp);
425 38885 : *bpp = bp;
426 38885 : return 0;
427 : }
428 :
429 : /*
430 : * Convert a block form directory to a leaf form directory.
431 : */
432 : int /* error */
433 5409 : xfs_dir2_block_to_leaf(
434 : xfs_da_args_t *args, /* operation arguments */
435 : struct xfs_buf *dbp) /* input block's buffer */
436 : {
437 5409 : __be16 *bestsp; /* leaf's bestsp entries */
438 5409 : xfs_dablk_t blkno; /* leaf block's bno */
439 5409 : xfs_dir2_data_hdr_t *hdr; /* block header */
440 5409 : xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
441 5409 : xfs_dir2_block_tail_t *btp; /* block's tail */
442 5409 : xfs_inode_t *dp; /* incore directory inode */
443 5409 : int error; /* error return code */
444 5409 : struct xfs_buf *lbp; /* leaf block's buffer */
445 5409 : xfs_dir2_db_t ldb; /* leaf block's bno */
446 5409 : xfs_dir2_leaf_t *leaf; /* leaf structure */
447 5409 : xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
448 5409 : int needlog; /* need to log block header */
449 5409 : int needscan; /* need to rescan bestfree */
450 5409 : xfs_trans_t *tp; /* transaction pointer */
451 5409 : struct xfs_dir2_data_free *bf;
452 5409 : struct xfs_dir3_icleaf_hdr leafhdr;
453 :
454 5409 : trace_xfs_dir2_block_to_leaf(args);
455 :
456 5409 : dp = args->dp;
457 5409 : 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 5409 : if ((error = xfs_da_grow_inode(args, &blkno))) {
464 : return error;
465 : }
466 5409 : ldb = xfs_dir2_da_to_db(args->geo, blkno);
467 5409 : 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 5409 : error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
472 5409 : if (error)
473 : return error;
474 :
475 5409 : leaf = lbp->b_addr;
476 5409 : hdr = dbp->b_addr;
477 5409 : xfs_dir3_data_check(dp, dbp);
478 5409 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
479 5409 : blp = xfs_dir2_block_leaf_p(btp);
480 5409 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
481 :
482 : /*
483 : * Set the counts in the leaf header.
484 : */
485 5409 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
486 5409 : leafhdr.count = be32_to_cpu(btp->count);
487 5409 : leafhdr.stale = be32_to_cpu(btp->stale);
488 5409 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
489 5409 : 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 10818 : memcpy(leafhdr.ents, blp,
496 : be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
497 5409 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
498 5409 : needscan = 0;
499 5409 : needlog = 1;
500 : /*
501 : * Make the space formerly occupied by the leaf entries and block
502 : * tail be free.
503 : */
504 5409 : xfs_dir2_data_make_free(args, dbp,
505 5409 : (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
506 5409 : (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 5409 : dbp->b_ops = &xfs_dir3_data_buf_ops;
513 5409 : xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
514 5409 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
515 0 : hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
516 : else
517 5409 : hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
518 :
519 5409 : if (needscan)
520 3 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
521 : /*
522 : * Set up leaf tail and bests table.
523 : */
524 5409 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
525 5409 : ltp->bestcount = cpu_to_be32(1);
526 5409 : bestsp = xfs_dir2_leaf_bests_p(ltp);
527 5409 : bestsp[0] = bf[0].length;
528 : /*
529 : * Log the data header and leaf bests table.
530 : */
531 5409 : if (needlog)
532 5409 : xfs_dir2_data_log_header(args, dbp);
533 5409 : xfs_dir3_leaf_check(dp, lbp);
534 5409 : xfs_dir3_data_check(dp, dbp);
535 5409 : xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
536 5409 : return 0;
537 : }
538 :
539 : STATIC void
540 24677241 : 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 2162893839 : for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
551 2161382014 : 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 1058064977 : for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
562 1048270530 : if (ents[*highstale].address ==
563 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
564 : break;
565 1044089648 : if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
566 : break;
567 : }
568 24677241 : }
569 :
570 : struct xfs_dir2_leaf_entry *
571 35991110 : 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 35991110 : if (!leafhdr->stale) {
582 11316950 : 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 11316950 : lep = &ents[index];
590 11316950 : if (index < leafhdr->count)
591 21585254 : memmove(lep + 1, lep,
592 : (leafhdr->count - index) * sizeof(*lep));
593 :
594 : /*
595 : * Record low and high logging indices for the leaf.
596 : */
597 11316950 : *lfloglow = index;
598 11316950 : *lfloghigh = leafhdr->count++;
599 11316950 : 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 24674160 : if (compact == 0)
612 24670769 : xfs_dir3_leaf_find_stale(leafhdr, ents, index,
613 : &lowstale, &highstale);
614 :
615 : /*
616 : * If the low one is better, use it.
617 : */
618 24676665 : if (lowstale >= 0 &&
619 23164922 : (highstale == leafhdr->count ||
620 13369194 : index - lowstale - 1 < highstale - index)) {
621 20654157 : ASSERT(index - lowstale - 1 >= 0);
622 20654157 : 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 20654157 : if (index - lowstale - 1 > 0) {
630 39837760 : memmove(&ents[lowstale], &ents[lowstale + 1],
631 : (index - lowstale - 1) *
632 : sizeof(xfs_dir2_leaf_entry_t));
633 : }
634 20654157 : *lfloglow = min(lowstale, *lfloglow);
635 20654157 : *lfloghigh = max(index - 1, *lfloghigh);
636 20654157 : leafhdr->stale--;
637 20654157 : return &ents[index - 1];
638 : }
639 :
640 : /*
641 : * The high one is better, so use that one.
642 : */
643 4022508 : ASSERT(highstale - index >= 0);
644 4022508 : 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 4022508 : if (highstale - index > 0) {
651 6805030 : memmove(&ents[index + 1], &ents[index],
652 : (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
653 : }
654 4022508 : *lfloglow = min(index, *lfloglow);
655 4022508 : *lfloghigh = max(highstale, *lfloghigh);
656 4022508 : leafhdr->stale--;
657 4022508 : return &ents[index];
658 : }
659 :
660 : /*
661 : * Add an entry to a leaf form directory.
662 : */
663 : int /* error */
664 3921972 : xfs_dir2_leaf_addname(
665 : struct xfs_da_args *args) /* operation arguments */
666 : {
667 3921972 : struct xfs_dir3_icleaf_hdr leafhdr;
668 3921972 : struct xfs_trans *tp = args->trans;
669 3921972 : __be16 *bestsp; /* freespace table in leaf */
670 3921972 : __be16 *tagp; /* end of data entry */
671 3921972 : struct xfs_buf *dbp; /* data block buffer */
672 3921972 : struct xfs_buf *lbp; /* leaf's buffer */
673 3921972 : struct xfs_dir2_leaf *leaf; /* leaf structure */
674 3921972 : struct xfs_inode *dp = args->dp; /* incore directory inode */
675 3921972 : struct xfs_dir2_data_hdr *hdr; /* data block header */
676 3921972 : struct xfs_dir2_data_entry *dep; /* data block entry */
677 3921972 : struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
678 3921972 : struct xfs_dir2_leaf_entry *ents;
679 3921972 : struct xfs_dir2_data_unused *dup; /* data unused entry */
680 3921972 : struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
681 3921972 : struct xfs_dir2_data_free *bf; /* bestfree table */
682 3921972 : int compact; /* need to compact leaves */
683 3921972 : int error; /* error return value */
684 3921972 : int grown; /* allocated new data block */
685 3921972 : int highstale = 0; /* index of next stale leaf */
686 3921972 : int i; /* temporary, index */
687 3921972 : int index; /* leaf table position */
688 3921972 : int length; /* length of new entry */
689 3921972 : int lfloglow; /* low leaf logging index */
690 3921972 : int lfloghigh; /* high leaf logging index */
691 3921972 : int lowstale = 0; /* index of prev stale leaf */
692 3921972 : int needbytes; /* leaf block bytes needed */
693 3921972 : int needlog; /* need to log data header */
694 3921972 : int needscan; /* need to rescan data free */
695 3921972 : xfs_dir2_db_t use_block; /* data block number */
696 :
697 3921972 : trace_xfs_dir2_leaf_addname(args);
698 :
699 3921976 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
700 : &lbp);
701 3922036 : 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 3922031 : index = xfs_dir2_leaf_search_hash(args, lbp);
711 3922024 : leaf = lbp->b_addr;
712 3922024 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
713 3922024 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
714 3922002 : ents = leafhdr.ents;
715 3922002 : bestsp = xfs_dir2_leaf_bests_p(ltp);
716 3922002 : 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 3922002 : for (use_block = -1, lep = &ents[index];
725 13500729 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
726 3106940 : index++, lep++) {
727 3119689 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
728 31336 : continue;
729 3088353 : i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
730 6176706 : ASSERT(i < be32_to_cpu(ltp->bestcount));
731 3088353 : ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
732 3088270 : if (be16_to_cpu(bestsp[i]) >= length) {
733 12662 : use_block = i;
734 12662 : break;
735 : }
736 : }
737 : /*
738 : * Didn't find a block yet, linear search all the data blocks.
739 : */
740 3921913 : if (use_block == -1) {
741 13172620 : for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
742 : /*
743 : * Remember a block we see that's missing.
744 : */
745 6580886 : if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
746 2937 : use_block == -1)
747 2935 : use_block = i;
748 6577951 : else if (be16_to_cpu(bestsp[i]) >= length) {
749 3903868 : use_block = i;
750 3903868 : break;
751 : }
752 : }
753 : }
754 : /*
755 : * How many bytes do we need in the leaf block?
756 : */
757 3921913 : needbytes = 0;
758 3921913 : if (!leafhdr.stale)
759 1122372 : needbytes += sizeof(xfs_dir2_leaf_entry_t);
760 3921913 : if (use_block == -1)
761 5423 : 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 3921913 : if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
768 3 : 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 3921913 : 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 3921913 : else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
782 : /*
783 : * Just checking or no space reservation, give up.
784 : */
785 1722 : if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
786 1722 : args->total == 0) {
787 0 : xfs_trans_brelse(tp, lbp);
788 0 : return -ENOSPC;
789 : }
790 : /*
791 : * Convert to node form.
792 : */
793 1722 : error = xfs_dir2_leaf_to_node(args, lbp);
794 1722 : if (error)
795 : return error;
796 : /*
797 : * Then add the new entry.
798 : */
799 1722 : 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 3920191 : 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 3920191 : 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 3920191 : 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 3920191 : else if (leafhdr.stale) {
837 2799564 : lfloglow = leafhdr.count;
838 2799564 : lfloghigh = -1;
839 : }
840 : /*
841 : * If there was no data block space found, we need to allocate
842 : * a new one.
843 : */
844 3920191 : if (use_block == -1) {
845 : /*
846 : * Add the new data block.
847 : */
848 5403 : 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 5403 : 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 10806 : if (use_block >= be32_to_cpu(ltp->bestcount)) {
865 5400 : bestsp--;
866 10800 : memmove(&bestsp[0], &bestsp[1],
867 : be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
868 5400 : be32_add_cpu(<p->bestcount, 1);
869 5400 : xfs_dir3_leaf_log_tail(args, lbp);
870 5400 : xfs_dir3_leaf_log_bests(args, lbp, 0,
871 5400 : 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 3 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
878 5403 : hdr = dbp->b_addr;
879 5403 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
880 5403 : bestsp[use_block] = bf[0].length;
881 5403 : grown = 1;
882 : } else {
883 : /*
884 : * Already had space in some data block.
885 : * Just read that one in.
886 : */
887 3914788 : error = xfs_dir3_data_read(tp, dp, args->owner,
888 : xfs_dir2_db_to_da(args->geo, use_block), 0,
889 : &dbp);
890 3914876 : if (error) {
891 37 : xfs_trans_brelse(tp, lbp);
892 37 : return error;
893 : }
894 3914839 : hdr = dbp->b_addr;
895 3914839 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
896 3914839 : grown = 0;
897 : }
898 : /*
899 : * Point to the biggest freespace in our data block.
900 : */
901 7840532 : dup = (xfs_dir2_data_unused_t *)
902 3920266 : ((char *)hdr + be16_to_cpu(bf[0].offset));
903 3920266 : needscan = needlog = 0;
904 : /*
905 : * Mark the initial part of our freespace in use for the new entry.
906 : */
907 3920266 : error = xfs_dir2_data_use_free(args, dbp, dup,
908 : (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
909 : length, &needlog, &needscan);
910 3920035 : if (error) {
911 0 : xfs_trans_brelse(tp, lbp);
912 0 : return error;
913 : }
914 : /*
915 : * Initialize our new entry (at last).
916 : */
917 3920035 : dep = (xfs_dir2_data_entry_t *)dup;
918 3920035 : dep->inumber = cpu_to_be64(args->inumber);
919 3920035 : dep->namelen = args->namelen;
920 7840070 : memcpy(dep->name, args->name, dep->namelen);
921 3920035 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
922 3920167 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
923 3920100 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
924 : /*
925 : * Need to scan fix up the bestfree table.
926 : */
927 3920100 : if (needscan)
928 1834526 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
929 : /*
930 : * Need to log the data block's header.
931 : */
932 3920097 : if (needlog)
933 3920107 : xfs_dir2_data_log_header(args, dbp);
934 3919977 : 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 3920095 : if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
940 2386545 : bestsp[use_block] = bf[0].length;
941 2386545 : if (!grown)
942 2381143 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
943 : }
944 :
945 3920008 : 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 3920119 : lep->hashval = cpu_to_be32(args->hashval);
952 3920119 : 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 3920119 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
959 3920021 : xfs_dir3_leaf_log_header(args, lbp);
960 3920016 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
961 3919891 : xfs_dir3_leaf_check(dp, lbp);
962 3920102 : xfs_dir3_data_check(dp, dbp);
963 3920102 : 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 42231 : 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 42231 : int from; /* source leaf index */
977 42231 : xfs_dir2_leaf_t *leaf; /* leaf structure */
978 42231 : int loglow; /* first leaf entry to log */
979 42231 : int to; /* target leaf index */
980 42231 : struct xfs_inode *dp = args->dp;
981 :
982 42231 : leaf = bp->b_addr;
983 42231 : if (!leafhdr->stale)
984 : return;
985 :
986 : /*
987 : * Compress out the stale entries in place.
988 : */
989 14784389 : for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
990 14742158 : if (leafhdr->ents[from].address ==
991 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
992 6554571 : continue;
993 : /*
994 : * Only actually copy the entries that are different.
995 : */
996 8187587 : if (from > to) {
997 7589023 : if (loglow == -1)
998 41601 : loglow = to;
999 7589023 : leafhdr->ents[to] = leafhdr->ents[from];
1000 : }
1001 8187587 : to++;
1002 : }
1003 : /*
1004 : * Update and log the header, log the leaf entries.
1005 : */
1006 42231 : ASSERT(leafhdr->stale == from - to);
1007 42231 : leafhdr->count -= leafhdr->stale;
1008 42231 : leafhdr->stale = 0;
1009 :
1010 42231 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
1011 42231 : xfs_dir3_leaf_log_header(args, bp);
1012 42231 : if (loglow != -1)
1013 41601 : 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 3488 : 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 3488 : int from; /* source copy index */
1035 3488 : int highstale; /* stale entry at/after index */
1036 3488 : int index; /* insertion index */
1037 3488 : int keepstale; /* source index of kept stale */
1038 3488 : int lowstale; /* stale entry before index */
1039 3488 : int newindex=0; /* new insertion index */
1040 3488 : int to; /* destination copy index */
1041 :
1042 3488 : ASSERT(leafhdr->stale > 1);
1043 3488 : index = *indexp;
1044 :
1045 3488 : xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1046 :
1047 : /*
1048 : * Pick the better of lowstale and highstale.
1049 : */
1050 3488 : if (lowstale >= 0 &&
1051 3409 : (highstale == leafhdr->count ||
1052 2262 : index - lowstale <= highstale - index))
1053 : keepstale = lowstale;
1054 : else
1055 349 : keepstale = highstale;
1056 : /*
1057 : * Copy the entries in place, removing all the stale entries
1058 : * except keepstale.
1059 : */
1060 1762976 : for (from = to = 0; from < leafhdr->count; from++) {
1061 : /*
1062 : * Notice the new value of index.
1063 : */
1064 1759488 : if (index == from)
1065 3403 : newindex = to;
1066 1759488 : if (from != keepstale &&
1067 1756000 : ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1068 12154 : if (from == to)
1069 3488 : *lowlogp = to;
1070 12154 : continue;
1071 : }
1072 : /*
1073 : * Record the new keepstale value for the insertion.
1074 : */
1075 1747334 : if (from == keepstale)
1076 3488 : lowstale = highstale = to;
1077 : /*
1078 : * Copy only the entries that have moved.
1079 : */
1080 1747334 : if (from > to)
1081 1376953 : ents[to] = ents[from];
1082 1747334 : to++;
1083 : }
1084 3488 : ASSERT(from > to);
1085 : /*
1086 : * If the insertion point was past the last entry,
1087 : * set the new insertion point accordingly.
1088 : */
1089 3488 : if (index == from)
1090 85 : newindex = to;
1091 3488 : *indexp = newindex;
1092 : /*
1093 : * Adjust the leaf header values.
1094 : */
1095 3488 : leafhdr->count -= from - to;
1096 3488 : leafhdr->stale = 1;
1097 : /*
1098 : * Remember the low/high stale value only in the "right"
1099 : * direction.
1100 : */
1101 3488 : if (lowstale >= newindex)
1102 349 : lowstale = -1;
1103 : else
1104 3139 : highstale = leafhdr->count;
1105 3488 : *highlogp = leafhdr->count - 1;
1106 3488 : *lowstalep = lowstale;
1107 3488 : *highstalep = highstale;
1108 3488 : }
1109 :
1110 : /*
1111 : * Log the bests entries indicated from a leaf1 block.
1112 : */
1113 : static void
1114 3894482 : 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 3894482 : __be16 *firstb; /* pointer to first entry */
1121 3894482 : __be16 *lastb; /* pointer to last entry */
1122 3894482 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1123 3894482 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1124 :
1125 3894482 : 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 3894482 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1129 3894482 : firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1130 3894482 : lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1131 3894482 : xfs_trans_log_buf(args->trans, bp,
1132 3894482 : (uint)((char *)firstb - (char *)leaf),
1133 3894482 : (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1134 3894309 : }
1135 :
1136 : /*
1137 : * Log the leaf entries indicated from a leaf1 or leafn block.
1138 : */
1139 : void
1140 68603449 : 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 68603449 : xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1148 68603449 : xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1149 68603449 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1150 :
1151 68603449 : 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 68603449 : firstlep = &hdr->ents[first];
1157 68603449 : lastlep = &hdr->ents[last];
1158 68603449 : xfs_trans_log_buf(args->trans, bp,
1159 68603449 : (uint)((char *)firstlep - (char *)leaf),
1160 68603449 : (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1161 68609835 : }
1162 :
1163 : /*
1164 : * Log the header of the leaf1 or leafn block.
1165 : */
1166 : void
1167 68696811 : xfs_dir3_leaf_log_header(
1168 : struct xfs_da_args *args,
1169 : struct xfs_buf *bp)
1170 : {
1171 68696811 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1172 :
1173 68696811 : 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 68696811 : xfs_trans_log_buf(args->trans, bp,
1179 : (uint)((char *)&leaf->hdr - (char *)leaf),
1180 68696811 : args->geo->leaf_hdr_size - 1);
1181 68695201 : }
1182 :
1183 : /*
1184 : * Log the tail of the leaf1 block.
1185 : */
1186 : STATIC void
1187 14177 : xfs_dir3_leaf_log_tail(
1188 : struct xfs_da_args *args,
1189 : struct xfs_buf *bp)
1190 : {
1191 14177 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1192 14177 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1193 :
1194 14177 : 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 14177 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1200 14177 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1201 : (uint)(args->geo->blksize - 1));
1202 14177 : }
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 10180955 : xfs_dir2_leaf_lookup(
1211 : xfs_da_args_t *args) /* operation arguments */
1212 : {
1213 10180955 : struct xfs_buf *dbp; /* data block buffer */
1214 10180955 : xfs_dir2_data_entry_t *dep; /* data block entry */
1215 10180955 : xfs_inode_t *dp; /* incore directory inode */
1216 10180955 : int error; /* error return code */
1217 10180955 : int index; /* found entry index */
1218 10180955 : struct xfs_buf *lbp; /* leaf buffer */
1219 10180955 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1220 10180955 : xfs_trans_t *tp; /* transaction pointer */
1221 10180955 : struct xfs_dir3_icleaf_hdr leafhdr;
1222 :
1223 10180955 : trace_xfs_dir2_leaf_lookup(args);
1224 :
1225 : /*
1226 : * Look up name in the leaf block, returning both buffers and index.
1227 : */
1228 10181572 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1229 10181368 : if (error)
1230 : return error;
1231 :
1232 1731030 : tp = args->trans;
1233 1731030 : dp = args->dp;
1234 1731030 : xfs_dir3_leaf_check(dp, lbp);
1235 :
1236 : /*
1237 : * Get to the leaf entry and contained data entry address.
1238 : */
1239 1731034 : lep = &leafhdr.ents[index];
1240 :
1241 : /*
1242 : * Point to the data entry.
1243 : */
1244 3462068 : dep = (xfs_dir2_data_entry_t *)
1245 1731034 : ((char *)dbp->b_addr +
1246 1731034 : 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 1731034 : args->inumber = be64_to_cpu(dep->inumber);
1251 1731034 : args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
1252 1731034 : error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1253 1731038 : xfs_trans_brelse(tp, dbp);
1254 1731043 : xfs_trans_brelse(tp, lbp);
1255 1731043 : 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 14184736 : 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 14184736 : xfs_dir2_db_t curdb = -1; /* current data block number */
1273 14184736 : struct xfs_buf *dbp = NULL; /* data buffer */
1274 14184736 : xfs_dir2_data_entry_t *dep; /* data entry */
1275 14184736 : xfs_inode_t *dp; /* incore directory inode */
1276 14184736 : int error; /* error return code */
1277 14184736 : int index; /* index in leaf block */
1278 14184736 : struct xfs_buf *lbp; /* leaf buffer */
1279 14184736 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1280 14184736 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1281 14184736 : xfs_mount_t *mp; /* filesystem mount point */
1282 14184736 : xfs_dir2_db_t newdb; /* new data block number */
1283 14184736 : xfs_trans_t *tp; /* transaction pointer */
1284 14184736 : xfs_dir2_db_t cidb = -1; /* case match data block no. */
1285 14184736 : enum xfs_dacmp cmp; /* name compare result */
1286 :
1287 14184736 : dp = args->dp;
1288 14184736 : tp = args->trans;
1289 14184736 : mp = dp->i_mount;
1290 :
1291 14184736 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
1292 : &lbp);
1293 14186388 : if (error)
1294 : return error;
1295 :
1296 14185910 : *lbpp = lbp;
1297 14185910 : leaf = lbp->b_addr;
1298 14185910 : xfs_dir3_leaf_check(dp, lbp);
1299 14185633 : xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1300 :
1301 : /*
1302 : * Look for the first leaf entry with our hash value.
1303 : */
1304 14185563 : 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 14185809 : for (lep = &leafhdr->ents[index];
1310 43598856 : index < leafhdr->count &&
1311 83967668 : be32_to_cpu(lep->hashval) == args->hashval;
1312 29413212 : lep++, index++) {
1313 : /*
1314 : * Skip over stale leaf entries.
1315 : */
1316 35125743 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1317 3749127 : continue;
1318 : /*
1319 : * Get the new data block number.
1320 : */
1321 31376616 : 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 31376616 : if (newdb != curdb) {
1328 6023278 : if (dbp)
1329 259767 : xfs_trans_brelse(tp, dbp);
1330 6023278 : error = xfs_dir3_data_read(tp, dp, args->owner,
1331 : xfs_dir2_db_to_da(args->geo, newdb), 0,
1332 : &dbp);
1333 6023421 : if (error) {
1334 182 : xfs_trans_brelse(tp, lbp);
1335 182 : return error;
1336 : }
1337 : curdb = newdb;
1338 : }
1339 : /*
1340 : * Point to the data entry.
1341 : */
1342 62753154 : dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1343 62753154 : xfs_dir2_dataptr_to_off(args->geo,
1344 31376577 : 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 31376577 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
1351 31376106 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1352 5735759 : args->cmpresult = cmp;
1353 5735759 : *indexp = index;
1354 : /* case exact match: return the current buffer. */
1355 5735759 : if (cmp == XFS_CMP_EXACT) {
1356 5712186 : *dbpp = dbp;
1357 5712186 : return 0;
1358 : }
1359 : cidb = curdb;
1360 : }
1361 : }
1362 8473113 : 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 8473113 : 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 8449425 : ASSERT(cidb == -1);
1387 8449425 : if (dbp)
1388 27416 : xfs_trans_brelse(tp, dbp);
1389 8449425 : xfs_trans_brelse(tp, lbp);
1390 8449425 : return -ENOENT;
1391 : }
1392 :
1393 : /*
1394 : * Remove an entry from a leaf format directory.
1395 : */
1396 : int /* error */
1397 3804019 : xfs_dir2_leaf_removename(
1398 : xfs_da_args_t *args) /* operation arguments */
1399 : {
1400 3804019 : struct xfs_da_geometry *geo = args->geo;
1401 3804019 : __be16 *bestsp; /* leaf block best freespace */
1402 3804019 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1403 3804019 : xfs_dir2_db_t db; /* data block number */
1404 3804019 : struct xfs_buf *dbp; /* data block buffer */
1405 3804019 : xfs_dir2_data_entry_t *dep; /* data entry structure */
1406 3804019 : xfs_inode_t *dp; /* incore directory inode */
1407 3804019 : int error; /* error return code */
1408 3804019 : xfs_dir2_db_t i; /* temporary data block # */
1409 3804019 : int index; /* index into leaf entries */
1410 3804019 : struct xfs_buf *lbp; /* leaf buffer */
1411 3804019 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1412 3804019 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1413 3804019 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1414 3804019 : int needlog; /* need to log data header */
1415 3804019 : int needscan; /* need to rescan data frees */
1416 3804019 : xfs_dir2_data_off_t oldbest; /* old value of best free */
1417 3804019 : struct xfs_dir2_data_free *bf; /* bestfree table */
1418 3804019 : struct xfs_dir3_icleaf_hdr leafhdr;
1419 :
1420 3804019 : trace_xfs_dir2_leaf_removename(args);
1421 :
1422 : /*
1423 : * Lookup the leaf entry, get the leaf and data blocks read in.
1424 : */
1425 3804035 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1426 3804030 : if (error)
1427 : return error;
1428 :
1429 3804034 : dp = args->dp;
1430 3804034 : leaf = lbp->b_addr;
1431 3804034 : hdr = dbp->b_addr;
1432 3804034 : xfs_dir3_data_check(dp, dbp);
1433 3803719 : 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 3804010 : lep = &leafhdr.ents[index];
1439 3804010 : db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1440 3804010 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1441 3804010 : xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
1442 3804010 : needscan = needlog = 0;
1443 3804010 : oldbest = be16_to_cpu(bf[0].length);
1444 3804010 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1445 3804010 : bestsp = xfs_dir2_leaf_bests_p(ltp);
1446 3804010 : 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 3804010 : xfs_dir2_data_make_free(args, dbp,
1456 : (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1457 3804010 : 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 3804013 : leafhdr.stale++;
1463 3804013 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1464 3803994 : xfs_dir3_leaf_log_header(args, lbp);
1465 :
1466 3803971 : lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1467 3803971 : 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 3804007 : if (needscan)
1474 513737 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1475 3804008 : if (needlog)
1476 2559049 : 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 3804001 : if (be16_to_cpu(bf[0].length) != oldbest) {
1482 1499168 : bestsp[db] = bf[0].length;
1483 1499168 : xfs_dir3_leaf_log_bests(args, lbp, db, db);
1484 : }
1485 3803996 : xfs_dir3_data_check(dp, dbp);
1486 : /*
1487 : * If the data block is now empty then get rid of the data block.
1488 : */
1489 3804035 : if (be16_to_cpu(bf[0].length) ==
1490 3804035 : geo->blksize - geo->data_entry_offset) {
1491 5892 : ASSERT(db != geo->datablk);
1492 5892 : 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 5892 : dbp = NULL;
1505 : /*
1506 : * If this is the last data block then compact the
1507 : * bests table by getting rid of entries.
1508 : */
1509 5892 : if (db == be32_to_cpu(ltp->bestcount) - 1) {
1510 : /*
1511 : * Look for the last active entry (i).
1512 : */
1513 11674 : for (i = db - 1; i > 0; i--) {
1514 9694 : 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 6456 : memmove(&bestsp[db - i], bestsp,
1522 : (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1523 2152 : be32_add_cpu(<p->bestcount, -(db - i));
1524 2152 : xfs_dir3_leaf_log_tail(args, lbp);
1525 2152 : xfs_dir3_leaf_log_bests(args, lbp, 0,
1526 2152 : be32_to_cpu(ltp->bestcount) - 1);
1527 : } else
1528 3740 : bestsp[db] = cpu_to_be16(NULLDATAOFF);
1529 : }
1530 : /*
1531 : * If the data block was not the first one, drop it.
1532 : */
1533 3798143 : else if (db != geo->datablk)
1534 1763156 : dbp = NULL;
1535 :
1536 3804035 : xfs_dir3_leaf_check(dp, lbp);
1537 : /*
1538 : * See if we can convert to block form.
1539 : */
1540 3804013 : 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 200877 : xfs_dir2_leaf_replace(
1548 : xfs_da_args_t *args) /* operation arguments */
1549 : {
1550 200877 : struct xfs_buf *dbp; /* data block buffer */
1551 200877 : xfs_dir2_data_entry_t *dep; /* data block entry */
1552 200877 : xfs_inode_t *dp; /* incore directory inode */
1553 200877 : int error; /* error return code */
1554 200877 : int index; /* index of leaf entry */
1555 200877 : struct xfs_buf *lbp; /* leaf buffer */
1556 200877 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1557 200877 : xfs_trans_t *tp; /* transaction pointer */
1558 200877 : struct xfs_dir3_icleaf_hdr leafhdr;
1559 :
1560 200877 : trace_xfs_dir2_leaf_replace(args);
1561 :
1562 : /*
1563 : * Look up the entry.
1564 : */
1565 200877 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1566 200877 : if (error)
1567 : return error;
1568 :
1569 200867 : dp = args->dp;
1570 : /*
1571 : * Point to the leaf entry, get data address from it.
1572 : */
1573 200867 : lep = &leafhdr.ents[index];
1574 : /*
1575 : * Point to the data entry.
1576 : */
1577 401734 : dep = (xfs_dir2_data_entry_t *)
1578 200867 : ((char *)dbp->b_addr +
1579 200867 : xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1580 200867 : ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1581 : /*
1582 : * Put the new inode number in, log it.
1583 : */
1584 200867 : dep->inumber = cpu_to_be64(args->inumber);
1585 200867 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1586 200866 : tp = args->trans;
1587 200866 : xfs_dir2_data_log_entry(args, dbp, dep);
1588 200866 : xfs_dir3_leaf_check(dp, lbp);
1589 200867 : xfs_trans_brelse(tp, lbp);
1590 200867 : 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 153720761 : xfs_dir2_leaf_search_hash(
1600 : xfs_da_args_t *args, /* operation arguments */
1601 : struct xfs_buf *lbp) /* leaf buffer */
1602 : {
1603 153720761 : xfs_dahash_t hash=0; /* hash from this entry */
1604 153720761 : xfs_dahash_t hashwant; /* hash value looking for */
1605 153720761 : int high; /* high leaf index */
1606 153720761 : int low; /* low leaf index */
1607 153720761 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1608 153720761 : int mid=0; /* current leaf index */
1609 153720761 : struct xfs_dir3_icleaf_hdr leafhdr;
1610 :
1611 153720761 : 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 153729901 : for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
1618 153729901 : hashwant = args->hashval;
1619 1262695782 : low <= high; ) {
1620 1207809272 : mid = (low + high) >> 1;
1621 2415618544 : if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1622 : break;
1623 1108965881 : if (hash < hashwant)
1624 625944449 : low = mid + 1;
1625 : else
1626 483021432 : high = mid - 1;
1627 : }
1628 : /*
1629 : * Found one, back up through all the equal hash values.
1630 : */
1631 153729901 : if (hash == hashwant) {
1632 471656886 : 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 54886589 : else if (hash < hashwant)
1640 25089449 : mid++;
1641 153729901 : 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 1325 : xfs_dir3_leaf_size(
1711 : struct xfs_dir3_icleaf_hdr *hdr,
1712 : int counts)
1713 : {
1714 1325 : int entries;
1715 1325 : int hdrsize;
1716 :
1717 1325 : entries = hdr->count - hdr->stale;
1718 1325 : 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 1325 : hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1723 :
1724 1325 : return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1725 1325 : + counts * sizeof(xfs_dir2_data_off_t)
1726 1325 : + 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 28709906 : xfs_dir2_node_to_leaf(
1736 : xfs_da_state_t *state) /* directory operation state */
1737 : {
1738 28709906 : xfs_da_args_t *args; /* operation arguments */
1739 28709906 : xfs_inode_t *dp; /* incore directory inode */
1740 28709906 : int error; /* error return code */
1741 28709906 : struct xfs_buf *fbp; /* buffer for freespace block */
1742 28709906 : xfs_fileoff_t fo; /* freespace file offset */
1743 28709906 : struct xfs_buf *lbp; /* buffer for leaf block */
1744 28709906 : xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1745 28709906 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1746 28709906 : xfs_mount_t *mp; /* filesystem mount point */
1747 28709906 : int rval; /* successful free trim? */
1748 28709906 : xfs_trans_t *tp; /* transaction pointer */
1749 28709906 : struct xfs_dir3_icleaf_hdr leafhdr;
1750 28709906 : 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 28709906 : if (state->path.active > 1)
1757 : return 0;
1758 22457 : args = state->args;
1759 :
1760 22457 : trace_xfs_dir2_node_to_leaf(args);
1761 :
1762 22457 : mp = state->mp;
1763 22457 : dp = args->dp;
1764 22457 : tp = args->trans;
1765 : /*
1766 : * Get the last offset in the file.
1767 : */
1768 22457 : if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1769 : return error;
1770 : }
1771 22457 : 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 22457 : while (fo > args->geo->freeblk) {
1779 2213 : if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1780 0 : return error;
1781 : }
1782 2213 : 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 20244 : 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 20244 : if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1797 : return 0;
1798 1325 : lbp = state->path.blk[0].bp;
1799 1325 : leaf = lbp->b_addr;
1800 1325 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1801 :
1802 1325 : ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1803 : leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1804 :
1805 : /*
1806 : * Read the freespace block.
1807 : */
1808 1325 : error = xfs_dir2_free_read(tp, dp, args->owner, args->geo->freeblk,
1809 : &fbp);
1810 1325 : if (error)
1811 : return error;
1812 1325 : xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
1813 :
1814 1325 : 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 1325 : if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1821 109 : xfs_trans_brelse(tp, fbp);
1822 109 : return 0;
1823 : }
1824 :
1825 : /*
1826 : * If the leaf has any stale entries in it, compress them out.
1827 : */
1828 1216 : if (leafhdr.stale)
1829 127 : xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1830 :
1831 1216 : lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1832 1216 : xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1833 1216 : 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 1216 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1841 1216 : ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1842 :
1843 : /*
1844 : * Set up the leaf bests table.
1845 : */
1846 1216 : memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
1847 : freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1848 :
1849 1216 : xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1850 1216 : xfs_dir3_leaf_log_header(args, lbp);
1851 1216 : xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1852 1216 : xfs_dir3_leaf_log_tail(args, lbp);
1853 1216 : xfs_dir3_leaf_check(dp, lbp);
1854 :
1855 : /*
1856 : * Get rid of the freespace block.
1857 : */
1858 1216 : error = xfs_dir2_shrink_inode(args,
1859 : xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1860 : fbp);
1861 1216 : 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 1216 : 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 1216 : error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1878 1216 : state->path.blk[0].bp = NULL;
1879 1216 : return error;
1880 : }
|