blob: a5e7d9bd7552b5b49fbbc06b769bd807e2403e68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
28#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000032#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110034#include "xfs_buf_item.h"
35#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110036#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Function declarations.
40 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100041static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 xfs_da_state_blk_t *blk1,
45 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int index, xfs_da_state_blk_t *dblk,
48 int *rval);
49static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 xfs_da_state_blk_t *fblk);
51
Dave Chinner24df33b2013-04-12 07:30:21 +100052/*
53 * Check internal consistency of a leafn block.
54 */
55#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080056static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100057xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110058 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100059 struct xfs_buf *bp)
60{
61 struct xfs_dir2_leaf *leaf = bp->b_addr;
62 struct xfs_dir3_icleaf_hdr leafhdr;
63
Dave Chinner01ba43b2013-10-29 22:11:52 +110064 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100065
66 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
67 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
68 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080069 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100070 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080071 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100072
Dave Chinner41419562013-10-29 22:11:50 +110073 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100074}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080075
76static inline void
77xfs_dir3_leaf_check(
78 struct xfs_inode *dp,
79 struct xfs_buf *bp)
80{
81 xfs_failaddr_t fa;
82
83 fa = xfs_dir3_leafn_check(dp, bp);
84 if (!fa)
85 return;
86 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070087 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
88 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080089 ASSERT(0);
90}
Dave Chinner24df33b2013-04-12 07:30:21 +100091#else
Dave Chinner41419562013-10-29 22:11:50 +110092#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100093#endif
94
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080095static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110096xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110097 struct xfs_buf *bp)
98{
99 struct xfs_mount *mp = bp->b_target->bt_mount;
100 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +1100101
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100102 if (xfs_sb_version_hascrc(&mp->m_sb)) {
103 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
104
105 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800106 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000107 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800108 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100109 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800110 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100111 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800112 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100113 } else {
114 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800115 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100116 }
117
118 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
119
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800120 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100121}
122
123static void
124xfs_dir3_free_read_verify(
125 struct xfs_buf *bp)
126{
127 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800128 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100129
Eric Sandeence5028c2014-02-27 15:23:10 +1100130 if (xfs_sb_version_hascrc(&mp->m_sb) &&
131 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800132 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
133 else {
134 fa = xfs_dir3_free_verify(bp);
135 if (fa)
136 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
137 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100138}
Dave Chinner20252072012-11-12 22:54:13 +1100139
Dave Chinner612cfbf2012-11-14 17:52:32 +1100140static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100141xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100142 struct xfs_buf *bp)
143{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100144 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800145 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100146 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800147 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100148
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800149 fa = xfs_dir3_free_verify(bp);
150 if (fa) {
151 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100152 return;
153 }
154
155 if (!xfs_sb_version_hascrc(&mp->m_sb))
156 return;
157
158 if (bip)
159 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
160
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100161 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100162}
163
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100164const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100165 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100166 .verify_read = xfs_dir3_free_read_verify,
167 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800168 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100169};
Dave Chinner20252072012-11-12 22:54:13 +1100170
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800171/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800172static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800173xfs_dir3_free_header_check(
174 struct xfs_inode *dp,
175 xfs_dablk_t fbno,
176 struct xfs_buf *bp)
177{
178 struct xfs_mount *mp = dp->i_mount;
179 unsigned int firstdb;
180 int maxbests;
181
182 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
183 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
184 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
185 maxbests;
186 if (xfs_sb_version_hascrc(&mp->m_sb)) {
187 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
188
189 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800190 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800191 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800192 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800193 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800194 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800195 } else {
196 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
197
198 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800199 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800200 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800201 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800202 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800203 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800204 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800205 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800206}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100207
Dave Chinner20252072012-11-12 22:54:13 +1100208static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100209__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100210 struct xfs_trans *tp,
211 struct xfs_inode *dp,
212 xfs_dablk_t fbno,
213 xfs_daddr_t mappedbno,
214 struct xfs_buf **bpp)
215{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800216 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100217 int err;
218
219 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100220 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800221 if (err || !*bpp)
222 return err;
223
224 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800225 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
226 if (fa) {
227 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800228 xfs_trans_brelse(tp, *bpp);
229 return -EFSCORRUPTED;
230 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100231
232 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800233 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100234 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800235
236 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100237}
238
239int
240xfs_dir2_free_read(
241 struct xfs_trans *tp,
242 struct xfs_inode *dp,
243 xfs_dablk_t fbno,
244 struct xfs_buf **bpp)
245{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100246 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100247}
248
249static int
250xfs_dir2_free_try_read(
251 struct xfs_trans *tp,
252 struct xfs_inode *dp,
253 xfs_dablk_t fbno,
254 struct xfs_buf **bpp)
255{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100256 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
257}
258
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100259static int
260xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000261 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100262 xfs_dir2_db_t fbno,
263 struct xfs_buf **bpp)
264{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000265 struct xfs_trans *tp = args->trans;
266 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100267 struct xfs_mount *mp = dp->i_mount;
268 struct xfs_buf *bp;
269 int error;
270 struct xfs_dir3_icfree_hdr hdr;
271
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000272 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100273 -1, &bp, XFS_DATA_FORK);
274 if (error)
275 return error;
276
Dave Chinner61fe1352013-04-03 16:11:30 +1100277 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100278 bp->b_ops = &xfs_dir3_free_buf_ops;
279
280 /*
281 * Initialize the new block to be empty, and remember
282 * its first slot as our empty slot.
283 */
Dave Chinnere400d272013-05-28 18:37:17 +1000284 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
285 memset(&hdr, 0, sizeof(hdr));
286
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100287 if (xfs_sb_version_hascrc(&mp->m_sb)) {
288 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
289
290 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000291
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100292 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
293 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000294 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000295 } else
296 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100297 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100298 *bpp = bp;
299 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * Log entries from a freespace block.
304 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000305STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000307 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000308 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int first, /* first entry to log */
310 int last) /* last entry to log */
311{
312 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100313 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dave Chinner1d9025e2012-06-22 18:50:14 +1000315 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000316 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100317 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
318 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000319 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100320 (uint)((char *)&bests[first] - (char *)free),
321 (uint)((char *)&bests[last] - (char *)free +
322 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * Log header from a freespace block.
327 */
328static void
329xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000330 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000331 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Dave Chinner836a94a2013-08-12 20:49:44 +1000333#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_dir2_free_t *free; /* freespace structure */
335
Dave Chinner1d9025e2012-06-22 18:50:14 +1000336 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100337 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
338 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000339#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000340 xfs_trans_log_buf(args->trans, bp, 0,
341 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
344/*
345 * Convert a leaf-format directory to a node-format directory.
346 * We need to change the magic number of the leaf block, and copy
347 * the freespace table out of the leaf block into its own block.
348 */
349int /* error */
350xfs_dir2_leaf_to_node(
351 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000352 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 xfs_inode_t *dp; /* incore directory inode */
355 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000356 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 xfs_dir2_db_t fdb; /* freespace block number */
358 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100359 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int i; /* leaf freespace index */
361 xfs_dir2_leaf_t *leaf; /* leaf structure */
362 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int n; /* count of live freespc ents */
364 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100365 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100367 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000369 trace_xfs_dir2_leaf_to_node(args);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 tp = args->trans;
373 /*
374 * Add a freespace block to the directory.
375 */
376 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
377 return error;
378 }
Dave Chinner30028032014-06-06 15:08:18 +1000379 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * Get the buffer for the new freespace block.
382 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000383 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100384 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100386
Dave Chinner1d9025e2012-06-22 18:50:14 +1000387 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100388 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000389 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000390 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800391 if (be32_to_cpu(ltp->bestcount) >
392 (uint)dp->i_d.di_size / args->geo->blksize)
393 return -EFSCORRUPTED;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
396 * Copy freespace entries from the leaf block to the new block.
397 * Count active entries.
398 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100399 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500400 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100401 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100402 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100404 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100408 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100410 freehdr.nused = n;
411 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
412
Dave Chinner01ba43b2013-10-29 22:11:52 +1100413 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000414 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
415 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100416
Dave Chinner24df33b2013-04-12 07:30:21 +1000417 /*
418 * Converting the leaf to a leafnode is just a matter of changing the
419 * magic number and the ops. Do the change directly to the buffer as
420 * it's less work (and less code) than decoding the header to host
421 * format and back again.
422 */
423 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
424 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
425 else
426 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
427 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100428 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000429 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100430 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return 0;
432}
433
434/*
435 * Add a leaf entry to a leaf block in a node-form directory.
436 * The other work necessary is done from the caller.
437 */
438static int /* error */
439xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000440 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 xfs_da_args_t *args, /* operation arguments */
442 int index) /* insertion pt for new entry */
443{
444 int compact; /* compacting stale leaves */
445 xfs_inode_t *dp; /* incore directory inode */
446 int highstale; /* next stale entry */
447 xfs_dir2_leaf_t *leaf; /* leaf structure */
448 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
449 int lfloghigh; /* high leaf entry logging */
450 int lfloglow; /* low leaf entry logging */
451 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000452 struct xfs_dir3_icleaf_hdr leafhdr;
453 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000455 trace_xfs_dir2_leafn_add(args, index);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000458 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100459 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100460 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * Quick check just to make sure we are not going to index
464 * into other peoples memory
465 */
466 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000467 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 /*
470 * If there are already the maximum number of leaf entries in
471 * the block, if there are no stale entries it won't fit.
472 * Caller will do a split. If there are stale entries we'll do
473 * a compact.
474 */
475
Dave Chinner8f661932014-06-06 15:15:59 +1000476 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000477 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000478 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000479 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 } else
481 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000482 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
483 ASSERT(index == leafhdr.count ||
484 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Barry Naujok6a178102008-05-21 16:42:05 +1000486 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 0;
488
489 /*
490 * Compact out all but one stale leaf entry. Leaves behind
491 * the entry closest to index.
492 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 if (compact)
494 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
495 &highstale, &lfloglow, &lfloghigh);
496 else if (leafhdr.stale) {
497 /*
498 * Set impossible logging indices for this case.
499 */
500 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 lfloghigh = -1;
502 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /*
505 * Insert the new entry, log everything.
506 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000507 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200508 highstale, &lfloglow, &lfloghigh);
509
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100510 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000511 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100512 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000513
Dave Chinner01ba43b2013-10-29 22:11:52 +1100514 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000515 xfs_dir3_leaf_log_header(args, bp);
516 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100517 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
519}
520
521#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100522static void
523xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100524 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100525 struct xfs_buf *bp,
526 xfs_dir2_db_t db)
527{
528 struct xfs_dir3_icfree_hdr hdr;
529
Dave Chinner01ba43b2013-10-29 22:11:52 +1100530 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100531
Dave Chinner8f661932014-06-06 15:15:59 +1000532 ASSERT((hdr.firstdb %
533 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100534 ASSERT(hdr.firstdb <= db);
535 ASSERT(db < hdr.firstdb + hdr.nvalid);
536}
537#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100538#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif /* DEBUG */
540
541/*
542 * Return the last hash value in the leaf.
543 * Stale entries are ok.
544 */
545xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700546xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100547 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000548 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int *count) /* count of entries in leaf */
550{
Dave Chinner24df33b2013-04-12 07:30:21 +1000551 struct xfs_dir2_leaf *leaf = bp->b_addr;
552 struct xfs_dir2_leaf_entry *ents;
553 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dave Chinner01ba43b2013-10-29 22:11:52 +1100555 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000556
557 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700558 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
559 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
560 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000563 *count = leafhdr.count;
564 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000566
Dave Chinner41419562013-10-29 22:11:50 +1100567 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000568 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
571/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000572 * Look up a leaf entry for space to add a name in a node-format leaf block.
573 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000575STATIC int
576xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000577 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 xfs_da_args_t *args, /* operation arguments */
579 int *indexp, /* out: leaf entry index */
580 xfs_da_state_t *state) /* state to fill in */
581{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000582 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000583 xfs_dir2_db_t curdb = -1; /* current data block number */
584 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 xfs_inode_t *dp; /* incore directory inode */
586 int error; /* error return value */
587 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000588 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 int index; /* leaf entry index */
590 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000591 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
593 xfs_mount_t *mp; /* filesystem mount point */
594 xfs_dir2_db_t newdb; /* new data block number */
595 xfs_dir2_db_t newfdb; /* new free block number */
596 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000597 struct xfs_dir2_leaf_entry *ents;
598 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 dp = args->dp;
601 tp = args->trans;
602 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000603 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100604 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100605 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000606
Dave Chinner41419562013-10-29 22:11:50 +1100607 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000608 ASSERT(leafhdr.count > 0);
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Look up the hash value in the leaf entries.
612 */
613 index = xfs_dir2_leaf_search_hash(args, bp);
614 /*
615 * Do we have a buffer coming in?
616 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000617 if (state->extravalid) {
618 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000620 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000621 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100622 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
623 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100625 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 /*
627 * Loop over leaf entries with the right hash value.
628 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000629 for (lep = &ents[index];
630 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
631 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * Skip stale leaf entries.
634 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100635 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 continue;
637 /*
638 * Pull the data block number from the entry.
639 */
Dave Chinner30028032014-06-06 15:08:18 +1000640 newdb = xfs_dir2_dataptr_to_db(args->geo,
641 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /*
643 * For addname, we're looking for a place to put the new entry.
644 * We want to use a data block with an entry of equal
645 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 *
647 * If this block isn't the data block we already have
648 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100651 __be16 *bests;
652
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000653 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000655 * Convert the data block to the free block
656 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Dave Chinner8f661932014-06-06 15:15:59 +1000658 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
666 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000667 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100668
669 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000670 xfs_dir2_db_to_da(args->geo,
671 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100672 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000673 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000675 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100676
Dave Chinner01ba43b2013-10-29 22:11:52 +1100677 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000680 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 */
Dave Chinner8f661932014-06-06 15:15:59 +1000682 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000684 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500686 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100687 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000688 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
689 XFS_ERRLEVEL_LOW, mp);
690 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000691 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000692 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000694 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100695 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000699 /* Didn't find any space */
700 fi = -1;
701out:
Barry Naujok6a178102008-05-21 16:42:05 +1000702 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000703 if (curbp) {
704 /* Giving back a free block. */
705 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000707 state->extrablk.index = fi;
708 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100709
710 /*
711 * Important: this magic number is not in the buffer - it's for
712 * buffer type information and therefore only the free/data type
713 * matters here, not whether CRCs are enabled or not.
714 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000715 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
716 } else {
717 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000720 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
722 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000723 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000727 * Look up a leaf entry in a node-format leaf block.
728 * The extrablk in state a data block.
729 */
730STATIC int
731xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000732 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000733 xfs_da_args_t *args, /* operation arguments */
734 int *indexp, /* out: leaf entry index */
735 xfs_da_state_t *state) /* state to fill in */
736{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000737 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000738 xfs_dir2_db_t curdb = -1; /* current data block number */
739 xfs_dir2_data_entry_t *dep; /* data block entry */
740 xfs_inode_t *dp; /* incore directory inode */
741 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000742 int index; /* leaf entry index */
743 xfs_dir2_leaf_t *leaf; /* leaf structure */
744 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
745 xfs_mount_t *mp; /* filesystem mount point */
746 xfs_dir2_db_t newdb; /* new data block number */
747 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000748 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000749 struct xfs_dir2_leaf_entry *ents;
750 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000751
752 dp = args->dp;
753 tp = args->trans;
754 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000755 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100756 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100757 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000758
Dave Chinner41419562013-10-29 22:11:50 +1100759 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000760 ASSERT(leafhdr.count > 0);
761
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000762 /*
763 * Look up the hash value in the leaf entries.
764 */
765 index = xfs_dir2_leaf_search_hash(args, bp);
766 /*
767 * Do we have a buffer coming in?
768 */
769 if (state->extravalid) {
770 curbp = state->extrablk.bp;
771 curdb = state->extrablk.blkno;
772 }
773 /*
774 * Loop over leaf entries with the right hash value.
775 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000776 for (lep = &ents[index];
777 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
778 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000779 /*
780 * Skip stale leaf entries.
781 */
782 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
783 continue;
784 /*
785 * Pull the data block number from the entry.
786 */
Dave Chinner30028032014-06-06 15:08:18 +1000787 newdb = xfs_dir2_dataptr_to_db(args->geo,
788 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000789 /*
790 * Not adding a new entry, so we really want to find
791 * the name given to us.
792 *
793 * If it's a different data block, go get it.
794 */
795 if (newdb != curdb) {
796 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000797 * If we had a block before that we aren't saving
798 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000799 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000800 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
801 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000802 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000803 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000804 * If needing the block that is saved with a CI match,
805 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000806 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000807 if (args->cmpresult != XFS_CMP_DIFFERENT &&
808 newdb == state->extrablk.blkno) {
809 ASSERT(state->extravalid);
810 curbp = state->extrablk.bp;
811 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100812 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000813 xfs_dir2_db_to_da(args->geo,
814 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100815 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000816 if (error)
817 return error;
818 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100819 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000820 curdb = newdb;
821 }
822 /*
823 * Point to the data entry.
824 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000825 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000826 xfs_dir2_dataptr_to_off(args->geo,
827 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000828 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000829 * Compare the entry and if it's an exact match, return
830 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000831 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000832 */
Barry Naujok5163f952008-05-21 16:41:01 +1000833 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
834 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000835 /* If there is a CI match block, drop it */
836 if (args->cmpresult != XFS_CMP_DIFFERENT &&
837 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000838 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000839 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000840 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100841 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000842 *indexp = index;
843 state->extravalid = 1;
844 state->extrablk.bp = curbp;
845 state->extrablk.blkno = curdb;
846 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000848 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 Naujok5163f952008-05-21 16:41:01 +1000851 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000852 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000853 }
854 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000855 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000856 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000857 if (args->cmpresult == XFS_CMP_DIFFERENT) {
858 /* Giving back last used data block. */
859 state->extravalid = 1;
860 state->extrablk.bp = curbp;
861 state->extrablk.index = -1;
862 state->extrablk.blkno = curdb;
863 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100864 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100865 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000866 } else {
867 /* If the curbp is not the CI match block, drop it */
868 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000869 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000870 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000871 } else {
872 state->extravalid = 0;
873 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000874 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000875 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000876}
877
878/*
879 * Look up a leaf entry in a node-format leaf block.
880 * If this is an addname then the extrablk in state is a freespace block,
881 * otherwise it's a data block.
882 */
883int
884xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000885 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000886 xfs_da_args_t *args, /* operation arguments */
887 int *indexp, /* out: leaf entry index */
888 xfs_da_state_t *state) /* state to fill in */
889{
Barry Naujok6a178102008-05-21 16:42:05 +1000890 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000891 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
892 state);
893 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
894}
895
896/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 * Move count leaf entries from source to destination leaf.
898 * Log entries and headers. Stale entries are preserved.
899 */
900static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000901xfs_dir3_leafn_moveents(
902 xfs_da_args_t *args, /* operation arguments */
903 struct xfs_buf *bp_s, /* source */
904 struct xfs_dir3_icleaf_hdr *shdr,
905 struct xfs_dir2_leaf_entry *sents,
906 int start_s,/* source leaf index */
907 struct xfs_buf *bp_d, /* destination */
908 struct xfs_dir3_icleaf_hdr *dhdr,
909 struct xfs_dir2_leaf_entry *dents,
910 int start_d,/* destination leaf index */
911 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Dave Chinner24df33b2013-04-12 07:30:21 +1000913 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000915 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /*
918 * Silently return if nothing to do.
919 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /*
924 * If the destination index is not the end of the current
925 * destination leaf entries, open up a hole in the destination
926 * to hold the new entries.
927 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000928 if (start_d < dhdr->count) {
929 memmove(&dents[start_d + count], &dents[start_d],
930 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000931 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000932 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934 /*
935 * If the source has stale leaves, count the ones in the copy range
936 * so we can update the header correctly.
937 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000938 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int i; /* temp leaf index */
940
941 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000942 if (sents[i].address ==
943 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 stale++;
945 }
946 } else
947 stale = 0;
948 /*
949 * Copy the leaf entries from source to destination.
950 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000951 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000953 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 /*
956 * If there are source entries after the ones we copied,
957 * delete the ones we copied by sliding the next ones down.
958 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000959 if (start_s + count < shdr->count) {
960 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000962 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
966 * Update the headers and log them.
967 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000968 shdr->count -= count;
969 shdr->stale -= stale;
970 dhdr->count += count;
971 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Determine the sort order of two leaf blocks.
976 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
977 */
978int /* sort order */
979xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100980 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000981 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
982 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Dave Chinner24df33b2013-04-12 07:30:21 +1000984 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
985 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
986 struct xfs_dir2_leaf_entry *ents1;
987 struct xfs_dir2_leaf_entry *ents2;
988 struct xfs_dir3_icleaf_hdr hdr1;
989 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Chinner01ba43b2013-10-29 22:11:52 +1100991 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
992 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100993 ents1 = dp->d_ops->leaf_ents_p(leaf1);
994 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000995
996 if (hdr1.count > 0 && hdr2.count > 0 &&
997 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
998 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
999 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 1;
1001 return 0;
1002}
1003
1004/*
1005 * Rebalance leaf entries between two leaf blocks.
1006 * This is actually only called when the second block is new,
1007 * though the code deals with the general case.
1008 * A new entry will be inserted in one of the blocks, and that
1009 * entry is taken into account when balancing.
1010 */
1011static void
1012xfs_dir2_leafn_rebalance(
1013 xfs_da_state_t *state, /* btree cursor */
1014 xfs_da_state_blk_t *blk1, /* first btree block */
1015 xfs_da_state_blk_t *blk2) /* second btree block */
1016{
1017 xfs_da_args_t *args; /* operation arguments */
1018 int count; /* count (& direction) leaves */
1019 int isleft; /* new goes in left leaf */
1020 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1021 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1022 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001023#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int oldstale; /* old count of stale leaves */
1025#endif
1026 int oldsum; /* old total leaf count */
1027 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 struct xfs_dir2_leaf_entry *ents1;
1029 struct xfs_dir2_leaf_entry *ents2;
1030 struct xfs_dir3_icleaf_hdr hdr1;
1031 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001032 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 args = state->args;
1035 /*
1036 * If the block order is wrong, swap the arguments.
1037 */
Dave Chinner41419562013-10-29 22:11:50 +11001038 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 xfs_da_state_blk_t *tmp; /* temp for block swap */
1040
1041 tmp = blk1;
1042 blk1 = blk2;
1043 blk2 = tmp;
1044 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001045 leaf1 = blk1->bp->b_addr;
1046 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001047 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1048 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001049 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1050 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001051
1052 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001053#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001054 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055#endif
1056 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * If the old leaf count was odd then the new one will be even,
1060 * so we need to divide the new count evenly.
1061 */
1062 if (oldsum & 1) {
1063 xfs_dahash_t midhash; /* middle entry hash value */
1064
Dave Chinner24df33b2013-04-12 07:30:21 +10001065 if (mid >= hdr1.count)
1066 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001068 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 isleft = args->hashval <= midhash;
1070 }
1071 /*
1072 * If the old count is even then the new count is odd, so there's
1073 * no preferred side for the new entry.
1074 * Pick the left one.
1075 */
1076 else
1077 isleft = 1;
1078 /*
1079 * Calculate moved entry count. Positive means left-to-right,
1080 * negative means right-to-left. Then move the entries.
1081 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001082 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001084 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1085 hdr1.count - count, blk2->bp,
1086 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001088 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1089 blk1->bp, &hdr1, ents1,
1090 hdr1.count, count);
1091
1092 ASSERT(hdr1.count + hdr2.count == oldsum);
1093 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1094
1095 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001096 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1097 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001098 xfs_dir3_leaf_log_header(args, blk1->bp);
1099 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001100
Dave Chinner41419562013-10-29 22:11:50 +11001101 xfs_dir3_leaf_check(dp, blk1->bp);
1102 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * Mark whether we're inserting into the old or new leaf.
1106 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001107 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 state->inleaf = !swap;
1111 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001112 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /*
1114 * Adjust the expected index for insertion.
1115 */
1116 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001117 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001118
1119 /*
1120 * Finally sanity check just to make sure we are not returning a
1121 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Dave Chinner41419562013-10-29 22:11:50 +11001123 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 state->inleaf = 1;
1125 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001126 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001127 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001128 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130}
1131
Dave Chinner20252072012-11-12 22:54:13 +11001132static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001133xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001134 xfs_da_args_t *args,
1135 struct xfs_dir2_data_hdr *hdr,
1136 struct xfs_dir2_free *free,
1137 xfs_dir2_db_t fdb,
1138 int findex,
1139 struct xfs_buf *fbp,
1140 int longest)
1141{
Dave Chinner20252072012-11-12 22:54:13 +11001142 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001143 __be16 *bests;
1144 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001145 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001146
Dave Chinner01ba43b2013-10-29 22:11:52 +11001147 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001148 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001149 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001150 /*
1151 * Data block is not empty, just set the free entry to the new
1152 * value.
1153 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001154 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001155 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001156 return 0;
1157 }
1158
1159 /* One less used entry in the free table. */
1160 freehdr.nused--;
1161
1162 /*
1163 * If this was the last entry in the table, we can trim the table size
1164 * back. There might be other entries at the end referring to
1165 * non-existent data blocks, get those too.
1166 */
1167 if (findex == freehdr.nvalid - 1) {
1168 int i; /* free entry index */
1169
1170 for (i = findex - 1; i >= 0; i--) {
1171 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1172 break;
1173 }
1174 freehdr.nvalid = i + 1;
1175 logfree = 0;
1176 } else {
1177 /* Not the last entry, just punch it out. */
1178 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001179 logfree = 1;
1180 }
1181
Dave Chinner01ba43b2013-10-29 22:11:52 +11001182 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001183 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001184
1185 /*
1186 * If there are no useful entries left in the block, get rid of the
1187 * block if we can.
1188 */
1189 if (!freehdr.nused) {
1190 int error;
1191
1192 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1193 if (error == 0) {
1194 fbp = NULL;
1195 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001196 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001197 return error;
1198 /*
1199 * It's possible to get ENOSPC if there is no
1200 * space reservation. In this case some one
1201 * else will eventually get rid of this block.
1202 */
1203 }
1204
Dave Chinner20252072012-11-12 22:54:13 +11001205 /* Log the free entry that changed, unless we got rid of it. */
1206 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001207 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001208 return 0;
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/*
1212 * Remove an entry from a node directory.
1213 * This removes the leaf entry and the data entry,
1214 * and updates the free block if necessary.
1215 */
1216static int /* error */
1217xfs_dir2_leafn_remove(
1218 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001219 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int index, /* leaf entry index */
1221 xfs_da_state_blk_t *dblk, /* data block */
1222 int *rval) /* resulting block needs join */
1223{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001224 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001226 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 xfs_dir2_data_entry_t *dep; /* data block entry */
1228 xfs_inode_t *dp; /* incore directory inode */
1229 xfs_dir2_leaf_t *leaf; /* leaf structure */
1230 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1231 int longest; /* longest data free entry */
1232 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int needlog; /* need to log data header */
1234 int needscan; /* need to rescan data frees */
1235 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001236 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001237 struct xfs_dir3_icleaf_hdr leafhdr;
1238 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001240 trace_xfs_dir2_leafn_remove(args, index);
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 dp = args->dp;
1243 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001244 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001245 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001246 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Point to the entry we're removing.
1250 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001251 lep = &ents[index];
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /*
1254 * Extract the data block and offset from the entry.
1255 */
Dave Chinner30028032014-06-06 15:08:18 +10001256 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001258 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Kill the leaf entry by marking it stale.
1263 * Log the leaf block changes.
1264 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001265 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001266 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001267 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001268
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001269 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001270 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * Make the data entry free. Keep track of the longest freespace
1274 * in the data block in case it changes.
1275 */
1276 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001277 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001278 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001279 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001280 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001282 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001283 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /*
1285 * Rescan the data block freespaces for bestfree.
1286 * Log the data block header if needed.
1287 */
1288 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001289 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001291 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001292 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
1294 * If the longest data block freespace changes, need to update
1295 * the corresponding freeblock entry.
1296 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001297 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 xfs_dir2_db_t fdb; /* freeblock block number */
1301 int findex; /* index in freeblock entries */
1302 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /*
1305 * Convert the data block number to a free block,
1306 * read in the free block.
1307 */
Dave Chinner8f661932014-06-06 15:15:59 +10001308 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001309 error = xfs_dir2_free_read(tp, dp,
1310 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001311 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001312 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001314 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001315#ifdef DEBUG
1316 {
1317 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001318 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001319 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001320 (fdb - xfs_dir2_byte_to_db(args->geo,
1321 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001322 }
1323#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * Calculate which entry we need to fix.
1326 */
Dave Chinner8f661932014-06-06 15:15:59 +10001327 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001328 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 /*
1330 * If the data block is now empty we can get rid of it
1331 * (usually).
1332 */
Dave Chinner8f661932014-06-06 15:15:59 +10001333 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001334 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /*
1336 * Try to punch out the data block.
1337 */
1338 error = xfs_dir2_shrink_inode(args, db, dbp);
1339 if (error == 0) {
1340 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001341 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 /*
1344 * We can get ENOSPC if there's no space reservation.
1345 * In this case just drop the buffer and some one else
1346 * will eventually get rid of the empty block.
1347 */
Dave Chinner24513372014-06-25 14:58:08 +10001348 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return error;
1350 }
1351 /*
1352 * If we got rid of the data block, we can eliminate that entry
1353 * in the free block.
1354 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001355 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001356 fdb, findex, fbp, longest);
1357 if (error)
1358 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
Dave Chinner20252072012-11-12 22:54:13 +11001360
Dave Chinner41419562013-10-29 22:11:50 +11001361 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001363 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * to justify trying to join it with a neighbor.
1365 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001366 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001367 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001368 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370}
1371
1372/*
1373 * Split the leaf entries in the old block into old and new blocks.
1374 */
1375int /* error */
1376xfs_dir2_leafn_split(
1377 xfs_da_state_t *state, /* btree cursor */
1378 xfs_da_state_blk_t *oldblk, /* original block */
1379 xfs_da_state_blk_t *newblk) /* newly created block */
1380{
1381 xfs_da_args_t *args; /* operation arguments */
1382 xfs_dablk_t blkno; /* new leaf block number */
1383 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001384 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /*
1387 * Allocate space for a new leaf node.
1388 */
1389 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001390 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1392 error = xfs_da_grow_inode(args, &blkno);
1393 if (error) {
1394 return error;
1395 }
1396 /*
1397 * Initialize the new leaf block.
1398 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001399 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001400 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1401 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 newblk->blkno = blkno;
1405 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1406 /*
1407 * Rebalance the entries across the two leaves, link the new
1408 * block into the leaves.
1409 */
1410 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001411 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (error) {
1413 return error;
1414 }
1415 /*
1416 * Insert the new entry in the correct block.
1417 */
1418 if (state->inleaf)
1419 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1420 else
1421 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1422 /*
1423 * Update last hashval in each block since we added the name.
1424 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001425 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1426 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001427 xfs_dir3_leaf_check(dp, oldblk->bp);
1428 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return error;
1430}
1431
1432/*
1433 * Check a leaf block and its neighbors to see if the block should be
1434 * collapsed into one or the other neighbor. Always keep the block
1435 * with the smaller block number.
1436 * If the current block is over 50% full, don't try to join it, return 0.
1437 * If the block is empty, fill in the state structure and return 2.
1438 * If it can be collapsed, fill in the state structure and return 1.
1439 * If nothing can be done, return 0.
1440 */
1441int /* error */
1442xfs_dir2_leafn_toosmall(
1443 xfs_da_state_t *state, /* btree cursor */
1444 int *action) /* resulting action to take */
1445{
1446 xfs_da_state_blk_t *blk; /* leaf block */
1447 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001448 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int bytes; /* bytes in use */
1450 int count; /* leaf live entry count */
1451 int error; /* error return value */
1452 int forward; /* sibling block direction */
1453 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfs_dir2_leaf_t *leaf; /* leaf structure */
1455 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001456 struct xfs_dir3_icleaf_hdr leafhdr;
1457 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001458 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * Check for the degenerate case of the block being over 50% full.
1462 * If so, it's not worth even looking to see if we might be able
1463 * to coalesce with a sibling.
1464 */
1465 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001466 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001467 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001468 ents = dp->d_ops->leaf_ents_p(leaf);
1469 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001470
1471 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001472 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001473 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /*
1475 * Blk over 50%, don't try to join.
1476 */
1477 *action = 0;
1478 return 0;
1479 }
1480 /*
1481 * Check for the degenerate case of the block being empty.
1482 * If the block is empty, we'll simply delete it, no need to
1483 * coalesce it with a sibling block. We choose (arbitrarily)
1484 * to merge with the forward block unless it is NULL.
1485 */
1486 if (count == 0) {
1487 /*
1488 * Make altpath point to the block we want to keep and
1489 * path point to the block we want to drop (this one).
1490 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001491 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001493 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 &rval);
1495 if (error)
1496 return error;
1497 *action = rval ? 2 : 0;
1498 return 0;
1499 }
1500 /*
1501 * Examine each sibling block to see if we can coalesce with
1502 * at least 25% free space to spare. We need to figure out
1503 * whether to merge with the forward or the backward block.
1504 * We prefer coalescing with the lower numbered sibling so as
1505 * to shrink a directory over time.
1506 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001507 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001509 struct xfs_dir3_icleaf_hdr hdr2;
1510
1511 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (blkno == 0)
1513 continue;
1514 /*
1515 * Read the sibling leaf block.
1516 */
Dave Chinner41419562013-10-29 22:11:50 +11001517 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001518 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001519 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 /*
1523 * Count bytes in the two blocks combined.
1524 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001525 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001526 bytes = state->args->geo->blksize -
1527 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001528
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001530 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001531 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001532 count += hdr2.count - hdr2.stale;
1533 bytes -= count * sizeof(ents[0]);
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /*
1536 * Fits with at least 25% to spare.
1537 */
1538 if (bytes >= 0)
1539 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001540 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542 /*
1543 * Didn't like either block, give up.
1544 */
1545 if (i >= 2) {
1546 *action = 0;
1547 return 0;
1548 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * Make altpath point to the block we want to keep (the lower
1552 * numbered block) and path point to the block we want to drop.
1553 */
1554 memcpy(&state->altpath, &state->path, sizeof(state->path));
1555 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001556 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 &rval);
1558 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001559 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 &rval);
1561 if (error) {
1562 return error;
1563 }
1564 *action = rval ? 0 : 1;
1565 return 0;
1566}
1567
1568/*
1569 * Move all the leaf entries from drop_blk to save_blk.
1570 * This is done as part of a join operation.
1571 */
1572void
1573xfs_dir2_leafn_unbalance(
1574 xfs_da_state_t *state, /* cursor */
1575 xfs_da_state_blk_t *drop_blk, /* dead block */
1576 xfs_da_state_blk_t *save_blk) /* surviving block */
1577{
1578 xfs_da_args_t *args; /* operation arguments */
1579 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1580 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001581 struct xfs_dir3_icleaf_hdr savehdr;
1582 struct xfs_dir3_icleaf_hdr drophdr;
1583 struct xfs_dir2_leaf_entry *sents;
1584 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001585 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 args = state->args;
1588 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1589 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001590 drop_leaf = drop_blk->bp->b_addr;
1591 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001592
Dave Chinner01ba43b2013-10-29 22:11:52 +11001593 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1594 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1595 sents = dp->d_ops->leaf_ents_p(save_leaf);
1596 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * If there are any stale leaf entries, take this opportunity
1600 * to purge them.
1601 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001602 if (drophdr.stale)
1603 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1604 if (savehdr.stale)
1605 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /*
1608 * Move the entries from drop to the appropriate end of save.
1609 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001610 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001611 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001612 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1613 save_blk->bp, &savehdr, sents, 0,
1614 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001616 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1617 save_blk->bp, &savehdr, sents,
1618 savehdr.count, drophdr.count);
1619 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1620
1621 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001622 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1623 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001624 xfs_dir3_leaf_log_header(args, save_blk->bp);
1625 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001626
Dave Chinner41419562013-10-29 22:11:50 +11001627 xfs_dir3_leaf_check(dp, save_blk->bp);
1628 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631/*
1632 * Top-level node form directory addname routine.
1633 */
1634int /* error */
1635xfs_dir2_node_addname(
1636 xfs_da_args_t *args) /* operation arguments */
1637{
1638 xfs_da_state_blk_t *blk; /* leaf block for insert */
1639 int error; /* error return value */
1640 int rval; /* sub-return value */
1641 xfs_da_state_t *state; /* btree cursor */
1642
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001643 trace_xfs_dir2_node_addname(args);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 /*
1646 * Allocate and initialize the state (btree cursor).
1647 */
1648 state = xfs_da_state_alloc();
1649 state->args = args;
1650 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * Look up the name. We're not supposed to find it, but
1653 * this gives us the insertion point.
1654 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001655 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (error)
1657 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001658 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto done;
1660 }
1661 /*
1662 * Add the data entry to a data block.
1663 * Extravalid is set to a freeblock found by lookup.
1664 */
1665 rval = xfs_dir2_node_addname_int(args,
1666 state->extravalid ? &state->extrablk : NULL);
1667 if (rval) {
1668 goto done;
1669 }
1670 blk = &state->path.blk[state->path.active - 1];
1671 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1672 /*
1673 * Add the new leaf entry.
1674 */
1675 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1676 if (rval == 0) {
1677 /*
1678 * It worked, fix the hash values up the btree.
1679 */
Barry Naujok6a178102008-05-21 16:42:05 +10001680 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001681 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 } else {
1683 /*
1684 * It didn't work, we need to split the leaf block.
1685 */
1686 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001687 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto done;
1689 }
1690 /*
1691 * Split the leaf block and insert the new entry.
1692 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001693 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695done:
1696 xfs_da_state_free(state);
1697 return rval;
1698}
1699
1700/*
1701 * Add the data entry for a node-format directory name addition.
1702 * The leaf entry is added in xfs_dir2_leafn_add.
1703 * We may enter with a freespace block that the lookup found.
1704 */
1705static int /* error */
1706xfs_dir2_node_addname_int(
1707 xfs_da_args_t *args, /* operation arguments */
1708 xfs_da_state_blk_t *fblk) /* optional freespace block */
1709{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001710 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001712 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1714 xfs_inode_t *dp; /* incore directory inode */
1715 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1716 int error; /* error return value */
1717 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001718 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int findex; /* freespace entry index */
1720 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1721 xfs_dir2_db_t ifbno; /* initial freespace block no */
1722 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1723 int length; /* length of the new entry */
1724 int logfree; /* need to log free entry */
1725 xfs_mount_t *mp; /* filesystem mount point */
1726 int needlog; /* need to log data header */
1727 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001728 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001730 __be16 *bests;
1731 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001732 struct xfs_dir2_data_free *bf;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001733 xfs_dir2_data_aoff_t aoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 dp = args->dp;
1736 mp = dp->i_mount;
1737 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001738 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
1740 * If we came in with a freespace block that means that lookup
1741 * found an entry with our hash value. This is the freespace
1742 * block for that data entry.
1743 */
1744 if (fblk) {
1745 fbp = fblk->bp;
1746 /*
1747 * Remember initial freespace block number.
1748 */
1749 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001750 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001752 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001753 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * This means the free entry showed that the data block had
1757 * space for our entry, so we remembered it.
1758 * Use that data block.
1759 */
1760 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001761 ASSERT(findex < freehdr.nvalid);
1762 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1763 ASSERT(be16_to_cpu(bests[findex]) >= length);
1764 dbno = freehdr.firstdb + findex;
1765 } else {
1766 /*
1767 * The data block looked at didn't have enough room.
1768 * We'll start at the beginning of the freespace entries.
1769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 dbno = -1;
1771 findex = 0;
1772 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001773 } else {
1774 /*
1775 * Didn't come in with a freespace block, so no data block.
1776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ifbno = dbno = -1;
1778 fbp = NULL;
1779 findex = 0;
1780 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /*
1783 * If we don't have a data block yet, we're going to scan the
1784 * freespace blocks looking for one. Figure out what the
1785 * highest freespace block number is.
1786 */
1787 if (dbno == -1) {
1788 xfs_fileoff_t fo; /* freespace block number */
1789
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001790 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001792 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 fbno = ifbno;
1794 }
1795 /*
1796 * While we haven't identified a data block, search the freeblock
1797 * data for a good data block. If we find a null freeblock entry,
1798 * indicating a hole in the data blocks, remember that.
1799 */
1800 while (dbno == -1) {
1801 /*
1802 * If we don't have a freeblock in hand, get the next one.
1803 */
1804 if (fbp == NULL) {
1805 /*
1806 * Happens the first time through unless lookup gave
1807 * us a freespace block to start with.
1808 */
1809 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001810 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001811 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * If it's ifbno we already looked at it.
1814 */
1815 if (fbno == ifbno)
1816 fbno++;
1817 /*
1818 * If it's off the end we're done.
1819 */
1820 if (fbno >= lastfbno)
1821 break;
1822 /*
1823 * Read the block. There can be holes in the
1824 * freespace blocks, so this might not succeed.
1825 * This should be really rare, so there's no reason
1826 * to avoid it.
1827 */
Dave Chinner20252072012-11-12 22:54:13 +11001828 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001829 xfs_dir2_db_to_da(args->geo, fbno),
1830 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001831 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001833 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001835 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 findex = 0;
1837 }
1838 /*
1839 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001840 *
1841 * The bests initialisation should be where the bufer is read in
1842 * the above branch. But gcc is too stupid to realise that bests
1843 * and the freehdr are actually initialised if they are placed
1844 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001846 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001847 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001848 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1849 be16_to_cpu(bests[findex]) >= length)
1850 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 else {
1852 /*
1853 * Are we done with the freeblock?
1854 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001855 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
1857 * Drop the block.
1858 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001859 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 fbp = NULL;
1861 if (fblk && fblk->bp)
1862 fblk->bp = NULL;
1863 }
1864 }
1865 }
1866 /*
1867 * If we don't have a data block, we need to allocate one and make
1868 * the freespace entries refer to it.
1869 */
1870 if (unlikely(dbno == -1)) {
1871 /*
1872 * Not allowed to allocate, return failure.
1873 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001874 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001875 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /*
1878 * Allocate and initialize the new data block.
1879 */
1880 if (unlikely((error = xfs_dir2_grow_inode(args,
1881 XFS_DIR2_DATA_SPACE,
1882 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001883 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /*
1887 * If (somehow) we have a freespace block, get rid of it.
1888 */
1889 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001890 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (fblk && fblk->bp)
1892 fblk->bp = NULL;
1893
1894 /*
1895 * Get the freespace block corresponding to the data block
1896 * that was just allocated.
1897 */
Dave Chinner8f661932014-06-06 15:15:59 +10001898 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001899 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001900 xfs_dir2_db_to_da(args->geo, fbno),
1901 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001902 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /*
1906 * If there wasn't a freespace block, the read will
1907 * return a NULL fbp. Allocate and initialize a new one.
1908 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001909 if (!fbp) {
1910 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1911 &fbno);
1912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Dave Chinner8f661932014-06-06 15:15:59 +10001915 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001916 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001917"%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 +11001918 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001919 (long long)dp->d_ops->db_to_fdb(
1920 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 (long long)dbno, (long long)fbno,
1922 (unsigned long long)ifbno, lastfbno);
1923 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001924 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08001925 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 fblk,
1927 (unsigned long long)fblk->blkno,
1928 fblk->index,
1929 fblk->magic);
1930 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001931 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1934 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001935 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
1938 /*
1939 * Get a buffer for the new block.
1940 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001941 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001942 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001944 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001945 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001946 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001949 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001951 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001952 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001953 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001954 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001956 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001957 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001958 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
1961 /*
1962 * Set the freespace block index from the data block number.
1963 */
Dave Chinner8f661932014-06-06 15:15:59 +10001964 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /*
1966 * If it's after the end of the current entries in the
1967 * freespace block, extend that table.
1968 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001969 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001970 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001971 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
1973 * Tag new entry so nused will go up.
1974 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001975 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977 /*
1978 * If this entry was for an empty data block
1979 * (this should always be true) then update the header.
1980 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001981 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1982 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001983 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001984 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986 /*
1987 * Update the real value in the table.
1988 * We haven't allocated the data entry yet so this will
1989 * change again.
1990 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001991 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001992 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001993 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 logfree = 1;
1995 }
1996 /*
1997 * We had a data block so we don't have to make a new one.
1998 */
1999 else {
2000 /*
2001 * If just checking, we succeeded.
2002 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002003 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * Read the data block in.
2008 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002009 error = xfs_dir3_data_read(tp, dp,
2010 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11002011 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002012 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002014 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11002015 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 logfree = 0;
2017 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002018 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
2020 * Point to the existing unused space.
2021 */
2022 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002023 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 needscan = needlog = 0;
2025 /*
2026 * Mark the first part of the unused space, inuse for us.
2027 */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07002028 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
2029 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
2030 &needlog, &needscan);
2031 if (error) {
2032 xfs_trans_brelse(tp, dbp);
2033 return error;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /*
2036 * Fill in the new entry and log it.
2037 */
2038 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002039 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dep->namelen = args->namelen;
2041 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002042 dp->d_ops->data_put_ftype(dep, args->filetype);
2043 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002044 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002045 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /*
2047 * Rescan the block for bestfree if needed.
2048 */
2049 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002050 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /*
2052 * Log the data block header if needed.
2053 */
2054 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002055 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * If the freespace entry is now wrong, update it.
2058 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002059 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002060 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2061 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 logfree = 1;
2063 }
2064 /*
2065 * Log the freespace entry if needed.
2066 */
2067 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002068 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 * Return the data block and offset in args, then drop the data block.
2071 */
2072 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002073 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075}
2076
2077/*
2078 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002079 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * The only real output is the inode number of the entry.
2081 */
2082int /* error */
2083xfs_dir2_node_lookup(
2084 xfs_da_args_t *args) /* operation arguments */
2085{
2086 int error; /* error return value */
2087 int i; /* btree level */
2088 int rval; /* operation return value */
2089 xfs_da_state_t *state; /* btree cursor */
2090
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002091 trace_xfs_dir2_node_lookup(args);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 /*
2094 * Allocate and initialize the btree cursor.
2095 */
2096 state = xfs_da_state_alloc();
2097 state->args = args;
2098 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * Fill in the path to the entry in the cursor.
2101 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002102 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (error)
2104 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002105 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2106 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002107 xfs_dir2_data_entry_t *dep;
2108
Dave Chinner1d9025e2012-06-22 18:50:14 +10002109 dep = (xfs_dir2_data_entry_t *)
2110 ((char *)state->extrablk.bp->b_addr +
2111 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002112 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * Release the btree blocks and leaf block.
2116 */
2117 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002118 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 state->path.blk[i].bp = NULL;
2120 }
2121 /*
2122 * Release the data block if we have it.
2123 */
2124 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002125 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 state->extrablk.bp = NULL;
2127 }
2128 xfs_da_state_free(state);
2129 return rval;
2130}
2131
2132/*
2133 * Remove an entry from a node-format directory.
2134 */
2135int /* error */
2136xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002137 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002139 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 int error; /* error return value */
2141 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002142 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002144 trace_xfs_dir2_node_removename(args);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * Allocate and initialize the btree cursor.
2148 */
2149 state = xfs_da_state_alloc();
2150 state->args = args;
2151 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002152
2153 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002154 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002155 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002156 goto out_free;
2157
2158 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002159 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002160 error = rval;
2161 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 blk = &state->path.blk[state->path.active - 1];
2165 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2166 ASSERT(state->extravalid);
2167 /*
2168 * Remove the leaf and data entries.
2169 * Extrablk refers to the data block.
2170 */
2171 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2172 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002173 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002174 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * Fix the hash values up the btree.
2177 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002178 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * If we need to join leaf blocks, do it.
2181 */
2182 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002183 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
2185 * If no errors so far, try conversion to leaf format.
2186 */
2187 if (!error)
2188 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002189out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 xfs_da_state_free(state);
2191 return error;
2192}
2193
2194/*
2195 * Replace an entry's inode number in a node-format directory.
2196 */
2197int /* error */
2198xfs_dir2_node_replace(
2199 xfs_da_args_t *args) /* operation arguments */
2200{
2201 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002202 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 xfs_dir2_data_entry_t *dep; /* data entry changed */
2204 int error; /* error return value */
2205 int i; /* btree level */
2206 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002207 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 xfs_dir2_leaf_t *leaf; /* leaf structure */
2209 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2210 int rval; /* internal return value */
2211 xfs_da_state_t *state; /* btree cursor */
2212
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002213 trace_xfs_dir2_node_replace(args);
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * Allocate and initialize the btree cursor.
2217 */
2218 state = xfs_da_state_alloc();
2219 state->args = args;
2220 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002221
2222 /*
2223 * We have to save new inode number and ftype since
2224 * xfs_da3_node_lookup_int() is going to overwrite them
2225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002227 ftype = args->filetype;
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /*
2230 * Lookup the entry to change in the btree.
2231 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002232 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (error) {
2234 rval = error;
2235 }
2236 /*
2237 * It should be found, since the vnodeops layer has looked it up
2238 * and locked it. But paranoia is good.
2239 */
Dave Chinner24513372014-06-25 14:58:08 +10002240 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002241 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /*
2243 * Find the leaf entry.
2244 */
2245 blk = &state->path.blk[state->path.active - 1];
2246 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002247 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002248 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002249 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 ASSERT(state->extravalid);
2251 /*
2252 * Point to the data entry.
2253 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002254 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002255 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2256 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002258 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002259 xfs_dir2_dataptr_to_off(args->geo,
2260 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002261 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /*
2263 * Fill in the new inode number and log the entry.
2264 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002265 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002266 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002267 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 rval = 0;
2269 }
2270 /*
2271 * Didn't find it, and we're holding a data block. Drop it.
2272 */
2273 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002274 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 state->extrablk.bp = NULL;
2276 }
2277 /*
2278 * Release all the buffers in the cursor.
2279 */
2280 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002281 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 state->path.blk[i].bp = NULL;
2283 }
2284 xfs_da_state_free(state);
2285 return rval;
2286}
2287
2288/*
2289 * Trim off a trailing empty freespace block.
2290 * Return (in rvalp) 1 if we did it, 0 if not.
2291 */
2292int /* error */
2293xfs_dir2_node_trim_free(
2294 xfs_da_args_t *args, /* operation arguments */
2295 xfs_fileoff_t fo, /* free block number */
2296 int *rvalp) /* out: did something */
2297{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002298 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 xfs_inode_t *dp; /* incore directory inode */
2300 int error; /* error return code */
2301 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002303 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002307
2308 *rvalp = 0;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * Read the freespace block.
2312 */
Dave Chinner20252072012-11-12 22:54:13 +11002313 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002314 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
2317 * There can be holes in freespace. If fo is a hole, there's
2318 * nothing to do.
2319 */
Dave Chinner20252072012-11-12 22:54:13 +11002320 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002322 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002323 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /*
2326 * If there are used entries, there's nothing to do.
2327 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002328 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002329 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
2331 }
2332 /*
2333 * Blow the block away.
2334 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002335 error = xfs_dir2_shrink_inode(args,
2336 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2337 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * Can't fail with ENOSPC since that only happens with no
2340 * space reservation, when breaking up an extent into two
2341 * pieces. This is the last block of an extent.
2342 */
Dave Chinner24513372014-06-25 14:58:08 +10002343 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002344 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return error;
2346 }
2347 /*
2348 * Return that we succeeded.
2349 */
2350 *rvalp = 1;
2351 return 0;
2352}