blob: f1bb3434f51c79d17fbc951b6e108d8c33b6865e [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11004 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11008#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11009#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110013#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110014#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "xfs_inode.h"
16#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100017#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020018#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000020#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110022#include "xfs_buf_item.h"
23#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110024#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Function declarations.
28 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100029static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
30 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
32 xfs_da_state_blk_t *blk1,
33 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100034static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 int index, xfs_da_state_blk_t *dblk,
36 int *rval);
37static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
38 xfs_da_state_blk_t *fblk);
39
Dave Chinner24df33b2013-04-12 07:30:21 +100040/*
41 * Check internal consistency of a leafn block.
42 */
43#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080044static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100045xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110046 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100047 struct xfs_buf *bp)
48{
49 struct xfs_dir2_leaf *leaf = bp->b_addr;
50 struct xfs_dir3_icleaf_hdr leafhdr;
51
Dave Chinner01ba43b2013-10-29 22:11:52 +110052 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100053
54 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
55 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
56 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080057 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100058 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080059 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100060
Dave Chinner41419562013-10-29 22:11:50 +110061 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100062}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080063
64static inline void
65xfs_dir3_leaf_check(
66 struct xfs_inode *dp,
67 struct xfs_buf *bp)
68{
69 xfs_failaddr_t fa;
70
71 fa = xfs_dir3_leafn_check(dp, bp);
72 if (!fa)
73 return;
74 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
Darrick J. Wong2551a532018-06-04 10:23:54 -070075 bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__,
76 fa);
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080077 ASSERT(0);
78}
Dave Chinner24df33b2013-04-12 07:30:21 +100079#else
Dave Chinner41419562013-10-29 22:11:50 +110080#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100081#endif
82
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080083static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110084xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110085 struct xfs_buf *bp)
86{
87 struct xfs_mount *mp = bp->b_target->bt_mount;
88 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110089
Dave Chinnercbc8adf2013-04-03 16:11:21 +110090 if (xfs_sb_version_hascrc(&mp->m_sb)) {
91 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
92
93 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080094 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +100095 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080096 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +110097 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080098 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +110099 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800100 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100101 } else {
102 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800103 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100104 }
105
106 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
107
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800108 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100109}
110
111static void
112xfs_dir3_free_read_verify(
113 struct xfs_buf *bp)
114{
115 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800116 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100117
Eric Sandeence5028c2014-02-27 15:23:10 +1100118 if (xfs_sb_version_hascrc(&mp->m_sb) &&
119 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800120 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
121 else {
122 fa = xfs_dir3_free_verify(bp);
123 if (fa)
124 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
125 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100126}
Dave Chinner20252072012-11-12 22:54:13 +1100127
Dave Chinner612cfbf2012-11-14 17:52:32 +1100128static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100129xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100130 struct xfs_buf *bp)
131{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100132 struct xfs_mount *mp = bp->b_target->bt_mount;
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800133 struct xfs_buf_log_item *bip = bp->b_log_item;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100134 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800135 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100136
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800137 fa = xfs_dir3_free_verify(bp);
138 if (fa) {
139 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100140 return;
141 }
142
143 if (!xfs_sb_version_hascrc(&mp->m_sb))
144 return;
145
146 if (bip)
147 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
148
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100149 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100150}
151
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100152const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100153 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100154 .verify_read = xfs_dir3_free_read_verify,
155 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800156 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100157};
Dave Chinner20252072012-11-12 22:54:13 +1100158
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800159/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800160static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800161xfs_dir3_free_header_check(
162 struct xfs_inode *dp,
163 xfs_dablk_t fbno,
164 struct xfs_buf *bp)
165{
166 struct xfs_mount *mp = dp->i_mount;
167 unsigned int firstdb;
168 int maxbests;
169
170 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
171 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
172 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
173 maxbests;
174 if (xfs_sb_version_hascrc(&mp->m_sb)) {
175 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
176
177 if (be32_to_cpu(hdr3->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800178 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800179 if (be32_to_cpu(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800180 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800181 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800182 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800183 } else {
184 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
185
186 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800187 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800188 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800189 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800190 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800191 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800192 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800193 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800194}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100195
Dave Chinner20252072012-11-12 22:54:13 +1100196static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100197__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100198 struct xfs_trans *tp,
199 struct xfs_inode *dp,
200 xfs_dablk_t fbno,
201 xfs_daddr_t mappedbno,
202 struct xfs_buf **bpp)
203{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800204 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100205 int err;
206
207 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100208 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800209 if (err || !*bpp)
210 return err;
211
212 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800213 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
214 if (fa) {
215 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800216 xfs_trans_brelse(tp, *bpp);
217 return -EFSCORRUPTED;
218 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100219
220 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800221 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100222 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800223
224 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100225}
226
227int
228xfs_dir2_free_read(
229 struct xfs_trans *tp,
230 struct xfs_inode *dp,
231 xfs_dablk_t fbno,
232 struct xfs_buf **bpp)
233{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100234 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100235}
236
237static int
238xfs_dir2_free_try_read(
239 struct xfs_trans *tp,
240 struct xfs_inode *dp,
241 xfs_dablk_t fbno,
242 struct xfs_buf **bpp)
243{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100244 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
245}
246
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100247static int
248xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000249 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100250 xfs_dir2_db_t fbno,
251 struct xfs_buf **bpp)
252{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000253 struct xfs_trans *tp = args->trans;
254 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100255 struct xfs_mount *mp = dp->i_mount;
256 struct xfs_buf *bp;
257 int error;
258 struct xfs_dir3_icfree_hdr hdr;
259
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000260 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100261 -1, &bp, XFS_DATA_FORK);
262 if (error)
263 return error;
264
Dave Chinner61fe1352013-04-03 16:11:30 +1100265 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100266 bp->b_ops = &xfs_dir3_free_buf_ops;
267
268 /*
269 * Initialize the new block to be empty, and remember
270 * its first slot as our empty slot.
271 */
Dave Chinnere400d272013-05-28 18:37:17 +1000272 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
273 memset(&hdr, 0, sizeof(hdr));
274
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100275 if (xfs_sb_version_hascrc(&mp->m_sb)) {
276 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
277
278 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000279
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100280 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
281 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000282 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000283 } else
284 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100285 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100286 *bpp = bp;
287 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
291 * Log entries from a freespace block.
292 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000293STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000295 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000296 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int first, /* first entry to log */
298 int last) /* last entry to log */
299{
300 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100301 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Dave Chinner1d9025e2012-06-22 18:50:14 +1000303 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000304 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100305 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
306 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000307 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100308 (uint)((char *)&bests[first] - (char *)free),
309 (uint)((char *)&bests[last] - (char *)free +
310 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
314 * Log header from a freespace block.
315 */
316static void
317xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000318 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000319 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Dave Chinner836a94a2013-08-12 20:49:44 +1000321#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 xfs_dir2_free_t *free; /* freespace structure */
323
Dave Chinner1d9025e2012-06-22 18:50:14 +1000324 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100325 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
326 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000327#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000328 xfs_trans_log_buf(args->trans, bp, 0,
329 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
333 * Convert a leaf-format directory to a node-format directory.
334 * We need to change the magic number of the leaf block, and copy
335 * the freespace table out of the leaf block into its own block.
336 */
337int /* error */
338xfs_dir2_leaf_to_node(
339 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000340 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 xfs_inode_t *dp; /* incore directory inode */
343 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000344 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 xfs_dir2_db_t fdb; /* freespace block number */
346 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100347 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int i; /* leaf freespace index */
349 xfs_dir2_leaf_t *leaf; /* leaf structure */
350 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int n; /* count of live freespc ents */
352 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100353 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100355 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000357 trace_xfs_dir2_leaf_to_node(args);
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 tp = args->trans;
361 /*
362 * Add a freespace block to the directory.
363 */
364 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
365 return error;
366 }
Dave Chinner30028032014-06-06 15:08:18 +1000367 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /*
369 * Get the buffer for the new freespace block.
370 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000371 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100374
Dave Chinner1d9025e2012-06-22 18:50:14 +1000375 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100376 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000377 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000378 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Darrick J. Wong3f883f52018-03-06 17:08:31 -0800379 if (be32_to_cpu(ltp->bestcount) >
380 (uint)dp->i_d.di_size / args->geo->blksize)
381 return -EFSCORRUPTED;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Copy freespace entries from the leaf block to the new block.
385 * Count active entries.
386 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100387 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500388 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100389 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100390 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100392 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100396 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100398 freehdr.nused = n;
399 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
400
Dave Chinner01ba43b2013-10-29 22:11:52 +1100401 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000402 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
403 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100404
Dave Chinner24df33b2013-04-12 07:30:21 +1000405 /*
406 * Converting the leaf to a leafnode is just a matter of changing the
407 * magic number and the ops. Do the change directly to the buffer as
408 * it's less work (and less code) than decoding the header to host
409 * format and back again.
410 */
411 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
412 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
413 else
414 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
415 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100416 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000417 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100418 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420}
421
422/*
423 * Add a leaf entry to a leaf block in a node-form directory.
424 * The other work necessary is done from the caller.
425 */
426static int /* error */
427xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000428 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 xfs_da_args_t *args, /* operation arguments */
430 int index) /* insertion pt for new entry */
431{
432 int compact; /* compacting stale leaves */
433 xfs_inode_t *dp; /* incore directory inode */
434 int highstale; /* next stale entry */
435 xfs_dir2_leaf_t *leaf; /* leaf structure */
436 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
437 int lfloghigh; /* high leaf entry logging */
438 int lfloglow; /* low leaf entry logging */
439 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000440 struct xfs_dir3_icleaf_hdr leafhdr;
441 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000443 trace_xfs_dir2_leafn_add(args, index);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000446 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100447 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100448 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 /*
451 * Quick check just to make sure we are not going to index
452 * into other peoples memory
453 */
454 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000455 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 /*
458 * If there are already the maximum number of leaf entries in
459 * the block, if there are no stale entries it won't fit.
460 * Caller will do a split. If there are stale entries we'll do
461 * a compact.
462 */
463
Dave Chinner8f661932014-06-06 15:15:59 +1000464 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000465 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000466 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000467 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else
469 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000470 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
471 ASSERT(index == leafhdr.count ||
472 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Barry Naujok6a178102008-05-21 16:42:05 +1000474 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return 0;
476
477 /*
478 * Compact out all but one stale leaf entry. Leaves behind
479 * the entry closest to index.
480 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000481 if (compact)
482 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
483 &highstale, &lfloglow, &lfloghigh);
484 else if (leafhdr.stale) {
485 /*
486 * Set impossible logging indices for this case.
487 */
488 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 lfloghigh = -1;
490 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Insert the new entry, log everything.
494 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000495 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200496 highstale, &lfloglow, &lfloghigh);
497
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100498 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000499 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100500 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000501
Dave Chinner01ba43b2013-10-29 22:11:52 +1100502 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000503 xfs_dir3_leaf_log_header(args, bp);
504 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100505 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507}
508
509#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100510static void
511xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100512 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100513 struct xfs_buf *bp,
514 xfs_dir2_db_t db)
515{
516 struct xfs_dir3_icfree_hdr hdr;
517
Dave Chinner01ba43b2013-10-29 22:11:52 +1100518 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100519
Dave Chinner8f661932014-06-06 15:15:59 +1000520 ASSERT((hdr.firstdb %
521 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100522 ASSERT(hdr.firstdb <= db);
523 ASSERT(db < hdr.firstdb + hdr.nvalid);
524}
525#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100526#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#endif /* DEBUG */
528
529/*
530 * Return the last hash value in the leaf.
531 * Stale entries are ok.
532 */
533xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700534xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100535 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000536 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int *count) /* count of entries in leaf */
538{
Dave Chinner24df33b2013-04-12 07:30:21 +1000539 struct xfs_dir2_leaf *leaf = bp->b_addr;
540 struct xfs_dir2_leaf_entry *ents;
541 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dave Chinner01ba43b2013-10-29 22:11:52 +1100543 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000544
545 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700546 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
547 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
548 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000551 *count = leafhdr.count;
552 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000554
Dave Chinner41419562013-10-29 22:11:50 +1100555 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000556 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000560 * Look up a leaf entry for space to add a name in a node-format leaf block.
561 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000563STATIC int
564xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000565 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 xfs_da_args_t *args, /* operation arguments */
567 int *indexp, /* out: leaf entry index */
568 xfs_da_state_t *state) /* state to fill in */
569{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000570 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000571 xfs_dir2_db_t curdb = -1; /* current data block number */
572 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 xfs_inode_t *dp; /* incore directory inode */
574 int error; /* error return value */
575 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000576 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 int index; /* leaf entry index */
578 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000579 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
581 xfs_mount_t *mp; /* filesystem mount point */
582 xfs_dir2_db_t newdb; /* new data block number */
583 xfs_dir2_db_t newfdb; /* new free block number */
584 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000585 struct xfs_dir2_leaf_entry *ents;
586 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 dp = args->dp;
589 tp = args->trans;
590 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000591 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100592 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100593 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000594
Dave Chinner41419562013-10-29 22:11:50 +1100595 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000596 ASSERT(leafhdr.count > 0);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Look up the hash value in the leaf entries.
600 */
601 index = xfs_dir2_leaf_search_hash(args, bp);
602 /*
603 * Do we have a buffer coming in?
604 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000605 if (state->extravalid) {
606 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000608 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000609 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100610 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
611 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100613 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Loop over leaf entries with the right hash value.
616 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000617 for (lep = &ents[index];
618 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
619 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Skip stale leaf entries.
622 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100623 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 continue;
625 /*
626 * Pull the data block number from the entry.
627 */
Dave Chinner30028032014-06-06 15:08:18 +1000628 newdb = xfs_dir2_dataptr_to_db(args->geo,
629 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * For addname, we're looking for a place to put the new entry.
632 * We want to use a data block with an entry of equal
633 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000634 *
635 * If this block isn't the data block we already have
636 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000638 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100639 __be16 *bests;
640
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000641 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643 * Convert the data block to the free block
644 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 */
Dave Chinner8f661932014-06-06 15:15:59 +1000646 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000648 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000652 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000655 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100656
657 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000658 xfs_dir2_db_to_da(args->geo,
659 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100660 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000661 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000663 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100664
Dave Chinner01ba43b2013-10-29 22:11:52 +1100665 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000668 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
Dave Chinner8f661932014-06-06 15:15:59 +1000670 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000672 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500674 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100675 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
677 XFS_ERRLEVEL_LOW, mp);
678 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000679 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000680 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000682 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100683 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687 /* Didn't find any space */
688 fi = -1;
689out:
Barry Naujok6a178102008-05-21 16:42:05 +1000690 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000691 if (curbp) {
692 /* Giving back a free block. */
693 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000695 state->extrablk.index = fi;
696 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100697
698 /*
699 * Important: this magic number is not in the buffer - it's for
700 * buffer type information and therefore only the free/data type
701 * matters here, not whether CRCs are enabled or not.
702 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000703 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
704 } else {
705 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000708 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000711 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000715 * Look up a leaf entry in a node-format leaf block.
716 * The extrablk in state a data block.
717 */
718STATIC int
719xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000720 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000721 xfs_da_args_t *args, /* operation arguments */
722 int *indexp, /* out: leaf entry index */
723 xfs_da_state_t *state) /* state to fill in */
724{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000725 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000726 xfs_dir2_db_t curdb = -1; /* current data block number */
727 xfs_dir2_data_entry_t *dep; /* data block entry */
728 xfs_inode_t *dp; /* incore directory inode */
729 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000730 int index; /* leaf entry index */
731 xfs_dir2_leaf_t *leaf; /* leaf structure */
732 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
733 xfs_mount_t *mp; /* filesystem mount point */
734 xfs_dir2_db_t newdb; /* new data block number */
735 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000736 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000737 struct xfs_dir2_leaf_entry *ents;
738 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000739
740 dp = args->dp;
741 tp = args->trans;
742 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000743 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100744 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100745 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000746
Dave Chinner41419562013-10-29 22:11:50 +1100747 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000748 ASSERT(leafhdr.count > 0);
749
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000750 /*
751 * Look up the hash value in the leaf entries.
752 */
753 index = xfs_dir2_leaf_search_hash(args, bp);
754 /*
755 * Do we have a buffer coming in?
756 */
757 if (state->extravalid) {
758 curbp = state->extrablk.bp;
759 curdb = state->extrablk.blkno;
760 }
761 /*
762 * Loop over leaf entries with the right hash value.
763 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000764 for (lep = &ents[index];
765 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
766 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000767 /*
768 * Skip stale leaf entries.
769 */
770 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
771 continue;
772 /*
773 * Pull the data block number from the entry.
774 */
Dave Chinner30028032014-06-06 15:08:18 +1000775 newdb = xfs_dir2_dataptr_to_db(args->geo,
776 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000777 /*
778 * Not adding a new entry, so we really want to find
779 * the name given to us.
780 *
781 * If it's a different data block, go get it.
782 */
783 if (newdb != curdb) {
784 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000785 * If we had a block before that we aren't saving
786 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000787 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000788 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
789 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000790 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000791 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000792 * If needing the block that is saved with a CI match,
793 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000794 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000795 if (args->cmpresult != XFS_CMP_DIFFERENT &&
796 newdb == state->extrablk.blkno) {
797 ASSERT(state->extravalid);
798 curbp = state->extrablk.bp;
799 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100800 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000801 xfs_dir2_db_to_da(args->geo,
802 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100803 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000804 if (error)
805 return error;
806 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100807 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000808 curdb = newdb;
809 }
810 /*
811 * Point to the data entry.
812 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000813 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000814 xfs_dir2_dataptr_to_off(args->geo,
815 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000816 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000817 * Compare the entry and if it's an exact match, return
818 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000819 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000820 */
Barry Naujok5163f952008-05-21 16:41:01 +1000821 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
822 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000823 /* If there is a CI match block, drop it */
824 if (args->cmpresult != XFS_CMP_DIFFERENT &&
825 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000826 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000827 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000828 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100829 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000830 *indexp = index;
831 state->extravalid = 1;
832 state->extrablk.bp = curbp;
833 state->extrablk.blkno = curdb;
834 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000835 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000836 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100837 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100838 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000839 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000840 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000841 }
842 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000843 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000844 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000845 if (args->cmpresult == XFS_CMP_DIFFERENT) {
846 /* Giving back last used data block. */
847 state->extravalid = 1;
848 state->extrablk.bp = curbp;
849 state->extrablk.index = -1;
850 state->extrablk.blkno = curdb;
851 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100852 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100853 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000854 } else {
855 /* If the curbp is not the CI match block, drop it */
856 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000857 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000858 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000859 } else {
860 state->extravalid = 0;
861 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000862 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000863 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000864}
865
866/*
867 * Look up a leaf entry in a node-format leaf block.
868 * If this is an addname then the extrablk in state is a freespace block,
869 * otherwise it's a data block.
870 */
871int
872xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000873 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000874 xfs_da_args_t *args, /* operation arguments */
875 int *indexp, /* out: leaf entry index */
876 xfs_da_state_t *state) /* state to fill in */
877{
Barry Naujok6a178102008-05-21 16:42:05 +1000878 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000879 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
880 state);
881 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
882}
883
884/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 * Move count leaf entries from source to destination leaf.
886 * Log entries and headers. Stale entries are preserved.
887 */
888static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000889xfs_dir3_leafn_moveents(
890 xfs_da_args_t *args, /* operation arguments */
891 struct xfs_buf *bp_s, /* source */
892 struct xfs_dir3_icleaf_hdr *shdr,
893 struct xfs_dir2_leaf_entry *sents,
894 int start_s,/* source leaf index */
895 struct xfs_buf *bp_d, /* destination */
896 struct xfs_dir3_icleaf_hdr *dhdr,
897 struct xfs_dir2_leaf_entry *dents,
898 int start_d,/* destination leaf index */
899 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Dave Chinner24df33b2013-04-12 07:30:21 +1000901 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000903 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * Silently return if nothing to do.
907 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000908 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /*
912 * If the destination index is not the end of the current
913 * destination leaf entries, open up a hole in the destination
914 * to hold the new entries.
915 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 if (start_d < dhdr->count) {
917 memmove(&dents[start_d + count], &dents[start_d],
918 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000919 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
922 /*
923 * If the source has stale leaves, count the ones in the copy range
924 * so we can update the header correctly.
925 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000926 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int i; /* temp leaf index */
928
929 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000930 if (sents[i].address ==
931 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 stale++;
933 }
934 } else
935 stale = 0;
936 /*
937 * Copy the leaf entries from source to destination.
938 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000939 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000941 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * If there are source entries after the ones we copied,
945 * delete the ones we copied by sliding the next ones down.
946 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000947 if (start_s + count < shdr->count) {
948 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000950 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /*
954 * Update the headers and log them.
955 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000956 shdr->count -= count;
957 shdr->stale -= stale;
958 dhdr->count += count;
959 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/*
963 * Determine the sort order of two leaf blocks.
964 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
965 */
966int /* sort order */
967xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100968 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000969 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
970 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Dave Chinner24df33b2013-04-12 07:30:21 +1000972 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
973 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
974 struct xfs_dir2_leaf_entry *ents1;
975 struct xfs_dir2_leaf_entry *ents2;
976 struct xfs_dir3_icleaf_hdr hdr1;
977 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Dave Chinner01ba43b2013-10-29 22:11:52 +1100979 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
980 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100981 ents1 = dp->d_ops->leaf_ents_p(leaf1);
982 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000983
984 if (hdr1.count > 0 && hdr2.count > 0 &&
985 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
986 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
987 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return 1;
989 return 0;
990}
991
992/*
993 * Rebalance leaf entries between two leaf blocks.
994 * This is actually only called when the second block is new,
995 * though the code deals with the general case.
996 * A new entry will be inserted in one of the blocks, and that
997 * entry is taken into account when balancing.
998 */
999static void
1000xfs_dir2_leafn_rebalance(
1001 xfs_da_state_t *state, /* btree cursor */
1002 xfs_da_state_blk_t *blk1, /* first btree block */
1003 xfs_da_state_blk_t *blk2) /* second btree block */
1004{
1005 xfs_da_args_t *args; /* operation arguments */
1006 int count; /* count (& direction) leaves */
1007 int isleft; /* new goes in left leaf */
1008 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1009 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1010 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001011#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int oldstale; /* old count of stale leaves */
1013#endif
1014 int oldsum; /* old total leaf count */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001015 int swap_blocks; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001016 struct xfs_dir2_leaf_entry *ents1;
1017 struct xfs_dir2_leaf_entry *ents2;
1018 struct xfs_dir3_icleaf_hdr hdr1;
1019 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001020 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 args = state->args;
1023 /*
1024 * If the block order is wrong, swap the arguments.
1025 */
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001026 swap_blocks = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp);
1027 if (swap_blocks)
1028 swap(blk1, blk2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Dave Chinner1d9025e2012-06-22 18:50:14 +10001030 leaf1 = blk1->bp->b_addr;
1031 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001032 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1033 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001034 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1035 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001036
1037 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001038#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001039 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040#endif
1041 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * If the old leaf count was odd then the new one will be even,
1045 * so we need to divide the new count evenly.
1046 */
1047 if (oldsum & 1) {
1048 xfs_dahash_t midhash; /* middle entry hash value */
1049
Dave Chinner24df33b2013-04-12 07:30:21 +10001050 if (mid >= hdr1.count)
1051 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001053 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 isleft = args->hashval <= midhash;
1055 }
1056 /*
1057 * If the old count is even then the new count is odd, so there's
1058 * no preferred side for the new entry.
1059 * Pick the left one.
1060 */
1061 else
1062 isleft = 1;
1063 /*
1064 * Calculate moved entry count. Positive means left-to-right,
1065 * negative means right-to-left. Then move the entries.
1066 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001067 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001069 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1070 hdr1.count - count, blk2->bp,
1071 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001073 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1074 blk1->bp, &hdr1, ents1,
1075 hdr1.count, count);
1076
1077 ASSERT(hdr1.count + hdr2.count == oldsum);
1078 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1079
1080 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001081 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1082 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001083 xfs_dir3_leaf_log_header(args, blk1->bp);
1084 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001085
Dave Chinner41419562013-10-29 22:11:50 +11001086 xfs_dir3_leaf_check(dp, blk1->bp);
1087 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Mark whether we're inserting into the old or new leaf.
1091 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001092 if (hdr1.count < hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001093 state->inleaf = swap_blocks;
Dave Chinner24df33b2013-04-12 07:30:21 +10001094 else if (hdr1.count > hdr2.count)
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001095 state->inleaf = !swap_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 else
Gustavo A. R. Silvae4e542a2018-07-17 14:25:01 -07001097 state->inleaf = swap_blocks ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
1099 * Adjust the expected index for insertion.
1100 */
1101 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001102 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001103
1104 /*
1105 * Finally sanity check just to make sure we are not returning a
1106 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
Dave Chinner41419562013-10-29 22:11:50 +11001108 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 state->inleaf = 1;
1110 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001111 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001112 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001113 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115}
1116
Dave Chinner20252072012-11-12 22:54:13 +11001117static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001118xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001119 xfs_da_args_t *args,
1120 struct xfs_dir2_data_hdr *hdr,
1121 struct xfs_dir2_free *free,
1122 xfs_dir2_db_t fdb,
1123 int findex,
1124 struct xfs_buf *fbp,
1125 int longest)
1126{
Dave Chinner20252072012-11-12 22:54:13 +11001127 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001128 __be16 *bests;
1129 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001130 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001131
Dave Chinner01ba43b2013-10-29 22:11:52 +11001132 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001133 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001134 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001135 /*
1136 * Data block is not empty, just set the free entry to the new
1137 * value.
1138 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001139 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001140 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001141 return 0;
1142 }
1143
1144 /* One less used entry in the free table. */
1145 freehdr.nused--;
1146
1147 /*
1148 * If this was the last entry in the table, we can trim the table size
1149 * back. There might be other entries at the end referring to
1150 * non-existent data blocks, get those too.
1151 */
1152 if (findex == freehdr.nvalid - 1) {
1153 int i; /* free entry index */
1154
1155 for (i = findex - 1; i >= 0; i--) {
1156 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1157 break;
1158 }
1159 freehdr.nvalid = i + 1;
1160 logfree = 0;
1161 } else {
1162 /* Not the last entry, just punch it out. */
1163 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001164 logfree = 1;
1165 }
1166
Dave Chinner01ba43b2013-10-29 22:11:52 +11001167 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001168 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001169
1170 /*
1171 * If there are no useful entries left in the block, get rid of the
1172 * block if we can.
1173 */
1174 if (!freehdr.nused) {
1175 int error;
1176
1177 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1178 if (error == 0) {
1179 fbp = NULL;
1180 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001181 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001182 return error;
1183 /*
1184 * It's possible to get ENOSPC if there is no
1185 * space reservation. In this case some one
1186 * else will eventually get rid of this block.
1187 */
1188 }
1189
Dave Chinner20252072012-11-12 22:54:13 +11001190 /* Log the free entry that changed, unless we got rid of it. */
1191 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001192 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001193 return 0;
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196/*
1197 * Remove an entry from a node directory.
1198 * This removes the leaf entry and the data entry,
1199 * and updates the free block if necessary.
1200 */
1201static int /* error */
1202xfs_dir2_leafn_remove(
1203 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001204 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int index, /* leaf entry index */
1206 xfs_da_state_blk_t *dblk, /* data block */
1207 int *rval) /* resulting block needs join */
1208{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001209 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001211 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 xfs_dir2_data_entry_t *dep; /* data block entry */
1213 xfs_inode_t *dp; /* incore directory inode */
1214 xfs_dir2_leaf_t *leaf; /* leaf structure */
1215 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1216 int longest; /* longest data free entry */
1217 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int needlog; /* need to log data header */
1219 int needscan; /* need to rescan data frees */
1220 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001221 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001222 struct xfs_dir3_icleaf_hdr leafhdr;
1223 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001225 trace_xfs_dir2_leafn_remove(args, index);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 dp = args->dp;
1228 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001229 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001230 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001231 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * Point to the entry we're removing.
1235 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001236 lep = &ents[index];
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * Extract the data block and offset from the entry.
1240 */
Dave Chinner30028032014-06-06 15:08:18 +10001241 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001243 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Kill the leaf entry by marking it stale.
1248 * Log the leaf block changes.
1249 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001250 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001251 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001252 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001253
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001254 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001255 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /*
1258 * Make the data entry free. Keep track of the longest freespace
1259 * in the data block in case it changes.
1260 */
1261 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001262 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001263 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001264 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001265 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001267 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001268 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * Rescan the data block freespaces for bestfree.
1271 * Log the data block header if needed.
1272 */
1273 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001274 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001276 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001277 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * If the longest data block freespace changes, need to update
1280 * the corresponding freeblock entry.
1281 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001282 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001284 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 xfs_dir2_db_t fdb; /* freeblock block number */
1286 int findex; /* index in freeblock entries */
1287 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /*
1290 * Convert the data block number to a free block,
1291 * read in the free block.
1292 */
Dave Chinner8f661932014-06-06 15:15:59 +10001293 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001294 error = xfs_dir2_free_read(tp, dp,
1295 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001296 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001299 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001300#ifdef DEBUG
1301 {
1302 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001303 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001304 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001305 (fdb - xfs_dir2_byte_to_db(args->geo,
1306 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001307 }
1308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 /*
1310 * Calculate which entry we need to fix.
1311 */
Dave Chinner8f661932014-06-06 15:15:59 +10001312 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001313 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
1315 * If the data block is now empty we can get rid of it
1316 * (usually).
1317 */
Dave Chinner8f661932014-06-06 15:15:59 +10001318 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001319 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 /*
1321 * Try to punch out the data block.
1322 */
1323 error = xfs_dir2_shrink_inode(args, db, dbp);
1324 if (error == 0) {
1325 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001326 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 /*
1329 * We can get ENOSPC if there's no space reservation.
1330 * In this case just drop the buffer and some one else
1331 * will eventually get rid of the empty block.
1332 */
Dave Chinner24513372014-06-25 14:58:08 +10001333 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return error;
1335 }
1336 /*
1337 * If we got rid of the data block, we can eliminate that entry
1338 * in the free block.
1339 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001340 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001341 fdb, findex, fbp, longest);
1342 if (error)
1343 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Dave Chinner20252072012-11-12 22:54:13 +11001345
Dave Chinner41419562013-10-29 22:11:50 +11001346 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001348 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * to justify trying to join it with a neighbor.
1350 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001351 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001352 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001353 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return 0;
1355}
1356
1357/*
1358 * Split the leaf entries in the old block into old and new blocks.
1359 */
1360int /* error */
1361xfs_dir2_leafn_split(
1362 xfs_da_state_t *state, /* btree cursor */
1363 xfs_da_state_blk_t *oldblk, /* original block */
1364 xfs_da_state_blk_t *newblk) /* newly created block */
1365{
1366 xfs_da_args_t *args; /* operation arguments */
1367 xfs_dablk_t blkno; /* new leaf block number */
1368 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001369 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
1372 * Allocate space for a new leaf node.
1373 */
1374 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001375 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1377 error = xfs_da_grow_inode(args, &blkno);
1378 if (error) {
1379 return error;
1380 }
1381 /*
1382 * Initialize the new leaf block.
1383 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001384 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001385 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1386 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 newblk->blkno = blkno;
1390 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1391 /*
1392 * Rebalance the entries across the two leaves, link the new
1393 * block into the leaves.
1394 */
1395 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001396 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (error) {
1398 return error;
1399 }
1400 /*
1401 * Insert the new entry in the correct block.
1402 */
1403 if (state->inleaf)
1404 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1405 else
1406 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1407 /*
1408 * Update last hashval in each block since we added the name.
1409 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001410 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1411 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001412 xfs_dir3_leaf_check(dp, oldblk->bp);
1413 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return error;
1415}
1416
1417/*
1418 * Check a leaf block and its neighbors to see if the block should be
1419 * collapsed into one or the other neighbor. Always keep the block
1420 * with the smaller block number.
1421 * If the current block is over 50% full, don't try to join it, return 0.
1422 * If the block is empty, fill in the state structure and return 2.
1423 * If it can be collapsed, fill in the state structure and return 1.
1424 * If nothing can be done, return 0.
1425 */
1426int /* error */
1427xfs_dir2_leafn_toosmall(
1428 xfs_da_state_t *state, /* btree cursor */
1429 int *action) /* resulting action to take */
1430{
1431 xfs_da_state_blk_t *blk; /* leaf block */
1432 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001433 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int bytes; /* bytes in use */
1435 int count; /* leaf live entry count */
1436 int error; /* error return value */
1437 int forward; /* sibling block direction */
1438 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 xfs_dir2_leaf_t *leaf; /* leaf structure */
1440 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001441 struct xfs_dir3_icleaf_hdr leafhdr;
1442 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001443 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /*
1446 * Check for the degenerate case of the block being over 50% full.
1447 * If so, it's not worth even looking to see if we might be able
1448 * to coalesce with a sibling.
1449 */
1450 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001451 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001452 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001453 ents = dp->d_ops->leaf_ents_p(leaf);
1454 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001455
1456 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001457 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001458 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /*
1460 * Blk over 50%, don't try to join.
1461 */
1462 *action = 0;
1463 return 0;
1464 }
1465 /*
1466 * Check for the degenerate case of the block being empty.
1467 * If the block is empty, we'll simply delete it, no need to
1468 * coalesce it with a sibling block. We choose (arbitrarily)
1469 * to merge with the forward block unless it is NULL.
1470 */
1471 if (count == 0) {
1472 /*
1473 * Make altpath point to the block we want to keep and
1474 * path point to the block we want to drop (this one).
1475 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001476 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001478 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 &rval);
1480 if (error)
1481 return error;
1482 *action = rval ? 2 : 0;
1483 return 0;
1484 }
1485 /*
1486 * Examine each sibling block to see if we can coalesce with
1487 * at least 25% free space to spare. We need to figure out
1488 * whether to merge with the forward or the backward block.
1489 * We prefer coalescing with the lower numbered sibling so as
1490 * to shrink a directory over time.
1491 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001492 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001494 struct xfs_dir3_icleaf_hdr hdr2;
1495
1496 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (blkno == 0)
1498 continue;
1499 /*
1500 * Read the sibling leaf block.
1501 */
Dave Chinner41419562013-10-29 22:11:50 +11001502 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001503 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001504 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /*
1508 * Count bytes in the two blocks combined.
1509 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001510 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001511 bytes = state->args->geo->blksize -
1512 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001513
Dave Chinner1d9025e2012-06-22 18:50:14 +10001514 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001515 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001516 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001517 count += hdr2.count - hdr2.stale;
1518 bytes -= count * sizeof(ents[0]);
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * Fits with at least 25% to spare.
1522 */
1523 if (bytes >= 0)
1524 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001525 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 /*
1528 * Didn't like either block, give up.
1529 */
1530 if (i >= 2) {
1531 *action = 0;
1532 return 0;
1533 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 /*
1536 * Make altpath point to the block we want to keep (the lower
1537 * numbered block) and path point to the block we want to drop.
1538 */
1539 memcpy(&state->altpath, &state->path, sizeof(state->path));
1540 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001541 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 &rval);
1543 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001544 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 &rval);
1546 if (error) {
1547 return error;
1548 }
1549 *action = rval ? 0 : 1;
1550 return 0;
1551}
1552
1553/*
1554 * Move all the leaf entries from drop_blk to save_blk.
1555 * This is done as part of a join operation.
1556 */
1557void
1558xfs_dir2_leafn_unbalance(
1559 xfs_da_state_t *state, /* cursor */
1560 xfs_da_state_blk_t *drop_blk, /* dead block */
1561 xfs_da_state_blk_t *save_blk) /* surviving block */
1562{
1563 xfs_da_args_t *args; /* operation arguments */
1564 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1565 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001566 struct xfs_dir3_icleaf_hdr savehdr;
1567 struct xfs_dir3_icleaf_hdr drophdr;
1568 struct xfs_dir2_leaf_entry *sents;
1569 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001570 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 args = state->args;
1573 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1574 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575 drop_leaf = drop_blk->bp->b_addr;
1576 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001577
Dave Chinner01ba43b2013-10-29 22:11:52 +11001578 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1579 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1580 sents = dp->d_ops->leaf_ents_p(save_leaf);
1581 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /*
1584 * If there are any stale leaf entries, take this opportunity
1585 * to purge them.
1586 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001587 if (drophdr.stale)
1588 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1589 if (savehdr.stale)
1590 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /*
1593 * Move the entries from drop to the appropriate end of save.
1594 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001595 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001596 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001597 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1598 save_blk->bp, &savehdr, sents, 0,
1599 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 else
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,
1603 savehdr.count, drophdr.count);
1604 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1605
1606 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001607 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1608 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001609 xfs_dir3_leaf_log_header(args, save_blk->bp);
1610 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001611
Dave Chinner41419562013-10-29 22:11:50 +11001612 xfs_dir3_leaf_check(dp, save_blk->bp);
1613 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
1617 * Top-level node form directory addname routine.
1618 */
1619int /* error */
1620xfs_dir2_node_addname(
1621 xfs_da_args_t *args) /* operation arguments */
1622{
1623 xfs_da_state_blk_t *blk; /* leaf block for insert */
1624 int error; /* error return value */
1625 int rval; /* sub-return value */
1626 xfs_da_state_t *state; /* btree cursor */
1627
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001628 trace_xfs_dir2_node_addname(args);
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Allocate and initialize the state (btree cursor).
1632 */
1633 state = xfs_da_state_alloc();
1634 state->args = args;
1635 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /*
1637 * Look up the name. We're not supposed to find it, but
1638 * this gives us the insertion point.
1639 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001640 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (error)
1642 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001643 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto done;
1645 }
1646 /*
1647 * Add the data entry to a data block.
1648 * Extravalid is set to a freeblock found by lookup.
1649 */
1650 rval = xfs_dir2_node_addname_int(args,
1651 state->extravalid ? &state->extrablk : NULL);
1652 if (rval) {
1653 goto done;
1654 }
1655 blk = &state->path.blk[state->path.active - 1];
1656 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1657 /*
1658 * Add the new leaf entry.
1659 */
1660 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1661 if (rval == 0) {
1662 /*
1663 * It worked, fix the hash values up the btree.
1664 */
Barry Naujok6a178102008-05-21 16:42:05 +10001665 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001666 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 } else {
1668 /*
1669 * It didn't work, we need to split the leaf block.
1670 */
1671 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001672 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto done;
1674 }
1675 /*
1676 * Split the leaf block and insert the new entry.
1677 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001678 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680done:
1681 xfs_da_state_free(state);
1682 return rval;
1683}
1684
1685/*
1686 * Add the data entry for a node-format directory name addition.
1687 * The leaf entry is added in xfs_dir2_leafn_add.
1688 * We may enter with a freespace block that the lookup found.
1689 */
1690static int /* error */
1691xfs_dir2_node_addname_int(
1692 xfs_da_args_t *args, /* operation arguments */
1693 xfs_da_state_blk_t *fblk) /* optional freespace block */
1694{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001695 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001697 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1699 xfs_inode_t *dp; /* incore directory inode */
1700 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1701 int error; /* error return value */
1702 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001703 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int findex; /* freespace entry index */
1705 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1706 xfs_dir2_db_t ifbno; /* initial freespace block no */
1707 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1708 int length; /* length of the new entry */
1709 int logfree; /* need to log free entry */
1710 xfs_mount_t *mp; /* filesystem mount point */
1711 int needlog; /* need to log data header */
1712 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001713 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001715 __be16 *bests;
1716 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001717 struct xfs_dir2_data_free *bf;
Darrick J. Wong6915ef32018-03-23 10:06:51 -07001718 xfs_dir2_data_aoff_t aoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 dp = args->dp;
1721 mp = dp->i_mount;
1722 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001723 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /*
1725 * If we came in with a freespace block that means that lookup
1726 * found an entry with our hash value. This is the freespace
1727 * block for that data entry.
1728 */
1729 if (fblk) {
1730 fbp = fblk->bp;
1731 /*
1732 * Remember initial freespace block number.
1733 */
1734 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001735 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001737 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001738 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /*
1741 * This means the free entry showed that the data block had
1742 * space for our entry, so we remembered it.
1743 * Use that data block.
1744 */
1745 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001746 ASSERT(findex < freehdr.nvalid);
1747 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1748 ASSERT(be16_to_cpu(bests[findex]) >= length);
1749 dbno = freehdr.firstdb + findex;
1750 } else {
1751 /*
1752 * The data block looked at didn't have enough room.
1753 * We'll start at the beginning of the freespace entries.
1754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 dbno = -1;
1756 findex = 0;
1757 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001758 } else {
1759 /*
1760 * Didn't come in with a freespace block, so no data block.
1761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 ifbno = dbno = -1;
1763 fbp = NULL;
1764 findex = 0;
1765 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * If we don't have a data block yet, we're going to scan the
1769 * freespace blocks looking for one. Figure out what the
1770 * highest freespace block number is.
1771 */
1772 if (dbno == -1) {
1773 xfs_fileoff_t fo; /* freespace block number */
1774
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001775 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001777 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 fbno = ifbno;
1779 }
1780 /*
1781 * While we haven't identified a data block, search the freeblock
1782 * data for a good data block. If we find a null freeblock entry,
1783 * indicating a hole in the data blocks, remember that.
1784 */
1785 while (dbno == -1) {
1786 /*
1787 * If we don't have a freeblock in hand, get the next one.
1788 */
1789 if (fbp == NULL) {
1790 /*
1791 * Happens the first time through unless lookup gave
1792 * us a freespace block to start with.
1793 */
1794 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001795 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001796 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * If it's ifbno we already looked at it.
1799 */
1800 if (fbno == ifbno)
1801 fbno++;
1802 /*
1803 * If it's off the end we're done.
1804 */
1805 if (fbno >= lastfbno)
1806 break;
1807 /*
1808 * Read the block. There can be holes in the
1809 * freespace blocks, so this might not succeed.
1810 * This should be really rare, so there's no reason
1811 * to avoid it.
1812 */
Dave Chinner20252072012-11-12 22:54:13 +11001813 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001814 xfs_dir2_db_to_da(args->geo, fbno),
1815 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001818 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001820 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 findex = 0;
1822 }
1823 /*
1824 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001825 *
1826 * The bests initialisation should be where the bufer is read in
1827 * the above branch. But gcc is too stupid to realise that bests
1828 * and the freehdr are actually initialised if they are placed
1829 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001831 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001832 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001833 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1834 be16_to_cpu(bests[findex]) >= length)
1835 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 else {
1837 /*
1838 * Are we done with the freeblock?
1839 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001840 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /*
1842 * Drop the block.
1843 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001844 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 fbp = NULL;
1846 if (fblk && fblk->bp)
1847 fblk->bp = NULL;
1848 }
1849 }
1850 }
1851 /*
1852 * If we don't have a data block, we need to allocate one and make
1853 * the freespace entries refer to it.
1854 */
1855 if (unlikely(dbno == -1)) {
1856 /*
1857 * Not allowed to allocate, return failure.
1858 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001859 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001860 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /*
1863 * Allocate and initialize the new data block.
1864 */
1865 if (unlikely((error = xfs_dir2_grow_inode(args,
1866 XFS_DIR2_DATA_SPACE,
1867 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001868 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 /*
1872 * If (somehow) we have a freespace block, get rid of it.
1873 */
1874 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001875 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (fblk && fblk->bp)
1877 fblk->bp = NULL;
1878
1879 /*
1880 * Get the freespace block corresponding to the data block
1881 * that was just allocated.
1882 */
Dave Chinner8f661932014-06-06 15:15:59 +10001883 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001884 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001885 xfs_dir2_db_to_da(args->geo, fbno),
1886 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * If there wasn't a freespace block, the read will
1892 * return a NULL fbp. Allocate and initialize a new one.
1893 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001894 if (!fbp) {
1895 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1896 &fbno);
1897 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Dave Chinner8f661932014-06-06 15:15:59 +10001900 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001901 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001902"%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 +11001903 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001904 (long long)dp->d_ops->db_to_fdb(
1905 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 (long long)dbno, (long long)fbno,
1907 (unsigned long long)ifbno, lastfbno);
1908 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001909 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08001910 " fblk "PTR_FMT" blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 fblk,
1912 (unsigned long long)fblk->blkno,
1913 fblk->index,
1914 fblk->magic);
1915 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001916 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1919 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001920 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
1923 /*
1924 * Get a buffer for the new block.
1925 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001926 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001927 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001929 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001930 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001931 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001934 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001936 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001937 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001938 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001939 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001941 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001942 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001943 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
1946 /*
1947 * Set the freespace block index from the data block number.
1948 */
Dave Chinner8f661932014-06-06 15:15:59 +10001949 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /*
1951 * If it's after the end of the current entries in the
1952 * freespace block, extend that table.
1953 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001954 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001955 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001956 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /*
1958 * Tag new entry so nused will go up.
1959 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001960 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 /*
1963 * If this entry was for an empty data block
1964 * (this should always be true) then update the header.
1965 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001966 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1967 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001968 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001969 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 /*
1972 * Update the real value in the table.
1973 * We haven't allocated the data entry yet so this will
1974 * change again.
1975 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001976 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001977 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001978 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 logfree = 1;
1980 }
1981 /*
1982 * We had a data block so we don't have to make a new one.
1983 */
1984 else {
1985 /*
1986 * If just checking, we succeeded.
1987 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001988 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Read the data block in.
1993 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001994 error = xfs_dir3_data_read(tp, dp,
1995 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001996 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001997 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001999 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11002000 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 logfree = 0;
2002 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002003 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /*
2005 * Point to the existing unused space.
2006 */
2007 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002008 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 needscan = needlog = 0;
2010 /*
2011 * Mark the first part of the unused space, inuse for us.
2012 */
Darrick J. Wong6915ef32018-03-23 10:06:51 -07002013 aoff = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
2014 error = xfs_dir2_data_use_free(args, dbp, dup, aoff, length,
2015 &needlog, &needscan);
2016 if (error) {
2017 xfs_trans_brelse(tp, dbp);
2018 return error;
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /*
2021 * Fill in the new entry and log it.
2022 */
2023 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002024 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 dep->namelen = args->namelen;
2026 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002027 dp->d_ops->data_put_ftype(dep, args->filetype);
2028 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002029 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002030 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
2032 * Rescan the block for bestfree if needed.
2033 */
2034 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002035 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /*
2037 * Log the data block header if needed.
2038 */
2039 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002040 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * If the freespace entry is now wrong, update it.
2043 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002044 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002045 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2046 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 logfree = 1;
2048 }
2049 /*
2050 * Log the freespace entry if needed.
2051 */
2052 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002053 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 * Return the data block and offset in args, then drop the data block.
2056 */
2057 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002058 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return 0;
2060}
2061
2062/*
2063 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002064 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * The only real output is the inode number of the entry.
2066 */
2067int /* error */
2068xfs_dir2_node_lookup(
2069 xfs_da_args_t *args) /* operation arguments */
2070{
2071 int error; /* error return value */
2072 int i; /* btree level */
2073 int rval; /* operation return value */
2074 xfs_da_state_t *state; /* btree cursor */
2075
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002076 trace_xfs_dir2_node_lookup(args);
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /*
2079 * Allocate and initialize the btree cursor.
2080 */
2081 state = xfs_da_state_alloc();
2082 state->args = args;
2083 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Fill in the path to the entry in the cursor.
2086 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002087 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (error)
2089 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002090 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2091 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002092 xfs_dir2_data_entry_t *dep;
2093
Dave Chinner1d9025e2012-06-22 18:50:14 +10002094 dep = (xfs_dir2_data_entry_t *)
2095 ((char *)state->extrablk.bp->b_addr +
2096 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002097 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * Release the btree blocks and leaf block.
2101 */
2102 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002103 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 state->path.blk[i].bp = NULL;
2105 }
2106 /*
2107 * Release the data block if we have it.
2108 */
2109 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002110 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 state->extrablk.bp = NULL;
2112 }
2113 xfs_da_state_free(state);
2114 return rval;
2115}
2116
2117/*
2118 * Remove an entry from a node-format directory.
2119 */
2120int /* error */
2121xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002122 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002124 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int error; /* error return value */
2126 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002127 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002129 trace_xfs_dir2_node_removename(args);
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 /*
2132 * Allocate and initialize the btree cursor.
2133 */
2134 state = xfs_da_state_alloc();
2135 state->args = args;
2136 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002137
2138 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002139 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002140 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002141 goto out_free;
2142
2143 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002144 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002145 error = rval;
2146 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 blk = &state->path.blk[state->path.active - 1];
2150 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2151 ASSERT(state->extravalid);
2152 /*
2153 * Remove the leaf and data entries.
2154 * Extrablk refers to the data block.
2155 */
2156 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2157 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002158 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002159 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * Fix the hash values up the btree.
2162 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002163 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 /*
2165 * If we need to join leaf blocks, do it.
2166 */
2167 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002168 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * If no errors so far, try conversion to leaf format.
2171 */
2172 if (!error)
2173 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002174out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_da_state_free(state);
2176 return error;
2177}
2178
2179/*
2180 * Replace an entry's inode number in a node-format directory.
2181 */
2182int /* error */
2183xfs_dir2_node_replace(
2184 xfs_da_args_t *args) /* operation arguments */
2185{
2186 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002187 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 xfs_dir2_data_entry_t *dep; /* data entry changed */
2189 int error; /* error return value */
2190 int i; /* btree level */
2191 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002192 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 xfs_dir2_leaf_t *leaf; /* leaf structure */
2194 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2195 int rval; /* internal return value */
2196 xfs_da_state_t *state; /* btree cursor */
2197
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002198 trace_xfs_dir2_node_replace(args);
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /*
2201 * Allocate and initialize the btree cursor.
2202 */
2203 state = xfs_da_state_alloc();
2204 state->args = args;
2205 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002206
2207 /*
2208 * We have to save new inode number and ftype since
2209 * xfs_da3_node_lookup_int() is going to overwrite them
2210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002212 ftype = args->filetype;
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /*
2215 * Lookup the entry to change in the btree.
2216 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002217 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (error) {
2219 rval = error;
2220 }
2221 /*
2222 * It should be found, since the vnodeops layer has looked it up
2223 * and locked it. But paranoia is good.
2224 */
Dave Chinner24513372014-06-25 14:58:08 +10002225 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002226 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * Find the leaf entry.
2229 */
2230 blk = &state->path.blk[state->path.active - 1];
2231 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002232 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002233 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002234 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 ASSERT(state->extravalid);
2236 /*
2237 * Point to the data entry.
2238 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002239 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002240 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2241 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002243 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002244 xfs_dir2_dataptr_to_off(args->geo,
2245 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002246 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /*
2248 * Fill in the new inode number and log the entry.
2249 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002250 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002251 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002252 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 rval = 0;
2254 }
2255 /*
2256 * Didn't find it, and we're holding a data block. Drop it.
2257 */
2258 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002259 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 state->extrablk.bp = NULL;
2261 }
2262 /*
2263 * Release all the buffers in the cursor.
2264 */
2265 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002266 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 state->path.blk[i].bp = NULL;
2268 }
2269 xfs_da_state_free(state);
2270 return rval;
2271}
2272
2273/*
2274 * Trim off a trailing empty freespace block.
2275 * Return (in rvalp) 1 if we did it, 0 if not.
2276 */
2277int /* error */
2278xfs_dir2_node_trim_free(
2279 xfs_da_args_t *args, /* operation arguments */
2280 xfs_fileoff_t fo, /* free block number */
2281 int *rvalp) /* out: did something */
2282{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002283 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 xfs_inode_t *dp; /* incore directory inode */
2285 int error; /* error return code */
2286 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002288 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002292
2293 *rvalp = 0;
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /*
2296 * Read the freespace block.
2297 */
Dave Chinner20252072012-11-12 22:54:13 +11002298 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 /*
2302 * There can be holes in freespace. If fo is a hole, there's
2303 * nothing to do.
2304 */
Dave Chinner20252072012-11-12 22:54:13 +11002305 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002307 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002308 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * If there are used entries, there's nothing to do.
2312 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002313 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002314 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return 0;
2316 }
2317 /*
2318 * Blow the block away.
2319 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002320 error = xfs_dir2_shrink_inode(args,
2321 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2322 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Can't fail with ENOSPC since that only happens with no
2325 * space reservation, when breaking up an extent into two
2326 * pieces. This is the last block of an extent.
2327 */
Dave Chinner24513372014-06-25 14:58:08 +10002328 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002329 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return error;
2331 }
2332 /*
2333 * Return that we succeeded.
2334 */
2335 *rvalp = 1;
2336 return 0;
2337}