blob: b9d5f4ae9a8d1f970e2ddab5915b779b05e96b82 [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
Christoph Hellwig5ba30912019-11-08 14:57:52 -080057 return xfs_dir3_leaf_check_int(dp->i_mount, &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
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800223void
224xfs_dir2_free_hdr_from_disk(
225 struct xfs_mount *mp,
226 struct xfs_dir3_icfree_hdr *to,
227 struct xfs_dir2_free *from)
228{
229 if (xfs_sb_version_hascrc(&mp->m_sb)) {
230 struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
231
232 to->magic = be32_to_cpu(from3->hdr.hdr.magic);
233 to->firstdb = be32_to_cpu(from3->hdr.firstdb);
234 to->nvalid = be32_to_cpu(from3->hdr.nvalid);
235 to->nused = be32_to_cpu(from3->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800236 to->bests = from3->bests;
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800237
238 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
239 } else {
240 to->magic = be32_to_cpu(from->hdr.magic);
241 to->firstdb = be32_to_cpu(from->hdr.firstdb);
242 to->nvalid = be32_to_cpu(from->hdr.nvalid);
243 to->nused = be32_to_cpu(from->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800244 to->bests = from->bests;
245
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800246 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
247 }
248}
249
Christoph Hellwig200dada2019-11-08 14:57:52 -0800250static void
251xfs_dir2_free_hdr_to_disk(
252 struct xfs_mount *mp,
253 struct xfs_dir2_free *to,
254 struct xfs_dir3_icfree_hdr *from)
255{
256 if (xfs_sb_version_hascrc(&mp->m_sb)) {
257 struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
258
259 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
260
261 to3->hdr.hdr.magic = cpu_to_be32(from->magic);
262 to3->hdr.firstdb = cpu_to_be32(from->firstdb);
263 to3->hdr.nvalid = cpu_to_be32(from->nvalid);
264 to3->hdr.nused = cpu_to_be32(from->nused);
265 } else {
266 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
267
268 to->hdr.magic = cpu_to_be32(from->magic);
269 to->hdr.firstdb = cpu_to_be32(from->firstdb);
270 to->hdr.nvalid = cpu_to_be32(from->nvalid);
271 to->hdr.nused = cpu_to_be32(from->nused);
272 }
273}
274
Dave Chinner20252072012-11-12 22:54:13 +1100275int
276xfs_dir2_free_read(
277 struct xfs_trans *tp,
278 struct xfs_inode *dp,
279 xfs_dablk_t fbno,
280 struct xfs_buf **bpp)
281{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100282 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100283}
284
285static int
286xfs_dir2_free_try_read(
287 struct xfs_trans *tp,
288 struct xfs_inode *dp,
289 xfs_dablk_t fbno,
290 struct xfs_buf **bpp)
291{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100292 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
293}
294
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100295static int
296xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000297 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100298 xfs_dir2_db_t fbno,
299 struct xfs_buf **bpp)
300{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000301 struct xfs_trans *tp = args->trans;
302 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100303 struct xfs_mount *mp = dp->i_mount;
304 struct xfs_buf *bp;
305 int error;
306 struct xfs_dir3_icfree_hdr hdr;
307
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000308 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100309 -1, &bp, XFS_DATA_FORK);
310 if (error)
311 return error;
312
Dave Chinner61fe1352013-04-03 16:11:30 +1100313 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100314 bp->b_ops = &xfs_dir3_free_buf_ops;
315
316 /*
317 * Initialize the new block to be empty, and remember
318 * its first slot as our empty slot.
319 */
Dave Chinnere400d272013-05-28 18:37:17 +1000320 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
321 memset(&hdr, 0, sizeof(hdr));
322
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100323 if (xfs_sb_version_hascrc(&mp->m_sb)) {
324 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
325
326 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000327
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100328 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
329 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000330 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000331 } else
332 hdr.magic = XFS_DIR2_FREE_MAGIC;
Christoph Hellwig200dada2019-11-08 14:57:52 -0800333 xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100334 *bpp = bp;
335 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
339 * Log entries from a freespace block.
340 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000341STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000343 struct xfs_da_args *args,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800344 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000345 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int first, /* first entry to log */
347 int last) /* last entry to log */
348{
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800349 struct xfs_dir2_free *free = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100351 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
352 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000353 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800354 (char *)&hdr->bests[first] - (char *)free,
355 (char *)&hdr->bests[last] - (char *)free +
356 sizeof(hdr->bests[0]) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
360 * Log header from a freespace block.
361 */
362static void
363xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000364 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000365 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Dave Chinner836a94a2013-08-12 20:49:44 +1000367#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 xfs_dir2_free_t *free; /* freespace structure */
369
Dave Chinner1d9025e2012-06-22 18:50:14 +1000370 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100371 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
372 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000373#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000374 xfs_trans_log_buf(args->trans, bp, 0,
Christoph Hellwiged1d6122019-11-08 15:01:29 -0800375 args->geo->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378/*
379 * Convert a leaf-format directory to a node-format directory.
380 * We need to change the magic number of the leaf block, and copy
381 * the freespace table out of the leaf block into its own block.
382 */
383int /* error */
384xfs_dir2_leaf_to_node(
385 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000386 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 xfs_inode_t *dp; /* incore directory inode */
389 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000390 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 xfs_dir2_db_t fdb; /* freespace block number */
Nathan Scott68b3a102006-03-17 17:27:19 +1100392 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int i; /* leaf freespace index */
394 xfs_dir2_leaf_t *leaf; /* leaf structure */
395 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int n; /* count of live freespc ents */
397 xfs_dir2_data_off_t off; /* freespace entry value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100399 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000401 trace_xfs_dir2_leaf_to_node(args);
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 tp = args->trans;
405 /*
406 * Add a freespace block to the directory.
407 */
408 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
409 return error;
410 }
Dave Chinner30028032014-06-06 15:08:18 +1000411 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Get the buffer for the new freespace block.
414 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000415 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100416 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100418
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800419 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000420 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000421 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800422 if (be32_to_cpu(ltp->bestcount) >
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700423 (uint)dp->i_d.di_size / args->geo->blksize) {
424 xfs_buf_corruption_error(lbp);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800425 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700426 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Copy freespace entries from the leaf block to the new block.
430 * Count active entries.
431 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100432 from = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800433 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
434 off = be16_to_cpu(*from);
435 if (off != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 n++;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800437 freehdr.bests[i] = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100441 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100443 freehdr.nused = n;
444 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
445
Christoph Hellwig200dada2019-11-08 14:57:52 -0800446 xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800447 xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
Dave Chinnerbc851782014-06-06 15:20:54 +1000448 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100449
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 /*
451 * Converting the leaf to a leafnode is just a matter of changing the
452 * magic number and the ops. Do the change directly to the buffer as
453 * it's less work (and less code) than decoding the header to host
454 * format and back again.
455 */
456 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
457 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
458 else
459 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
460 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100461 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000462 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100463 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
465}
466
467/*
468 * Add a leaf entry to a leaf block in a node-form directory.
469 * The other work necessary is done from the caller.
470 */
471static int /* error */
472xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000473 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800474 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int index) /* insertion pt for new entry */
476{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800477 struct xfs_dir3_icleaf_hdr leafhdr;
478 struct xfs_inode *dp = args->dp;
479 struct xfs_dir2_leaf *leaf = bp->b_addr;
480 struct xfs_dir2_leaf_entry *lep;
481 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800483 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int lfloghigh; /* high leaf entry logging */
485 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800486 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000488 trace_xfs_dir2_leafn_add(args, index);
489
Christoph Hellwig51842552019-11-08 14:57:49 -0800490 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800491 ents = leafhdr.ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /*
494 * Quick check just to make sure we are not going to index
495 * into other peoples memory
496 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700497 if (index < 0) {
498 xfs_buf_corruption_error(bp);
Dave Chinner24513372014-06-25 14:58:08 +1000499 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * If there are already the maximum number of leaf entries in
504 * the block, if there are no stale entries it won't fit.
505 * Caller will do a split. If there are stale entries we'll do
506 * a compact.
507 */
508
Christoph Hellwig478c7832019-11-08 14:57:51 -0800509 if (leafhdr.count == args->geo->leaf_max_ents) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000510 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000511 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000512 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else
514 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000515 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
516 ASSERT(index == leafhdr.count ||
517 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Barry Naujok6a178102008-05-21 16:42:05 +1000519 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521
522 /*
523 * Compact out all but one stale leaf entry. Leaves behind
524 * the entry closest to index.
525 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000526 if (compact)
527 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
528 &highstale, &lfloglow, &lfloghigh);
529 else if (leafhdr.stale) {
530 /*
531 * Set impossible logging indices for this case.
532 */
533 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 lfloghigh = -1;
535 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
538 * Insert the new entry, log everything.
539 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000540 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200541 highstale, &lfloglow, &lfloghigh);
542
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100543 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000544 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100545 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000546
Christoph Hellwig163fbbb2019-11-08 14:57:50 -0800547 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000548 xfs_dir3_leaf_log_header(args, bp);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800549 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100550 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552}
553
554#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100555static void
556xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100557 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100558 struct xfs_buf *bp,
559 xfs_dir2_db_t db)
560{
561 struct xfs_dir3_icfree_hdr hdr;
562
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800563 xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100564
Dave Chinner8f661932014-06-06 15:15:59 +1000565 ASSERT((hdr.firstdb %
566 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100567 ASSERT(hdr.firstdb <= db);
568 ASSERT(db < hdr.firstdb + hdr.nvalid);
569}
570#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100571#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#endif /* DEBUG */
573
574/*
575 * Return the last hash value in the leaf.
576 * Stale entries are ok.
577 */
578xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700579xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100580 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000581 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int *count) /* count of entries in leaf */
583{
Dave Chinner24df33b2013-04-12 07:30:21 +1000584 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Christoph Hellwig787b0892019-11-08 14:57:50 -0800586 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000587
588 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700589 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
590 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
591 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000594 *count = leafhdr.count;
595 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 0;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800597 return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601 * Look up a leaf entry for space to add a name in a node-format leaf block.
602 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000604STATIC int
605xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000606 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 xfs_da_args_t *args, /* operation arguments */
608 int *indexp, /* out: leaf entry index */
609 xfs_da_state_t *state) /* state to fill in */
610{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 xfs_dir2_db_t curdb = -1; /* current data block number */
613 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 xfs_inode_t *dp; /* incore directory inode */
615 int error; /* error return value */
616 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000617 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int index; /* leaf entry index */
619 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000620 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
622 xfs_mount_t *mp; /* filesystem mount point */
623 xfs_dir2_db_t newdb; /* new data block number */
624 xfs_dir2_db_t newfdb; /* new free block number */
625 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000626 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 dp = args->dp;
629 tp = args->trans;
630 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000631 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800632 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000633
Dave Chinner41419562013-10-29 22:11:50 +1100634 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000635 ASSERT(leafhdr.count > 0);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 /*
638 * Look up the hash value in the leaf entries.
639 */
640 index = xfs_dir2_leaf_search_hash(args, bp);
641 /*
642 * Do we have a buffer coming in?
643 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 if (state->extravalid) {
645 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000647 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000648 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100649 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
650 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100652 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * Loop over leaf entries with the right hash value.
655 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800656 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000657 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
658 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Skip stale leaf entries.
661 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100662 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 continue;
664 /*
665 * Pull the data block number from the entry.
666 */
Dave Chinner30028032014-06-06 15:08:18 +1000667 newdb = xfs_dir2_dataptr_to_db(args->geo,
668 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * For addname, we're looking for a place to put the new entry.
671 * We want to use a data block with an entry of equal
672 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000673 *
674 * If this block isn't the data block we already have
675 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 if (newdb != curdb) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800678 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100679
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000680 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000682 * Convert the data block to the free block
683 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Dave Chinner8f661932014-06-06 15:15:59 +1000685 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000689 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000691 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 */
693 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100695
696 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000697 xfs_dir2_db_to_da(args->geo,
698 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100699 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000700 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000702 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100703
Dave Chinner01ba43b2013-10-29 22:11:52 +1100704 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000707 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Dave Chinner8f661932014-06-06 15:15:59 +1000709 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000711 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800713 xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
714 if (unlikely(
715 freehdr.bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000716 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
717 XFS_ERRLEVEL_LOW, mp);
718 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000719 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000720 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000722 curfdb = newfdb;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800723 if (be16_to_cpu(freehdr.bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000727 /* Didn't find any space */
728 fi = -1;
729out:
Barry Naujok6a178102008-05-21 16:42:05 +1000730 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731 if (curbp) {
732 /* Giving back a free block. */
733 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000735 state->extrablk.index = fi;
736 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100737
738 /*
739 * Important: this magic number is not in the buffer - it's for
740 * buffer type information and therefore only the free/data type
741 * matters here, not whether CRCs are enabled or not.
742 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000743 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
744 } else {
745 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000748 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 */
750 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000751 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000755 * Look up a leaf entry in a node-format leaf block.
756 * The extrablk in state a data block.
757 */
758STATIC int
759xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000760 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000761 xfs_da_args_t *args, /* operation arguments */
762 int *indexp, /* out: leaf entry index */
763 xfs_da_state_t *state) /* state to fill in */
764{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000765 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000766 xfs_dir2_db_t curdb = -1; /* current data block number */
767 xfs_dir2_data_entry_t *dep; /* data block entry */
768 xfs_inode_t *dp; /* incore directory inode */
769 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000770 int index; /* leaf entry index */
771 xfs_dir2_leaf_t *leaf; /* leaf structure */
772 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
773 xfs_mount_t *mp; /* filesystem mount point */
774 xfs_dir2_db_t newdb; /* new data block number */
775 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000776 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000777 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000778
779 dp = args->dp;
780 tp = args->trans;
781 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000782 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800783 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000784
Dave Chinner41419562013-10-29 22:11:50 +1100785 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700786 if (leafhdr.count <= 0) {
787 xfs_buf_corruption_error(bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700788 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700789 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000790
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000791 /*
792 * Look up the hash value in the leaf entries.
793 */
794 index = xfs_dir2_leaf_search_hash(args, bp);
795 /*
796 * Do we have a buffer coming in?
797 */
798 if (state->extravalid) {
799 curbp = state->extrablk.bp;
800 curdb = state->extrablk.blkno;
801 }
802 /*
803 * Loop over leaf entries with the right hash value.
804 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800805 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000806 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
807 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000808 /*
809 * Skip stale leaf entries.
810 */
811 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
812 continue;
813 /*
814 * Pull the data block number from the entry.
815 */
Dave Chinner30028032014-06-06 15:08:18 +1000816 newdb = xfs_dir2_dataptr_to_db(args->geo,
817 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000818 /*
819 * Not adding a new entry, so we really want to find
820 * the name given to us.
821 *
822 * If it's a different data block, go get it.
823 */
824 if (newdb != curdb) {
825 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000826 * If we had a block before that we aren't saving
827 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000828 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000829 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
830 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000831 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000832 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000833 * If needing the block that is saved with a CI match,
834 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000835 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000836 if (args->cmpresult != XFS_CMP_DIFFERENT &&
837 newdb == state->extrablk.blkno) {
838 ASSERT(state->extravalid);
839 curbp = state->extrablk.bp;
840 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100841 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000842 xfs_dir2_db_to_da(args->geo,
843 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100844 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000845 if (error)
846 return error;
847 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100848 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000849 curdb = newdb;
850 }
851 /*
852 * Point to the data entry.
853 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000855 xfs_dir2_dataptr_to_off(args->geo,
856 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000857 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000858 * Compare the entry and if it's an exact match, return
859 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000860 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000861 */
Barry Naujok5163f952008-05-21 16:41:01 +1000862 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
863 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000864 /* If there is a CI match block, drop it */
865 if (args->cmpresult != XFS_CMP_DIFFERENT &&
866 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000867 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000868 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000869 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100870 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000871 *indexp = index;
872 state->extravalid = 1;
873 state->extrablk.bp = curbp;
874 state->extrablk.blkno = curdb;
875 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000876 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000877 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100878 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100879 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000880 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000881 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000882 }
883 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000884 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000885 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000886 if (args->cmpresult == XFS_CMP_DIFFERENT) {
887 /* Giving back last used data block. */
888 state->extravalid = 1;
889 state->extrablk.bp = curbp;
890 state->extrablk.index = -1;
891 state->extrablk.blkno = curdb;
892 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100893 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100894 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000895 } else {
896 /* If the curbp is not the CI match block, drop it */
897 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000898 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000899 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000900 } else {
901 state->extravalid = 0;
902 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000903 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000904 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000905}
906
907/*
908 * Look up a leaf entry in a node-format leaf block.
909 * If this is an addname then the extrablk in state is a freespace block,
910 * otherwise it's a data block.
911 */
912int
913xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000914 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000915 xfs_da_args_t *args, /* operation arguments */
916 int *indexp, /* out: leaf entry index */
917 xfs_da_state_t *state) /* state to fill in */
918{
Barry Naujok6a178102008-05-21 16:42:05 +1000919 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000920 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
921 state);
922 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
923}
924
925/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 * Move count leaf entries from source to destination leaf.
927 * Log entries and headers. Stale entries are preserved.
928 */
929static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000930xfs_dir3_leafn_moveents(
931 xfs_da_args_t *args, /* operation arguments */
932 struct xfs_buf *bp_s, /* source */
933 struct xfs_dir3_icleaf_hdr *shdr,
934 struct xfs_dir2_leaf_entry *sents,
935 int start_s,/* source leaf index */
936 struct xfs_buf *bp_d, /* destination */
937 struct xfs_dir3_icleaf_hdr *dhdr,
938 struct xfs_dir2_leaf_entry *dents,
939 int start_d,/* destination leaf index */
940 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Dave Chinner24df33b2013-04-12 07:30:21 +1000942 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000944 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
947 * Silently return if nothing to do.
948 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000949 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * If the destination index is not the end of the current
954 * destination leaf entries, open up a hole in the destination
955 * to hold the new entries.
956 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 if (start_d < dhdr->count) {
958 memmove(&dents[start_d + count], &dents[start_d],
959 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800960 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000961 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 /*
964 * If the source has stale leaves, count the ones in the copy range
965 * so we can update the header correctly.
966 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000967 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int i; /* temp leaf index */
969
970 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000971 if (sents[i].address ==
972 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 stale++;
974 }
975 } else
976 stale = 0;
977 /*
978 * Copy the leaf entries from source to destination.
979 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000980 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800982 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /*
985 * If there are source entries after the ones we copied,
986 * delete the ones we copied by sliding the next ones down.
987 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000988 if (start_s + count < shdr->count) {
989 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800991 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
992 start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 /*
996 * Update the headers and log them.
997 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000998 shdr->count -= count;
999 shdr->stale -= stale;
1000 dhdr->count += count;
1001 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/*
1005 * Determine the sort order of two leaf blocks.
1006 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1007 */
1008int /* sort order */
1009xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +11001010 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +10001011 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
1012 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Dave Chinner24df33b2013-04-12 07:30:21 +10001014 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1015 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1016 struct xfs_dir2_leaf_entry *ents1;
1017 struct xfs_dir2_leaf_entry *ents2;
1018 struct xfs_dir3_icleaf_hdr hdr1;
1019 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Christoph Hellwig51842552019-11-08 14:57:49 -08001021 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1022 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001023 ents1 = hdr1.ents;
1024 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001025
1026 if (hdr1.count > 0 && hdr2.count > 0 &&
1027 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1028 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1029 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return 1;
1031 return 0;
1032}
1033
1034/*
1035 * Rebalance leaf entries between two leaf blocks.
1036 * This is actually only called when the second block is new,
1037 * though the code deals with the general case.
1038 * A new entry will be inserted in one of the blocks, and that
1039 * entry is taken into account when balancing.
1040 */
1041static void
1042xfs_dir2_leafn_rebalance(
1043 xfs_da_state_t *state, /* btree cursor */
1044 xfs_da_state_blk_t *blk1, /* first btree block */
1045 xfs_da_state_blk_t *blk2) /* second btree block */
1046{
1047 xfs_da_args_t *args; /* operation arguments */
1048 int count; /* count (& direction) leaves */
1049 int isleft; /* new goes in left leaf */
1050 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1051 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1052 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001053#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 int oldstale; /* old count of stale leaves */
1055#endif
1056 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001057 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001058 struct xfs_dir2_leaf_entry *ents1;
1059 struct xfs_dir2_leaf_entry *ents2;
1060 struct xfs_dir3_icleaf_hdr hdr1;
1061 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001062 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 args = state->args;
1065 /*
1066 * If the block order is wrong, swap the arguments.
1067 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001068 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1069 if (swap_blocks)
1070 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Chinner1d9025e2012-06-22 18:50:14 +10001072 leaf1 = blk1->bp->b_addr;
1073 leaf2 = blk2->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001074 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1075 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001076 ents1 = hdr1.ents;
1077 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001078
1079 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001080#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001081 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082#endif
1083 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /*
1086 * If the old leaf count was odd then the new one will be even,
1087 * so we need to divide the new count evenly.
1088 */
1089 if (oldsum & 1) {
1090 xfs_dahash_t midhash; /* middle entry hash value */
1091
Dave Chinner24df33b2013-04-12 07:30:21 +10001092 if (mid >= hdr1.count)
1093 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001095 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 isleft = args->hashval <= midhash;
1097 }
1098 /*
1099 * If the old count is even then the new count is odd, so there's
1100 * no preferred side for the new entry.
1101 * Pick the left one.
1102 */
1103 else
1104 isleft = 1;
1105 /*
1106 * Calculate moved entry count. Positive means left-to-right,
1107 * negative means right-to-left. Then move the entries.
1108 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001111 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1112 hdr1.count - count, blk2->bp,
1113 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001115 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1116 blk1->bp, &hdr1, ents1,
1117 hdr1.count, count);
1118
1119 ASSERT(hdr1.count + hdr2.count == oldsum);
1120 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1121
1122 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001123 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1124 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001125 xfs_dir3_leaf_log_header(args, blk1->bp);
1126 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001127
Dave Chinner41419562013-10-29 22:11:50 +11001128 xfs_dir3_leaf_check(dp, blk1->bp);
1129 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * Mark whether we're inserting into the old or new leaf.
1133 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001134 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001135 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001136 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001137 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001139 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * Adjust the expected index for insertion.
1142 */
1143 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001144 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001145
1146 /*
1147 * Finally sanity check just to make sure we are not returning a
1148 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Dave Chinner41419562013-10-29 22:11:50 +11001150 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 state->inleaf = 1;
1152 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001153 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001154 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001155 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157}
1158
Dave Chinner20252072012-11-12 22:54:13 +11001159static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001160xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001161 xfs_da_args_t *args,
1162 struct xfs_dir2_data_hdr *hdr,
1163 struct xfs_dir2_free *free,
1164 xfs_dir2_db_t fdb,
1165 int findex,
1166 struct xfs_buf *fbp,
1167 int longest)
1168{
Dave Chinner20252072012-11-12 22:54:13 +11001169 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001170 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001171 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001172
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001173 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001174 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001175 /*
1176 * Data block is not empty, just set the free entry to the new
1177 * value.
1178 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001179 freehdr.bests[findex] = cpu_to_be16(longest);
1180 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001181 return 0;
1182 }
1183
1184 /* One less used entry in the free table. */
1185 freehdr.nused--;
1186
1187 /*
1188 * If this was the last entry in the table, we can trim the table size
1189 * back. There might be other entries at the end referring to
1190 * non-existent data blocks, get those too.
1191 */
1192 if (findex == freehdr.nvalid - 1) {
1193 int i; /* free entry index */
1194
1195 for (i = findex - 1; i >= 0; i--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001196 if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001197 break;
1198 }
1199 freehdr.nvalid = i + 1;
1200 logfree = 0;
1201 } else {
1202 /* Not the last entry, just punch it out. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001203 freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001204 logfree = 1;
1205 }
1206
Christoph Hellwig200dada2019-11-08 14:57:52 -08001207 xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001208 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001209
1210 /*
1211 * If there are no useful entries left in the block, get rid of the
1212 * block if we can.
1213 */
1214 if (!freehdr.nused) {
1215 int error;
1216
1217 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1218 if (error == 0) {
1219 fbp = NULL;
1220 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001221 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001222 return error;
1223 /*
1224 * It's possible to get ENOSPC if there is no
1225 * space reservation. In this case some one
1226 * else will eventually get rid of this block.
1227 */
1228 }
1229
Dave Chinner20252072012-11-12 22:54:13 +11001230 /* Log the free entry that changed, unless we got rid of it. */
1231 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001232 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001233 return 0;
1234}
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236/*
1237 * Remove an entry from a node directory.
1238 * This removes the leaf entry and the data entry,
1239 * and updates the free block if necessary.
1240 */
1241static int /* error */
1242xfs_dir2_leafn_remove(
1243 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001244 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int index, /* leaf entry index */
1246 xfs_da_state_blk_t *dblk, /* data block */
1247 int *rval) /* resulting block needs join */
1248{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001249 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001251 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 xfs_dir2_data_entry_t *dep; /* data block entry */
1253 xfs_inode_t *dp; /* incore directory inode */
1254 xfs_dir2_leaf_t *leaf; /* leaf structure */
1255 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1256 int longest; /* longest data free entry */
1257 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int needlog; /* need to log data header */
1259 int needscan; /* need to rescan data frees */
1260 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001261 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001262 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001264 trace_xfs_dir2_leafn_remove(args, index);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 dp = args->dp;
1267 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001268 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001269 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Point to the entry we're removing.
1273 */
Christoph Hellwig787b0892019-11-08 14:57:50 -08001274 lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +10001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Extract the data block and offset from the entry.
1278 */
Dave Chinner30028032014-06-06 15:08:18 +10001279 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001281 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Kill the leaf entry by marking it stale.
1286 * Log the leaf block changes.
1287 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001288 leafhdr.stale++;
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001289 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001290 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001291
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001292 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001293 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /*
1296 * Make the data entry free. Keep track of the longest freespace
1297 * in the data block in case it changes.
1298 */
1299 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001300 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001301 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001302 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001303 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001305 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001306 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 /*
1308 * Rescan the data block freespaces for bestfree.
1309 * Log the data block header if needed.
1310 */
1311 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001312 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001314 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001315 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
1317 * If the longest data block freespace changes, need to update
1318 * the corresponding freeblock entry.
1319 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001320 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001322 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfs_dir2_db_t fdb; /* freeblock block number */
1324 int findex; /* index in freeblock entries */
1325 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /*
1328 * Convert the data block number to a free block,
1329 * read in the free block.
1330 */
Dave Chinner8f661932014-06-06 15:15:59 +10001331 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001332 error = xfs_dir2_free_read(tp, dp,
1333 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001334 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001335 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001337 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001338#ifdef DEBUG
1339 {
1340 struct xfs_dir3_icfree_hdr freehdr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001341
1342 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001343 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001344 (fdb - xfs_dir2_byte_to_db(args->geo,
1345 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001346 }
1347#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
1349 * Calculate which entry we need to fix.
1350 */
Dave Chinner8f661932014-06-06 15:15:59 +10001351 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001352 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /*
1354 * If the data block is now empty we can get rid of it
1355 * (usually).
1356 */
Dave Chinner8f661932014-06-06 15:15:59 +10001357 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001358 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Try to punch out the data block.
1361 */
1362 error = xfs_dir2_shrink_inode(args, db, dbp);
1363 if (error == 0) {
1364 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001365 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 /*
1368 * We can get ENOSPC if there's no space reservation.
1369 * In this case just drop the buffer and some one else
1370 * will eventually get rid of the empty block.
1371 */
Dave Chinner24513372014-06-25 14:58:08 +10001372 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return error;
1374 }
1375 /*
1376 * If we got rid of the data block, we can eliminate that entry
1377 * in the free block.
1378 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001379 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001380 fdb, findex, fbp, longest);
1381 if (error)
1382 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Dave Chinner20252072012-11-12 22:54:13 +11001384
Dave Chinner41419562013-10-29 22:11:50 +11001385 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001387 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 * to justify trying to join it with a neighbor.
1389 */
Christoph Hellwig545910b2019-11-08 14:57:51 -08001390 *rval = (args->geo->leaf_hdr_size +
Christoph Hellwig787b0892019-11-08 14:57:50 -08001391 (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001392 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
1394}
1395
1396/*
1397 * Split the leaf entries in the old block into old and new blocks.
1398 */
1399int /* error */
1400xfs_dir2_leafn_split(
1401 xfs_da_state_t *state, /* btree cursor */
1402 xfs_da_state_blk_t *oldblk, /* original block */
1403 xfs_da_state_blk_t *newblk) /* newly created block */
1404{
1405 xfs_da_args_t *args; /* operation arguments */
1406 xfs_dablk_t blkno; /* new leaf block number */
1407 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001408 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
1411 * Allocate space for a new leaf node.
1412 */
1413 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001414 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1416 error = xfs_da_grow_inode(args, &blkno);
1417 if (error) {
1418 return error;
1419 }
1420 /*
1421 * Initialize the new leaf block.
1422 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001423 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001424 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1425 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 newblk->blkno = blkno;
1429 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1430 /*
1431 * Rebalance the entries across the two leaves, link the new
1432 * block into the leaves.
1433 */
1434 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001435 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (error) {
1437 return error;
1438 }
1439 /*
1440 * Insert the new entry in the correct block.
1441 */
1442 if (state->inleaf)
1443 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1444 else
1445 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1446 /*
1447 * Update last hashval in each block since we added the name.
1448 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001449 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1450 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001451 xfs_dir3_leaf_check(dp, oldblk->bp);
1452 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return error;
1454}
1455
1456/*
1457 * Check a leaf block and its neighbors to see if the block should be
1458 * collapsed into one or the other neighbor. Always keep the block
1459 * with the smaller block number.
1460 * If the current block is over 50% full, don't try to join it, return 0.
1461 * If the block is empty, fill in the state structure and return 2.
1462 * If it can be collapsed, fill in the state structure and return 1.
1463 * If nothing can be done, return 0.
1464 */
1465int /* error */
1466xfs_dir2_leafn_toosmall(
1467 xfs_da_state_t *state, /* btree cursor */
1468 int *action) /* resulting action to take */
1469{
1470 xfs_da_state_blk_t *blk; /* leaf block */
1471 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001472 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 int bytes; /* bytes in use */
1474 int count; /* leaf live entry count */
1475 int error; /* error return value */
1476 int forward; /* sibling block direction */
1477 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 xfs_dir2_leaf_t *leaf; /* leaf structure */
1479 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001480 struct xfs_dir3_icleaf_hdr leafhdr;
1481 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001482 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /*
1485 * Check for the degenerate case of the block being over 50% full.
1486 * If so, it's not worth even looking to see if we might be able
1487 * to coalesce with a sibling.
1488 */
1489 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001490 leaf = blk->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001491 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001492 ents = leafhdr.ents;
Dave Chinner41419562013-10-29 22:11:50 +11001493 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001494
1495 count = leafhdr.count - leafhdr.stale;
Christoph Hellwig545910b2019-11-08 14:57:51 -08001496 bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001497 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 /*
1499 * Blk over 50%, don't try to join.
1500 */
1501 *action = 0;
1502 return 0;
1503 }
1504 /*
1505 * Check for the degenerate case of the block being empty.
1506 * If the block is empty, we'll simply delete it, no need to
1507 * coalesce it with a sibling block. We choose (arbitrarily)
1508 * to merge with the forward block unless it is NULL.
1509 */
1510 if (count == 0) {
1511 /*
1512 * Make altpath point to the block we want to keep and
1513 * path point to the block we want to drop (this one).
1514 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001515 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001517 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 &rval);
1519 if (error)
1520 return error;
1521 *action = rval ? 2 : 0;
1522 return 0;
1523 }
1524 /*
1525 * Examine each sibling block to see if we can coalesce with
1526 * at least 25% free space to spare. We need to figure out
1527 * whether to merge with the forward or the backward block.
1528 * We prefer coalescing with the lower numbered sibling so as
1529 * to shrink a directory over time.
1530 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001531 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001533 struct xfs_dir3_icleaf_hdr hdr2;
1534
1535 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (blkno == 0)
1537 continue;
1538 /*
1539 * Read the sibling leaf block.
1540 */
Dave Chinner41419562013-10-29 22:11:50 +11001541 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001542 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001543 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
1547 * Count bytes in the two blocks combined.
1548 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001549 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001550 bytes = state->args->geo->blksize -
1551 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001552
Dave Chinner1d9025e2012-06-22 18:50:14 +10001553 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001554 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001555 ents = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001556 count += hdr2.count - hdr2.stale;
1557 bytes -= count * sizeof(ents[0]);
1558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /*
1560 * Fits with at least 25% to spare.
1561 */
1562 if (bytes >= 0)
1563 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001564 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566 /*
1567 * Didn't like either block, give up.
1568 */
1569 if (i >= 2) {
1570 *action = 0;
1571 return 0;
1572 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * Make altpath point to the block we want to keep (the lower
1576 * numbered block) and path point to the block we want to drop.
1577 */
1578 memcpy(&state->altpath, &state->path, sizeof(state->path));
1579 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001580 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 &rval);
1582 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001583 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 &rval);
1585 if (error) {
1586 return error;
1587 }
1588 *action = rval ? 0 : 1;
1589 return 0;
1590}
1591
1592/*
1593 * Move all the leaf entries from drop_blk to save_blk.
1594 * This is done as part of a join operation.
1595 */
1596void
1597xfs_dir2_leafn_unbalance(
1598 xfs_da_state_t *state, /* cursor */
1599 xfs_da_state_blk_t *drop_blk, /* dead block */
1600 xfs_da_state_blk_t *save_blk) /* surviving block */
1601{
1602 xfs_da_args_t *args; /* operation arguments */
1603 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1604 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001605 struct xfs_dir3_icleaf_hdr savehdr;
1606 struct xfs_dir3_icleaf_hdr drophdr;
1607 struct xfs_dir2_leaf_entry *sents;
1608 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001609 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
1611 args = state->args;
1612 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1613 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001614 drop_leaf = drop_blk->bp->b_addr;
1615 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001616
Christoph Hellwig51842552019-11-08 14:57:49 -08001617 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1618 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001619 sents = savehdr.ents;
1620 dents = drophdr.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /*
1623 * If there are any stale leaf entries, take this opportunity
1624 * to purge them.
1625 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001626 if (drophdr.stale)
1627 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1628 if (savehdr.stale)
1629 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Move the entries from drop to the appropriate end of save.
1633 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001634 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001635 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001636 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1637 save_blk->bp, &savehdr, sents, 0,
1638 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001640 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1641 save_blk->bp, &savehdr, sents,
1642 savehdr.count, drophdr.count);
1643 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1644
1645 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001646 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1647 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001648 xfs_dir3_leaf_log_header(args, save_blk->bp);
1649 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001650
Dave Chinner41419562013-10-29 22:11:50 +11001651 xfs_dir3_leaf_check(dp, save_blk->bp);
1652 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001656 * Add a new data block to the directory at the free space index that the caller
1657 * has specified.
1658 */
1659static int
1660xfs_dir2_node_add_datablk(
1661 struct xfs_da_args *args,
1662 struct xfs_da_state_blk *fblk,
1663 xfs_dir2_db_t *dbno,
1664 struct xfs_buf **dbpp,
1665 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001666 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinnera07258a2019-08-29 09:04:06 -07001667 int *findex)
1668{
1669 struct xfs_inode *dp = args->dp;
1670 struct xfs_trans *tp = args->trans;
1671 struct xfs_mount *mp = dp->i_mount;
Dave Chinnera07258a2019-08-29 09:04:06 -07001672 struct xfs_dir2_data_free *bf;
Dave Chinnera07258a2019-08-29 09:04:06 -07001673 xfs_dir2_db_t fbno;
1674 struct xfs_buf *fbp;
1675 struct xfs_buf *dbp;
Dave Chinnera07258a2019-08-29 09:04:06 -07001676 int error;
1677
1678 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001679 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001680 return -ENOSPC;
1681
1682 /* Allocate and initialize the new data block. */
1683 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1684 if (error)
1685 return error;
1686 error = xfs_dir3_data_init(args, *dbno, &dbp);
1687 if (error)
1688 return error;
1689
1690 /*
1691 * Get the freespace block corresponding to the data block
1692 * that was just allocated.
1693 */
1694 fbno = dp->d_ops->db_to_fdb(args->geo, *dbno);
1695 error = xfs_dir2_free_try_read(tp, dp,
1696 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1697 if (error)
1698 return error;
1699
1700 /*
1701 * If there wasn't a freespace block, the read will
1702 * return a NULL fbp. Allocate and initialize a new one.
1703 */
1704 if (!fbp) {
1705 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1706 if (error)
1707 return error;
1708
1709 if (dp->d_ops->db_to_fdb(args->geo, *dbno) != fbno) {
1710 xfs_alert(mp,
1711"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1712 __func__, (unsigned long long)dp->i_ino,
1713 (long long)dp->d_ops->db_to_fdb(args->geo, *dbno),
1714 (long long)*dbno, (long long)fbno);
1715 if (fblk) {
1716 xfs_alert(mp,
1717 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1718 fblk, (unsigned long long)fblk->blkno,
1719 fblk->index, fblk->magic);
1720 } else {
1721 xfs_alert(mp, " ... fblk is NULL");
1722 }
1723 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1724 return -EFSCORRUPTED;
1725 }
1726
1727 /* Get a buffer for the new block. */
1728 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1729 if (error)
1730 return error;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001731 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001732
1733 /* Remember the first slot as our empty slot. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001734 hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinnera07258a2019-08-29 09:04:06 -07001735 XFS_DIR2_FREE_OFFSET)) *
1736 dp->d_ops->free_max_bests(args->geo);
1737 } else {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001738 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001739 }
1740
1741 /* Set the freespace block index from the data block number. */
1742 *findex = dp->d_ops->db_to_fdindex(args->geo, *dbno);
1743
1744 /* Extend the freespace table if the new data block is off the end. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001745 if (*findex >= hdr->nvalid) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001746 ASSERT(*findex < dp->d_ops->free_max_bests(args->geo));
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001747 hdr->nvalid = *findex + 1;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001748 hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinnera07258a2019-08-29 09:04:06 -07001749 }
1750
1751 /*
1752 * If this entry was for an empty data block (this should always be
1753 * true) then update the header.
1754 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001755 if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001756 hdr->nused++;
1757 xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001758 xfs_dir2_free_log_header(args, fbp);
1759 }
1760
1761 /* Update the freespace value for the new block in the table. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001762 bf = dp->d_ops->data_bestfree_p(dbp->b_addr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001763 hdr->bests[*findex] = bf[0].length;
Dave Chinnera07258a2019-08-29 09:04:06 -07001764
1765 *dbpp = dbp;
1766 *fbpp = fbp;
1767 return 0;
1768}
1769
Dave Chinner0e822252019-08-29 09:04:07 -07001770static int
1771xfs_dir2_node_find_freeblk(
1772 struct xfs_da_args *args,
1773 struct xfs_da_state_blk *fblk,
1774 xfs_dir2_db_t *dbnop,
1775 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001776 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner0e822252019-08-29 09:04:07 -07001777 int *findexp,
1778 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Dave Chinner0e822252019-08-29 09:04:07 -07001780 struct xfs_inode *dp = args->dp;
1781 struct xfs_trans *tp = args->trans;
1782 struct xfs_buf *fbp = NULL;
Dave Chinner756c6f02019-08-29 09:04:08 -07001783 xfs_dir2_db_t firstfbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001784 xfs_dir2_db_t lastfbno;
1785 xfs_dir2_db_t ifbno = -1;
1786 xfs_dir2_db_t dbno = -1;
Dave Chinner756c6f02019-08-29 09:04:08 -07001787 xfs_dir2_db_t fbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001788 xfs_fileoff_t fo;
Dave Chinner610125a2019-08-29 09:04:07 -07001789 int findex = 0;
Dave Chinner0e822252019-08-29 09:04:07 -07001790 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /*
1793 * If we came in with a freespace block that means that lookup
1794 * found an entry with our hash value. This is the freespace
1795 * block for that data entry.
1796 */
1797 if (fblk) {
1798 fbp = fblk->bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 findex = fblk->index;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001800 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001802 /* caller already found the freespace for us. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001803 ASSERT(findex < hdr->nvalid);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001804 ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1805 ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001806 dbno = hdr->firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001807 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Dave Chinner0e822252019-08-29 09:04:07 -07001809
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001810 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001811 * The data block looked at didn't have enough room.
1812 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001813 */
Dave Chinner0e822252019-08-29 09:04:07 -07001814 ifbno = fblk->blkno;
Dave Chinner610125a2019-08-29 09:04:07 -07001815 xfs_trans_brelse(tp, fbp);
1816 fbp = NULL;
1817 fblk->bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Dave Chinner0e822252019-08-29 09:04:07 -07001819
1820 /*
1821 * If we don't have a data block yet, we're going to scan the freespace
Dave Chinner610125a2019-08-29 09:04:07 -07001822 * data for a data block with enough free space in it.
Dave Chinner0e822252019-08-29 09:04:07 -07001823 */
1824 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1825 if (error)
1826 return error;
1827 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Dave Chinner756c6f02019-08-29 09:04:08 -07001828 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0e822252019-08-29 09:04:07 -07001829
Dave Chinner756c6f02019-08-29 09:04:08 -07001830 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001831 /* If it's ifbno we already looked at it. */
1832 if (fbno == ifbno)
1833 continue;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /*
Dave Chinner610125a2019-08-29 09:04:07 -07001836 * Read the block. There can be holes in the freespace blocks,
1837 * so this might not succeed. This should be really rare, so
1838 * there's no reason to avoid it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 */
Dave Chinner610125a2019-08-29 09:04:07 -07001840 error = xfs_dir2_free_try_read(tp, dp,
1841 xfs_dir2_db_to_da(args->geo, fbno),
1842 &fbp);
1843 if (error)
1844 return error;
1845 if (!fbp)
1846 continue;
1847
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001848 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Dave Chinner610125a2019-08-29 09:04:07 -07001849
1850 /* Scan the free entry array for a large enough free space. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001851 for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001852 if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1853 be16_to_cpu(hdr->bests[findex]) >= length) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001854 dbno = hdr->firstdb + findex;
Dave Chinner610125a2019-08-29 09:04:07 -07001855 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
1857 }
Dave Chinner610125a2019-08-29 09:04:07 -07001858
1859 /* Didn't find free space, go on to next free block */
1860 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Dave Chinner610125a2019-08-29 09:04:07 -07001862
Dave Chinner0e822252019-08-29 09:04:07 -07001863found_block:
1864 *dbnop = dbno;
1865 *fbpp = fbp;
1866 *findexp = findex;
1867 return 0;
1868}
1869
Dave Chinner0e822252019-08-29 09:04:07 -07001870/*
1871 * Add the data entry for a node-format directory name addition.
1872 * The leaf entry is added in xfs_dir2_leafn_add.
1873 * We may enter with a freespace block that the lookup found.
1874 */
1875static int
1876xfs_dir2_node_addname_int(
1877 struct xfs_da_args *args, /* operation arguments */
1878 struct xfs_da_state_blk *fblk) /* optional freespace block */
1879{
1880 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1881 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1882 struct xfs_dir2_data_hdr *hdr; /* data block header */
1883 struct xfs_dir2_data_free *bf;
Dave Chinner0e822252019-08-29 09:04:07 -07001884 struct xfs_trans *tp = args->trans;
1885 struct xfs_inode *dp = args->dp;
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001886 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001887 struct xfs_buf *dbp; /* data block buffer */
1888 struct xfs_buf *fbp; /* freespace buffer */
1889 xfs_dir2_data_aoff_t aoff;
1890 xfs_dir2_db_t dbno; /* data block number */
1891 int error; /* error return value */
1892 int findex; /* freespace entry index */
1893 int length; /* length of the new entry */
1894 int logfree = 0; /* need to log free entry */
1895 int needlog = 0; /* need to log data header */
1896 int needscan = 0; /* need to rescan data frees */
1897 __be16 *tagp; /* data entry tag pointer */
Dave Chinner0e822252019-08-29 09:04:07 -07001898
1899 length = dp->d_ops->data_entsize(args->namelen);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001900 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1901 &findex, length);
Dave Chinner0e822252019-08-29 09:04:07 -07001902 if (error)
1903 return error;
1904
1905 /*
1906 * Now we know if we must allocate blocks, so if we are checking whether
1907 * we can insert without allocation then we can return now.
1908 */
1909 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1910 if (dbno == -1)
1911 return -ENOSPC;
1912 return 0;
1913 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /*
1916 * If we don't have a data block, we need to allocate one and make
1917 * the freespace entries refer to it.
1918 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001919 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001920 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001922 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001923 &freehdr, &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001924 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001925 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001926 error = xfs_dir3_data_read(tp, dp,
1927 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001928 -1, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
Dave Chinner0e822252019-08-29 09:04:07 -07001930 if (error)
1931 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001932
1933 /* setup for data block up now */
1934 hdr = dbp->b_addr;
1935 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001936 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001937
1938 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001940 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001941
1942 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001943 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1944 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1945 &needlog, &needscan);
1946 if (error) {
1947 xfs_trans_brelse(tp, dbp);
1948 return error;
1949 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001950
1951 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001953 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 dep->namelen = args->namelen;
1955 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001956 dp->d_ops->data_put_ftype(dep, args->filetype);
1957 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001958 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001959 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001960
1961 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001963 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001965 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001966
1967 /* If the freespace block entry is now wrong, update it. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001968 if (freehdr.bests[findex] != bf[0].length) {
1969 freehdr.bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 logfree = 1;
1971 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001972
1973 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001975 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001976
1977 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001979 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return 0;
1981}
1982
1983/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07001984 * Top-level node form directory addname routine.
1985 */
1986int /* error */
1987xfs_dir2_node_addname(
1988 xfs_da_args_t *args) /* operation arguments */
1989{
1990 xfs_da_state_blk_t *blk; /* leaf block for insert */
1991 int error; /* error return value */
1992 int rval; /* sub-return value */
1993 xfs_da_state_t *state; /* btree cursor */
1994
1995 trace_xfs_dir2_node_addname(args);
1996
1997 /*
1998 * Allocate and initialize the state (btree cursor).
1999 */
2000 state = xfs_da_state_alloc();
2001 state->args = args;
2002 state->mp = args->dp->i_mount;
2003 /*
2004 * Look up the name. We're not supposed to find it, but
2005 * this gives us the insertion point.
2006 */
2007 error = xfs_da3_node_lookup_int(state, &rval);
2008 if (error)
2009 rval = error;
2010 if (rval != -ENOENT) {
2011 goto done;
2012 }
2013 /*
2014 * Add the data entry to a data block.
2015 * Extravalid is set to a freeblock found by lookup.
2016 */
2017 rval = xfs_dir2_node_addname_int(args,
2018 state->extravalid ? &state->extrablk : NULL);
2019 if (rval) {
2020 goto done;
2021 }
2022 blk = &state->path.blk[state->path.active - 1];
2023 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2024 /*
2025 * Add the new leaf entry.
2026 */
2027 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2028 if (rval == 0) {
2029 /*
2030 * It worked, fix the hash values up the btree.
2031 */
2032 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2033 xfs_da3_fixhashpath(state, &state->path);
2034 } else {
2035 /*
2036 * It didn't work, we need to split the leaf block.
2037 */
2038 if (args->total == 0) {
2039 ASSERT(rval == -ENOSPC);
2040 goto done;
2041 }
2042 /*
2043 * Split the leaf block and insert the new entry.
2044 */
2045 rval = xfs_da3_split(state);
2046 }
2047done:
2048 xfs_da_state_free(state);
2049 return rval;
2050}
2051
2052/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002054 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 * The only real output is the inode number of the entry.
2056 */
2057int /* error */
2058xfs_dir2_node_lookup(
2059 xfs_da_args_t *args) /* operation arguments */
2060{
2061 int error; /* error return value */
2062 int i; /* btree level */
2063 int rval; /* operation return value */
2064 xfs_da_state_t *state; /* btree cursor */
2065
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002066 trace_xfs_dir2_node_lookup(args);
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * Allocate and initialize the btree cursor.
2070 */
2071 state = xfs_da_state_alloc();
2072 state->args = args;
2073 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * Fill in the path to the entry in the cursor.
2076 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002077 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (error)
2079 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002080 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2081 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002082 xfs_dir2_data_entry_t *dep;
2083
Dave Chinner1d9025e2012-06-22 18:50:14 +10002084 dep = (xfs_dir2_data_entry_t *)
2085 ((char *)state->extrablk.bp->b_addr +
2086 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002087 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * Release the btree blocks and leaf block.
2091 */
2092 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002093 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 state->path.blk[i].bp = NULL;
2095 }
2096 /*
2097 * Release the data block if we have it.
2098 */
2099 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002100 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 state->extrablk.bp = NULL;
2102 }
2103 xfs_da_state_free(state);
2104 return rval;
2105}
2106
2107/*
2108 * Remove an entry from a node-format directory.
2109 */
2110int /* error */
2111xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002112 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002114 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 int error; /* error return value */
2116 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002117 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002119 trace_xfs_dir2_node_removename(args);
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /*
2122 * Allocate and initialize the btree cursor.
2123 */
2124 state = xfs_da_state_alloc();
2125 state->args = args;
2126 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002127
2128 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002129 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002130 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002131 goto out_free;
2132
2133 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002134 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002135 error = rval;
2136 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 blk = &state->path.blk[state->path.active - 1];
2140 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2141 ASSERT(state->extravalid);
2142 /*
2143 * Remove the leaf and data entries.
2144 * Extrablk refers to the data block.
2145 */
2146 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2147 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002148 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002149 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * Fix the hash values up the btree.
2152 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002153 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * If we need to join leaf blocks, do it.
2156 */
2157 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002158 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
2160 * If no errors so far, try conversion to leaf format.
2161 */
2162 if (!error)
2163 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002164out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 xfs_da_state_free(state);
2166 return error;
2167}
2168
2169/*
2170 * Replace an entry's inode number in a node-format directory.
2171 */
2172int /* error */
2173xfs_dir2_node_replace(
2174 xfs_da_args_t *args) /* operation arguments */
2175{
2176 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002177 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 xfs_dir2_data_entry_t *dep; /* data entry changed */
2179 int error; /* error return value */
2180 int i; /* btree level */
2181 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002182 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int rval; /* internal return value */
2184 xfs_da_state_t *state; /* btree cursor */
2185
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002186 trace_xfs_dir2_node_replace(args);
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 /*
2189 * Allocate and initialize the btree cursor.
2190 */
2191 state = xfs_da_state_alloc();
2192 state->args = args;
2193 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002194
2195 /*
2196 * We have to save new inode number and ftype since
2197 * xfs_da3_node_lookup_int() is going to overwrite them
2198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002200 ftype = args->filetype;
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /*
2203 * Lookup the entry to change in the btree.
2204 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002205 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (error) {
2207 rval = error;
2208 }
2209 /*
2210 * It should be found, since the vnodeops layer has looked it up
2211 * and locked it. But paranoia is good.
2212 */
Dave Chinner24513372014-06-25 14:58:08 +10002213 if (rval == -EEXIST) {
Christoph Hellwig787b0892019-11-08 14:57:50 -08002214 struct xfs_dir3_icleaf_hdr leafhdr;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
2217 * Find the leaf entry.
2218 */
2219 blk = &state->path.blk[state->path.active - 1];
2220 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 ASSERT(state->extravalid);
Christoph Hellwig787b0892019-11-08 14:57:50 -08002222
2223 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2224 blk->bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /*
2226 * Point to the data entry.
2227 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002228 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002229 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2230 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002232 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002233 xfs_dir2_dataptr_to_off(args->geo,
Christoph Hellwig787b0892019-11-08 14:57:50 -08002234 be32_to_cpu(leafhdr.ents[blk->index].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002235 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 /*
2237 * Fill in the new inode number and log the entry.
2238 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002239 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002240 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002241 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 rval = 0;
2243 }
2244 /*
2245 * Didn't find it, and we're holding a data block. Drop it.
2246 */
2247 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002248 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 state->extrablk.bp = NULL;
2250 }
2251 /*
2252 * Release all the buffers in the cursor.
2253 */
2254 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002255 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 state->path.blk[i].bp = NULL;
2257 }
2258 xfs_da_state_free(state);
2259 return rval;
2260}
2261
2262/*
2263 * Trim off a trailing empty freespace block.
2264 * Return (in rvalp) 1 if we did it, 0 if not.
2265 */
2266int /* error */
2267xfs_dir2_node_trim_free(
2268 xfs_da_args_t *args, /* operation arguments */
2269 xfs_fileoff_t fo, /* free block number */
2270 int *rvalp) /* out: did something */
2271{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002272 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 xfs_inode_t *dp; /* incore directory inode */
2274 int error; /* error return code */
2275 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002277 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002281
2282 *rvalp = 0;
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 /*
2285 * Read the freespace block.
2286 */
Dave Chinner20252072012-11-12 22:54:13 +11002287 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002288 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 /*
2291 * There can be holes in freespace. If fo is a hole, there's
2292 * nothing to do.
2293 */
Dave Chinner20252072012-11-12 22:54:13 +11002294 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002296 free = bp->b_addr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08002297 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /*
2300 * If there are used entries, there's nothing to do.
2301 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002302 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002303 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return 0;
2305 }
2306 /*
2307 * Blow the block away.
2308 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002309 error = xfs_dir2_shrink_inode(args,
2310 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2311 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Can't fail with ENOSPC since that only happens with no
2314 * space reservation, when breaking up an extent into two
2315 * pieces. This is the last block of an extent.
2316 */
Dave Chinner24513372014-06-25 14:58:08 +10002317 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002318 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return error;
2320 }
2321 /*
2322 * Return that we succeeded.
2323 */
2324 *rvalp = 1;
2325 return 0;
2326}