blob: 93254f45a5f95eb6d50febe7afd2859244c6fab2 [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
Dave Chinner01ba43b2013-10-29 22:11:52 +110048 dp->d_ops->leaf_hdr_from_disk(&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) >
376 (uint)dp->i_d.di_size / args->geo->blksize)
377 return -EFSCORRUPTED;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * Copy freespace entries from the leaf block to the new block.
381 * Count active entries.
382 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100383 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500384 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100385 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100386 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100388 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100392 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100394 freehdr.nused = n;
395 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
396
Dave Chinner01ba43b2013-10-29 22:11:52 +1100397 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000398 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
399 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100400
Dave Chinner24df33b2013-04-12 07:30:21 +1000401 /*
402 * Converting the leaf to a leafnode is just a matter of changing the
403 * magic number and the ops. Do the change directly to the buffer as
404 * it's less work (and less code) than decoding the header to host
405 * format and back again.
406 */
407 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
408 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
409 else
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
411 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100412 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000413 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100414 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416}
417
418/*
419 * Add a leaf entry to a leaf block in a node-form directory.
420 * The other work necessary is done from the caller.
421 */
422static int /* error */
423xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000424 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800425 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int index) /* insertion pt for new entry */
427{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800428 struct xfs_dir3_icleaf_hdr leafhdr;
429 struct xfs_inode *dp = args->dp;
430 struct xfs_dir2_leaf *leaf = bp->b_addr;
431 struct xfs_dir2_leaf_entry *lep;
432 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800434 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int lfloghigh; /* high leaf entry logging */
436 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800437 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000439 trace_xfs_dir2_leafn_add(args, index);
440
Dave Chinner01ba43b2013-10-29 22:11:52 +1100441 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100442 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
445 * Quick check just to make sure we are not going to index
446 * into other peoples memory
447 */
448 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000449 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * If there are already the maximum number of leaf entries in
453 * the block, if there are no stale entries it won't fit.
454 * Caller will do a split. If there are stale entries we'll do
455 * a compact.
456 */
457
Dave Chinner8f661932014-06-06 15:15:59 +1000458 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000459 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000460 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000461 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else
463 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000464 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
465 ASSERT(index == leafhdr.count ||
466 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Barry Naujok6a178102008-05-21 16:42:05 +1000468 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 0;
470
471 /*
472 * Compact out all but one stale leaf entry. Leaves behind
473 * the entry closest to index.
474 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000475 if (compact)
476 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
477 &highstale, &lfloglow, &lfloghigh);
478 else if (leafhdr.stale) {
479 /*
480 * Set impossible logging indices for this case.
481 */
482 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 lfloghigh = -1;
484 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Insert the new entry, log everything.
488 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000489 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200490 highstale, &lfloglow, &lfloghigh);
491
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100492 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000493 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100494 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000495
Dave Chinner01ba43b2013-10-29 22:11:52 +1100496 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000497 xfs_dir3_leaf_log_header(args, bp);
498 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100499 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
503#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100504static void
505xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100506 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100507 struct xfs_buf *bp,
508 xfs_dir2_db_t db)
509{
510 struct xfs_dir3_icfree_hdr hdr;
511
Dave Chinner01ba43b2013-10-29 22:11:52 +1100512 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100513
Dave Chinner8f661932014-06-06 15:15:59 +1000514 ASSERT((hdr.firstdb %
515 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100516 ASSERT(hdr.firstdb <= db);
517 ASSERT(db < hdr.firstdb + hdr.nvalid);
518}
519#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100520#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#endif /* DEBUG */
522
523/*
524 * Return the last hash value in the leaf.
525 * Stale entries are ok.
526 */
527xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700528xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100529 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000530 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int *count) /* count of entries in leaf */
532{
Dave Chinner24df33b2013-04-12 07:30:21 +1000533 struct xfs_dir2_leaf *leaf = bp->b_addr;
534 struct xfs_dir2_leaf_entry *ents;
535 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dave Chinner01ba43b2013-10-29 22:11:52 +1100537 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000538
539 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700540 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
541 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
542 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000545 *count = leafhdr.count;
546 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000548
Dave Chinner41419562013-10-29 22:11:50 +1100549 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000550 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000554 * Look up a leaf entry for space to add a name in a node-format leaf block.
555 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000557STATIC int
558xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000559 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 xfs_da_args_t *args, /* operation arguments */
561 int *indexp, /* out: leaf entry index */
562 xfs_da_state_t *state) /* state to fill in */
563{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000564 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000565 xfs_dir2_db_t curdb = -1; /* current data block number */
566 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_inode_t *dp; /* incore directory inode */
568 int error; /* error return value */
569 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000570 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int index; /* leaf entry index */
572 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000573 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
575 xfs_mount_t *mp; /* filesystem mount point */
576 xfs_dir2_db_t newdb; /* new data block number */
577 xfs_dir2_db_t newfdb; /* new free block number */
578 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000579 struct xfs_dir2_leaf_entry *ents;
580 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 dp = args->dp;
583 tp = args->trans;
584 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000585 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100586 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100587 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000588
Dave Chinner41419562013-10-29 22:11:50 +1100589 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000590 ASSERT(leafhdr.count > 0);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * Look up the hash value in the leaf entries.
594 */
595 index = xfs_dir2_leaf_search_hash(args, bp);
596 /*
597 * Do we have a buffer coming in?
598 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 if (state->extravalid) {
600 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000602 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000603 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100604 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
605 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100607 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Loop over leaf entries with the right hash value.
610 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000611 for (lep = &ents[index];
612 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
613 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Skip stale leaf entries.
616 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100617 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 continue;
619 /*
620 * Pull the data block number from the entry.
621 */
Dave Chinner30028032014-06-06 15:08:18 +1000622 newdb = xfs_dir2_dataptr_to_db(args->geo,
623 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * For addname, we're looking for a place to put the new entry.
626 * We want to use a data block with an entry of equal
627 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000628 *
629 * If this block isn't the data block we already have
630 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000632 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100633 __be16 *bests;
634
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000637 * Convert the data block to the free block
638 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 */
Dave Chinner8f661932014-06-06 15:15:59 +1000640 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000649 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100650
651 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000652 xfs_dir2_db_to_da(args->geo,
653 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100654 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000655 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000657 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100658
Dave Chinner01ba43b2013-10-29 22:11:52 +1100659 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Dave Chinner8f661932014-06-06 15:15:59 +1000664 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000666 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500668 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100669 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000670 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
671 XFS_ERRLEVEL_LOW, mp);
672 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000674 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100677 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000681 /* Didn't find any space */
682 fi = -1;
683out:
Barry Naujok6a178102008-05-21 16:42:05 +1000684 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000685 if (curbp) {
686 /* Giving back a free block. */
687 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000689 state->extrablk.index = fi;
690 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100691
692 /*
693 * Important: this magic number is not in the buffer - it's for
694 * buffer type information and therefore only the free/data type
695 * matters here, not whether CRCs are enabled or not.
696 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000697 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
698 } else {
699 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000702 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
704 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000705 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000709 * Look up a leaf entry in a node-format leaf block.
710 * The extrablk in state a data block.
711 */
712STATIC int
713xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000714 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000715 xfs_da_args_t *args, /* operation arguments */
716 int *indexp, /* out: leaf entry index */
717 xfs_da_state_t *state) /* state to fill in */
718{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000719 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000720 xfs_dir2_db_t curdb = -1; /* current data block number */
721 xfs_dir2_data_entry_t *dep; /* data block entry */
722 xfs_inode_t *dp; /* incore directory inode */
723 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 int index; /* leaf entry index */
725 xfs_dir2_leaf_t *leaf; /* leaf structure */
726 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
727 xfs_mount_t *mp; /* filesystem mount point */
728 xfs_dir2_db_t newdb; /* new data block number */
729 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000730 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000731 struct xfs_dir2_leaf_entry *ents;
732 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000733
734 dp = args->dp;
735 tp = args->trans;
736 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000737 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100738 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100739 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000740
Dave Chinner41419562013-10-29 22:11:50 +1100741 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700742 if (leafhdr.count <= 0)
743 return -EFSCORRUPTED;
Dave Chinner24df33b2013-04-12 07:30:21 +1000744
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000745 /*
746 * Look up the hash value in the leaf entries.
747 */
748 index = xfs_dir2_leaf_search_hash(args, bp);
749 /*
750 * Do we have a buffer coming in?
751 */
752 if (state->extravalid) {
753 curbp = state->extrablk.bp;
754 curdb = state->extrablk.blkno;
755 }
756 /*
757 * Loop over leaf entries with the right hash value.
758 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000759 for (lep = &ents[index];
760 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
761 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000762 /*
763 * Skip stale leaf entries.
764 */
765 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
766 continue;
767 /*
768 * Pull the data block number from the entry.
769 */
Dave Chinner30028032014-06-06 15:08:18 +1000770 newdb = xfs_dir2_dataptr_to_db(args->geo,
771 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000772 /*
773 * Not adding a new entry, so we really want to find
774 * the name given to us.
775 *
776 * If it's a different data block, go get it.
777 */
778 if (newdb != curdb) {
779 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000780 * If we had a block before that we aren't saving
781 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000782 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000783 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
784 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000785 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000786 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000787 * If needing the block that is saved with a CI match,
788 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000789 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000790 if (args->cmpresult != XFS_CMP_DIFFERENT &&
791 newdb == state->extrablk.blkno) {
792 ASSERT(state->extravalid);
793 curbp = state->extrablk.bp;
794 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100795 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000796 xfs_dir2_db_to_da(args->geo,
797 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100798 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000799 if (error)
800 return error;
801 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100802 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000803 curdb = newdb;
804 }
805 /*
806 * Point to the data entry.
807 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000809 xfs_dir2_dataptr_to_off(args->geo,
810 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000811 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000812 * Compare the entry and if it's an exact match, return
813 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000814 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000815 */
Barry Naujok5163f952008-05-21 16:41:01 +1000816 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
817 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000818 /* If there is a CI match block, drop it */
819 if (args->cmpresult != XFS_CMP_DIFFERENT &&
820 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000821 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000822 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000823 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100824 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000825 *indexp = index;
826 state->extravalid = 1;
827 state->extrablk.bp = curbp;
828 state->extrablk.blkno = curdb;
829 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000830 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000831 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100832 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100833 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000834 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000835 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000836 }
837 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000838 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000839 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000840 if (args->cmpresult == XFS_CMP_DIFFERENT) {
841 /* Giving back last used data block. */
842 state->extravalid = 1;
843 state->extrablk.bp = curbp;
844 state->extrablk.index = -1;
845 state->extrablk.blkno = curdb;
846 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100847 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100848 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000849 } else {
850 /* If the curbp is not the CI match block, drop it */
851 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000852 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000853 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000854 } else {
855 state->extravalid = 0;
856 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000857 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000858 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000859}
860
861/*
862 * Look up a leaf entry in a node-format leaf block.
863 * If this is an addname then the extrablk in state is a freespace block,
864 * otherwise it's a data block.
865 */
866int
867xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000868 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000869 xfs_da_args_t *args, /* operation arguments */
870 int *indexp, /* out: leaf entry index */
871 xfs_da_state_t *state) /* state to fill in */
872{
Barry Naujok6a178102008-05-21 16:42:05 +1000873 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000874 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
875 state);
876 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
877}
878
879/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * Move count leaf entries from source to destination leaf.
881 * Log entries and headers. Stale entries are preserved.
882 */
883static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000884xfs_dir3_leafn_moveents(
885 xfs_da_args_t *args, /* operation arguments */
886 struct xfs_buf *bp_s, /* source */
887 struct xfs_dir3_icleaf_hdr *shdr,
888 struct xfs_dir2_leaf_entry *sents,
889 int start_s,/* source leaf index */
890 struct xfs_buf *bp_d, /* destination */
891 struct xfs_dir3_icleaf_hdr *dhdr,
892 struct xfs_dir2_leaf_entry *dents,
893 int start_d,/* destination leaf index */
894 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Dave Chinner24df33b2013-04-12 07:30:21 +1000896 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000898 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Silently return if nothing to do.
902 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000903 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * If the destination index is not the end of the current
908 * destination leaf entries, open up a hole in the destination
909 * to hold the new entries.
910 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000911 if (start_d < dhdr->count) {
912 memmove(&dents[start_d + count], &dents[start_d],
913 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000914 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000915 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 /*
918 * If the source has stale leaves, count the ones in the copy range
919 * so we can update the header correctly.
920 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000921 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int i; /* temp leaf index */
923
924 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000925 if (sents[i].address ==
926 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 stale++;
928 }
929 } else
930 stale = 0;
931 /*
932 * Copy the leaf entries from source to destination.
933 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000934 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000936 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * If there are source entries after the ones we copied,
940 * delete the ones we copied by sliding the next ones down.
941 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000942 if (start_s + count < shdr->count) {
943 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000945 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /*
949 * Update the headers and log them.
950 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000951 shdr->count -= count;
952 shdr->stale -= stale;
953 dhdr->count += count;
954 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * Determine the sort order of two leaf blocks.
959 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
960 */
961int /* sort order */
962xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100963 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000964 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
965 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Dave Chinner24df33b2013-04-12 07:30:21 +1000967 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
968 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
969 struct xfs_dir2_leaf_entry *ents1;
970 struct xfs_dir2_leaf_entry *ents2;
971 struct xfs_dir3_icleaf_hdr hdr1;
972 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Chinner01ba43b2013-10-29 22:11:52 +1100974 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
975 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100976 ents1 = dp->d_ops->leaf_ents_p(leaf1);
977 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000978
979 if (hdr1.count > 0 && hdr2.count > 0 &&
980 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
981 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
982 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return 1;
984 return 0;
985}
986
987/*
988 * Rebalance leaf entries between two leaf blocks.
989 * This is actually only called when the second block is new,
990 * though the code deals with the general case.
991 * A new entry will be inserted in one of the blocks, and that
992 * entry is taken into account when balancing.
993 */
994static void
995xfs_dir2_leafn_rebalance(
996 xfs_da_state_t *state, /* btree cursor */
997 xfs_da_state_blk_t *blk1, /* first btree block */
998 xfs_da_state_blk_t *blk2) /* second btree block */
999{
1000 xfs_da_args_t *args; /* operation arguments */
1001 int count; /* count (& direction) leaves */
1002 int isleft; /* new goes in left leaf */
1003 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1004 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1005 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001006#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int oldstale; /* old count of stale leaves */
1008#endif
1009 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001010 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001011 struct xfs_dir2_leaf_entry *ents1;
1012 struct xfs_dir2_leaf_entry *ents2;
1013 struct xfs_dir3_icleaf_hdr hdr1;
1014 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001015 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 args = state->args;
1018 /*
1019 * If the block order is wrong, swap the arguments.
1020 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001021 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1022 if (swap_blocks)
1023 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dave Chinner1d9025e2012-06-22 18:50:14 +10001025 leaf1 = blk1->bp->b_addr;
1026 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001027 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1028 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001029 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1030 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001031
1032 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001033#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001034 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035#endif
1036 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * If the old leaf count was odd then the new one will be even,
1040 * so we need to divide the new count evenly.
1041 */
1042 if (oldsum & 1) {
1043 xfs_dahash_t midhash; /* middle entry hash value */
1044
Dave Chinner24df33b2013-04-12 07:30:21 +10001045 if (mid >= hdr1.count)
1046 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001048 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 isleft = args->hashval <= midhash;
1050 }
1051 /*
1052 * If the old count is even then the new count is odd, so there's
1053 * no preferred side for the new entry.
1054 * Pick the left one.
1055 */
1056 else
1057 isleft = 1;
1058 /*
1059 * Calculate moved entry count. Positive means left-to-right,
1060 * negative means right-to-left. Then move the entries.
1061 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001062 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001064 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1065 hdr1.count - count, blk2->bp,
1066 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001068 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1069 blk1->bp, &hdr1, ents1,
1070 hdr1.count, count);
1071
1072 ASSERT(hdr1.count + hdr2.count == oldsum);
1073 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1074
1075 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001076 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1077 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001078 xfs_dir3_leaf_log_header(args, blk1->bp);
1079 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001080
Dave Chinner41419562013-10-29 22:11:50 +11001081 xfs_dir3_leaf_check(dp, blk1->bp);
1082 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Mark whether we're inserting into the old or new leaf.
1086 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001087 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001088 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001089 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001090 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001092 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Adjust the expected index for insertion.
1095 */
1096 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001097 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001098
1099 /*
1100 * Finally sanity check just to make sure we are not returning a
1101 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Dave Chinner41419562013-10-29 22:11:50 +11001103 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 state->inleaf = 1;
1105 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001106 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001107 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001108 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110}
1111
Dave Chinner20252072012-11-12 22:54:13 +11001112static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001113xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001114 xfs_da_args_t *args,
1115 struct xfs_dir2_data_hdr *hdr,
1116 struct xfs_dir2_free *free,
1117 xfs_dir2_db_t fdb,
1118 int findex,
1119 struct xfs_buf *fbp,
1120 int longest)
1121{
Dave Chinner20252072012-11-12 22:54:13 +11001122 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001123 __be16 *bests;
1124 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001125 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001126
Dave Chinner01ba43b2013-10-29 22:11:52 +11001127 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001128 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001129 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001130 /*
1131 * Data block is not empty, just set the free entry to the new
1132 * value.
1133 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001134 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001135 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001136 return 0;
1137 }
1138
1139 /* One less used entry in the free table. */
1140 freehdr.nused--;
1141
1142 /*
1143 * If this was the last entry in the table, we can trim the table size
1144 * back. There might be other entries at the end referring to
1145 * non-existent data blocks, get those too.
1146 */
1147 if (findex == freehdr.nvalid - 1) {
1148 int i; /* free entry index */
1149
1150 for (i = findex - 1; i >= 0; i--) {
1151 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1152 break;
1153 }
1154 freehdr.nvalid = i + 1;
1155 logfree = 0;
1156 } else {
1157 /* Not the last entry, just punch it out. */
1158 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001159 logfree = 1;
1160 }
1161
Dave Chinner01ba43b2013-10-29 22:11:52 +11001162 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001163 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001164
1165 /*
1166 * If there are no useful entries left in the block, get rid of the
1167 * block if we can.
1168 */
1169 if (!freehdr.nused) {
1170 int error;
1171
1172 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1173 if (error == 0) {
1174 fbp = NULL;
1175 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001176 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001177 return error;
1178 /*
1179 * It's possible to get ENOSPC if there is no
1180 * space reservation. In this case some one
1181 * else will eventually get rid of this block.
1182 */
1183 }
1184
Dave Chinner20252072012-11-12 22:54:13 +11001185 /* Log the free entry that changed, unless we got rid of it. */
1186 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001187 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001188 return 0;
1189}
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*
1192 * Remove an entry from a node directory.
1193 * This removes the leaf entry and the data entry,
1194 * and updates the free block if necessary.
1195 */
1196static int /* error */
1197xfs_dir2_leafn_remove(
1198 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001199 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int index, /* leaf entry index */
1201 xfs_da_state_blk_t *dblk, /* data block */
1202 int *rval) /* resulting block needs join */
1203{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001204 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001206 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 xfs_dir2_data_entry_t *dep; /* data block entry */
1208 xfs_inode_t *dp; /* incore directory inode */
1209 xfs_dir2_leaf_t *leaf; /* leaf structure */
1210 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1211 int longest; /* longest data free entry */
1212 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int needlog; /* need to log data header */
1214 int needscan; /* need to rescan data frees */
1215 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001216 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001217 struct xfs_dir3_icleaf_hdr leafhdr;
1218 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001220 trace_xfs_dir2_leafn_remove(args, index);
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 dp = args->dp;
1223 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001224 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001225 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001226 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * Point to the entry we're removing.
1230 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001231 lep = &ents[index];
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Extract the data block and offset from the entry.
1235 */
Dave Chinner30028032014-06-06 15:08:18 +10001236 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001238 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /*
1242 * Kill the leaf entry by marking it stale.
1243 * Log the leaf block changes.
1244 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001245 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001246 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001247 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001248
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001249 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001250 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /*
1253 * Make the data entry free. Keep track of the longest freespace
1254 * in the data block in case it changes.
1255 */
1256 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001257 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001258 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001259 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001260 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001262 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001263 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
1265 * Rescan the data block freespaces for bestfree.
1266 * Log the data block header if needed.
1267 */
1268 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001269 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001271 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001272 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * If the longest data block freespace changes, need to update
1275 * the corresponding freeblock entry.
1276 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001277 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001279 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 xfs_dir2_db_t fdb; /* freeblock block number */
1281 int findex; /* index in freeblock entries */
1282 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /*
1285 * Convert the data block number to a free block,
1286 * read in the free block.
1287 */
Dave Chinner8f661932014-06-06 15:15:59 +10001288 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001289 error = xfs_dir2_free_read(tp, dp,
1290 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001291 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001292 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001294 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001295#ifdef DEBUG
1296 {
1297 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001298 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001299 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001300 (fdb - xfs_dir2_byte_to_db(args->geo,
1301 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001302 }
1303#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Calculate which entry we need to fix.
1306 */
Dave Chinner8f661932014-06-06 15:15:59 +10001307 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001308 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * If the data block is now empty we can get rid of it
1311 * (usually).
1312 */
Dave Chinner8f661932014-06-06 15:15:59 +10001313 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001314 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Try to punch out the data block.
1317 */
1318 error = xfs_dir2_shrink_inode(args, db, dbp);
1319 if (error == 0) {
1320 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001321 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323 /*
1324 * We can get ENOSPC if there's no space reservation.
1325 * In this case just drop the buffer and some one else
1326 * will eventually get rid of the empty block.
1327 */
Dave Chinner24513372014-06-25 14:58:08 +10001328 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return error;
1330 }
1331 /*
1332 * If we got rid of the data block, we can eliminate that entry
1333 * in the free block.
1334 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001335 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001336 fdb, findex, fbp, longest);
1337 if (error)
1338 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Dave Chinner20252072012-11-12 22:54:13 +11001340
Dave Chinner41419562013-10-29 22:11:50 +11001341 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001343 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * to justify trying to join it with a neighbor.
1345 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001346 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001347 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001348 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}
1351
1352/*
1353 * Split the leaf entries in the old block into old and new blocks.
1354 */
1355int /* error */
1356xfs_dir2_leafn_split(
1357 xfs_da_state_t *state, /* btree cursor */
1358 xfs_da_state_blk_t *oldblk, /* original block */
1359 xfs_da_state_blk_t *newblk) /* newly created block */
1360{
1361 xfs_da_args_t *args; /* operation arguments */
1362 xfs_dablk_t blkno; /* new leaf block number */
1363 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001364 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /*
1367 * Allocate space for a new leaf node.
1368 */
1369 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001370 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1372 error = xfs_da_grow_inode(args, &blkno);
1373 if (error) {
1374 return error;
1375 }
1376 /*
1377 * Initialize the new leaf block.
1378 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001379 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001380 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 newblk->blkno = blkno;
1385 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1386 /*
1387 * Rebalance the entries across the two leaves, link the new
1388 * block into the leaves.
1389 */
1390 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001391 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (error) {
1393 return error;
1394 }
1395 /*
1396 * Insert the new entry in the correct block.
1397 */
1398 if (state->inleaf)
1399 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1400 else
1401 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1402 /*
1403 * Update last hashval in each block since we added the name.
1404 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001405 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1406 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001407 xfs_dir3_leaf_check(dp, oldblk->bp);
1408 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return error;
1410}
1411
1412/*
1413 * Check a leaf block and its neighbors to see if the block should be
1414 * collapsed into one or the other neighbor. Always keep the block
1415 * with the smaller block number.
1416 * If the current block is over 50% full, don't try to join it, return 0.
1417 * If the block is empty, fill in the state structure and return 2.
1418 * If it can be collapsed, fill in the state structure and return 1.
1419 * If nothing can be done, return 0.
1420 */
1421int /* error */
1422xfs_dir2_leafn_toosmall(
1423 xfs_da_state_t *state, /* btree cursor */
1424 int *action) /* resulting action to take */
1425{
1426 xfs_da_state_blk_t *blk; /* leaf block */
1427 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001428 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int bytes; /* bytes in use */
1430 int count; /* leaf live entry count */
1431 int error; /* error return value */
1432 int forward; /* sibling block direction */
1433 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 xfs_dir2_leaf_t *leaf; /* leaf structure */
1435 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001436 struct xfs_dir3_icleaf_hdr leafhdr;
1437 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001438 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /*
1441 * Check for the degenerate case of the block being over 50% full.
1442 * If so, it's not worth even looking to see if we might be able
1443 * to coalesce with a sibling.
1444 */
1445 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001446 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001447 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001448 ents = dp->d_ops->leaf_ents_p(leaf);
1449 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001450
1451 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001452 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001453 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /*
1455 * Blk over 50%, don't try to join.
1456 */
1457 *action = 0;
1458 return 0;
1459 }
1460 /*
1461 * Check for the degenerate case of the block being empty.
1462 * If the block is empty, we'll simply delete it, no need to
1463 * coalesce it with a sibling block. We choose (arbitrarily)
1464 * to merge with the forward block unless it is NULL.
1465 */
1466 if (count == 0) {
1467 /*
1468 * Make altpath point to the block we want to keep and
1469 * path point to the block we want to drop (this one).
1470 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001471 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001473 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 &rval);
1475 if (error)
1476 return error;
1477 *action = rval ? 2 : 0;
1478 return 0;
1479 }
1480 /*
1481 * Examine each sibling block to see if we can coalesce with
1482 * at least 25% free space to spare. We need to figure out
1483 * whether to merge with the forward or the backward block.
1484 * We prefer coalescing with the lower numbered sibling so as
1485 * to shrink a directory over time.
1486 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001487 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001489 struct xfs_dir3_icleaf_hdr hdr2;
1490
1491 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (blkno == 0)
1493 continue;
1494 /*
1495 * Read the sibling leaf block.
1496 */
Dave Chinner41419562013-10-29 22:11:50 +11001497 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001498 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001499 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /*
1503 * Count bytes in the two blocks combined.
1504 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001505 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001506 bytes = state->args->geo->blksize -
1507 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001508
Dave Chinner1d9025e2012-06-22 18:50:14 +10001509 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001510 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001511 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001512 count += hdr2.count - hdr2.stale;
1513 bytes -= count * sizeof(ents[0]);
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 /*
1516 * Fits with at least 25% to spare.
1517 */
1518 if (bytes >= 0)
1519 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001520 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522 /*
1523 * Didn't like either block, give up.
1524 */
1525 if (i >= 2) {
1526 *action = 0;
1527 return 0;
1528 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * Make altpath point to the block we want to keep (the lower
1532 * numbered block) and path point to the block we want to drop.
1533 */
1534 memcpy(&state->altpath, &state->path, sizeof(state->path));
1535 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001536 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 &rval);
1538 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001539 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 &rval);
1541 if (error) {
1542 return error;
1543 }
1544 *action = rval ? 0 : 1;
1545 return 0;
1546}
1547
1548/*
1549 * Move all the leaf entries from drop_blk to save_blk.
1550 * This is done as part of a join operation.
1551 */
1552void
1553xfs_dir2_leafn_unbalance(
1554 xfs_da_state_t *state, /* cursor */
1555 xfs_da_state_blk_t *drop_blk, /* dead block */
1556 xfs_da_state_blk_t *save_blk) /* surviving block */
1557{
1558 xfs_da_args_t *args; /* operation arguments */
1559 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1560 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001561 struct xfs_dir3_icleaf_hdr savehdr;
1562 struct xfs_dir3_icleaf_hdr drophdr;
1563 struct xfs_dir2_leaf_entry *sents;
1564 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001565 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 args = state->args;
1568 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1569 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001570 drop_leaf = drop_blk->bp->b_addr;
1571 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001572
Dave Chinner01ba43b2013-10-29 22:11:52 +11001573 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1574 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1575 sents = dp->d_ops->leaf_ents_p(save_leaf);
1576 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * If there are any stale leaf entries, take this opportunity
1580 * to purge them.
1581 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001582 if (drophdr.stale)
1583 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1584 if (savehdr.stale)
1585 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Move the entries from drop to the appropriate end of save.
1589 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001590 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001591 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001592 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1593 save_blk->bp, &savehdr, sents, 0,
1594 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001596 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1597 save_blk->bp, &savehdr, sents,
1598 savehdr.count, drophdr.count);
1599 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1600
1601 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001602 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1603 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001604 xfs_dir3_leaf_log_header(args, save_blk->bp);
1605 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001606
Dave Chinner41419562013-10-29 22:11:50 +11001607 xfs_dir3_leaf_check(dp, save_blk->bp);
1608 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
1611/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001612 * Add a new data block to the directory at the free space index that the caller
1613 * has specified.
1614 */
1615static int
1616xfs_dir2_node_add_datablk(
1617 struct xfs_da_args *args,
1618 struct xfs_da_state_blk *fblk,
1619 xfs_dir2_db_t *dbno,
1620 struct xfs_buf **dbpp,
1621 struct xfs_buf **fbpp,
1622 int *findex)
1623{
1624 struct xfs_inode *dp = args->dp;
1625 struct xfs_trans *tp = args->trans;
1626 struct xfs_mount *mp = dp->i_mount;
1627 struct xfs_dir3_icfree_hdr freehdr;
1628 struct xfs_dir2_data_free *bf;
1629 struct xfs_dir2_data_hdr *hdr;
1630 struct xfs_dir2_free *free = NULL;
1631 xfs_dir2_db_t fbno;
1632 struct xfs_buf *fbp;
1633 struct xfs_buf *dbp;
1634 __be16 *bests = NULL;
1635 int error;
1636
1637 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001638 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001639 return -ENOSPC;
1640
1641 /* Allocate and initialize the new data block. */
1642 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1643 if (error)
1644 return error;
1645 error = xfs_dir3_data_init(args, *dbno, &dbp);
1646 if (error)
1647 return error;
1648
1649 /*
1650 * Get the freespace block corresponding to the data block
1651 * that was just allocated.
1652 */
1653 fbno = dp->d_ops->db_to_fdb(args->geo, *dbno);
1654 error = xfs_dir2_free_try_read(tp, dp,
1655 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1656 if (error)
1657 return error;
1658
1659 /*
1660 * If there wasn't a freespace block, the read will
1661 * return a NULL fbp. Allocate and initialize a new one.
1662 */
1663 if (!fbp) {
1664 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1665 if (error)
1666 return error;
1667
1668 if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) {
1669 xfs_alert(mp,
1670"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1671 __func__, (unsigned long long)dp->i_ino,
1672 (long long)dp->d_ops->db_to_fdb(args->geo, *dbno),
1673 (long long)*dbno, (long long)fbno);
1674 if (fblk) {
1675 xfs_alert(mp,
1676 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1677 fblk, (unsigned long long)fblk->blkno,
1678 fblk->index, fblk->magic);
1679 } else {
1680 xfs_alert(mp, " ... fblk is NULL");
1681 }
1682 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1683 return -EFSCORRUPTED;
1684 }
1685
1686 /* Get a buffer for the new block. */
1687 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1688 if (error)
1689 return error;
1690 free = fbp->b_addr;
1691 bests = dp->d_ops->free_bests_p(free);
1692 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1693
1694 /* Remember the first slot as our empty slot. */
1695 freehdr.firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1696 XFS_DIR2_FREE_OFFSET)) *
1697 dp->d_ops->free_max_bests(args->geo);
1698 } else {
1699 free = fbp->b_addr;
1700 bests = dp->d_ops->free_bests_p(free);
1701 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1702 }
1703
1704 /* Set the freespace block index from the data block number. */
1705 *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno);
1706
1707 /* Extend the freespace table if the new data block is off the end. */
1708 if (*findex >= freehdr.nvalid) {
1709 ASSERT(*findex < dp->d_ops->free_max_bests(args->geo));
1710 freehdr.nvalid = *findex + 1;
1711 bests[*findex] = cpu_to_be16(NULLDATAOFF);
1712 }
1713
1714 /*
1715 * If this entry was for an empty data block (this should always be
1716 * true) then update the header.
1717 */
1718 if (bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1719 freehdr.nused++;
1720 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1721 xfs_dir2_free_log_header(args, fbp);
1722 }
1723
1724 /* Update the freespace value for the new block in the table. */
1725 hdr = dbp->b_addr;
1726 bf = dp->d_ops->data_bestfree_p(hdr);
1727 bests[*findex] = bf[0].length;
1728
1729 *dbpp = dbp;
1730 *fbpp = fbp;
1731 return 0;
1732}
1733
Dave Chinner0e822252019-08-29 09:04:07 -07001734static int
1735xfs_dir2_node_find_freeblk(
1736 struct xfs_da_args *args,
1737 struct xfs_da_state_blk *fblk,
1738 xfs_dir2_db_t *dbnop,
1739 struct xfs_buf **fbpp,
1740 int *findexp,
1741 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001743 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001744 struct xfs_dir2_free *free = NULL;
1745 struct xfs_inode *dp = args->dp;
1746 struct xfs_trans *tp = args->trans;
1747 struct xfs_buf *fbp = NULL;
1748 xfs_dir2_db_t lastfbno;
1749 xfs_dir2_db_t ifbno = -1;
1750 xfs_dir2_db_t dbno = -1;
1751 xfs_dir2_db_t fbno = -1;
1752 xfs_fileoff_t fo;
1753 __be16 *bests;
1754 int findex;
1755 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * If we came in with a freespace block that means that lookup
1759 * found an entry with our hash value. This is the freespace
1760 * block for that data entry.
1761 */
1762 if (fblk) {
1763 fbp = fblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001764 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 findex = fblk->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001767 /* caller already found the freespace for us. */
1768 bests = dp->d_ops->free_bests_p(free);
1769 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1770
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001771 ASSERT(findex < freehdr.nvalid);
1772 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1773 ASSERT(be16_to_cpu(bests[findex]) >= length);
1774 dbno = freehdr.firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001775 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Dave Chinner0e822252019-08-29 09:04:07 -07001777
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001778 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001779 * The data block looked at didn't have enough room.
1780 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001781 */
Dave Chinner0e822252019-08-29 09:04:07 -07001782 ifbno = fblk->blkno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 fbno = ifbno;
1784 }
Dave Chinner0e822252019-08-29 09:04:07 -07001785 ASSERT(dbno == -1);
1786 findex = 0;
1787
1788 /*
1789 * If we don't have a data block yet, we're going to scan the freespace
1790 * blocks looking for one. Figure out what the highest freespace block
1791 * number is.
1792 */
1793 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1794 if (error)
1795 return error;
1796 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
1797
1798 /* If we haven't get a search start block, set it now */
1799 if (fbno == -1)
1800 fbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 /*
1803 * While we haven't identified a data block, search the freeblock
1804 * data for a good data block. If we find a null freeblock entry,
1805 * indicating a hole in the data blocks, remember that.
1806 */
1807 while (dbno == -1) {
1808 /*
1809 * If we don't have a freeblock in hand, get the next one.
1810 */
1811 if (fbp == NULL) {
1812 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * If it's ifbno we already looked at it.
1814 */
Dave Chinner0e822252019-08-29 09:04:07 -07001815 if (++fbno == ifbno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 fbno++;
1817 /*
1818 * If it's off the end we're done.
1819 */
1820 if (fbno >= lastfbno)
1821 break;
1822 /*
1823 * Read the block. There can be holes in the
1824 * freespace blocks, so this might not succeed.
1825 * This should be really rare, so there's no reason
1826 * to avoid it.
1827 */
Dave Chinner20252072012-11-12 22:54:13 +11001828 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001829 xfs_dir2_db_to_da(args->geo, fbno),
1830 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001831 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001833 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001835 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 findex = 0;
1837 }
1838 /*
1839 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001840 *
1841 * The bests initialisation should be where the bufer is read in
1842 * the above branch. But gcc is too stupid to realise that bests
1843 * and the freehdr are actually initialised if they are placed
1844 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001846 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001847 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001848 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1849 be16_to_cpu(bests[findex]) >= length)
1850 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 else {
1852 /*
1853 * Are we done with the freeblock?
1854 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001855 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
1857 * Drop the block.
1858 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001859 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 fbp = NULL;
1861 if (fblk && fblk->bp)
1862 fblk->bp = NULL;
1863 }
1864 }
1865 }
Dave Chinner0e822252019-08-29 09:04:07 -07001866found_block:
1867 *dbnop = dbno;
1868 *fbpp = fbp;
1869 *findexp = findex;
1870 return 0;
1871}
1872
1873
1874/*
1875 * Add the data entry for a node-format directory name addition.
1876 * The leaf entry is added in xfs_dir2_leafn_add.
1877 * We may enter with a freespace block that the lookup found.
1878 */
1879static int
1880xfs_dir2_node_addname_int(
1881 struct xfs_da_args *args, /* operation arguments */
1882 struct xfs_da_state_blk *fblk) /* optional freespace block */
1883{
1884 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1885 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1886 struct xfs_dir2_data_hdr *hdr; /* data block header */
1887 struct xfs_dir2_data_free *bf;
1888 struct xfs_dir2_free *free = NULL; /* freespace block structure */
1889 struct xfs_trans *tp = args->trans;
1890 struct xfs_inode *dp = args->dp;
1891 struct xfs_buf *dbp; /* data block buffer */
1892 struct xfs_buf *fbp; /* freespace buffer */
1893 xfs_dir2_data_aoff_t aoff;
1894 xfs_dir2_db_t dbno; /* data block number */
1895 int error; /* error return value */
1896 int findex; /* freespace entry index */
1897 int length; /* length of the new entry */
1898 int logfree = 0; /* need to log free entry */
1899 int needlog = 0; /* need to log data header */
1900 int needscan = 0; /* need to rescan data frees */
1901 __be16 *tagp; /* data entry tag pointer */
1902 __be16 *bests;
1903
1904 length = dp->d_ops->data_entsize(args->namelen);
1905 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &findex,
1906 length);
1907 if (error)
1908 return error;
1909
1910 /*
1911 * Now we know if we must allocate blocks, so if we are checking whether
1912 * we can insert without allocation then we can return now.
1913 */
1914 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1915 if (dbno == -1)
1916 return -ENOSPC;
1917 return 0;
1918 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * If we don't have a data block, we need to allocate one and make
1922 * the freespace entries refer to it.
1923 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001924 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001925 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001927 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1928 &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001929 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001930 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001931 error = xfs_dir3_data_read(tp, dp,
1932 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001933 -1, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Dave Chinner0e822252019-08-29 09:04:07 -07001935 if (error)
1936 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001937
1938 /* setup for data block up now */
1939 hdr = dbp->b_addr;
1940 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001941 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001942
1943 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001945 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001946
1947 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001948 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1949 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1950 &needlog, &needscan);
1951 if (error) {
1952 xfs_trans_brelse(tp, dbp);
1953 return error;
1954 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001955
1956 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001958 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 dep->namelen = args->namelen;
1960 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001961 dp->d_ops->data_put_ftype(dep, args->filetype);
1962 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001963 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001964 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001965
1966 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001968 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001970 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001971
1972 /* If the freespace block entry is now wrong, update it. */
Dave Chinner0e822252019-08-29 09:04:07 -07001973 free = fbp->b_addr;
Dave Chinnera07258a2019-08-29 09:04:06 -07001974 bests = dp->d_ops->free_bests_p(free);
1975 if (bests[findex] != bf[0].length) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001976 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 logfree = 1;
1978 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001979
1980 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001982 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001983
1984 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001986 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 return 0;
1988}
1989
1990/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07001991 * Top-level node form directory addname routine.
1992 */
1993int /* error */
1994xfs_dir2_node_addname(
1995 xfs_da_args_t *args) /* operation arguments */
1996{
1997 xfs_da_state_blk_t *blk; /* leaf block for insert */
1998 int error; /* error return value */
1999 int rval; /* sub-return value */
2000 xfs_da_state_t *state; /* btree cursor */
2001
2002 trace_xfs_dir2_node_addname(args);
2003
2004 /*
2005 * Allocate and initialize the state (btree cursor).
2006 */
2007 state = xfs_da_state_alloc();
2008 state->args = args;
2009 state->mp = args->dp->i_mount;
2010 /*
2011 * Look up the name. We're not supposed to find it, but
2012 * this gives us the insertion point.
2013 */
2014 error = xfs_da3_node_lookup_int(state, &rval);
2015 if (error)
2016 rval = error;
2017 if (rval != -ENOENT) {
2018 goto done;
2019 }
2020 /*
2021 * Add the data entry to a data block.
2022 * Extravalid is set to a freeblock found by lookup.
2023 */
2024 rval = xfs_dir2_node_addname_int(args,
2025 state->extravalid ? &state->extrablk : NULL);
2026 if (rval) {
2027 goto done;
2028 }
2029 blk = &state->path.blk[state->path.active - 1];
2030 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2031 /*
2032 * Add the new leaf entry.
2033 */
2034 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2035 if (rval == 0) {
2036 /*
2037 * It worked, fix the hash values up the btree.
2038 */
2039 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2040 xfs_da3_fixhashpath(state, &state->path);
2041 } else {
2042 /*
2043 * It didn't work, we need to split the leaf block.
2044 */
2045 if (args->total == 0) {
2046 ASSERT(rval == -ENOSPC);
2047 goto done;
2048 }
2049 /*
2050 * Split the leaf block and insert the new entry.
2051 */
2052 rval = xfs_da3_split(state);
2053 }
2054done:
2055 xfs_da_state_free(state);
2056 return rval;
2057}
2058
2059/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002061 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * The only real output is the inode number of the entry.
2063 */
2064int /* error */
2065xfs_dir2_node_lookup(
2066 xfs_da_args_t *args) /* operation arguments */
2067{
2068 int error; /* error return value */
2069 int i; /* btree level */
2070 int rval; /* operation return value */
2071 xfs_da_state_t *state; /* btree cursor */
2072
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002073 trace_xfs_dir2_node_lookup(args);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Allocate and initialize the btree cursor.
2077 */
2078 state = xfs_da_state_alloc();
2079 state->args = args;
2080 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Fill in the path to the entry in the cursor.
2083 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002084 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (error)
2086 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002087 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2088 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002089 xfs_dir2_data_entry_t *dep;
2090
Dave Chinner1d9025e2012-06-22 18:50:14 +10002091 dep = (xfs_dir2_data_entry_t *)
2092 ((char *)state->extrablk.bp->b_addr +
2093 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002094 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
2097 * Release the btree blocks and leaf block.
2098 */
2099 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002100 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 state->path.blk[i].bp = NULL;
2102 }
2103 /*
2104 * Release the data block if we have it.
2105 */
2106 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002107 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 state->extrablk.bp = NULL;
2109 }
2110 xfs_da_state_free(state);
2111 return rval;
2112}
2113
2114/*
2115 * Remove an entry from a node-format directory.
2116 */
2117int /* error */
2118xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002119 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002121 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int error; /* error return value */
2123 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002124 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002126 trace_xfs_dir2_node_removename(args);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Allocate and initialize the btree cursor.
2130 */
2131 state = xfs_da_state_alloc();
2132 state->args = args;
2133 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002134
2135 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002136 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002137 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002138 goto out_free;
2139
2140 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002141 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002142 error = rval;
2143 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 blk = &state->path.blk[state->path.active - 1];
2147 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2148 ASSERT(state->extravalid);
2149 /*
2150 * Remove the leaf and data entries.
2151 * Extrablk refers to the data block.
2152 */
2153 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2154 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002155 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002156 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /*
2158 * Fix the hash values up the btree.
2159 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002160 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /*
2162 * If we need to join leaf blocks, do it.
2163 */
2164 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002165 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * If no errors so far, try conversion to leaf format.
2168 */
2169 if (!error)
2170 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002171out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 xfs_da_state_free(state);
2173 return error;
2174}
2175
2176/*
2177 * Replace an entry's inode number in a node-format directory.
2178 */
2179int /* error */
2180xfs_dir2_node_replace(
2181 xfs_da_args_t *args) /* operation arguments */
2182{
2183 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002184 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 xfs_dir2_data_entry_t *dep; /* data entry changed */
2186 int error; /* error return value */
2187 int i; /* btree level */
2188 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002189 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 xfs_dir2_leaf_t *leaf; /* leaf structure */
2191 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2192 int rval; /* internal return value */
2193 xfs_da_state_t *state; /* btree cursor */
2194
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002195 trace_xfs_dir2_node_replace(args);
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
2198 * Allocate and initialize the btree cursor.
2199 */
2200 state = xfs_da_state_alloc();
2201 state->args = args;
2202 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002203
2204 /*
2205 * We have to save new inode number and ftype since
2206 * xfs_da3_node_lookup_int() is going to overwrite them
2207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002209 ftype = args->filetype;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /*
2212 * Lookup the entry to change in the btree.
2213 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002214 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (error) {
2216 rval = error;
2217 }
2218 /*
2219 * It should be found, since the vnodeops layer has looked it up
2220 * and locked it. But paranoia is good.
2221 */
Dave Chinner24513372014-06-25 14:58:08 +10002222 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002223 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /*
2225 * Find the leaf entry.
2226 */
2227 blk = &state->path.blk[state->path.active - 1];
2228 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002229 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002230 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002231 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 ASSERT(state->extravalid);
2233 /*
2234 * Point to the data entry.
2235 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002236 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002237 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2238 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002240 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002241 xfs_dir2_dataptr_to_off(args->geo,
2242 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002243 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /*
2245 * Fill in the new inode number and log the entry.
2246 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002247 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002248 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002249 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 rval = 0;
2251 }
2252 /*
2253 * Didn't find it, and we're holding a data block. Drop it.
2254 */
2255 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002256 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 state->extrablk.bp = NULL;
2258 }
2259 /*
2260 * Release all the buffers in the cursor.
2261 */
2262 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002263 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 state->path.blk[i].bp = NULL;
2265 }
2266 xfs_da_state_free(state);
2267 return rval;
2268}
2269
2270/*
2271 * Trim off a trailing empty freespace block.
2272 * Return (in rvalp) 1 if we did it, 0 if not.
2273 */
2274int /* error */
2275xfs_dir2_node_trim_free(
2276 xfs_da_args_t *args, /* operation arguments */
2277 xfs_fileoff_t fo, /* free block number */
2278 int *rvalp) /* out: did something */
2279{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002280 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 xfs_inode_t *dp; /* incore directory inode */
2282 int error; /* error return code */
2283 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002285 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002289
2290 *rvalp = 0;
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /*
2293 * Read the freespace block.
2294 */
Dave Chinner20252072012-11-12 22:54:13 +11002295 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * There can be holes in freespace. If fo is a hole, there's
2300 * nothing to do.
2301 */
Dave Chinner20252072012-11-12 22:54:13 +11002302 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002304 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002305 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * If there are used entries, there's nothing to do.
2309 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002310 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002311 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 0;
2313 }
2314 /*
2315 * Blow the block away.
2316 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002317 error = xfs_dir2_shrink_inode(args,
2318 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2319 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /*
2321 * Can't fail with ENOSPC since that only happens with no
2322 * space reservation, when breaking up an extent into two
2323 * pieces. This is the last block of an extent.
2324 */
Dave Chinner24513372014-06-25 14:58:08 +10002325 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002326 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return error;
2328 }
2329 /*
2330 * Return that we succeeded.
2331 */
2332 *rvalp = 1;
2333 return 0;
2334}