blob: 3bdbe18972121d76891aa11d1024926a4b4a6ad4 [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,
87 bp->b_addr, __FILE__, __LINE__, fa);
88 ASSERT(0);
89}
Dave Chinner24df33b2013-04-12 07:30:21 +100090#else
Dave Chinner41419562013-10-29 22:11:50 +110091#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100092#endif
93
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080094static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110095xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110096 struct xfs_buf *bp)
97{
98 struct xfs_mount *mp = bp->b_target->bt_mount;
99 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +1100100
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100101 if (xfs_sb_version_hascrc(&mp->m_sb)) {
102 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
103
104 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800105 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000106 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800107 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100108 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800109 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100110 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800111 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100112 } else {
113 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800114 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100115 }
116
117 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
118
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800119 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100120}
121
122static void
123xfs_dir3_free_read_verify(
124 struct xfs_buf *bp)
125{
126 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800127 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128
Eric Sandeence5028c2014-02-27 15:23:10 +1100129 if (xfs_sb_version_hascrc(&mp->m_sb) &&
130 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800131 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
132 else {
133 fa = xfs_dir3_free_verify(bp);
134 if (fa)
135 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
136 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100137}
Dave Chinner20252072012-11-12 22:54:13 +1100138
Dave Chinner612cfbf2012-11-14 17:52:32 +1100139static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100140xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100141 struct xfs_buf *bp)
142{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100143 struct xfs_mount *mp = bp->b_target->bt_mount;
144 struct xfs_buf_log_item *bip = bp->b_fspriv;
145 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800146 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100147
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800148 fa = xfs_dir3_free_verify(bp);
149 if (fa) {
150 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100151 return;
152 }
153
154 if (!xfs_sb_version_hascrc(&mp->m_sb))
155 return;
156
157 if (bip)
158 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
159
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100160 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100161}
162
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100163const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100164 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100165 .verify_read = xfs_dir3_free_read_verify,
166 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100167};
Dave Chinner20252072012-11-12 22:54:13 +1100168
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800169/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800170static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800171xfs_dir3_free_header_check(
172 struct xfs_inode *dp,
173 xfs_dablk_t fbno,
174 struct xfs_buf *bp)
175{
176 struct xfs_mount *mp = dp->i_mount;
177 unsigned int firstdb;
178 int maxbests;
179
180 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
181 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
182 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
183 maxbests;
184 if (xfs_sb_version_hascrc(&mp->m_sb)) {
185 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
186
187 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800188 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800189 if (be32_to_cpu(hdr3->nvalid) > maxbests)
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) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800192 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800193 } else {
194 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
195
196 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800197 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800198 if (be32_to_cpu(hdr->nvalid) > maxbests)
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) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800201 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800202 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800203 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800204}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100205
Dave Chinner20252072012-11-12 22:54:13 +1100206static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100207__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100208 struct xfs_trans *tp,
209 struct xfs_inode *dp,
210 xfs_dablk_t fbno,
211 xfs_daddr_t mappedbno,
212 struct xfs_buf **bpp)
213{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800214 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100215 int err;
216
217 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100218 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800219 if (err || !*bpp)
220 return err;
221
222 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800223 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
224 if (fa) {
225 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800226 xfs_trans_brelse(tp, *bpp);
227 return -EFSCORRUPTED;
228 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100229
230 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800231 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100232 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800233
234 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100235}
236
237int
238xfs_dir2_free_read(
239 struct xfs_trans *tp,
240 struct xfs_inode *dp,
241 xfs_dablk_t fbno,
242 struct xfs_buf **bpp)
243{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100244 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100245}
246
247static int
248xfs_dir2_free_try_read(
249 struct xfs_trans *tp,
250 struct xfs_inode *dp,
251 xfs_dablk_t fbno,
252 struct xfs_buf **bpp)
253{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100254 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
255}
256
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100257static int
258xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000259 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100260 xfs_dir2_db_t fbno,
261 struct xfs_buf **bpp)
262{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000263 struct xfs_trans *tp = args->trans;
264 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100265 struct xfs_mount *mp = dp->i_mount;
266 struct xfs_buf *bp;
267 int error;
268 struct xfs_dir3_icfree_hdr hdr;
269
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000270 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100271 -1, &bp, XFS_DATA_FORK);
272 if (error)
273 return error;
274
Dave Chinner61fe1352013-04-03 16:11:30 +1100275 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100276 bp->b_ops = &xfs_dir3_free_buf_ops;
277
278 /*
279 * Initialize the new block to be empty, and remember
280 * its first slot as our empty slot.
281 */
Dave Chinnere400d272013-05-28 18:37:17 +1000282 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
283 memset(&hdr, 0, sizeof(hdr));
284
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100285 if (xfs_sb_version_hascrc(&mp->m_sb)) {
286 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
287
288 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000289
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100290 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
291 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000292 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000293 } else
294 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100295 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100296 *bpp = bp;
297 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
301 * Log entries from a freespace block.
302 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000303STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000305 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000306 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int first, /* first entry to log */
308 int last) /* last entry to log */
309{
310 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100311 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dave Chinner1d9025e2012-06-22 18:50:14 +1000313 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000314 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100315 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
316 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000317 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100318 (uint)((char *)&bests[first] - (char *)free),
319 (uint)((char *)&bests[last] - (char *)free +
320 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323/*
324 * Log header from a freespace block.
325 */
326static void
327xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000328 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000329 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Dave Chinner836a94a2013-08-12 20:49:44 +1000331#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_dir2_free_t *free; /* freespace structure */
333
Dave Chinner1d9025e2012-06-22 18:50:14 +1000334 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100335 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
336 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000337#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000338 xfs_trans_log_buf(args->trans, bp, 0,
339 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/*
343 * Convert a leaf-format directory to a node-format directory.
344 * We need to change the magic number of the leaf block, and copy
345 * the freespace table out of the leaf block into its own block.
346 */
347int /* error */
348xfs_dir2_leaf_to_node(
349 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000350 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 xfs_inode_t *dp; /* incore directory inode */
353 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000354 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 xfs_dir2_db_t fdb; /* freespace block number */
356 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100357 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 int i; /* leaf freespace index */
359 xfs_dir2_leaf_t *leaf; /* leaf structure */
360 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int n; /* count of live freespc ents */
362 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100363 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100365 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000367 trace_xfs_dir2_leaf_to_node(args);
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tp = args->trans;
371 /*
372 * Add a freespace block to the directory.
373 */
374 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
375 return error;
376 }
Dave Chinner30028032014-06-06 15:08:18 +1000377 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Get the buffer for the new freespace block.
380 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000381 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100382 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100384
Dave Chinner1d9025e2012-06-22 18:50:14 +1000385 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100386 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000387 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000388 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100389 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000390 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * Copy freespace entries from the leaf block to the new block.
394 * Count active entries.
395 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100396 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500397 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100398 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100399 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100401 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100405 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100407 freehdr.nused = n;
408 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
409
Dave Chinner01ba43b2013-10-29 22:11:52 +1100410 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000411 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
412 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100413
Dave Chinner24df33b2013-04-12 07:30:21 +1000414 /*
415 * Converting the leaf to a leafnode is just a matter of changing the
416 * magic number and the ops. Do the change directly to the buffer as
417 * it's less work (and less code) than decoding the header to host
418 * format and back again.
419 */
420 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
421 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
422 else
423 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
424 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100425 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000426 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100427 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return 0;
429}
430
431/*
432 * Add a leaf entry to a leaf block in a node-form directory.
433 * The other work necessary is done from the caller.
434 */
435static int /* error */
436xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000437 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 xfs_da_args_t *args, /* operation arguments */
439 int index) /* insertion pt for new entry */
440{
441 int compact; /* compacting stale leaves */
442 xfs_inode_t *dp; /* incore directory inode */
443 int highstale; /* next stale entry */
444 xfs_dir2_leaf_t *leaf; /* leaf structure */
445 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
446 int lfloghigh; /* high leaf entry logging */
447 int lfloglow; /* low leaf entry logging */
448 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000449 struct xfs_dir3_icleaf_hdr leafhdr;
450 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000452 trace_xfs_dir2_leafn_add(args, index);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000455 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100456 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100457 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /*
460 * Quick check just to make sure we are not going to index
461 * into other peoples memory
462 */
463 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000464 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /*
467 * If there are already the maximum number of leaf entries in
468 * the block, if there are no stale entries it won't fit.
469 * Caller will do a split. If there are stale entries we'll do
470 * a compact.
471 */
472
Dave Chinner8f661932014-06-06 15:15:59 +1000473 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000474 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000475 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000476 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 } else
478 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000479 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
480 ASSERT(index == leafhdr.count ||
481 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Barry Naujok6a178102008-05-21 16:42:05 +1000483 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485
486 /*
487 * Compact out all but one stale leaf entry. Leaves behind
488 * the entry closest to index.
489 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000490 if (compact)
491 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
492 &highstale, &lfloglow, &lfloghigh);
493 else if (leafhdr.stale) {
494 /*
495 * Set impossible logging indices for this case.
496 */
497 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 lfloghigh = -1;
499 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * Insert the new entry, log everything.
503 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000504 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200505 highstale, &lfloglow, &lfloghigh);
506
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100507 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000508 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100509 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000510
Dave Chinner01ba43b2013-10-29 22:11:52 +1100511 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000512 xfs_dir3_leaf_log_header(args, bp);
513 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100514 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516}
517
518#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100519static void
520xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100521 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100522 struct xfs_buf *bp,
523 xfs_dir2_db_t db)
524{
525 struct xfs_dir3_icfree_hdr hdr;
526
Dave Chinner01ba43b2013-10-29 22:11:52 +1100527 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100528
Dave Chinner8f661932014-06-06 15:15:59 +1000529 ASSERT((hdr.firstdb %
530 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100531 ASSERT(hdr.firstdb <= db);
532 ASSERT(db < hdr.firstdb + hdr.nvalid);
533}
534#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100535#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#endif /* DEBUG */
537
538/*
539 * Return the last hash value in the leaf.
540 * Stale entries are ok.
541 */
542xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700543xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100544 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000545 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 int *count) /* count of entries in leaf */
547{
Dave Chinner24df33b2013-04-12 07:30:21 +1000548 struct xfs_dir2_leaf *leaf = bp->b_addr;
549 struct xfs_dir2_leaf_entry *ents;
550 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Dave Chinner01ba43b2013-10-29 22:11:52 +1100552 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000553
554 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700555 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
556 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
557 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000560 *count = leafhdr.count;
561 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000563
Dave Chinner41419562013-10-29 22:11:50 +1100564 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000565 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000569 * Look up a leaf entry for space to add a name in a node-format leaf block.
570 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000572STATIC int
573xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000574 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 xfs_da_args_t *args, /* operation arguments */
576 int *indexp, /* out: leaf entry index */
577 xfs_da_state_t *state) /* state to fill in */
578{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000579 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000580 xfs_dir2_db_t curdb = -1; /* current data block number */
581 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 xfs_inode_t *dp; /* incore directory inode */
583 int error; /* error return value */
584 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000585 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int index; /* leaf entry index */
587 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000588 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
590 xfs_mount_t *mp; /* filesystem mount point */
591 xfs_dir2_db_t newdb; /* new data block number */
592 xfs_dir2_db_t newfdb; /* new free block number */
593 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000594 struct xfs_dir2_leaf_entry *ents;
595 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 dp = args->dp;
598 tp = args->trans;
599 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000600 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100601 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100602 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000603
Dave Chinner41419562013-10-29 22:11:50 +1100604 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000605 ASSERT(leafhdr.count > 0);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /*
608 * Look up the hash value in the leaf entries.
609 */
610 index = xfs_dir2_leaf_search_hash(args, bp);
611 /*
612 * Do we have a buffer coming in?
613 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000614 if (state->extravalid) {
615 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000617 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000618 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100619 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
620 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100622 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * Loop over leaf entries with the right hash value.
625 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000626 for (lep = &ents[index];
627 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
628 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /*
630 * Skip stale leaf entries.
631 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100632 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 continue;
634 /*
635 * Pull the data block number from the entry.
636 */
Dave Chinner30028032014-06-06 15:08:18 +1000637 newdb = xfs_dir2_dataptr_to_db(args->geo,
638 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * For addname, we're looking for a place to put the new entry.
641 * We want to use a data block with an entry of equal
642 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643 *
644 * If this block isn't the data block we already have
645 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000647 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100648 __be16 *bests;
649
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000652 * Convert the data block to the free block
653 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Dave Chinner8f661932014-06-06 15:15:59 +1000655 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000657 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000661 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
663 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000664 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100665
666 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000667 xfs_dir2_db_to_da(args->geo,
668 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100669 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000670 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000672 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100673
Dave Chinner01ba43b2013-10-29 22:11:52 +1100674 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Dave Chinner8f661932014-06-06 15:15:59 +1000679 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000681 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500683 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100684 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000685 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
686 XFS_ERRLEVEL_LOW, mp);
687 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000688 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000689 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000691 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100692 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000693 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
695 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000696 /* Didn't find any space */
697 fi = -1;
698out:
Barry Naujok6a178102008-05-21 16:42:05 +1000699 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000700 if (curbp) {
701 /* Giving back a free block. */
702 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000704 state->extrablk.index = fi;
705 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100706
707 /*
708 * Important: this magic number is not in the buffer - it's for
709 * buffer type information and therefore only the free/data type
710 * matters here, not whether CRCs are enabled or not.
711 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000712 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
713 } else {
714 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000717 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000720 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 * Look up a leaf entry in a node-format leaf block.
725 * The extrablk in state a data block.
726 */
727STATIC int
728xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000729 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000730 xfs_da_args_t *args, /* operation arguments */
731 int *indexp, /* out: leaf entry index */
732 xfs_da_state_t *state) /* state to fill in */
733{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000734 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000735 xfs_dir2_db_t curdb = -1; /* current data block number */
736 xfs_dir2_data_entry_t *dep; /* data block entry */
737 xfs_inode_t *dp; /* incore directory inode */
738 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000739 int index; /* leaf entry index */
740 xfs_dir2_leaf_t *leaf; /* leaf structure */
741 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
742 xfs_mount_t *mp; /* filesystem mount point */
743 xfs_dir2_db_t newdb; /* new data block number */
744 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000745 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000746 struct xfs_dir2_leaf_entry *ents;
747 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000748
749 dp = args->dp;
750 tp = args->trans;
751 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000752 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100753 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100754 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000755
Dave Chinner41419562013-10-29 22:11:50 +1100756 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000757 ASSERT(leafhdr.count > 0);
758
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000759 /*
760 * Look up the hash value in the leaf entries.
761 */
762 index = xfs_dir2_leaf_search_hash(args, bp);
763 /*
764 * Do we have a buffer coming in?
765 */
766 if (state->extravalid) {
767 curbp = state->extrablk.bp;
768 curdb = state->extrablk.blkno;
769 }
770 /*
771 * Loop over leaf entries with the right hash value.
772 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000773 for (lep = &ents[index];
774 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
775 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000776 /*
777 * Skip stale leaf entries.
778 */
779 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
780 continue;
781 /*
782 * Pull the data block number from the entry.
783 */
Dave Chinner30028032014-06-06 15:08:18 +1000784 newdb = xfs_dir2_dataptr_to_db(args->geo,
785 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000786 /*
787 * Not adding a new entry, so we really want to find
788 * the name given to us.
789 *
790 * If it's a different data block, go get it.
791 */
792 if (newdb != curdb) {
793 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000794 * If we had a block before that we aren't saving
795 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000796 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000797 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
798 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000799 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000800 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000801 * If needing the block that is saved with a CI match,
802 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000803 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000804 if (args->cmpresult != XFS_CMP_DIFFERENT &&
805 newdb == state->extrablk.blkno) {
806 ASSERT(state->extravalid);
807 curbp = state->extrablk.bp;
808 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100809 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000810 xfs_dir2_db_to_da(args->geo,
811 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100812 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000813 if (error)
814 return error;
815 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100816 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000817 curdb = newdb;
818 }
819 /*
820 * Point to the data entry.
821 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000822 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000823 xfs_dir2_dataptr_to_off(args->geo,
824 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000825 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000826 * Compare the entry and if it's an exact match, return
827 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000828 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000829 */
Barry Naujok5163f952008-05-21 16:41:01 +1000830 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
831 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000832 /* If there is a CI match block, drop it */
833 if (args->cmpresult != XFS_CMP_DIFFERENT &&
834 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000835 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000836 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000837 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100838 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000839 *indexp = index;
840 state->extravalid = 1;
841 state->extrablk.bp = curbp;
842 state->extrablk.blkno = curdb;
843 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000844 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000845 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100846 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100847 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000848 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000849 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000850 }
851 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000852 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000853 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000854 if (args->cmpresult == XFS_CMP_DIFFERENT) {
855 /* Giving back last used data block. */
856 state->extravalid = 1;
857 state->extrablk.bp = curbp;
858 state->extrablk.index = -1;
859 state->extrablk.blkno = curdb;
860 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100861 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100862 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000863 } else {
864 /* If the curbp is not the CI match block, drop it */
865 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000866 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000867 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000868 } else {
869 state->extravalid = 0;
870 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000871 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000872 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000873}
874
875/*
876 * Look up a leaf entry in a node-format leaf block.
877 * If this is an addname then the extrablk in state is a freespace block,
878 * otherwise it's a data block.
879 */
880int
881xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000882 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000883 xfs_da_args_t *args, /* operation arguments */
884 int *indexp, /* out: leaf entry index */
885 xfs_da_state_t *state) /* state to fill in */
886{
Barry Naujok6a178102008-05-21 16:42:05 +1000887 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000888 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
889 state);
890 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
891}
892
893/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 * Move count leaf entries from source to destination leaf.
895 * Log entries and headers. Stale entries are preserved.
896 */
897static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000898xfs_dir3_leafn_moveents(
899 xfs_da_args_t *args, /* operation arguments */
900 struct xfs_buf *bp_s, /* source */
901 struct xfs_dir3_icleaf_hdr *shdr,
902 struct xfs_dir2_leaf_entry *sents,
903 int start_s,/* source leaf index */
904 struct xfs_buf *bp_d, /* destination */
905 struct xfs_dir3_icleaf_hdr *dhdr,
906 struct xfs_dir2_leaf_entry *dents,
907 int start_d,/* destination leaf index */
908 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Dave Chinner24df33b2013-04-12 07:30:21 +1000910 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000912 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /*
915 * Silently return if nothing to do.
916 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 /*
921 * If the destination index is not the end of the current
922 * destination leaf entries, open up a hole in the destination
923 * to hold the new entries.
924 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000925 if (start_d < dhdr->count) {
926 memmove(&dents[start_d + count], &dents[start_d],
927 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000928 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000929 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 /*
932 * If the source has stale leaves, count the ones in the copy range
933 * so we can update the header correctly.
934 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000935 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int i; /* temp leaf index */
937
938 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000939 if (sents[i].address ==
940 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 stale++;
942 }
943 } else
944 stale = 0;
945 /*
946 * Copy the leaf entries from source to destination.
947 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000948 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000950 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
953 * If there are source entries after the ones we copied,
954 * delete the ones we copied by sliding the next ones down.
955 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000956 if (start_s + count < shdr->count) {
957 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000959 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /*
963 * Update the headers and log them.
964 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000965 shdr->count -= count;
966 shdr->stale -= stale;
967 dhdr->count += count;
968 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
971/*
972 * Determine the sort order of two leaf blocks.
973 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
974 */
975int /* sort order */
976xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100977 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000978 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
979 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Dave Chinner24df33b2013-04-12 07:30:21 +1000981 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
982 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
983 struct xfs_dir2_leaf_entry *ents1;
984 struct xfs_dir2_leaf_entry *ents2;
985 struct xfs_dir3_icleaf_hdr hdr1;
986 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Dave Chinner01ba43b2013-10-29 22:11:52 +1100988 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
989 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100990 ents1 = dp->d_ops->leaf_ents_p(leaf1);
991 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000992
993 if (hdr1.count > 0 && hdr2.count > 0 &&
994 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
995 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
996 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 1;
998 return 0;
999}
1000
1001/*
1002 * Rebalance leaf entries between two leaf blocks.
1003 * This is actually only called when the second block is new,
1004 * though the code deals with the general case.
1005 * A new entry will be inserted in one of the blocks, and that
1006 * entry is taken into account when balancing.
1007 */
1008static void
1009xfs_dir2_leafn_rebalance(
1010 xfs_da_state_t *state, /* btree cursor */
1011 xfs_da_state_blk_t *blk1, /* first btree block */
1012 xfs_da_state_blk_t *blk2) /* second btree block */
1013{
1014 xfs_da_args_t *args; /* operation arguments */
1015 int count; /* count (& direction) leaves */
1016 int isleft; /* new goes in left leaf */
1017 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1018 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1019 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001020#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int oldstale; /* old count of stale leaves */
1022#endif
1023 int oldsum; /* old total leaf count */
1024 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001025 struct xfs_dir2_leaf_entry *ents1;
1026 struct xfs_dir2_leaf_entry *ents2;
1027 struct xfs_dir3_icleaf_hdr hdr1;
1028 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001029 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 args = state->args;
1032 /*
1033 * If the block order is wrong, swap the arguments.
1034 */
Dave Chinner41419562013-10-29 22:11:50 +11001035 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 xfs_da_state_blk_t *tmp; /* temp for block swap */
1037
1038 tmp = blk1;
1039 blk1 = blk2;
1040 blk2 = tmp;
1041 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001042 leaf1 = blk1->bp->b_addr;
1043 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001044 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1045 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001046 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1047 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001048
1049 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001050#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001051 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#endif
1053 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /*
1056 * If the old leaf count was odd then the new one will be even,
1057 * so we need to divide the new count evenly.
1058 */
1059 if (oldsum & 1) {
1060 xfs_dahash_t midhash; /* middle entry hash value */
1061
Dave Chinner24df33b2013-04-12 07:30:21 +10001062 if (mid >= hdr1.count)
1063 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001065 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 isleft = args->hashval <= midhash;
1067 }
1068 /*
1069 * If the old count is even then the new count is odd, so there's
1070 * no preferred side for the new entry.
1071 * Pick the left one.
1072 */
1073 else
1074 isleft = 1;
1075 /*
1076 * Calculate moved entry count. Positive means left-to-right,
1077 * negative means right-to-left. Then move the entries.
1078 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001079 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001081 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1082 hdr1.count - count, blk2->bp,
1083 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001085 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1086 blk1->bp, &hdr1, ents1,
1087 hdr1.count, count);
1088
1089 ASSERT(hdr1.count + hdr2.count == oldsum);
1090 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1091
1092 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001093 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1094 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001095 xfs_dir3_leaf_log_header(args, blk1->bp);
1096 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001097
Dave Chinner41419562013-10-29 22:11:50 +11001098 xfs_dir3_leaf_check(dp, blk1->bp);
1099 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /*
1102 * Mark whether we're inserting into the old or new leaf.
1103 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001104 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001106 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 state->inleaf = !swap;
1108 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001109 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /*
1111 * Adjust the expected index for insertion.
1112 */
1113 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001114 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001115
1116 /*
1117 * Finally sanity check just to make sure we are not returning a
1118 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
Dave Chinner41419562013-10-29 22:11:50 +11001120 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 state->inleaf = 1;
1122 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001123 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001124 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001125 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127}
1128
Dave Chinner20252072012-11-12 22:54:13 +11001129static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001130xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001131 xfs_da_args_t *args,
1132 struct xfs_dir2_data_hdr *hdr,
1133 struct xfs_dir2_free *free,
1134 xfs_dir2_db_t fdb,
1135 int findex,
1136 struct xfs_buf *fbp,
1137 int longest)
1138{
Dave Chinner20252072012-11-12 22:54:13 +11001139 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001140 __be16 *bests;
1141 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001142 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001143
Dave Chinner01ba43b2013-10-29 22:11:52 +11001144 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001145 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001146 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001147 /*
1148 * Data block is not empty, just set the free entry to the new
1149 * value.
1150 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001151 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001152 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001153 return 0;
1154 }
1155
1156 /* One less used entry in the free table. */
1157 freehdr.nused--;
1158
1159 /*
1160 * If this was the last entry in the table, we can trim the table size
1161 * back. There might be other entries at the end referring to
1162 * non-existent data blocks, get those too.
1163 */
1164 if (findex == freehdr.nvalid - 1) {
1165 int i; /* free entry index */
1166
1167 for (i = findex - 1; i >= 0; i--) {
1168 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1169 break;
1170 }
1171 freehdr.nvalid = i + 1;
1172 logfree = 0;
1173 } else {
1174 /* Not the last entry, just punch it out. */
1175 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001176 logfree = 1;
1177 }
1178
Dave Chinner01ba43b2013-10-29 22:11:52 +11001179 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001180 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001181
1182 /*
1183 * If there are no useful entries left in the block, get rid of the
1184 * block if we can.
1185 */
1186 if (!freehdr.nused) {
1187 int error;
1188
1189 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1190 if (error == 0) {
1191 fbp = NULL;
1192 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001193 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001194 return error;
1195 /*
1196 * It's possible to get ENOSPC if there is no
1197 * space reservation. In this case some one
1198 * else will eventually get rid of this block.
1199 */
1200 }
1201
Dave Chinner20252072012-11-12 22:54:13 +11001202 /* Log the free entry that changed, unless we got rid of it. */
1203 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001204 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001205 return 0;
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/*
1209 * Remove an entry from a node directory.
1210 * This removes the leaf entry and the data entry,
1211 * and updates the free block if necessary.
1212 */
1213static int /* error */
1214xfs_dir2_leafn_remove(
1215 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001216 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int index, /* leaf entry index */
1218 xfs_da_state_blk_t *dblk, /* data block */
1219 int *rval) /* resulting block needs join */
1220{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001221 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001223 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 xfs_dir2_data_entry_t *dep; /* data block entry */
1225 xfs_inode_t *dp; /* incore directory inode */
1226 xfs_dir2_leaf_t *leaf; /* leaf structure */
1227 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1228 int longest; /* longest data free entry */
1229 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int needlog; /* need to log data header */
1231 int needscan; /* need to rescan data frees */
1232 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001233 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001234 struct xfs_dir3_icleaf_hdr leafhdr;
1235 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001237 trace_xfs_dir2_leafn_remove(args, index);
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dp = args->dp;
1240 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001241 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001242 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001243 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 /*
1246 * Point to the entry we're removing.
1247 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001248 lep = &ents[index];
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Extract the data block and offset from the entry.
1252 */
Dave Chinner30028032014-06-06 15:08:18 +10001253 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001255 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * Kill the leaf entry by marking it stale.
1260 * Log the leaf block changes.
1261 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001262 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001263 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001264 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001265
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001266 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001267 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * Make the data entry free. Keep track of the longest freespace
1271 * in the data block in case it changes.
1272 */
1273 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001274 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001275 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001276 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001277 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001279 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001280 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 /*
1282 * Rescan the data block freespaces for bestfree.
1283 * Log the data block header if needed.
1284 */
1285 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001286 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001288 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001289 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 /*
1291 * If the longest data block freespace changes, need to update
1292 * the corresponding freeblock entry.
1293 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001294 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001296 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 xfs_dir2_db_t fdb; /* freeblock block number */
1298 int findex; /* index in freeblock entries */
1299 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 /*
1302 * Convert the data block number to a free block,
1303 * read in the free block.
1304 */
Dave Chinner8f661932014-06-06 15:15:59 +10001305 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001306 error = xfs_dir2_free_read(tp, dp,
1307 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001308 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001309 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001311 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001312#ifdef DEBUG
1313 {
1314 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001315 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001316 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001317 (fdb - xfs_dir2_byte_to_db(args->geo,
1318 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001319 }
1320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 /*
1322 * Calculate which entry we need to fix.
1323 */
Dave Chinner8f661932014-06-06 15:15:59 +10001324 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001325 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /*
1327 * If the data block is now empty we can get rid of it
1328 * (usually).
1329 */
Dave Chinner8f661932014-06-06 15:15:59 +10001330 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001331 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * Try to punch out the data block.
1334 */
1335 error = xfs_dir2_shrink_inode(args, db, dbp);
1336 if (error == 0) {
1337 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001338 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340 /*
1341 * We can get ENOSPC if there's no space reservation.
1342 * In this case just drop the buffer and some one else
1343 * will eventually get rid of the empty block.
1344 */
Dave Chinner24513372014-06-25 14:58:08 +10001345 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return error;
1347 }
1348 /*
1349 * If we got rid of the data block, we can eliminate that entry
1350 * in the free block.
1351 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001352 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001353 fdb, findex, fbp, longest);
1354 if (error)
1355 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Dave Chinner20252072012-11-12 22:54:13 +11001357
Dave Chinner41419562013-10-29 22:11:50 +11001358 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001360 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * to justify trying to join it with a neighbor.
1362 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001363 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001364 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001365 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367}
1368
1369/*
1370 * Split the leaf entries in the old block into old and new blocks.
1371 */
1372int /* error */
1373xfs_dir2_leafn_split(
1374 xfs_da_state_t *state, /* btree cursor */
1375 xfs_da_state_blk_t *oldblk, /* original block */
1376 xfs_da_state_blk_t *newblk) /* newly created block */
1377{
1378 xfs_da_args_t *args; /* operation arguments */
1379 xfs_dablk_t blkno; /* new leaf block number */
1380 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001381 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
1384 * Allocate space for a new leaf node.
1385 */
1386 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001387 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1389 error = xfs_da_grow_inode(args, &blkno);
1390 if (error) {
1391 return error;
1392 }
1393 /*
1394 * Initialize the new leaf block.
1395 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001396 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001397 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1398 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 newblk->blkno = blkno;
1402 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1403 /*
1404 * Rebalance the entries across the two leaves, link the new
1405 * block into the leaves.
1406 */
1407 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001408 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (error) {
1410 return error;
1411 }
1412 /*
1413 * Insert the new entry in the correct block.
1414 */
1415 if (state->inleaf)
1416 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1417 else
1418 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1419 /*
1420 * Update last hashval in each block since we added the name.
1421 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001422 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1423 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001424 xfs_dir3_leaf_check(dp, oldblk->bp);
1425 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return error;
1427}
1428
1429/*
1430 * Check a leaf block and its neighbors to see if the block should be
1431 * collapsed into one or the other neighbor. Always keep the block
1432 * with the smaller block number.
1433 * If the current block is over 50% full, don't try to join it, return 0.
1434 * If the block is empty, fill in the state structure and return 2.
1435 * If it can be collapsed, fill in the state structure and return 1.
1436 * If nothing can be done, return 0.
1437 */
1438int /* error */
1439xfs_dir2_leafn_toosmall(
1440 xfs_da_state_t *state, /* btree cursor */
1441 int *action) /* resulting action to take */
1442{
1443 xfs_da_state_blk_t *blk; /* leaf block */
1444 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001445 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 int bytes; /* bytes in use */
1447 int count; /* leaf live entry count */
1448 int error; /* error return value */
1449 int forward; /* sibling block direction */
1450 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 xfs_dir2_leaf_t *leaf; /* leaf structure */
1452 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001453 struct xfs_dir3_icleaf_hdr leafhdr;
1454 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001455 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 /*
1458 * Check for the degenerate case of the block being over 50% full.
1459 * If so, it's not worth even looking to see if we might be able
1460 * to coalesce with a sibling.
1461 */
1462 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001463 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001464 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001465 ents = dp->d_ops->leaf_ents_p(leaf);
1466 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001467
1468 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001469 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001470 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * Blk over 50%, don't try to join.
1473 */
1474 *action = 0;
1475 return 0;
1476 }
1477 /*
1478 * Check for the degenerate case of the block being empty.
1479 * If the block is empty, we'll simply delete it, no need to
1480 * coalesce it with a sibling block. We choose (arbitrarily)
1481 * to merge with the forward block unless it is NULL.
1482 */
1483 if (count == 0) {
1484 /*
1485 * Make altpath point to the block we want to keep and
1486 * path point to the block we want to drop (this one).
1487 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001488 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001490 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 &rval);
1492 if (error)
1493 return error;
1494 *action = rval ? 2 : 0;
1495 return 0;
1496 }
1497 /*
1498 * Examine each sibling block to see if we can coalesce with
1499 * at least 25% free space to spare. We need to figure out
1500 * whether to merge with the forward or the backward block.
1501 * We prefer coalescing with the lower numbered sibling so as
1502 * to shrink a directory over time.
1503 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001504 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001506 struct xfs_dir3_icleaf_hdr hdr2;
1507
1508 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (blkno == 0)
1510 continue;
1511 /*
1512 * Read the sibling leaf block.
1513 */
Dave Chinner41419562013-10-29 22:11:50 +11001514 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001515 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001516 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 /*
1520 * Count bytes in the two blocks combined.
1521 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001522 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001523 bytes = state->args->geo->blksize -
1524 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001525
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001527 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001528 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001529 count += hdr2.count - hdr2.stale;
1530 bytes -= count * sizeof(ents[0]);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /*
1533 * Fits with at least 25% to spare.
1534 */
1535 if (bytes >= 0)
1536 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001537 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539 /*
1540 * Didn't like either block, give up.
1541 */
1542 if (i >= 2) {
1543 *action = 0;
1544 return 0;
1545 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * Make altpath point to the block we want to keep (the lower
1549 * numbered block) and path point to the block we want to drop.
1550 */
1551 memcpy(&state->altpath, &state->path, sizeof(state->path));
1552 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001553 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 &rval);
1555 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001556 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 &rval);
1558 if (error) {
1559 return error;
1560 }
1561 *action = rval ? 0 : 1;
1562 return 0;
1563}
1564
1565/*
1566 * Move all the leaf entries from drop_blk to save_blk.
1567 * This is done as part of a join operation.
1568 */
1569void
1570xfs_dir2_leafn_unbalance(
1571 xfs_da_state_t *state, /* cursor */
1572 xfs_da_state_blk_t *drop_blk, /* dead block */
1573 xfs_da_state_blk_t *save_blk) /* surviving block */
1574{
1575 xfs_da_args_t *args; /* operation arguments */
1576 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1577 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001578 struct xfs_dir3_icleaf_hdr savehdr;
1579 struct xfs_dir3_icleaf_hdr drophdr;
1580 struct xfs_dir2_leaf_entry *sents;
1581 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001582 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 args = state->args;
1585 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1586 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001587 drop_leaf = drop_blk->bp->b_addr;
1588 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001589
Dave Chinner01ba43b2013-10-29 22:11:52 +11001590 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1591 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1592 sents = dp->d_ops->leaf_ents_p(save_leaf);
1593 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /*
1596 * If there are any stale leaf entries, take this opportunity
1597 * to purge them.
1598 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001599 if (drophdr.stale)
1600 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1601 if (savehdr.stale)
1602 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 /*
1605 * Move the entries from drop to the appropriate end of save.
1606 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001607 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001608 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001609 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1610 save_blk->bp, &savehdr, sents, 0,
1611 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001613 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1614 save_blk->bp, &savehdr, sents,
1615 savehdr.count, drophdr.count);
1616 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1617
1618 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001619 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1620 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001621 xfs_dir3_leaf_log_header(args, save_blk->bp);
1622 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001623
Dave Chinner41419562013-10-29 22:11:50 +11001624 xfs_dir3_leaf_check(dp, save_blk->bp);
1625 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
1629 * Top-level node form directory addname routine.
1630 */
1631int /* error */
1632xfs_dir2_node_addname(
1633 xfs_da_args_t *args) /* operation arguments */
1634{
1635 xfs_da_state_blk_t *blk; /* leaf block for insert */
1636 int error; /* error return value */
1637 int rval; /* sub-return value */
1638 xfs_da_state_t *state; /* btree cursor */
1639
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001640 trace_xfs_dir2_node_addname(args);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /*
1643 * Allocate and initialize the state (btree cursor).
1644 */
1645 state = xfs_da_state_alloc();
1646 state->args = args;
1647 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * Look up the name. We're not supposed to find it, but
1650 * this gives us the insertion point.
1651 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001652 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (error)
1654 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001655 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 goto done;
1657 }
1658 /*
1659 * Add the data entry to a data block.
1660 * Extravalid is set to a freeblock found by lookup.
1661 */
1662 rval = xfs_dir2_node_addname_int(args,
1663 state->extravalid ? &state->extrablk : NULL);
1664 if (rval) {
1665 goto done;
1666 }
1667 blk = &state->path.blk[state->path.active - 1];
1668 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1669 /*
1670 * Add the new leaf entry.
1671 */
1672 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1673 if (rval == 0) {
1674 /*
1675 * It worked, fix the hash values up the btree.
1676 */
Barry Naujok6a178102008-05-21 16:42:05 +10001677 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001678 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 } else {
1680 /*
1681 * It didn't work, we need to split the leaf block.
1682 */
1683 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001684 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 goto done;
1686 }
1687 /*
1688 * Split the leaf block and insert the new entry.
1689 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001690 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692done:
1693 xfs_da_state_free(state);
1694 return rval;
1695}
1696
1697/*
1698 * Add the data entry for a node-format directory name addition.
1699 * The leaf entry is added in xfs_dir2_leafn_add.
1700 * We may enter with a freespace block that the lookup found.
1701 */
1702static int /* error */
1703xfs_dir2_node_addname_int(
1704 xfs_da_args_t *args, /* operation arguments */
1705 xfs_da_state_blk_t *fblk) /* optional freespace block */
1706{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001707 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001709 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1711 xfs_inode_t *dp; /* incore directory inode */
1712 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1713 int error; /* error return value */
1714 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001715 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int findex; /* freespace entry index */
1717 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1718 xfs_dir2_db_t ifbno; /* initial freespace block no */
1719 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1720 int length; /* length of the new entry */
1721 int logfree; /* need to log free entry */
1722 xfs_mount_t *mp; /* filesystem mount point */
1723 int needlog; /* need to log data header */
1724 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001725 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001727 __be16 *bests;
1728 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001729 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 dp = args->dp;
1732 mp = dp->i_mount;
1733 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001734 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
1736 * If we came in with a freespace block that means that lookup
1737 * found an entry with our hash value. This is the freespace
1738 * block for that data entry.
1739 */
1740 if (fblk) {
1741 fbp = fblk->bp;
1742 /*
1743 * Remember initial freespace block number.
1744 */
1745 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001746 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001748 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001749 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * This means the free entry showed that the data block had
1753 * space for our entry, so we remembered it.
1754 * Use that data block.
1755 */
1756 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001757 ASSERT(findex < freehdr.nvalid);
1758 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1759 ASSERT(be16_to_cpu(bests[findex]) >= length);
1760 dbno = freehdr.firstdb + findex;
1761 } else {
1762 /*
1763 * The data block looked at didn't have enough room.
1764 * We'll start at the beginning of the freespace entries.
1765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 dbno = -1;
1767 findex = 0;
1768 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001769 } else {
1770 /*
1771 * Didn't come in with a freespace block, so no data block.
1772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 ifbno = dbno = -1;
1774 fbp = NULL;
1775 findex = 0;
1776 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /*
1779 * If we don't have a data block yet, we're going to scan the
1780 * freespace blocks looking for one. Figure out what the
1781 * highest freespace block number is.
1782 */
1783 if (dbno == -1) {
1784 xfs_fileoff_t fo; /* freespace block number */
1785
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001786 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001788 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 fbno = ifbno;
1790 }
1791 /*
1792 * While we haven't identified a data block, search the freeblock
1793 * data for a good data block. If we find a null freeblock entry,
1794 * indicating a hole in the data blocks, remember that.
1795 */
1796 while (dbno == -1) {
1797 /*
1798 * If we don't have a freeblock in hand, get the next one.
1799 */
1800 if (fbp == NULL) {
1801 /*
1802 * Happens the first time through unless lookup gave
1803 * us a freespace block to start with.
1804 */
1805 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001806 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001807 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * If it's ifbno we already looked at it.
1810 */
1811 if (fbno == ifbno)
1812 fbno++;
1813 /*
1814 * If it's off the end we're done.
1815 */
1816 if (fbno >= lastfbno)
1817 break;
1818 /*
1819 * Read the block. There can be holes in the
1820 * freespace blocks, so this might not succeed.
1821 * This should be really rare, so there's no reason
1822 * to avoid it.
1823 */
Dave Chinner20252072012-11-12 22:54:13 +11001824 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001825 xfs_dir2_db_to_da(args->geo, fbno),
1826 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001827 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001829 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001831 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 findex = 0;
1833 }
1834 /*
1835 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001836 *
1837 * The bests initialisation should be where the bufer is read in
1838 * the above branch. But gcc is too stupid to realise that bests
1839 * and the freehdr are actually initialised if they are placed
1840 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001842 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001843 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001844 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1845 be16_to_cpu(bests[findex]) >= length)
1846 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 else {
1848 /*
1849 * Are we done with the freeblock?
1850 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001851 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * Drop the block.
1854 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001855 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 fbp = NULL;
1857 if (fblk && fblk->bp)
1858 fblk->bp = NULL;
1859 }
1860 }
1861 }
1862 /*
1863 * If we don't have a data block, we need to allocate one and make
1864 * the freespace entries refer to it.
1865 */
1866 if (unlikely(dbno == -1)) {
1867 /*
1868 * Not allowed to allocate, return failure.
1869 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001870 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001871 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 /*
1874 * Allocate and initialize the new data block.
1875 */
1876 if (unlikely((error = xfs_dir2_grow_inode(args,
1877 XFS_DIR2_DATA_SPACE,
1878 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001879 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /*
1883 * If (somehow) we have a freespace block, get rid of it.
1884 */
1885 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001886 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (fblk && fblk->bp)
1888 fblk->bp = NULL;
1889
1890 /*
1891 * Get the freespace block corresponding to the data block
1892 * that was just allocated.
1893 */
Dave Chinner8f661932014-06-06 15:15:59 +10001894 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001895 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001896 xfs_dir2_db_to_da(args->geo, fbno),
1897 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001898 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /*
1902 * If there wasn't a freespace block, the read will
1903 * return a NULL fbp. Allocate and initialize a new one.
1904 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001905 if (!fbp) {
1906 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1907 &fbno);
1908 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Dave Chinner8f661932014-06-06 15:15:59 +10001911 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001912 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001913"%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 +11001914 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001915 (long long)dp->d_ops->db_to_fdb(
1916 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 (long long)dbno, (long long)fbno,
1918 (unsigned long long)ifbno, lastfbno);
1919 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001920 xfs_alert(mp,
1921 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 fblk,
1923 (unsigned long long)fblk->blkno,
1924 fblk->index,
1925 fblk->magic);
1926 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001927 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1930 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001931 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
1934 /*
1935 * Get a buffer for the new block.
1936 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001937 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001938 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001940 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001941 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001942 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001945 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001947 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001948 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001949 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001950 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001952 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001953 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001954 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 /*
1958 * Set the freespace block index from the data block number.
1959 */
Dave Chinner8f661932014-06-06 15:15:59 +10001960 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * If it's after the end of the current entries in the
1963 * freespace block, extend that table.
1964 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001965 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001966 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001967 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
1969 * Tag new entry so nused will go up.
1970 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001971 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 }
1973 /*
1974 * If this entry was for an empty data block
1975 * (this should always be true) then update the header.
1976 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001977 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1978 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001979 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001980 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 /*
1983 * Update the real value in the table.
1984 * We haven't allocated the data entry yet so this will
1985 * change again.
1986 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001987 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001988 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001989 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 logfree = 1;
1991 }
1992 /*
1993 * We had a data block so we don't have to make a new one.
1994 */
1995 else {
1996 /*
1997 * If just checking, we succeeded.
1998 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001999 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /*
2003 * Read the data block in.
2004 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002005 error = xfs_dir3_data_read(tp, dp,
2006 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11002007 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002008 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002010 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11002011 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 logfree = 0;
2013 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002014 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
2016 * Point to the existing unused space.
2017 */
2018 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002019 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 needscan = needlog = 0;
2021 /*
2022 * Mark the first part of the unused space, inuse for us.
2023 */
Dave Chinnerbc851782014-06-06 15:20:54 +10002024 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002025 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 &needlog, &needscan);
2027 /*
2028 * Fill in the new entry and log it.
2029 */
2030 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002031 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 dep->namelen = args->namelen;
2033 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002034 dp->d_ops->data_put_ftype(dep, args->filetype);
2035 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002036 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002037 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * Rescan the block for bestfree if needed.
2040 */
2041 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002042 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
2044 * Log the data block header if needed.
2045 */
2046 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002047 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
2049 * If the freespace entry is now wrong, update it.
2050 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002051 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002052 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2053 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 logfree = 1;
2055 }
2056 /*
2057 * Log the freespace entry if needed.
2058 */
2059 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002060 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * Return the data block and offset in args, then drop the data block.
2063 */
2064 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002065 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
2067}
2068
2069/*
2070 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002071 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 * The only real output is the inode number of the entry.
2073 */
2074int /* error */
2075xfs_dir2_node_lookup(
2076 xfs_da_args_t *args) /* operation arguments */
2077{
2078 int error; /* error return value */
2079 int i; /* btree level */
2080 int rval; /* operation return value */
2081 xfs_da_state_t *state; /* btree cursor */
2082
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002083 trace_xfs_dir2_node_lookup(args);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Allocate and initialize the btree cursor.
2087 */
2088 state = xfs_da_state_alloc();
2089 state->args = args;
2090 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /*
2092 * Fill in the path to the entry in the cursor.
2093 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002094 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (error)
2096 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002097 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2098 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002099 xfs_dir2_data_entry_t *dep;
2100
Dave Chinner1d9025e2012-06-22 18:50:14 +10002101 dep = (xfs_dir2_data_entry_t *)
2102 ((char *)state->extrablk.bp->b_addr +
2103 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002104 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Release the btree blocks and leaf block.
2108 */
2109 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002110 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 state->path.blk[i].bp = NULL;
2112 }
2113 /*
2114 * Release the data block if we have it.
2115 */
2116 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002117 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 state->extrablk.bp = NULL;
2119 }
2120 xfs_da_state_free(state);
2121 return rval;
2122}
2123
2124/*
2125 * Remove an entry from a node-format directory.
2126 */
2127int /* error */
2128xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002129 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002131 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int error; /* error return value */
2133 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002134 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002136 trace_xfs_dir2_node_removename(args);
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * Allocate and initialize the btree cursor.
2140 */
2141 state = xfs_da_state_alloc();
2142 state->args = args;
2143 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002144
2145 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002146 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002147 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002148 goto out_free;
2149
2150 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002151 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002152 error = rval;
2153 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 blk = &state->path.blk[state->path.active - 1];
2157 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2158 ASSERT(state->extravalid);
2159 /*
2160 * Remove the leaf and data entries.
2161 * Extrablk refers to the data block.
2162 */
2163 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2164 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002165 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002166 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 /*
2168 * Fix the hash values up the btree.
2169 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002170 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /*
2172 * If we need to join leaf blocks, do it.
2173 */
2174 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002175 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * If no errors so far, try conversion to leaf format.
2178 */
2179 if (!error)
2180 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002181out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 xfs_da_state_free(state);
2183 return error;
2184}
2185
2186/*
2187 * Replace an entry's inode number in a node-format directory.
2188 */
2189int /* error */
2190xfs_dir2_node_replace(
2191 xfs_da_args_t *args) /* operation arguments */
2192{
2193 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002194 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 xfs_dir2_data_entry_t *dep; /* data entry changed */
2196 int error; /* error return value */
2197 int i; /* btree level */
2198 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002199 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 xfs_dir2_leaf_t *leaf; /* leaf structure */
2201 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2202 int rval; /* internal return value */
2203 xfs_da_state_t *state; /* btree cursor */
2204
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002205 trace_xfs_dir2_node_replace(args);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Allocate and initialize the btree cursor.
2209 */
2210 state = xfs_da_state_alloc();
2211 state->args = args;
2212 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002213
2214 /*
2215 * We have to save new inode number and ftype since
2216 * xfs_da3_node_lookup_int() is going to overwrite them
2217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002219 ftype = args->filetype;
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /*
2222 * Lookup the entry to change in the btree.
2223 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002224 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (error) {
2226 rval = error;
2227 }
2228 /*
2229 * It should be found, since the vnodeops layer has looked it up
2230 * and locked it. But paranoia is good.
2231 */
Dave Chinner24513372014-06-25 14:58:08 +10002232 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002233 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /*
2235 * Find the leaf entry.
2236 */
2237 blk = &state->path.blk[state->path.active - 1];
2238 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002239 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002240 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002241 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ASSERT(state->extravalid);
2243 /*
2244 * Point to the data entry.
2245 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002246 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002247 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2248 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002250 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002251 xfs_dir2_dataptr_to_off(args->geo,
2252 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002253 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /*
2255 * Fill in the new inode number and log the entry.
2256 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002257 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002258 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002259 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 rval = 0;
2261 }
2262 /*
2263 * Didn't find it, and we're holding a data block. Drop it.
2264 */
2265 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002266 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 state->extrablk.bp = NULL;
2268 }
2269 /*
2270 * Release all the buffers in the cursor.
2271 */
2272 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002273 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 state->path.blk[i].bp = NULL;
2275 }
2276 xfs_da_state_free(state);
2277 return rval;
2278}
2279
2280/*
2281 * Trim off a trailing empty freespace block.
2282 * Return (in rvalp) 1 if we did it, 0 if not.
2283 */
2284int /* error */
2285xfs_dir2_node_trim_free(
2286 xfs_da_args_t *args, /* operation arguments */
2287 xfs_fileoff_t fo, /* free block number */
2288 int *rvalp) /* out: did something */
2289{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002290 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 xfs_inode_t *dp; /* incore directory inode */
2292 int error; /* error return code */
2293 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002295 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002299
2300 *rvalp = 0;
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Read the freespace block.
2304 */
Dave Chinner20252072012-11-12 22:54:13 +11002305 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002306 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /*
2309 * There can be holes in freespace. If fo is a hole, there's
2310 * nothing to do.
2311 */
Dave Chinner20252072012-11-12 22:54:13 +11002312 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002314 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002315 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
2318 * If there are used entries, there's nothing to do.
2319 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002320 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002321 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 0;
2323 }
2324 /*
2325 * Blow the block away.
2326 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002327 error = xfs_dir2_shrink_inode(args,
2328 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2329 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /*
2331 * Can't fail with ENOSPC since that only happens with no
2332 * space reservation, when breaking up an extent into two
2333 * pieces. This is the last block of an extent.
2334 */
Dave Chinner24513372014-06-25 14:58:08 +10002335 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002336 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 return error;
2338 }
2339 /*
2340 * Return that we succeeded.
2341 */
2342 *rvalp = 1;
2343 return 0;
2344}