blob: 76d19b522e923af002020f174ff5ddcd5a90c370 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07009#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110010#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110011#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_inode.h"
15#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100016#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020017#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000019#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110021#include "xfs_buf_item.h"
Brian Fostera45086e2015-10-12 15:59:25 +110022#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Function declarations.
26 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100027static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
28 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
30 xfs_da_state_blk_t *blk1,
31 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100032static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 int index, xfs_da_state_blk_t *dblk,
34 int *rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Dave Chinner24df33b2013-04-12 07:30:21 +100036/*
Christoph Hellwig3d92c932019-11-08 15:01:39 -080037 * Convert data space db to the corresponding free db.
38 */
39static xfs_dir2_db_t
40xfs_dir2_db_to_fdb(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
41{
42 return xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET) +
43 (db / geo->free_max_bests);
44}
45
46/*
47 * Convert data space db to the corresponding index in a free db.
48 */
49static int
50xfs_dir2_db_to_fdindex(struct xfs_da_geometry *geo, xfs_dir2_db_t db)
51{
52 return db % geo->free_max_bests;
53}
54
55/*
Dave Chinner24df33b2013-04-12 07:30:21 +100056 * Check internal consistency of a leafn block.
57 */
58#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080059static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100060xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110061 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100062 struct xfs_buf *bp)
63{
64 struct xfs_dir2_leaf *leaf = bp->b_addr;
65 struct xfs_dir3_icleaf_hdr leafhdr;
66
Christoph Hellwig51842552019-11-08 14:57:49 -080067 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100068
69 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
70 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
71 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080072 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100073 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080074 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100075
Christoph Hellwig5ba30912019-11-08 14:57:52 -080076 return xfs_dir3_leaf_check_int(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100077}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080078
79static inline void
80xfs_dir3_leaf_check(
81 struct xfs_inode *dp,
82 struct xfs_buf *bp)
83{
84 xfs_failaddr_t fa;
85
86 fa = xfs_dir3_leafn_check(dp, bp);
87 if (!fa)
88 return;
89 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070090 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
91 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080092 ASSERT(0);
93}
Dave Chinner24df33b2013-04-12 07:30:21 +100094#else
Dave Chinner41419562013-10-29 22:11:50 +110095#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100096#endif
97
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080098static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110099xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +1100100 struct xfs_buf *bp)
101{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700102 struct xfs_mount *mp = bp->b_mount;
Dave Chinner20252072012-11-12 22:54:13 +1100103 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +1100104
Brian Foster39708c22019-02-07 10:45:48 -0800105 if (!xfs_verify_magic(bp, hdr->magic))
106 return __this_address;
107
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100108 if (xfs_sb_version_hascrc(&mp->m_sb)) {
109 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
110
Eric Sandeence748ea2015-07-29 11:53:31 +1000111 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800112 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100113 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800114 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100115 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800116 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100117 }
118
119 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
120
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800121 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100122}
123
124static void
125xfs_dir3_free_read_verify(
126 struct xfs_buf *bp)
127{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700128 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800129 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130
Eric Sandeence5028c2014-02-27 15:23:10 +1100131 if (xfs_sb_version_hascrc(&mp->m_sb) &&
132 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800133 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
134 else {
135 fa = xfs_dir3_free_verify(bp);
136 if (fa)
137 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
138 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100139}
Dave Chinner20252072012-11-12 22:54:13 +1100140
Dave Chinner612cfbf2012-11-14 17:52:32 +1100141static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100142xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100143 struct xfs_buf *bp)
144{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700145 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800146 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100147 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800148 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100149
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800150 fa = xfs_dir3_free_verify(bp);
151 if (fa) {
152 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100153 return;
154 }
155
156 if (!xfs_sb_version_hascrc(&mp->m_sb))
157 return;
158
159 if (bip)
160 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
161
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100162 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100163}
164
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100165const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100166 .name = "xfs_dir3_free",
Brian Foster39708c22019-02-07 10:45:48 -0800167 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
168 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100169 .verify_read = xfs_dir3_free_read_verify,
170 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800171 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100172};
Dave Chinner20252072012-11-12 22:54:13 +1100173
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800174/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800175static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800176xfs_dir3_free_header_check(
177 struct xfs_inode *dp,
178 xfs_dablk_t fbno,
179 struct xfs_buf *bp)
180{
181 struct xfs_mount *mp = dp->i_mount;
Christoph Hellwig5893e4f2019-11-08 15:01:30 -0800182 int maxbests = mp->m_dir_geo->free_max_bests;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800183 unsigned int firstdb;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800184
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800185 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
186 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
187 maxbests;
188 if (xfs_sb_version_hascrc(&mp->m_sb)) {
189 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
190
191 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800192 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800193 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800194 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800195 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800196 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800197 } else {
198 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
199
200 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800201 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800202 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800203 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800204 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800205 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800206 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800207 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800208}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100209
Dave Chinner20252072012-11-12 22:54:13 +1100210static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100211__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100212 struct xfs_trans *tp,
213 struct xfs_inode *dp,
214 xfs_dablk_t fbno,
215 xfs_daddr_t mappedbno,
216 struct xfs_buf **bpp)
217{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800218 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100219 int err;
220
221 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100222 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800223 if (err || !*bpp)
224 return err;
225
226 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800227 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
228 if (fa) {
229 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800230 xfs_trans_brelse(tp, *bpp);
231 return -EFSCORRUPTED;
232 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100233
234 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800235 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100236 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800237
238 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100239}
240
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800241void
242xfs_dir2_free_hdr_from_disk(
243 struct xfs_mount *mp,
244 struct xfs_dir3_icfree_hdr *to,
245 struct xfs_dir2_free *from)
246{
247 if (xfs_sb_version_hascrc(&mp->m_sb)) {
248 struct xfs_dir3_free *from3 = (struct xfs_dir3_free *)from;
249
250 to->magic = be32_to_cpu(from3->hdr.hdr.magic);
251 to->firstdb = be32_to_cpu(from3->hdr.firstdb);
252 to->nvalid = be32_to_cpu(from3->hdr.nvalid);
253 to->nused = be32_to_cpu(from3->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800254 to->bests = from3->bests;
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800255
256 ASSERT(to->magic == XFS_DIR3_FREE_MAGIC);
257 } else {
258 to->magic = be32_to_cpu(from->hdr.magic);
259 to->firstdb = be32_to_cpu(from->hdr.firstdb);
260 to->nvalid = be32_to_cpu(from->hdr.nvalid);
261 to->nused = be32_to_cpu(from->hdr.nused);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800262 to->bests = from->bests;
263
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800264 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC);
265 }
266}
267
Christoph Hellwig200dada2019-11-08 14:57:52 -0800268static void
269xfs_dir2_free_hdr_to_disk(
270 struct xfs_mount *mp,
271 struct xfs_dir2_free *to,
272 struct xfs_dir3_icfree_hdr *from)
273{
274 if (xfs_sb_version_hascrc(&mp->m_sb)) {
275 struct xfs_dir3_free *to3 = (struct xfs_dir3_free *)to;
276
277 ASSERT(from->magic == XFS_DIR3_FREE_MAGIC);
278
279 to3->hdr.hdr.magic = cpu_to_be32(from->magic);
280 to3->hdr.firstdb = cpu_to_be32(from->firstdb);
281 to3->hdr.nvalid = cpu_to_be32(from->nvalid);
282 to3->hdr.nused = cpu_to_be32(from->nused);
283 } else {
284 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC);
285
286 to->hdr.magic = cpu_to_be32(from->magic);
287 to->hdr.firstdb = cpu_to_be32(from->firstdb);
288 to->hdr.nvalid = cpu_to_be32(from->nvalid);
289 to->hdr.nused = cpu_to_be32(from->nused);
290 }
291}
292
Dave Chinner20252072012-11-12 22:54:13 +1100293int
294xfs_dir2_free_read(
295 struct xfs_trans *tp,
296 struct xfs_inode *dp,
297 xfs_dablk_t fbno,
298 struct xfs_buf **bpp)
299{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100300 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100301}
302
303static int
304xfs_dir2_free_try_read(
305 struct xfs_trans *tp,
306 struct xfs_inode *dp,
307 xfs_dablk_t fbno,
308 struct xfs_buf **bpp)
309{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100310 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
311}
312
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100313static int
314xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000315 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100316 xfs_dir2_db_t fbno,
317 struct xfs_buf **bpp)
318{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000319 struct xfs_trans *tp = args->trans;
320 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100321 struct xfs_mount *mp = dp->i_mount;
322 struct xfs_buf *bp;
323 int error;
324 struct xfs_dir3_icfree_hdr hdr;
325
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000326 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100327 -1, &bp, XFS_DATA_FORK);
328 if (error)
329 return error;
330
Dave Chinner61fe1352013-04-03 16:11:30 +1100331 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100332 bp->b_ops = &xfs_dir3_free_buf_ops;
333
334 /*
335 * Initialize the new block to be empty, and remember
336 * its first slot as our empty slot.
337 */
Dave Chinnere400d272013-05-28 18:37:17 +1000338 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
339 memset(&hdr, 0, sizeof(hdr));
340
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100341 if (xfs_sb_version_hascrc(&mp->m_sb)) {
342 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
343
344 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000345
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100346 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
347 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000348 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000349 } else
350 hdr.magic = XFS_DIR2_FREE_MAGIC;
Christoph Hellwig200dada2019-11-08 14:57:52 -0800351 xfs_dir2_free_hdr_to_disk(mp, bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100352 *bpp = bp;
353 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
357 * Log entries from a freespace block.
358 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000359STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000361 struct xfs_da_args *args,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800362 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000363 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int first, /* first entry to log */
365 int last) /* last entry to log */
366{
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800367 struct xfs_dir2_free *free = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100369 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
370 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000371 xfs_trans_log_buf(args->trans, bp,
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800372 (char *)&hdr->bests[first] - (char *)free,
373 (char *)&hdr->bests[last] - (char *)free +
374 sizeof(hdr->bests[0]) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/*
378 * Log header from a freespace block.
379 */
380static void
381xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000382 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000383 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Dave Chinner836a94a2013-08-12 20:49:44 +1000385#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 xfs_dir2_free_t *free; /* freespace structure */
387
Dave Chinner1d9025e2012-06-22 18:50:14 +1000388 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100389 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
390 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000391#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000392 xfs_trans_log_buf(args->trans, bp, 0,
Christoph Hellwiged1d6122019-11-08 15:01:29 -0800393 args->geo->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * Convert a leaf-format directory to a node-format directory.
398 * We need to change the magic number of the leaf block, and copy
399 * the freespace table out of the leaf block into its own block.
400 */
401int /* error */
402xfs_dir2_leaf_to_node(
403 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000404 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 xfs_inode_t *dp; /* incore directory inode */
407 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000408 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 xfs_dir2_db_t fdb; /* freespace block number */
Nathan Scott68b3a102006-03-17 17:27:19 +1100410 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int i; /* leaf freespace index */
412 xfs_dir2_leaf_t *leaf; /* leaf structure */
413 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int n; /* count of live freespc ents */
415 xfs_dir2_data_off_t off; /* freespace entry value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100417 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000419 trace_xfs_dir2_leaf_to_node(args);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 tp = args->trans;
423 /*
424 * Add a freespace block to the directory.
425 */
426 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
427 return error;
428 }
Dave Chinner30028032014-06-06 15:08:18 +1000429 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
431 * Get the buffer for the new freespace block.
432 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000433 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100434 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100436
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800437 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, fbp->b_addr);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000438 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000439 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800440 if (be32_to_cpu(ltp->bestcount) >
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700441 (uint)dp->i_d.di_size / args->geo->blksize) {
442 xfs_buf_corruption_error(lbp);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800443 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700444 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Copy freespace entries from the leaf block to the new block.
448 * Count active entries.
449 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100450 from = xfs_dir2_leaf_bests_p(ltp);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800451 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++) {
452 off = be16_to_cpu(*from);
453 if (off != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 n++;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800455 freehdr.bests[i] = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100459 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100461 freehdr.nused = n;
462 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
463
Christoph Hellwig200dada2019-11-08 14:57:52 -0800464 xfs_dir2_free_hdr_to_disk(dp->i_mount, fbp->b_addr, &freehdr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800465 xfs_dir2_free_log_bests(args, &freehdr, fbp, 0, freehdr.nvalid - 1);
Dave Chinnerbc851782014-06-06 15:20:54 +1000466 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100467
Dave Chinner24df33b2013-04-12 07:30:21 +1000468 /*
469 * Converting the leaf to a leafnode is just a matter of changing the
470 * magic number and the ops. Do the change directly to the buffer as
471 * it's less work (and less code) than decoding the header to host
472 * format and back again.
473 */
474 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
475 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
476 else
477 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
478 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100479 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000480 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100481 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
485/*
486 * Add a leaf entry to a leaf block in a node-form directory.
487 * The other work necessary is done from the caller.
488 */
489static int /* error */
490xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000491 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800492 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int index) /* insertion pt for new entry */
494{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800495 struct xfs_dir3_icleaf_hdr leafhdr;
496 struct xfs_inode *dp = args->dp;
497 struct xfs_dir2_leaf *leaf = bp->b_addr;
498 struct xfs_dir2_leaf_entry *lep;
499 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800501 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int lfloghigh; /* high leaf entry logging */
503 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800504 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000506 trace_xfs_dir2_leafn_add(args, index);
507
Christoph Hellwig51842552019-11-08 14:57:49 -0800508 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800509 ents = leafhdr.ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /*
512 * Quick check just to make sure we are not going to index
513 * into other peoples memory
514 */
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700515 if (index < 0) {
516 xfs_buf_corruption_error(bp);
Dave Chinner24513372014-06-25 14:58:08 +1000517 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /*
521 * If there are already the maximum number of leaf entries in
522 * the block, if there are no stale entries it won't fit.
523 * Caller will do a split. If there are stale entries we'll do
524 * a compact.
525 */
526
Christoph Hellwig478c7832019-11-08 14:57:51 -0800527 if (leafhdr.count == args->geo->leaf_max_ents) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000528 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000529 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000530 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 } else
532 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000533 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
534 ASSERT(index == leafhdr.count ||
535 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Barry Naujok6a178102008-05-21 16:42:05 +1000537 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
540 /*
541 * Compact out all but one stale leaf entry. Leaves behind
542 * the entry closest to index.
543 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000544 if (compact)
545 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
546 &highstale, &lfloglow, &lfloghigh);
547 else if (leafhdr.stale) {
548 /*
549 * Set impossible logging indices for this case.
550 */
551 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 lfloghigh = -1;
553 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * Insert the new entry, log everything.
557 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000558 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200559 highstale, &lfloglow, &lfloghigh);
560
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100561 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000562 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100563 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000564
Christoph Hellwig163fbbb2019-11-08 14:57:50 -0800565 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000566 xfs_dir3_leaf_log_header(args, bp);
Christoph Hellwig787b0892019-11-08 14:57:50 -0800567 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100568 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 0;
570}
571
572#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100573static void
574xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100575 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100576 struct xfs_buf *bp,
577 xfs_dir2_db_t db)
578{
579 struct xfs_dir3_icfree_hdr hdr;
580
Christoph Hellwig5ba30912019-11-08 14:57:52 -0800581 xfs_dir2_free_hdr_from_disk(dp->i_mount, &hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100582
Christoph Hellwig5893e4f2019-11-08 15:01:30 -0800583 ASSERT((hdr.firstdb % dp->i_mount->m_dir_geo->free_max_bests) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100584 ASSERT(hdr.firstdb <= db);
585 ASSERT(db < hdr.firstdb + hdr.nvalid);
586}
587#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100588#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#endif /* DEBUG */
590
591/*
592 * Return the last hash value in the leaf.
593 * Stale entries are ok.
594 */
595xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700596xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100597 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000598 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 int *count) /* count of entries in leaf */
600{
Dave Chinner24df33b2013-04-12 07:30:21 +1000601 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Christoph Hellwig787b0892019-11-08 14:57:50 -0800603 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, bp->b_addr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000604
605 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700606 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
607 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
608 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000611 *count = leafhdr.count;
612 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return 0;
Christoph Hellwig787b0892019-11-08 14:57:50 -0800614 return be32_to_cpu(leafhdr.ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000618 * Look up a leaf entry for space to add a name in a node-format leaf block.
619 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000621STATIC int
622xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000623 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 xfs_da_args_t *args, /* operation arguments */
625 int *indexp, /* out: leaf entry index */
626 xfs_da_state_t *state) /* state to fill in */
627{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000628 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000629 xfs_dir2_db_t curdb = -1; /* current data block number */
630 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 xfs_inode_t *dp; /* incore directory inode */
632 int error; /* error return value */
633 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000634 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 int index; /* leaf entry index */
636 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000637 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
639 xfs_mount_t *mp; /* filesystem mount point */
640 xfs_dir2_db_t newdb; /* new data block number */
641 xfs_dir2_db_t newfdb; /* new free block number */
642 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000643 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 dp = args->dp;
646 tp = args->trans;
647 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000648 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800649 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000650
Dave Chinner41419562013-10-29 22:11:50 +1100651 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000652 ASSERT(leafhdr.count > 0);
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
655 * Look up the hash value in the leaf entries.
656 */
657 index = xfs_dir2_leaf_search_hash(args, bp);
658 /*
659 * Do we have a buffer coming in?
660 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000661 if (state->extravalid) {
662 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000665 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100666 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
667 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -0800669 length = xfs_dir2_data_entsize(mp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * Loop over leaf entries with the right hash value.
672 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800673 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000674 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
675 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * Skip stale leaf entries.
678 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100679 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 continue;
681 /*
682 * Pull the data block number from the entry.
683 */
Dave Chinner30028032014-06-06 15:08:18 +1000684 newdb = xfs_dir2_dataptr_to_db(args->geo,
685 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /*
687 * For addname, we're looking for a place to put the new entry.
688 * We want to use a data block with an entry of equal
689 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000690 *
691 * If this block isn't the data block we already have
692 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000694 if (newdb != curdb) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800695 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100696
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000697 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000699 * Convert the data block to the free block
700 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -0800702 newfdb = xfs_dir2_db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000704 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000706 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000708 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000711 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100712
713 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000714 xfs_dir2_db_to_da(args->geo,
715 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100716 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000719 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100720
Dave Chinner01ba43b2013-10-29 22:11:52 +1100721 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -0800726 fi = xfs_dir2_db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000728 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800730 xfs_dir2_free_hdr_from_disk(mp, &freehdr, free);
731 if (unlikely(
732 freehdr.bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000733 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
734 XFS_ERRLEVEL_LOW, mp);
735 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000736 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000737 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000739 curfdb = newfdb;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -0800740 if (be16_to_cpu(freehdr.bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000741 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000744 /* Didn't find any space */
745 fi = -1;
746out:
Barry Naujok6a178102008-05-21 16:42:05 +1000747 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000748 if (curbp) {
749 /* Giving back a free block. */
750 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000752 state->extrablk.index = fi;
753 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100754
755 /*
756 * Important: this magic number is not in the buffer - it's for
757 * buffer type information and therefore only the free/data type
758 * matters here, not whether CRCs are enabled or not.
759 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000760 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
761 } else {
762 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000765 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000768 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000772 * Look up a leaf entry in a node-format leaf block.
773 * The extrablk in state a data block.
774 */
775STATIC int
776xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000777 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000778 xfs_da_args_t *args, /* operation arguments */
779 int *indexp, /* out: leaf entry index */
780 xfs_da_state_t *state) /* state to fill in */
781{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000782 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000783 xfs_dir2_db_t curdb = -1; /* current data block number */
784 xfs_dir2_data_entry_t *dep; /* data block entry */
785 xfs_inode_t *dp; /* incore directory inode */
786 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000787 int index; /* leaf entry index */
788 xfs_dir2_leaf_t *leaf; /* leaf structure */
789 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
790 xfs_mount_t *mp; /* filesystem mount point */
791 xfs_dir2_db_t newdb; /* new data block number */
792 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000793 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000794 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000795
796 dp = args->dp;
797 tp = args->trans;
798 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000799 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -0800800 xfs_dir2_leaf_hdr_from_disk(mp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000801
Dave Chinner41419562013-10-29 22:11:50 +1100802 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700803 if (leafhdr.count <= 0) {
804 xfs_buf_corruption_error(bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700805 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700806 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000807
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000808 /*
809 * Look up the hash value in the leaf entries.
810 */
811 index = xfs_dir2_leaf_search_hash(args, bp);
812 /*
813 * Do we have a buffer coming in?
814 */
815 if (state->extravalid) {
816 curbp = state->extrablk.bp;
817 curdb = state->extrablk.blkno;
818 }
819 /*
820 * Loop over leaf entries with the right hash value.
821 */
Christoph Hellwig787b0892019-11-08 14:57:50 -0800822 for (lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +1000823 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
824 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000825 /*
826 * Skip stale leaf entries.
827 */
828 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
829 continue;
830 /*
831 * Pull the data block number from the entry.
832 */
Dave Chinner30028032014-06-06 15:08:18 +1000833 newdb = xfs_dir2_dataptr_to_db(args->geo,
834 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000835 /*
836 * Not adding a new entry, so we really want to find
837 * the name given to us.
838 *
839 * If it's a different data block, go get it.
840 */
841 if (newdb != curdb) {
842 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000843 * If we had a block before that we aren't saving
844 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000845 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000846 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
847 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000848 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000849 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000850 * If needing the block that is saved with a CI match,
851 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000852 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000853 if (args->cmpresult != XFS_CMP_DIFFERENT &&
854 newdb == state->extrablk.blkno) {
855 ASSERT(state->extravalid);
856 curbp = state->extrablk.bp;
857 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100858 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000859 xfs_dir2_db_to_da(args->geo,
860 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100861 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000862 if (error)
863 return error;
864 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100865 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000866 curdb = newdb;
867 }
868 /*
869 * Point to the data entry.
870 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000871 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000872 xfs_dir2_dataptr_to_off(args->geo,
873 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000874 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000875 * Compare the entry and if it's an exact match, return
876 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000877 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000878 */
Barry Naujok5163f952008-05-21 16:41:01 +1000879 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
880 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000881 /* If there is a CI match block, drop it */
882 if (args->cmpresult != XFS_CMP_DIFFERENT &&
883 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000884 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000885 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000886 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100887 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000888 *indexp = index;
889 state->extravalid = 1;
890 state->extrablk.bp = curbp;
891 state->extrablk.blkno = curdb;
892 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000893 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000894 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100895 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100896 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000897 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000898 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000899 }
900 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000901 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000902 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000903 if (args->cmpresult == XFS_CMP_DIFFERENT) {
904 /* Giving back last used data block. */
905 state->extravalid = 1;
906 state->extrablk.bp = curbp;
907 state->extrablk.index = -1;
908 state->extrablk.blkno = curdb;
909 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100910 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100911 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000912 } else {
913 /* If the curbp is not the CI match block, drop it */
914 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000915 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000916 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000917 } else {
918 state->extravalid = 0;
919 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000920 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000921 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000922}
923
924/*
925 * Look up a leaf entry in a node-format leaf block.
926 * If this is an addname then the extrablk in state is a freespace block,
927 * otherwise it's a data block.
928 */
929int
930xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000931 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000932 xfs_da_args_t *args, /* operation arguments */
933 int *indexp, /* out: leaf entry index */
934 xfs_da_state_t *state) /* state to fill in */
935{
Barry Naujok6a178102008-05-21 16:42:05 +1000936 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000937 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
938 state);
939 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
940}
941
942/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * Move count leaf entries from source to destination leaf.
944 * Log entries and headers. Stale entries are preserved.
945 */
946static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000947xfs_dir3_leafn_moveents(
948 xfs_da_args_t *args, /* operation arguments */
949 struct xfs_buf *bp_s, /* source */
950 struct xfs_dir3_icleaf_hdr *shdr,
951 struct xfs_dir2_leaf_entry *sents,
952 int start_s,/* source leaf index */
953 struct xfs_buf *bp_d, /* destination */
954 struct xfs_dir3_icleaf_hdr *dhdr,
955 struct xfs_dir2_leaf_entry *dents,
956 int start_d,/* destination leaf index */
957 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Dave Chinner24df33b2013-04-12 07:30:21 +1000959 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000961 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /*
964 * Silently return if nothing to do.
965 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000966 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /*
970 * If the destination index is not the end of the current
971 * destination leaf entries, open up a hole in the destination
972 * to hold the new entries.
973 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000974 if (start_d < dhdr->count) {
975 memmove(&dents[start_d + count], &dents[start_d],
976 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800977 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000978 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980 /*
981 * If the source has stale leaves, count the ones in the copy range
982 * so we can update the header correctly.
983 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000984 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int i; /* temp leaf index */
986
987 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000988 if (sents[i].address ==
989 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 stale++;
991 }
992 } else
993 stale = 0;
994 /*
995 * Copy the leaf entries from source to destination.
996 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000997 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -0800999 xfs_dir3_leaf_log_ents(args, dhdr, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +10001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 /*
1002 * If there are source entries after the ones we copied,
1003 * delete the ones we copied by sliding the next ones down.
1004 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001005 if (start_s + count < shdr->count) {
1006 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 count * sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig787b0892019-11-08 14:57:50 -08001008 xfs_dir3_leaf_log_ents(args, shdr, bp_s, start_s,
1009 start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
Dave Chinner24df33b2013-04-12 07:30:21 +10001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Update the headers and log them.
1014 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001015 shdr->count -= count;
1016 shdr->stale -= stale;
1017 dhdr->count += count;
1018 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
1021/*
1022 * Determine the sort order of two leaf blocks.
1023 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
1024 */
1025int /* sort order */
1026xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +11001027 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
1029 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Dave Chinner24df33b2013-04-12 07:30:21 +10001031 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
1032 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
1033 struct xfs_dir2_leaf_entry *ents1;
1034 struct xfs_dir2_leaf_entry *ents2;
1035 struct xfs_dir3_icleaf_hdr hdr1;
1036 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig51842552019-11-08 14:57:49 -08001038 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1039 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001040 ents1 = hdr1.ents;
1041 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001042
1043 if (hdr1.count > 0 && hdr2.count > 0 &&
1044 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
1045 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
1046 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 1;
1048 return 0;
1049}
1050
1051/*
1052 * Rebalance leaf entries between two leaf blocks.
1053 * This is actually only called when the second block is new,
1054 * though the code deals with the general case.
1055 * A new entry will be inserted in one of the blocks, and that
1056 * entry is taken into account when balancing.
1057 */
1058static void
1059xfs_dir2_leafn_rebalance(
1060 xfs_da_state_t *state, /* btree cursor */
1061 xfs_da_state_blk_t *blk1, /* first btree block */
1062 xfs_da_state_blk_t *blk2) /* second btree block */
1063{
1064 xfs_da_args_t *args; /* operation arguments */
1065 int count; /* count (& direction) leaves */
1066 int isleft; /* new goes in left leaf */
1067 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1068 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1069 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001070#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 int oldstale; /* old count of stale leaves */
1072#endif
1073 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001074 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001075 struct xfs_dir2_leaf_entry *ents1;
1076 struct xfs_dir2_leaf_entry *ents2;
1077 struct xfs_dir3_icleaf_hdr hdr1;
1078 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001079 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 args = state->args;
1082 /*
1083 * If the block order is wrong, swap the arguments.
1084 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001085 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1086 if (swap_blocks)
1087 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dave Chinner1d9025e2012-06-22 18:50:14 +10001089 leaf1 = blk1->bp->b_addr;
1090 leaf2 = blk2->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001091 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr1, leaf1);
1092 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf2);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001093 ents1 = hdr1.ents;
1094 ents2 = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001095
1096 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001097#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001098 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#endif
1100 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * If the old leaf count was odd then the new one will be even,
1104 * so we need to divide the new count evenly.
1105 */
1106 if (oldsum & 1) {
1107 xfs_dahash_t midhash; /* middle entry hash value */
1108
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 if (mid >= hdr1.count)
1110 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001112 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 isleft = args->hashval <= midhash;
1114 }
1115 /*
1116 * If the old count is even then the new count is odd, so there's
1117 * no preferred side for the new entry.
1118 * Pick the left one.
1119 */
1120 else
1121 isleft = 1;
1122 /*
1123 * Calculate moved entry count. Positive means left-to-right,
1124 * negative means right-to-left. Then move the entries.
1125 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001126 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001128 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1129 hdr1.count - count, blk2->bp,
1130 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001132 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1133 blk1->bp, &hdr1, ents1,
1134 hdr1.count, count);
1135
1136 ASSERT(hdr1.count + hdr2.count == oldsum);
1137 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1138
1139 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001140 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf1, &hdr1);
1141 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001142 xfs_dir3_leaf_log_header(args, blk1->bp);
1143 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001144
Dave Chinner41419562013-10-29 22:11:50 +11001145 xfs_dir3_leaf_check(dp, blk1->bp);
1146 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /*
1149 * Mark whether we're inserting into the old or new leaf.
1150 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001151 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001152 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001153 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001154 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001156 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
1158 * Adjust the expected index for insertion.
1159 */
1160 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001161 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001162
1163 /*
1164 * Finally sanity check just to make sure we are not returning a
1165 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Dave Chinner41419562013-10-29 22:11:50 +11001167 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 state->inleaf = 1;
1169 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001170 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001171 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001172 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
1174}
1175
Dave Chinner20252072012-11-12 22:54:13 +11001176static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001177xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001178 xfs_da_args_t *args,
1179 struct xfs_dir2_data_hdr *hdr,
1180 struct xfs_dir2_free *free,
1181 xfs_dir2_db_t fdb,
1182 int findex,
1183 struct xfs_buf *fbp,
1184 int longest)
1185{
Dave Chinner20252072012-11-12 22:54:13 +11001186 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001187 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001188 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001189
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001190 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001191 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001192 /*
1193 * Data block is not empty, just set the free entry to the new
1194 * value.
1195 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001196 freehdr.bests[findex] = cpu_to_be16(longest);
1197 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001198 return 0;
1199 }
1200
1201 /* One less used entry in the free table. */
1202 freehdr.nused--;
1203
1204 /*
1205 * If this was the last entry in the table, we can trim the table size
1206 * back. There might be other entries at the end referring to
1207 * non-existent data blocks, get those too.
1208 */
1209 if (findex == freehdr.nvalid - 1) {
1210 int i; /* free entry index */
1211
1212 for (i = findex - 1; i >= 0; i--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001213 if (freehdr.bests[i] != cpu_to_be16(NULLDATAOFF))
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001214 break;
1215 }
1216 freehdr.nvalid = i + 1;
1217 logfree = 0;
1218 } else {
1219 /* Not the last entry, just punch it out. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001220 freehdr.bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001221 logfree = 1;
1222 }
1223
Christoph Hellwig200dada2019-11-08 14:57:52 -08001224 xfs_dir2_free_hdr_to_disk(dp->i_mount, free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001225 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001226
1227 /*
1228 * If there are no useful entries left in the block, get rid of the
1229 * block if we can.
1230 */
1231 if (!freehdr.nused) {
1232 int error;
1233
1234 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1235 if (error == 0) {
1236 fbp = NULL;
1237 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001238 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001239 return error;
1240 /*
1241 * It's possible to get ENOSPC if there is no
1242 * space reservation. In this case some one
1243 * else will eventually get rid of this block.
1244 */
1245 }
1246
Dave Chinner20252072012-11-12 22:54:13 +11001247 /* Log the free entry that changed, unless we got rid of it. */
1248 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001249 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001250 return 0;
1251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/*
1254 * Remove an entry from a node directory.
1255 * This removes the leaf entry and the data entry,
1256 * and updates the free block if necessary.
1257 */
1258static int /* error */
1259xfs_dir2_leafn_remove(
1260 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001261 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int index, /* leaf entry index */
1263 xfs_da_state_blk_t *dblk, /* data block */
1264 int *rval) /* resulting block needs join */
1265{
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001266 struct xfs_da_geometry *geo = args->geo;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001267 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001269 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 xfs_dir2_data_entry_t *dep; /* data block entry */
1271 xfs_inode_t *dp; /* incore directory inode */
1272 xfs_dir2_leaf_t *leaf; /* leaf structure */
1273 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1274 int longest; /* longest data free entry */
1275 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int needlog; /* need to log data header */
1277 int needscan; /* need to rescan data frees */
1278 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001279 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001280 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001282 trace_xfs_dir2_leafn_remove(args, index);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 dp = args->dp;
1285 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001286 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001287 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /*
1290 * Point to the entry we're removing.
1291 */
Christoph Hellwig787b0892019-11-08 14:57:50 -08001292 lep = &leafhdr.ents[index];
Dave Chinner24df33b2013-04-12 07:30:21 +10001293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /*
1295 * Extract the data block and offset from the entry.
1296 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001297 db = xfs_dir2_dataptr_to_db(geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ASSERT(dblk->blkno == db);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001299 off = xfs_dir2_dataptr_to_off(geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /*
1303 * Kill the leaf entry by marking it stale.
1304 * Log the leaf block changes.
1305 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001306 leafhdr.stale++;
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001307 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001308 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001309
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001310 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001311 xfs_dir3_leaf_log_ents(args, &leafhdr, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
1314 * Make the data entry free. Keep track of the longest freespace
1315 * in the data block in case it changes.
1316 */
1317 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001318 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001319 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Christoph Hellwig1848b602019-11-08 15:05:39 -08001320 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001321 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001323 xfs_dir2_data_make_free(args, dbp, off,
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -08001324 xfs_dir2_data_entsize(dp->i_mount, dep->namelen), &needlog,
1325 &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /*
1327 * Rescan the data block freespaces for bestfree.
1328 * Log the data block header if needed.
1329 */
1330 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001331 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001333 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001334 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * If the longest data block freespace changes, need to update
1337 * the corresponding freeblock entry.
1338 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001339 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001341 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 xfs_dir2_db_t fdb; /* freeblock block number */
1343 int findex; /* index in freeblock entries */
1344 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /*
1347 * Convert the data block number to a free block,
1348 * read in the free block.
1349 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001350 fdb = xfs_dir2_db_to_fdb(geo, db);
1351 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001352 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001353 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001355 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001356#ifdef DEBUG
1357 {
1358 struct xfs_dir3_icfree_hdr freehdr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08001359
1360 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001361 ASSERT(freehdr.firstdb == geo->free_max_bests *
1362 (fdb - xfs_dir2_byte_to_db(geo, XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001363 }
1364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
1366 * Calculate which entry we need to fix.
1367 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001368 findex = xfs_dir2_db_to_fdindex(geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001369 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * If the data block is now empty we can get rid of it
1372 * (usually).
1373 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001374 if (longest == geo->blksize - geo->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 /*
1376 * Try to punch out the data block.
1377 */
1378 error = xfs_dir2_shrink_inode(args, db, dbp);
1379 if (error == 0) {
1380 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001381 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 /*
1384 * We can get ENOSPC if there's no space reservation.
1385 * In this case just drop the buffer and some one else
1386 * will eventually get rid of the empty block.
1387 */
Dave Chinner24513372014-06-25 14:58:08 +10001388 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return error;
1390 }
1391 /*
1392 * If we got rid of the data block, we can eliminate that entry
1393 * in the free block.
1394 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001395 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001396 fdb, findex, fbp, longest);
1397 if (error)
1398 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
Dave Chinner20252072012-11-12 22:54:13 +11001400
Dave Chinner41419562013-10-29 22:11:50 +11001401 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001403 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * to justify trying to join it with a neighbor.
1405 */
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001406 *rval = (geo->leaf_hdr_size +
Christoph Hellwig787b0892019-11-08 14:57:50 -08001407 (uint)sizeof(leafhdr.ents) * (leafhdr.count - leafhdr.stale)) <
Christoph Hellwigd73e1ce2019-11-08 15:05:38 -08001408 geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
1410}
1411
1412/*
1413 * Split the leaf entries in the old block into old and new blocks.
1414 */
1415int /* error */
1416xfs_dir2_leafn_split(
1417 xfs_da_state_t *state, /* btree cursor */
1418 xfs_da_state_blk_t *oldblk, /* original block */
1419 xfs_da_state_blk_t *newblk) /* newly created block */
1420{
1421 xfs_da_args_t *args; /* operation arguments */
1422 xfs_dablk_t blkno; /* new leaf block number */
1423 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001424 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /*
1427 * Allocate space for a new leaf node.
1428 */
1429 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001430 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1432 error = xfs_da_grow_inode(args, &blkno);
1433 if (error) {
1434 return error;
1435 }
1436 /*
1437 * Initialize the new leaf block.
1438 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001439 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001440 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1441 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 newblk->blkno = blkno;
1445 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1446 /*
1447 * Rebalance the entries across the two leaves, link the new
1448 * block into the leaves.
1449 */
1450 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001451 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (error) {
1453 return error;
1454 }
1455 /*
1456 * Insert the new entry in the correct block.
1457 */
1458 if (state->inleaf)
1459 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1460 else
1461 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1462 /*
1463 * Update last hashval in each block since we added the name.
1464 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001465 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1466 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001467 xfs_dir3_leaf_check(dp, oldblk->bp);
1468 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return error;
1470}
1471
1472/*
1473 * Check a leaf block and its neighbors to see if the block should be
1474 * collapsed into one or the other neighbor. Always keep the block
1475 * with the smaller block number.
1476 * If the current block is over 50% full, don't try to join it, return 0.
1477 * If the block is empty, fill in the state structure and return 2.
1478 * If it can be collapsed, fill in the state structure and return 1.
1479 * If nothing can be done, return 0.
1480 */
1481int /* error */
1482xfs_dir2_leafn_toosmall(
1483 xfs_da_state_t *state, /* btree cursor */
1484 int *action) /* resulting action to take */
1485{
1486 xfs_da_state_blk_t *blk; /* leaf block */
1487 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001488 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int bytes; /* bytes in use */
1490 int count; /* leaf live entry count */
1491 int error; /* error return value */
1492 int forward; /* sibling block direction */
1493 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 xfs_dir2_leaf_t *leaf; /* leaf structure */
1495 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001496 struct xfs_dir3_icleaf_hdr leafhdr;
1497 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001498 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /*
1501 * Check for the degenerate case of the block being over 50% full.
1502 * If so, it's not worth even looking to see if we might be able
1503 * to coalesce with a sibling.
1504 */
1505 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001506 leaf = blk->bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001507 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &leafhdr, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001508 ents = leafhdr.ents;
Dave Chinner41419562013-10-29 22:11:50 +11001509 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001510
1511 count = leafhdr.count - leafhdr.stale;
Christoph Hellwig545910b2019-11-08 14:57:51 -08001512 bytes = state->args->geo->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001513 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /*
1515 * Blk over 50%, don't try to join.
1516 */
1517 *action = 0;
1518 return 0;
1519 }
1520 /*
1521 * Check for the degenerate case of the block being empty.
1522 * If the block is empty, we'll simply delete it, no need to
1523 * coalesce it with a sibling block. We choose (arbitrarily)
1524 * to merge with the forward block unless it is NULL.
1525 */
1526 if (count == 0) {
1527 /*
1528 * Make altpath point to the block we want to keep and
1529 * path point to the block we want to drop (this one).
1530 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001531 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001533 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 &rval);
1535 if (error)
1536 return error;
1537 *action = rval ? 2 : 0;
1538 return 0;
1539 }
1540 /*
1541 * Examine each sibling block to see if we can coalesce with
1542 * at least 25% free space to spare. We need to figure out
1543 * whether to merge with the forward or the backward block.
1544 * We prefer coalescing with the lower numbered sibling so as
1545 * to shrink a directory over time.
1546 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001547 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001549 struct xfs_dir3_icleaf_hdr hdr2;
1550
1551 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (blkno == 0)
1553 continue;
1554 /*
1555 * Read the sibling leaf block.
1556 */
Dave Chinner41419562013-10-29 22:11:50 +11001557 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001558 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001559 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Count bytes in the two blocks combined.
1564 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001565 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001566 bytes = state->args->geo->blksize -
1567 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001568
Dave Chinner1d9025e2012-06-22 18:50:14 +10001569 leaf = bp->b_addr;
Christoph Hellwig51842552019-11-08 14:57:49 -08001570 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &hdr2, leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001571 ents = hdr2.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001572 count += hdr2.count - hdr2.stale;
1573 bytes -= count * sizeof(ents[0]);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
1576 * Fits with at least 25% to spare.
1577 */
1578 if (bytes >= 0)
1579 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001580 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582 /*
1583 * Didn't like either block, give up.
1584 */
1585 if (i >= 2) {
1586 *action = 0;
1587 return 0;
1588 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Make altpath point to the block we want to keep (the lower
1592 * numbered block) and path point to the block we want to drop.
1593 */
1594 memcpy(&state->altpath, &state->path, sizeof(state->path));
1595 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001596 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 &rval);
1598 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001599 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 &rval);
1601 if (error) {
1602 return error;
1603 }
1604 *action = rval ? 0 : 1;
1605 return 0;
1606}
1607
1608/*
1609 * Move all the leaf entries from drop_blk to save_blk.
1610 * This is done as part of a join operation.
1611 */
1612void
1613xfs_dir2_leafn_unbalance(
1614 xfs_da_state_t *state, /* cursor */
1615 xfs_da_state_blk_t *drop_blk, /* dead block */
1616 xfs_da_state_blk_t *save_blk) /* surviving block */
1617{
1618 xfs_da_args_t *args; /* operation arguments */
1619 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1620 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001621 struct xfs_dir3_icleaf_hdr savehdr;
1622 struct xfs_dir3_icleaf_hdr drophdr;
1623 struct xfs_dir2_leaf_entry *sents;
1624 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001625 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 args = state->args;
1628 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1629 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001630 drop_leaf = drop_blk->bp->b_addr;
1631 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001632
Christoph Hellwig51842552019-11-08 14:57:49 -08001633 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &savehdr, save_leaf);
1634 xfs_dir2_leaf_hdr_from_disk(dp->i_mount, &drophdr, drop_leaf);
Christoph Hellwig787b0892019-11-08 14:57:50 -08001635 sents = savehdr.ents;
1636 dents = drophdr.ents;
Dave Chinner24df33b2013-04-12 07:30:21 +10001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 /*
1639 * If there are any stale leaf entries, take this opportunity
1640 * to purge them.
1641 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001642 if (drophdr.stale)
1643 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1644 if (savehdr.stale)
1645 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Move the entries from drop to the appropriate end of save.
1649 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001650 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001651 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001652 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1653 save_blk->bp, &savehdr, sents, 0,
1654 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001656 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1657 save_blk->bp, &savehdr, sents,
1658 savehdr.count, drophdr.count);
1659 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1660
1661 /* log the changes made when moving the entries */
Christoph Hellwig163fbbb2019-11-08 14:57:50 -08001662 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, save_leaf, &savehdr);
1663 xfs_dir2_leaf_hdr_to_disk(dp->i_mount, drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001664 xfs_dir3_leaf_log_header(args, save_blk->bp);
1665 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001666
Dave Chinner41419562013-10-29 22:11:50 +11001667 xfs_dir3_leaf_check(dp, save_blk->bp);
1668 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671/*
Dave Chinnera07258a2019-08-29 09:04:06 -07001672 * Add a new data block to the directory at the free space index that the caller
1673 * has specified.
1674 */
1675static int
1676xfs_dir2_node_add_datablk(
1677 struct xfs_da_args *args,
1678 struct xfs_da_state_blk *fblk,
1679 xfs_dir2_db_t *dbno,
1680 struct xfs_buf **dbpp,
1681 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001682 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinnera07258a2019-08-29 09:04:06 -07001683 int *findex)
1684{
1685 struct xfs_inode *dp = args->dp;
1686 struct xfs_trans *tp = args->trans;
1687 struct xfs_mount *mp = dp->i_mount;
Dave Chinnera07258a2019-08-29 09:04:06 -07001688 struct xfs_dir2_data_free *bf;
Dave Chinnera07258a2019-08-29 09:04:06 -07001689 xfs_dir2_db_t fbno;
1690 struct xfs_buf *fbp;
1691 struct xfs_buf *dbp;
Dave Chinnera07258a2019-08-29 09:04:06 -07001692 int error;
1693
1694 /* Not allowed to allocate, return failure. */
Dave Chinner0e822252019-08-29 09:04:07 -07001695 if (args->total == 0)
Dave Chinnera07258a2019-08-29 09:04:06 -07001696 return -ENOSPC;
1697
1698 /* Allocate and initialize the new data block. */
1699 error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, dbno);
1700 if (error)
1701 return error;
1702 error = xfs_dir3_data_init(args, *dbno, &dbp);
1703 if (error)
1704 return error;
1705
1706 /*
1707 * Get the freespace block corresponding to the data block
1708 * that was just allocated.
1709 */
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001710 fbno = xfs_dir2_db_to_fdb(args->geo, *dbno);
Dave Chinnera07258a2019-08-29 09:04:06 -07001711 error = xfs_dir2_free_try_read(tp, dp,
1712 xfs_dir2_db_to_da(args->geo, fbno), &fbp);
1713 if (error)
1714 return error;
1715
1716 /*
1717 * If there wasn't a freespace block, the read will
1718 * return a NULL fbp. Allocate and initialize a new one.
1719 */
1720 if (!fbp) {
1721 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fbno);
1722 if (error)
1723 return error;
1724
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001725 if (xfs_dir2_db_to_fdb(args->geo, *dbno) != fbno) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001726 xfs_alert(mp,
1727"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld",
1728 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001729 (long long)xfs_dir2_db_to_fdb(args->geo, *dbno),
Dave Chinnera07258a2019-08-29 09:04:06 -07001730 (long long)*dbno, (long long)fbno);
1731 if (fblk) {
1732 xfs_alert(mp,
1733 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
1734 fblk, (unsigned long long)fblk->blkno,
1735 fblk->index, fblk->magic);
1736 } else {
1737 xfs_alert(mp, " ... fblk is NULL");
1738 }
1739 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1740 return -EFSCORRUPTED;
1741 }
1742
1743 /* Get a buffer for the new block. */
1744 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
1745 if (error)
1746 return error;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001747 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001748
1749 /* Remember the first slot as our empty slot. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001750 hdr->firstdb = (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinnera07258a2019-08-29 09:04:06 -07001751 XFS_DIR2_FREE_OFFSET)) *
Christoph Hellwig5893e4f2019-11-08 15:01:30 -08001752 args->geo->free_max_bests;
Dave Chinnera07258a2019-08-29 09:04:06 -07001753 } else {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001754 xfs_dir2_free_hdr_from_disk(mp, hdr, fbp->b_addr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001755 }
1756
1757 /* Set the freespace block index from the data block number. */
Christoph Hellwig3d92c932019-11-08 15:01:39 -08001758 *findex = xfs_dir2_db_to_fdindex(args->geo, *dbno);
Dave Chinnera07258a2019-08-29 09:04:06 -07001759
1760 /* Extend the freespace table if the new data block is off the end. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001761 if (*findex >= hdr->nvalid) {
Christoph Hellwig5893e4f2019-11-08 15:01:30 -08001762 ASSERT(*findex < args->geo->free_max_bests);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001763 hdr->nvalid = *findex + 1;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001764 hdr->bests[*findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinnera07258a2019-08-29 09:04:06 -07001765 }
1766
1767 /*
1768 * If this entry was for an empty data block (this should always be
1769 * true) then update the header.
1770 */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001771 if (hdr->bests[*findex] == cpu_to_be16(NULLDATAOFF)) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001772 hdr->nused++;
1773 xfs_dir2_free_hdr_to_disk(mp, fbp->b_addr, hdr);
Dave Chinnera07258a2019-08-29 09:04:06 -07001774 xfs_dir2_free_log_header(args, fbp);
1775 }
1776
1777 /* Update the freespace value for the new block in the table. */
Christoph Hellwig1848b602019-11-08 15:05:39 -08001778 bf = xfs_dir2_data_bestfree_p(mp, dbp->b_addr);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001779 hdr->bests[*findex] = bf[0].length;
Dave Chinnera07258a2019-08-29 09:04:06 -07001780
1781 *dbpp = dbp;
1782 *fbpp = fbp;
1783 return 0;
1784}
1785
Dave Chinner0e822252019-08-29 09:04:07 -07001786static int
1787xfs_dir2_node_find_freeblk(
1788 struct xfs_da_args *args,
1789 struct xfs_da_state_blk *fblk,
1790 xfs_dir2_db_t *dbnop,
1791 struct xfs_buf **fbpp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001792 struct xfs_dir3_icfree_hdr *hdr,
Dave Chinner0e822252019-08-29 09:04:07 -07001793 int *findexp,
1794 int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Dave Chinner0e822252019-08-29 09:04:07 -07001796 struct xfs_inode *dp = args->dp;
1797 struct xfs_trans *tp = args->trans;
1798 struct xfs_buf *fbp = NULL;
Dave Chinner756c6f02019-08-29 09:04:08 -07001799 xfs_dir2_db_t firstfbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001800 xfs_dir2_db_t lastfbno;
1801 xfs_dir2_db_t ifbno = -1;
1802 xfs_dir2_db_t dbno = -1;
Dave Chinner756c6f02019-08-29 09:04:08 -07001803 xfs_dir2_db_t fbno;
Dave Chinner0e822252019-08-29 09:04:07 -07001804 xfs_fileoff_t fo;
Dave Chinner610125a2019-08-29 09:04:07 -07001805 int findex = 0;
Dave Chinner0e822252019-08-29 09:04:07 -07001806 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * If we came in with a freespace block that means that lookup
1810 * found an entry with our hash value. This is the freespace
1811 * block for that data entry.
1812 */
1813 if (fblk) {
1814 fbp = fblk->bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 findex = fblk->index;
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001816 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (findex >= 0) {
Dave Chinner0e822252019-08-29 09:04:07 -07001818 /* caller already found the freespace for us. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001819 ASSERT(findex < hdr->nvalid);
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001820 ASSERT(be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF);
1821 ASSERT(be16_to_cpu(hdr->bests[findex]) >= length);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001822 dbno = hdr->firstdb + findex;
Dave Chinnera07258a2019-08-29 09:04:06 -07001823 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
Dave Chinner0e822252019-08-29 09:04:07 -07001825
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001826 /*
Dave Chinner0e822252019-08-29 09:04:07 -07001827 * The data block looked at didn't have enough room.
1828 * We'll start at the beginning of the freespace entries.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001829 */
Dave Chinner0e822252019-08-29 09:04:07 -07001830 ifbno = fblk->blkno;
Dave Chinner610125a2019-08-29 09:04:07 -07001831 xfs_trans_brelse(tp, fbp);
1832 fbp = NULL;
1833 fblk->bp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Dave Chinner0e822252019-08-29 09:04:07 -07001835
1836 /*
1837 * If we don't have a data block yet, we're going to scan the freespace
Dave Chinner610125a2019-08-29 09:04:07 -07001838 * data for a data block with enough free space in it.
Dave Chinner0e822252019-08-29 09:04:07 -07001839 */
1840 error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK);
1841 if (error)
1842 return error;
1843 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Dave Chinner756c6f02019-08-29 09:04:08 -07001844 firstfbno = xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET);
Dave Chinner0e822252019-08-29 09:04:07 -07001845
Dave Chinner756c6f02019-08-29 09:04:08 -07001846 for (fbno = lastfbno - 1; fbno >= firstfbno; fbno--) {
Dave Chinner610125a2019-08-29 09:04:07 -07001847 /* If it's ifbno we already looked at it. */
1848 if (fbno == ifbno)
1849 continue;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /*
Dave Chinner610125a2019-08-29 09:04:07 -07001852 * Read the block. There can be holes in the freespace blocks,
1853 * so this might not succeed. This should be really rare, so
1854 * there's no reason to avoid it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Dave Chinner610125a2019-08-29 09:04:07 -07001856 error = xfs_dir2_free_try_read(tp, dp,
1857 xfs_dir2_db_to_da(args->geo, fbno),
1858 &fbp);
1859 if (error)
1860 return error;
1861 if (!fbp)
1862 continue;
1863
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001864 xfs_dir2_free_hdr_from_disk(dp->i_mount, hdr, fbp->b_addr);
Dave Chinner610125a2019-08-29 09:04:07 -07001865
1866 /* Scan the free entry array for a large enough free space. */
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001867 for (findex = hdr->nvalid - 1; findex >= 0; findex--) {
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001868 if (be16_to_cpu(hdr->bests[findex]) != NULLDATAOFF &&
1869 be16_to_cpu(hdr->bests[findex]) >= length) {
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001870 dbno = hdr->firstdb + findex;
Dave Chinner610125a2019-08-29 09:04:07 -07001871 goto found_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 }
Dave Chinner610125a2019-08-29 09:04:07 -07001874
1875 /* Didn't find free space, go on to next free block */
1876 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Dave Chinner610125a2019-08-29 09:04:07 -07001878
Dave Chinner0e822252019-08-29 09:04:07 -07001879found_block:
1880 *dbnop = dbno;
1881 *fbpp = fbp;
1882 *findexp = findex;
1883 return 0;
1884}
1885
Dave Chinner0e822252019-08-29 09:04:07 -07001886/*
1887 * Add the data entry for a node-format directory name addition.
1888 * The leaf entry is added in xfs_dir2_leafn_add.
1889 * We may enter with a freespace block that the lookup found.
1890 */
1891static int
1892xfs_dir2_node_addname_int(
1893 struct xfs_da_args *args, /* operation arguments */
1894 struct xfs_da_state_blk *fblk) /* optional freespace block */
1895{
1896 struct xfs_dir2_data_unused *dup; /* data unused entry pointer */
1897 struct xfs_dir2_data_entry *dep; /* data entry pointer */
1898 struct xfs_dir2_data_hdr *hdr; /* data block header */
1899 struct xfs_dir2_data_free *bf;
Dave Chinner0e822252019-08-29 09:04:07 -07001900 struct xfs_trans *tp = args->trans;
1901 struct xfs_inode *dp = args->dp;
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001902 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner0e822252019-08-29 09:04:07 -07001903 struct xfs_buf *dbp; /* data block buffer */
1904 struct xfs_buf *fbp; /* freespace buffer */
1905 xfs_dir2_data_aoff_t aoff;
1906 xfs_dir2_db_t dbno; /* data block number */
1907 int error; /* error return value */
1908 int findex; /* freespace entry index */
1909 int length; /* length of the new entry */
1910 int logfree = 0; /* need to log free entry */
1911 int needlog = 0; /* need to log data header */
1912 int needscan = 0; /* need to rescan data frees */
1913 __be16 *tagp; /* data entry tag pointer */
Dave Chinner0e822252019-08-29 09:04:07 -07001914
Christoph Hellwigfdbb8c52019-11-08 15:05:37 -08001915 length = xfs_dir2_data_entsize(dp->i_mount, args->namelen);
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001916 error = xfs_dir2_node_find_freeblk(args, fblk, &dbno, &fbp, &freehdr,
1917 &findex, length);
Dave Chinner0e822252019-08-29 09:04:07 -07001918 if (error)
1919 return error;
1920
1921 /*
1922 * Now we know if we must allocate blocks, so if we are checking whether
1923 * we can insert without allocation then we can return now.
1924 */
1925 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
1926 if (dbno == -1)
1927 return -ENOSPC;
1928 return 0;
1929 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
1932 * If we don't have a data block, we need to allocate one and make
1933 * the freespace entries refer to it.
1934 */
Dave Chinnera07258a2019-08-29 09:04:06 -07001935 if (dbno == -1) {
Dave Chinnera07258a2019-08-29 09:04:06 -07001936 /* we're going to have to log the free block index later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 logfree = 1;
Dave Chinner0e822252019-08-29 09:04:07 -07001938 error = xfs_dir2_node_add_datablk(args, fblk, &dbno, &dbp, &fbp,
Christoph Hellwig195b0a42019-11-08 14:57:53 -08001939 &freehdr, &findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001940 } else {
Dave Chinnera07258a2019-08-29 09:04:06 -07001941 /* Read the data block in. */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001942 error = xfs_dir3_data_read(tp, dp,
1943 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001944 -1, &dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Dave Chinner0e822252019-08-29 09:04:07 -07001946 if (error)
1947 return error;
Dave Chinnera07258a2019-08-29 09:04:06 -07001948
1949 /* setup for data block up now */
1950 hdr = dbp->b_addr;
Christoph Hellwig1848b602019-11-08 15:05:39 -08001951 bf = xfs_dir2_data_bestfree_p(dp->i_mount, hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001952 ASSERT(be16_to_cpu(bf[0].length) >= length);
Dave Chinnera07258a2019-08-29 09:04:06 -07001953
1954 /* Point to the existing unused space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001956 ((char *)hdr + be16_to_cpu(bf[0].offset));
Dave Chinnera07258a2019-08-29 09:04:06 -07001957
1958 /* Mark the first part of the unused space, inuse for us. */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001959 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
1960 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
1961 &needlog, &needscan);
1962 if (error) {
1963 xfs_trans_brelse(tp, dbp);
1964 return error;
1965 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001966
1967 /* Fill in the new entry and log it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001969 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dep->namelen = args->namelen;
1971 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001972 dp->d_ops->data_put_ftype(dep, args->filetype);
Christoph Hellwig7e8ae7bd2019-11-08 15:05:37 -08001973 tagp = xfs_dir2_data_entry_tag_p(dp->i_mount, dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001974 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001975 xfs_dir2_data_log_entry(args, dbp, dep);
Dave Chinnera07258a2019-08-29 09:04:06 -07001976
1977 /* Rescan the freespace and log the data block if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001979 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001981 xfs_dir2_data_log_header(args, dbp);
Dave Chinnera07258a2019-08-29 09:04:06 -07001982
1983 /* If the freespace block entry is now wrong, update it. */
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001984 if (freehdr.bests[findex] != bf[0].length) {
1985 freehdr.bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 logfree = 1;
1987 }
Dave Chinnera07258a2019-08-29 09:04:06 -07001988
1989 /* Log the freespace entry if needed. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (logfree)
Christoph Hellwiga84f3d52019-11-08 14:58:05 -08001991 xfs_dir2_free_log_bests(args, &freehdr, fbp, findex, findex);
Dave Chinnera07258a2019-08-29 09:04:06 -07001992
1993 /* Return the data block and offset in args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001995 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
1997}
1998
1999/*
Dave Chinneraee7754b2019-08-29 09:04:06 -07002000 * Top-level node form directory addname routine.
2001 */
2002int /* error */
2003xfs_dir2_node_addname(
2004 xfs_da_args_t *args) /* operation arguments */
2005{
2006 xfs_da_state_blk_t *blk; /* leaf block for insert */
2007 int error; /* error return value */
2008 int rval; /* sub-return value */
2009 xfs_da_state_t *state; /* btree cursor */
2010
2011 trace_xfs_dir2_node_addname(args);
2012
2013 /*
2014 * Allocate and initialize the state (btree cursor).
2015 */
2016 state = xfs_da_state_alloc();
2017 state->args = args;
2018 state->mp = args->dp->i_mount;
2019 /*
2020 * Look up the name. We're not supposed to find it, but
2021 * this gives us the insertion point.
2022 */
2023 error = xfs_da3_node_lookup_int(state, &rval);
2024 if (error)
2025 rval = error;
2026 if (rval != -ENOENT) {
2027 goto done;
2028 }
2029 /*
2030 * Add the data entry to a data block.
2031 * Extravalid is set to a freeblock found by lookup.
2032 */
2033 rval = xfs_dir2_node_addname_int(args,
2034 state->extravalid ? &state->extrablk : NULL);
2035 if (rval) {
2036 goto done;
2037 }
2038 blk = &state->path.blk[state->path.active - 1];
2039 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2040 /*
2041 * Add the new leaf entry.
2042 */
2043 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
2044 if (rval == 0) {
2045 /*
2046 * It worked, fix the hash values up the btree.
2047 */
2048 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
2049 xfs_da3_fixhashpath(state, &state->path);
2050 } else {
2051 /*
2052 * It didn't work, we need to split the leaf block.
2053 */
2054 if (args->total == 0) {
2055 ASSERT(rval == -ENOSPC);
2056 goto done;
2057 }
2058 /*
2059 * Split the leaf block and insert the new entry.
2060 */
2061 rval = xfs_da3_split(state);
2062 }
2063done:
2064 xfs_da_state_free(state);
2065 return rval;
2066}
2067
2068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002070 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * The only real output is the inode number of the entry.
2072 */
2073int /* error */
2074xfs_dir2_node_lookup(
2075 xfs_da_args_t *args) /* operation arguments */
2076{
2077 int error; /* error return value */
2078 int i; /* btree level */
2079 int rval; /* operation return value */
2080 xfs_da_state_t *state; /* btree cursor */
2081
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002082 trace_xfs_dir2_node_lookup(args);
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Allocate and initialize the btree cursor.
2086 */
2087 state = xfs_da_state_alloc();
2088 state->args = args;
2089 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * Fill in the path to the entry in the cursor.
2092 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002093 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (error)
2095 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002096 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2097 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002098 xfs_dir2_data_entry_t *dep;
2099
Dave Chinner1d9025e2012-06-22 18:50:14 +10002100 dep = (xfs_dir2_data_entry_t *)
2101 ((char *)state->extrablk.bp->b_addr +
2102 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002103 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Release the btree blocks and leaf block.
2107 */
2108 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002109 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 state->path.blk[i].bp = NULL;
2111 }
2112 /*
2113 * Release the data block if we have it.
2114 */
2115 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002116 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 state->extrablk.bp = NULL;
2118 }
2119 xfs_da_state_free(state);
2120 return rval;
2121}
2122
2123/*
2124 * Remove an entry from a node-format directory.
2125 */
2126int /* error */
2127xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002128 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002130 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 int error; /* error return value */
2132 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002133 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002135 trace_xfs_dir2_node_removename(args);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * Allocate and initialize the btree cursor.
2139 */
2140 state = xfs_da_state_alloc();
2141 state->args = args;
2142 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002143
2144 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002145 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002146 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002147 goto out_free;
2148
2149 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002150 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002151 error = rval;
2152 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 blk = &state->path.blk[state->path.active - 1];
2156 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2157 ASSERT(state->extravalid);
2158 /*
2159 * Remove the leaf and data entries.
2160 * Extrablk refers to the data block.
2161 */
2162 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2163 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002164 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002165 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * Fix the hash values up the btree.
2168 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002169 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * If we need to join leaf blocks, do it.
2172 */
2173 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002174 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * If no errors so far, try conversion to leaf format.
2177 */
2178 if (!error)
2179 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002180out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 xfs_da_state_free(state);
2182 return error;
2183}
2184
2185/*
2186 * Replace an entry's inode number in a node-format directory.
2187 */
2188int /* error */
2189xfs_dir2_node_replace(
2190 xfs_da_args_t *args) /* operation arguments */
2191{
2192 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002193 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 xfs_dir2_data_entry_t *dep; /* data entry changed */
2195 int error; /* error return value */
2196 int i; /* btree level */
2197 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002198 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int rval; /* internal return value */
2200 xfs_da_state_t *state; /* btree cursor */
2201
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002202 trace_xfs_dir2_node_replace(args);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /*
2205 * Allocate and initialize the btree cursor.
2206 */
2207 state = xfs_da_state_alloc();
2208 state->args = args;
2209 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002210
2211 /*
2212 * We have to save new inode number and ftype since
2213 * xfs_da3_node_lookup_int() is going to overwrite them
2214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002216 ftype = args->filetype;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Lookup the entry to change in the btree.
2220 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002221 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (error) {
2223 rval = error;
2224 }
2225 /*
2226 * It should be found, since the vnodeops layer has looked it up
2227 * and locked it. But paranoia is good.
2228 */
Dave Chinner24513372014-06-25 14:58:08 +10002229 if (rval == -EEXIST) {
Christoph Hellwig787b0892019-11-08 14:57:50 -08002230 struct xfs_dir3_icleaf_hdr leafhdr;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 /*
2233 * Find the leaf entry.
2234 */
2235 blk = &state->path.blk[state->path.active - 1];
2236 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 ASSERT(state->extravalid);
Christoph Hellwig787b0892019-11-08 14:57:50 -08002238
2239 xfs_dir2_leaf_hdr_from_disk(state->mp, &leafhdr,
2240 blk->bp->b_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /*
2242 * Point to the data entry.
2243 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002244 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002245 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2246 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002248 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002249 xfs_dir2_dataptr_to_off(args->geo,
Christoph Hellwig787b0892019-11-08 14:57:50 -08002250 be32_to_cpu(leafhdr.ents[blk->index].address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002251 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Fill in the new inode number and log the entry.
2254 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002255 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002256 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002257 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rval = 0;
2259 }
2260 /*
2261 * Didn't find it, and we're holding a data block. Drop it.
2262 */
2263 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002264 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 state->extrablk.bp = NULL;
2266 }
2267 /*
2268 * Release all the buffers in the cursor.
2269 */
2270 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002271 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 state->path.blk[i].bp = NULL;
2273 }
2274 xfs_da_state_free(state);
2275 return rval;
2276}
2277
2278/*
2279 * Trim off a trailing empty freespace block.
2280 * Return (in rvalp) 1 if we did it, 0 if not.
2281 */
2282int /* error */
2283xfs_dir2_node_trim_free(
2284 xfs_da_args_t *args, /* operation arguments */
2285 xfs_fileoff_t fo, /* free block number */
2286 int *rvalp) /* out: did something */
2287{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002288 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 xfs_inode_t *dp; /* incore directory inode */
2290 int error; /* error return code */
2291 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002293 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002297
2298 *rvalp = 0;
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * Read the freespace block.
2302 */
Dave Chinner20252072012-11-12 22:54:13 +11002303 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002304 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /*
2307 * There can be holes in freespace. If fo is a hole, there's
2308 * nothing to do.
2309 */
Dave Chinner20252072012-11-12 22:54:13 +11002310 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002312 free = bp->b_addr;
Christoph Hellwig5ba30912019-11-08 14:57:52 -08002313 xfs_dir2_free_hdr_from_disk(dp->i_mount, &freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /*
2316 * If there are used entries, there's nothing to do.
2317 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002318 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002319 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 return 0;
2321 }
2322 /*
2323 * Blow the block away.
2324 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002325 error = xfs_dir2_shrink_inode(args,
2326 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2327 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /*
2329 * Can't fail with ENOSPC since that only happens with no
2330 * space reservation, when breaking up an extent into two
2331 * pieces. This is the last block of an extent.
2332 */
Dave Chinner24513372014-06-25 14:58:08 +10002333 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002334 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return error;
2336 }
2337 /*
2338 * Return that we succeeded.
2339 */
2340 *rvalp = 1;
2341 return 0;
2342}