blob: 051a21f3ad06f910b66cc0bf26df10c381c6c701 [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;
127
Eric Sandeence5028c2014-02-27 15:23:10 +1100128 if (xfs_sb_version_hascrc(&mp->m_sb) &&
129 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800130 xfs_verifier_error(bp, -EFSBADCRC);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800131 else if (xfs_dir3_free_verify(bp))
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800132 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100133}
Dave Chinner20252072012-11-12 22:54:13 +1100134
Dave Chinner612cfbf2012-11-14 17:52:32 +1100135static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100136xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100137 struct xfs_buf *bp)
138{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100139 struct xfs_mount *mp = bp->b_target->bt_mount;
140 struct xfs_buf_log_item *bip = bp->b_fspriv;
141 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
142
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800143 if (xfs_dir3_free_verify(bp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800144 xfs_verifier_error(bp, -EFSCORRUPTED);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100145 return;
146 }
147
148 if (!xfs_sb_version_hascrc(&mp->m_sb))
149 return;
150
151 if (bip)
152 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
153
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100154 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100155}
156
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100157const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100158 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100159 .verify_read = xfs_dir3_free_read_verify,
160 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100161};
Dave Chinner20252072012-11-12 22:54:13 +1100162
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800163/* Everything ok in the free block header? */
164static bool
165xfs_dir3_free_header_check(
166 struct xfs_inode *dp,
167 xfs_dablk_t fbno,
168 struct xfs_buf *bp)
169{
170 struct xfs_mount *mp = dp->i_mount;
171 unsigned int firstdb;
172 int maxbests;
173
174 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
175 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
176 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
177 maxbests;
178 if (xfs_sb_version_hascrc(&mp->m_sb)) {
179 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
180
181 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800182 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800183 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800184 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800185 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800186 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800187 } else {
188 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
189
190 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800191 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800192 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800193 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800194 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800195 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800196 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800197 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800198}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100199
Dave Chinner20252072012-11-12 22:54:13 +1100200static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100201__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100202 struct xfs_trans *tp,
203 struct xfs_inode *dp,
204 xfs_dablk_t fbno,
205 xfs_daddr_t mappedbno,
206 struct xfs_buf **bpp)
207{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100208 int err;
209
210 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100211 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800212 if (err || !*bpp)
213 return err;
214
215 /* Check things that we can't do in the verifier. */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800216 if (xfs_dir3_free_header_check(dp, fbno, *bpp)) {
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800217 xfs_verifier_error(*bpp, -EFSCORRUPTED);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800218 xfs_trans_brelse(tp, *bpp);
219 return -EFSCORRUPTED;
220 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100221
222 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800223 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100224 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800225
226 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100227}
228
229int
230xfs_dir2_free_read(
231 struct xfs_trans *tp,
232 struct xfs_inode *dp,
233 xfs_dablk_t fbno,
234 struct xfs_buf **bpp)
235{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100236 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100237}
238
239static int
240xfs_dir2_free_try_read(
241 struct xfs_trans *tp,
242 struct xfs_inode *dp,
243 xfs_dablk_t fbno,
244 struct xfs_buf **bpp)
245{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100246 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
247}
248
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100249static int
250xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000251 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100252 xfs_dir2_db_t fbno,
253 struct xfs_buf **bpp)
254{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000255 struct xfs_trans *tp = args->trans;
256 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100257 struct xfs_mount *mp = dp->i_mount;
258 struct xfs_buf *bp;
259 int error;
260 struct xfs_dir3_icfree_hdr hdr;
261
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000262 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100263 -1, &bp, XFS_DATA_FORK);
264 if (error)
265 return error;
266
Dave Chinner61fe1352013-04-03 16:11:30 +1100267 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100268 bp->b_ops = &xfs_dir3_free_buf_ops;
269
270 /*
271 * Initialize the new block to be empty, and remember
272 * its first slot as our empty slot.
273 */
Dave Chinnere400d272013-05-28 18:37:17 +1000274 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
275 memset(&hdr, 0, sizeof(hdr));
276
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100277 if (xfs_sb_version_hascrc(&mp->m_sb)) {
278 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
279
280 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000281
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100282 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
283 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000284 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000285 } else
286 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100287 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100288 *bpp = bp;
289 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
293 * Log entries from a freespace block.
294 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000295STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000297 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000298 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int first, /* first entry to log */
300 int last) /* last entry to log */
301{
302 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100303 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dave Chinner1d9025e2012-06-22 18:50:14 +1000305 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000306 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100307 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
308 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000309 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100310 (uint)((char *)&bests[first] - (char *)free),
311 (uint)((char *)&bests[last] - (char *)free +
312 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*
316 * Log header from a freespace block.
317 */
318static void
319xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000320 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000321 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Dave Chinner836a94a2013-08-12 20:49:44 +1000323#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 xfs_dir2_free_t *free; /* freespace structure */
325
Dave Chinner1d9025e2012-06-22 18:50:14 +1000326 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100327 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
328 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000329#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000330 xfs_trans_log_buf(args->trans, bp, 0,
331 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
335 * Convert a leaf-format directory to a node-format directory.
336 * We need to change the magic number of the leaf block, and copy
337 * the freespace table out of the leaf block into its own block.
338 */
339int /* error */
340xfs_dir2_leaf_to_node(
341 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000342 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 xfs_inode_t *dp; /* incore directory inode */
345 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000346 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 xfs_dir2_db_t fdb; /* freespace block number */
348 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100349 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 int i; /* leaf freespace index */
351 xfs_dir2_leaf_t *leaf; /* leaf structure */
352 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 int n; /* count of live freespc ents */
354 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100355 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100357 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000359 trace_xfs_dir2_leaf_to_node(args);
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 tp = args->trans;
363 /*
364 * Add a freespace block to the directory.
365 */
366 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
367 return error;
368 }
Dave Chinner30028032014-06-06 15:08:18 +1000369 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Get the buffer for the new freespace block.
372 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000373 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100376
Dave Chinner1d9025e2012-06-22 18:50:14 +1000377 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100378 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000379 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000380 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100381 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000382 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 /*
385 * Copy freespace entries from the leaf block to the new block.
386 * Count active entries.
387 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100388 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500389 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100390 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100391 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100393 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100397 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100399 freehdr.nused = n;
400 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
401
Dave Chinner01ba43b2013-10-29 22:11:52 +1100402 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000403 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
404 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100405
Dave Chinner24df33b2013-04-12 07:30:21 +1000406 /*
407 * Converting the leaf to a leafnode is just a matter of changing the
408 * magic number and the ops. Do the change directly to the buffer as
409 * it's less work (and less code) than decoding the header to host
410 * format and back again.
411 */
412 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
413 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
414 else
415 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
416 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100417 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000418 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100419 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return 0;
421}
422
423/*
424 * Add a leaf entry to a leaf block in a node-form directory.
425 * The other work necessary is done from the caller.
426 */
427static int /* error */
428xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000429 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 xfs_da_args_t *args, /* operation arguments */
431 int index) /* insertion pt for new entry */
432{
433 int compact; /* compacting stale leaves */
434 xfs_inode_t *dp; /* incore directory inode */
435 int highstale; /* next stale entry */
436 xfs_dir2_leaf_t *leaf; /* leaf structure */
437 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
438 int lfloghigh; /* high leaf entry logging */
439 int lfloglow; /* low leaf entry logging */
440 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000441 struct xfs_dir3_icleaf_hdr leafhdr;
442 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000444 trace_xfs_dir2_leafn_add(args, index);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000447 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100448 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100449 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * Quick check just to make sure we are not going to index
453 * into other peoples memory
454 */
455 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000456 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /*
459 * If there are already the maximum number of leaf entries in
460 * the block, if there are no stale entries it won't fit.
461 * Caller will do a split. If there are stale entries we'll do
462 * a compact.
463 */
464
Dave Chinner8f661932014-06-06 15:15:59 +1000465 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000466 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000467 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000468 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 } else
470 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000471 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
472 ASSERT(index == leafhdr.count ||
473 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Barry Naujok6a178102008-05-21 16:42:05 +1000475 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
477
478 /*
479 * Compact out all but one stale leaf entry. Leaves behind
480 * the entry closest to index.
481 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000482 if (compact)
483 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
484 &highstale, &lfloglow, &lfloghigh);
485 else if (leafhdr.stale) {
486 /*
487 * Set impossible logging indices for this case.
488 */
489 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 lfloghigh = -1;
491 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Insert the new entry, log everything.
495 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000496 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200497 highstale, &lfloglow, &lfloghigh);
498
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100499 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000500 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100501 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000502
Dave Chinner01ba43b2013-10-29 22:11:52 +1100503 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000504 xfs_dir3_leaf_log_header(args, bp);
505 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100506 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 0;
508}
509
510#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100511static void
512xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100513 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100514 struct xfs_buf *bp,
515 xfs_dir2_db_t db)
516{
517 struct xfs_dir3_icfree_hdr hdr;
518
Dave Chinner01ba43b2013-10-29 22:11:52 +1100519 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100520
Dave Chinner8f661932014-06-06 15:15:59 +1000521 ASSERT((hdr.firstdb %
522 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100523 ASSERT(hdr.firstdb <= db);
524 ASSERT(db < hdr.firstdb + hdr.nvalid);
525}
526#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100527#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#endif /* DEBUG */
529
530/*
531 * Return the last hash value in the leaf.
532 * Stale entries are ok.
533 */
534xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700535xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100536 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000537 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int *count) /* count of entries in leaf */
539{
Dave Chinner24df33b2013-04-12 07:30:21 +1000540 struct xfs_dir2_leaf *leaf = bp->b_addr;
541 struct xfs_dir2_leaf_entry *ents;
542 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Chinner01ba43b2013-10-29 22:11:52 +1100544 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000545
546 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700547 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
548 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
549 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000552 *count = leafhdr.count;
553 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000555
Dave Chinner41419562013-10-29 22:11:50 +1100556 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000557 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000561 * Look up a leaf entry for space to add a name in a node-format leaf block.
562 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000564STATIC int
565xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000566 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_da_args_t *args, /* operation arguments */
568 int *indexp, /* out: leaf entry index */
569 xfs_da_state_t *state) /* state to fill in */
570{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000571 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000572 xfs_dir2_db_t curdb = -1; /* current data block number */
573 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xfs_inode_t *dp; /* incore directory inode */
575 int error; /* error return value */
576 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000577 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int index; /* leaf entry index */
579 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000580 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
582 xfs_mount_t *mp; /* filesystem mount point */
583 xfs_dir2_db_t newdb; /* new data block number */
584 xfs_dir2_db_t newfdb; /* new free block number */
585 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000586 struct xfs_dir2_leaf_entry *ents;
587 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 dp = args->dp;
590 tp = args->trans;
591 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000592 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100593 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100594 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000595
Dave Chinner41419562013-10-29 22:11:50 +1100596 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000597 ASSERT(leafhdr.count > 0);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
600 * Look up the hash value in the leaf entries.
601 */
602 index = xfs_dir2_leaf_search_hash(args, bp);
603 /*
604 * Do we have a buffer coming in?
605 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000606 if (state->extravalid) {
607 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000609 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000610 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100611 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
612 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100614 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * Loop over leaf entries with the right hash value.
617 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000618 for (lep = &ents[index];
619 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
620 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 /*
622 * Skip stale leaf entries.
623 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100624 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 continue;
626 /*
627 * Pull the data block number from the entry.
628 */
Dave Chinner30028032014-06-06 15:08:18 +1000629 newdb = xfs_dir2_dataptr_to_db(args->geo,
630 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /*
632 * For addname, we're looking for a place to put the new entry.
633 * We want to use a data block with an entry of equal
634 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 *
636 * If this block isn't the data block we already have
637 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100640 __be16 *bests;
641
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 * Convert the data block to the free block
645 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Dave Chinner8f661932014-06-06 15:15:59 +1000647 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000649 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000651 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000653 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000656 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100657
658 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000659 xfs_dir2_db_to_da(args->geo,
660 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100661 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000664 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100665
Dave Chinner01ba43b2013-10-29 22:11:52 +1100666 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000669 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
Dave Chinner8f661932014-06-06 15:15:59 +1000671 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000673 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500675 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100676 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
678 XFS_ERRLEVEL_LOW, mp);
679 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000680 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000681 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100684 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000685 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000688 /* Didn't find any space */
689 fi = -1;
690out:
Barry Naujok6a178102008-05-21 16:42:05 +1000691 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000692 if (curbp) {
693 /* Giving back a free block. */
694 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000696 state->extrablk.index = fi;
697 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100698
699 /*
700 * Important: this magic number is not in the buffer - it's for
701 * buffer type information and therefore only the free/data type
702 * matters here, not whether CRCs are enabled or not.
703 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000704 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
705 } else {
706 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000709 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
711 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000712 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000716 * Look up a leaf entry in a node-format leaf block.
717 * The extrablk in state a data block.
718 */
719STATIC int
720xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000721 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000722 xfs_da_args_t *args, /* operation arguments */
723 int *indexp, /* out: leaf entry index */
724 xfs_da_state_t *state) /* state to fill in */
725{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000726 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000727 xfs_dir2_db_t curdb = -1; /* current data block number */
728 xfs_dir2_data_entry_t *dep; /* data block entry */
729 xfs_inode_t *dp; /* incore directory inode */
730 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731 int index; /* leaf entry index */
732 xfs_dir2_leaf_t *leaf; /* leaf structure */
733 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
734 xfs_mount_t *mp; /* filesystem mount point */
735 xfs_dir2_db_t newdb; /* new data block number */
736 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000737 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000738 struct xfs_dir2_leaf_entry *ents;
739 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000740
741 dp = args->dp;
742 tp = args->trans;
743 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000744 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100745 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100746 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000747
Dave Chinner41419562013-10-29 22:11:50 +1100748 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000749 ASSERT(leafhdr.count > 0);
750
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000751 /*
752 * Look up the hash value in the leaf entries.
753 */
754 index = xfs_dir2_leaf_search_hash(args, bp);
755 /*
756 * Do we have a buffer coming in?
757 */
758 if (state->extravalid) {
759 curbp = state->extrablk.bp;
760 curdb = state->extrablk.blkno;
761 }
762 /*
763 * Loop over leaf entries with the right hash value.
764 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000765 for (lep = &ents[index];
766 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
767 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000768 /*
769 * Skip stale leaf entries.
770 */
771 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
772 continue;
773 /*
774 * Pull the data block number from the entry.
775 */
Dave Chinner30028032014-06-06 15:08:18 +1000776 newdb = xfs_dir2_dataptr_to_db(args->geo,
777 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000778 /*
779 * Not adding a new entry, so we really want to find
780 * the name given to us.
781 *
782 * If it's a different data block, go get it.
783 */
784 if (newdb != curdb) {
785 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000786 * If we had a block before that we aren't saving
787 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000788 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000789 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
790 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000792 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000793 * If needing the block that is saved with a CI match,
794 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000795 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000796 if (args->cmpresult != XFS_CMP_DIFFERENT &&
797 newdb == state->extrablk.blkno) {
798 ASSERT(state->extravalid);
799 curbp = state->extrablk.bp;
800 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100801 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000802 xfs_dir2_db_to_da(args->geo,
803 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100804 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000805 if (error)
806 return error;
807 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100808 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809 curdb = newdb;
810 }
811 /*
812 * Point to the data entry.
813 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000814 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000815 xfs_dir2_dataptr_to_off(args->geo,
816 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000817 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000818 * Compare the entry and if it's an exact match, return
819 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000820 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000821 */
Barry Naujok5163f952008-05-21 16:41:01 +1000822 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
823 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000824 /* If there is a CI match block, drop it */
825 if (args->cmpresult != XFS_CMP_DIFFERENT &&
826 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000827 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000828 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000829 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100830 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000831 *indexp = index;
832 state->extravalid = 1;
833 state->extrablk.bp = curbp;
834 state->extrablk.blkno = curdb;
835 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000836 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000837 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100838 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100839 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000840 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000841 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000842 }
843 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000844 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000845 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000846 if (args->cmpresult == XFS_CMP_DIFFERENT) {
847 /* Giving back last used data block. */
848 state->extravalid = 1;
849 state->extrablk.bp = curbp;
850 state->extrablk.index = -1;
851 state->extrablk.blkno = curdb;
852 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100853 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100854 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000855 } else {
856 /* If the curbp is not the CI match block, drop it */
857 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000858 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000859 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000860 } else {
861 state->extravalid = 0;
862 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000863 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000864 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000865}
866
867/*
868 * Look up a leaf entry in a node-format leaf block.
869 * If this is an addname then the extrablk in state is a freespace block,
870 * otherwise it's a data block.
871 */
872int
873xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000874 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000875 xfs_da_args_t *args, /* operation arguments */
876 int *indexp, /* out: leaf entry index */
877 xfs_da_state_t *state) /* state to fill in */
878{
Barry Naujok6a178102008-05-21 16:42:05 +1000879 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000880 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
881 state);
882 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
883}
884
885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Move count leaf entries from source to destination leaf.
887 * Log entries and headers. Stale entries are preserved.
888 */
889static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000890xfs_dir3_leafn_moveents(
891 xfs_da_args_t *args, /* operation arguments */
892 struct xfs_buf *bp_s, /* source */
893 struct xfs_dir3_icleaf_hdr *shdr,
894 struct xfs_dir2_leaf_entry *sents,
895 int start_s,/* source leaf index */
896 struct xfs_buf *bp_d, /* destination */
897 struct xfs_dir3_icleaf_hdr *dhdr,
898 struct xfs_dir2_leaf_entry *dents,
899 int start_d,/* destination leaf index */
900 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Dave Chinner24df33b2013-04-12 07:30:21 +1000902 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000904 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Silently return if nothing to do.
908 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000909 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 /*
913 * If the destination index is not the end of the current
914 * destination leaf entries, open up a hole in the destination
915 * to hold the new entries.
916 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 if (start_d < dhdr->count) {
918 memmove(&dents[start_d + count], &dents[start_d],
919 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000920 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000921 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 /*
924 * If the source has stale leaves, count the ones in the copy range
925 * so we can update the header correctly.
926 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000927 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int i; /* temp leaf index */
929
930 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000931 if (sents[i].address ==
932 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 stale++;
934 }
935 } else
936 stale = 0;
937 /*
938 * Copy the leaf entries from source to destination.
939 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000940 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000942 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
945 * If there are source entries after the ones we copied,
946 * delete the ones we copied by sliding the next ones down.
947 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000948 if (start_s + count < shdr->count) {
949 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000951 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * Update the headers and log them.
956 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 shdr->count -= count;
958 shdr->stale -= stale;
959 dhdr->count += count;
960 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963/*
964 * Determine the sort order of two leaf blocks.
965 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
966 */
967int /* sort order */
968xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100969 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000970 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
971 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Dave Chinner24df33b2013-04-12 07:30:21 +1000973 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
974 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
975 struct xfs_dir2_leaf_entry *ents1;
976 struct xfs_dir2_leaf_entry *ents2;
977 struct xfs_dir3_icleaf_hdr hdr1;
978 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Dave Chinner01ba43b2013-10-29 22:11:52 +1100980 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
981 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100982 ents1 = dp->d_ops->leaf_ents_p(leaf1);
983 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000984
985 if (hdr1.count > 0 && hdr2.count > 0 &&
986 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
987 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
988 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 1;
990 return 0;
991}
992
993/*
994 * Rebalance leaf entries between two leaf blocks.
995 * This is actually only called when the second block is new,
996 * though the code deals with the general case.
997 * A new entry will be inserted in one of the blocks, and that
998 * entry is taken into account when balancing.
999 */
1000static void
1001xfs_dir2_leafn_rebalance(
1002 xfs_da_state_t *state, /* btree cursor */
1003 xfs_da_state_blk_t *blk1, /* first btree block */
1004 xfs_da_state_blk_t *blk2) /* second btree block */
1005{
1006 xfs_da_args_t *args; /* operation arguments */
1007 int count; /* count (& direction) leaves */
1008 int isleft; /* new goes in left leaf */
1009 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1010 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1011 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001012#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 int oldstale; /* old count of stale leaves */
1014#endif
1015 int oldsum; /* old total leaf count */
1016 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001017 struct xfs_dir2_leaf_entry *ents1;
1018 struct xfs_dir2_leaf_entry *ents2;
1019 struct xfs_dir3_icleaf_hdr hdr1;
1020 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001021 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 args = state->args;
1024 /*
1025 * If the block order is wrong, swap the arguments.
1026 */
Dave Chinner41419562013-10-29 22:11:50 +11001027 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 xfs_da_state_blk_t *tmp; /* temp for block swap */
1029
1030 tmp = blk1;
1031 blk1 = blk2;
1032 blk2 = tmp;
1033 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001034 leaf1 = blk1->bp->b_addr;
1035 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001036 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1037 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001038 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1039 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001040
1041 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001042#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001043 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044#endif
1045 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * If the old leaf count was odd then the new one will be even,
1049 * so we need to divide the new count evenly.
1050 */
1051 if (oldsum & 1) {
1052 xfs_dahash_t midhash; /* middle entry hash value */
1053
Dave Chinner24df33b2013-04-12 07:30:21 +10001054 if (mid >= hdr1.count)
1055 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001057 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 isleft = args->hashval <= midhash;
1059 }
1060 /*
1061 * If the old count is even then the new count is odd, so there's
1062 * no preferred side for the new entry.
1063 * Pick the left one.
1064 */
1065 else
1066 isleft = 1;
1067 /*
1068 * Calculate moved entry count. Positive means left-to-right,
1069 * negative means right-to-left. Then move the entries.
1070 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001071 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001073 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1074 hdr1.count - count, blk2->bp,
1075 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001077 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1078 blk1->bp, &hdr1, ents1,
1079 hdr1.count, count);
1080
1081 ASSERT(hdr1.count + hdr2.count == oldsum);
1082 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1083
1084 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001085 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1086 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001087 xfs_dir3_leaf_log_header(args, blk1->bp);
1088 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001089
Dave Chinner41419562013-10-29 22:11:50 +11001090 xfs_dir3_leaf_check(dp, blk1->bp);
1091 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
1094 * Mark whether we're inserting into the old or new leaf.
1095 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001096 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001098 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 state->inleaf = !swap;
1100 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001101 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Adjust the expected index for insertion.
1104 */
1105 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001106 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001107
1108 /*
1109 * Finally sanity check just to make sure we are not returning a
1110 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Dave Chinner41419562013-10-29 22:11:50 +11001112 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 state->inleaf = 1;
1114 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001115 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001116 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001117 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
1120
Dave Chinner20252072012-11-12 22:54:13 +11001121static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001122xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001123 xfs_da_args_t *args,
1124 struct xfs_dir2_data_hdr *hdr,
1125 struct xfs_dir2_free *free,
1126 xfs_dir2_db_t fdb,
1127 int findex,
1128 struct xfs_buf *fbp,
1129 int longest)
1130{
Dave Chinner20252072012-11-12 22:54:13 +11001131 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001132 __be16 *bests;
1133 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001134 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001135
Dave Chinner01ba43b2013-10-29 22:11:52 +11001136 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001137 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001138 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001139 /*
1140 * Data block is not empty, just set the free entry to the new
1141 * value.
1142 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001143 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001144 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001145 return 0;
1146 }
1147
1148 /* One less used entry in the free table. */
1149 freehdr.nused--;
1150
1151 /*
1152 * If this was the last entry in the table, we can trim the table size
1153 * back. There might be other entries at the end referring to
1154 * non-existent data blocks, get those too.
1155 */
1156 if (findex == freehdr.nvalid - 1) {
1157 int i; /* free entry index */
1158
1159 for (i = findex - 1; i >= 0; i--) {
1160 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1161 break;
1162 }
1163 freehdr.nvalid = i + 1;
1164 logfree = 0;
1165 } else {
1166 /* Not the last entry, just punch it out. */
1167 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001168 logfree = 1;
1169 }
1170
Dave Chinner01ba43b2013-10-29 22:11:52 +11001171 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001172 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001173
1174 /*
1175 * If there are no useful entries left in the block, get rid of the
1176 * block if we can.
1177 */
1178 if (!freehdr.nused) {
1179 int error;
1180
1181 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1182 if (error == 0) {
1183 fbp = NULL;
1184 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001185 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001186 return error;
1187 /*
1188 * It's possible to get ENOSPC if there is no
1189 * space reservation. In this case some one
1190 * else will eventually get rid of this block.
1191 */
1192 }
1193
Dave Chinner20252072012-11-12 22:54:13 +11001194 /* Log the free entry that changed, unless we got rid of it. */
1195 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001196 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001197 return 0;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/*
1201 * Remove an entry from a node directory.
1202 * This removes the leaf entry and the data entry,
1203 * and updates the free block if necessary.
1204 */
1205static int /* error */
1206xfs_dir2_leafn_remove(
1207 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001208 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int index, /* leaf entry index */
1210 xfs_da_state_blk_t *dblk, /* data block */
1211 int *rval) /* resulting block needs join */
1212{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001213 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001215 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 xfs_dir2_data_entry_t *dep; /* data block entry */
1217 xfs_inode_t *dp; /* incore directory inode */
1218 xfs_dir2_leaf_t *leaf; /* leaf structure */
1219 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1220 int longest; /* longest data free entry */
1221 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int needlog; /* need to log data header */
1223 int needscan; /* need to rescan data frees */
1224 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001225 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001226 struct xfs_dir3_icleaf_hdr leafhdr;
1227 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001229 trace_xfs_dir2_leafn_remove(args, index);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dp = args->dp;
1232 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001233 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001234 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001235 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /*
1238 * Point to the entry we're removing.
1239 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001240 lep = &ents[index];
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * Extract the data block and offset from the entry.
1244 */
Dave Chinner30028032014-06-06 15:08:18 +10001245 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001247 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Kill the leaf entry by marking it stale.
1252 * Log the leaf block changes.
1253 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001254 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001255 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001256 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001257
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001258 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001259 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Make the data entry free. Keep track of the longest freespace
1263 * in the data block in case it changes.
1264 */
1265 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001266 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001267 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001268 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001269 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001271 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001272 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /*
1274 * Rescan the data block freespaces for bestfree.
1275 * Log the data block header if needed.
1276 */
1277 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001278 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001280 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001281 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * If the longest data block freespace changes, need to update
1284 * the corresponding freeblock entry.
1285 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001286 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001288 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 xfs_dir2_db_t fdb; /* freeblock block number */
1290 int findex; /* index in freeblock entries */
1291 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 /*
1294 * Convert the data block number to a free block,
1295 * read in the free block.
1296 */
Dave Chinner8f661932014-06-06 15:15:59 +10001297 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001298 error = xfs_dir2_free_read(tp, dp,
1299 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001300 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001303 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001304#ifdef DEBUG
1305 {
1306 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001307 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001308 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001309 (fdb - xfs_dir2_byte_to_db(args->geo,
1310 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001311 }
1312#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /*
1314 * Calculate which entry we need to fix.
1315 */
Dave Chinner8f661932014-06-06 15:15:59 +10001316 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001317 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /*
1319 * If the data block is now empty we can get rid of it
1320 * (usually).
1321 */
Dave Chinner8f661932014-06-06 15:15:59 +10001322 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001323 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
1325 * Try to punch out the data block.
1326 */
1327 error = xfs_dir2_shrink_inode(args, db, dbp);
1328 if (error == 0) {
1329 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001330 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 /*
1333 * We can get ENOSPC if there's no space reservation.
1334 * In this case just drop the buffer and some one else
1335 * will eventually get rid of the empty block.
1336 */
Dave Chinner24513372014-06-25 14:58:08 +10001337 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return error;
1339 }
1340 /*
1341 * If we got rid of the data block, we can eliminate that entry
1342 * in the free block.
1343 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001344 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001345 fdb, findex, fbp, longest);
1346 if (error)
1347 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Dave Chinner20252072012-11-12 22:54:13 +11001349
Dave Chinner41419562013-10-29 22:11:50 +11001350 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001352 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * to justify trying to join it with a neighbor.
1354 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001355 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001356 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001357 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359}
1360
1361/*
1362 * Split the leaf entries in the old block into old and new blocks.
1363 */
1364int /* error */
1365xfs_dir2_leafn_split(
1366 xfs_da_state_t *state, /* btree cursor */
1367 xfs_da_state_blk_t *oldblk, /* original block */
1368 xfs_da_state_blk_t *newblk) /* newly created block */
1369{
1370 xfs_da_args_t *args; /* operation arguments */
1371 xfs_dablk_t blkno; /* new leaf block number */
1372 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001373 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /*
1376 * Allocate space for a new leaf node.
1377 */
1378 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001379 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1381 error = xfs_da_grow_inode(args, &blkno);
1382 if (error) {
1383 return error;
1384 }
1385 /*
1386 * Initialize the new leaf block.
1387 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001388 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001389 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1390 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 newblk->blkno = blkno;
1394 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1395 /*
1396 * Rebalance the entries across the two leaves, link the new
1397 * block into the leaves.
1398 */
1399 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001400 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (error) {
1402 return error;
1403 }
1404 /*
1405 * Insert the new entry in the correct block.
1406 */
1407 if (state->inleaf)
1408 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1409 else
1410 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1411 /*
1412 * Update last hashval in each block since we added the name.
1413 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001414 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1415 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001416 xfs_dir3_leaf_check(dp, oldblk->bp);
1417 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return error;
1419}
1420
1421/*
1422 * Check a leaf block and its neighbors to see if the block should be
1423 * collapsed into one or the other neighbor. Always keep the block
1424 * with the smaller block number.
1425 * If the current block is over 50% full, don't try to join it, return 0.
1426 * If the block is empty, fill in the state structure and return 2.
1427 * If it can be collapsed, fill in the state structure and return 1.
1428 * If nothing can be done, return 0.
1429 */
1430int /* error */
1431xfs_dir2_leafn_toosmall(
1432 xfs_da_state_t *state, /* btree cursor */
1433 int *action) /* resulting action to take */
1434{
1435 xfs_da_state_blk_t *blk; /* leaf block */
1436 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001437 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int bytes; /* bytes in use */
1439 int count; /* leaf live entry count */
1440 int error; /* error return value */
1441 int forward; /* sibling block direction */
1442 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 xfs_dir2_leaf_t *leaf; /* leaf structure */
1444 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001445 struct xfs_dir3_icleaf_hdr leafhdr;
1446 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001447 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
1450 * Check for the degenerate case of the block being over 50% full.
1451 * If so, it's not worth even looking to see if we might be able
1452 * to coalesce with a sibling.
1453 */
1454 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001455 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001456 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001457 ents = dp->d_ops->leaf_ents_p(leaf);
1458 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001459
1460 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001461 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001462 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
1464 * Blk over 50%, don't try to join.
1465 */
1466 *action = 0;
1467 return 0;
1468 }
1469 /*
1470 * Check for the degenerate case of the block being empty.
1471 * If the block is empty, we'll simply delete it, no need to
1472 * coalesce it with a sibling block. We choose (arbitrarily)
1473 * to merge with the forward block unless it is NULL.
1474 */
1475 if (count == 0) {
1476 /*
1477 * Make altpath point to the block we want to keep and
1478 * path point to the block we want to drop (this one).
1479 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001480 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001482 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 &rval);
1484 if (error)
1485 return error;
1486 *action = rval ? 2 : 0;
1487 return 0;
1488 }
1489 /*
1490 * Examine each sibling block to see if we can coalesce with
1491 * at least 25% free space to spare. We need to figure out
1492 * whether to merge with the forward or the backward block.
1493 * We prefer coalescing with the lower numbered sibling so as
1494 * to shrink a directory over time.
1495 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001496 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001498 struct xfs_dir3_icleaf_hdr hdr2;
1499
1500 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (blkno == 0)
1502 continue;
1503 /*
1504 * Read the sibling leaf block.
1505 */
Dave Chinner41419562013-10-29 22:11:50 +11001506 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001507 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001508 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /*
1512 * Count bytes in the two blocks combined.
1513 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001514 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001515 bytes = state->args->geo->blksize -
1516 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001517
Dave Chinner1d9025e2012-06-22 18:50:14 +10001518 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001519 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001520 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001521 count += hdr2.count - hdr2.stale;
1522 bytes -= count * sizeof(ents[0]);
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /*
1525 * Fits with at least 25% to spare.
1526 */
1527 if (bytes >= 0)
1528 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 /*
1532 * Didn't like either block, give up.
1533 */
1534 if (i >= 2) {
1535 *action = 0;
1536 return 0;
1537 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * Make altpath point to the block we want to keep (the lower
1541 * numbered block) and path point to the block we want to drop.
1542 */
1543 memcpy(&state->altpath, &state->path, sizeof(state->path));
1544 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001545 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 &rval);
1547 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001548 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 &rval);
1550 if (error) {
1551 return error;
1552 }
1553 *action = rval ? 0 : 1;
1554 return 0;
1555}
1556
1557/*
1558 * Move all the leaf entries from drop_blk to save_blk.
1559 * This is done as part of a join operation.
1560 */
1561void
1562xfs_dir2_leafn_unbalance(
1563 xfs_da_state_t *state, /* cursor */
1564 xfs_da_state_blk_t *drop_blk, /* dead block */
1565 xfs_da_state_blk_t *save_blk) /* surviving block */
1566{
1567 xfs_da_args_t *args; /* operation arguments */
1568 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1569 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001570 struct xfs_dir3_icleaf_hdr savehdr;
1571 struct xfs_dir3_icleaf_hdr drophdr;
1572 struct xfs_dir2_leaf_entry *sents;
1573 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001574 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 args = state->args;
1577 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1578 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001579 drop_leaf = drop_blk->bp->b_addr;
1580 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001581
Dave Chinner01ba43b2013-10-29 22:11:52 +11001582 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1583 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1584 sents = dp->d_ops->leaf_ents_p(save_leaf);
1585 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * If there are any stale leaf entries, take this opportunity
1589 * to purge them.
1590 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001591 if (drophdr.stale)
1592 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1593 if (savehdr.stale)
1594 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * Move the entries from drop to the appropriate end of save.
1598 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001599 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001600 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001601 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1602 save_blk->bp, &savehdr, sents, 0,
1603 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001605 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1606 save_blk->bp, &savehdr, sents,
1607 savehdr.count, drophdr.count);
1608 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1609
1610 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001611 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1612 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001613 xfs_dir3_leaf_log_header(args, save_blk->bp);
1614 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001615
Dave Chinner41419562013-10-29 22:11:50 +11001616 xfs_dir3_leaf_check(dp, save_blk->bp);
1617 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
1620/*
1621 * Top-level node form directory addname routine.
1622 */
1623int /* error */
1624xfs_dir2_node_addname(
1625 xfs_da_args_t *args) /* operation arguments */
1626{
1627 xfs_da_state_blk_t *blk; /* leaf block for insert */
1628 int error; /* error return value */
1629 int rval; /* sub-return value */
1630 xfs_da_state_t *state; /* btree cursor */
1631
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001632 trace_xfs_dir2_node_addname(args);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /*
1635 * Allocate and initialize the state (btree cursor).
1636 */
1637 state = xfs_da_state_alloc();
1638 state->args = args;
1639 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Look up the name. We're not supposed to find it, but
1642 * this gives us the insertion point.
1643 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001644 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (error)
1646 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001647 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto done;
1649 }
1650 /*
1651 * Add the data entry to a data block.
1652 * Extravalid is set to a freeblock found by lookup.
1653 */
1654 rval = xfs_dir2_node_addname_int(args,
1655 state->extravalid ? &state->extrablk : NULL);
1656 if (rval) {
1657 goto done;
1658 }
1659 blk = &state->path.blk[state->path.active - 1];
1660 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1661 /*
1662 * Add the new leaf entry.
1663 */
1664 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1665 if (rval == 0) {
1666 /*
1667 * It worked, fix the hash values up the btree.
1668 */
Barry Naujok6a178102008-05-21 16:42:05 +10001669 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001670 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else {
1672 /*
1673 * It didn't work, we need to split the leaf block.
1674 */
1675 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001676 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 goto done;
1678 }
1679 /*
1680 * Split the leaf block and insert the new entry.
1681 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001682 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684done:
1685 xfs_da_state_free(state);
1686 return rval;
1687}
1688
1689/*
1690 * Add the data entry for a node-format directory name addition.
1691 * The leaf entry is added in xfs_dir2_leafn_add.
1692 * We may enter with a freespace block that the lookup found.
1693 */
1694static int /* error */
1695xfs_dir2_node_addname_int(
1696 xfs_da_args_t *args, /* operation arguments */
1697 xfs_da_state_blk_t *fblk) /* optional freespace block */
1698{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001699 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001701 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1703 xfs_inode_t *dp; /* incore directory inode */
1704 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1705 int error; /* error return value */
1706 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001707 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 int findex; /* freespace entry index */
1709 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1710 xfs_dir2_db_t ifbno; /* initial freespace block no */
1711 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1712 int length; /* length of the new entry */
1713 int logfree; /* need to log free entry */
1714 xfs_mount_t *mp; /* filesystem mount point */
1715 int needlog; /* need to log data header */
1716 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001717 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001719 __be16 *bests;
1720 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001721 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 dp = args->dp;
1724 mp = dp->i_mount;
1725 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001726 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * If we came in with a freespace block that means that lookup
1729 * found an entry with our hash value. This is the freespace
1730 * block for that data entry.
1731 */
1732 if (fblk) {
1733 fbp = fblk->bp;
1734 /*
1735 * Remember initial freespace block number.
1736 */
1737 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001738 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001740 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001741 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * This means the free entry showed that the data block had
1745 * space for our entry, so we remembered it.
1746 * Use that data block.
1747 */
1748 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001749 ASSERT(findex < freehdr.nvalid);
1750 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1751 ASSERT(be16_to_cpu(bests[findex]) >= length);
1752 dbno = freehdr.firstdb + findex;
1753 } else {
1754 /*
1755 * The data block looked at didn't have enough room.
1756 * We'll start at the beginning of the freespace entries.
1757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 dbno = -1;
1759 findex = 0;
1760 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001761 } else {
1762 /*
1763 * Didn't come in with a freespace block, so no data block.
1764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 ifbno = dbno = -1;
1766 fbp = NULL;
1767 findex = 0;
1768 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * If we don't have a data block yet, we're going to scan the
1772 * freespace blocks looking for one. Figure out what the
1773 * highest freespace block number is.
1774 */
1775 if (dbno == -1) {
1776 xfs_fileoff_t fo; /* freespace block number */
1777
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001778 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001780 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 fbno = ifbno;
1782 }
1783 /*
1784 * While we haven't identified a data block, search the freeblock
1785 * data for a good data block. If we find a null freeblock entry,
1786 * indicating a hole in the data blocks, remember that.
1787 */
1788 while (dbno == -1) {
1789 /*
1790 * If we don't have a freeblock in hand, get the next one.
1791 */
1792 if (fbp == NULL) {
1793 /*
1794 * Happens the first time through unless lookup gave
1795 * us a freespace block to start with.
1796 */
1797 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001798 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001799 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * If it's ifbno we already looked at it.
1802 */
1803 if (fbno == ifbno)
1804 fbno++;
1805 /*
1806 * If it's off the end we're done.
1807 */
1808 if (fbno >= lastfbno)
1809 break;
1810 /*
1811 * Read the block. There can be holes in the
1812 * freespace blocks, so this might not succeed.
1813 * This should be really rare, so there's no reason
1814 * to avoid it.
1815 */
Dave Chinner20252072012-11-12 22:54:13 +11001816 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001817 xfs_dir2_db_to_da(args->geo, fbno),
1818 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001819 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001821 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001823 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 findex = 0;
1825 }
1826 /*
1827 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001828 *
1829 * The bests initialisation should be where the bufer is read in
1830 * the above branch. But gcc is too stupid to realise that bests
1831 * and the freehdr are actually initialised if they are placed
1832 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001834 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001835 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001836 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1837 be16_to_cpu(bests[findex]) >= length)
1838 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 else {
1840 /*
1841 * Are we done with the freeblock?
1842 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001843 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Drop the block.
1846 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001847 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 fbp = NULL;
1849 if (fblk && fblk->bp)
1850 fblk->bp = NULL;
1851 }
1852 }
1853 }
1854 /*
1855 * If we don't have a data block, we need to allocate one and make
1856 * the freespace entries refer to it.
1857 */
1858 if (unlikely(dbno == -1)) {
1859 /*
1860 * Not allowed to allocate, return failure.
1861 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001862 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001863 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Allocate and initialize the new data block.
1867 */
1868 if (unlikely((error = xfs_dir2_grow_inode(args,
1869 XFS_DIR2_DATA_SPACE,
1870 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001871 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * If (somehow) we have a freespace block, get rid of it.
1876 */
1877 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001878 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (fblk && fblk->bp)
1880 fblk->bp = NULL;
1881
1882 /*
1883 * Get the freespace block corresponding to the data block
1884 * that was just allocated.
1885 */
Dave Chinner8f661932014-06-06 15:15:59 +10001886 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001887 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001888 xfs_dir2_db_to_da(args->geo, fbno),
1889 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001890 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /*
1894 * If there wasn't a freespace block, the read will
1895 * return a NULL fbp. Allocate and initialize a new one.
1896 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001897 if (!fbp) {
1898 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1899 &fbno);
1900 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Dave Chinner8f661932014-06-06 15:15:59 +10001903 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001904 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001905"%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 +11001906 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001907 (long long)dp->d_ops->db_to_fdb(
1908 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 (long long)dbno, (long long)fbno,
1910 (unsigned long long)ifbno, lastfbno);
1911 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001912 xfs_alert(mp,
1913 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 fblk,
1915 (unsigned long long)fblk->blkno,
1916 fblk->index,
1917 fblk->magic);
1918 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001919 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1922 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001923 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
1926 /*
1927 * Get a buffer for the new block.
1928 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001929 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001930 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001932 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001933 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001934 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001937 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001939 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001940 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001941 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001942 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001944 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001945 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001946 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
1949 /*
1950 * Set the freespace block index from the data block number.
1951 */
Dave Chinner8f661932014-06-06 15:15:59 +10001952 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /*
1954 * If it's after the end of the current entries in the
1955 * freespace block, extend that table.
1956 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001957 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001958 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001959 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Tag new entry so nused will go up.
1962 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001963 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 /*
1966 * If this entry was for an empty data block
1967 * (this should always be true) then update the header.
1968 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001969 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1970 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001971 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001972 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 /*
1975 * Update the real value in the table.
1976 * We haven't allocated the data entry yet so this will
1977 * change again.
1978 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001979 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001980 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001981 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 logfree = 1;
1983 }
1984 /*
1985 * We had a data block so we don't have to make a new one.
1986 */
1987 else {
1988 /*
1989 * If just checking, we succeeded.
1990 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001991 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
1995 * Read the data block in.
1996 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001997 error = xfs_dir3_data_read(tp, dp,
1998 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001999 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002000 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002002 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11002003 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 logfree = 0;
2005 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002006 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Point to the existing unused space.
2009 */
2010 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002011 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 needscan = needlog = 0;
2013 /*
2014 * Mark the first part of the unused space, inuse for us.
2015 */
Dave Chinnerbc851782014-06-06 15:20:54 +10002016 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002017 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 &needlog, &needscan);
2019 /*
2020 * Fill in the new entry and log it.
2021 */
2022 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002023 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 dep->namelen = args->namelen;
2025 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002026 dp->d_ops->data_put_ftype(dep, args->filetype);
2027 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002028 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002029 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
2031 * Rescan the block for bestfree if needed.
2032 */
2033 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002034 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /*
2036 * Log the data block header if needed.
2037 */
2038 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002039 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /*
2041 * If the freespace entry is now wrong, update it.
2042 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002043 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002044 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2045 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 logfree = 1;
2047 }
2048 /*
2049 * Log the freespace entry if needed.
2050 */
2051 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002052 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 * Return the data block and offset in args, then drop the data block.
2055 */
2056 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002057 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
2060
2061/*
2062 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002063 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * The only real output is the inode number of the entry.
2065 */
2066int /* error */
2067xfs_dir2_node_lookup(
2068 xfs_da_args_t *args) /* operation arguments */
2069{
2070 int error; /* error return value */
2071 int i; /* btree level */
2072 int rval; /* operation return value */
2073 xfs_da_state_t *state; /* btree cursor */
2074
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002075 trace_xfs_dir2_node_lookup(args);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /*
2078 * Allocate and initialize the btree cursor.
2079 */
2080 state = xfs_da_state_alloc();
2081 state->args = args;
2082 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * Fill in the path to the entry in the cursor.
2085 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002086 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (error)
2088 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002089 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2090 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002091 xfs_dir2_data_entry_t *dep;
2092
Dave Chinner1d9025e2012-06-22 18:50:14 +10002093 dep = (xfs_dir2_data_entry_t *)
2094 ((char *)state->extrablk.bp->b_addr +
2095 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002096 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
2099 * Release the btree blocks and leaf block.
2100 */
2101 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002102 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 state->path.blk[i].bp = NULL;
2104 }
2105 /*
2106 * Release the data block if we have it.
2107 */
2108 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002109 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 state->extrablk.bp = NULL;
2111 }
2112 xfs_da_state_free(state);
2113 return rval;
2114}
2115
2116/*
2117 * Remove an entry from a node-format directory.
2118 */
2119int /* error */
2120xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002121 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002123 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int error; /* error return value */
2125 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002126 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002128 trace_xfs_dir2_node_removename(args);
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /*
2131 * Allocate and initialize the btree cursor.
2132 */
2133 state = xfs_da_state_alloc();
2134 state->args = args;
2135 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002136
2137 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002138 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002139 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002140 goto out_free;
2141
2142 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002143 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002144 error = rval;
2145 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 blk = &state->path.blk[state->path.active - 1];
2149 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2150 ASSERT(state->extravalid);
2151 /*
2152 * Remove the leaf and data entries.
2153 * Extrablk refers to the data block.
2154 */
2155 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2156 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002157 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002158 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
2160 * Fix the hash values up the btree.
2161 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002162 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * If we need to join leaf blocks, do it.
2165 */
2166 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002167 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /*
2169 * If no errors so far, try conversion to leaf format.
2170 */
2171 if (!error)
2172 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002173out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 xfs_da_state_free(state);
2175 return error;
2176}
2177
2178/*
2179 * Replace an entry's inode number in a node-format directory.
2180 */
2181int /* error */
2182xfs_dir2_node_replace(
2183 xfs_da_args_t *args) /* operation arguments */
2184{
2185 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002186 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 xfs_dir2_data_entry_t *dep; /* data entry changed */
2188 int error; /* error return value */
2189 int i; /* btree level */
2190 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002191 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 xfs_dir2_leaf_t *leaf; /* leaf structure */
2193 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2194 int rval; /* internal return value */
2195 xfs_da_state_t *state; /* btree cursor */
2196
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002197 trace_xfs_dir2_node_replace(args);
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * Allocate and initialize the btree cursor.
2201 */
2202 state = xfs_da_state_alloc();
2203 state->args = args;
2204 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002205
2206 /*
2207 * We have to save new inode number and ftype since
2208 * xfs_da3_node_lookup_int() is going to overwrite them
2209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002211 ftype = args->filetype;
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Lookup the entry to change in the btree.
2215 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002216 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (error) {
2218 rval = error;
2219 }
2220 /*
2221 * It should be found, since the vnodeops layer has looked it up
2222 * and locked it. But paranoia is good.
2223 */
Dave Chinner24513372014-06-25 14:58:08 +10002224 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002225 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Find the leaf entry.
2228 */
2229 blk = &state->path.blk[state->path.active - 1];
2230 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002231 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002232 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002233 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 ASSERT(state->extravalid);
2235 /*
2236 * Point to the data entry.
2237 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002238 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002239 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2240 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002242 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002243 xfs_dir2_dataptr_to_off(args->geo,
2244 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002245 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /*
2247 * Fill in the new inode number and log the entry.
2248 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002249 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002250 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002251 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 rval = 0;
2253 }
2254 /*
2255 * Didn't find it, and we're holding a data block. Drop it.
2256 */
2257 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002258 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 state->extrablk.bp = NULL;
2260 }
2261 /*
2262 * Release all the buffers in the cursor.
2263 */
2264 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002265 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 state->path.blk[i].bp = NULL;
2267 }
2268 xfs_da_state_free(state);
2269 return rval;
2270}
2271
2272/*
2273 * Trim off a trailing empty freespace block.
2274 * Return (in rvalp) 1 if we did it, 0 if not.
2275 */
2276int /* error */
2277xfs_dir2_node_trim_free(
2278 xfs_da_args_t *args, /* operation arguments */
2279 xfs_fileoff_t fo, /* free block number */
2280 int *rvalp) /* out: did something */
2281{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002282 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 xfs_inode_t *dp; /* incore directory inode */
2284 int error; /* error return code */
2285 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002287 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002291
2292 *rvalp = 0;
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /*
2295 * Read the freespace block.
2296 */
Dave Chinner20252072012-11-12 22:54:13 +11002297 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * There can be holes in freespace. If fo is a hole, there's
2302 * nothing to do.
2303 */
Dave Chinner20252072012-11-12 22:54:13 +11002304 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002306 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002307 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * If there are used entries, there's nothing to do.
2311 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002312 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002313 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
2315 }
2316 /*
2317 * Blow the block away.
2318 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002319 error = xfs_dir2_shrink_inode(args,
2320 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2321 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 /*
2323 * Can't fail with ENOSPC since that only happens with no
2324 * space reservation, when breaking up an extent into two
2325 * pieces. This is the last block of an extent.
2326 */
Dave Chinner24513372014-06-25 14:58:08 +10002327 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002328 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return error;
2330 }
2331 /*
2332 * Return that we succeeded.
2333 */
2334 *rvalp = 1;
2335 return 0;
2336}