blob: 1fc44efc344df9ebfd22597b57447e3b77cfb836 [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);
35static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
36 xfs_da_state_blk_t *fblk);
37
Dave Chinner24df33b2013-04-12 07:30:21 +100038/*
39 * Check internal consistency of a leafn block.
40 */
41#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080042static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100043xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110044 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100045 struct xfs_buf *bp)
46{
47 struct xfs_dir2_leaf *leaf = bp->b_addr;
48 struct xfs_dir3_icleaf_hdr leafhdr;
49
Dave Chinner01ba43b2013-10-29 22:11:52 +110050 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100051
52 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
53 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
54 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080055 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100056 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080057 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100058
Dave Chinner41419562013-10-29 22:11:50 +110059 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100060}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080061
62static inline void
63xfs_dir3_leaf_check(
64 struct xfs_inode *dp,
65 struct xfs_buf *bp)
66{
67 xfs_failaddr_t fa;
68
69 fa = xfs_dir3_leafn_check(dp, bp);
70 if (!fa)
71 return;
72 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070073 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
74 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080075 ASSERT(0);
76}
Dave Chinner24df33b2013-04-12 07:30:21 +100077#else
Dave Chinner41419562013-10-29 22:11:50 +110078#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100079#endif
80
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080081static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110082xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110083 struct xfs_buf *bp)
84{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -070085 struct xfs_mount *mp = bp->b_mount;
Dave Chinner20252072012-11-12 22:54:13 +110086 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110087
Brian Foster39708c22019-02-07 10:45:48 -080088 if (!xfs_verify_magic(bp, hdr->magic))
89 return __this_address;
90
Dave Chinnercbc8adf2013-04-03 16:11:21 +110091 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
93
Eric Sandeence748ea2015-07-29 11:53:31 +100094 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080095 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110096 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080097 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +110098 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080099 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100100 }
101
102 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
103
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800104 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100105}
106
107static void
108xfs_dir3_free_read_verify(
109 struct xfs_buf *bp)
110{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700111 struct xfs_mount *mp = bp->b_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800112 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100113
Eric Sandeence5028c2014-02-27 15:23:10 +1100114 if (xfs_sb_version_hascrc(&mp->m_sb) &&
115 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800116 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
117 else {
118 fa = xfs_dir3_free_verify(bp);
119 if (fa)
120 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
121 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100122}
Dave Chinner20252072012-11-12 22:54:13 +1100123
Dave Chinner612cfbf2012-11-14 17:52:32 +1100124static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100125xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100126 struct xfs_buf *bp)
127{
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700128 struct xfs_mount *mp = bp->b_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800129 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800131 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100132
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800133 fa = xfs_dir3_free_verify(bp);
134 if (fa) {
135 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100136 return;
137 }
138
139 if (!xfs_sb_version_hascrc(&mp->m_sb))
140 return;
141
142 if (bip)
143 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
144
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100145 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100146}
147
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100148const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100149 .name = "xfs_dir3_free",
Brian Foster39708c22019-02-07 10:45:48 -0800150 .magic = { cpu_to_be32(XFS_DIR2_FREE_MAGIC),
151 cpu_to_be32(XFS_DIR3_FREE_MAGIC) },
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100152 .verify_read = xfs_dir3_free_read_verify,
153 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800154 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100155};
Dave Chinner20252072012-11-12 22:54:13 +1100156
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800157/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800158static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800159xfs_dir3_free_header_check(
160 struct xfs_inode *dp,
161 xfs_dablk_t fbno,
162 struct xfs_buf *bp)
163{
164 struct xfs_mount *mp = dp->i_mount;
165 unsigned int firstdb;
166 int maxbests;
167
168 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
169 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
170 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
171 maxbests;
172 if (xfs_sb_version_hascrc(&mp->m_sb)) {
173 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
174
175 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800176 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800177 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800178 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800179 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800180 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800181 } else {
182 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
183
184 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800185 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800186 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800187 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800188 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800189 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800190 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800191 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800192}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100193
Dave Chinner20252072012-11-12 22:54:13 +1100194static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100195__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100196 struct xfs_trans *tp,
197 struct xfs_inode *dp,
198 xfs_dablk_t fbno,
199 xfs_daddr_t mappedbno,
200 struct xfs_buf **bpp)
201{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800202 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100203 int err;
204
205 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100206 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800207 if (err || !*bpp)
208 return err;
209
210 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800211 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
212 if (fa) {
213 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800214 xfs_trans_brelse(tp, *bpp);
215 return -EFSCORRUPTED;
216 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100217
218 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800219 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100220 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800221
222 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100223}
224
225int
226xfs_dir2_free_read(
227 struct xfs_trans *tp,
228 struct xfs_inode *dp,
229 xfs_dablk_t fbno,
230 struct xfs_buf **bpp)
231{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100232 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100233}
234
235static int
236xfs_dir2_free_try_read(
237 struct xfs_trans *tp,
238 struct xfs_inode *dp,
239 xfs_dablk_t fbno,
240 struct xfs_buf **bpp)
241{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100242 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
243}
244
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100245static int
246xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000247 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100248 xfs_dir2_db_t fbno,
249 struct xfs_buf **bpp)
250{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000251 struct xfs_trans *tp = args->trans;
252 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100253 struct xfs_mount *mp = dp->i_mount;
254 struct xfs_buf *bp;
255 int error;
256 struct xfs_dir3_icfree_hdr hdr;
257
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000258 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100259 -1, &bp, XFS_DATA_FORK);
260 if (error)
261 return error;
262
Dave Chinner61fe1352013-04-03 16:11:30 +1100263 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100264 bp->b_ops = &xfs_dir3_free_buf_ops;
265
266 /*
267 * Initialize the new block to be empty, and remember
268 * its first slot as our empty slot.
269 */
Dave Chinnere400d272013-05-28 18:37:17 +1000270 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
271 memset(&hdr, 0, sizeof(hdr));
272
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100273 if (xfs_sb_version_hascrc(&mp->m_sb)) {
274 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
275
276 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000277
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100278 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
279 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000280 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000281 } else
282 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100283 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100284 *bpp = bp;
285 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * Log entries from a freespace block.
290 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000291STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000293 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000294 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int first, /* first entry to log */
296 int last) /* last entry to log */
297{
298 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100299 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dave Chinner1d9025e2012-06-22 18:50:14 +1000301 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000302 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100303 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
304 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000305 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100306 (uint)((char *)&bests[first] - (char *)free),
307 (uint)((char *)&bests[last] - (char *)free +
308 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
312 * Log header from a freespace block.
313 */
314static void
315xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000316 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000317 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Dave Chinner836a94a2013-08-12 20:49:44 +1000319#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 xfs_dir2_free_t *free; /* freespace structure */
321
Dave Chinner1d9025e2012-06-22 18:50:14 +1000322 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100323 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
324 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000325#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000326 xfs_trans_log_buf(args->trans, bp, 0,
327 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/*
331 * Convert a leaf-format directory to a node-format directory.
332 * We need to change the magic number of the leaf block, and copy
333 * the freespace table out of the leaf block into its own block.
334 */
335int /* error */
336xfs_dir2_leaf_to_node(
337 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000338 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 xfs_inode_t *dp; /* incore directory inode */
341 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000342 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 xfs_dir2_db_t fdb; /* freespace block number */
344 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100345 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int i; /* leaf freespace index */
347 xfs_dir2_leaf_t *leaf; /* leaf structure */
348 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 int n; /* count of live freespc ents */
350 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100351 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100353 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000355 trace_xfs_dir2_leaf_to_node(args);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 tp = args->trans;
359 /*
360 * Add a freespace block to the directory.
361 */
362 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
363 return error;
364 }
Dave Chinner30028032014-06-06 15:08:18 +1000365 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Get the buffer for the new freespace block.
368 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000369 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100372
Dave Chinner1d9025e2012-06-22 18:50:14 +1000373 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100374 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000375 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000376 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800377 if (be32_to_cpu(ltp->bestcount) >
378 (uint)dp->i_d.di_size / args->geo->blksize)
379 return -EFSCORRUPTED;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Copy freespace entries from the leaf block to the new block.
383 * Count active entries.
384 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100385 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500386 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100387 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100388 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100390 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100394 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100396 freehdr.nused = n;
397 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
398
Dave Chinner01ba43b2013-10-29 22:11:52 +1100399 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000400 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
401 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100402
Dave Chinner24df33b2013-04-12 07:30:21 +1000403 /*
404 * Converting the leaf to a leafnode is just a matter of changing the
405 * magic number and the ops. Do the change directly to the buffer as
406 * it's less work (and less code) than decoding the header to host
407 * format and back again.
408 */
409 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
410 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
411 else
412 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
413 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100414 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000415 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100416 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 0;
418}
419
420/*
421 * Add a leaf entry to a leaf block in a node-form directory.
422 * The other work necessary is done from the caller.
423 */
424static int /* error */
425xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000426 struct xfs_buf *bp, /* leaf buffer */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800427 struct xfs_da_args *args, /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 int index) /* insertion pt for new entry */
429{
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800430 struct xfs_dir3_icleaf_hdr leafhdr;
431 struct xfs_inode *dp = args->dp;
432 struct xfs_dir2_leaf *leaf = bp->b_addr;
433 struct xfs_dir2_leaf_entry *lep;
434 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int compact; /* compacting stale leaves */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800436 int highstale = 0; /* next stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int lfloghigh; /* high leaf entry logging */
438 int lfloglow; /* low leaf entry logging */
Darrick J. Wong79622c7ce2019-03-07 16:50:11 -0800439 int lowstale = 0; /* previous stale entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000441 trace_xfs_dir2_leafn_add(args, index);
442
Dave Chinner01ba43b2013-10-29 22:11:52 +1100443 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100444 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /*
447 * Quick check just to make sure we are not going to index
448 * into other peoples memory
449 */
450 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000451 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 /*
454 * If there are already the maximum number of leaf entries in
455 * the block, if there are no stale entries it won't fit.
456 * Caller will do a split. If there are stale entries we'll do
457 * a compact.
458 */
459
Dave Chinner8f661932014-06-06 15:15:59 +1000460 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000461 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000462 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000463 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else
465 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000466 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
467 ASSERT(index == leafhdr.count ||
468 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Barry Naujok6a178102008-05-21 16:42:05 +1000470 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return 0;
472
473 /*
474 * Compact out all but one stale leaf entry. Leaves behind
475 * the entry closest to index.
476 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000477 if (compact)
478 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
479 &highstale, &lfloglow, &lfloghigh);
480 else if (leafhdr.stale) {
481 /*
482 * Set impossible logging indices for this case.
483 */
484 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 lfloghigh = -1;
486 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /*
489 * Insert the new entry, log everything.
490 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000491 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200492 highstale, &lfloglow, &lfloghigh);
493
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100494 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000495 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100496 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000497
Dave Chinner01ba43b2013-10-29 22:11:52 +1100498 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000499 xfs_dir3_leaf_log_header(args, bp);
500 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100501 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503}
504
505#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100506static void
507xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100508 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100509 struct xfs_buf *bp,
510 xfs_dir2_db_t db)
511{
512 struct xfs_dir3_icfree_hdr hdr;
513
Dave Chinner01ba43b2013-10-29 22:11:52 +1100514 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100515
Dave Chinner8f661932014-06-06 15:15:59 +1000516 ASSERT((hdr.firstdb %
517 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100518 ASSERT(hdr.firstdb <= db);
519 ASSERT(db < hdr.firstdb + hdr.nvalid);
520}
521#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100522#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#endif /* DEBUG */
524
525/*
526 * Return the last hash value in the leaf.
527 * Stale entries are ok.
528 */
529xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700530xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100531 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000532 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int *count) /* count of entries in leaf */
534{
Dave Chinner24df33b2013-04-12 07:30:21 +1000535 struct xfs_dir2_leaf *leaf = bp->b_addr;
536 struct xfs_dir2_leaf_entry *ents;
537 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Chinner01ba43b2013-10-29 22:11:52 +1100539 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000540
541 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700542 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
543 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
544 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000547 *count = leafhdr.count;
548 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000550
Dave Chinner41419562013-10-29 22:11:50 +1100551 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000552 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000556 * Look up a leaf entry for space to add a name in a node-format leaf block.
557 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000559STATIC int
560xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000561 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 xfs_da_args_t *args, /* operation arguments */
563 int *indexp, /* out: leaf entry index */
564 xfs_da_state_t *state) /* state to fill in */
565{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000566 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000567 xfs_dir2_db_t curdb = -1; /* current data block number */
568 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 xfs_inode_t *dp; /* incore directory inode */
570 int error; /* error return value */
571 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000572 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int index; /* leaf entry index */
574 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000575 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
577 xfs_mount_t *mp; /* filesystem mount point */
578 xfs_dir2_db_t newdb; /* new data block number */
579 xfs_dir2_db_t newfdb; /* new free block number */
580 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000581 struct xfs_dir2_leaf_entry *ents;
582 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 dp = args->dp;
585 tp = args->trans;
586 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000587 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100588 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100589 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000590
Dave Chinner41419562013-10-29 22:11:50 +1100591 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000592 ASSERT(leafhdr.count > 0);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Look up the hash value in the leaf entries.
596 */
597 index = xfs_dir2_leaf_search_hash(args, bp);
598 /*
599 * Do we have a buffer coming in?
600 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601 if (state->extravalid) {
602 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000604 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000605 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100606 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
607 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100609 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Loop over leaf entries with the right hash value.
612 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000613 for (lep = &ents[index];
614 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
615 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /*
617 * Skip stale leaf entries.
618 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100619 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 continue;
621 /*
622 * Pull the data block number from the entry.
623 */
Dave Chinner30028032014-06-06 15:08:18 +1000624 newdb = xfs_dir2_dataptr_to_db(args->geo,
625 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * For addname, we're looking for a place to put the new entry.
628 * We want to use a data block with an entry of equal
629 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000630 *
631 * If this block isn't the data block we already have
632 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000634 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100635 __be16 *bests;
636
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000637 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 * Convert the data block to the free block
640 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Dave Chinner8f661932014-06-06 15:15:59 +1000642 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000648 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
650 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000651 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100652
653 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000654 xfs_dir2_db_to_da(args->geo,
655 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100656 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000657 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000659 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100660
Dave Chinner01ba43b2013-10-29 22:11:52 +1100661 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Dave Chinner8f661932014-06-06 15:15:59 +1000666 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000668 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500670 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100671 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000672 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
673 XFS_ERRLEVEL_LOW, mp);
674 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000675 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000676 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100679 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000680 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683 /* Didn't find any space */
684 fi = -1;
685out:
Barry Naujok6a178102008-05-21 16:42:05 +1000686 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687 if (curbp) {
688 /* Giving back a free block. */
689 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000691 state->extrablk.index = fi;
692 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100693
694 /*
695 * Important: this magic number is not in the buffer - it's for
696 * buffer type information and therefore only the free/data type
697 * matters here, not whether CRCs are enabled or not.
698 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000699 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
700 } else {
701 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000704 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 */
706 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000707 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000711 * Look up a leaf entry in a node-format leaf block.
712 * The extrablk in state a data block.
713 */
714STATIC int
715xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000716 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000717 xfs_da_args_t *args, /* operation arguments */
718 int *indexp, /* out: leaf entry index */
719 xfs_da_state_t *state) /* state to fill in */
720{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000721 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000722 xfs_dir2_db_t curdb = -1; /* current data block number */
723 xfs_dir2_data_entry_t *dep; /* data block entry */
724 xfs_inode_t *dp; /* incore directory inode */
725 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000726 int index; /* leaf entry index */
727 xfs_dir2_leaf_t *leaf; /* leaf structure */
728 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
729 xfs_mount_t *mp; /* filesystem mount point */
730 xfs_dir2_db_t newdb; /* new data block number */
731 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000732 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000733 struct xfs_dir2_leaf_entry *ents;
734 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000735
736 dp = args->dp;
737 tp = args->trans;
738 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000739 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100740 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100741 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000742
Dave Chinner41419562013-10-29 22:11:50 +1100743 xfs_dir3_leaf_check(dp, bp);
Darrick J. Wong858b44d2019-08-11 15:52:26 -0700744 if (leafhdr.count <= 0)
745 return -EFSCORRUPTED;
Dave Chinner24df33b2013-04-12 07:30:21 +1000746
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000747 /*
748 * Look up the hash value in the leaf entries.
749 */
750 index = xfs_dir2_leaf_search_hash(args, bp);
751 /*
752 * Do we have a buffer coming in?
753 */
754 if (state->extravalid) {
755 curbp = state->extrablk.bp;
756 curdb = state->extrablk.blkno;
757 }
758 /*
759 * Loop over leaf entries with the right hash value.
760 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000761 for (lep = &ents[index];
762 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
763 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000764 /*
765 * Skip stale leaf entries.
766 */
767 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
768 continue;
769 /*
770 * Pull the data block number from the entry.
771 */
Dave Chinner30028032014-06-06 15:08:18 +1000772 newdb = xfs_dir2_dataptr_to_db(args->geo,
773 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000774 /*
775 * Not adding a new entry, so we really want to find
776 * the name given to us.
777 *
778 * If it's a different data block, go get it.
779 */
780 if (newdb != curdb) {
781 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000782 * If we had a block before that we aren't saving
783 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000784 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000785 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
786 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000787 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000788 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000789 * If needing the block that is saved with a CI match,
790 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000791 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000792 if (args->cmpresult != XFS_CMP_DIFFERENT &&
793 newdb == state->extrablk.blkno) {
794 ASSERT(state->extravalid);
795 curbp = state->extrablk.bp;
796 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100797 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000798 xfs_dir2_db_to_da(args->geo,
799 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100800 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000801 if (error)
802 return error;
803 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100804 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000805 curdb = newdb;
806 }
807 /*
808 * Point to the data entry.
809 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000810 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000811 xfs_dir2_dataptr_to_off(args->geo,
812 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000813 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000814 * Compare the entry and if it's an exact match, return
815 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000816 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000817 */
Barry Naujok5163f952008-05-21 16:41:01 +1000818 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
819 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000820 /* If there is a CI match block, drop it */
821 if (args->cmpresult != XFS_CMP_DIFFERENT &&
822 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000823 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000824 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000825 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100826 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000827 *indexp = index;
828 state->extravalid = 1;
829 state->extrablk.bp = curbp;
830 state->extrablk.blkno = curdb;
831 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000832 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000833 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100834 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100835 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000836 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000837 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000838 }
839 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000840 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000841 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000842 if (args->cmpresult == XFS_CMP_DIFFERENT) {
843 /* Giving back last used data block. */
844 state->extravalid = 1;
845 state->extrablk.bp = curbp;
846 state->extrablk.index = -1;
847 state->extrablk.blkno = curdb;
848 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100849 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100850 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000851 } else {
852 /* If the curbp is not the CI match block, drop it */
853 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000854 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000855 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000856 } else {
857 state->extravalid = 0;
858 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000859 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000860 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000861}
862
863/*
864 * Look up a leaf entry in a node-format leaf block.
865 * If this is an addname then the extrablk in state is a freespace block,
866 * otherwise it's a data block.
867 */
868int
869xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000870 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000871 xfs_da_args_t *args, /* operation arguments */
872 int *indexp, /* out: leaf entry index */
873 xfs_da_state_t *state) /* state to fill in */
874{
Barry Naujok6a178102008-05-21 16:42:05 +1000875 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000876 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
877 state);
878 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
879}
880
881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * Move count leaf entries from source to destination leaf.
883 * Log entries and headers. Stale entries are preserved.
884 */
885static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000886xfs_dir3_leafn_moveents(
887 xfs_da_args_t *args, /* operation arguments */
888 struct xfs_buf *bp_s, /* source */
889 struct xfs_dir3_icleaf_hdr *shdr,
890 struct xfs_dir2_leaf_entry *sents,
891 int start_s,/* source leaf index */
892 struct xfs_buf *bp_d, /* destination */
893 struct xfs_dir3_icleaf_hdr *dhdr,
894 struct xfs_dir2_leaf_entry *dents,
895 int start_d,/* destination leaf index */
896 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Dave Chinner24df33b2013-04-12 07:30:21 +1000898 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000900 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * Silently return if nothing to do.
904 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000905 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 /*
909 * If the destination index is not the end of the current
910 * destination leaf entries, open up a hole in the destination
911 * to hold the new entries.
912 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000913 if (start_d < dhdr->count) {
914 memmove(&dents[start_d + count], &dents[start_d],
915 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000916 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919 /*
920 * If the source has stale leaves, count the ones in the copy range
921 * so we can update the header correctly.
922 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000923 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 int i; /* temp leaf index */
925
926 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000927 if (sents[i].address ==
928 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 stale++;
930 }
931 } else
932 stale = 0;
933 /*
934 * Copy the leaf entries from source to destination.
935 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000936 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000938 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * If there are source entries after the ones we copied,
942 * delete the ones we copied by sliding the next ones down.
943 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000944 if (start_s + count < shdr->count) {
945 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000947 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /*
951 * Update the headers and log them.
952 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000953 shdr->count -= count;
954 shdr->stale -= stale;
955 dhdr->count += count;
956 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
960 * Determine the sort order of two leaf blocks.
961 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
962 */
963int /* sort order */
964xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100965 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000966 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
967 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Dave Chinner24df33b2013-04-12 07:30:21 +1000969 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
970 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
971 struct xfs_dir2_leaf_entry *ents1;
972 struct xfs_dir2_leaf_entry *ents2;
973 struct xfs_dir3_icleaf_hdr hdr1;
974 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Chinner01ba43b2013-10-29 22:11:52 +1100976 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
977 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100978 ents1 = dp->d_ops->leaf_ents_p(leaf1);
979 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000980
981 if (hdr1.count > 0 && hdr2.count > 0 &&
982 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
983 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
984 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return 1;
986 return 0;
987}
988
989/*
990 * Rebalance leaf entries between two leaf blocks.
991 * This is actually only called when the second block is new,
992 * though the code deals with the general case.
993 * A new entry will be inserted in one of the blocks, and that
994 * entry is taken into account when balancing.
995 */
996static void
997xfs_dir2_leafn_rebalance(
998 xfs_da_state_t *state, /* btree cursor */
999 xfs_da_state_blk_t *blk1, /* first btree block */
1000 xfs_da_state_blk_t *blk2) /* second btree block */
1001{
1002 xfs_da_args_t *args; /* operation arguments */
1003 int count; /* count (& direction) leaves */
1004 int isleft; /* new goes in left leaf */
1005 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1006 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1007 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001008#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 int oldstale; /* old count of stale leaves */
1010#endif
1011 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001012 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001013 struct xfs_dir2_leaf_entry *ents1;
1014 struct xfs_dir2_leaf_entry *ents2;
1015 struct xfs_dir3_icleaf_hdr hdr1;
1016 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001017 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 args = state->args;
1020 /*
1021 * If the block order is wrong, swap the arguments.
1022 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001023 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1024 if (swap_blocks)
1025 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Dave Chinner1d9025e2012-06-22 18:50:14 +10001027 leaf1 = blk1->bp->b_addr;
1028 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001029 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1030 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001031 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1032 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001033
1034 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001035#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001036 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037#endif
1038 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * If the old leaf count was odd then the new one will be even,
1042 * so we need to divide the new count evenly.
1043 */
1044 if (oldsum & 1) {
1045 xfs_dahash_t midhash; /* middle entry hash value */
1046
Dave Chinner24df33b2013-04-12 07:30:21 +10001047 if (mid >= hdr1.count)
1048 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001050 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 isleft = args->hashval <= midhash;
1052 }
1053 /*
1054 * If the old count is even then the new count is odd, so there's
1055 * no preferred side for the new entry.
1056 * Pick the left one.
1057 */
1058 else
1059 isleft = 1;
1060 /*
1061 * Calculate moved entry count. Positive means left-to-right,
1062 * negative means right-to-left. Then move the entries.
1063 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001064 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001066 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1067 hdr1.count - count, blk2->bp,
1068 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001070 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1071 blk1->bp, &hdr1, ents1,
1072 hdr1.count, count);
1073
1074 ASSERT(hdr1.count + hdr2.count == oldsum);
1075 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1076
1077 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001078 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1079 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001080 xfs_dir3_leaf_log_header(args, blk1->bp);
1081 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001082
Dave Chinner41419562013-10-29 22:11:50 +11001083 xfs_dir3_leaf_check(dp, blk1->bp);
1084 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /*
1087 * Mark whether we're inserting into the old or new leaf.
1088 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001089 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001090 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001091 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001092 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001094 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
1096 * Adjust the expected index for insertion.
1097 */
1098 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001099 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001100
1101 /*
1102 * Finally sanity check just to make sure we are not returning a
1103 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Dave Chinner41419562013-10-29 22:11:50 +11001105 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 state->inleaf = 1;
1107 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001108 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001109 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001110 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112}
1113
Dave Chinner20252072012-11-12 22:54:13 +11001114static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001115xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001116 xfs_da_args_t *args,
1117 struct xfs_dir2_data_hdr *hdr,
1118 struct xfs_dir2_free *free,
1119 xfs_dir2_db_t fdb,
1120 int findex,
1121 struct xfs_buf *fbp,
1122 int longest)
1123{
Dave Chinner20252072012-11-12 22:54:13 +11001124 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001125 __be16 *bests;
1126 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001127 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001128
Dave Chinner01ba43b2013-10-29 22:11:52 +11001129 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001130 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001131 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001132 /*
1133 * Data block is not empty, just set the free entry to the new
1134 * value.
1135 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001136 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001137 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001138 return 0;
1139 }
1140
1141 /* One less used entry in the free table. */
1142 freehdr.nused--;
1143
1144 /*
1145 * If this was the last entry in the table, we can trim the table size
1146 * back. There might be other entries at the end referring to
1147 * non-existent data blocks, get those too.
1148 */
1149 if (findex == freehdr.nvalid - 1) {
1150 int i; /* free entry index */
1151
1152 for (i = findex - 1; i >= 0; i--) {
1153 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1154 break;
1155 }
1156 freehdr.nvalid = i + 1;
1157 logfree = 0;
1158 } else {
1159 /* Not the last entry, just punch it out. */
1160 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001161 logfree = 1;
1162 }
1163
Dave Chinner01ba43b2013-10-29 22:11:52 +11001164 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001165 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001166
1167 /*
1168 * If there are no useful entries left in the block, get rid of the
1169 * block if we can.
1170 */
1171 if (!freehdr.nused) {
1172 int error;
1173
1174 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1175 if (error == 0) {
1176 fbp = NULL;
1177 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001178 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001179 return error;
1180 /*
1181 * It's possible to get ENOSPC if there is no
1182 * space reservation. In this case some one
1183 * else will eventually get rid of this block.
1184 */
1185 }
1186
Dave Chinner20252072012-11-12 22:54:13 +11001187 /* Log the free entry that changed, unless we got rid of it. */
1188 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001189 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001190 return 0;
1191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193/*
1194 * Remove an entry from a node directory.
1195 * This removes the leaf entry and the data entry,
1196 * and updates the free block if necessary.
1197 */
1198static int /* error */
1199xfs_dir2_leafn_remove(
1200 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001201 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int index, /* leaf entry index */
1203 xfs_da_state_blk_t *dblk, /* data block */
1204 int *rval) /* resulting block needs join */
1205{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001206 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001208 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 xfs_dir2_data_entry_t *dep; /* data block entry */
1210 xfs_inode_t *dp; /* incore directory inode */
1211 xfs_dir2_leaf_t *leaf; /* leaf structure */
1212 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1213 int longest; /* longest data free entry */
1214 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 int needlog; /* need to log data header */
1216 int needscan; /* need to rescan data frees */
1217 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001218 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001219 struct xfs_dir3_icleaf_hdr leafhdr;
1220 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001222 trace_xfs_dir2_leafn_remove(args, index);
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 dp = args->dp;
1225 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001226 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001227 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001228 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /*
1231 * Point to the entry we're removing.
1232 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001233 lep = &ents[index];
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /*
1236 * Extract the data block and offset from the entry.
1237 */
Dave Chinner30028032014-06-06 15:08:18 +10001238 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001240 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * Kill the leaf entry by marking it stale.
1245 * Log the leaf block changes.
1246 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001247 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001248 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001249 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001250
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001251 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001252 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 /*
1255 * Make the data entry free. Keep track of the longest freespace
1256 * in the data block in case it changes.
1257 */
1258 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001259 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001260 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001261 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001262 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001264 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001265 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * Rescan the data block freespaces for bestfree.
1268 * Log the data block header if needed.
1269 */
1270 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001271 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001273 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001274 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /*
1276 * If the longest data block freespace changes, need to update
1277 * the corresponding freeblock entry.
1278 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001279 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001281 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 xfs_dir2_db_t fdb; /* freeblock block number */
1283 int findex; /* index in freeblock entries */
1284 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 /*
1287 * Convert the data block number to a free block,
1288 * read in the free block.
1289 */
Dave Chinner8f661932014-06-06 15:15:59 +10001290 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001291 error = xfs_dir2_free_read(tp, dp,
1292 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001293 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001296 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001297#ifdef DEBUG
1298 {
1299 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001300 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001301 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001302 (fdb - xfs_dir2_byte_to_db(args->geo,
1303 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001304 }
1305#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * Calculate which entry we need to fix.
1308 */
Dave Chinner8f661932014-06-06 15:15:59 +10001309 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001310 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 /*
1312 * If the data block is now empty we can get rid of it
1313 * (usually).
1314 */
Dave Chinner8f661932014-06-06 15:15:59 +10001315 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001316 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /*
1318 * Try to punch out the data block.
1319 */
1320 error = xfs_dir2_shrink_inode(args, db, dbp);
1321 if (error == 0) {
1322 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001323 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 /*
1326 * We can get ENOSPC if there's no space reservation.
1327 * In this case just drop the buffer and some one else
1328 * will eventually get rid of the empty block.
1329 */
Dave Chinner24513372014-06-25 14:58:08 +10001330 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return error;
1332 }
1333 /*
1334 * If we got rid of the data block, we can eliminate that entry
1335 * in the free block.
1336 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001337 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001338 fdb, findex, fbp, longest);
1339 if (error)
1340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Dave Chinner20252072012-11-12 22:54:13 +11001342
Dave Chinner41419562013-10-29 22:11:50 +11001343 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001345 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 * to justify trying to join it with a neighbor.
1347 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001348 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001349 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001350 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353
1354/*
1355 * Split the leaf entries in the old block into old and new blocks.
1356 */
1357int /* error */
1358xfs_dir2_leafn_split(
1359 xfs_da_state_t *state, /* btree cursor */
1360 xfs_da_state_blk_t *oldblk, /* original block */
1361 xfs_da_state_blk_t *newblk) /* newly created block */
1362{
1363 xfs_da_args_t *args; /* operation arguments */
1364 xfs_dablk_t blkno; /* new leaf block number */
1365 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001366 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /*
1369 * Allocate space for a new leaf node.
1370 */
1371 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001372 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1374 error = xfs_da_grow_inode(args, &blkno);
1375 if (error) {
1376 return error;
1377 }
1378 /*
1379 * Initialize the new leaf block.
1380 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001381 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001382 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1383 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 newblk->blkno = blkno;
1387 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1388 /*
1389 * Rebalance the entries across the two leaves, link the new
1390 * block into the leaves.
1391 */
1392 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001393 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (error) {
1395 return error;
1396 }
1397 /*
1398 * Insert the new entry in the correct block.
1399 */
1400 if (state->inleaf)
1401 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1402 else
1403 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1404 /*
1405 * Update last hashval in each block since we added the name.
1406 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001407 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1408 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001409 xfs_dir3_leaf_check(dp, oldblk->bp);
1410 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 return error;
1412}
1413
1414/*
1415 * Check a leaf block and its neighbors to see if the block should be
1416 * collapsed into one or the other neighbor. Always keep the block
1417 * with the smaller block number.
1418 * If the current block is over 50% full, don't try to join it, return 0.
1419 * If the block is empty, fill in the state structure and return 2.
1420 * If it can be collapsed, fill in the state structure and return 1.
1421 * If nothing can be done, return 0.
1422 */
1423int /* error */
1424xfs_dir2_leafn_toosmall(
1425 xfs_da_state_t *state, /* btree cursor */
1426 int *action) /* resulting action to take */
1427{
1428 xfs_da_state_blk_t *blk; /* leaf block */
1429 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001430 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 int bytes; /* bytes in use */
1432 int count; /* leaf live entry count */
1433 int error; /* error return value */
1434 int forward; /* sibling block direction */
1435 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 xfs_dir2_leaf_t *leaf; /* leaf structure */
1437 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001438 struct xfs_dir3_icleaf_hdr leafhdr;
1439 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001440 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /*
1443 * Check for the degenerate case of the block being over 50% full.
1444 * If so, it's not worth even looking to see if we might be able
1445 * to coalesce with a sibling.
1446 */
1447 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001448 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001449 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001450 ents = dp->d_ops->leaf_ents_p(leaf);
1451 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001452
1453 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001454 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001455 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * Blk over 50%, don't try to join.
1458 */
1459 *action = 0;
1460 return 0;
1461 }
1462 /*
1463 * Check for the degenerate case of the block being empty.
1464 * If the block is empty, we'll simply delete it, no need to
1465 * coalesce it with a sibling block. We choose (arbitrarily)
1466 * to merge with the forward block unless it is NULL.
1467 */
1468 if (count == 0) {
1469 /*
1470 * Make altpath point to the block we want to keep and
1471 * path point to the block we want to drop (this one).
1472 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001473 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001475 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 &rval);
1477 if (error)
1478 return error;
1479 *action = rval ? 2 : 0;
1480 return 0;
1481 }
1482 /*
1483 * Examine each sibling block to see if we can coalesce with
1484 * at least 25% free space to spare. We need to figure out
1485 * whether to merge with the forward or the backward block.
1486 * We prefer coalescing with the lower numbered sibling so as
1487 * to shrink a directory over time.
1488 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001489 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001491 struct xfs_dir3_icleaf_hdr hdr2;
1492
1493 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (blkno == 0)
1495 continue;
1496 /*
1497 * Read the sibling leaf block.
1498 */
Dave Chinner41419562013-10-29 22:11:50 +11001499 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001500 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001501 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 /*
1505 * Count bytes in the two blocks combined.
1506 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001507 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001508 bytes = state->args->geo->blksize -
1509 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001510
Dave Chinner1d9025e2012-06-22 18:50:14 +10001511 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001512 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001513 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001514 count += hdr2.count - hdr2.stale;
1515 bytes -= count * sizeof(ents[0]);
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
1518 * Fits with at least 25% to spare.
1519 */
1520 if (bytes >= 0)
1521 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001522 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 /*
1525 * Didn't like either block, give up.
1526 */
1527 if (i >= 2) {
1528 *action = 0;
1529 return 0;
1530 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
1533 * Make altpath point to the block we want to keep (the lower
1534 * numbered block) and path point to the block we want to drop.
1535 */
1536 memcpy(&state->altpath, &state->path, sizeof(state->path));
1537 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001538 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 &rval);
1540 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001541 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 &rval);
1543 if (error) {
1544 return error;
1545 }
1546 *action = rval ? 0 : 1;
1547 return 0;
1548}
1549
1550/*
1551 * Move all the leaf entries from drop_blk to save_blk.
1552 * This is done as part of a join operation.
1553 */
1554void
1555xfs_dir2_leafn_unbalance(
1556 xfs_da_state_t *state, /* cursor */
1557 xfs_da_state_blk_t *drop_blk, /* dead block */
1558 xfs_da_state_blk_t *save_blk) /* surviving block */
1559{
1560 xfs_da_args_t *args; /* operation arguments */
1561 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1562 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001563 struct xfs_dir3_icleaf_hdr savehdr;
1564 struct xfs_dir3_icleaf_hdr drophdr;
1565 struct xfs_dir2_leaf_entry *sents;
1566 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001567 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 args = state->args;
1570 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1571 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001572 drop_leaf = drop_blk->bp->b_addr;
1573 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001574
Dave Chinner01ba43b2013-10-29 22:11:52 +11001575 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1576 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1577 sents = dp->d_ops->leaf_ents_p(save_leaf);
1578 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * If there are any stale leaf entries, take this opportunity
1582 * to purge them.
1583 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001584 if (drophdr.stale)
1585 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1586 if (savehdr.stale)
1587 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * Move the entries from drop to the appropriate end of save.
1591 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001592 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001593 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001594 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1595 save_blk->bp, &savehdr, sents, 0,
1596 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001598 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1599 save_blk->bp, &savehdr, sents,
1600 savehdr.count, drophdr.count);
1601 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1602
1603 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001604 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1605 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001606 xfs_dir3_leaf_log_header(args, save_blk->bp);
1607 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001608
Dave Chinner41419562013-10-29 22:11:50 +11001609 xfs_dir3_leaf_check(dp, save_blk->bp);
1610 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
1614 * Top-level node form directory addname routine.
1615 */
1616int /* error */
1617xfs_dir2_node_addname(
1618 xfs_da_args_t *args) /* operation arguments */
1619{
1620 xfs_da_state_blk_t *blk; /* leaf block for insert */
1621 int error; /* error return value */
1622 int rval; /* sub-return value */
1623 xfs_da_state_t *state; /* btree cursor */
1624
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001625 trace_xfs_dir2_node_addname(args);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 /*
1628 * Allocate and initialize the state (btree cursor).
1629 */
1630 state = xfs_da_state_alloc();
1631 state->args = args;
1632 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /*
1634 * Look up the name. We're not supposed to find it, but
1635 * this gives us the insertion point.
1636 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001637 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (error)
1639 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001640 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 goto done;
1642 }
1643 /*
1644 * Add the data entry to a data block.
1645 * Extravalid is set to a freeblock found by lookup.
1646 */
1647 rval = xfs_dir2_node_addname_int(args,
1648 state->extravalid ? &state->extrablk : NULL);
1649 if (rval) {
1650 goto done;
1651 }
1652 blk = &state->path.blk[state->path.active - 1];
1653 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1654 /*
1655 * Add the new leaf entry.
1656 */
1657 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1658 if (rval == 0) {
1659 /*
1660 * It worked, fix the hash values up the btree.
1661 */
Barry Naujok6a178102008-05-21 16:42:05 +10001662 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001663 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 } else {
1665 /*
1666 * It didn't work, we need to split the leaf block.
1667 */
1668 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001669 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto done;
1671 }
1672 /*
1673 * Split the leaf block and insert the new entry.
1674 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001675 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677done:
1678 xfs_da_state_free(state);
1679 return rval;
1680}
1681
1682/*
1683 * Add the data entry for a node-format directory name addition.
1684 * The leaf entry is added in xfs_dir2_leafn_add.
1685 * We may enter with a freespace block that the lookup found.
1686 */
1687static int /* error */
1688xfs_dir2_node_addname_int(
1689 xfs_da_args_t *args, /* operation arguments */
1690 xfs_da_state_blk_t *fblk) /* optional freespace block */
1691{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001692 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001694 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1696 xfs_inode_t *dp; /* incore directory inode */
1697 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1698 int error; /* error return value */
1699 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001700 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 int findex; /* freespace entry index */
1702 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1703 xfs_dir2_db_t ifbno; /* initial freespace block no */
1704 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1705 int length; /* length of the new entry */
1706 int logfree; /* need to log free entry */
1707 xfs_mount_t *mp; /* filesystem mount point */
1708 int needlog; /* need to log data header */
1709 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001710 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001712 __be16 *bests;
1713 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001714 struct xfs_dir2_data_free *bf;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001715 xfs_dir2_data_aoff_t aoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 dp = args->dp;
1718 mp = dp->i_mount;
1719 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001720 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * If we came in with a freespace block that means that lookup
1723 * found an entry with our hash value. This is the freespace
1724 * block for that data entry.
1725 */
1726 if (fblk) {
1727 fbp = fblk->bp;
1728 /*
1729 * Remember initial freespace block number.
1730 */
1731 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001732 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001734 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001735 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /*
1738 * This means the free entry showed that the data block had
1739 * space for our entry, so we remembered it.
1740 * Use that data block.
1741 */
1742 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001743 ASSERT(findex < freehdr.nvalid);
1744 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1745 ASSERT(be16_to_cpu(bests[findex]) >= length);
1746 dbno = freehdr.firstdb + findex;
1747 } else {
1748 /*
1749 * The data block looked at didn't have enough room.
1750 * We'll start at the beginning of the freespace entries.
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 dbno = -1;
1753 findex = 0;
1754 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001755 } else {
1756 /*
1757 * Didn't come in with a freespace block, so no data block.
1758 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ifbno = dbno = -1;
1760 fbp = NULL;
1761 findex = 0;
1762 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * If we don't have a data block yet, we're going to scan the
1766 * freespace blocks looking for one. Figure out what the
1767 * highest freespace block number is.
1768 */
1769 if (dbno == -1) {
1770 xfs_fileoff_t fo; /* freespace block number */
1771
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001772 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001774 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 fbno = ifbno;
1776 }
1777 /*
1778 * While we haven't identified a data block, search the freeblock
1779 * data for a good data block. If we find a null freeblock entry,
1780 * indicating a hole in the data blocks, remember that.
1781 */
1782 while (dbno == -1) {
1783 /*
1784 * If we don't have a freeblock in hand, get the next one.
1785 */
1786 if (fbp == NULL) {
1787 /*
1788 * Happens the first time through unless lookup gave
1789 * us a freespace block to start with.
1790 */
1791 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001792 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001793 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * If it's ifbno we already looked at it.
1796 */
1797 if (fbno == ifbno)
1798 fbno++;
1799 /*
1800 * If it's off the end we're done.
1801 */
1802 if (fbno >= lastfbno)
1803 break;
1804 /*
1805 * Read the block. There can be holes in the
1806 * freespace blocks, so this might not succeed.
1807 * This should be really rare, so there's no reason
1808 * to avoid it.
1809 */
Dave Chinner20252072012-11-12 22:54:13 +11001810 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001811 xfs_dir2_db_to_da(args->geo, fbno),
1812 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001815 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001817 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 findex = 0;
1819 }
1820 /*
1821 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001822 *
1823 * The bests initialisation should be where the bufer is read in
1824 * the above branch. But gcc is too stupid to realise that bests
1825 * and the freehdr are actually initialised if they are placed
1826 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001828 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001829 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001830 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1831 be16_to_cpu(bests[findex]) >= length)
1832 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 else {
1834 /*
1835 * Are we done with the freeblock?
1836 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001837 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /*
1839 * Drop the block.
1840 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001841 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 fbp = NULL;
1843 if (fblk && fblk->bp)
1844 fblk->bp = NULL;
1845 }
1846 }
1847 }
1848 /*
1849 * If we don't have a data block, we need to allocate one and make
1850 * the freespace entries refer to it.
1851 */
1852 if (unlikely(dbno == -1)) {
1853 /*
1854 * Not allowed to allocate, return failure.
1855 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001856 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001857 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /*
1860 * Allocate and initialize the new data block.
1861 */
1862 if (unlikely((error = xfs_dir2_grow_inode(args,
1863 XFS_DIR2_DATA_SPACE,
1864 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001865 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * If (somehow) we have a freespace block, get rid of it.
1870 */
1871 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001872 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (fblk && fblk->bp)
1874 fblk->bp = NULL;
1875
1876 /*
1877 * Get the freespace block corresponding to the data block
1878 * that was just allocated.
1879 */
Dave Chinner8f661932014-06-06 15:15:59 +10001880 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001881 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001882 xfs_dir2_db_to_da(args->geo, fbno),
1883 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001884 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /*
1888 * If there wasn't a freespace block, the read will
1889 * return a NULL fbp. Allocate and initialize a new one.
1890 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001891 if (!fbp) {
1892 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1893 &fbno);
1894 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Dave Chinner8f661932014-06-06 15:15:59 +10001897 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001898 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001899"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001900 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001901 (long long)dp->d_ops->db_to_fdb(
1902 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 (long long)dbno, (long long)fbno,
1904 (unsigned long long)ifbno, lastfbno);
1905 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001906 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08001907 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 fblk,
1909 (unsigned long long)fblk->blkno,
1910 fblk->index,
1911 fblk->magic);
1912 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001913 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1916 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001917 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919
1920 /*
1921 * Get a buffer for the new block.
1922 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001923 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001924 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001926 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001927 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001928 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001931 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001933 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001934 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001935 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001936 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001938 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001939 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001940 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
1943 /*
1944 * Set the freespace block index from the data block number.
1945 */
Dave Chinner8f661932014-06-06 15:15:59 +10001946 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /*
1948 * If it's after the end of the current entries in the
1949 * freespace block, extend that table.
1950 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001951 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001952 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001953 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /*
1955 * Tag new entry so nused will go up.
1956 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001957 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 /*
1960 * If this entry was for an empty data block
1961 * (this should always be true) then update the header.
1962 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001963 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1964 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001965 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001966 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 /*
1969 * Update the real value in the table.
1970 * We haven't allocated the data entry yet so this will
1971 * change again.
1972 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001973 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001974 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001975 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 logfree = 1;
1977 }
1978 /*
1979 * We had a data block so we don't have to make a new one.
1980 */
1981 else {
1982 /*
1983 * If just checking, we succeeded.
1984 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001985 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * Read the data block in.
1990 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001991 error = xfs_dir3_data_read(tp, dp,
1992 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001993 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001994 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001996 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001997 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 logfree = 0;
1999 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002000 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
2002 * Point to the existing unused space.
2003 */
2004 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002005 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 needscan = needlog = 0;
2007 /*
2008 * Mark the first part of the unused space, inuse for us.
2009 */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07002010 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
2011 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
2012 &needlog, &needscan);
2013 if (error) {
2014 xfs_trans_brelse(tp, dbp);
2015 return error;
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /*
2018 * Fill in the new entry and log it.
2019 */
2020 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002021 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 dep->namelen = args->namelen;
2023 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002024 dp->d_ops->data_put_ftype(dep, args->filetype);
2025 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002026 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002027 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /*
2029 * Rescan the block for bestfree if needed.
2030 */
2031 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002032 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
2034 * Log the data block header if needed.
2035 */
2036 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002037 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * If the freespace entry is now wrong, update it.
2040 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002041 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002042 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2043 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 logfree = 1;
2045 }
2046 /*
2047 * Log the freespace entry if needed.
2048 */
2049 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002050 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * Return the data block and offset in args, then drop the data block.
2053 */
2054 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002055 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
2057}
2058
2059/*
2060 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002061 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * The only real output is the inode number of the entry.
2063 */
2064int /* error */
2065xfs_dir2_node_lookup(
2066 xfs_da_args_t *args) /* operation arguments */
2067{
2068 int error; /* error return value */
2069 int i; /* btree level */
2070 int rval; /* operation return value */
2071 xfs_da_state_t *state; /* btree cursor */
2072
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002073 trace_xfs_dir2_node_lookup(args);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Allocate and initialize the btree cursor.
2077 */
2078 state = xfs_da_state_alloc();
2079 state->args = args;
2080 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Fill in the path to the entry in the cursor.
2083 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002084 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (error)
2086 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002087 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2088 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002089 xfs_dir2_data_entry_t *dep;
2090
Dave Chinner1d9025e2012-06-22 18:50:14 +10002091 dep = (xfs_dir2_data_entry_t *)
2092 ((char *)state->extrablk.bp->b_addr +
2093 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002094 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
2097 * Release the btree blocks and leaf block.
2098 */
2099 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002100 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 state->path.blk[i].bp = NULL;
2102 }
2103 /*
2104 * Release the data block if we have it.
2105 */
2106 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002107 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 state->extrablk.bp = NULL;
2109 }
2110 xfs_da_state_free(state);
2111 return rval;
2112}
2113
2114/*
2115 * Remove an entry from a node-format directory.
2116 */
2117int /* error */
2118xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002119 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002121 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int error; /* error return value */
2123 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002124 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002126 trace_xfs_dir2_node_removename(args);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Allocate and initialize the btree cursor.
2130 */
2131 state = xfs_da_state_alloc();
2132 state->args = args;
2133 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002134
2135 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002136 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002137 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002138 goto out_free;
2139
2140 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002141 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002142 error = rval;
2143 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 blk = &state->path.blk[state->path.active - 1];
2147 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2148 ASSERT(state->extravalid);
2149 /*
2150 * Remove the leaf and data entries.
2151 * Extrablk refers to the data block.
2152 */
2153 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2154 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002155 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002156 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /*
2158 * Fix the hash values up the btree.
2159 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002160 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /*
2162 * If we need to join leaf blocks, do it.
2163 */
2164 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002165 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
2167 * If no errors so far, try conversion to leaf format.
2168 */
2169 if (!error)
2170 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002171out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 xfs_da_state_free(state);
2173 return error;
2174}
2175
2176/*
2177 * Replace an entry's inode number in a node-format directory.
2178 */
2179int /* error */
2180xfs_dir2_node_replace(
2181 xfs_da_args_t *args) /* operation arguments */
2182{
2183 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002184 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 xfs_dir2_data_entry_t *dep; /* data entry changed */
2186 int error; /* error return value */
2187 int i; /* btree level */
2188 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002189 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 xfs_dir2_leaf_t *leaf; /* leaf structure */
2191 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2192 int rval; /* internal return value */
2193 xfs_da_state_t *state; /* btree cursor */
2194
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002195 trace_xfs_dir2_node_replace(args);
2196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
2198 * Allocate and initialize the btree cursor.
2199 */
2200 state = xfs_da_state_alloc();
2201 state->args = args;
2202 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002203
2204 /*
2205 * We have to save new inode number and ftype since
2206 * xfs_da3_node_lookup_int() is going to overwrite them
2207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002209 ftype = args->filetype;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /*
2212 * Lookup the entry to change in the btree.
2213 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002214 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (error) {
2216 rval = error;
2217 }
2218 /*
2219 * It should be found, since the vnodeops layer has looked it up
2220 * and locked it. But paranoia is good.
2221 */
Dave Chinner24513372014-06-25 14:58:08 +10002222 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002223 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /*
2225 * Find the leaf entry.
2226 */
2227 blk = &state->path.blk[state->path.active - 1];
2228 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002229 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002230 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002231 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 ASSERT(state->extravalid);
2233 /*
2234 * Point to the data entry.
2235 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002236 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002237 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2238 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002240 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002241 xfs_dir2_dataptr_to_off(args->geo,
2242 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002243 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /*
2245 * Fill in the new inode number and log the entry.
2246 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002247 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002248 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002249 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 rval = 0;
2251 }
2252 /*
2253 * Didn't find it, and we're holding a data block. Drop it.
2254 */
2255 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002256 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 state->extrablk.bp = NULL;
2258 }
2259 /*
2260 * Release all the buffers in the cursor.
2261 */
2262 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002263 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 state->path.blk[i].bp = NULL;
2265 }
2266 xfs_da_state_free(state);
2267 return rval;
2268}
2269
2270/*
2271 * Trim off a trailing empty freespace block.
2272 * Return (in rvalp) 1 if we did it, 0 if not.
2273 */
2274int /* error */
2275xfs_dir2_node_trim_free(
2276 xfs_da_args_t *args, /* operation arguments */
2277 xfs_fileoff_t fo, /* free block number */
2278 int *rvalp) /* out: did something */
2279{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002280 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 xfs_inode_t *dp; /* incore directory inode */
2282 int error; /* error return code */
2283 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002285 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002289
2290 *rvalp = 0;
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /*
2293 * Read the freespace block.
2294 */
Dave Chinner20252072012-11-12 22:54:13 +11002295 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * There can be holes in freespace. If fo is a hole, there's
2300 * nothing to do.
2301 */
Dave Chinner20252072012-11-12 22:54:13 +11002302 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002304 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002305 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /*
2308 * If there are used entries, there's nothing to do.
2309 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002310 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002311 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 0;
2313 }
2314 /*
2315 * Blow the block away.
2316 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002317 error = xfs_dir2_shrink_inode(args,
2318 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2319 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /*
2321 * Can't fail with ENOSPC since that only happens with no
2322 * space reservation, when breaking up an extent into two
2323 * pieces. This is the last block of an extent.
2324 */
Dave Chinner24513372014-06-25 14:58:08 +10002325 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002326 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return error;
2328 }
2329 /*
2330 * Return that we succeeded.
2331 */
2332 *rvalp = 1;
2333 return 0;
2334}