blob: d4402b491a41d494b1ddd28aa0c8555cd27529df [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/*
37 * Check internal consistency of a leafn block.
38 */
39#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080040static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100041xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110042 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100043 struct xfs_buf *bp)
44{
45 struct xfs_dir2_leaf *leaf = bp->b_addr;
46 struct xfs_dir3_icleaf_hdr leafhdr;
47
Christoph Hellwig51842552019-11-08 14:57:49 -080048 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100049
50 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
51 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
52 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080053 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100054 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080055 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100056
Dave Chinner41419562013-10-29 22:11:50 +110057 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100058}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080059
60static inline void
61xfs_dir3_leaf_check(
62 struct xfs_inode *dp,
63 struct xfs_buf *bp)
64{
65 xfs_failaddr_t fa;
66
67 fa = xfs_dir3_leafn_check(dp, bp);
68 if (!fa)
69 return;
70 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070071 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
72 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080073 ASSERT(0);
74}
Dave Chinner24df33b2013-04-12 07:30:21 +100075#else
Dave Chinner41419562013-10-29 22:11:50 +110076#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100077#endif
78
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080079static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110080xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110081 struct xfs_buf *bp)
82{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -070083 struct xfs_mount *mp = bp->b_mount;
Dave Chinner20252072012-11-12 22:54:13 +110084 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110085
Brian Foster39708c22019-02-07 10:45:48 -080086 if (!xfs_verify_magic(bp, hdr->magic))
87 return __this_address;
88
Dave Chinnercbc8adf2013-04-03 16:11:21 +110089 if (xfs_sb_version_hascrc(&mp->m_sb)) {
90 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
91
Eric Sandeence748ea2015-07-29 11:53:31 +100092 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080093 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110094 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080095 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +110096 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080097 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110098 }
99
100 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
101
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800102 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100103}
104
105static void
106xfs_dir3_free_read_verify(
107 struct xfs_buf *bp)
108{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700109 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800110 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100111
Eric Sandeence5028c2014-02-27 15:23:10 +1100112 if (xfs_sb_version_hascrc(&mp->m_sb) &&
113 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800114 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
115 else {
116 fa = xfs_dir3_free_verify(bp);
117 if (fa)
118 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
119 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100120}
Dave Chinner20252072012-11-12 22:54:13 +1100121
Dave Chinner612cfbf2012-11-14 17:52:32 +1100122static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100123xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100124 struct xfs_buf *bp)
125{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700126 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800127 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800129 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800131 fa = xfs_dir3_free_verify(bp);
132 if (fa) {
133 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100134 return;
135 }
136
137 if (!xfs_sb_version_hascrc(&mp->m_sb))
138 return;
139
140 if (bip)
141 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
142
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100143 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100144}
145
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100146const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100147 .name = "xfs_dir3_free",
Brian Foster39708c22019-02-07 10:45:48 -0800148 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
149 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100150 .verify_read = xfs_dir3_free_read_verify,
151 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800152 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100153};
Dave Chinner20252072012-11-12 22:54:13 +1100154
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800155/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800156static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800157xfs_dir3_free_header_check(
158 struct xfs_inode *dp,
159 xfs_dablk_t fbno,
160 struct xfs_buf *bp)
161{
162 struct xfs_mount *mp = dp->i_mount;
163 unsigned int firstdb;
164 int maxbests;
165
166 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
167 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
168 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
169 maxbests;
170 if (xfs_sb_version_hascrc(&mp->m_sb)) {
171 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
172
173 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800174 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800175 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800176 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800177 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800178 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800179 } else {
180 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
181
182 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800183 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800184 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800185 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800186 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800187 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800188 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800189 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800190}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100191
Dave Chinner20252072012-11-12 22:54:13 +1100192static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100193__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100194 struct xfs_trans *tp,
195 struct xfs_inode *dp,
196 xfs_dablk_t fbno,
197 xfs_daddr_t mappedbno,
198 struct xfs_buf **bpp)
199{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800200 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100201 int err;
202
203 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100204 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800205 if (err || !*bpp)
206 return err;
207
208 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800209 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
210 if (fa) {
211 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800212 xfs_trans_brelse(tp, *bpp);
213 return -EFSCORRUPTED;
214 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100215
216 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800217 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100218 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800219
220 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100221}
222
223int
224xfs_dir2_free_read(
225 struct xfs_trans *tp,
226 struct xfs_inode *dp,
227 xfs_dablk_t fbno,
228 struct xfs_buf **bpp)
229{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100230 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100231}
232
233static int
234xfs_dir2_free_try_read(
235 struct xfs_trans *tp,
236 struct xfs_inode *dp,
237 xfs_dablk_t fbno,
238 struct xfs_buf **bpp)
239{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100240 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
241}
242
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100243static int
244xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000245 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100246 xfs_dir2_db_t fbno,
247 struct xfs_buf **bpp)
248{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000249 struct xfs_trans *tp = args->trans;
250 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100251 struct xfs_mount *mp = dp->i_mount;
252 struct xfs_buf *bp;
253 int error;
254 struct xfs_dir3_icfree_hdr hdr;
255
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000256 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100257 -1, &bp, XFS_DATA_FORK);
258 if (error)
259 return error;
260
Dave Chinner61fe1352013-04-03 16:11:30 +1100261 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100262 bp->b_ops = &xfs_dir3_free_buf_ops;
263
264 /*
265 * Initialize the new block to be empty, and remember
266 * its first slot as our empty slot.
267 */
Dave Chinnere400d272013-05-28 18:37:17 +1000268 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
269 memset(&hdr, 0, sizeof(hdr));
270
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
272 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
273
274 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000275
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100276 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
277 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000278 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000279 } else
280 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100281 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100282 *bpp = bp;
283 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Log entries from a freespace block.
288 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000289STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000291 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000292 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int first, /* first entry to log */
294 int last) /* last entry to log */
295{
296 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100297 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dave Chinner1d9025e2012-06-22 18:50:14 +1000299 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000300 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100301 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
302 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000303 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100304 (uint)((char *)&bests[first] - (char *)free),
305 (uint)((char *)&bests[last] - (char *)free +
306 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Log header from a freespace block.
311 */
312static void
313xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000314 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000315 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Dave Chinner836a94a2013-08-12 20:49:44 +1000317#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_dir2_free_t *free; /* freespace structure */
319
Dave Chinner1d9025e2012-06-22 18:50:14 +1000320 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100321 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
322 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000323#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000324 xfs_trans_log_buf(args->trans, bp, 0,
325 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
329 * Convert a leaf-format directory to a node-format directory.
330 * We need to change the magic number of the leaf block, and copy
331 * the freespace table out of the leaf block into its own block.
332 */
333int /* error */
334xfs_dir2_leaf_to_node(
335 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000336 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 xfs_inode_t *dp; /* incore directory inode */
339 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000340 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 xfs_dir2_db_t fdb; /* freespace block number */
342 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100343 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int i; /* leaf freespace index */
345 xfs_dir2_leaf_t *leaf; /* leaf structure */
346 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int n; /* count of live freespc ents */
348 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100349 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100351 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000353 trace_xfs_dir2_leaf_to_node(args);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 tp = args->trans;
357 /*
358 * Add a freespace block to the directory.
359 */
360 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
361 return error;
362 }
Dave Chinner30028032014-06-06 15:08:18 +1000363 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Get the buffer for the new freespace block.
366 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000367 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100370
Dave Chinner1d9025e2012-06-22 18:50:14 +1000371 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100372 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000373 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000374 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800375 if (be32_to_cpu(ltp->bestcount) >
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700376 (uint)dp->i_d.di_size / args->geo->blksize) {
377 xfs_buf_corruption_error(lbp);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800378 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700379 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Copy freespace entries from the leaf block to the new block.
383 * Count active entries.
384 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100385 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500386 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100387 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100388 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100390 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100394 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100396 freehdr.nused = n;
397 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
398
Dave Chinner01ba43b2013-10-29 22:11:52 +1100399 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000400 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
401 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100402
Dave Chinner24df33b2013-04-12 07:30:21 +1000403 /*
404 * Converting the leaf to a leafnode is just a matter of changing the
405 * magic number and the ops. Do the change directly to the buffer as
406 * it's less work (and less code) than decoding the header to host
407 * format and back again.
408 */
409 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
411 else
412 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
413 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100414 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000415 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100416 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418}
419
420/*
421 * Add a leaf entry to a leaf block in a node-form directory.
422 * The other work necessary is done from the caller.
423 */
424static int /* error */
425xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000426 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800427 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int index) /* insertion pt for new entry */
429{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800430 struct xfs_dir3_icleaf_hdr leafhdr;
431 struct xfs_inode *dp = args->dp;
432 struct xfs_dir2_leaf *leaf = bp->b_addr;
433 struct xfs_dir2_leaf_entry *lep;
434 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800436 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int lfloghigh; /* high leaf entry logging */
438 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800439 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000441 trace_xfs_dir2_leafn_add(args, index);
442
Christoph Hellwig51842552019-11-08 14:57:49 -0800443 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100444 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * Quick check just to make sure we are not going to index
448 * into other peoples memory
449 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700450 if (index < 0) {
451 xfs_buf_corruption_error(bp);
Dave Chinner24513372014-06-25 14:58:08 +1000452 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /*
456 * If there are already the maximum number of leaf entries in
457 * the block, if there are no stale entries it won't fit.
458 * Caller will do a split. If there are stale entries we'll do
459 * a compact.
460 */
461
Dave Chinner8f661932014-06-06 15:15:59 +1000462 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000463 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000464 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000465 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else
467 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000468 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
469 ASSERT(index == leafhdr.count ||
470 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Barry Naujok6a178102008-05-21 16:42:05 +1000472 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474
475 /*
476 * Compact out all but one stale leaf entry. Leaves behind
477 * the entry closest to index.
478 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000479 if (compact)
480 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
481 &highstale, &lfloglow, &lfloghigh);
482 else if (leafhdr.stale) {
483 /*
484 * Set impossible logging indices for this case.
485 */
486 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 lfloghigh = -1;
488 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Insert the new entry, log everything.
492 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200494 highstale, &lfloglow, &lfloghigh);
495
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100496 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000497 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100498 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000499
Dave Chinner01ba43b2013-10-29 22:11:52 +1100500 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000501 xfs_dir3_leaf_log_header(args, bp);
502 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100503 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
507#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100508static void
509xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100510 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100511 struct xfs_buf *bp,
512 xfs_dir2_db_t db)
513{
514 struct xfs_dir3_icfree_hdr hdr;
515
Dave Chinner01ba43b2013-10-29 22:11:52 +1100516 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100517
Dave Chinner8f661932014-06-06 15:15:59 +1000518 ASSERT((hdr.firstdb %
519 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100520 ASSERT(hdr.firstdb <= db);
521 ASSERT(db < hdr.firstdb + hdr.nvalid);
522}
523#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100524#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif /* DEBUG */
526
527/*
528 * Return the last hash value in the leaf.
529 * Stale entries are ok.
530 */
531xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700532xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100533 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000534 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int *count) /* count of entries in leaf */
536{
Dave Chinner24df33b2013-04-12 07:30:21 +1000537 struct xfs_dir2_leaf *leaf = bp->b_addr;
538 struct xfs_dir2_leaf_entry *ents;
539 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Christoph Hellwig51842552019-11-08 14:57:49 -0800541 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000542
543 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700544 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
545 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
546 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000549 *count = leafhdr.count;
550 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000552
Dave Chinner41419562013-10-29 22:11:50 +1100553 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000554 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000558 * Look up a leaf entry for space to add a name in a node-format leaf block.
559 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000561STATIC int
562xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000563 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 xfs_da_args_t *args, /* operation arguments */
565 int *indexp, /* out: leaf entry index */
566 xfs_da_state_t *state) /* state to fill in */
567{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000568 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000569 xfs_dir2_db_t curdb = -1; /* current data block number */
570 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 xfs_inode_t *dp; /* incore directory inode */
572 int error; /* error return value */
573 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000574 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int index; /* leaf entry index */
576 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000577 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
579 xfs_mount_t *mp; /* filesystem mount point */
580 xfs_dir2_db_t newdb; /* new data block number */
581 xfs_dir2_db_t newfdb; /* new free block number */
582 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000583 struct xfs_dir2_leaf_entry *ents;
584 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 dp = args->dp;
587 tp = args->trans;
588 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000589 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800590 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100591 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000592
Dave Chinner41419562013-10-29 22:11:50 +1100593 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000594 ASSERT(leafhdr.count > 0);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Look up the hash value in the leaf entries.
598 */
599 index = xfs_dir2_leaf_search_hash(args, bp);
600 /*
601 * Do we have a buffer coming in?
602 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000603 if (state->extravalid) {
604 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000606 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000607 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100608 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
609 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100611 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Loop over leaf entries with the right hash value.
614 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000615 for (lep = &ents[index];
616 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
617 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 * Skip stale leaf entries.
620 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100621 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 continue;
623 /*
624 * Pull the data block number from the entry.
625 */
Dave Chinner30028032014-06-06 15:08:18 +1000626 newdb = xfs_dir2_dataptr_to_db(args->geo,
627 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * For addname, we're looking for a place to put the new entry.
630 * We want to use a data block with an entry of equal
631 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000632 *
633 * If this block isn't the data block we already have
634 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000636 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100637 __be16 *bests;
638
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000641 * Convert the data block to the free block
642 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Dave Chinner8f661932014-06-06 15:15:59 +1000644 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000648 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 */
652 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000653 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100654
655 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000656 xfs_dir2_db_to_da(args->geo,
657 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100658 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000661 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100662
Dave Chinner01ba43b2013-10-29 22:11:52 +1100663 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000666 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Dave Chinner8f661932014-06-06 15:15:59 +1000668 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000670 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500672 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100673 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
675 XFS_ERRLEVEL_LOW, mp);
676 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000677 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000678 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000680 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100681 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000685 /* Didn't find any space */
686 fi = -1;
687out:
Barry Naujok6a178102008-05-21 16:42:05 +1000688 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000689 if (curbp) {
690 /* Giving back a free block. */
691 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000693 state->extrablk.index = fi;
694 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100695
696 /*
697 * Important: this magic number is not in the buffer - it's for
698 * buffer type information and therefore only the free/data type
699 * matters here, not whether CRCs are enabled or not.
700 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000701 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
702 } else {
703 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000706 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
708 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000709 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000713 * Look up a leaf entry in a node-format leaf block.
714 * The extrablk in state a data block.
715 */
716STATIC int
717xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000718 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000719 xfs_da_args_t *args, /* operation arguments */
720 int *indexp, /* out: leaf entry index */
721 xfs_da_state_t *state) /* state to fill in */
722{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000723 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 xfs_dir2_db_t curdb = -1; /* current data block number */
725 xfs_dir2_data_entry_t *dep; /* data block entry */
726 xfs_inode_t *dp; /* incore directory inode */
727 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000728 int index; /* leaf entry index */
729 xfs_dir2_leaf_t *leaf; /* leaf structure */
730 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
731 xfs_mount_t *mp; /* filesystem mount point */
732 xfs_dir2_db_t newdb; /* new data block number */
733 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000734 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000735 struct xfs_dir2_leaf_entry *ents;
736 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000737
738 dp = args->dp;
739 tp = args->trans;
740 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000741 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800742 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100743 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000744
Dave Chinner41419562013-10-29 22:11:50 +1100745 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700746 if (leafhdr.count <= 0) {
747 xfs_buf_corruption_error(bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700748 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700749 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000750
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000751 /*
752 * Look up the hash value in the leaf entries.
753 */
754 index = xfs_dir2_leaf_search_hash(args, bp);
755 /*
756 * Do we have a buffer coming in?
757 */
758 if (state->extravalid) {
759 curbp = state->extrablk.bp;
760 curdb = state->extrablk.blkno;
761 }
762 /*
763 * Loop over leaf entries with the right hash value.
764 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000765 for (lep = &ents[index];
766 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
767 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000768 /*
769 * Skip stale leaf entries.
770 */
771 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
772 continue;
773 /*
774 * Pull the data block number from the entry.
775 */
Dave Chinner30028032014-06-06 15:08:18 +1000776 newdb = xfs_dir2_dataptr_to_db(args->geo,
777 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000778 /*
779 * Not adding a new entry, so we really want to find
780 * the name given to us.
781 *
782 * If it's a different data block, go get it.
783 */
784 if (newdb != curdb) {
785 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000786 * If we had a block before that we aren't saving
787 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000788 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000789 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
790 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000792 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000793 * If needing the block that is saved with a CI match,
794 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000795 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000796 if (args->cmpresult != XFS_CMP_DIFFERENT &&
797 newdb == state->extrablk.blkno) {
798 ASSERT(state->extravalid);
799 curbp = state->extrablk.bp;
800 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100801 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000802 xfs_dir2_db_to_da(args->geo,
803 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100804 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000805 if (error)
806 return error;
807 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100808 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809 curdb = newdb;
810 }
811 /*
812 * Point to the data entry.
813 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000814 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000815 xfs_dir2_dataptr_to_off(args->geo,
816 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000817 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000818 * Compare the entry and if it's an exact match, return
819 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000820 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000821 */
Barry Naujok5163f952008-05-21 16:41:01 +1000822 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
823 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000824 /* If there is a CI match block, drop it */
825 if (args->cmpresult != XFS_CMP_DIFFERENT &&
826 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000827 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000828 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000829 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100830 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000831 *indexp = index;
832 state->extravalid = 1;
833 state->extrablk.bp = curbp;
834 state->extrablk.blkno = curdb;
835 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000836 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000837 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100838 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100839 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000840 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000841 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000842 }
843 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000844 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000845 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000846 if (args->cmpresult == XFS_CMP_DIFFERENT) {
847 /* Giving back last used data block. */
848 state->extravalid = 1;
849 state->extrablk.bp = curbp;
850 state->extrablk.index = -1;
851 state->extrablk.blkno = curdb;
852 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100853 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100854 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000855 } else {
856 /* If the curbp is not the CI match block, drop it */
857 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000858 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000859 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000860 } else {
861 state->extravalid = 0;
862 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000863 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000864 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000865}
866
867/*
868 * Look up a leaf entry in a node-format leaf block.
869 * If this is an addname then the extrablk in state is a freespace block,
870 * otherwise it's a data block.
871 */
872int
873xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000874 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000875 xfs_da_args_t *args, /* operation arguments */
876 int *indexp, /* out: leaf entry index */
877 xfs_da_state_t *state) /* state to fill in */
878{
Barry Naujok6a178102008-05-21 16:42:05 +1000879 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000880 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
881 state);
882 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
883}
884
885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Move count leaf entries from source to destination leaf.
887 * Log entries and headers. Stale entries are preserved.
888 */
889static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000890xfs_dir3_leafn_moveents(
891 xfs_da_args_t *args, /* operation arguments */
892 struct xfs_buf *bp_s, /* source */
893 struct xfs_dir3_icleaf_hdr *shdr,
894 struct xfs_dir2_leaf_entry *sents,
895 int start_s,/* source leaf index */
896 struct xfs_buf *bp_d, /* destination */
897 struct xfs_dir3_icleaf_hdr *dhdr,
898 struct xfs_dir2_leaf_entry *dents,
899 int start_d,/* destination leaf index */
900 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Dave Chinner24df33b2013-04-12 07:30:21 +1000902 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000904 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Silently return if nothing to do.
908 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000909 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * If the destination index is not the end of the current
914 * destination leaf entries, open up a hole in the destination
915 * to hold the new entries.
916 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 if (start_d < dhdr->count) {
918 memmove(&dents[start_d + count], &dents[start_d],
919 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000920 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000921 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 /*
924 * If the source has stale leaves, count the ones in the copy range
925 * so we can update the header correctly.
926 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000927 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int i; /* temp leaf index */
929
930 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000931 if (sents[i].address ==
932 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 stale++;
934 }
935 } else
936 stale = 0;
937 /*
938 * Copy the leaf entries from source to destination.
939 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000940 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000942 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * If there are source entries after the ones we copied,
946 * delete the ones we copied by sliding the next ones down.
947 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000948 if (start_s + count < shdr->count) {
949 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000951 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * Update the headers and log them.
956 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 shdr->count -= count;
958 shdr->stale -= stale;
959 dhdr->count += count;
960 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963/*
964 * Determine the sort order of two leaf blocks.
965 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
966 */
967int /* sort order */
968xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100969 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000970 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
971 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Dave Chinner24df33b2013-04-12 07:30:21 +1000973 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
974 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
975 struct xfs_dir2_leaf_entry *ents1;
976 struct xfs_dir2_leaf_entry *ents2;
977 struct xfs_dir3_icleaf_hdr hdr1;
978 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Christoph Hellwig51842552019-11-08 14:57:49 -0800980 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
981 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100982 ents1 = dp->d_ops->leaf_ents_p(leaf1);
983 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000984
985 if (hdr1.count > 0 && hdr2.count > 0 &&
986 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
987 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
988 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 1;
990 return 0;
991}
992
993/*
994 * Rebalance leaf entries between two leaf blocks.
995 * This is actually only called when the second block is new,
996 * though the code deals with the general case.
997 * A new entry will be inserted in one of the blocks, and that
998 * entry is taken into account when balancing.
999 */
1000static void
1001xfs_dir2_leafn_rebalance(
1002 xfs_da_state_t *state, /* btree cursor */
1003 xfs_da_state_blk_t *blk1, /* first btree block */
1004 xfs_da_state_blk_t *blk2) /* second btree block */
1005{
1006 xfs_da_args_t *args; /* operation arguments */
1007 int count; /* count (& direction) leaves */
1008 int isleft; /* new goes in left leaf */
1009 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1010 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1011 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001012#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int oldstale; /* old count of stale leaves */
1014#endif
1015 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001016 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001017 struct xfs_dir2_leaf_entry *ents1;
1018 struct xfs_dir2_leaf_entry *ents2;
1019 struct xfs_dir3_icleaf_hdr hdr1;
1020 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001021 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 args = state->args;
1024 /*
1025 * If the block order is wrong, swap the arguments.
1026 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001027 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1028 if (swap_blocks)
1029 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dave Chinner1d9025e2012-06-22 18:50:14 +10001031 leaf1 = blk1->bp->b_addr;
1032 leaf2 = blk2->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001033 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1034 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001035 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1036 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001037
1038 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001039#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001040 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041#endif
1042 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * If the old leaf count was odd then the new one will be even,
1046 * so we need to divide the new count evenly.
1047 */
1048 if (oldsum & 1) {
1049 xfs_dahash_t midhash; /* middle entry hash value */
1050
Dave Chinner24df33b2013-04-12 07:30:21 +10001051 if (mid >= hdr1.count)
1052 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001054 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 isleft = args->hashval <= midhash;
1056 }
1057 /*
1058 * If the old count is even then the new count is odd, so there's
1059 * no preferred side for the new entry.
1060 * Pick the left one.
1061 */
1062 else
1063 isleft = 1;
1064 /*
1065 * Calculate moved entry count. Positive means left-to-right,
1066 * negative means right-to-left. Then move the entries.
1067 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001068 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001070 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1071 hdr1.count - count, blk2->bp,
1072 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001074 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1075 blk1->bp, &hdr1, ents1,
1076 hdr1.count, count);
1077
1078 ASSERT(hdr1.count + hdr2.count == oldsum);
1079 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1080
1081 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001082 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1083 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001084 xfs_dir3_leaf_log_header(args, blk1->bp);
1085 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001086
Dave Chinner41419562013-10-29 22:11:50 +11001087 xfs_dir3_leaf_check(dp, blk1->bp);
1088 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /*
1091 * Mark whether we're inserting into the old or new leaf.
1092 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001093 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001094 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001095 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001096 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001098 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * Adjust the expected index for insertion.
1101 */
1102 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001103 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001104
1105 /*
1106 * Finally sanity check just to make sure we are not returning a
1107 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
Dave Chinner41419562013-10-29 22:11:50 +11001109 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 state->inleaf = 1;
1111 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001112 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001113 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001114 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116}
1117
Dave Chinner20252072012-11-12 22:54:13 +11001118static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001119xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001120 xfs_da_args_t *args,
1121 struct xfs_dir2_data_hdr *hdr,
1122 struct xfs_dir2_free *free,
1123 xfs_dir2_db_t fdb,
1124 int findex,
1125 struct xfs_buf *fbp,
1126 int longest)
1127{
Dave Chinner20252072012-11-12 22:54:13 +11001128 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001129 __be16 *bests;
1130 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001131 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001132
Dave Chinner01ba43b2013-10-29 22:11:52 +11001133 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001134 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001135 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001136 /*
1137 * Data block is not empty, just set the free entry to the new
1138 * value.
1139 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001140 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001141 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001142 return 0;
1143 }
1144
1145 /* One less used entry in the free table. */
1146 freehdr.nused--;
1147
1148 /*
1149 * If this was the last entry in the table, we can trim the table size
1150 * back. There might be other entries at the end referring to
1151 * non-existent data blocks, get those too.
1152 */
1153 if (findex == freehdr.nvalid - 1) {
1154 int i; /* free entry index */
1155
1156 for (i = findex - 1; i >= 0; i--) {
1157 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1158 break;
1159 }
1160 freehdr.nvalid = i + 1;
1161 logfree = 0;
1162 } else {
1163 /* Not the last entry, just punch it out. */
1164 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001165 logfree = 1;
1166 }
1167
Dave Chinner01ba43b2013-10-29 22:11:52 +11001168 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001169 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001170
1171 /*
1172 * If there are no useful entries left in the block, get rid of the
1173 * block if we can.
1174 */
1175 if (!freehdr.nused) {
1176 int error;
1177
1178 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1179 if (error == 0) {
1180 fbp = NULL;
1181 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001182 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001183 return error;
1184 /*
1185 * It's possible to get ENOSPC if there is no
1186 * space reservation. In this case some one
1187 * else will eventually get rid of this block.
1188 */
1189 }
1190
Dave Chinner20252072012-11-12 22:54:13 +11001191 /* Log the free entry that changed, unless we got rid of it. */
1192 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001193 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001194 return 0;
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/*
1198 * Remove an entry from a node directory.
1199 * This removes the leaf entry and the data entry,
1200 * and updates the free block if necessary.
1201 */
1202static int /* error */
1203xfs_dir2_leafn_remove(
1204 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001205 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 int index, /* leaf entry index */
1207 xfs_da_state_blk_t *dblk, /* data block */
1208 int *rval) /* resulting block needs join */
1209{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001210 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001212 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 xfs_dir2_data_entry_t *dep; /* data block entry */
1214 xfs_inode_t *dp; /* incore directory inode */
1215 xfs_dir2_leaf_t *leaf; /* leaf structure */
1216 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1217 int longest; /* longest data free entry */
1218 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int needlog; /* need to log data header */
1220 int needscan; /* need to rescan data frees */
1221 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001222 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001223 struct xfs_dir3_icleaf_hdr leafhdr;
1224 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001226 trace_xfs_dir2_leafn_remove(args, index);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 dp = args->dp;
1229 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001230 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001231 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001232 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * Point to the entry we're removing.
1236 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001237 lep = &ents[index];
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 /*
1240 * Extract the data block and offset from the entry.
1241 */
Dave Chinner30028032014-06-06 15:08:18 +10001242 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001244 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /*
1248 * Kill the leaf entry by marking it stale.
1249 * Log the leaf block changes.
1250 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001251 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001252 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001253 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001254
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001255 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001256 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * Make the data entry free. Keep track of the longest freespace
1260 * in the data block in case it changes.
1261 */
1262 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001263 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001264 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001265 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001266 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001268 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001269 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Rescan the data block freespaces for bestfree.
1272 * Log the data block header if needed.
1273 */
1274 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001275 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001277 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001278 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * If the longest data block freespace changes, need to update
1281 * the corresponding freeblock entry.
1282 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001283 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 xfs_dir2_db_t fdb; /* freeblock block number */
1287 int findex; /* index in freeblock entries */
1288 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /*
1291 * Convert the data block number to a free block,
1292 * read in the free block.
1293 */
Dave Chinner8f661932014-06-06 15:15:59 +10001294 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001295 error = xfs_dir2_free_read(tp, dp,
1296 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001297 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001300 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001301#ifdef DEBUG
1302 {
1303 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001304 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001305 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001306 (fdb - xfs_dir2_byte_to_db(args->geo,
1307 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001308 }
1309#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Calculate which entry we need to fix.
1312 */
Dave Chinner8f661932014-06-06 15:15:59 +10001313 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001314 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * If the data block is now empty we can get rid of it
1317 * (usually).
1318 */
Dave Chinner8f661932014-06-06 15:15:59 +10001319 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001320 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Try to punch out the data block.
1323 */
1324 error = xfs_dir2_shrink_inode(args, db, dbp);
1325 if (error == 0) {
1326 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001327 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 /*
1330 * We can get ENOSPC if there's no space reservation.
1331 * In this case just drop the buffer and some one else
1332 * will eventually get rid of the empty block.
1333 */
Dave Chinner24513372014-06-25 14:58:08 +10001334 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return error;
1336 }
1337 /*
1338 * If we got rid of the data block, we can eliminate that entry
1339 * in the free block.
1340 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001341 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001342 fdb, findex, fbp, longest);
1343 if (error)
1344 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Dave Chinner20252072012-11-12 22:54:13 +11001346
Dave Chinner41419562013-10-29 22:11:50 +11001347 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001349 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * to justify trying to join it with a neighbor.
1351 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001352 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001353 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001354 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 0;
1356}
1357
1358/*
1359 * Split the leaf entries in the old block into old and new blocks.
1360 */
1361int /* error */
1362xfs_dir2_leafn_split(
1363 xfs_da_state_t *state, /* btree cursor */
1364 xfs_da_state_blk_t *oldblk, /* original block */
1365 xfs_da_state_blk_t *newblk) /* newly created block */
1366{
1367 xfs_da_args_t *args; /* operation arguments */
1368 xfs_dablk_t blkno; /* new leaf block number */
1369 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001370 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
1373 * Allocate space for a new leaf node.
1374 */
1375 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001376 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1378 error = xfs_da_grow_inode(args, &blkno);
1379 if (error) {
1380 return error;
1381 }
1382 /*
1383 * Initialize the new leaf block.
1384 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001385 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001386 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1387 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 newblk->blkno = blkno;
1391 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1392 /*
1393 * Rebalance the entries across the two leaves, link the new
1394 * block into the leaves.
1395 */
1396 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001397 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (error) {
1399 return error;
1400 }
1401 /*
1402 * Insert the new entry in the correct block.
1403 */
1404 if (state->inleaf)
1405 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1406 else
1407 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1408 /*
1409 * Update last hashval in each block since we added the name.
1410 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001411 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1412 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001413 xfs_dir3_leaf_check(dp, oldblk->bp);
1414 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return error;
1416}
1417
1418/*
1419 * Check a leaf block and its neighbors to see if the block should be
1420 * collapsed into one or the other neighbor. Always keep the block
1421 * with the smaller block number.
1422 * If the current block is over 50% full, don't try to join it, return 0.
1423 * If the block is empty, fill in the state structure and return 2.
1424 * If it can be collapsed, fill in the state structure and return 1.
1425 * If nothing can be done, return 0.
1426 */
1427int /* error */
1428xfs_dir2_leafn_toosmall(
1429 xfs_da_state_t *state, /* btree cursor */
1430 int *action) /* resulting action to take */
1431{
1432 xfs_da_state_blk_t *blk; /* leaf block */
1433 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001434 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int bytes; /* bytes in use */
1436 int count; /* leaf live entry count */
1437 int error; /* error return value */
1438 int forward; /* sibling block direction */
1439 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 xfs_dir2_leaf_t *leaf; /* leaf structure */
1441 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001442 struct xfs_dir3_icleaf_hdr leafhdr;
1443 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001444 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /*
1447 * Check for the degenerate case of the block being over 50% full.
1448 * If so, it's not worth even looking to see if we might be able
1449 * to coalesce with a sibling.
1450 */
1451 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001452 leaf = blk->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001453 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001454 ents = dp->d_ops->leaf_ents_p(leaf);
1455 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001456
1457 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001458 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001459 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * Blk over 50%, don't try to join.
1462 */
1463 *action = 0;
1464 return 0;
1465 }
1466 /*
1467 * Check for the degenerate case of the block being empty.
1468 * If the block is empty, we'll simply delete it, no need to
1469 * coalesce it with a sibling block. We choose (arbitrarily)
1470 * to merge with the forward block unless it is NULL.
1471 */
1472 if (count == 0) {
1473 /*
1474 * Make altpath point to the block we want to keep and
1475 * path point to the block we want to drop (this one).
1476 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001477 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001479 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 &rval);
1481 if (error)
1482 return error;
1483 *action = rval ? 2 : 0;
1484 return 0;
1485 }
1486 /*
1487 * Examine each sibling block to see if we can coalesce with
1488 * at least 25% free space to spare. We need to figure out
1489 * whether to merge with the forward or the backward block.
1490 * We prefer coalescing with the lower numbered sibling so as
1491 * to shrink a directory over time.
1492 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001493 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001495 struct xfs_dir3_icleaf_hdr hdr2;
1496
1497 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 if (blkno == 0)
1499 continue;
1500 /*
1501 * Read the sibling leaf block.
1502 */
Dave Chinner41419562013-10-29 22:11:50 +11001503 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001504 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001505 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * Count bytes in the two blocks combined.
1510 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001511 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001512 bytes = state->args->geo->blksize -
1513 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001514
Dave Chinner1d9025e2012-06-22 18:50:14 +10001515 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001516 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001517 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001518 count += hdr2.count - hdr2.stale;
1519 bytes -= count * sizeof(ents[0]);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /*
1522 * Fits with at least 25% to spare.
1523 */
1524 if (bytes >= 0)
1525 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 /*
1529 * Didn't like either block, give up.
1530 */
1531 if (i >= 2) {
1532 *action = 0;
1533 return 0;
1534 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 /*
1537 * Make altpath point to the block we want to keep (the lower
1538 * numbered block) and path point to the block we want to drop.
1539 */
1540 memcpy(&state->altpath, &state->path, sizeof(state->path));
1541 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001542 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 &rval);
1544 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001545 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 &rval);
1547 if (error) {
1548 return error;
1549 }
1550 *action = rval ? 0 : 1;
1551 return 0;
1552}
1553
1554/*
1555 * Move all the leaf entries from drop_blk to save_blk.
1556 * This is done as part of a join operation.
1557 */
1558void
1559xfs_dir2_leafn_unbalance(
1560 xfs_da_state_t *state, /* cursor */
1561 xfs_da_state_blk_t *drop_blk, /* dead block */
1562 xfs_da_state_blk_t *save_blk) /* surviving block */
1563{
1564 xfs_da_args_t *args; /* operation arguments */
1565 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1566 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001567 struct xfs_dir3_icleaf_hdr savehdr;
1568 struct xfs_dir3_icleaf_hdr drophdr;
1569 struct xfs_dir2_leaf_entry *sents;
1570 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001571 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 args = state->args;
1574 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1575 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001576 drop_leaf = drop_blk->bp->b_addr;
1577 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001578
Christoph Hellwig51842552019-11-08 14:57:49 -08001579 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1580 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001581 sents = dp->d_ops->leaf_ents_p(save_leaf);
1582 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /*
1585 * If there are any stale leaf entries, take this opportunity
1586 * to purge them.
1587 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001588 if (drophdr.stale)
1589 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1590 if (savehdr.stale)
1591 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Move the entries from drop to the appropriate end of save.
1595 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001596 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001597 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001598 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1599 save_blk->bp, &savehdr, sents, 0,
1600 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001602 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1603 save_blk->bp, &savehdr, sents,
1604 savehdr.count, drophdr.count);
1605 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1606
1607 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001608 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1609 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001610 xfs_dir3_leaf_log_header(args, save_blk->bp);
1611 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001612
Dave Chinner41419562013-10-29 22:11:50 +11001613 xfs_dir3_leaf_check(dp, save_blk->bp);
1614 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001618 * Add a new data block to the directory at the free space index that the caller
1619 * has specified.
1620 */
1621static int
1622xfs_dir2_node_add_datablk(
1623 struct xfs_da_args *args,
1624 struct xfs_da_state_blk *fblk,
1625 xfs_dir2_db_t *dbno,
1626 struct xfs_buf **dbpp,
1627 struct xfs_buf **fbpp,
1628 int *findex)
1629{
1630 struct xfs_inode *dp = args->dp;
1631 struct xfs_trans *tp = args->trans;
1632 struct xfs_mount *mp = dp->i_mount;
1633 struct xfs_dir3_icfree_hdr freehdr;
1634 struct xfs_dir2_data_free *bf;
1635 struct xfs_dir2_data_hdr *hdr;
1636 struct xfs_dir2_free *free = NULL;
1637 xfs_dir2_db_t fbno;
1638 struct xfs_buf *fbp;
1639 struct xfs_buf *dbp;
1640 __be16 *bests = NULL;
1641 int error;
1642
1643 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001644 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001645 return -ENOSPC;
1646
1647 /* Allocate and initialize the new data block. */
1648 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1649 if (error)
1650 return error;
1651 error = xfs_dir3_data_init(args, *dbno, &dbp);
1652 if (error)
1653 return error;
1654
1655 /*
1656 * Get the freespace block corresponding to the data block
1657 * that was just allocated.
1658 */
1659 fbno = dp->d_ops->db_to_fdb(args->geo, *dbno);
1660 error = xfs_dir2_free_try_read(tp, dp,
1661 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1662 if (error)
1663 return error;
1664
1665 /*
1666 * If there wasn't a freespace block, the read will
1667 * return a NULL fbp. Allocate and initialize a new one.
1668 */
1669 if (!fbp) {
1670 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1671 if (error)
1672 return error;
1673
1674 if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) {
1675 xfs_alert(mp,
1676"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1677 __func__, (unsigned long long)dp->i_ino,
1678 (long long)dp->d_ops->db_to_fdb(args->geo, *dbno),
1679 (long long)*dbno, (long long)fbno);
1680 if (fblk) {
1681 xfs_alert(mp,
1682 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1683 fblk, (unsigned long long)fblk->blkno,
1684 fblk->index, fblk->magic);
1685 } else {
1686 xfs_alert(mp, " ... fblk is NULL");
1687 }
1688 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1689 return -EFSCORRUPTED;
1690 }
1691
1692 /* Get a buffer for the new block. */
1693 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1694 if (error)
1695 return error;
1696 free = fbp->b_addr;
1697 bests = dp->d_ops->free_bests_p(free);
1698 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1699
1700 /* Remember the first slot as our empty slot. */
1701 freehdr.firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1702 XFS_DIR2_FREE_OFFSET)) *
1703 dp->d_ops->free_max_bests(args->geo);
1704 } else {
1705 free = fbp->b_addr;
1706 bests = dp->d_ops->free_bests_p(free);
1707 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1708 }
1709
1710 /* Set the freespace block index from the data block number. */
1711 *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno);
1712
1713 /* Extend the freespace table if the new data block is off the end. */
1714 if (*findex >= freehdr.nvalid) {
1715 ASSERT(*findex < dp->d_ops->free_max_bests(args->geo));
1716 freehdr.nvalid = *findex + 1;
1717 bests[*findex] = cpu_to_be16(NULLDATAOFF);
1718 }
1719
1720 /*
1721 * If this entry was for an empty data block (this should always be
1722 * true) then update the header.
1723 */
1724 if (bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1725 freehdr.nused++;
1726 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1727 xfs_dir2_free_log_header(args, fbp);
1728 }
1729
1730 /* Update the freespace value for the new block in the table. */
1731 hdr = dbp->b_addr;
1732 bf = dp->d_ops->data_bestfree_p(hdr);
1733 bests[*findex] = bf[0].length;
1734
1735 *dbpp = dbp;
1736 *fbpp = fbp;
1737 return 0;
1738}
1739
Dave Chinner0e822252019-08-29 09:04:07 -07001740static int
1741xfs_dir2_node_find_freeblk(
1742 struct xfs_da_args *args,
1743 struct xfs_da_state_blk *fblk,
1744 xfs_dir2_db_t *dbnop,
1745 struct xfs_buf **fbpp,
1746 int *findexp,
1747 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001749 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001750 struct xfs_dir2_free *free = NULL;
1751 struct xfs_inode *dp = args->dp;
1752 struct xfs_trans *tp = args->trans;
1753 struct xfs_buf *fbp = NULL;
Dave Chinner756c6f02019-08-29 09:04:08 -07001754 xfs_dir2_db_t firstfbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001755 xfs_dir2_db_t lastfbno;
1756 xfs_dir2_db_t ifbno = -1;
1757 xfs_dir2_db_t dbno = -1;
Dave Chinner756c6f02019-08-29 09:04:08 -07001758 xfs_dir2_db_t fbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001759 xfs_fileoff_t fo;
Dave Chinner610125a2019-08-29 09:04:07 -07001760 __be16 *bests = NULL;
1761 int findex = 0;
Dave Chinner0e822252019-08-29 09:04:07 -07001762 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * If we came in with a freespace block that means that lookup
1766 * found an entry with our hash value. This is the freespace
1767 * block for that data entry.
1768 */
1769 if (fblk) {
1770 fbp = fblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001771 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 findex = fblk->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001774 /* caller already found the freespace for us. */
1775 bests = dp->d_ops->free_bests_p(free);
1776 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1777
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001778 ASSERT(findex < freehdr.nvalid);
1779 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1780 ASSERT(be16_to_cpu(bests[findex]) >= length);
1781 dbno = freehdr.firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001782 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Dave Chinner0e822252019-08-29 09:04:07 -07001784
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001785 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001786 * The data block looked at didn't have enough room.
1787 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001788 */
Dave Chinner0e822252019-08-29 09:04:07 -07001789 ifbno = fblk->blkno;
Dave Chinner610125a2019-08-29 09:04:07 -07001790 xfs_trans_brelse(tp, fbp);
1791 fbp = NULL;
1792 fblk->bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Dave Chinner0e822252019-08-29 09:04:07 -07001794
1795 /*
1796 * If we don't have a data block yet, we're going to scan the freespace
Dave Chinner610125a2019-08-29 09:04:07 -07001797 * data for a data block with enough free space in it.
Dave Chinner0e822252019-08-29 09:04:07 -07001798 */
1799 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1800 if (error)
1801 return error;
1802 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Dave Chinner756c6f02019-08-29 09:04:08 -07001803 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0e822252019-08-29 09:04:07 -07001804
Dave Chinner756c6f02019-08-29 09:04:08 -07001805 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001806 /* If it's ifbno we already looked at it. */
1807 if (fbno == ifbno)
1808 continue;
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
Dave Chinner610125a2019-08-29 09:04:07 -07001811 * Read the block. There can be holes in the freespace blocks,
1812 * so this might not succeed. This should be really rare, so
1813 * there's no reason to avoid it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Dave Chinner610125a2019-08-29 09:04:07 -07001815 error = xfs_dir2_free_try_read(tp, dp,
1816 xfs_dir2_db_to_da(args->geo, fbno),
1817 &fbp);
1818 if (error)
1819 return error;
1820 if (!fbp)
1821 continue;
1822
1823 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001824 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001825 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner610125a2019-08-29 09:04:07 -07001826
1827 /* Scan the free entry array for a large enough free space. */
Dave Chinner756c6f02019-08-29 09:04:08 -07001828 for (findex = freehdr.nvalid - 1; findex >= 0; findex--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001829 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1830 be16_to_cpu(bests[findex]) >= length) {
1831 dbno = freehdr.firstdb + findex;
1832 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 }
Dave Chinner610125a2019-08-29 09:04:07 -07001835
1836 /* Didn't find free space, go on to next free block */
1837 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
Dave Chinner610125a2019-08-29 09:04:07 -07001839
Dave Chinner0e822252019-08-29 09:04:07 -07001840found_block:
1841 *dbnop = dbno;
1842 *fbpp = fbp;
1843 *findexp = findex;
1844 return 0;
1845}
1846
1847
1848/*
1849 * Add the data entry for a node-format directory name addition.
1850 * The leaf entry is added in xfs_dir2_leafn_add.
1851 * We may enter with a freespace block that the lookup found.
1852 */
1853static int
1854xfs_dir2_node_addname_int(
1855 struct xfs_da_args *args, /* operation arguments */
1856 struct xfs_da_state_blk *fblk) /* optional freespace block */
1857{
1858 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1859 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1860 struct xfs_dir2_data_hdr *hdr; /* data block header */
1861 struct xfs_dir2_data_free *bf;
1862 struct xfs_dir2_free *free = NULL; /* freespace block structure */
1863 struct xfs_trans *tp = args->trans;
1864 struct xfs_inode *dp = args->dp;
1865 struct xfs_buf *dbp; /* data block buffer */
1866 struct xfs_buf *fbp; /* freespace buffer */
1867 xfs_dir2_data_aoff_t aoff;
1868 xfs_dir2_db_t dbno; /* data block number */
1869 int error; /* error return value */
1870 int findex; /* freespace entry index */
1871 int length; /* length of the new entry */
1872 int logfree = 0; /* need to log free entry */
1873 int needlog = 0; /* need to log data header */
1874 int needscan = 0; /* need to rescan data frees */
1875 __be16 *tagp; /* data entry tag pointer */
1876 __be16 *bests;
1877
1878 length = dp->d_ops->data_entsize(args->namelen);
1879 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &findex,
1880 length);
1881 if (error)
1882 return error;
1883
1884 /*
1885 * Now we know if we must allocate blocks, so if we are checking whether
1886 * we can insert without allocation then we can return now.
1887 */
1888 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1889 if (dbno == -1)
1890 return -ENOSPC;
1891 return 0;
1892 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /*
1895 * If we don't have a data block, we need to allocate one and make
1896 * the freespace entries refer to it.
1897 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001898 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001899 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001901 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1902 &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001903 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001904 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001905 error = xfs_dir3_data_read(tp, dp,
1906 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001907 -1, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Dave Chinner0e822252019-08-29 09:04:07 -07001909 if (error)
1910 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001911
1912 /* setup for data block up now */
1913 hdr = dbp->b_addr;
1914 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001915 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001916
1917 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001919 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001920
1921 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001922 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1923 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1924 &needlog, &needscan);
1925 if (error) {
1926 xfs_trans_brelse(tp, dbp);
1927 return error;
1928 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001929
1930 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001932 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 dep->namelen = args->namelen;
1934 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001935 dp->d_ops->data_put_ftype(dep, args->filetype);
1936 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001937 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001938 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001939
1940 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001942 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001944 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001945
1946 /* If the freespace block entry is now wrong, update it. */
Dave Chinner0e822252019-08-29 09:04:07 -07001947 free = fbp->b_addr;
Dave Chinnera07258a2019-08-29 09:04:06 -07001948 bests = dp->d_ops->free_bests_p(free);
1949 if (bests[findex] != bf[0].length) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001950 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 logfree = 1;
1952 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001953
1954 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001956 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001957
1958 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001960 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962}
1963
1964/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07001965 * Top-level node form directory addname routine.
1966 */
1967int /* error */
1968xfs_dir2_node_addname(
1969 xfs_da_args_t *args) /* operation arguments */
1970{
1971 xfs_da_state_blk_t *blk; /* leaf block for insert */
1972 int error; /* error return value */
1973 int rval; /* sub-return value */
1974 xfs_da_state_t *state; /* btree cursor */
1975
1976 trace_xfs_dir2_node_addname(args);
1977
1978 /*
1979 * Allocate and initialize the state (btree cursor).
1980 */
1981 state = xfs_da_state_alloc();
1982 state->args = args;
1983 state->mp = args->dp->i_mount;
1984 /*
1985 * Look up the name. We're not supposed to find it, but
1986 * this gives us the insertion point.
1987 */
1988 error = xfs_da3_node_lookup_int(state, &rval);
1989 if (error)
1990 rval = error;
1991 if (rval != -ENOENT) {
1992 goto done;
1993 }
1994 /*
1995 * Add the data entry to a data block.
1996 * Extravalid is set to a freeblock found by lookup.
1997 */
1998 rval = xfs_dir2_node_addname_int(args,
1999 state->extravalid ? &state->extrablk : NULL);
2000 if (rval) {
2001 goto done;
2002 }
2003 blk = &state->path.blk[state->path.active - 1];
2004 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2005 /*
2006 * Add the new leaf entry.
2007 */
2008 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2009 if (rval == 0) {
2010 /*
2011 * It worked, fix the hash values up the btree.
2012 */
2013 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2014 xfs_da3_fixhashpath(state, &state->path);
2015 } else {
2016 /*
2017 * It didn't work, we need to split the leaf block.
2018 */
2019 if (args->total == 0) {
2020 ASSERT(rval == -ENOSPC);
2021 goto done;
2022 }
2023 /*
2024 * Split the leaf block and insert the new entry.
2025 */
2026 rval = xfs_da3_split(state);
2027 }
2028done:
2029 xfs_da_state_free(state);
2030 return rval;
2031}
2032
2033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002035 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * The only real output is the inode number of the entry.
2037 */
2038int /* error */
2039xfs_dir2_node_lookup(
2040 xfs_da_args_t *args) /* operation arguments */
2041{
2042 int error; /* error return value */
2043 int i; /* btree level */
2044 int rval; /* operation return value */
2045 xfs_da_state_t *state; /* btree cursor */
2046
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002047 trace_xfs_dir2_node_lookup(args);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /*
2050 * Allocate and initialize the btree cursor.
2051 */
2052 state = xfs_da_state_alloc();
2053 state->args = args;
2054 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
2056 * Fill in the path to the entry in the cursor.
2057 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002058 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (error)
2060 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002061 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2062 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002063 xfs_dir2_data_entry_t *dep;
2064
Dave Chinner1d9025e2012-06-22 18:50:14 +10002065 dep = (xfs_dir2_data_entry_t *)
2066 ((char *)state->extrablk.bp->b_addr +
2067 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002068 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Release the btree blocks and leaf block.
2072 */
2073 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002074 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 state->path.blk[i].bp = NULL;
2076 }
2077 /*
2078 * Release the data block if we have it.
2079 */
2080 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002081 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 state->extrablk.bp = NULL;
2083 }
2084 xfs_da_state_free(state);
2085 return rval;
2086}
2087
2088/*
2089 * Remove an entry from a node-format directory.
2090 */
2091int /* error */
2092xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002093 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002095 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int error; /* error return value */
2097 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002098 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002100 trace_xfs_dir2_node_removename(args);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /*
2103 * Allocate and initialize the btree cursor.
2104 */
2105 state = xfs_da_state_alloc();
2106 state->args = args;
2107 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002108
2109 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002110 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002111 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002112 goto out_free;
2113
2114 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002115 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002116 error = rval;
2117 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 blk = &state->path.blk[state->path.active - 1];
2121 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2122 ASSERT(state->extravalid);
2123 /*
2124 * Remove the leaf and data entries.
2125 * Extrablk refers to the data block.
2126 */
2127 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2128 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002129 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002130 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * Fix the hash values up the btree.
2133 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002134 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * If we need to join leaf blocks, do it.
2137 */
2138 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002139 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * If no errors so far, try conversion to leaf format.
2142 */
2143 if (!error)
2144 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002145out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 xfs_da_state_free(state);
2147 return error;
2148}
2149
2150/*
2151 * Replace an entry's inode number in a node-format directory.
2152 */
2153int /* error */
2154xfs_dir2_node_replace(
2155 xfs_da_args_t *args) /* operation arguments */
2156{
2157 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002158 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 xfs_dir2_data_entry_t *dep; /* data entry changed */
2160 int error; /* error return value */
2161 int i; /* btree level */
2162 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002163 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 xfs_dir2_leaf_t *leaf; /* leaf structure */
2165 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2166 int rval; /* internal return value */
2167 xfs_da_state_t *state; /* btree cursor */
2168
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002169 trace_xfs_dir2_node_replace(args);
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /*
2172 * Allocate and initialize the btree cursor.
2173 */
2174 state = xfs_da_state_alloc();
2175 state->args = args;
2176 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002177
2178 /*
2179 * We have to save new inode number and ftype since
2180 * xfs_da3_node_lookup_int() is going to overwrite them
2181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002183 ftype = args->filetype;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * Lookup the entry to change in the btree.
2187 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002188 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (error) {
2190 rval = error;
2191 }
2192 /*
2193 * It should be found, since the vnodeops layer has looked it up
2194 * and locked it. But paranoia is good.
2195 */
Dave Chinner24513372014-06-25 14:58:08 +10002196 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002197 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Find the leaf entry.
2200 */
2201 blk = &state->path.blk[state->path.active - 1];
2202 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002203 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002204 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002205 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 ASSERT(state->extravalid);
2207 /*
2208 * Point to the data entry.
2209 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002210 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002211 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2212 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002214 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002215 xfs_dir2_dataptr_to_off(args->geo,
2216 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002217 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Fill in the new inode number and log the entry.
2220 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002221 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002222 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002223 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 rval = 0;
2225 }
2226 /*
2227 * Didn't find it, and we're holding a data block. Drop it.
2228 */
2229 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002230 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 state->extrablk.bp = NULL;
2232 }
2233 /*
2234 * Release all the buffers in the cursor.
2235 */
2236 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002237 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 state->path.blk[i].bp = NULL;
2239 }
2240 xfs_da_state_free(state);
2241 return rval;
2242}
2243
2244/*
2245 * Trim off a trailing empty freespace block.
2246 * Return (in rvalp) 1 if we did it, 0 if not.
2247 */
2248int /* error */
2249xfs_dir2_node_trim_free(
2250 xfs_da_args_t *args, /* operation arguments */
2251 xfs_fileoff_t fo, /* free block number */
2252 int *rvalp) /* out: did something */
2253{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002254 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 xfs_inode_t *dp; /* incore directory inode */
2256 int error; /* error return code */
2257 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002259 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002263
2264 *rvalp = 0;
2265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * Read the freespace block.
2268 */
Dave Chinner20252072012-11-12 22:54:13 +11002269 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002270 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /*
2273 * There can be holes in freespace. If fo is a hole, there's
2274 * nothing to do.
2275 */
Dave Chinner20252072012-11-12 22:54:13 +11002276 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002278 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002279 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /*
2282 * If there are used entries, there's nothing to do.
2283 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002284 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002285 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 0;
2287 }
2288 /*
2289 * Blow the block away.
2290 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002291 error = xfs_dir2_shrink_inode(args,
2292 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2293 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /*
2295 * Can't fail with ENOSPC since that only happens with no
2296 * space reservation, when breaking up an extent into two
2297 * pieces. This is the last block of an extent.
2298 */
Dave Chinner24513372014-06-25 14:58:08 +10002299 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002300 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return error;
2302 }
2303 /*
2304 * Return that we succeeded.
2305 */
2306 *rvalp = 1;
2307 return 0;
2308}