blob: d0520afb913a0e7b67c675cf5731ca8a39e5fda2 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
15#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020017#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000019#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110021#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110022#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Function declarations.
26 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100027static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
28 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
30 xfs_da_state_blk_t *blk1,
31 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100032static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int index, xfs_da_state_blk_t *dblk,
34 int *rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dave Chinner24df33b2013-04-12 07:30:21 +100036/*
Christoph Hellwig3d92c932019-11-08 15:01:39 -080037 * Convert data space db to the corresponding free db.
38 */
39static xfs_dir2_db_t
40xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
41{
42 return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
43 (db / geo->free_max_bests);
44}
45
46/*
47 * Convert data space db to the corresponding index in a free db.
48 */
49static int
50xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
51{
52 return db % geo->free_max_bests;
53}
54
55/*
Dave Chinner24df33b2013-04-12 07:30:21 +100056 * Check internal consistency of a leafn block.
57 */
58#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080059static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100060xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110061 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100062 struct xfs_buf *bp)
63{
64 struct xfs_dir2_leaf *leaf = bp->b_addr;
65 struct xfs_dir3_icleaf_hdr leafhdr;
66
Christoph Hellwig51842552019-11-08 14:57:49 -080067 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100068
69 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
70 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
71 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080072 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100073 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080074 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100075
Dave Chinner1fea3232021-03-22 09:52:05 -070076 return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf, false);
Dave Chinner24df33b2013-04-12 07:30:21 +100077}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080078
79static inline void
80xfs_dir3_leaf_check(
81 struct xfs_inode *dp,
82 struct xfs_buf *bp)
83{
84 xfs_failaddr_t fa;
85
86 fa = xfs_dir3_leafn_check(dp, bp);
87 if (!fa)
88 return;
89 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070090 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
91 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080092 ASSERT(0);
93}
Dave Chinner24df33b2013-04-12 07:30:21 +100094#else
Dave Chinner41419562013-10-29 22:11:50 +110095#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100096#endif
97
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080098static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110099xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +1100100 struct xfs_buf *bp)
101{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700102 struct xfs_mount *mp = bp->b_mount;
Dave Chinner20252072012-11-12 22:54:13 +1100103 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +1100104
Brian Foster39708c22019-02-07 10:45:48 -0800105 if (!xfs_verify_magic(bp, hdr->magic))
106 return __this_address;
107
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100108 if (xfs_sb_version_hascrc(&mp->m_sb)) {
109 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
110
Eric Sandeence748ea2015-07-29 11:53:31 +1000111 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800112 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100113 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800114 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100115 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800116 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100117 }
118
119 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
120
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800121 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100122}
123
124static void
125xfs_dir3_free_read_verify(
126 struct xfs_buf *bp)
127{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700128 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800129 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130
Eric Sandeence5028c2014-02-27 15:23:10 +1100131 if (xfs_sb_version_hascrc(&mp->m_sb) &&
132 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800133 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
134 else {
135 fa = xfs_dir3_free_verify(bp);
136 if (fa)
137 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
138 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100139}
Dave Chinner20252072012-11-12 22:54:13 +1100140
Dave Chinner612cfbf2012-11-14 17:52:32 +1100141static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100142xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100143 struct xfs_buf *bp)
144{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700145 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800146 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100147 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800148 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100149
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800150 fa = xfs_dir3_free_verify(bp);
151 if (fa) {
152 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100153 return;
154 }
155
156 if (!xfs_sb_version_hascrc(&mp->m_sb))
157 return;
158
159 if (bip)
160 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
161
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100162 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100163}
164
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100165const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100166 .name = "xfs_dir3_free",
Brian Foster39708c22019-02-07 10:45:48 -0800167 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
168 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100169 .verify_read = xfs_dir3_free_read_verify,
170 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800171 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100172};
Dave Chinner20252072012-11-12 22:54:13 +1100173
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800174/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800175static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800176xfs_dir3_free_header_check(
177 struct xfs_inode *dp,
178 xfs_dablk_t fbno,
179 struct xfs_buf *bp)
180{
181 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwig5893e4f2019-11-08 15:01:30 -0800182 int maxbests = mp->m_dir_geo->free_max_bests;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800183 unsigned int firstdb;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800184
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800185 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
186 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
187 maxbests;
188 if (xfs_sb_version_hascrc(&mp->m_sb)) {
189 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
190
191 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800192 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800193 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800194 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800195 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800196 return __this_address;
Darrick J. Wong6fb5aac2020-03-11 10:37:56 -0700197 if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino)
198 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800199 } else {
200 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
201
202 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800203 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800204 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800205 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800206 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800207 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800208 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800209 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800210}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100211
Dave Chinner20252072012-11-12 22:54:13 +1100212static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100213__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100214 struct xfs_trans *tp,
215 struct xfs_inode *dp,
216 xfs_dablk_t fbno,
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800217 unsigned int flags,
Dave Chinner20252072012-11-12 22:54:13 +1100218 struct xfs_buf **bpp)
219{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800220 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100221 int err;
222
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800223 err = xfs_da_read_buf(tp, dp, fbno, flags, bpp, XFS_DATA_FORK,
224 &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800225 if (err || !*bpp)
226 return err;
227
228 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800229 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
230 if (fa) {
Darrick J. Wongce994942020-03-11 10:37:55 -0700231 __xfs_buf_mark_corrupt(*bpp, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800232 xfs_trans_brelse(tp, *bpp);
Darrick J. Wong1cb5deb2020-03-11 10:37:55 -0700233 *bpp = NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800234 return -EFSCORRUPTED;
235 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100236
237 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800238 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100239 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800240
241 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100242}
243
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800244void
245xfs_dir2_free_hdr_from_disk(
246 struct xfs_mount *mp,
247 struct xfs_dir3_icfree_hdr *to,
248 struct xfs_dir2_free *from)
249{
250 if (xfs_sb_version_hascrc(&mp->m_sb)) {
251 struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
252
253 to->magic = be32_to_cpu(from3->hdr.hdr.magic);
254 to->firstdb = be32_to_cpu(from3->hdr.firstdb);
255 to->nvalid = be32_to_cpu(from3->hdr.nvalid);
256 to->nused = be32_to_cpu(from3->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800257 to->bests = from3->bests;
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800258
259 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
260 } else {
261 to->magic = be32_to_cpu(from->hdr.magic);
262 to->firstdb = be32_to_cpu(from->hdr.firstdb);
263 to->nvalid = be32_to_cpu(from->hdr.nvalid);
264 to->nused = be32_to_cpu(from->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800265 to->bests = from->bests;
266
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800267 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
268 }
269}
270
Christoph Hellwig200dada2019-11-08 14:57:52 -0800271static void
272xfs_dir2_free_hdr_to_disk(
273 struct xfs_mount *mp,
274 struct xfs_dir2_free *to,
275 struct xfs_dir3_icfree_hdr *from)
276{
277 if (xfs_sb_version_hascrc(&mp->m_sb)) {
278 struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
279
280 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
281
282 to3->hdr.hdr.magic = cpu_to_be32(from->magic);
283 to3->hdr.firstdb = cpu_to_be32(from->firstdb);
284 to3->hdr.nvalid = cpu_to_be32(from->nvalid);
285 to3->hdr.nused = cpu_to_be32(from->nused);
286 } else {
287 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
288
289 to->hdr.magic = cpu_to_be32(from->magic);
290 to->hdr.firstdb = cpu_to_be32(from->firstdb);
291 to->hdr.nvalid = cpu_to_be32(from->nvalid);
292 to->hdr.nused = cpu_to_be32(from->nused);
293 }
294}
295
Dave Chinner20252072012-11-12 22:54:13 +1100296int
297xfs_dir2_free_read(
298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
300 xfs_dablk_t fbno,
301 struct xfs_buf **bpp)
302{
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800303 return __xfs_dir3_free_read(tp, dp, fbno, 0, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100304}
305
306static int
307xfs_dir2_free_try_read(
308 struct xfs_trans *tp,
309 struct xfs_inode *dp,
310 xfs_dablk_t fbno,
311 struct xfs_buf **bpp)
312{
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800313 return __xfs_dir3_free_read(tp, dp, fbno, XFS_DABUF_MAP_HOLE_OK, bpp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100314}
315
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100316static int
317xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000318 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100319 xfs_dir2_db_t fbno,
320 struct xfs_buf **bpp)
321{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000322 struct xfs_trans *tp = args->trans;
323 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100324 struct xfs_mount *mp = dp->i_mount;
325 struct xfs_buf *bp;
326 int error;
327 struct xfs_dir3_icfree_hdr hdr;
328
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000329 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Christoph Hellwig2911edb2019-11-20 09:46:05 -0800330 &bp, XFS_DATA_FORK);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100331 if (error)
332 return error;
333
Dave Chinner61fe1352013-04-03 16:11:30 +1100334 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100335 bp->b_ops = &xfs_dir3_free_buf_ops;
336
337 /*
338 * Initialize the new block to be empty, and remember
339 * its first slot as our empty slot.
340 */
Dave Chinnere400d272013-05-28 18:37:17 +1000341 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
342 memset(&hdr, 0, sizeof(hdr));
343
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100344 if (xfs_sb_version_hascrc(&mp->m_sb)) {
345 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
346
347 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000348
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100349 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
350 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000351 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000352 } else
353 hdr.magic = XFS_DIR2_FREE_MAGIC;
Christoph Hellwig200dada2019-11-08 14:57:52 -0800354 xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100355 *bpp = bp;
356 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*
360 * Log entries from a freespace block.
361 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000362STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000364 struct xfs_da_args *args,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800365 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000366 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int first, /* first entry to log */
368 int last) /* last entry to log */
369{
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800370 struct xfs_dir2_free *free = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100372 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
373 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000374 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800375 (char *)&hdr->bests[first] - (char *)free,
376 (char *)&hdr->bests[last] - (char *)free +
377 sizeof(hdr->bests[0]) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
381 * Log header from a freespace block.
382 */
383static void
384xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000385 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000386 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Dave Chinner836a94a2013-08-12 20:49:44 +1000388#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 xfs_dir2_free_t *free; /* freespace structure */
390
Dave Chinner1d9025e2012-06-22 18:50:14 +1000391 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100392 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
393 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000394#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000395 xfs_trans_log_buf(args->trans, bp, 0,
Christoph Hellwiged1d6122019-11-08 15:01:29 -0800396 args->geo->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399/*
400 * Convert a leaf-format directory to a node-format directory.
401 * We need to change the magic number of the leaf block, and copy
402 * the freespace table out of the leaf block into its own block.
403 */
404int /* error */
405xfs_dir2_leaf_to_node(
406 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000407 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 xfs_inode_t *dp; /* incore directory inode */
410 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000411 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 xfs_dir2_db_t fdb; /* freespace block number */
Nathan Scott68b3a102006-03-17 17:27:19 +1100413 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int i; /* leaf freespace index */
415 xfs_dir2_leaf_t *leaf; /* leaf structure */
416 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int n; /* count of live freespc ents */
418 xfs_dir2_data_off_t off; /* freespace entry value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100420 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000422 trace_xfs_dir2_leaf_to_node(args);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 tp = args->trans;
426 /*
427 * Add a freespace block to the directory.
428 */
429 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
430 return error;
431 }
Dave Chinner30028032014-06-06 15:08:18 +1000432 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /*
434 * Get the buffer for the new freespace block.
435 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000436 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100439
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800440 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000441 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000442 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800443 if (be32_to_cpu(ltp->bestcount) >
Christoph Hellwig13d2c102021-03-29 11:11:40 -0700444 (uint)dp->i_disk_size / args->geo->blksize) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -0700445 xfs_buf_mark_corrupt(lbp);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800446 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700447 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /*
450 * Copy freespace entries from the leaf block to the new block.
451 * Count active entries.
452 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100453 from = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800454 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
455 off = be16_to_cpu(*from);
456 if (off != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 n++;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800458 freehdr.bests[i] = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100462 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100464 freehdr.nused = n;
465 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
466
Christoph Hellwig200dada2019-11-08 14:57:52 -0800467 xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800468 xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
Dave Chinnerbc851782014-06-06 15:20:54 +1000469 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100470
Dave Chinner24df33b2013-04-12 07:30:21 +1000471 /*
472 * Converting the leaf to a leafnode is just a matter of changing the
473 * magic number and the ops. Do the change directly to the buffer as
474 * it's less work (and less code) than decoding the header to host
475 * format and back again.
476 */
477 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
478 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
479 else
480 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
481 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100482 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000483 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100484 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486}
487
488/*
489 * Add a leaf entry to a leaf block in a node-form directory.
490 * The other work necessary is done from the caller.
491 */
492static int /* error */
493xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000494 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800495 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int index) /* insertion pt for new entry */
497{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800498 struct xfs_dir3_icleaf_hdr leafhdr;
499 struct xfs_inode *dp = args->dp;
500 struct xfs_dir2_leaf *leaf = bp->b_addr;
501 struct xfs_dir2_leaf_entry *lep;
502 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800504 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int lfloghigh; /* high leaf entry logging */
506 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800507 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000509 trace_xfs_dir2_leafn_add(args, index);
510
Christoph Hellwig51842552019-11-08 14:57:49 -0800511 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800512 ents = leafhdr.ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
515 * Quick check just to make sure we are not going to index
516 * into other peoples memory
517 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700518 if (index < 0) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -0700519 xfs_buf_mark_corrupt(bp);
Dave Chinner24513372014-06-25 14:58:08 +1000520 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /*
524 * If there are already the maximum number of leaf entries in
525 * the block, if there are no stale entries it won't fit.
526 * Caller will do a split. If there are stale entries we'll do
527 * a compact.
528 */
529
Christoph Hellwig478c7832019-11-08 14:57:51 -0800530 if (leafhdr.count == args->geo->leaf_max_ents) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000531 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000532 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000533 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else
535 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000536 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
537 ASSERT(index == leafhdr.count ||
538 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Barry Naujok6a178102008-05-21 16:42:05 +1000540 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542
543 /*
544 * Compact out all but one stale leaf entry. Leaves behind
545 * the entry closest to index.
546 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000547 if (compact)
548 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
549 &highstale, &lfloglow, &lfloghigh);
550 else if (leafhdr.stale) {
551 /*
552 * Set impossible logging indices for this case.
553 */
554 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 lfloghigh = -1;
556 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Insert the new entry, log everything.
560 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000561 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200562 highstale, &lfloglow, &lfloghigh);
563
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100564 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000565 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100566 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000567
Christoph Hellwig163fbbb2019-11-08 14:57:50 -0800568 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000569 xfs_dir3_leaf_log_header(args, bp);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800570 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100571 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return 0;
573}
574
575#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100576static void
577xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100578 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100579 struct xfs_buf *bp,
580 xfs_dir2_db_t db)
581{
582 struct xfs_dir3_icfree_hdr hdr;
583
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800584 xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100585
Christoph Hellwig5893e4f2019-11-08 15:01:30 -0800586 ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100587 ASSERT(hdr.firstdb <= db);
588 ASSERT(db < hdr.firstdb + hdr.nvalid);
589}
590#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100591#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#endif /* DEBUG */
593
594/*
595 * Return the last hash value in the leaf.
596 * Stale entries are ok.
597 */
598xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700599xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100600 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 int *count) /* count of entries in leaf */
603{
Dave Chinner24df33b2013-04-12 07:30:21 +1000604 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Christoph Hellwig787b0892019-11-08 14:57:50 -0800606 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000607
608 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700609 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
610 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
611 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000614 *count = leafhdr.count;
615 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800617 return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000621 * Look up a leaf entry for space to add a name in a node-format leaf block.
622 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000624STATIC int
625xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000626 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 xfs_da_args_t *args, /* operation arguments */
628 int *indexp, /* out: leaf entry index */
629 xfs_da_state_t *state) /* state to fill in */
630{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000631 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000632 xfs_dir2_db_t curdb = -1; /* current data block number */
633 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_inode_t *dp; /* incore directory inode */
635 int error; /* error return value */
636 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000637 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 int index; /* leaf entry index */
639 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000640 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
642 xfs_mount_t *mp; /* filesystem mount point */
643 xfs_dir2_db_t newdb; /* new data block number */
644 xfs_dir2_db_t newfdb; /* new free block number */
645 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000646 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 dp = args->dp;
649 tp = args->trans;
650 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000651 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800652 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000653
Dave Chinner41419562013-10-29 22:11:50 +1100654 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000655 ASSERT(leafhdr.count > 0);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Look up the hash value in the leaf entries.
659 */
660 index = xfs_dir2_leaf_search_hash(args, bp);
661 /*
662 * Do we have a buffer coming in?
663 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 if (state->extravalid) {
665 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000667 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000668 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100669 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
670 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800672 length = xfs_dir2_data_entsize(mp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Loop over leaf entries with the right hash value.
675 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800676 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000677 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
678 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
680 * Skip stale leaf entries.
681 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100682 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 continue;
684 /*
685 * Pull the data block number from the entry.
686 */
Dave Chinner30028032014-06-06 15:08:18 +1000687 newdb = xfs_dir2_dataptr_to_db(args->geo,
688 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /*
690 * For addname, we're looking for a place to put the new entry.
691 * We want to use a data block with an entry of equal
692 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000693 *
694 * If this block isn't the data block we already have
695 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000697 if (newdb != curdb) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800698 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100699
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000700 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000702 * Convert the data block to the free block
703 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -0800705 newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000707 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000709 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000711 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
713 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000714 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100715
716 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000717 xfs_dir2_db_to_da(args->geo,
718 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100719 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000720 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000722 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100723
Dave Chinner01ba43b2013-10-29 22:11:52 +1100724 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000727 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -0800729 fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800733 xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
Darrick J. Wonga71895c2019-11-11 12:53:22 -0800734 if (XFS_IS_CORRUPT(mp,
735 freehdr.bests[fi] ==
736 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000737 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000738 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000739 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000741 curfdb = newfdb;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800742 if (be16_to_cpu(freehdr.bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000746 /* Didn't find any space */
747 fi = -1;
748out:
Barry Naujok6a178102008-05-21 16:42:05 +1000749 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000750 if (curbp) {
751 /* Giving back a free block. */
752 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000754 state->extrablk.index = fi;
755 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100756
757 /*
758 * Important: this magic number is not in the buffer - it's for
759 * buffer type information and therefore only the free/data type
760 * matters here, not whether CRCs are enabled or not.
761 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000762 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
763 } else {
764 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000767 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000770 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000774 * Look up a leaf entry in a node-format leaf block.
775 * The extrablk in state a data block.
776 */
777STATIC int
778xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000779 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000780 xfs_da_args_t *args, /* operation arguments */
781 int *indexp, /* out: leaf entry index */
782 xfs_da_state_t *state) /* state to fill in */
783{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000784 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000785 xfs_dir2_db_t curdb = -1; /* current data block number */
786 xfs_dir2_data_entry_t *dep; /* data block entry */
787 xfs_inode_t *dp; /* incore directory inode */
788 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000789 int index; /* leaf entry index */
790 xfs_dir2_leaf_t *leaf; /* leaf structure */
791 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
792 xfs_mount_t *mp; /* filesystem mount point */
793 xfs_dir2_db_t newdb; /* new data block number */
794 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000795 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000796 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000797
798 dp = args->dp;
799 tp = args->trans;
800 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000801 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800802 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000803
Dave Chinner41419562013-10-29 22:11:50 +1100804 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700805 if (leafhdr.count <= 0) {
Darrick J. Wong8d57c212020-03-11 10:37:54 -0700806 xfs_buf_mark_corrupt(bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700807 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700808 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000809
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000810 /*
811 * Look up the hash value in the leaf entries.
812 */
813 index = xfs_dir2_leaf_search_hash(args, bp);
814 /*
815 * Do we have a buffer coming in?
816 */
817 if (state->extravalid) {
818 curbp = state->extrablk.bp;
819 curdb = state->extrablk.blkno;
820 }
821 /*
822 * Loop over leaf entries with the right hash value.
823 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800824 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000825 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
826 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000827 /*
828 * Skip stale leaf entries.
829 */
830 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
831 continue;
832 /*
833 * Pull the data block number from the entry.
834 */
Dave Chinner30028032014-06-06 15:08:18 +1000835 newdb = xfs_dir2_dataptr_to_db(args->geo,
836 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000837 /*
838 * Not adding a new entry, so we really want to find
839 * the name given to us.
840 *
841 * If it's a different data block, go get it.
842 */
843 if (newdb != curdb) {
844 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000845 * If we had a block before that we aren't saving
846 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000847 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000848 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
849 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000850 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000851 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000852 * If needing the block that is saved with a CI match,
853 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000854 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000855 if (args->cmpresult != XFS_CMP_DIFFERENT &&
856 newdb == state->extrablk.blkno) {
857 ASSERT(state->extravalid);
858 curbp = state->extrablk.bp;
859 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100860 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000861 xfs_dir2_db_to_da(args->geo,
862 newdb),
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -0800863 0, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000864 if (error)
865 return error;
866 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100867 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000868 curdb = newdb;
869 }
870 /*
871 * Point to the data entry.
872 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000873 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000874 xfs_dir2_dataptr_to_off(args->geo,
875 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000876 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000877 * Compare the entry and if it's an exact match, return
878 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000879 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000880 */
Christoph Hellwigd8d11fc2019-11-11 12:59:26 -0800881 cmp = xfs_dir2_compname(args, dep->name, dep->namelen);
Barry Naujok5163f952008-05-21 16:41:01 +1000882 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000883 /* If there is a CI match block, drop it */
884 if (args->cmpresult != XFS_CMP_DIFFERENT &&
885 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000886 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000887 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000888 args->inumber = be64_to_cpu(dep->inumber);
Christoph Hellwig59b8b462019-11-08 15:05:48 -0800889 args->filetype = xfs_dir2_data_get_ftype(mp, dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000890 *indexp = index;
891 state->extravalid = 1;
892 state->extrablk.bp = curbp;
893 state->extrablk.blkno = curdb;
894 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000895 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000896 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100897 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100898 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000899 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000900 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000901 }
902 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000903 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000904 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000905 if (args->cmpresult == XFS_CMP_DIFFERENT) {
906 /* Giving back last used data block. */
907 state->extravalid = 1;
908 state->extrablk.bp = curbp;
909 state->extrablk.index = -1;
910 state->extrablk.blkno = curdb;
911 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100912 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100913 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000914 } else {
915 /* If the curbp is not the CI match block, drop it */
916 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000917 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000918 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000919 } else {
920 state->extravalid = 0;
921 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000922 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000923 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000924}
925
926/*
927 * Look up a leaf entry in a node-format leaf block.
928 * If this is an addname then the extrablk in state is a freespace block,
929 * otherwise it's a data block.
930 */
931int
932xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000933 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000934 xfs_da_args_t *args, /* operation arguments */
935 int *indexp, /* out: leaf entry index */
936 xfs_da_state_t *state) /* state to fill in */
937{
Barry Naujok6a178102008-05-21 16:42:05 +1000938 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000939 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
940 state);
941 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
942}
943
944/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * Move count leaf entries from source to destination leaf.
946 * Log entries and headers. Stale entries are preserved.
947 */
948static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000949xfs_dir3_leafn_moveents(
950 xfs_da_args_t *args, /* operation arguments */
951 struct xfs_buf *bp_s, /* source */
952 struct xfs_dir3_icleaf_hdr *shdr,
953 struct xfs_dir2_leaf_entry *sents,
954 int start_s,/* source leaf index */
955 struct xfs_buf *bp_d, /* destination */
956 struct xfs_dir3_icleaf_hdr *dhdr,
957 struct xfs_dir2_leaf_entry *dents,
958 int start_d,/* destination leaf index */
959 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Dave Chinner24df33b2013-04-12 07:30:21 +1000961 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000963 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Silently return if nothing to do.
967 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000968 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * If the destination index is not the end of the current
973 * destination leaf entries, open up a hole in the destination
974 * to hold the new entries.
975 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000976 if (start_d < dhdr->count) {
977 memmove(&dents[start_d + count], &dents[start_d],
978 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800979 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000980 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 /*
983 * If the source has stale leaves, count the ones in the copy range
984 * so we can update the header correctly.
985 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000986 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int i; /* temp leaf index */
988
989 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 if (sents[i].address ==
991 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 stale++;
993 }
994 } else
995 stale = 0;
996 /*
997 * Copy the leaf entries from source to destination.
998 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000999 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -08001001 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +10001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * If there are source entries after the ones we copied,
1005 * delete the ones we copied by sliding the next ones down.
1006 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001007 if (start_s + count < shdr->count) {
1008 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -08001010 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
1011 start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Dave Chinner24df33b2013-04-12 07:30:21 +10001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /*
1015 * Update the headers and log them.
1016 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001017 shdr->count -= count;
1018 shdr->stale -= stale;
1019 dhdr->count += count;
1020 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023/*
1024 * Determine the sort order of two leaf blocks.
1025 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1026 */
1027int /* sort order */
1028xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +11001029 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +10001030 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
1031 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Dave Chinner24df33b2013-04-12 07:30:21 +10001033 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1034 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1035 struct xfs_dir2_leaf_entry *ents1;
1036 struct xfs_dir2_leaf_entry *ents2;
1037 struct xfs_dir3_icleaf_hdr hdr1;
1038 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Christoph Hellwig51842552019-11-08 14:57:49 -08001040 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1041 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001042 ents1 = hdr1.ents;
1043 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001044
1045 if (hdr1.count > 0 && hdr2.count > 0 &&
1046 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1047 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1048 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 1;
1050 return 0;
1051}
1052
1053/*
1054 * Rebalance leaf entries between two leaf blocks.
1055 * This is actually only called when the second block is new,
1056 * though the code deals with the general case.
1057 * A new entry will be inserted in one of the blocks, and that
1058 * entry is taken into account when balancing.
1059 */
1060static void
1061xfs_dir2_leafn_rebalance(
1062 xfs_da_state_t *state, /* btree cursor */
1063 xfs_da_state_blk_t *blk1, /* first btree block */
1064 xfs_da_state_blk_t *blk2) /* second btree block */
1065{
1066 xfs_da_args_t *args; /* operation arguments */
1067 int count; /* count (& direction) leaves */
1068 int isleft; /* new goes in left leaf */
1069 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1070 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1071 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001072#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 int oldstale; /* old count of stale leaves */
1074#endif
1075 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001076 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001077 struct xfs_dir2_leaf_entry *ents1;
1078 struct xfs_dir2_leaf_entry *ents2;
1079 struct xfs_dir3_icleaf_hdr hdr1;
1080 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001081 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 args = state->args;
1084 /*
1085 * If the block order is wrong, swap the arguments.
1086 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001087 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1088 if (swap_blocks)
1089 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Dave Chinner1d9025e2012-06-22 18:50:14 +10001091 leaf1 = blk1->bp->b_addr;
1092 leaf2 = blk2->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001093 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1094 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001095 ents1 = hdr1.ents;
1096 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001097
1098 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001099#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001100 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#endif
1102 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * If the old leaf count was odd then the new one will be even,
1106 * so we need to divide the new count evenly.
1107 */
1108 if (oldsum & 1) {
1109 xfs_dahash_t midhash; /* middle entry hash value */
1110
Dave Chinner24df33b2013-04-12 07:30:21 +10001111 if (mid >= hdr1.count)
1112 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001114 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 isleft = args->hashval <= midhash;
1116 }
1117 /*
1118 * If the old count is even then the new count is odd, so there's
1119 * no preferred side for the new entry.
1120 * Pick the left one.
1121 */
1122 else
1123 isleft = 1;
1124 /*
1125 * Calculate moved entry count. Positive means left-to-right,
1126 * negative means right-to-left. Then move the entries.
1127 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001128 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001130 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1131 hdr1.count - count, blk2->bp,
1132 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001134 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1135 blk1->bp, &hdr1, ents1,
1136 hdr1.count, count);
1137
1138 ASSERT(hdr1.count + hdr2.count == oldsum);
1139 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1140
1141 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001142 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1143 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001144 xfs_dir3_leaf_log_header(args, blk1->bp);
1145 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001146
Dave Chinner41419562013-10-29 22:11:50 +11001147 xfs_dir3_leaf_check(dp, blk1->bp);
1148 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * Mark whether we're inserting into the old or new leaf.
1152 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001153 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001154 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001155 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001156 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001158 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * Adjust the expected index for insertion.
1161 */
1162 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001163 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001164
1165 /*
1166 * Finally sanity check just to make sure we are not returning a
1167 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
Dave Chinner41419562013-10-29 22:11:50 +11001169 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 state->inleaf = 1;
1171 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001172 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001173 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001174 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176}
1177
Dave Chinner20252072012-11-12 22:54:13 +11001178static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001179xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001180 xfs_da_args_t *args,
1181 struct xfs_dir2_data_hdr *hdr,
1182 struct xfs_dir2_free *free,
1183 xfs_dir2_db_t fdb,
1184 int findex,
1185 struct xfs_buf *fbp,
1186 int longest)
1187{
Dave Chinner20252072012-11-12 22:54:13 +11001188 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001189 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001190 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001191
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001192 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001193 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001194 /*
1195 * Data block is not empty, just set the free entry to the new
1196 * value.
1197 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001198 freehdr.bests[findex] = cpu_to_be16(longest);
1199 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001200 return 0;
1201 }
1202
1203 /* One less used entry in the free table. */
1204 freehdr.nused--;
1205
1206 /*
1207 * If this was the last entry in the table, we can trim the table size
1208 * back. There might be other entries at the end referring to
1209 * non-existent data blocks, get those too.
1210 */
1211 if (findex == freehdr.nvalid - 1) {
1212 int i; /* free entry index */
1213
1214 for (i = findex - 1; i >= 0; i--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001215 if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001216 break;
1217 }
1218 freehdr.nvalid = i + 1;
1219 logfree = 0;
1220 } else {
1221 /* Not the last entry, just punch it out. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001222 freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001223 logfree = 1;
1224 }
1225
Christoph Hellwig200dada2019-11-08 14:57:52 -08001226 xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001227 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001228
1229 /*
1230 * If there are no useful entries left in the block, get rid of the
1231 * block if we can.
1232 */
1233 if (!freehdr.nused) {
1234 int error;
1235
1236 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1237 if (error == 0) {
1238 fbp = NULL;
1239 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001240 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001241 return error;
1242 /*
1243 * It's possible to get ENOSPC if there is no
1244 * space reservation. In this case some one
1245 * else will eventually get rid of this block.
1246 */
1247 }
1248
Dave Chinner20252072012-11-12 22:54:13 +11001249 /* Log the free entry that changed, unless we got rid of it. */
1250 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001251 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001252 return 0;
1253}
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255/*
1256 * Remove an entry from a node directory.
1257 * This removes the leaf entry and the data entry,
1258 * and updates the free block if necessary.
1259 */
1260static int /* error */
1261xfs_dir2_leafn_remove(
1262 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001263 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int index, /* leaf entry index */
1265 xfs_da_state_blk_t *dblk, /* data block */
1266 int *rval) /* resulting block needs join */
1267{
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001268 struct xfs_da_geometry *geo = args->geo;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001269 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001271 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 xfs_dir2_data_entry_t *dep; /* data block entry */
1273 xfs_inode_t *dp; /* incore directory inode */
1274 xfs_dir2_leaf_t *leaf; /* leaf structure */
1275 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1276 int longest; /* longest data free entry */
1277 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int needlog; /* need to log data header */
1279 int needscan; /* need to rescan data frees */
1280 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001281 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001282 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001284 trace_xfs_dir2_leafn_remove(args, index);
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 dp = args->dp;
1287 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001288 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001289 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * Point to the entry we're removing.
1293 */
Christoph Hellwig787b0892019-11-08 14:57:50 -08001294 lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +10001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 /*
1297 * Extract the data block and offset from the entry.
1298 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001299 db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ASSERT(dblk->blkno == db);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001301 off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Kill the leaf entry by marking it stale.
1306 * Log the leaf block changes.
1307 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001308 leafhdr.stale++;
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001309 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001310 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001311
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001312 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001313 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Make the data entry free. Keep track of the longest freespace
1317 * in the data block in case it changes.
1318 */
1319 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001320 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001321 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Christoph Hellwig1848b602019-11-08 15:05:39 -08001322 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001323 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001325 xfs_dir2_data_make_free(args, dbp, off,
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -08001326 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1327 &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 /*
1329 * Rescan the data block freespaces for bestfree.
1330 * Log the data block header if needed.
1331 */
1332 if (needscan)
Christoph Hellwigae429762019-11-08 15:06:02 -08001333 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001335 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001336 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * If the longest data block freespace changes, need to update
1339 * the corresponding freeblock entry.
1340 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001341 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001343 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 xfs_dir2_db_t fdb; /* freeblock block number */
1345 int findex; /* index in freeblock entries */
1346 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 /*
1349 * Convert the data block number to a free block,
1350 * read in the free block.
1351 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001352 fdb = xfs_dir2_db_to_fdb(geo, db);
1353 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001354 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001355 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001357 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001358#ifdef DEBUG
1359 {
1360 struct xfs_dir3_icfree_hdr freehdr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001361
1362 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001363 ASSERT(freehdr.firstdb == geo->free_max_bests *
1364 (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001365 }
1366#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /*
1368 * Calculate which entry we need to fix.
1369 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001370 findex = xfs_dir2_db_to_fdindex(geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001371 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 /*
1373 * If the data block is now empty we can get rid of it
1374 * (usually).
1375 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001376 if (longest == geo->blksize - geo->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 /*
1378 * Try to punch out the data block.
1379 */
1380 error = xfs_dir2_shrink_inode(args, db, dbp);
1381 if (error == 0) {
1382 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001383 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 /*
1386 * We can get ENOSPC if there's no space reservation.
1387 * In this case just drop the buffer and some one else
1388 * will eventually get rid of the empty block.
1389 */
Dave Chinner24513372014-06-25 14:58:08 +10001390 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return error;
1392 }
1393 /*
1394 * If we got rid of the data block, we can eliminate that entry
1395 * in the free block.
1396 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001397 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001398 fdb, findex, fbp, longest);
1399 if (error)
1400 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
Dave Chinner20252072012-11-12 22:54:13 +11001402
Dave Chinner41419562013-10-29 22:11:50 +11001403 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001405 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 * to justify trying to join it with a neighbor.
1407 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001408 *rval = (geo->leaf_hdr_size +
Christoph Hellwig787b0892019-11-08 14:57:50 -08001409 (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001410 geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return 0;
1412}
1413
1414/*
1415 * Split the leaf entries in the old block into old and new blocks.
1416 */
1417int /* error */
1418xfs_dir2_leafn_split(
1419 xfs_da_state_t *state, /* btree cursor */
1420 xfs_da_state_blk_t *oldblk, /* original block */
1421 xfs_da_state_blk_t *newblk) /* newly created block */
1422{
1423 xfs_da_args_t *args; /* operation arguments */
1424 xfs_dablk_t blkno; /* new leaf block number */
1425 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001426 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /*
1429 * Allocate space for a new leaf node.
1430 */
1431 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001432 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1434 error = xfs_da_grow_inode(args, &blkno);
1435 if (error) {
1436 return error;
1437 }
1438 /*
1439 * Initialize the new leaf block.
1440 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001441 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001442 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1443 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 newblk->blkno = blkno;
1447 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1448 /*
1449 * Rebalance the entries across the two leaves, link the new
1450 * block into the leaves.
1451 */
1452 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001453 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (error) {
1455 return error;
1456 }
1457 /*
1458 * Insert the new entry in the correct block.
1459 */
1460 if (state->inleaf)
1461 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1462 else
1463 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1464 /*
1465 * Update last hashval in each block since we added the name.
1466 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001467 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1468 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001469 xfs_dir3_leaf_check(dp, oldblk->bp);
1470 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return error;
1472}
1473
1474/*
1475 * Check a leaf block and its neighbors to see if the block should be
1476 * collapsed into one or the other neighbor. Always keep the block
1477 * with the smaller block number.
1478 * If the current block is over 50% full, don't try to join it, return 0.
1479 * If the block is empty, fill in the state structure and return 2.
1480 * If it can be collapsed, fill in the state structure and return 1.
1481 * If nothing can be done, return 0.
1482 */
1483int /* error */
1484xfs_dir2_leafn_toosmall(
1485 xfs_da_state_t *state, /* btree cursor */
1486 int *action) /* resulting action to take */
1487{
1488 xfs_da_state_blk_t *blk; /* leaf block */
1489 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001490 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int bytes; /* bytes in use */
1492 int count; /* leaf live entry count */
1493 int error; /* error return value */
1494 int forward; /* sibling block direction */
1495 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 xfs_dir2_leaf_t *leaf; /* leaf structure */
1497 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001498 struct xfs_dir3_icleaf_hdr leafhdr;
1499 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001500 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /*
1503 * Check for the degenerate case of the block being over 50% full.
1504 * If so, it's not worth even looking to see if we might be able
1505 * to coalesce with a sibling.
1506 */
1507 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001508 leaf = blk->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001509 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001510 ents = leafhdr.ents;
Dave Chinner41419562013-10-29 22:11:50 +11001511 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001512
1513 count = leafhdr.count - leafhdr.stale;
Christoph Hellwig545910b2019-11-08 14:57:51 -08001514 bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001515 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * Blk over 50%, don't try to join.
1518 */
1519 *action = 0;
1520 return 0;
1521 }
1522 /*
1523 * Check for the degenerate case of the block being empty.
1524 * If the block is empty, we'll simply delete it, no need to
1525 * coalesce it with a sibling block. We choose (arbitrarily)
1526 * to merge with the forward block unless it is NULL.
1527 */
1528 if (count == 0) {
1529 /*
1530 * Make altpath point to the block we want to keep and
1531 * path point to the block we want to drop (this one).
1532 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001533 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001535 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 &rval);
1537 if (error)
1538 return error;
1539 *action = rval ? 2 : 0;
1540 return 0;
1541 }
1542 /*
1543 * Examine each sibling block to see if we can coalesce with
1544 * at least 25% free space to spare. We need to figure out
1545 * whether to merge with the forward or the backward block.
1546 * We prefer coalescing with the lower numbered sibling so as
1547 * to shrink a directory over time.
1548 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001549 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001551 struct xfs_dir3_icleaf_hdr hdr2;
1552
1553 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (blkno == 0)
1555 continue;
1556 /*
1557 * Read the sibling leaf block.
1558 */
Christoph Hellwigf3fcb312019-11-20 09:46:03 -08001559 error = xfs_dir3_leafn_read(state->args->trans, dp, blkno, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001560 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 /*
1564 * Count bytes in the two blocks combined.
1565 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001566 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001567 bytes = state->args->geo->blksize -
1568 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001569
Dave Chinner1d9025e2012-06-22 18:50:14 +10001570 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001571 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001572 ents = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001573 count += hdr2.count - hdr2.stale;
1574 bytes -= count * sizeof(ents[0]);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 /*
1577 * Fits with at least 25% to spare.
1578 */
1579 if (bytes >= 0)
1580 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001581 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583 /*
1584 * Didn't like either block, give up.
1585 */
1586 if (i >= 2) {
1587 *action = 0;
1588 return 0;
1589 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Make altpath point to the block we want to keep (the lower
1593 * numbered block) and path point to the block we want to drop.
1594 */
1595 memcpy(&state->altpath, &state->path, sizeof(state->path));
1596 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001597 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 &rval);
1599 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001600 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 &rval);
1602 if (error) {
1603 return error;
1604 }
1605 *action = rval ? 0 : 1;
1606 return 0;
1607}
1608
1609/*
1610 * Move all the leaf entries from drop_blk to save_blk.
1611 * This is done as part of a join operation.
1612 */
1613void
1614xfs_dir2_leafn_unbalance(
1615 xfs_da_state_t *state, /* cursor */
1616 xfs_da_state_blk_t *drop_blk, /* dead block */
1617 xfs_da_state_blk_t *save_blk) /* surviving block */
1618{
1619 xfs_da_args_t *args; /* operation arguments */
1620 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1621 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001622 struct xfs_dir3_icleaf_hdr savehdr;
1623 struct xfs_dir3_icleaf_hdr drophdr;
1624 struct xfs_dir2_leaf_entry *sents;
1625 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001626 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 args = state->args;
1629 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1630 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001631 drop_leaf = drop_blk->bp->b_addr;
1632 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001633
Christoph Hellwig51842552019-11-08 14:57:49 -08001634 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1635 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001636 sents = savehdr.ents;
1637 dents = drophdr.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * If there are any stale leaf entries, take this opportunity
1641 * to purge them.
1642 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001643 if (drophdr.stale)
1644 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1645 if (savehdr.stale)
1646 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * Move the entries from drop to the appropriate end of save.
1650 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001651 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001652 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001653 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1654 save_blk->bp, &savehdr, sents, 0,
1655 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001657 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1658 save_blk->bp, &savehdr, sents,
1659 savehdr.count, drophdr.count);
1660 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1661
1662 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001663 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1664 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001665 xfs_dir3_leaf_log_header(args, save_blk->bp);
1666 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001667
Dave Chinner41419562013-10-29 22:11:50 +11001668 xfs_dir3_leaf_check(dp, save_blk->bp);
1669 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001673 * Add a new data block to the directory at the free space index that the caller
1674 * has specified.
1675 */
1676static int
1677xfs_dir2_node_add_datablk(
1678 struct xfs_da_args *args,
1679 struct xfs_da_state_blk *fblk,
1680 xfs_dir2_db_t *dbno,
1681 struct xfs_buf **dbpp,
1682 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001683 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinnera07258a2019-08-29 09:04:06 -07001684 int *findex)
1685{
1686 struct xfs_inode *dp = args->dp;
1687 struct xfs_trans *tp = args->trans;
1688 struct xfs_mount *mp = dp->i_mount;
Dave Chinnera07258a2019-08-29 09:04:06 -07001689 struct xfs_dir2_data_free *bf;
Dave Chinnera07258a2019-08-29 09:04:06 -07001690 xfs_dir2_db_t fbno;
1691 struct xfs_buf *fbp;
1692 struct xfs_buf *dbp;
Dave Chinnera07258a2019-08-29 09:04:06 -07001693 int error;
1694
1695 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001696 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001697 return -ENOSPC;
1698
1699 /* Allocate and initialize the new data block. */
1700 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1701 if (error)
1702 return error;
1703 error = xfs_dir3_data_init(args, *dbno, &dbp);
1704 if (error)
1705 return error;
1706
1707 /*
1708 * Get the freespace block corresponding to the data block
1709 * that was just allocated.
1710 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001711 fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
Dave Chinnera07258a2019-08-29 09:04:06 -07001712 error = xfs_dir2_free_try_read(tp, dp,
1713 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1714 if (error)
1715 return error;
1716
1717 /*
1718 * If there wasn't a freespace block, the read will
1719 * return a NULL fbp. Allocate and initialize a new one.
1720 */
1721 if (!fbp) {
1722 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1723 if (error)
1724 return error;
1725
Darrick J. Wonga71895c2019-11-11 12:53:22 -08001726 if (XFS_IS_CORRUPT(mp,
1727 xfs_dir2_db_to_fdb(args->geo, *dbno) !=
1728 fbno)) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001729 xfs_alert(mp,
1730"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1731 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001732 (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
Dave Chinnera07258a2019-08-29 09:04:06 -07001733 (long long)*dbno, (long long)fbno);
1734 if (fblk) {
1735 xfs_alert(mp,
1736 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1737 fblk, (unsigned long long)fblk->blkno,
1738 fblk->index, fblk->magic);
1739 } else {
1740 xfs_alert(mp, " ... fblk is NULL");
1741 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001742 return -EFSCORRUPTED;
1743 }
1744
1745 /* Get a buffer for the new block. */
1746 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1747 if (error)
1748 return error;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001749 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001750
1751 /* Remember the first slot as our empty slot. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001752 hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinnera07258a2019-08-29 09:04:06 -07001753 XFS_DIR2_FREE_OFFSET)) *
Christoph Hellwig5893e4f2019-11-08 15:01:30 -08001754 args->geo->free_max_bests;
Dave Chinnera07258a2019-08-29 09:04:06 -07001755 } else {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001756 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001757 }
1758
1759 /* Set the freespace block index from the data block number. */
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001760 *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
Dave Chinnera07258a2019-08-29 09:04:06 -07001761
1762 /* Extend the freespace table if the new data block is off the end. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001763 if (*findex >= hdr->nvalid) {
Christoph Hellwig5893e4f2019-11-08 15:01:30 -08001764 ASSERT(*findex < args->geo->free_max_bests);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001765 hdr->nvalid = *findex + 1;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001766 hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinnera07258a2019-08-29 09:04:06 -07001767 }
1768
1769 /*
1770 * If this entry was for an empty data block (this should always be
1771 * true) then update the header.
1772 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001773 if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001774 hdr->nused++;
1775 xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001776 xfs_dir2_free_log_header(args, fbp);
1777 }
1778
1779 /* Update the freespace value for the new block in the table. */
Christoph Hellwig1848b602019-11-08 15:05:39 -08001780 bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001781 hdr->bests[*findex] = bf[0].length;
Dave Chinnera07258a2019-08-29 09:04:06 -07001782
1783 *dbpp = dbp;
1784 *fbpp = fbp;
1785 return 0;
1786}
1787
Dave Chinner0e822252019-08-29 09:04:07 -07001788static int
1789xfs_dir2_node_find_freeblk(
1790 struct xfs_da_args *args,
1791 struct xfs_da_state_blk *fblk,
1792 xfs_dir2_db_t *dbnop,
1793 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001794 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner0e822252019-08-29 09:04:07 -07001795 int *findexp,
1796 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Dave Chinner0e822252019-08-29 09:04:07 -07001798 struct xfs_inode *dp = args->dp;
1799 struct xfs_trans *tp = args->trans;
1800 struct xfs_buf *fbp = NULL;
Dave Chinner756c6f02019-08-29 09:04:08 -07001801 xfs_dir2_db_t firstfbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001802 xfs_dir2_db_t lastfbno;
1803 xfs_dir2_db_t ifbno = -1;
1804 xfs_dir2_db_t dbno = -1;
Dave Chinner756c6f02019-08-29 09:04:08 -07001805 xfs_dir2_db_t fbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001806 xfs_fileoff_t fo;
Dave Chinner610125a2019-08-29 09:04:07 -07001807 int findex = 0;
Dave Chinner0e822252019-08-29 09:04:07 -07001808 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * If we came in with a freespace block that means that lookup
1812 * found an entry with our hash value. This is the freespace
1813 * block for that data entry.
1814 */
1815 if (fblk) {
1816 fbp = fblk->bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 findex = fblk->index;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001818 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001820 /* caller already found the freespace for us. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001821 ASSERT(findex < hdr->nvalid);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001822 ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1823 ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001824 dbno = hdr->firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001825 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Dave Chinner0e822252019-08-29 09:04:07 -07001827
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001828 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001829 * The data block looked at didn't have enough room.
1830 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001831 */
Dave Chinner0e822252019-08-29 09:04:07 -07001832 ifbno = fblk->blkno;
Dave Chinner610125a2019-08-29 09:04:07 -07001833 xfs_trans_brelse(tp, fbp);
1834 fbp = NULL;
1835 fblk->bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
Dave Chinner0e822252019-08-29 09:04:07 -07001837
1838 /*
1839 * If we don't have a data block yet, we're going to scan the freespace
Dave Chinner610125a2019-08-29 09:04:07 -07001840 * data for a data block with enough free space in it.
Dave Chinner0e822252019-08-29 09:04:07 -07001841 */
1842 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1843 if (error)
1844 return error;
1845 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Dave Chinner756c6f02019-08-29 09:04:08 -07001846 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0e822252019-08-29 09:04:07 -07001847
Dave Chinner756c6f02019-08-29 09:04:08 -07001848 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001849 /* If it's ifbno we already looked at it. */
1850 if (fbno == ifbno)
1851 continue;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
Dave Chinner610125a2019-08-29 09:04:07 -07001854 * Read the block. There can be holes in the freespace blocks,
1855 * so this might not succeed. This should be really rare, so
1856 * there's no reason to avoid it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 */
Dave Chinner610125a2019-08-29 09:04:07 -07001858 error = xfs_dir2_free_try_read(tp, dp,
1859 xfs_dir2_db_to_da(args->geo, fbno),
1860 &fbp);
1861 if (error)
1862 return error;
1863 if (!fbp)
1864 continue;
1865
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001866 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Dave Chinner610125a2019-08-29 09:04:07 -07001867
1868 /* Scan the free entry array for a large enough free space. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001869 for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001870 if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1871 be16_to_cpu(hdr->bests[findex]) >= length) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001872 dbno = hdr->firstdb + findex;
Dave Chinner610125a2019-08-29 09:04:07 -07001873 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875 }
Dave Chinner610125a2019-08-29 09:04:07 -07001876
1877 /* Didn't find free space, go on to next free block */
1878 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Dave Chinner610125a2019-08-29 09:04:07 -07001880
Dave Chinner0e822252019-08-29 09:04:07 -07001881found_block:
1882 *dbnop = dbno;
1883 *fbpp = fbp;
1884 *findexp = findex;
1885 return 0;
1886}
1887
Dave Chinner0e822252019-08-29 09:04:07 -07001888/*
1889 * Add the data entry for a node-format directory name addition.
1890 * The leaf entry is added in xfs_dir2_leafn_add.
1891 * We may enter with a freespace block that the lookup found.
1892 */
1893static int
1894xfs_dir2_node_addname_int(
1895 struct xfs_da_args *args, /* operation arguments */
1896 struct xfs_da_state_blk *fblk) /* optional freespace block */
1897{
1898 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1899 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1900 struct xfs_dir2_data_hdr *hdr; /* data block header */
1901 struct xfs_dir2_data_free *bf;
Dave Chinner0e822252019-08-29 09:04:07 -07001902 struct xfs_trans *tp = args->trans;
1903 struct xfs_inode *dp = args->dp;
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001904 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001905 struct xfs_buf *dbp; /* data block buffer */
1906 struct xfs_buf *fbp; /* freespace buffer */
1907 xfs_dir2_data_aoff_t aoff;
1908 xfs_dir2_db_t dbno; /* data block number */
1909 int error; /* error return value */
1910 int findex; /* freespace entry index */
1911 int length; /* length of the new entry */
1912 int logfree = 0; /* need to log free entry */
1913 int needlog = 0; /* need to log data header */
1914 int needscan = 0; /* need to rescan data frees */
1915 __be16 *tagp; /* data entry tag pointer */
Dave Chinner0e822252019-08-29 09:04:07 -07001916
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -08001917 length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001918 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1919 &findex, length);
Dave Chinner0e822252019-08-29 09:04:07 -07001920 if (error)
1921 return error;
1922
1923 /*
1924 * Now we know if we must allocate blocks, so if we are checking whether
1925 * we can insert without allocation then we can return now.
1926 */
1927 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1928 if (dbno == -1)
1929 return -ENOSPC;
1930 return 0;
1931 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /*
1934 * If we don't have a data block, we need to allocate one and make
1935 * the freespace entries refer to it.
1936 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001937 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001938 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001940 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001941 &freehdr, &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001942 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001943 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001944 error = xfs_dir3_data_read(tp, dp,
1945 xfs_dir2_db_to_da(args->geo, dbno),
Christoph Hellwigcd2c9f12019-11-20 09:46:04 -08001946 0, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
Dave Chinner0e822252019-08-29 09:04:07 -07001948 if (error)
1949 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001950
1951 /* setup for data block up now */
1952 hdr = dbp->b_addr;
Christoph Hellwig1848b602019-11-08 15:05:39 -08001953 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001954 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001955
1956 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001958 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001959
1960 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001961 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1962 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1963 &needlog, &needscan);
1964 if (error) {
1965 xfs_trans_brelse(tp, dbp);
1966 return error;
1967 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001968
1969 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001971 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dep->namelen = args->namelen;
1973 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwig59b8b462019-11-08 15:05:48 -08001974 xfs_dir2_data_put_ftype(dp->i_mount, dep, args->filetype);
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -08001975 tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001976 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001977 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001978
1979 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (needscan)
Christoph Hellwigae429762019-11-08 15:06:02 -08001981 xfs_dir2_data_freescan(dp->i_mount, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001983 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001984
1985 /* If the freespace block entry is now wrong, update it. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001986 if (freehdr.bests[findex] != bf[0].length) {
1987 freehdr.bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 logfree = 1;
1989 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001990
1991 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001993 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001994
1995 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001997 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 0;
1999}
2000
2001/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07002002 * Top-level node form directory addname routine.
2003 */
2004int /* error */
2005xfs_dir2_node_addname(
2006 xfs_da_args_t *args) /* operation arguments */
2007{
2008 xfs_da_state_blk_t *blk; /* leaf block for insert */
2009 int error; /* error return value */
2010 int rval; /* sub-return value */
2011 xfs_da_state_t *state; /* btree cursor */
2012
2013 trace_xfs_dir2_node_addname(args);
2014
2015 /*
2016 * Allocate and initialize the state (btree cursor).
2017 */
Carlos Maiolino4491a3d2020-07-22 09:23:18 -07002018 state = xfs_da_state_alloc(args);
Dave Chinneraee7754b2019-08-29 09:04:06 -07002019 /*
2020 * Look up the name. We're not supposed to find it, but
2021 * this gives us the insertion point.
2022 */
2023 error = xfs_da3_node_lookup_int(state, &rval);
2024 if (error)
2025 rval = error;
2026 if (rval != -ENOENT) {
2027 goto done;
2028 }
2029 /*
2030 * Add the data entry to a data block.
2031 * Extravalid is set to a freeblock found by lookup.
2032 */
2033 rval = xfs_dir2_node_addname_int(args,
2034 state->extravalid ? &state->extrablk : NULL);
2035 if (rval) {
2036 goto done;
2037 }
2038 blk = &state->path.blk[state->path.active - 1];
2039 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2040 /*
2041 * Add the new leaf entry.
2042 */
2043 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2044 if (rval == 0) {
2045 /*
2046 * It worked, fix the hash values up the btree.
2047 */
2048 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2049 xfs_da3_fixhashpath(state, &state->path);
2050 } else {
2051 /*
2052 * It didn't work, we need to split the leaf block.
2053 */
2054 if (args->total == 0) {
2055 ASSERT(rval == -ENOSPC);
2056 goto done;
2057 }
2058 /*
2059 * Split the leaf block and insert the new entry.
2060 */
2061 rval = xfs_da3_split(state);
2062 }
2063done:
2064 xfs_da_state_free(state);
2065 return rval;
2066}
2067
2068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002070 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * The only real output is the inode number of the entry.
2072 */
2073int /* error */
2074xfs_dir2_node_lookup(
2075 xfs_da_args_t *args) /* operation arguments */
2076{
2077 int error; /* error return value */
2078 int i; /* btree level */
2079 int rval; /* operation return value */
2080 xfs_da_state_t *state; /* btree cursor */
2081
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002082 trace_xfs_dir2_node_lookup(args);
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Allocate and initialize the btree cursor.
2086 */
Carlos Maiolino4491a3d2020-07-22 09:23:18 -07002087 state = xfs_da_state_alloc(args);
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * Fill in the path to the entry in the cursor.
2091 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002092 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (error)
2094 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002095 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2096 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002097 xfs_dir2_data_entry_t *dep;
2098
Dave Chinner1d9025e2012-06-22 18:50:14 +10002099 dep = (xfs_dir2_data_entry_t *)
2100 ((char *)state->extrablk.bp->b_addr +
2101 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002102 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 /*
2105 * Release the btree blocks and leaf block.
2106 */
2107 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002108 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 state->path.blk[i].bp = NULL;
2110 }
2111 /*
2112 * Release the data block if we have it.
2113 */
2114 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002115 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 state->extrablk.bp = NULL;
2117 }
2118 xfs_da_state_free(state);
2119 return rval;
2120}
2121
2122/*
2123 * Remove an entry from a node-format directory.
2124 */
2125int /* error */
2126xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002127 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002129 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 int error; /* error return value */
2131 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002132 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002134 trace_xfs_dir2_node_removename(args);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * Allocate and initialize the btree cursor.
2138 */
Carlos Maiolino4491a3d2020-07-22 09:23:18 -07002139 state = xfs_da_state_alloc(args);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002140
2141 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002142 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002143 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002144 goto out_free;
2145
2146 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002147 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002148 error = rval;
2149 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 blk = &state->path.blk[state->path.active - 1];
2153 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2154 ASSERT(state->extravalid);
2155 /*
2156 * Remove the leaf and data entries.
2157 * Extrablk refers to the data block.
2158 */
2159 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2160 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002161 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002162 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * Fix the hash values up the btree.
2165 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002166 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /*
2168 * If we need to join leaf blocks, do it.
2169 */
2170 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002171 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * If no errors so far, try conversion to leaf format.
2174 */
2175 if (!error)
2176 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002177out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 xfs_da_state_free(state);
2179 return error;
2180}
2181
2182/*
2183 * Replace an entry's inode number in a node-format directory.
2184 */
2185int /* error */
2186xfs_dir2_node_replace(
2187 xfs_da_args_t *args) /* operation arguments */
2188{
2189 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002190 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 xfs_dir2_data_entry_t *dep; /* data entry changed */
2192 int error; /* error return value */
2193 int i; /* btree level */
2194 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002195 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 int rval; /* internal return value */
2197 xfs_da_state_t *state; /* btree cursor */
2198
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002199 trace_xfs_dir2_node_replace(args);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /*
2202 * Allocate and initialize the btree cursor.
2203 */
Carlos Maiolino4491a3d2020-07-22 09:23:18 -07002204 state = xfs_da_state_alloc(args);
Jan Kara03754232015-08-25 10:05:13 +10002205
2206 /*
2207 * We have to save new inode number and ftype since
2208 * xfs_da3_node_lookup_int() is going to overwrite them
2209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002211 ftype = args->filetype;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Lookup the entry to change in the btree.
2215 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002216 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (error) {
2218 rval = error;
2219 }
2220 /*
2221 * It should be found, since the vnodeops layer has looked it up
2222 * and locked it. But paranoia is good.
2223 */
Dave Chinner24513372014-06-25 14:58:08 +10002224 if (rval == -EEXIST) {
Christoph Hellwig787b0892019-11-08 14:57:50 -08002225 struct xfs_dir3_icleaf_hdr leafhdr;
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * Find the leaf entry.
2229 */
2230 blk = &state->path.blk[state->path.active - 1];
2231 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 ASSERT(state->extravalid);
Christoph Hellwig787b0892019-11-08 14:57:50 -08002233
2234 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2235 blk->bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /*
2237 * Point to the data entry.
2238 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002239 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002240 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2241 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002243 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002244 xfs_dir2_dataptr_to_off(args->geo,
Christoph Hellwig787b0892019-11-08 14:57:50 -08002245 be32_to_cpu(leafhdr.ents[blk->index].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002246 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /*
2248 * Fill in the new inode number and log the entry.
2249 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002250 dep->inumber = cpu_to_be64(inum);
Christoph Hellwig59b8b462019-11-08 15:05:48 -08002251 xfs_dir2_data_put_ftype(state->mp, dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002252 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rval = 0;
2254 }
2255 /*
2256 * Didn't find it, and we're holding a data block. Drop it.
2257 */
2258 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002259 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 state->extrablk.bp = NULL;
2261 }
2262 /*
2263 * Release all the buffers in the cursor.
2264 */
2265 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002266 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 state->path.blk[i].bp = NULL;
2268 }
2269 xfs_da_state_free(state);
2270 return rval;
2271}
2272
2273/*
2274 * Trim off a trailing empty freespace block.
2275 * Return (in rvalp) 1 if we did it, 0 if not.
2276 */
2277int /* error */
2278xfs_dir2_node_trim_free(
2279 xfs_da_args_t *args, /* operation arguments */
2280 xfs_fileoff_t fo, /* free block number */
2281 int *rvalp) /* out: did something */
2282{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002283 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 xfs_inode_t *dp; /* incore directory inode */
2285 int error; /* error return code */
2286 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002288 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002292
2293 *rvalp = 0;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /*
2296 * Read the freespace block.
2297 */
Dave Chinner20252072012-11-12 22:54:13 +11002298 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /*
2302 * There can be holes in freespace. If fo is a hole, there's
2303 * nothing to do.
2304 */
Dave Chinner20252072012-11-12 22:54:13 +11002305 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002307 free = bp->b_addr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08002308 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * If there are used entries, there's nothing to do.
2312 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002313 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002314 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return 0;
2316 }
2317 /*
2318 * Blow the block away.
2319 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002320 error = xfs_dir2_shrink_inode(args,
2321 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2322 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Can't fail with ENOSPC since that only happens with no
2325 * space reservation, when breaking up an extent into two
2326 * pieces. This is the last block of an extent.
2327 */
Dave Chinner24513372014-06-25 14:58:08 +10002328 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002329 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return error;
2331 }
2332 /*
2333 * Return that we succeeded.
2334 */
2335 *rvalp = 1;
2336 return 0;
2337}