blob: 2c44248b7b4a8e55659dea430c659fa4a76f7e6c [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
15#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020017#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000019#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110021#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110022#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Function declarations.
26 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100027static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
28 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
30 xfs_da_state_blk_t *blk1,
31 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100032static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int index, xfs_da_state_blk_t *dblk,
34 int *rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dave Chinner24df33b2013-04-12 07:30:21 +100036/*
37 * Check internal consistency of a leafn block.
38 */
39#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080040static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100041xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110042 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100043 struct xfs_buf *bp)
44{
45 struct xfs_dir2_leaf *leaf = bp->b_addr;
46 struct xfs_dir3_icleaf_hdr leafhdr;
47
Christoph Hellwig51842552019-11-08 14:57:49 -080048 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100049
50 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
51 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
52 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080053 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100054 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080055 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100056
Dave Chinner41419562013-10-29 22:11:50 +110057 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100058}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080059
60static inline void
61xfs_dir3_leaf_check(
62 struct xfs_inode *dp,
63 struct xfs_buf *bp)
64{
65 xfs_failaddr_t fa;
66
67 fa = xfs_dir3_leafn_check(dp, bp);
68 if (!fa)
69 return;
70 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070071 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
72 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080073 ASSERT(0);
74}
Dave Chinner24df33b2013-04-12 07:30:21 +100075#else
Dave Chinner41419562013-10-29 22:11:50 +110076#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100077#endif
78
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080079static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110080xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110081 struct xfs_buf *bp)
82{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -070083 struct xfs_mount *mp = bp->b_mount;
Dave Chinner20252072012-11-12 22:54:13 +110084 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110085
Brian Foster39708c22019-02-07 10:45:48 -080086 if (!xfs_verify_magic(bp, hdr->magic))
87 return __this_address;
88
Dave Chinnercbc8adf2013-04-03 16:11:21 +110089 if (xfs_sb_version_hascrc(&mp->m_sb)) {
90 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
91
Eric Sandeence748ea2015-07-29 11:53:31 +100092 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080093 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110094 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080095 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +110096 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080097 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110098 }
99
100 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
101
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800102 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100103}
104
105static void
106xfs_dir3_free_read_verify(
107 struct xfs_buf *bp)
108{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700109 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800110 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100111
Eric Sandeence5028c2014-02-27 15:23:10 +1100112 if (xfs_sb_version_hascrc(&mp->m_sb) &&
113 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800114 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
115 else {
116 fa = xfs_dir3_free_verify(bp);
117 if (fa)
118 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
119 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100120}
Dave Chinner20252072012-11-12 22:54:13 +1100121
Dave Chinner612cfbf2012-11-14 17:52:32 +1100122static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100123xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100124 struct xfs_buf *bp)
125{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700126 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800127 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800129 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800131 fa = xfs_dir3_free_verify(bp);
132 if (fa) {
133 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100134 return;
135 }
136
137 if (!xfs_sb_version_hascrc(&mp->m_sb))
138 return;
139
140 if (bip)
141 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
142
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100143 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100144}
145
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100146const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100147 .name = "xfs_dir3_free",
Brian Foster39708c22019-02-07 10:45:48 -0800148 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
149 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100150 .verify_read = xfs_dir3_free_read_verify,
151 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800152 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100153};
Dave Chinner20252072012-11-12 22:54:13 +1100154
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800155/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800156static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800157xfs_dir3_free_header_check(
158 struct xfs_inode *dp,
159 xfs_dablk_t fbno,
160 struct xfs_buf *bp)
161{
162 struct xfs_mount *mp = dp->i_mount;
163 unsigned int firstdb;
164 int maxbests;
165
166 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
167 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
168 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
169 maxbests;
170 if (xfs_sb_version_hascrc(&mp->m_sb)) {
171 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
172
173 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800174 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800175 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800176 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800177 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800178 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800179 } else {
180 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
181
182 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800183 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800184 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800185 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800186 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800187 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800188 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800189 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800190}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100191
Dave Chinner20252072012-11-12 22:54:13 +1100192static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100193__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100194 struct xfs_trans *tp,
195 struct xfs_inode *dp,
196 xfs_dablk_t fbno,
197 xfs_daddr_t mappedbno,
198 struct xfs_buf **bpp)
199{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800200 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100201 int err;
202
203 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100204 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800205 if (err || !*bpp)
206 return err;
207
208 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800209 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
210 if (fa) {
211 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800212 xfs_trans_brelse(tp, *bpp);
213 return -EFSCORRUPTED;
214 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100215
216 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800217 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100218 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800219
220 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100221}
222
223int
224xfs_dir2_free_read(
225 struct xfs_trans *tp,
226 struct xfs_inode *dp,
227 xfs_dablk_t fbno,
228 struct xfs_buf **bpp)
229{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100230 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100231}
232
233static int
234xfs_dir2_free_try_read(
235 struct xfs_trans *tp,
236 struct xfs_inode *dp,
237 xfs_dablk_t fbno,
238 struct xfs_buf **bpp)
239{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100240 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
241}
242
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100243static int
244xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000245 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100246 xfs_dir2_db_t fbno,
247 struct xfs_buf **bpp)
248{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000249 struct xfs_trans *tp = args->trans;
250 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100251 struct xfs_mount *mp = dp->i_mount;
252 struct xfs_buf *bp;
253 int error;
254 struct xfs_dir3_icfree_hdr hdr;
255
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000256 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100257 -1, &bp, XFS_DATA_FORK);
258 if (error)
259 return error;
260
Dave Chinner61fe1352013-04-03 16:11:30 +1100261 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100262 bp->b_ops = &xfs_dir3_free_buf_ops;
263
264 /*
265 * Initialize the new block to be empty, and remember
266 * its first slot as our empty slot.
267 */
Dave Chinnere400d272013-05-28 18:37:17 +1000268 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
269 memset(&hdr, 0, sizeof(hdr));
270
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
272 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
273
274 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000275
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100276 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
277 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000278 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000279 } else
280 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100281 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100282 *bpp = bp;
283 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * Log entries from a freespace block.
288 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000289STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000291 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000292 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int first, /* first entry to log */
294 int last) /* last entry to log */
295{
296 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100297 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dave Chinner1d9025e2012-06-22 18:50:14 +1000299 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000300 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100301 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
302 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000303 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100304 (uint)((char *)&bests[first] - (char *)free),
305 (uint)((char *)&bests[last] - (char *)free +
306 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309/*
310 * Log header from a freespace block.
311 */
312static void
313xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000314 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000315 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Dave Chinner836a94a2013-08-12 20:49:44 +1000317#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 xfs_dir2_free_t *free; /* freespace structure */
319
Dave Chinner1d9025e2012-06-22 18:50:14 +1000320 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100321 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
322 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000323#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000324 xfs_trans_log_buf(args->trans, bp, 0,
325 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328/*
329 * Convert a leaf-format directory to a node-format directory.
330 * We need to change the magic number of the leaf block, and copy
331 * the freespace table out of the leaf block into its own block.
332 */
333int /* error */
334xfs_dir2_leaf_to_node(
335 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000336 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 xfs_inode_t *dp; /* incore directory inode */
339 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000340 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 xfs_dir2_db_t fdb; /* freespace block number */
342 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100343 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int i; /* leaf freespace index */
345 xfs_dir2_leaf_t *leaf; /* leaf structure */
346 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int n; /* count of live freespc ents */
348 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100349 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100351 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000353 trace_xfs_dir2_leaf_to_node(args);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 tp = args->trans;
357 /*
358 * Add a freespace block to the directory.
359 */
360 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
361 return error;
362 }
Dave Chinner30028032014-06-06 15:08:18 +1000363 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Get the buffer for the new freespace block.
366 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000367 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100370
Dave Chinner1d9025e2012-06-22 18:50:14 +1000371 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100372 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000373 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000374 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800375 if (be32_to_cpu(ltp->bestcount) >
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700376 (uint)dp->i_d.di_size / args->geo->blksize) {
377 xfs_buf_corruption_error(lbp);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800378 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700379 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Copy freespace entries from the leaf block to the new block.
383 * Count active entries.
384 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100385 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500386 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100387 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100388 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100390 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100394 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100396 freehdr.nused = n;
397 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
398
Dave Chinner01ba43b2013-10-29 22:11:52 +1100399 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000400 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
401 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100402
Dave Chinner24df33b2013-04-12 07:30:21 +1000403 /*
404 * Converting the leaf to a leafnode is just a matter of changing the
405 * magic number and the ops. Do the change directly to the buffer as
406 * it's less work (and less code) than decoding the header to host
407 * format and back again.
408 */
409 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
411 else
412 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
413 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100414 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000415 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100416 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418}
419
420/*
421 * Add a leaf entry to a leaf block in a node-form directory.
422 * The other work necessary is done from the caller.
423 */
424static int /* error */
425xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000426 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800427 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int index) /* insertion pt for new entry */
429{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800430 struct xfs_dir3_icleaf_hdr leafhdr;
431 struct xfs_inode *dp = args->dp;
432 struct xfs_dir2_leaf *leaf = bp->b_addr;
433 struct xfs_dir2_leaf_entry *lep;
434 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800436 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int lfloghigh; /* high leaf entry logging */
438 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800439 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000441 trace_xfs_dir2_leafn_add(args, index);
442
Christoph Hellwig51842552019-11-08 14:57:49 -0800443 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800444 ents = leafhdr.ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * Quick check just to make sure we are not going to index
448 * into other peoples memory
449 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700450 if (index < 0) {
451 xfs_buf_corruption_error(bp);
Dave Chinner24513372014-06-25 14:58:08 +1000452 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /*
456 * If there are already the maximum number of leaf entries in
457 * the block, if there are no stale entries it won't fit.
458 * Caller will do a split. If there are stale entries we'll do
459 * a compact.
460 */
461
Christoph Hellwig478c7832019-11-08 14:57:51 -0800462 if (leafhdr.count == args->geo->leaf_max_ents) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000463 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000464 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000465 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else
467 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000468 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
469 ASSERT(index == leafhdr.count ||
470 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Barry Naujok6a178102008-05-21 16:42:05 +1000472 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474
475 /*
476 * Compact out all but one stale leaf entry. Leaves behind
477 * the entry closest to index.
478 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000479 if (compact)
480 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
481 &highstale, &lfloglow, &lfloghigh);
482 else if (leafhdr.stale) {
483 /*
484 * Set impossible logging indices for this case.
485 */
486 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 lfloghigh = -1;
488 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Insert the new entry, log everything.
492 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200494 highstale, &lfloglow, &lfloghigh);
495
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100496 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000497 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100498 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000499
Christoph Hellwig163fbbb2019-11-08 14:57:50 -0800500 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000501 xfs_dir3_leaf_log_header(args, bp);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800502 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100503 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
507#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100508static void
509xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100510 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100511 struct xfs_buf *bp,
512 xfs_dir2_db_t db)
513{
514 struct xfs_dir3_icfree_hdr hdr;
515
Dave Chinner01ba43b2013-10-29 22:11:52 +1100516 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100517
Dave Chinner8f661932014-06-06 15:15:59 +1000518 ASSERT((hdr.firstdb %
519 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100520 ASSERT(hdr.firstdb <= db);
521 ASSERT(db < hdr.firstdb + hdr.nvalid);
522}
523#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100524#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif /* DEBUG */
526
527/*
528 * Return the last hash value in the leaf.
529 * Stale entries are ok.
530 */
531xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700532xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100533 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000534 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int *count) /* count of entries in leaf */
536{
Dave Chinner24df33b2013-04-12 07:30:21 +1000537 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Christoph Hellwig787b0892019-11-08 14:57:50 -0800539 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000540
541 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700542 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
543 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
544 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000547 *count = leafhdr.count;
548 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800550 return be32_to_cpu(leafhdr.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_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 dp = args->dp;
582 tp = args->trans;
583 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000584 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800585 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000586
Dave Chinner41419562013-10-29 22:11:50 +1100587 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000588 ASSERT(leafhdr.count > 0);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
591 * Look up the hash value in the leaf entries.
592 */
593 index = xfs_dir2_leaf_search_hash(args, bp);
594 /*
595 * Do we have a buffer coming in?
596 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000597 if (state->extravalid) {
598 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000600 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100602 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
603 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100605 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * Loop over leaf entries with the right hash value.
608 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800609 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000610 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
611 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Skip stale leaf entries.
614 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100615 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 continue;
617 /*
618 * Pull the data block number from the entry.
619 */
Dave Chinner30028032014-06-06 15:08:18 +1000620 newdb = xfs_dir2_dataptr_to_db(args->geo,
621 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * For addname, we're looking for a place to put the new entry.
624 * We want to use a data block with an entry of equal
625 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000626 *
627 * If this block isn't the data block we already have
628 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000630 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100631 __be16 *bests;
632
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000633 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 * Convert the data block to the free block
636 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Dave Chinner8f661932014-06-06 15:15:59 +1000638 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000640 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
646 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000647 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100648
649 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000650 xfs_dir2_db_to_da(args->geo,
651 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100652 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000653 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000655 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100656
Dave Chinner01ba43b2013-10-29 22:11:52 +1100657 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Dave Chinner8f661932014-06-06 15:15:59 +1000662 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500666 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100667 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000668 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
669 XFS_ERRLEVEL_LOW, mp);
670 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000672 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100675 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000679 /* Didn't find any space */
680 fi = -1;
681out:
Barry Naujok6a178102008-05-21 16:42:05 +1000682 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683 if (curbp) {
684 /* Giving back a free block. */
685 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687 state->extrablk.index = fi;
688 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100689
690 /*
691 * Important: this magic number is not in the buffer - it's for
692 * buffer type information and therefore only the free/data type
693 * matters here, not whether CRCs are enabled or not.
694 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000695 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
696 } else {
697 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000700 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
702 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000703 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000707 * Look up a leaf entry in a node-format leaf block.
708 * The extrablk in state a data block.
709 */
710STATIC int
711xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000712 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000713 xfs_da_args_t *args, /* operation arguments */
714 int *indexp, /* out: leaf entry index */
715 xfs_da_state_t *state) /* state to fill in */
716{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000717 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000718 xfs_dir2_db_t curdb = -1; /* current data block number */
719 xfs_dir2_data_entry_t *dep; /* data block entry */
720 xfs_inode_t *dp; /* incore directory inode */
721 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000722 int index; /* leaf entry index */
723 xfs_dir2_leaf_t *leaf; /* leaf structure */
724 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
725 xfs_mount_t *mp; /* filesystem mount point */
726 xfs_dir2_db_t newdb; /* new data block number */
727 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000728 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000729 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000730
731 dp = args->dp;
732 tp = args->trans;
733 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000734 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800735 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000736
Dave Chinner41419562013-10-29 22:11:50 +1100737 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700738 if (leafhdr.count <= 0) {
739 xfs_buf_corruption_error(bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700740 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700741 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000742
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000743 /*
744 * Look up the hash value in the leaf entries.
745 */
746 index = xfs_dir2_leaf_search_hash(args, bp);
747 /*
748 * Do we have a buffer coming in?
749 */
750 if (state->extravalid) {
751 curbp = state->extrablk.bp;
752 curdb = state->extrablk.blkno;
753 }
754 /*
755 * Loop over leaf entries with the right hash value.
756 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800757 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000758 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
759 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000760 /*
761 * Skip stale leaf entries.
762 */
763 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
764 continue;
765 /*
766 * Pull the data block number from the entry.
767 */
Dave Chinner30028032014-06-06 15:08:18 +1000768 newdb = xfs_dir2_dataptr_to_db(args->geo,
769 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000770 /*
771 * Not adding a new entry, so we really want to find
772 * the name given to us.
773 *
774 * If it's a different data block, go get it.
775 */
776 if (newdb != curdb) {
777 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000778 * If we had a block before that we aren't saving
779 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000780 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000781 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
782 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000783 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000784 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000785 * If needing the block that is saved with a CI match,
786 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000787 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000788 if (args->cmpresult != XFS_CMP_DIFFERENT &&
789 newdb == state->extrablk.blkno) {
790 ASSERT(state->extravalid);
791 curbp = state->extrablk.bp;
792 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100793 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000794 xfs_dir2_db_to_da(args->geo,
795 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100796 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000797 if (error)
798 return error;
799 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100800 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000801 curdb = newdb;
802 }
803 /*
804 * Point to the data entry.
805 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000806 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000807 xfs_dir2_dataptr_to_off(args->geo,
808 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000810 * Compare the entry and if it's an exact match, return
811 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000812 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000813 */
Barry Naujok5163f952008-05-21 16:41:01 +1000814 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
815 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000816 /* If there is a CI match block, drop it */
817 if (args->cmpresult != XFS_CMP_DIFFERENT &&
818 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000819 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000820 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000821 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100822 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000823 *indexp = index;
824 state->extravalid = 1;
825 state->extrablk.bp = curbp;
826 state->extrablk.blkno = curdb;
827 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000828 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000829 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100830 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100831 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000832 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000833 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000834 }
835 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000836 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000837 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000838 if (args->cmpresult == XFS_CMP_DIFFERENT) {
839 /* Giving back last used data block. */
840 state->extravalid = 1;
841 state->extrablk.bp = curbp;
842 state->extrablk.index = -1;
843 state->extrablk.blkno = curdb;
844 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100845 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100846 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000847 } else {
848 /* If the curbp is not the CI match block, drop it */
849 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000850 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000851 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000852 } else {
853 state->extravalid = 0;
854 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000855 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000856 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000857}
858
859/*
860 * Look up a leaf entry in a node-format leaf block.
861 * If this is an addname then the extrablk in state is a freespace block,
862 * otherwise it's a data block.
863 */
864int
865xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000866 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000867 xfs_da_args_t *args, /* operation arguments */
868 int *indexp, /* out: leaf entry index */
869 xfs_da_state_t *state) /* state to fill in */
870{
Barry Naujok6a178102008-05-21 16:42:05 +1000871 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000872 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
873 state);
874 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
875}
876
877/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * Move count leaf entries from source to destination leaf.
879 * Log entries and headers. Stale entries are preserved.
880 */
881static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000882xfs_dir3_leafn_moveents(
883 xfs_da_args_t *args, /* operation arguments */
884 struct xfs_buf *bp_s, /* source */
885 struct xfs_dir3_icleaf_hdr *shdr,
886 struct xfs_dir2_leaf_entry *sents,
887 int start_s,/* source leaf index */
888 struct xfs_buf *bp_d, /* destination */
889 struct xfs_dir3_icleaf_hdr *dhdr,
890 struct xfs_dir2_leaf_entry *dents,
891 int start_d,/* destination leaf index */
892 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Dave Chinner24df33b2013-04-12 07:30:21 +1000894 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000896 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Silently return if nothing to do.
900 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000901 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * If the destination index is not the end of the current
906 * destination leaf entries, open up a hole in the destination
907 * to hold the new entries.
908 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000909 if (start_d < dhdr->count) {
910 memmove(&dents[start_d + count], &dents[start_d],
911 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800912 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000913 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915 /*
916 * If the source has stale leaves, count the ones in the copy range
917 * so we can update the header correctly.
918 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000919 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 int i; /* temp leaf index */
921
922 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000923 if (sents[i].address ==
924 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 stale++;
926 }
927 } else
928 stale = 0;
929 /*
930 * Copy the leaf entries from source to destination.
931 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000932 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800934 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /*
937 * If there are source entries after the ones we copied,
938 * delete the ones we copied by sliding the next ones down.
939 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000940 if (start_s + count < shdr->count) {
941 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800943 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
944 start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Update the headers and log them.
949 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000950 shdr->count -= count;
951 shdr->stale -= stale;
952 dhdr->count += count;
953 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956/*
957 * Determine the sort order of two leaf blocks.
958 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
959 */
960int /* sort order */
961xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100962 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000963 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
964 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Dave Chinner24df33b2013-04-12 07:30:21 +1000966 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
967 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
968 struct xfs_dir2_leaf_entry *ents1;
969 struct xfs_dir2_leaf_entry *ents2;
970 struct xfs_dir3_icleaf_hdr hdr1;
971 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Christoph Hellwig51842552019-11-08 14:57:49 -0800973 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
974 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800975 ents1 = hdr1.ents;
976 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +1000977
978 if (hdr1.count > 0 && hdr2.count > 0 &&
979 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
980 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
981 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return 1;
983 return 0;
984}
985
986/*
987 * Rebalance leaf entries between two leaf blocks.
988 * This is actually only called when the second block is new,
989 * though the code deals with the general case.
990 * A new entry will be inserted in one of the blocks, and that
991 * entry is taken into account when balancing.
992 */
993static void
994xfs_dir2_leafn_rebalance(
995 xfs_da_state_t *state, /* btree cursor */
996 xfs_da_state_blk_t *blk1, /* first btree block */
997 xfs_da_state_blk_t *blk2) /* second btree block */
998{
999 xfs_da_args_t *args; /* operation arguments */
1000 int count; /* count (& direction) leaves */
1001 int isleft; /* new goes in left leaf */
1002 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1003 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1004 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001005#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int oldstale; /* old count of stale leaves */
1007#endif
1008 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001009 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001010 struct xfs_dir2_leaf_entry *ents1;
1011 struct xfs_dir2_leaf_entry *ents2;
1012 struct xfs_dir3_icleaf_hdr hdr1;
1013 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001014 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 args = state->args;
1017 /*
1018 * If the block order is wrong, swap the arguments.
1019 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001020 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1021 if (swap_blocks)
1022 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Dave Chinner1d9025e2012-06-22 18:50:14 +10001024 leaf1 = blk1->bp->b_addr;
1025 leaf2 = blk2->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001026 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1027 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001028 ents1 = hdr1.ents;
1029 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001030
1031 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001032#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001033 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034#endif
1035 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 /*
1038 * If the old leaf count was odd then the new one will be even,
1039 * so we need to divide the new count evenly.
1040 */
1041 if (oldsum & 1) {
1042 xfs_dahash_t midhash; /* middle entry hash value */
1043
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 if (mid >= hdr1.count)
1045 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001047 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 isleft = args->hashval <= midhash;
1049 }
1050 /*
1051 * If the old count is even then the new count is odd, so there's
1052 * no preferred side for the new entry.
1053 * Pick the left one.
1054 */
1055 else
1056 isleft = 1;
1057 /*
1058 * Calculate moved entry count. Positive means left-to-right,
1059 * negative means right-to-left. Then move the entries.
1060 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001061 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001063 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1064 hdr1.count - count, blk2->bp,
1065 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001067 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1068 blk1->bp, &hdr1, ents1,
1069 hdr1.count, count);
1070
1071 ASSERT(hdr1.count + hdr2.count == oldsum);
1072 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1073
1074 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001075 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1076 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001077 xfs_dir3_leaf_log_header(args, blk1->bp);
1078 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001079
Dave Chinner41419562013-10-29 22:11:50 +11001080 xfs_dir3_leaf_check(dp, blk1->bp);
1081 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /*
1084 * Mark whether we're inserting into the old or new leaf.
1085 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001086 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001087 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001088 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001089 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001091 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 /*
1093 * Adjust the expected index for insertion.
1094 */
1095 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001096 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001097
1098 /*
1099 * Finally sanity check just to make sure we are not returning a
1100 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Dave Chinner41419562013-10-29 22:11:50 +11001102 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 state->inleaf = 1;
1104 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001105 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001106 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001107 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109}
1110
Dave Chinner20252072012-11-12 22:54:13 +11001111static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001112xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001113 xfs_da_args_t *args,
1114 struct xfs_dir2_data_hdr *hdr,
1115 struct xfs_dir2_free *free,
1116 xfs_dir2_db_t fdb,
1117 int findex,
1118 struct xfs_buf *fbp,
1119 int longest)
1120{
Dave Chinner20252072012-11-12 22:54:13 +11001121 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001122 __be16 *bests;
1123 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001124 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001125
Dave Chinner01ba43b2013-10-29 22:11:52 +11001126 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001127 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001128 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001129 /*
1130 * Data block is not empty, just set the free entry to the new
1131 * value.
1132 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001133 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001134 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001135 return 0;
1136 }
1137
1138 /* One less used entry in the free table. */
1139 freehdr.nused--;
1140
1141 /*
1142 * If this was the last entry in the table, we can trim the table size
1143 * back. There might be other entries at the end referring to
1144 * non-existent data blocks, get those too.
1145 */
1146 if (findex == freehdr.nvalid - 1) {
1147 int i; /* free entry index */
1148
1149 for (i = findex - 1; i >= 0; i--) {
1150 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1151 break;
1152 }
1153 freehdr.nvalid = i + 1;
1154 logfree = 0;
1155 } else {
1156 /* Not the last entry, just punch it out. */
1157 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001158 logfree = 1;
1159 }
1160
Dave Chinner01ba43b2013-10-29 22:11:52 +11001161 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001162 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001163
1164 /*
1165 * If there are no useful entries left in the block, get rid of the
1166 * block if we can.
1167 */
1168 if (!freehdr.nused) {
1169 int error;
1170
1171 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1172 if (error == 0) {
1173 fbp = NULL;
1174 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001175 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001176 return error;
1177 /*
1178 * It's possible to get ENOSPC if there is no
1179 * space reservation. In this case some one
1180 * else will eventually get rid of this block.
1181 */
1182 }
1183
Dave Chinner20252072012-11-12 22:54:13 +11001184 /* Log the free entry that changed, unless we got rid of it. */
1185 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001186 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001187 return 0;
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190/*
1191 * Remove an entry from a node directory.
1192 * This removes the leaf entry and the data entry,
1193 * and updates the free block if necessary.
1194 */
1195static int /* error */
1196xfs_dir2_leafn_remove(
1197 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001198 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int index, /* leaf entry index */
1200 xfs_da_state_blk_t *dblk, /* data block */
1201 int *rval) /* resulting block needs join */
1202{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001203 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001205 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 xfs_dir2_data_entry_t *dep; /* data block entry */
1207 xfs_inode_t *dp; /* incore directory inode */
1208 xfs_dir2_leaf_t *leaf; /* leaf structure */
1209 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1210 int longest; /* longest data free entry */
1211 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int needlog; /* need to log data header */
1213 int needscan; /* need to rescan data frees */
1214 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001215 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001216 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001218 trace_xfs_dir2_leafn_remove(args, index);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 dp = args->dp;
1221 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001222 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001223 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Point to the entry we're removing.
1227 */
Christoph Hellwig787b0892019-11-08 14:57:50 -08001228 lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +10001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /*
1231 * Extract the data block and offset from the entry.
1232 */
Dave Chinner30028032014-06-06 15:08:18 +10001233 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001235 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Kill the leaf entry by marking it stale.
1240 * Log the leaf block changes.
1241 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001242 leafhdr.stale++;
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001243 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001244 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001245
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001246 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001247 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 /*
1250 * Make the data entry free. Keep track of the longest freespace
1251 * in the data block in case it changes.
1252 */
1253 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001254 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001255 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001256 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001257 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001259 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001260 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Rescan the data block freespaces for bestfree.
1263 * Log the data block header if needed.
1264 */
1265 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001266 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001268 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001269 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * If the longest data block freespace changes, need to update
1272 * the corresponding freeblock entry.
1273 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001274 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001276 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 xfs_dir2_db_t fdb; /* freeblock block number */
1278 int findex; /* index in freeblock entries */
1279 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /*
1282 * Convert the data block number to a free block,
1283 * read in the free block.
1284 */
Dave Chinner8f661932014-06-06 15:15:59 +10001285 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001286 error = xfs_dir2_free_read(tp, dp,
1287 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001288 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001289 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001291 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001292#ifdef DEBUG
1293 {
1294 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001295 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001296 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001297 (fdb - xfs_dir2_byte_to_db(args->geo,
1298 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001299 }
1300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Calculate which entry we need to fix.
1303 */
Dave Chinner8f661932014-06-06 15:15:59 +10001304 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001305 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * If the data block is now empty we can get rid of it
1308 * (usually).
1309 */
Dave Chinner8f661932014-06-06 15:15:59 +10001310 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001311 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * Try to punch out the data block.
1314 */
1315 error = xfs_dir2_shrink_inode(args, db, dbp);
1316 if (error == 0) {
1317 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001318 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 /*
1321 * We can get ENOSPC if there's no space reservation.
1322 * In this case just drop the buffer and some one else
1323 * will eventually get rid of the empty block.
1324 */
Dave Chinner24513372014-06-25 14:58:08 +10001325 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return error;
1327 }
1328 /*
1329 * If we got rid of the data block, we can eliminate that entry
1330 * in the free block.
1331 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001332 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001333 fdb, findex, fbp, longest);
1334 if (error)
1335 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Dave Chinner20252072012-11-12 22:54:13 +11001337
Dave Chinner41419562013-10-29 22:11:50 +11001338 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001340 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * to justify trying to join it with a neighbor.
1342 */
Christoph Hellwig545910b2019-11-08 14:57:51 -08001343 *rval = (args->geo->leaf_hdr_size +
Christoph Hellwig787b0892019-11-08 14:57:50 -08001344 (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001345 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return 0;
1347}
1348
1349/*
1350 * Split the leaf entries in the old block into old and new blocks.
1351 */
1352int /* error */
1353xfs_dir2_leafn_split(
1354 xfs_da_state_t *state, /* btree cursor */
1355 xfs_da_state_blk_t *oldblk, /* original block */
1356 xfs_da_state_blk_t *newblk) /* newly created block */
1357{
1358 xfs_da_args_t *args; /* operation arguments */
1359 xfs_dablk_t blkno; /* new leaf block number */
1360 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001361 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
1364 * Allocate space for a new leaf node.
1365 */
1366 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001367 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1369 error = xfs_da_grow_inode(args, &blkno);
1370 if (error) {
1371 return error;
1372 }
1373 /*
1374 * Initialize the new leaf block.
1375 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001376 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001377 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1378 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 newblk->blkno = blkno;
1382 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1383 /*
1384 * Rebalance the entries across the two leaves, link the new
1385 * block into the leaves.
1386 */
1387 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001388 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (error) {
1390 return error;
1391 }
1392 /*
1393 * Insert the new entry in the correct block.
1394 */
1395 if (state->inleaf)
1396 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1397 else
1398 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1399 /*
1400 * Update last hashval in each block since we added the name.
1401 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001402 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1403 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001404 xfs_dir3_leaf_check(dp, oldblk->bp);
1405 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return error;
1407}
1408
1409/*
1410 * Check a leaf block and its neighbors to see if the block should be
1411 * collapsed into one or the other neighbor. Always keep the block
1412 * with the smaller block number.
1413 * If the current block is over 50% full, don't try to join it, return 0.
1414 * If the block is empty, fill in the state structure and return 2.
1415 * If it can be collapsed, fill in the state structure and return 1.
1416 * If nothing can be done, return 0.
1417 */
1418int /* error */
1419xfs_dir2_leafn_toosmall(
1420 xfs_da_state_t *state, /* btree cursor */
1421 int *action) /* resulting action to take */
1422{
1423 xfs_da_state_blk_t *blk; /* leaf block */
1424 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001425 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int bytes; /* bytes in use */
1427 int count; /* leaf live entry count */
1428 int error; /* error return value */
1429 int forward; /* sibling block direction */
1430 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 xfs_dir2_leaf_t *leaf; /* leaf structure */
1432 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001433 struct xfs_dir3_icleaf_hdr leafhdr;
1434 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001435 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /*
1438 * Check for the degenerate case of the block being over 50% full.
1439 * If so, it's not worth even looking to see if we might be able
1440 * to coalesce with a sibling.
1441 */
1442 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001443 leaf = blk->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001444 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001445 ents = leafhdr.ents;
Dave Chinner41419562013-10-29 22:11:50 +11001446 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001447
1448 count = leafhdr.count - leafhdr.stale;
Christoph Hellwig545910b2019-11-08 14:57:51 -08001449 bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001450 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * Blk over 50%, don't try to join.
1453 */
1454 *action = 0;
1455 return 0;
1456 }
1457 /*
1458 * Check for the degenerate case of the block being empty.
1459 * If the block is empty, we'll simply delete it, no need to
1460 * coalesce it with a sibling block. We choose (arbitrarily)
1461 * to merge with the forward block unless it is NULL.
1462 */
1463 if (count == 0) {
1464 /*
1465 * Make altpath point to the block we want to keep and
1466 * path point to the block we want to drop (this one).
1467 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001468 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001470 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 &rval);
1472 if (error)
1473 return error;
1474 *action = rval ? 2 : 0;
1475 return 0;
1476 }
1477 /*
1478 * Examine each sibling block to see if we can coalesce with
1479 * at least 25% free space to spare. We need to figure out
1480 * whether to merge with the forward or the backward block.
1481 * We prefer coalescing with the lower numbered sibling so as
1482 * to shrink a directory over time.
1483 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001484 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001486 struct xfs_dir3_icleaf_hdr hdr2;
1487
1488 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (blkno == 0)
1490 continue;
1491 /*
1492 * Read the sibling leaf block.
1493 */
Dave Chinner41419562013-10-29 22:11:50 +11001494 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001495 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001496 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Count bytes in the two blocks combined.
1501 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001502 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001503 bytes = state->args->geo->blksize -
1504 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001505
Dave Chinner1d9025e2012-06-22 18:50:14 +10001506 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001507 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001508 ents = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001509 count += hdr2.count - hdr2.stale;
1510 bytes -= count * sizeof(ents[0]);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 /*
1513 * Fits with at least 25% to spare.
1514 */
1515 if (bytes >= 0)
1516 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001517 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 /*
1520 * Didn't like either block, give up.
1521 */
1522 if (i >= 2) {
1523 *action = 0;
1524 return 0;
1525 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Make altpath point to the block we want to keep (the lower
1529 * numbered block) and path point to the block we want to drop.
1530 */
1531 memcpy(&state->altpath, &state->path, sizeof(state->path));
1532 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001533 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 &rval);
1535 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001536 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 &rval);
1538 if (error) {
1539 return error;
1540 }
1541 *action = rval ? 0 : 1;
1542 return 0;
1543}
1544
1545/*
1546 * Move all the leaf entries from drop_blk to save_blk.
1547 * This is done as part of a join operation.
1548 */
1549void
1550xfs_dir2_leafn_unbalance(
1551 xfs_da_state_t *state, /* cursor */
1552 xfs_da_state_blk_t *drop_blk, /* dead block */
1553 xfs_da_state_blk_t *save_blk) /* surviving block */
1554{
1555 xfs_da_args_t *args; /* operation arguments */
1556 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1557 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001558 struct xfs_dir3_icleaf_hdr savehdr;
1559 struct xfs_dir3_icleaf_hdr drophdr;
1560 struct xfs_dir2_leaf_entry *sents;
1561 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001562 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 args = state->args;
1565 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1566 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001567 drop_leaf = drop_blk->bp->b_addr;
1568 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001569
Christoph Hellwig51842552019-11-08 14:57:49 -08001570 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1571 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001572 sents = savehdr.ents;
1573 dents = drophdr.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
1576 * If there are any stale leaf entries, take this opportunity
1577 * to purge them.
1578 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001579 if (drophdr.stale)
1580 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1581 if (savehdr.stale)
1582 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /*
1585 * Move the entries from drop to the appropriate end of save.
1586 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001587 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001588 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001589 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1590 save_blk->bp, &savehdr, sents, 0,
1591 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001593 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1594 save_blk->bp, &savehdr, sents,
1595 savehdr.count, drophdr.count);
1596 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1597
1598 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001599 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1600 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001601 xfs_dir3_leaf_log_header(args, save_blk->bp);
1602 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001603
Dave Chinner41419562013-10-29 22:11:50 +11001604 xfs_dir3_leaf_check(dp, save_blk->bp);
1605 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001609 * Add a new data block to the directory at the free space index that the caller
1610 * has specified.
1611 */
1612static int
1613xfs_dir2_node_add_datablk(
1614 struct xfs_da_args *args,
1615 struct xfs_da_state_blk *fblk,
1616 xfs_dir2_db_t *dbno,
1617 struct xfs_buf **dbpp,
1618 struct xfs_buf **fbpp,
1619 int *findex)
1620{
1621 struct xfs_inode *dp = args->dp;
1622 struct xfs_trans *tp = args->trans;
1623 struct xfs_mount *mp = dp->i_mount;
1624 struct xfs_dir3_icfree_hdr freehdr;
1625 struct xfs_dir2_data_free *bf;
1626 struct xfs_dir2_data_hdr *hdr;
1627 struct xfs_dir2_free *free = NULL;
1628 xfs_dir2_db_t fbno;
1629 struct xfs_buf *fbp;
1630 struct xfs_buf *dbp;
1631 __be16 *bests = NULL;
1632 int error;
1633
1634 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001635 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001636 return -ENOSPC;
1637
1638 /* Allocate and initialize the new data block. */
1639 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1640 if (error)
1641 return error;
1642 error = xfs_dir3_data_init(args, *dbno, &dbp);
1643 if (error)
1644 return error;
1645
1646 /*
1647 * Get the freespace block corresponding to the data block
1648 * that was just allocated.
1649 */
1650 fbno = dp->d_ops->db_to_fdb(args->geo, *dbno);
1651 error = xfs_dir2_free_try_read(tp, dp,
1652 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1653 if (error)
1654 return error;
1655
1656 /*
1657 * If there wasn't a freespace block, the read will
1658 * return a NULL fbp. Allocate and initialize a new one.
1659 */
1660 if (!fbp) {
1661 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1662 if (error)
1663 return error;
1664
1665 if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) {
1666 xfs_alert(mp,
1667"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1668 __func__, (unsigned long long)dp->i_ino,
1669 (long long)dp->d_ops->db_to_fdb(args->geo, *dbno),
1670 (long long)*dbno, (long long)fbno);
1671 if (fblk) {
1672 xfs_alert(mp,
1673 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1674 fblk, (unsigned long long)fblk->blkno,
1675 fblk->index, fblk->magic);
1676 } else {
1677 xfs_alert(mp, " ... fblk is NULL");
1678 }
1679 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1680 return -EFSCORRUPTED;
1681 }
1682
1683 /* Get a buffer for the new block. */
1684 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1685 if (error)
1686 return error;
1687 free = fbp->b_addr;
1688 bests = dp->d_ops->free_bests_p(free);
1689 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1690
1691 /* Remember the first slot as our empty slot. */
1692 freehdr.firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
1693 XFS_DIR2_FREE_OFFSET)) *
1694 dp->d_ops->free_max_bests(args->geo);
1695 } else {
1696 free = fbp->b_addr;
1697 bests = dp->d_ops->free_bests_p(free);
1698 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1699 }
1700
1701 /* Set the freespace block index from the data block number. */
1702 *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno);
1703
1704 /* Extend the freespace table if the new data block is off the end. */
1705 if (*findex >= freehdr.nvalid) {
1706 ASSERT(*findex < dp->d_ops->free_max_bests(args->geo));
1707 freehdr.nvalid = *findex + 1;
1708 bests[*findex] = cpu_to_be16(NULLDATAOFF);
1709 }
1710
1711 /*
1712 * If this entry was for an empty data block (this should always be
1713 * true) then update the header.
1714 */
1715 if (bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
1716 freehdr.nused++;
1717 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
1718 xfs_dir2_free_log_header(args, fbp);
1719 }
1720
1721 /* Update the freespace value for the new block in the table. */
1722 hdr = dbp->b_addr;
1723 bf = dp->d_ops->data_bestfree_p(hdr);
1724 bests[*findex] = bf[0].length;
1725
1726 *dbpp = dbp;
1727 *fbpp = fbp;
1728 return 0;
1729}
1730
Dave Chinner0e822252019-08-29 09:04:07 -07001731static int
1732xfs_dir2_node_find_freeblk(
1733 struct xfs_da_args *args,
1734 struct xfs_da_state_blk *fblk,
1735 xfs_dir2_db_t *dbnop,
1736 struct xfs_buf **fbpp,
1737 int *findexp,
1738 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001740 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001741 struct xfs_dir2_free *free = NULL;
1742 struct xfs_inode *dp = args->dp;
1743 struct xfs_trans *tp = args->trans;
1744 struct xfs_buf *fbp = NULL;
Dave Chinner756c6f02019-08-29 09:04:08 -07001745 xfs_dir2_db_t firstfbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001746 xfs_dir2_db_t lastfbno;
1747 xfs_dir2_db_t ifbno = -1;
1748 xfs_dir2_db_t dbno = -1;
Dave Chinner756c6f02019-08-29 09:04:08 -07001749 xfs_dir2_db_t fbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001750 xfs_fileoff_t fo;
Dave Chinner610125a2019-08-29 09:04:07 -07001751 __be16 *bests = NULL;
1752 int findex = 0;
Dave Chinner0e822252019-08-29 09:04:07 -07001753 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * If we came in with a freespace block that means that lookup
1757 * found an entry with our hash value. This is the freespace
1758 * block for that data entry.
1759 */
1760 if (fblk) {
1761 fbp = fblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001762 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 findex = fblk->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001765 /* caller already found the freespace for us. */
1766 bests = dp->d_ops->free_bests_p(free);
1767 dp->d_ops->free_hdr_from_disk(&freehdr, free);
1768
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001769 ASSERT(findex < freehdr.nvalid);
1770 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1771 ASSERT(be16_to_cpu(bests[findex]) >= length);
1772 dbno = freehdr.firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001773 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Dave Chinner0e822252019-08-29 09:04:07 -07001775
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001776 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001777 * The data block looked at didn't have enough room.
1778 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001779 */
Dave Chinner0e822252019-08-29 09:04:07 -07001780 ifbno = fblk->blkno;
Dave Chinner610125a2019-08-29 09:04:07 -07001781 xfs_trans_brelse(tp, fbp);
1782 fbp = NULL;
1783 fblk->bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Dave Chinner0e822252019-08-29 09:04:07 -07001785
1786 /*
1787 * If we don't have a data block yet, we're going to scan the freespace
Dave Chinner610125a2019-08-29 09:04:07 -07001788 * data for a data block with enough free space in it.
Dave Chinner0e822252019-08-29 09:04:07 -07001789 */
1790 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1791 if (error)
1792 return error;
1793 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Dave Chinner756c6f02019-08-29 09:04:08 -07001794 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0e822252019-08-29 09:04:07 -07001795
Dave Chinner756c6f02019-08-29 09:04:08 -07001796 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001797 /* If it's ifbno we already looked at it. */
1798 if (fbno == ifbno)
1799 continue;
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 /*
Dave Chinner610125a2019-08-29 09:04:07 -07001802 * Read the block. There can be holes in the freespace blocks,
1803 * so this might not succeed. This should be really rare, so
1804 * there's no reason to avoid it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Dave Chinner610125a2019-08-29 09:04:07 -07001806 error = xfs_dir2_free_try_read(tp, dp,
1807 xfs_dir2_db_to_da(args->geo, fbno),
1808 &fbp);
1809 if (error)
1810 return error;
1811 if (!fbp)
1812 continue;
1813
1814 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001815 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001816 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner610125a2019-08-29 09:04:07 -07001817
1818 /* Scan the free entry array for a large enough free space. */
Dave Chinner756c6f02019-08-29 09:04:08 -07001819 for (findex = freehdr.nvalid - 1; findex >= 0; findex--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001820 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1821 be16_to_cpu(bests[findex]) >= length) {
1822 dbno = freehdr.firstdb + findex;
1823 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 }
Dave Chinner610125a2019-08-29 09:04:07 -07001826
1827 /* Didn't find free space, go on to next free block */
1828 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Dave Chinner610125a2019-08-29 09:04:07 -07001830
Dave Chinner0e822252019-08-29 09:04:07 -07001831found_block:
1832 *dbnop = dbno;
1833 *fbpp = fbp;
1834 *findexp = findex;
1835 return 0;
1836}
1837
1838
1839/*
1840 * Add the data entry for a node-format directory name addition.
1841 * The leaf entry is added in xfs_dir2_leafn_add.
1842 * We may enter with a freespace block that the lookup found.
1843 */
1844static int
1845xfs_dir2_node_addname_int(
1846 struct xfs_da_args *args, /* operation arguments */
1847 struct xfs_da_state_blk *fblk) /* optional freespace block */
1848{
1849 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1850 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1851 struct xfs_dir2_data_hdr *hdr; /* data block header */
1852 struct xfs_dir2_data_free *bf;
1853 struct xfs_dir2_free *free = NULL; /* freespace block structure */
1854 struct xfs_trans *tp = args->trans;
1855 struct xfs_inode *dp = args->dp;
1856 struct xfs_buf *dbp; /* data block buffer */
1857 struct xfs_buf *fbp; /* freespace buffer */
1858 xfs_dir2_data_aoff_t aoff;
1859 xfs_dir2_db_t dbno; /* data block number */
1860 int error; /* error return value */
1861 int findex; /* freespace entry index */
1862 int length; /* length of the new entry */
1863 int logfree = 0; /* need to log free entry */
1864 int needlog = 0; /* need to log data header */
1865 int needscan = 0; /* need to rescan data frees */
1866 __be16 *tagp; /* data entry tag pointer */
1867 __be16 *bests;
1868
1869 length = dp->d_ops->data_entsize(args->namelen);
1870 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &findex,
1871 length);
1872 if (error)
1873 return error;
1874
1875 /*
1876 * Now we know if we must allocate blocks, so if we are checking whether
1877 * we can insert without allocation then we can return now.
1878 */
1879 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1880 if (dbno == -1)
1881 return -ENOSPC;
1882 return 0;
1883 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * If we don't have a data block, we need to allocate one and make
1887 * the freespace entries refer to it.
1888 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001889 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001890 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001892 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
1893 &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001894 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001895 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001896 error = xfs_dir3_data_read(tp, dp,
1897 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001898 -1, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Dave Chinner0e822252019-08-29 09:04:07 -07001900 if (error)
1901 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001902
1903 /* setup for data block up now */
1904 hdr = dbp->b_addr;
1905 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001906 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001907
1908 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001910 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001911
1912 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001913 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1914 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1915 &needlog, &needscan);
1916 if (error) {
1917 xfs_trans_brelse(tp, dbp);
1918 return error;
1919 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001920
1921 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001923 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 dep->namelen = args->namelen;
1925 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001926 dp->d_ops->data_put_ftype(dep, args->filetype);
1927 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001928 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001929 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001930
1931 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001933 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001935 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001936
1937 /* If the freespace block entry is now wrong, update it. */
Dave Chinner0e822252019-08-29 09:04:07 -07001938 free = fbp->b_addr;
Dave Chinnera07258a2019-08-29 09:04:06 -07001939 bests = dp->d_ops->free_bests_p(free);
1940 if (bests[findex] != bf[0].length) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001941 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 logfree = 1;
1943 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001944
1945 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001947 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001948
1949 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001951 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return 0;
1953}
1954
1955/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07001956 * Top-level node form directory addname routine.
1957 */
1958int /* error */
1959xfs_dir2_node_addname(
1960 xfs_da_args_t *args) /* operation arguments */
1961{
1962 xfs_da_state_blk_t *blk; /* leaf block for insert */
1963 int error; /* error return value */
1964 int rval; /* sub-return value */
1965 xfs_da_state_t *state; /* btree cursor */
1966
1967 trace_xfs_dir2_node_addname(args);
1968
1969 /*
1970 * Allocate and initialize the state (btree cursor).
1971 */
1972 state = xfs_da_state_alloc();
1973 state->args = args;
1974 state->mp = args->dp->i_mount;
1975 /*
1976 * Look up the name. We're not supposed to find it, but
1977 * this gives us the insertion point.
1978 */
1979 error = xfs_da3_node_lookup_int(state, &rval);
1980 if (error)
1981 rval = error;
1982 if (rval != -ENOENT) {
1983 goto done;
1984 }
1985 /*
1986 * Add the data entry to a data block.
1987 * Extravalid is set to a freeblock found by lookup.
1988 */
1989 rval = xfs_dir2_node_addname_int(args,
1990 state->extravalid ? &state->extrablk : NULL);
1991 if (rval) {
1992 goto done;
1993 }
1994 blk = &state->path.blk[state->path.active - 1];
1995 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1996 /*
1997 * Add the new leaf entry.
1998 */
1999 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2000 if (rval == 0) {
2001 /*
2002 * It worked, fix the hash values up the btree.
2003 */
2004 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2005 xfs_da3_fixhashpath(state, &state->path);
2006 } else {
2007 /*
2008 * It didn't work, we need to split the leaf block.
2009 */
2010 if (args->total == 0) {
2011 ASSERT(rval == -ENOSPC);
2012 goto done;
2013 }
2014 /*
2015 * Split the leaf block and insert the new entry.
2016 */
2017 rval = xfs_da3_split(state);
2018 }
2019done:
2020 xfs_da_state_free(state);
2021 return rval;
2022}
2023
2024/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002026 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 * The only real output is the inode number of the entry.
2028 */
2029int /* error */
2030xfs_dir2_node_lookup(
2031 xfs_da_args_t *args) /* operation arguments */
2032{
2033 int error; /* error return value */
2034 int i; /* btree level */
2035 int rval; /* operation return value */
2036 xfs_da_state_t *state; /* btree cursor */
2037
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002038 trace_xfs_dir2_node_lookup(args);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /*
2041 * Allocate and initialize the btree cursor.
2042 */
2043 state = xfs_da_state_alloc();
2044 state->args = args;
2045 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /*
2047 * Fill in the path to the entry in the cursor.
2048 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002049 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (error)
2051 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002052 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2053 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002054 xfs_dir2_data_entry_t *dep;
2055
Dave Chinner1d9025e2012-06-22 18:50:14 +10002056 dep = (xfs_dir2_data_entry_t *)
2057 ((char *)state->extrablk.bp->b_addr +
2058 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002059 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
2062 * Release the btree blocks and leaf block.
2063 */
2064 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002065 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 state->path.blk[i].bp = NULL;
2067 }
2068 /*
2069 * Release the data block if we have it.
2070 */
2071 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002072 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 state->extrablk.bp = NULL;
2074 }
2075 xfs_da_state_free(state);
2076 return rval;
2077}
2078
2079/*
2080 * Remove an entry from a node-format directory.
2081 */
2082int /* error */
2083xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002084 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002086 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 int error; /* error return value */
2088 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002089 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002091 trace_xfs_dir2_node_removename(args);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /*
2094 * Allocate and initialize the btree cursor.
2095 */
2096 state = xfs_da_state_alloc();
2097 state->args = args;
2098 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002099
2100 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002101 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002102 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002103 goto out_free;
2104
2105 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002106 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002107 error = rval;
2108 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 blk = &state->path.blk[state->path.active - 1];
2112 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2113 ASSERT(state->extravalid);
2114 /*
2115 * Remove the leaf and data entries.
2116 * Extrablk refers to the data block.
2117 */
2118 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2119 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002120 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002121 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Fix the hash values up the btree.
2124 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002125 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /*
2127 * If we need to join leaf blocks, do it.
2128 */
2129 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002130 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * If no errors so far, try conversion to leaf format.
2133 */
2134 if (!error)
2135 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002136out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 xfs_da_state_free(state);
2138 return error;
2139}
2140
2141/*
2142 * Replace an entry's inode number in a node-format directory.
2143 */
2144int /* error */
2145xfs_dir2_node_replace(
2146 xfs_da_args_t *args) /* operation arguments */
2147{
2148 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002149 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_dir2_data_entry_t *dep; /* data entry changed */
2151 int error; /* error return value */
2152 int i; /* btree level */
2153 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002154 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int rval; /* internal return value */
2156 xfs_da_state_t *state; /* btree cursor */
2157
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002158 trace_xfs_dir2_node_replace(args);
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * Allocate and initialize the btree cursor.
2162 */
2163 state = xfs_da_state_alloc();
2164 state->args = args;
2165 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002166
2167 /*
2168 * We have to save new inode number and ftype since
2169 * xfs_da3_node_lookup_int() is going to overwrite them
2170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002172 ftype = args->filetype;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /*
2175 * Lookup the entry to change in the btree.
2176 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002177 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (error) {
2179 rval = error;
2180 }
2181 /*
2182 * It should be found, since the vnodeops layer has looked it up
2183 * and locked it. But paranoia is good.
2184 */
Dave Chinner24513372014-06-25 14:58:08 +10002185 if (rval == -EEXIST) {
Christoph Hellwig787b0892019-11-08 14:57:50 -08002186 struct xfs_dir3_icleaf_hdr leafhdr;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /*
2189 * Find the leaf entry.
2190 */
2191 blk = &state->path.blk[state->path.active - 1];
2192 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 ASSERT(state->extravalid);
Christoph Hellwig787b0892019-11-08 14:57:50 -08002194
2195 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2196 blk->bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
2198 * Point to the data entry.
2199 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002200 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002201 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2202 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002204 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002205 xfs_dir2_dataptr_to_off(args->geo,
Christoph Hellwig787b0892019-11-08 14:57:50 -08002206 be32_to_cpu(leafhdr.ents[blk->index].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002207 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Fill in the new inode number and log the entry.
2210 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002211 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002212 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002213 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 rval = 0;
2215 }
2216 /*
2217 * Didn't find it, and we're holding a data block. Drop it.
2218 */
2219 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002220 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 state->extrablk.bp = NULL;
2222 }
2223 /*
2224 * Release all the buffers in the cursor.
2225 */
2226 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002227 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 state->path.blk[i].bp = NULL;
2229 }
2230 xfs_da_state_free(state);
2231 return rval;
2232}
2233
2234/*
2235 * Trim off a trailing empty freespace block.
2236 * Return (in rvalp) 1 if we did it, 0 if not.
2237 */
2238int /* error */
2239xfs_dir2_node_trim_free(
2240 xfs_da_args_t *args, /* operation arguments */
2241 xfs_fileoff_t fo, /* free block number */
2242 int *rvalp) /* out: did something */
2243{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002244 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xfs_inode_t *dp; /* incore directory inode */
2246 int error; /* error return code */
2247 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002249 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002253
2254 *rvalp = 0;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 /*
2257 * Read the freespace block.
2258 */
Dave Chinner20252072012-11-12 22:54:13 +11002259 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /*
2263 * There can be holes in freespace. If fo is a hole, there's
2264 * nothing to do.
2265 */
Dave Chinner20252072012-11-12 22:54:13 +11002266 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002268 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002269 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * If there are used entries, there's nothing to do.
2273 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002274 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002275 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return 0;
2277 }
2278 /*
2279 * Blow the block away.
2280 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002281 error = xfs_dir2_shrink_inode(args,
2282 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2283 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /*
2285 * Can't fail with ENOSPC since that only happens with no
2286 * space reservation, when breaking up an extent into two
2287 * pieces. This is the last block of an extent.
2288 */
Dave Chinner24513372014-06-25 14:58:08 +10002289 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002290 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return error;
2292 }
2293 /*
2294 * Return that we succeeded.
2295 */
2296 *rvalp = 1;
2297 return 0;
2298}