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 1447120213 : 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 1447120213 : if (xfs_has_crc(mp)) {
42 1447120213 : struct xfs_dir3_leaf *from3 = (struct xfs_dir3_leaf *)from;
43 :
44 1447120213 : to->forw = be32_to_cpu(from3->hdr.info.hdr.forw);
45 1447120213 : to->back = be32_to_cpu(from3->hdr.info.hdr.back);
46 1447120213 : to->magic = be16_to_cpu(from3->hdr.info.hdr.magic);
47 1447120213 : to->count = be16_to_cpu(from3->hdr.count);
48 1447120213 : to->stale = be16_to_cpu(from3->hdr.stale);
49 1447120213 : to->ents = from3->__ents;
50 :
51 1447120213 : 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 1447120213 : }
65 :
66 : void
67 114512277 : 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 114512277 : if (xfs_has_crc(mp)) {
73 114512277 : struct xfs_dir3_leaf *to3 = (struct xfs_dir3_leaf *)to;
74 :
75 114512277 : ASSERT(from->magic == XFS_DIR3_LEAF1_MAGIC ||
76 : from->magic == XFS_DIR3_LEAFN_MAGIC);
77 :
78 114512277 : to3->hdr.info.hdr.forw = cpu_to_be32(from->forw);
79 114512277 : to3->hdr.info.hdr.back = cpu_to_be32(from->back);
80 114512277 : to3->hdr.info.hdr.magic = cpu_to_be16(from->magic);
81 114512277 : to3->hdr.count = cpu_to_be16(from->count);
82 114512277 : 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 114512277 : }
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 72723707 : xfs_dir3_leaf1_check(
102 : struct xfs_inode *dp,
103 : struct xfs_buf *bp)
104 : {
105 72723707 : struct xfs_dir2_leaf *leaf = bp->b_addr;
106 72723707 : struct xfs_dir3_icleaf_hdr leafhdr;
107 :
108 72723707 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
109 :
110 72710425 : if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
111 72710425 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
112 72710425 : 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 72710425 : return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
118 : }
119 :
120 : static inline void
121 72775989 : xfs_dir3_leaf_check(
122 : struct xfs_inode *dp,
123 : struct xfs_buf *bp)
124 : {
125 72775989 : xfs_failaddr_t fa;
126 :
127 72775989 : fa = xfs_dir3_leaf1_check(dp, bp);
128 72766478 : 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 392364710 : 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 392364710 : struct xfs_da_geometry *geo = mp->m_dir_geo;
147 392364710 : xfs_dir2_leaf_tail_t *ltp;
148 392364710 : int stale;
149 392364710 : int i;
150 392364710 : bool isleaf1 = (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
151 : hdr->magic == XFS_DIR3_LEAF1_MAGIC);
152 :
153 392364710 : 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 392364710 : if (hdr->count > geo->leaf_max_ents)
161 0 : return __this_address;
162 :
163 : /* Leaves and bests don't overlap in leaf format. */
164 392364710 : if (isleaf1 &&
165 72943600 : (char *)&hdr->ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 0 : return __this_address;
167 :
168 392364710 : if (!expensive_checking)
169 : return NULL;
170 :
171 : /* Check hash value order, count stale entries. */
172 731361258 : for (i = stale = 0; i < hdr->count; i++) {
173 729224738 : if (i + 1 < hdr->count) {
174 727022928 : if (be32_to_cpu(hdr->ents[i].hashval) >
175 727022928 : be32_to_cpu(hdr->ents[i + 1].hashval))
176 0 : return __this_address;
177 : }
178 729224738 : if (hdr->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
179 60048167 : stale++;
180 729224738 : if (isleaf1 && xfs_dir2_dataptr_to_db(geo,
181 : be32_to_cpu(hdr->ents[i].address)) >=
182 50657436 : be32_to_cpu(ltp->bestcount))
183 0 : return __this_address;
184 : }
185 2136520 : 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 2136492 : xfs_dir3_leaf_verify(
197 : struct xfs_buf *bp)
198 : {
199 2136492 : struct xfs_mount *mp = bp->b_mount;
200 2136492 : struct xfs_dir3_icleaf_hdr leafhdr;
201 2136492 : xfs_failaddr_t fa;
202 :
203 2136492 : fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
204 2136494 : if (fa)
205 : return fa;
206 :
207 2136494 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, bp->b_addr);
208 2136493 : return xfs_dir3_leaf_check_int(mp, &leafhdr, bp->b_addr, true);
209 : }
210 :
211 : xfs_failaddr_t
212 283069840 : xfs_dir3_leaf_header_check(
213 : struct xfs_buf *bp,
214 : xfs_ino_t owner)
215 : {
216 283069840 : struct xfs_mount *mp = bp->b_mount;
217 :
218 283069840 : if (xfs_has_crc(mp)) {
219 283069840 : struct xfs_dir3_leaf *hdr3 = bp->b_addr;
220 :
221 283069840 : 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 283069840 : 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 141816 : xfs_dir3_leaf_read_verify(
235 : struct xfs_buf *bp)
236 : {
237 141816 : struct xfs_mount *mp = bp->b_mount;
238 141816 : xfs_failaddr_t fa;
239 :
240 283632 : if (xfs_has_crc(mp) &&
241 : !xfs_buf_verify_cksum(bp, XFS_DIR3_LEAF_CRC_OFF))
242 30 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
243 : else {
244 141786 : fa = xfs_dir3_leaf_verify(bp);
245 141786 : if (fa)
246 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
247 : }
248 141816 : }
249 :
250 : static void
251 1323422 : xfs_dir3_leaf_write_verify(
252 : struct xfs_buf *bp)
253 : {
254 1323422 : struct xfs_mount *mp = bp->b_mount;
255 1323422 : struct xfs_buf_log_item *bip = bp->b_log_item;
256 1323422 : struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
257 1323422 : xfs_failaddr_t fa;
258 :
259 1323422 : fa = xfs_dir3_leaf_verify(bp);
260 1323422 : if (fa) {
261 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
262 0 : return;
263 : }
264 :
265 1323422 : if (!xfs_has_crc(mp))
266 : return;
267 :
268 1323422 : if (bip)
269 1323422 : hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
270 :
271 1323422 : 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 54321564 : 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 54321564 : xfs_failaddr_t fa;
301 54321564 : int err;
302 :
303 54321564 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
304 : &xfs_dir3_leaf1_buf_ops);
305 54345260 : if (err || !(*bpp))
306 : return err;
307 :
308 54344999 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
309 54319432 : 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 54319432 : if (tp)
318 30926757 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
319 : return 0;
320 : }
321 :
322 : int
323 3663082 : 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 3663082 : xfs_failaddr_t fa;
331 3663082 : int err;
332 :
333 3663082 : err = xfs_da_read_buf(tp, dp, fbno, 0, bpp, XFS_DATA_FORK,
334 : &xfs_dir3_leafn_buf_ops);
335 3663163 : if (err || !(*bpp))
336 : return err;
337 :
338 3663163 : fa = xfs_dir3_leaf_header_check(*bpp, owner);
339 3663164 : 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 3663164 : if (tp)
348 3663164 : 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 115341 : xfs_dir3_leaf_init(
357 : struct xfs_da_args *args,
358 : struct xfs_buf *bp,
359 : uint16_t type)
360 : {
361 115341 : struct xfs_mount *mp = args->dp->i_mount;
362 115341 : struct xfs_trans *tp = args->trans;
363 115341 : struct xfs_dir2_leaf *leaf = bp->b_addr;
364 :
365 115341 : ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
366 :
367 115341 : if (xfs_has_crc(mp)) {
368 115341 : struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
369 :
370 115341 : memset(leaf3, 0, sizeof(*leaf3));
371 :
372 115341 : 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 115341 : leaf3->info.blkno = cpu_to_be64(xfs_buf_daddr(bp));
376 115341 : leaf3->info.owner = cpu_to_be64(args->owner);
377 115341 : 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 115340 : if (type == XFS_DIR2_LEAF1_MAGIC) {
388 29767 : struct xfs_dir2_leaf_tail *ltp;
389 :
390 29767 : ltp = xfs_dir2_leaf_tail_p(mp->m_dir_geo, leaf);
391 29767 : ltp->bestcount = 0;
392 29767 : bp->b_ops = &xfs_dir3_leaf1_buf_ops;
393 29767 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
394 : } else {
395 85573 : bp->b_ops = &xfs_dir3_leafn_buf_ops;
396 85573 : xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
397 : }
398 115342 : }
399 :
400 : int
401 115340 : 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 115340 : struct xfs_inode *dp = args->dp;
408 115340 : struct xfs_trans *tp = args->trans;
409 115340 : struct xfs_buf *bp;
410 115340 : int error;
411 :
412 115340 : ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
413 115340 : 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 115339 : error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, bno),
417 : &bp, XFS_DATA_FORK);
418 115342 : if (error)
419 : return error;
420 :
421 115341 : xfs_dir3_leaf_init(args, bp, magic);
422 115343 : xfs_dir3_leaf_log_header(args, bp);
423 115345 : if (magic == XFS_DIR2_LEAF1_MAGIC)
424 29768 : xfs_dir3_leaf_log_tail(args, bp);
425 115346 : *bpp = bp;
426 115346 : return 0;
427 : }
428 :
429 : /*
430 : * Convert a block form directory to a leaf form directory.
431 : */
432 : int /* error */
433 29769 : xfs_dir2_block_to_leaf(
434 : xfs_da_args_t *args, /* operation arguments */
435 : struct xfs_buf *dbp) /* input block's buffer */
436 : {
437 29769 : __be16 *bestsp; /* leaf's bestsp entries */
438 29769 : xfs_dablk_t blkno; /* leaf block's bno */
439 29769 : xfs_dir2_data_hdr_t *hdr; /* block header */
440 29769 : xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
441 29769 : xfs_dir2_block_tail_t *btp; /* block's tail */
442 29769 : xfs_inode_t *dp; /* incore directory inode */
443 29769 : int error; /* error return code */
444 29769 : struct xfs_buf *lbp; /* leaf block's buffer */
445 29769 : xfs_dir2_db_t ldb; /* leaf block's bno */
446 29769 : xfs_dir2_leaf_t *leaf; /* leaf structure */
447 29769 : xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
448 29769 : int needlog; /* need to log block header */
449 29769 : int needscan; /* need to rescan bestfree */
450 29769 : xfs_trans_t *tp; /* transaction pointer */
451 29769 : struct xfs_dir2_data_free *bf;
452 29769 : struct xfs_dir3_icleaf_hdr leafhdr;
453 :
454 29769 : trace_xfs_dir2_block_to_leaf(args);
455 :
456 29769 : dp = args->dp;
457 29769 : 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 29769 : if ((error = xfs_da_grow_inode(args, &blkno))) {
464 : return error;
465 : }
466 29768 : ldb = xfs_dir2_da_to_db(args->geo, blkno);
467 29768 : 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 29768 : error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
472 29769 : if (error)
473 : return error;
474 :
475 29769 : leaf = lbp->b_addr;
476 29769 : hdr = dbp->b_addr;
477 29769 : xfs_dir3_data_check(dp, dbp);
478 29769 : btp = xfs_dir2_block_tail_p(args->geo, hdr);
479 29769 : blp = xfs_dir2_block_leaf_p(btp);
480 29769 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
481 :
482 : /*
483 : * Set the counts in the leaf header.
484 : */
485 29769 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
486 29769 : leafhdr.count = be32_to_cpu(btp->count);
487 29769 : leafhdr.stale = be32_to_cpu(btp->stale);
488 29769 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
489 29769 : 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 59538 : memcpy(leafhdr.ents, blp,
496 : be32_to_cpu(btp->count) * sizeof(struct xfs_dir2_leaf_entry));
497 29769 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, 0, leafhdr.count - 1);
498 29769 : needscan = 0;
499 29769 : needlog = 1;
500 : /*
501 : * Make the space formerly occupied by the leaf entries and block
502 : * tail be free.
503 : */
504 29769 : xfs_dir2_data_make_free(args, dbp,
505 29769 : (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
506 29769 : (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 29768 : dbp->b_ops = &xfs_dir3_data_buf_ops;
513 29768 : xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
514 29767 : if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
515 0 : hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
516 : else
517 29767 : hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
518 :
519 29767 : if (needscan)
520 3 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
521 : /*
522 : * Set up leaf tail and bests table.
523 : */
524 29767 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
525 29767 : ltp->bestcount = cpu_to_be32(1);
526 29767 : bestsp = xfs_dir2_leaf_bests_p(ltp);
527 29767 : bestsp[0] = bf[0].length;
528 : /*
529 : * Log the data header and leaf bests table.
530 : */
531 29767 : if (needlog)
532 29767 : xfs_dir2_data_log_header(args, dbp);
533 29768 : xfs_dir3_leaf_check(dp, lbp);
534 29766 : xfs_dir3_data_check(dp, dbp);
535 29769 : xfs_dir3_leaf_log_bests(args, lbp, 0, 0);
536 29769 : return 0;
537 : }
538 :
539 : STATIC void
540 29367340 : 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 2457000405 : for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
551 2455021286 : 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 1078812292 : for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
562 1066782632 : if (ents[*highstale].address ==
563 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
564 : break;
565 1061529215 : if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
566 : break;
567 : }
568 29367340 : }
569 :
570 : struct xfs_dir2_leaf_entry *
571 60925316 : 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 60925316 : if (!leafhdr->stale) {
582 31579819 : 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 31579819 : lep = &ents[index];
590 31579819 : if (index < leafhdr->count)
591 59705592 : memmove(lep + 1, lep,
592 : (leafhdr->count - index) * sizeof(*lep));
593 :
594 : /*
595 : * Record low and high logging indices for the leaf.
596 : */
597 31579819 : *lfloglow = index;
598 31579819 : *lfloghigh = leafhdr->count++;
599 31579819 : 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 29345497 : if (compact == 0)
612 29342639 : xfs_dir3_leaf_find_stale(leafhdr, ents, index,
613 : &lowstale, &highstale);
614 :
615 : /*
616 : * If the low one is better, use it.
617 : */
618 29365597 : if (lowstale >= 0 &&
619 27389959 : (highstale == leafhdr->count ||
620 15359923 : index - lowstale - 1 < highstale - index)) {
621 24280201 : ASSERT(index - lowstale - 1 >= 0);
622 24280201 : 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 24280201 : if (index - lowstale - 1 > 0) {
630 45711496 : memmove(&ents[lowstale], &ents[lowstale + 1],
631 : (index - lowstale - 1) *
632 : sizeof(xfs_dir2_leaf_entry_t));
633 : }
634 24280201 : *lfloglow = min(lowstale, *lfloglow);
635 24280201 : *lfloghigh = max(index - 1, *lfloghigh);
636 24280201 : leafhdr->stale--;
637 24280201 : return &ents[index - 1];
638 : }
639 :
640 : /*
641 : * The high one is better, so use that one.
642 : */
643 5085396 : ASSERT(highstale - index >= 0);
644 5085396 : 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 5085396 : if (highstale - index > 0) {
651 7476498 : memmove(&ents[index + 1], &ents[index],
652 : (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
653 : }
654 5085396 : *lfloglow = min(index, *lfloglow);
655 5085396 : *lfloghigh = max(highstale, *lfloghigh);
656 5085396 : leafhdr->stale--;
657 5085396 : return &ents[index];
658 : }
659 :
660 : /*
661 : * Add an entry to a leaf form directory.
662 : */
663 : int /* error */
664 12511726 : xfs_dir2_leaf_addname(
665 : struct xfs_da_args *args) /* operation arguments */
666 : {
667 12511726 : struct xfs_dir3_icleaf_hdr leafhdr;
668 12511726 : struct xfs_trans *tp = args->trans;
669 12511726 : __be16 *bestsp; /* freespace table in leaf */
670 12511726 : __be16 *tagp; /* end of data entry */
671 12511726 : struct xfs_buf *dbp; /* data block buffer */
672 12511726 : struct xfs_buf *lbp; /* leaf's buffer */
673 12511726 : struct xfs_dir2_leaf *leaf; /* leaf structure */
674 12511726 : struct xfs_inode *dp = args->dp; /* incore directory inode */
675 12511726 : struct xfs_dir2_data_hdr *hdr; /* data block header */
676 12511726 : struct xfs_dir2_data_entry *dep; /* data block entry */
677 12511726 : struct xfs_dir2_leaf_entry *lep; /* leaf entry table pointer */
678 12511726 : struct xfs_dir2_leaf_entry *ents;
679 12511726 : struct xfs_dir2_data_unused *dup; /* data unused entry */
680 12511726 : struct xfs_dir2_leaf_tail *ltp; /* leaf tail pointer */
681 12511726 : struct xfs_dir2_data_free *bf; /* bestfree table */
682 12511726 : int compact; /* need to compact leaves */
683 12511726 : int error; /* error return value */
684 12511726 : int grown; /* allocated new data block */
685 12511726 : int highstale = 0; /* index of next stale leaf */
686 12511726 : int i; /* temporary, index */
687 12511726 : int index; /* leaf table position */
688 12511726 : int length; /* length of new entry */
689 12511726 : int lfloglow; /* low leaf logging index */
690 12511726 : int lfloghigh; /* high leaf logging index */
691 12511726 : int lowstale = 0; /* index of prev stale leaf */
692 12511726 : int needbytes; /* leaf block bytes needed */
693 12511726 : int needlog; /* need to log data header */
694 12511726 : int needscan; /* need to rescan data free */
695 12511726 : xfs_dir2_db_t use_block; /* data block number */
696 :
697 12511726 : trace_xfs_dir2_leaf_addname(args);
698 :
699 12499671 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
700 : &lbp);
701 12508995 : 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 12509053 : index = xfs_dir2_leaf_search_hash(args, lbp);
711 12516403 : leaf = lbp->b_addr;
712 12516403 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
713 12516403 : xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
714 12514620 : ents = leafhdr.ents;
715 12514620 : bestsp = xfs_dir2_leaf_bests_p(ltp);
716 12514620 : 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 12514620 : for (use_block = -1, lep = &ents[index];
725 21234562 : index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
726 8723580 : index++, lep++) {
727 8793568 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
728 96900 : continue;
729 8696668 : i = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
730 8674794 : ASSERT(i < be32_to_cpu(ltp->bestcount));
731 8674794 : ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
732 8674794 : if (be16_to_cpu(bestsp[i]) >= length) {
733 47893 : use_block = i;
734 47893 : break;
735 : }
736 : }
737 : /*
738 : * Didn't find a block yet, linear search all the data blocks.
739 : */
740 12488887 : if (use_block == -1) {
741 25264343 : for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
742 : /*
743 : * Remember a block we see that's missing.
744 : */
745 25227854 : if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
746 11980 : use_block == -1)
747 11975 : use_block = i;
748 25215879 : else if (be16_to_cpu(bestsp[i]) >= length) {
749 12409743 : use_block = i;
750 12409743 : break;
751 : }
752 : }
753 : }
754 : /*
755 : * How many bytes do we need in the leaf block?
756 : */
757 12488887 : needbytes = 0;
758 12488887 : if (!leafhdr.stale)
759 5830794 : needbytes += sizeof(xfs_dir2_leaf_entry_t);
760 12488887 : if (use_block == -1)
761 36487 : 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 12488887 : if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
768 7 : 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 12488887 : 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 12488887 : else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
782 : /*
783 : * Just checking or no space reservation, give up.
784 : */
785 10215 : if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
786 10215 : args->total == 0) {
787 0 : xfs_trans_brelse(tp, lbp);
788 0 : return -ENOSPC;
789 : }
790 : /*
791 : * Convert to node form.
792 : */
793 10215 : error = xfs_dir2_leaf_to_node(args, lbp);
794 10214 : if (error)
795 : return error;
796 : /*
797 : * Then add the new entry.
798 : */
799 10214 : 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 12478672 : if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
811 22 : xfs_trans_brelse(tp, lbp);
812 22 : return use_block == -1 ? -ENOSPC : 0;
813 : }
814 : /*
815 : * If no allocations are allowed, return now before we've
816 : * changed anything.
817 : */
818 12478650 : 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 12478650 : 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 12478650 : else if (leafhdr.stale) {
837 6662775 : lfloglow = leafhdr.count;
838 6662775 : lfloghigh = -1;
839 : }
840 : /*
841 : * If there was no data block space found, we need to allocate
842 : * a new one.
843 : */
844 12478650 : if (use_block == -1) {
845 : /*
846 : * Add the new data block.
847 : */
848 36404 : 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 36404 : 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 36400 : if (use_block >= be32_to_cpu(ltp->bestcount)) {
865 36393 : bestsp--;
866 72786 : memmove(&bestsp[0], &bestsp[1],
867 : be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
868 36393 : be32_add_cpu(<p->bestcount, 1);
869 36393 : xfs_dir3_leaf_log_tail(args, lbp);
870 36396 : xfs_dir3_leaf_log_bests(args, lbp, 0,
871 36396 : 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 7 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
878 36402 : hdr = dbp->b_addr;
879 36402 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
880 36401 : bestsp[use_block] = bf[0].length;
881 36401 : grown = 1;
882 : } else {
883 : /*
884 : * Already had space in some data block.
885 : * Just read that one in.
886 : */
887 12442246 : error = xfs_dir3_data_read(tp, dp, args->owner,
888 : xfs_dir2_db_to_da(args->geo, use_block), 0,
889 : &dbp);
890 12469789 : if (error) {
891 44 : xfs_trans_brelse(tp, lbp);
892 44 : return error;
893 : }
894 12469745 : hdr = dbp->b_addr;
895 12469745 : bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
896 12469745 : grown = 0;
897 : }
898 : /*
899 : * Point to the biggest freespace in our data block.
900 : */
901 12496120 : dup = (xfs_dir2_data_unused_t *)
902 12496120 : ((char *)hdr + be16_to_cpu(bf[0].offset));
903 12496120 : needscan = needlog = 0;
904 : /*
905 : * Mark the initial part of our freespace in use for the new entry.
906 : */
907 12496120 : error = xfs_dir2_data_use_free(args, dbp, dup,
908 : (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr),
909 : length, &needlog, &needscan);
910 12493394 : if (error) {
911 0 : xfs_trans_brelse(tp, lbp);
912 0 : return error;
913 : }
914 : /*
915 : * Initialize our new entry (at last).
916 : */
917 12493394 : dep = (xfs_dir2_data_entry_t *)dup;
918 12493394 : dep->inumber = cpu_to_be64(args->inumber);
919 12493394 : dep->namelen = args->namelen;
920 24986788 : memcpy(dep->name, args->name, dep->namelen);
921 12493394 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
922 12485351 : tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
923 12480825 : *tagp = cpu_to_be16((char *)dep - (char *)hdr);
924 : /*
925 : * Need to scan fix up the bestfree table.
926 : */
927 12480825 : if (needscan)
928 3757091 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
929 : /*
930 : * Need to log the data block's header.
931 : */
932 12485817 : if (needlog)
933 12488627 : xfs_dir2_data_log_header(args, dbp);
934 12504456 : 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 12507165 : if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
940 9480334 : bestsp[use_block] = bf[0].length;
941 9480334 : if (!grown)
942 9443800 : xfs_dir3_leaf_log_bests(args, lbp, use_block, use_block);
943 : }
944 :
945 12507322 : 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 12506778 : lep->hashval = cpu_to_be32(args->hashval);
952 12506778 : 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 12505416 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
959 12499508 : xfs_dir3_leaf_log_header(args, lbp);
960 12509282 : xfs_dir3_leaf_log_ents(args, &leafhdr, lbp, lfloglow, lfloghigh);
961 12505448 : xfs_dir3_leaf_check(dp, lbp);
962 12498925 : xfs_dir3_data_check(dp, dbp);
963 12498925 : 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 113877 : 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 113877 : int from; /* source leaf index */
977 113877 : xfs_dir2_leaf_t *leaf; /* leaf structure */
978 113877 : int loglow; /* first leaf entry to log */
979 113877 : int to; /* target leaf index */
980 113877 : struct xfs_inode *dp = args->dp;
981 :
982 113877 : leaf = bp->b_addr;
983 113877 : if (!leafhdr->stale)
984 : return;
985 :
986 : /*
987 : * Compress out the stale entries in place.
988 : */
989 40303768 : for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
990 40189891 : if (leafhdr->ents[from].address ==
991 : cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
992 19044679 : continue;
993 : /*
994 : * Only actually copy the entries that are different.
995 : */
996 21145212 : if (from > to) {
997 18727046 : if (loglow == -1)
998 111150 : loglow = to;
999 18727046 : leafhdr->ents[to] = leafhdr->ents[from];
1000 : }
1001 21145212 : to++;
1002 : }
1003 : /*
1004 : * Update and log the header, log the leaf entries.
1005 : */
1006 113877 : ASSERT(leafhdr->stale == from - to);
1007 113877 : leafhdr->count -= leafhdr->stale;
1008 113877 : leafhdr->stale = 0;
1009 :
1010 113877 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, leafhdr);
1011 113877 : xfs_dir3_leaf_log_header(args, bp);
1012 113877 : if (loglow != -1)
1013 111150 : 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 3191 : 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 3191 : int from; /* source copy index */
1035 3191 : int highstale; /* stale entry at/after index */
1036 3191 : int index; /* insertion index */
1037 3191 : int keepstale; /* source index of kept stale */
1038 3191 : int lowstale; /* stale entry before index */
1039 3191 : int newindex=0; /* new insertion index */
1040 3191 : int to; /* destination copy index */
1041 :
1042 3191 : ASSERT(leafhdr->stale > 1);
1043 3191 : index = *indexp;
1044 :
1045 3191 : xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
1046 :
1047 : /*
1048 : * Pick the better of lowstale and highstale.
1049 : */
1050 3191 : if (lowstale >= 0 &&
1051 3130 : (highstale == leafhdr->count ||
1052 2125 : index - lowstale <= highstale - index))
1053 : keepstale = lowstale;
1054 : else
1055 311 : keepstale = highstale;
1056 : /*
1057 : * Copy the entries in place, removing all the stale entries
1058 : * except keepstale.
1059 : */
1060 1612991 : for (from = to = 0; from < leafhdr->count; from++) {
1061 : /*
1062 : * Notice the new value of index.
1063 : */
1064 1609800 : if (index == from)
1065 3091 : newindex = to;
1066 1609800 : if (from != keepstale &&
1067 1606609 : ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
1068 9528 : if (from == to)
1069 3191 : *lowlogp = to;
1070 9528 : continue;
1071 : }
1072 : /*
1073 : * Record the new keepstale value for the insertion.
1074 : */
1075 1600272 : if (from == keepstale)
1076 3191 : lowstale = highstale = to;
1077 : /*
1078 : * Copy only the entries that have moved.
1079 : */
1080 1600272 : if (from > to)
1081 1273430 : ents[to] = ents[from];
1082 1600272 : to++;
1083 : }
1084 3191 : ASSERT(from > to);
1085 : /*
1086 : * If the insertion point was past the last entry,
1087 : * set the new insertion point accordingly.
1088 : */
1089 3191 : if (index == from)
1090 100 : newindex = to;
1091 3191 : *indexp = newindex;
1092 : /*
1093 : * Adjust the leaf header values.
1094 : */
1095 3191 : leafhdr->count -= from - to;
1096 3191 : leafhdr->stale = 1;
1097 : /*
1098 : * Remember the low/high stale value only in the "right"
1099 : * direction.
1100 : */
1101 3191 : if (lowstale >= newindex)
1102 311 : lowstale = -1;
1103 : else
1104 2880 : highstale = leafhdr->count;
1105 3191 : *highlogp = leafhdr->count - 1;
1106 3191 : *lowstalep = lowstale;
1107 3191 : *highstalep = highstale;
1108 3191 : }
1109 :
1110 : /*
1111 : * Log the bests entries indicated from a leaf1 block.
1112 : */
1113 : static void
1114 14917956 : 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 14917956 : __be16 *firstb; /* pointer to first entry */
1121 14917956 : __be16 *lastb; /* pointer to last entry */
1122 14917956 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1123 14917956 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1124 :
1125 14917956 : 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 14917956 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1129 14917956 : firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1130 14917956 : lastb = xfs_dir2_leaf_bests_p(ltp) + last;
1131 14917956 : xfs_trans_log_buf(args->trans, bp,
1132 14917956 : (uint)((char *)firstb - (char *)leaf),
1133 14917956 : (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1134 14925539 : }
1135 :
1136 : /*
1137 : * Log the leaf entries indicated from a leaf1 or leafn block.
1138 : */
1139 : void
1140 114420171 : 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 114420171 : xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1148 114420171 : xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
1149 114420171 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1150 :
1151 114420171 : 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 114420171 : firstlep = &hdr->ents[first];
1157 114420171 : lastlep = &hdr->ents[last];
1158 114420171 : xfs_trans_log_buf(args->trans, bp,
1159 114420171 : (uint)((char *)firstlep - (char *)leaf),
1160 114420171 : (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1161 114490816 : }
1162 :
1163 : /*
1164 : * Log the header of the leaf1 or leafn block.
1165 : */
1166 : void
1167 114601131 : xfs_dir3_leaf_log_header(
1168 : struct xfs_da_args *args,
1169 : struct xfs_buf *bp)
1170 : {
1171 114601131 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1172 :
1173 114601131 : 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 114601131 : xfs_trans_log_buf(args->trans, bp,
1179 : (uint)((char *)&leaf->hdr - (char *)leaf),
1180 114601131 : args->geo->leaf_hdr_size - 1);
1181 114748854 : }
1182 :
1183 : /*
1184 : * Log the tail of the leaf1 block.
1185 : */
1186 : STATIC void
1187 85634 : xfs_dir3_leaf_log_tail(
1188 : struct xfs_da_args *args,
1189 : struct xfs_buf *bp)
1190 : {
1191 85634 : struct xfs_dir2_leaf *leaf = bp->b_addr;
1192 85634 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1193 :
1194 85634 : 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 85634 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1200 85634 : xfs_trans_log_buf(args->trans, bp, (uint)((char *)ltp - (char *)leaf),
1201 : (uint)(args->geo->blksize - 1));
1202 85638 : }
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 30224330 : xfs_dir2_leaf_lookup(
1211 : xfs_da_args_t *args) /* operation arguments */
1212 : {
1213 30224330 : struct xfs_buf *dbp; /* data block buffer */
1214 30224330 : xfs_dir2_data_entry_t *dep; /* data block entry */
1215 30224330 : xfs_inode_t *dp; /* incore directory inode */
1216 30224330 : int error; /* error return code */
1217 30224330 : int index; /* found entry index */
1218 30224330 : struct xfs_buf *lbp; /* leaf buffer */
1219 30224330 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1220 30224330 : xfs_trans_t *tp; /* transaction pointer */
1221 30224330 : struct xfs_dir3_icleaf_hdr leafhdr;
1222 :
1223 30224330 : trace_xfs_dir2_leaf_lookup(args);
1224 :
1225 : /*
1226 : * Look up name in the leaf block, returning both buffers and index.
1227 : */
1228 30197481 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1229 30222273 : if (error)
1230 : return error;
1231 :
1232 6830589 : tp = args->trans;
1233 6830589 : dp = args->dp;
1234 6830589 : xfs_dir3_leaf_check(dp, lbp);
1235 :
1236 : /*
1237 : * Get to the leaf entry and contained data entry address.
1238 : */
1239 6830704 : lep = &leafhdr.ents[index];
1240 :
1241 : /*
1242 : * Point to the data entry.
1243 : */
1244 6830704 : dep = (xfs_dir2_data_entry_t *)
1245 6830704 : ((char *)dbp->b_addr +
1246 6830704 : 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 6830704 : args->inumber = be64_to_cpu(dep->inumber);
1251 6830704 : args->filetype = xfs_dir2_data_get_ftype(dp->i_mount, dep);
1252 6830052 : error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1253 6829903 : xfs_trans_brelse(tp, dbp);
1254 6829941 : xfs_trans_brelse(tp, lbp);
1255 6829941 : 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 41800898 : 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 41800898 : xfs_dir2_db_t curdb = -1; /* current data block number */
1273 41800898 : struct xfs_buf *dbp = NULL; /* data buffer */
1274 41800898 : xfs_dir2_data_entry_t *dep; /* data entry */
1275 41800898 : xfs_inode_t *dp; /* incore directory inode */
1276 41800898 : int error; /* error return code */
1277 41800898 : int index; /* index in leaf block */
1278 41800898 : struct xfs_buf *lbp; /* leaf buffer */
1279 41800898 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1280 41800898 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1281 41800898 : xfs_mount_t *mp; /* filesystem mount point */
1282 41800898 : xfs_dir2_db_t newdb; /* new data block number */
1283 41800898 : xfs_trans_t *tp; /* transaction pointer */
1284 41800898 : xfs_dir2_db_t cidb = -1; /* case match data block no. */
1285 41800898 : enum xfs_dacmp cmp; /* name compare result */
1286 :
1287 41800898 : dp = args->dp;
1288 41800898 : tp = args->trans;
1289 41800898 : mp = dp->i_mount;
1290 :
1291 41800898 : error = xfs_dir3_leaf_read(tp, dp, args->owner, args->geo->leafblk,
1292 : &lbp);
1293 41804313 : if (error)
1294 : return error;
1295 :
1296 41796433 : *lbpp = lbp;
1297 41796433 : leaf = lbp->b_addr;
1298 41796433 : xfs_dir3_leaf_check(dp, lbp);
1299 41799029 : xfs_dir2_leaf_hdr_from_disk(mp, leafhdr, leaf);
1300 :
1301 : /*
1302 : * Look for the first leaf entry with our hash value.
1303 : */
1304 41782709 : 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 41838573 : for (lep = &leafhdr->ents[index];
1310 106172064 : index < leafhdr->count &&
1311 101835831 : be32_to_cpu(lep->hashval) == args->hashval;
1312 64333547 : lep++, index++) {
1313 : /*
1314 : * Skip over stale leaf entries.
1315 : */
1316 82720613 : if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1317 8524998 : continue;
1318 : /*
1319 : * Get the new data block number.
1320 : */
1321 74195615 : newdb = xfs_dir2_dataptr_to_db(args->geo,
1322 : 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 74155406 : if (newdb != curdb) {
1328 19557336 : if (dbp)
1329 1032119 : xfs_trans_brelse(tp, dbp);
1330 19557336 : error = xfs_dir3_data_read(tp, dp, args->owner,
1331 : xfs_dir2_db_to_da(args->geo, newdb), 0,
1332 : &dbp);
1333 19564958 : if (error) {
1334 255 : xfs_trans_brelse(tp, lbp);
1335 255 : return error;
1336 : }
1337 : curdb = newdb;
1338 : }
1339 : /*
1340 : * Point to the data entry.
1341 : */
1342 74162773 : dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
1343 74162773 : xfs_dir2_dataptr_to_off(args->geo,
1344 74162773 : 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 74162773 : cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
1351 74155081 : if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1352 18427999 : args->cmpresult = cmp;
1353 18427999 : *indexp = index;
1354 : /* case exact match: return the current buffer. */
1355 18427999 : if (cmp == XFS_CMP_EXACT) {
1356 18346588 : *dbpp = dbp;
1357 18346588 : return 0;
1358 : }
1359 : cidb = curdb;
1360 : }
1361 : }
1362 23451451 : 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 23451451 : if (args->cmpresult == XFS_CMP_CASE) {
1369 81333 : ASSERT(cidb != -1);
1370 81333 : if (cidb != curdb) {
1371 59242 : xfs_trans_brelse(tp, dbp);
1372 59242 : error = xfs_dir3_data_read(tp, dp, args->owner,
1373 : xfs_dir2_db_to_da(args->geo, cidb), 0,
1374 : &dbp);
1375 59242 : if (error) {
1376 0 : xfs_trans_brelse(tp, lbp);
1377 0 : return error;
1378 : }
1379 : }
1380 81333 : *dbpp = dbp;
1381 81333 : return 0;
1382 : }
1383 : /*
1384 : * No match found, return -ENOENT.
1385 : */
1386 23370118 : ASSERT(cidb == -1);
1387 23370118 : if (dbp)
1388 97314 : xfs_trans_brelse(tp, dbp);
1389 23370118 : xfs_trans_brelse(tp, lbp);
1390 23370118 : return -ENOENT;
1391 : }
1392 :
1393 : /*
1394 : * Remove an entry from a leaf format directory.
1395 : */
1396 : int /* error */
1397 11005747 : xfs_dir2_leaf_removename(
1398 : xfs_da_args_t *args) /* operation arguments */
1399 : {
1400 11005747 : struct xfs_da_geometry *geo = args->geo;
1401 11005747 : __be16 *bestsp; /* leaf block best freespace */
1402 11005747 : xfs_dir2_data_hdr_t *hdr; /* data block header */
1403 11005747 : xfs_dir2_db_t db; /* data block number */
1404 11005747 : struct xfs_buf *dbp; /* data block buffer */
1405 11005747 : xfs_dir2_data_entry_t *dep; /* data entry structure */
1406 11005747 : xfs_inode_t *dp; /* incore directory inode */
1407 11005747 : int error; /* error return code */
1408 11005747 : xfs_dir2_db_t i; /* temporary data block # */
1409 11005747 : int index; /* index into leaf entries */
1410 11005747 : struct xfs_buf *lbp; /* leaf buffer */
1411 11005747 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1412 11005747 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1413 11005747 : xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1414 11005747 : int needlog; /* need to log data header */
1415 11005747 : int needscan; /* need to rescan data frees */
1416 11005747 : xfs_dir2_data_off_t oldbest; /* old value of best free */
1417 11005747 : struct xfs_dir2_data_free *bf; /* bestfree table */
1418 11005747 : struct xfs_dir3_icleaf_hdr leafhdr;
1419 :
1420 11005747 : trace_xfs_dir2_leaf_removename(args);
1421 :
1422 : /*
1423 : * Lookup the leaf entry, get the leaf and data blocks read in.
1424 : */
1425 10999278 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1426 11004304 : if (error)
1427 : return error;
1428 :
1429 11000857 : dp = args->dp;
1430 11000857 : leaf = lbp->b_addr;
1431 11000857 : hdr = dbp->b_addr;
1432 11000857 : xfs_dir3_data_check(dp, dbp);
1433 11010361 : 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 11010018 : lep = &leafhdr.ents[index];
1439 11010018 : db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
1440 11010189 : dep = (xfs_dir2_data_entry_t *)((char *)hdr +
1441 11010189 : xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address)));
1442 11010189 : needscan = needlog = 0;
1443 11010189 : oldbest = be16_to_cpu(bf[0].length);
1444 11010189 : ltp = xfs_dir2_leaf_tail_p(geo, leaf);
1445 11010189 : bestsp = xfs_dir2_leaf_bests_p(ltp);
1446 11010189 : 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 11010189 : xfs_dir2_data_make_free(args, dbp,
1456 : (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
1457 11010189 : 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 11009883 : leafhdr.stale++;
1463 11009883 : xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
1464 11007353 : xfs_dir3_leaf_log_header(args, lbp);
1465 :
1466 11010842 : lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1467 11010842 : 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 11010378 : if (needscan)
1474 1237008 : xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
1475 11010404 : if (needlog)
1476 8480578 : 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 11010736 : if (be16_to_cpu(bf[0].length) != oldbest) {
1482 5395996 : bestsp[db] = bf[0].length;
1483 5395996 : xfs_dir3_leaf_log_bests(args, lbp, db, db);
1484 : }
1485 11010820 : xfs_dir3_data_check(dp, dbp);
1486 : /*
1487 : * If the data block is now empty then get rid of the data block.
1488 : */
1489 11011370 : if (be16_to_cpu(bf[0].length) ==
1490 11011370 : geo->blksize - geo->data_entry_offset) {
1491 36295 : ASSERT(db != geo->datablk);
1492 36295 : 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 36295 : dbp = NULL;
1505 : /*
1506 : * If this is the last data block then compact the
1507 : * bests table by getting rid of entries.
1508 : */
1509 36295 : if (db == be32_to_cpu(ltp->bestcount) - 1) {
1510 : /*
1511 : * Look for the last active entry (i).
1512 : */
1513 73681 : for (i = db - 1; i > 0; i--) {
1514 62560 : 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 23866 : memmove(&bestsp[db - i], bestsp,
1522 : (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
1523 11933 : be32_add_cpu(<p->bestcount, -(db - i));
1524 11933 : xfs_dir3_leaf_log_tail(args, lbp);
1525 11933 : xfs_dir3_leaf_log_bests(args, lbp, 0,
1526 11933 : be32_to_cpu(ltp->bestcount) - 1);
1527 : } else
1528 24362 : bestsp[db] = cpu_to_be16(NULLDATAOFF);
1529 : }
1530 : /*
1531 : * If the data block was not the first one, drop it.
1532 : */
1533 10975075 : else if (db != geo->datablk)
1534 6393357 : dbp = NULL;
1535 :
1536 11011370 : xfs_dir3_leaf_check(dp, lbp);
1537 : /*
1538 : * See if we can convert to block form.
1539 : */
1540 11011121 : 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 595709 : xfs_dir2_leaf_replace(
1548 : xfs_da_args_t *args) /* operation arguments */
1549 : {
1550 595709 : struct xfs_buf *dbp; /* data block buffer */
1551 595709 : xfs_dir2_data_entry_t *dep; /* data block entry */
1552 595709 : xfs_inode_t *dp; /* incore directory inode */
1553 595709 : int error; /* error return code */
1554 595709 : int index; /* index of leaf entry */
1555 595709 : struct xfs_buf *lbp; /* leaf buffer */
1556 595709 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1557 595709 : xfs_trans_t *tp; /* transaction pointer */
1558 595709 : struct xfs_dir3_icleaf_hdr leafhdr;
1559 :
1560 595709 : trace_xfs_dir2_leaf_replace(args);
1561 :
1562 : /*
1563 : * Look up the entry.
1564 : */
1565 595707 : error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp, &leafhdr);
1566 595709 : if (error)
1567 : return error;
1568 :
1569 595705 : dp = args->dp;
1570 : /*
1571 : * Point to the leaf entry, get data address from it.
1572 : */
1573 595705 : lep = &leafhdr.ents[index];
1574 : /*
1575 : * Point to the data entry.
1576 : */
1577 595705 : dep = (xfs_dir2_data_entry_t *)
1578 595705 : ((char *)dbp->b_addr +
1579 595705 : xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address)));
1580 595705 : ASSERT(args->inumber != be64_to_cpu(dep->inumber));
1581 : /*
1582 : * Put the new inode number in, log it.
1583 : */
1584 595705 : dep->inumber = cpu_to_be64(args->inumber);
1585 595705 : xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
1586 595704 : tp = args->trans;
1587 595704 : xfs_dir2_data_log_entry(args, dbp, dep);
1588 595705 : xfs_dir3_leaf_check(dp, lbp);
1589 595705 : xfs_trans_brelse(tp, lbp);
1590 595705 : 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 277812307 : xfs_dir2_leaf_search_hash(
1600 : xfs_da_args_t *args, /* operation arguments */
1601 : struct xfs_buf *lbp) /* leaf buffer */
1602 : {
1603 277812307 : xfs_dahash_t hash=0; /* hash from this entry */
1604 277812307 : xfs_dahash_t hashwant; /* hash value looking for */
1605 277812307 : int high; /* high leaf index */
1606 277812307 : int low; /* low leaf index */
1607 277812307 : xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1608 277812307 : int mid=0; /* current leaf index */
1609 277812307 : struct xfs_dir3_icleaf_hdr leafhdr;
1610 :
1611 277812307 : 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 278678998 : for (lep = leafhdr.ents, low = 0, high = leafhdr.count - 1,
1618 278678998 : hashwant = args->hashval;
1619 2235827727 : low <= high; ) {
1620 2126792875 : mid = (low + high) >> 1;
1621 2126792875 : if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
1622 : break;
1623 1957148729 : if (hash < hashwant)
1624 1123339339 : low = mid + 1;
1625 : else
1626 833809390 : high = mid - 1;
1627 : }
1628 : /*
1629 : * Found one, back up through all the equal hash values.
1630 : */
1631 278678998 : if (hash == hashwant) {
1632 1687326118 : 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 109023311 : else if (hash < hashwant)
1640 50637758 : mid++;
1641 278678998 : 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 8133 : xfs_dir3_leaf_size(
1711 : struct xfs_dir3_icleaf_hdr *hdr,
1712 : int counts)
1713 : {
1714 8133 : int entries;
1715 8133 : int hdrsize;
1716 :
1717 8133 : entries = hdr->count - hdr->stale;
1718 8133 : 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 8133 : hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1723 :
1724 8133 : return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1725 8133 : + counts * sizeof(xfs_dir2_data_off_t)
1726 8133 : + 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 42128696 : xfs_dir2_node_to_leaf(
1736 : xfs_da_state_t *state) /* directory operation state */
1737 : {
1738 42128696 : xfs_da_args_t *args; /* operation arguments */
1739 42128696 : xfs_inode_t *dp; /* incore directory inode */
1740 42128696 : int error; /* error return code */
1741 42128696 : struct xfs_buf *fbp; /* buffer for freespace block */
1742 42128696 : xfs_fileoff_t fo; /* freespace file offset */
1743 42128696 : struct xfs_buf *lbp; /* buffer for leaf block */
1744 42128696 : xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1745 42128696 : xfs_dir2_leaf_t *leaf; /* leaf structure */
1746 42128696 : xfs_mount_t *mp; /* filesystem mount point */
1747 42128696 : int rval; /* successful free trim? */
1748 42128696 : xfs_trans_t *tp; /* transaction pointer */
1749 42128696 : struct xfs_dir3_icleaf_hdr leafhdr;
1750 42128696 : 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 42128696 : if (state->path.active > 1)
1757 : return 0;
1758 53310 : args = state->args;
1759 :
1760 53310 : trace_xfs_dir2_node_to_leaf(args);
1761 :
1762 53310 : mp = state->mp;
1763 53310 : dp = args->dp;
1764 53310 : tp = args->trans;
1765 : /*
1766 : * Get the last offset in the file.
1767 : */
1768 53310 : if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK))) {
1769 : return error;
1770 : }
1771 53310 : 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 53310 : while (fo > args->geo->freeblk) {
1779 8852 : if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1780 0 : return error;
1781 : }
1782 8852 : 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 44458 : 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 44458 : if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + args->geo->blksize)
1797 : return 0;
1798 8133 : lbp = state->path.blk[0].bp;
1799 8133 : leaf = lbp->b_addr;
1800 8133 : xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
1801 :
1802 8133 : ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1803 : leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1804 :
1805 : /*
1806 : * Read the freespace block.
1807 : */
1808 8133 : error = xfs_dir2_free_read(tp, dp, args->owner, args->geo->freeblk,
1809 : &fbp);
1810 8133 : if (error)
1811 : return error;
1812 8133 : xfs_dir2_free_hdr_from_disk(mp, &freehdr, fbp->b_addr);
1813 :
1814 8133 : 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 8133 : if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > args->geo->blksize) {
1821 594 : xfs_trans_brelse(tp, fbp);
1822 594 : return 0;
1823 : }
1824 :
1825 : /*
1826 : * If the leaf has any stale entries in it, compress them out.
1827 : */
1828 7539 : if (leafhdr.stale)
1829 288 : xfs_dir3_leaf_compact(args, &leafhdr, lbp);
1830 :
1831 7539 : lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
1832 7539 : xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
1833 7539 : 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 7539 : ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
1841 7539 : ltp->bestcount = cpu_to_be32(freehdr.nvalid);
1842 :
1843 : /*
1844 : * Set up the leaf bests table.
1845 : */
1846 15078 : memcpy(xfs_dir2_leaf_bests_p(ltp), freehdr.bests,
1847 : freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
1848 :
1849 7539 : xfs_dir2_leaf_hdr_to_disk(mp, leaf, &leafhdr);
1850 7539 : xfs_dir3_leaf_log_header(args, lbp);
1851 7539 : xfs_dir3_leaf_log_bests(args, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1852 7539 : xfs_dir3_leaf_log_tail(args, lbp);
1853 7539 : xfs_dir3_leaf_check(dp, lbp);
1854 :
1855 : /*
1856 : * Get rid of the freespace block.
1857 : */
1858 7539 : error = xfs_dir2_shrink_inode(args,
1859 : xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET),
1860 : fbp);
1861 7539 : 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 7539 : 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 7539 : error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1878 7539 : state->path.blk[0].bp = NULL;
1879 7539 : return error;
1880 : }
|