blob: 915c4fe5e4c3ca6a2f7c715f77230de978a3f174 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
28#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000032#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110034#include "xfs_buf_item.h"
35#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110036#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Function declarations.
40 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100041static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 xfs_da_state_blk_t *blk1,
45 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int index, xfs_da_state_blk_t *dblk,
48 int *rval);
49static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 xfs_da_state_blk_t *fblk);
51
Dave Chinner24df33b2013-04-12 07:30:21 +100052/*
53 * Check internal consistency of a leafn block.
54 */
55#ifdef DEBUG
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080056static xfs_failaddr_t
Dave Chinner24df33b2013-04-12 07:30:21 +100057xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110058 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100059 struct xfs_buf *bp)
60{
61 struct xfs_dir2_leaf *leaf = bp->b_addr;
62 struct xfs_dir3_icleaf_hdr leafhdr;
63
Dave Chinner01ba43b2013-10-29 22:11:52 +110064 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100065
66 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
67 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
68 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080069 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100070 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080071 return __this_address;
Dave Chinner24df33b2013-04-12 07:30:21 +100072
Dave Chinner41419562013-10-29 22:11:50 +110073 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100074}
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080075
76static inline void
77xfs_dir3_leaf_check(
78 struct xfs_inode *dp,
79 struct xfs_buf *bp)
80{
81 xfs_failaddr_t fa;
82
83 fa = xfs_dir3_leafn_check(dp, bp);
84 if (!fa)
85 return;
86 xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount,
87 bp->b_addr, __FILE__, __LINE__, fa);
88 ASSERT(0);
89}
Dave Chinner24df33b2013-04-12 07:30:21 +100090#else
Dave Chinner41419562013-10-29 22:11:50 +110091#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100092#endif
93
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080094static xfs_failaddr_t
Dave Chinnercbc8adf2013-04-03 16:11:21 +110095xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110096 struct xfs_buf *bp)
97{
98 struct xfs_mount *mp = bp->b_target->bt_mount;
99 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +1100100
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100101 if (xfs_sb_version_hascrc(&mp->m_sb)) {
102 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
103
104 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800105 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000106 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800107 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100108 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800109 return __this_address;
Brian Fostera45086e2015-10-12 15:59:25 +1100110 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800111 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100112 } else {
113 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800114 return __this_address;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100115 }
116
117 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
118
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800119 return NULL;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100120}
121
122static void
123xfs_dir3_free_read_verify(
124 struct xfs_buf *bp)
125{
126 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800127 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128
Eric Sandeence5028c2014-02-27 15:23:10 +1100129 if (xfs_sb_version_hascrc(&mp->m_sb) &&
130 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800131 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
132 else {
133 fa = xfs_dir3_free_verify(bp);
134 if (fa)
135 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
136 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100137}
Dave Chinner20252072012-11-12 22:54:13 +1100138
Dave Chinner612cfbf2012-11-14 17:52:32 +1100139static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100140xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100141 struct xfs_buf *bp)
142{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100143 struct xfs_mount *mp = bp->b_target->bt_mount;
144 struct xfs_buf_log_item *bip = bp->b_fspriv;
145 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800146 xfs_failaddr_t fa;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100147
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800148 fa = xfs_dir3_free_verify(bp);
149 if (fa) {
150 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100151 return;
152 }
153
154 if (!xfs_sb_version_hascrc(&mp->m_sb))
155 return;
156
157 if (bip)
158 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
159
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100160 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100161}
162
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100163const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100164 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100165 .verify_read = xfs_dir3_free_read_verify,
166 .verify_write = xfs_dir3_free_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800167 .verify_struct = xfs_dir3_free_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100168};
Dave Chinner20252072012-11-12 22:54:13 +1100169
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800170/* Everything ok in the free block header? */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800171static xfs_failaddr_t
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800172xfs_dir3_free_header_check(
173 struct xfs_inode *dp,
174 xfs_dablk_t fbno,
175 struct xfs_buf *bp)
176{
177 struct xfs_mount *mp = dp->i_mount;
178 unsigned int firstdb;
179 int maxbests;
180
181 maxbests = dp->d_ops->free_max_bests(mp->m_dir_geo);
182 firstdb = (xfs_dir2_da_to_db(mp->m_dir_geo, fbno) -
183 xfs_dir2_byte_to_db(mp->m_dir_geo, XFS_DIR2_FREE_OFFSET)) *
184 maxbests;
185 if (xfs_sb_version_hascrc(&mp->m_sb)) {
186 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
187
188 if (be32_to_cpu(hdr3->firstdb) != firstdb)
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(hdr3->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800191 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800192 if (be32_to_cpu(hdr3->nvalid) < be32_to_cpu(hdr3->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800193 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800194 } else {
195 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
196
197 if (be32_to_cpu(hdr->firstdb) != firstdb)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800198 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800199 if (be32_to_cpu(hdr->nvalid) > maxbests)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800200 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800201 if (be32_to_cpu(hdr->nvalid) < be32_to_cpu(hdr->nused))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800202 return __this_address;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800203 }
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800204 return NULL;
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800205}
Dave Chinner612cfbf2012-11-14 17:52:32 +1100206
Dave Chinner20252072012-11-12 22:54:13 +1100207static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100208__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100209 struct xfs_trans *tp,
210 struct xfs_inode *dp,
211 xfs_dablk_t fbno,
212 xfs_daddr_t mappedbno,
213 struct xfs_buf **bpp)
214{
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800215 xfs_failaddr_t fa;
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100216 int err;
217
218 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100219 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800220 if (err || !*bpp)
221 return err;
222
223 /* Check things that we can't do in the verifier. */
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800224 fa = xfs_dir3_free_header_check(dp, fbno, *bpp);
225 if (fa) {
226 xfs_verifier_error(*bpp, -EFSCORRUPTED, fa);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800227 xfs_trans_brelse(tp, *bpp);
228 return -EFSCORRUPTED;
229 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100230
231 /* try read returns without an error or *bpp if it lands in a hole */
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800232 if (tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100233 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Darrick J. Wongde14c5f2017-02-02 15:14:00 -0800234
235 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100236}
237
238int
239xfs_dir2_free_read(
240 struct xfs_trans *tp,
241 struct xfs_inode *dp,
242 xfs_dablk_t fbno,
243 struct xfs_buf **bpp)
244{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100245 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100246}
247
248static int
249xfs_dir2_free_try_read(
250 struct xfs_trans *tp,
251 struct xfs_inode *dp,
252 xfs_dablk_t fbno,
253 struct xfs_buf **bpp)
254{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100255 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
256}
257
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100258static int
259xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000260 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100261 xfs_dir2_db_t fbno,
262 struct xfs_buf **bpp)
263{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000264 struct xfs_trans *tp = args->trans;
265 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100266 struct xfs_mount *mp = dp->i_mount;
267 struct xfs_buf *bp;
268 int error;
269 struct xfs_dir3_icfree_hdr hdr;
270
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000271 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100272 -1, &bp, XFS_DATA_FORK);
273 if (error)
274 return error;
275
Dave Chinner61fe1352013-04-03 16:11:30 +1100276 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100277 bp->b_ops = &xfs_dir3_free_buf_ops;
278
279 /*
280 * Initialize the new block to be empty, and remember
281 * its first slot as our empty slot.
282 */
Dave Chinnere400d272013-05-28 18:37:17 +1000283 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
284 memset(&hdr, 0, sizeof(hdr));
285
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100286 if (xfs_sb_version_hascrc(&mp->m_sb)) {
287 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
288
289 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000290
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100291 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
292 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000293 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000294 } else
295 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100296 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100297 *bpp = bp;
298 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
302 * Log entries from a freespace block.
303 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000304STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000306 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000307 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int first, /* first entry to log */
309 int last) /* last entry to log */
310{
311 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100312 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dave Chinner1d9025e2012-06-22 18:50:14 +1000314 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000315 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100316 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
317 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000318 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100319 (uint)((char *)&bests[first] - (char *)free),
320 (uint)((char *)&bests[last] - (char *)free +
321 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Log header from a freespace block.
326 */
327static void
328xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000329 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000330 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Dave Chinner836a94a2013-08-12 20:49:44 +1000332#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_dir2_free_t *free; /* freespace structure */
334
Dave Chinner1d9025e2012-06-22 18:50:14 +1000335 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100336 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
337 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000338#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000339 xfs_trans_log_buf(args->trans, bp, 0,
340 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Convert a leaf-format directory to a node-format directory.
345 * We need to change the magic number of the leaf block, and copy
346 * the freespace table out of the leaf block into its own block.
347 */
348int /* error */
349xfs_dir2_leaf_to_node(
350 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000351 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 xfs_inode_t *dp; /* incore directory inode */
354 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000355 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 xfs_dir2_db_t fdb; /* freespace block number */
357 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100358 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 int i; /* leaf freespace index */
360 xfs_dir2_leaf_t *leaf; /* leaf structure */
361 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int n; /* count of live freespc ents */
363 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100364 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100366 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000368 trace_xfs_dir2_leaf_to_node(args);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 tp = args->trans;
372 /*
373 * Add a freespace block to the directory.
374 */
375 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
376 return error;
377 }
Dave Chinner30028032014-06-06 15:08:18 +1000378 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * Get the buffer for the new freespace block.
381 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000382 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100383 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100385
Dave Chinner1d9025e2012-06-22 18:50:14 +1000386 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100387 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000388 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000389 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100390 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000391 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Copy freespace entries from the leaf block to the new block.
395 * Count active entries.
396 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100397 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500398 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100399 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100400 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100402 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100406 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100408 freehdr.nused = n;
409 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
410
Dave Chinner01ba43b2013-10-29 22:11:52 +1100411 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000412 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
413 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100414
Dave Chinner24df33b2013-04-12 07:30:21 +1000415 /*
416 * Converting the leaf to a leafnode is just a matter of changing the
417 * magic number and the ops. Do the change directly to the buffer as
418 * it's less work (and less code) than decoding the header to host
419 * format and back again.
420 */
421 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
422 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
423 else
424 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
425 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100426 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000427 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100428 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
432/*
433 * Add a leaf entry to a leaf block in a node-form directory.
434 * The other work necessary is done from the caller.
435 */
436static int /* error */
437xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000438 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 xfs_da_args_t *args, /* operation arguments */
440 int index) /* insertion pt for new entry */
441{
442 int compact; /* compacting stale leaves */
443 xfs_inode_t *dp; /* incore directory inode */
444 int highstale; /* next stale entry */
445 xfs_dir2_leaf_t *leaf; /* leaf structure */
446 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
447 int lfloghigh; /* high leaf entry logging */
448 int lfloglow; /* low leaf entry logging */
449 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 struct xfs_dir3_icleaf_hdr leafhdr;
451 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000453 trace_xfs_dir2_leafn_add(args, index);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000456 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100457 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100458 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * Quick check just to make sure we are not going to index
462 * into other peoples memory
463 */
464 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000465 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * If there are already the maximum number of leaf entries in
469 * the block, if there are no stale entries it won't fit.
470 * Caller will do a split. If there are stale entries we'll do
471 * a compact.
472 */
473
Dave Chinner8f661932014-06-06 15:15:59 +1000474 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000475 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000476 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000477 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else
479 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000480 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
481 ASSERT(index == leafhdr.count ||
482 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Barry Naujok6a178102008-05-21 16:42:05 +1000484 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486
487 /*
488 * Compact out all but one stale leaf entry. Leaves behind
489 * the entry closest to index.
490 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000491 if (compact)
492 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
493 &highstale, &lfloglow, &lfloghigh);
494 else if (leafhdr.stale) {
495 /*
496 * Set impossible logging indices for this case.
497 */
498 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 lfloghigh = -1;
500 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Insert the new entry, log everything.
504 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000505 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200506 highstale, &lfloglow, &lfloghigh);
507
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100508 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000509 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100510 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000511
Dave Chinner01ba43b2013-10-29 22:11:52 +1100512 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000513 xfs_dir3_leaf_log_header(args, bp);
514 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100515 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return 0;
517}
518
519#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100520static void
521xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100522 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100523 struct xfs_buf *bp,
524 xfs_dir2_db_t db)
525{
526 struct xfs_dir3_icfree_hdr hdr;
527
Dave Chinner01ba43b2013-10-29 22:11:52 +1100528 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100529
Dave Chinner8f661932014-06-06 15:15:59 +1000530 ASSERT((hdr.firstdb %
531 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100532 ASSERT(hdr.firstdb <= db);
533 ASSERT(db < hdr.firstdb + hdr.nvalid);
534}
535#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100536#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537#endif /* DEBUG */
538
539/*
540 * Return the last hash value in the leaf.
541 * Stale entries are ok.
542 */
543xfs_dahash_t /* hash value */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700544xfs_dir2_leaf_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100545 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000546 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int *count) /* count of entries in leaf */
548{
Dave Chinner24df33b2013-04-12 07:30:21 +1000549 struct xfs_dir2_leaf *leaf = bp->b_addr;
550 struct xfs_dir2_leaf_entry *ents;
551 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Chinner01ba43b2013-10-29 22:11:52 +1100553 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000554
555 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
Darrick J. Wong8e8877e2017-06-16 11:00:13 -0700556 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC ||
557 leafhdr.magic == XFS_DIR2_LEAF1_MAGIC ||
558 leafhdr.magic == XFS_DIR3_LEAF1_MAGIC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000561 *count = leafhdr.count;
562 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000564
Dave Chinner41419562013-10-29 22:11:50 +1100565 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000566 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000570 * Look up a leaf entry for space to add a name in a node-format leaf block.
571 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000573STATIC int
574xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 xfs_da_args_t *args, /* operation arguments */
577 int *indexp, /* out: leaf entry index */
578 xfs_da_state_t *state) /* state to fill in */
579{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000580 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000581 xfs_dir2_db_t curdb = -1; /* current data block number */
582 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 xfs_inode_t *dp; /* incore directory inode */
584 int error; /* error return value */
585 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000586 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int index; /* leaf entry index */
588 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000589 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
591 xfs_mount_t *mp; /* filesystem mount point */
592 xfs_dir2_db_t newdb; /* new data block number */
593 xfs_dir2_db_t newfdb; /* new free block number */
594 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000595 struct xfs_dir2_leaf_entry *ents;
596 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 dp = args->dp;
599 tp = args->trans;
600 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000601 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100602 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100603 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000604
Dave Chinner41419562013-10-29 22:11:50 +1100605 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000606 ASSERT(leafhdr.count > 0);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Look up the hash value in the leaf entries.
610 */
611 index = xfs_dir2_leaf_search_hash(args, bp);
612 /*
613 * Do we have a buffer coming in?
614 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000615 if (state->extravalid) {
616 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000618 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000619 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100620 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
621 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100623 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /*
625 * Loop over leaf entries with the right hash value.
626 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000627 for (lep = &ents[index];
628 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
629 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * Skip stale leaf entries.
632 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100633 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 continue;
635 /*
636 * Pull the data block number from the entry.
637 */
Dave Chinner30028032014-06-06 15:08:18 +1000638 newdb = xfs_dir2_dataptr_to_db(args->geo,
639 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * For addname, we're looking for a place to put the new entry.
642 * We want to use a data block with an entry of equal
643 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 *
645 * If this block isn't the data block we already have
646 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000648 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100649 __be16 *bests;
650
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000651 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000653 * Convert the data block to the free block
654 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Dave Chinner8f661932014-06-06 15:15:59 +1000656 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000658 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
664 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000665 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100666
667 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000668 xfs_dir2_db_to_da(args->geo,
669 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100670 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000671 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100674
Dave Chinner01ba43b2013-10-29 22:11:52 +1100675 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 */
Dave Chinner8f661932014-06-06 15:15:59 +1000680 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000682 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500684 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100685 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000686 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
687 XFS_ERRLEVEL_LOW, mp);
688 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000689 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000690 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000692 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100693 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000697 /* Didn't find any space */
698 fi = -1;
699out:
Barry Naujok6a178102008-05-21 16:42:05 +1000700 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000701 if (curbp) {
702 /* Giving back a free block. */
703 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000705 state->extrablk.index = fi;
706 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100707
708 /*
709 * Important: this magic number is not in the buffer - it's for
710 * buffer type information and therefore only the free/data type
711 * matters here, not whether CRCs are enabled or not.
712 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000713 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
714 } else {
715 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000718 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000721 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
724/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000725 * Look up a leaf entry in a node-format leaf block.
726 * The extrablk in state a data block.
727 */
728STATIC int
729xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000730 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731 xfs_da_args_t *args, /* operation arguments */
732 int *indexp, /* out: leaf entry index */
733 xfs_da_state_t *state) /* state to fill in */
734{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000736 xfs_dir2_db_t curdb = -1; /* current data block number */
737 xfs_dir2_data_entry_t *dep; /* data block entry */
738 xfs_inode_t *dp; /* incore directory inode */
739 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000740 int index; /* leaf entry index */
741 xfs_dir2_leaf_t *leaf; /* leaf structure */
742 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
743 xfs_mount_t *mp; /* filesystem mount point */
744 xfs_dir2_db_t newdb; /* new data block number */
745 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000746 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000747 struct xfs_dir2_leaf_entry *ents;
748 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000749
750 dp = args->dp;
751 tp = args->trans;
752 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000753 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100754 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100755 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000756
Dave Chinner41419562013-10-29 22:11:50 +1100757 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000758 ASSERT(leafhdr.count > 0);
759
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000760 /*
761 * Look up the hash value in the leaf entries.
762 */
763 index = xfs_dir2_leaf_search_hash(args, bp);
764 /*
765 * Do we have a buffer coming in?
766 */
767 if (state->extravalid) {
768 curbp = state->extrablk.bp;
769 curdb = state->extrablk.blkno;
770 }
771 /*
772 * Loop over leaf entries with the right hash value.
773 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000774 for (lep = &ents[index];
775 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
776 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000777 /*
778 * Skip stale leaf entries.
779 */
780 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
781 continue;
782 /*
783 * Pull the data block number from the entry.
784 */
Dave Chinner30028032014-06-06 15:08:18 +1000785 newdb = xfs_dir2_dataptr_to_db(args->geo,
786 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000787 /*
788 * Not adding a new entry, so we really want to find
789 * the name given to us.
790 *
791 * If it's a different data block, go get it.
792 */
793 if (newdb != curdb) {
794 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000795 * If we had a block before that we aren't saving
796 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000797 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000798 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
799 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000800 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000801 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000802 * If needing the block that is saved with a CI match,
803 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000804 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000805 if (args->cmpresult != XFS_CMP_DIFFERENT &&
806 newdb == state->extrablk.blkno) {
807 ASSERT(state->extravalid);
808 curbp = state->extrablk.bp;
809 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100810 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000811 xfs_dir2_db_to_da(args->geo,
812 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100813 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000814 if (error)
815 return error;
816 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100817 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000818 curdb = newdb;
819 }
820 /*
821 * Point to the data entry.
822 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000823 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000824 xfs_dir2_dataptr_to_off(args->geo,
825 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000826 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000827 * Compare the entry and if it's an exact match, return
828 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000829 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000830 */
Barry Naujok5163f952008-05-21 16:41:01 +1000831 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
832 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000833 /* If there is a CI match block, drop it */
834 if (args->cmpresult != XFS_CMP_DIFFERENT &&
835 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000836 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000837 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000838 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100839 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000840 *indexp = index;
841 state->extravalid = 1;
842 state->extrablk.bp = curbp;
843 state->extrablk.blkno = curdb;
844 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000845 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000846 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100847 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100848 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000849 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000850 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000851 }
852 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000853 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000854 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000855 if (args->cmpresult == XFS_CMP_DIFFERENT) {
856 /* Giving back last used data block. */
857 state->extravalid = 1;
858 state->extrablk.bp = curbp;
859 state->extrablk.index = -1;
860 state->extrablk.blkno = curdb;
861 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100862 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100863 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000864 } else {
865 /* If the curbp is not the CI match block, drop it */
866 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000867 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000868 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000869 } else {
870 state->extravalid = 0;
871 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000872 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000873 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000874}
875
876/*
877 * Look up a leaf entry in a node-format leaf block.
878 * If this is an addname then the extrablk in state is a freespace block,
879 * otherwise it's a data block.
880 */
881int
882xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000883 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000884 xfs_da_args_t *args, /* operation arguments */
885 int *indexp, /* out: leaf entry index */
886 xfs_da_state_t *state) /* state to fill in */
887{
Barry Naujok6a178102008-05-21 16:42:05 +1000888 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000889 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
890 state);
891 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
892}
893
894/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * Move count leaf entries from source to destination leaf.
896 * Log entries and headers. Stale entries are preserved.
897 */
898static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000899xfs_dir3_leafn_moveents(
900 xfs_da_args_t *args, /* operation arguments */
901 struct xfs_buf *bp_s, /* source */
902 struct xfs_dir3_icleaf_hdr *shdr,
903 struct xfs_dir2_leaf_entry *sents,
904 int start_s,/* source leaf index */
905 struct xfs_buf *bp_d, /* destination */
906 struct xfs_dir3_icleaf_hdr *dhdr,
907 struct xfs_dir2_leaf_entry *dents,
908 int start_d,/* destination leaf index */
909 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Dave Chinner24df33b2013-04-12 07:30:21 +1000911 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000913 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 /*
916 * Silently return if nothing to do.
917 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000918 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /*
922 * If the destination index is not the end of the current
923 * destination leaf entries, open up a hole in the destination
924 * to hold the new entries.
925 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000926 if (start_d < dhdr->count) {
927 memmove(&dents[start_d + count], &dents[start_d],
928 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000929 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000930 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 /*
933 * If the source has stale leaves, count the ones in the copy range
934 * so we can update the header correctly.
935 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000936 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 int i; /* temp leaf index */
938
939 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000940 if (sents[i].address ==
941 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 stale++;
943 }
944 } else
945 stale = 0;
946 /*
947 * Copy the leaf entries from source to destination.
948 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000949 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000951 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 /*
954 * If there are source entries after the ones we copied,
955 * delete the ones we copied by sliding the next ones down.
956 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 if (start_s + count < shdr->count) {
958 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000960 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /*
964 * Update the headers and log them.
965 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000966 shdr->count -= count;
967 shdr->stale -= stale;
968 dhdr->count += count;
969 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972/*
973 * Determine the sort order of two leaf blocks.
974 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
975 */
976int /* sort order */
977xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100978 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000979 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
980 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Dave Chinner24df33b2013-04-12 07:30:21 +1000982 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
983 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
984 struct xfs_dir2_leaf_entry *ents1;
985 struct xfs_dir2_leaf_entry *ents2;
986 struct xfs_dir3_icleaf_hdr hdr1;
987 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Chinner01ba43b2013-10-29 22:11:52 +1100989 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
990 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100991 ents1 = dp->d_ops->leaf_ents_p(leaf1);
992 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000993
994 if (hdr1.count > 0 && hdr2.count > 0 &&
995 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
996 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
997 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return 1;
999 return 0;
1000}
1001
1002/*
1003 * Rebalance leaf entries between two leaf blocks.
1004 * This is actually only called when the second block is new,
1005 * though the code deals with the general case.
1006 * A new entry will be inserted in one of the blocks, and that
1007 * entry is taken into account when balancing.
1008 */
1009static void
1010xfs_dir2_leafn_rebalance(
1011 xfs_da_state_t *state, /* btree cursor */
1012 xfs_da_state_blk_t *blk1, /* first btree block */
1013 xfs_da_state_blk_t *blk2) /* second btree block */
1014{
1015 xfs_da_args_t *args; /* operation arguments */
1016 int count; /* count (& direction) leaves */
1017 int isleft; /* new goes in left leaf */
1018 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
1019 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
1020 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001021#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int oldstale; /* old count of stale leaves */
1023#endif
1024 int oldsum; /* old total leaf count */
1025 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001026 struct xfs_dir2_leaf_entry *ents1;
1027 struct xfs_dir2_leaf_entry *ents2;
1028 struct xfs_dir3_icleaf_hdr hdr1;
1029 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +11001030 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 args = state->args;
1033 /*
1034 * If the block order is wrong, swap the arguments.
1035 */
Dave Chinner41419562013-10-29 22:11:50 +11001036 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 xfs_da_state_blk_t *tmp; /* temp for block swap */
1038
1039 tmp = blk1;
1040 blk1 = blk2;
1041 blk2 = tmp;
1042 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001043 leaf1 = blk1->bp->b_addr;
1044 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001045 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
1046 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +11001047 ents1 = dp->d_ops->leaf_ents_p(leaf1);
1048 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001049
1050 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001051#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001052 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053#endif
1054 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * If the old leaf count was odd then the new one will be even,
1058 * so we need to divide the new count evenly.
1059 */
1060 if (oldsum & 1) {
1061 xfs_dahash_t midhash; /* middle entry hash value */
1062
Dave Chinner24df33b2013-04-12 07:30:21 +10001063 if (mid >= hdr1.count)
1064 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001066 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 isleft = args->hashval <= midhash;
1068 }
1069 /*
1070 * If the old count is even then the new count is odd, so there's
1071 * no preferred side for the new entry.
1072 * Pick the left one.
1073 */
1074 else
1075 isleft = 1;
1076 /*
1077 * Calculate moved entry count. Positive means left-to-right,
1078 * negative means right-to-left. Then move the entries.
1079 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001080 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001082 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1083 hdr1.count - count, blk2->bp,
1084 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001086 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1087 blk1->bp, &hdr1, ents1,
1088 hdr1.count, count);
1089
1090 ASSERT(hdr1.count + hdr2.count == oldsum);
1091 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1092
1093 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001094 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1095 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001096 xfs_dir3_leaf_log_header(args, blk1->bp);
1097 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001098
Dave Chinner41419562013-10-29 22:11:50 +11001099 xfs_dir3_leaf_check(dp, blk1->bp);
1100 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /*
1103 * Mark whether we're inserting into the old or new leaf.
1104 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001105 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001107 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 state->inleaf = !swap;
1109 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001110 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /*
1112 * Adjust the expected index for insertion.
1113 */
1114 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001115 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001116
1117 /*
1118 * Finally sanity check just to make sure we are not returning a
1119 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Dave Chinner41419562013-10-29 22:11:50 +11001121 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 state->inleaf = 1;
1123 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001124 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001125 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001126 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128}
1129
Dave Chinner20252072012-11-12 22:54:13 +11001130static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001131xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001132 xfs_da_args_t *args,
1133 struct xfs_dir2_data_hdr *hdr,
1134 struct xfs_dir2_free *free,
1135 xfs_dir2_db_t fdb,
1136 int findex,
1137 struct xfs_buf *fbp,
1138 int longest)
1139{
Dave Chinner20252072012-11-12 22:54:13 +11001140 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001141 __be16 *bests;
1142 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001143 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001144
Dave Chinner01ba43b2013-10-29 22:11:52 +11001145 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001146 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001147 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001148 /*
1149 * Data block is not empty, just set the free entry to the new
1150 * value.
1151 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001152 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001153 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001154 return 0;
1155 }
1156
1157 /* One less used entry in the free table. */
1158 freehdr.nused--;
1159
1160 /*
1161 * If this was the last entry in the table, we can trim the table size
1162 * back. There might be other entries at the end referring to
1163 * non-existent data blocks, get those too.
1164 */
1165 if (findex == freehdr.nvalid - 1) {
1166 int i; /* free entry index */
1167
1168 for (i = findex - 1; i >= 0; i--) {
1169 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1170 break;
1171 }
1172 freehdr.nvalid = i + 1;
1173 logfree = 0;
1174 } else {
1175 /* Not the last entry, just punch it out. */
1176 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001177 logfree = 1;
1178 }
1179
Dave Chinner01ba43b2013-10-29 22:11:52 +11001180 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001181 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001182
1183 /*
1184 * If there are no useful entries left in the block, get rid of the
1185 * block if we can.
1186 */
1187 if (!freehdr.nused) {
1188 int error;
1189
1190 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1191 if (error == 0) {
1192 fbp = NULL;
1193 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001194 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001195 return error;
1196 /*
1197 * It's possible to get ENOSPC if there is no
1198 * space reservation. In this case some one
1199 * else will eventually get rid of this block.
1200 */
1201 }
1202
Dave Chinner20252072012-11-12 22:54:13 +11001203 /* Log the free entry that changed, unless we got rid of it. */
1204 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001205 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001206 return 0;
1207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/*
1210 * Remove an entry from a node directory.
1211 * This removes the leaf entry and the data entry,
1212 * and updates the free block if necessary.
1213 */
1214static int /* error */
1215xfs_dir2_leafn_remove(
1216 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001217 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 int index, /* leaf entry index */
1219 xfs_da_state_blk_t *dblk, /* data block */
1220 int *rval) /* resulting block needs join */
1221{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001222 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001224 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 xfs_dir2_data_entry_t *dep; /* data block entry */
1226 xfs_inode_t *dp; /* incore directory inode */
1227 xfs_dir2_leaf_t *leaf; /* leaf structure */
1228 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1229 int longest; /* longest data free entry */
1230 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int needlog; /* need to log data header */
1232 int needscan; /* need to rescan data frees */
1233 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001234 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001235 struct xfs_dir3_icleaf_hdr leafhdr;
1236 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001238 trace_xfs_dir2_leafn_remove(args, index);
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dp = args->dp;
1241 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001242 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001243 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001244 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Point to the entry we're removing.
1248 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001249 lep = &ents[index];
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Extract the data block and offset from the entry.
1253 */
Dave Chinner30028032014-06-06 15:08:18 +10001254 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001256 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Kill the leaf entry by marking it stale.
1261 * Log the leaf block changes.
1262 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001263 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001264 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001265 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001266
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001267 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001268 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Make the data entry free. Keep track of the longest freespace
1272 * in the data block in case it changes.
1273 */
1274 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001275 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001276 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001277 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001278 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001280 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001281 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /*
1283 * Rescan the data block freespaces for bestfree.
1284 * Log the data block header if needed.
1285 */
1286 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001287 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001289 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001290 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * If the longest data block freespace changes, need to update
1293 * the corresponding freeblock entry.
1294 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001295 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001297 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 xfs_dir2_db_t fdb; /* freeblock block number */
1299 int findex; /* index in freeblock entries */
1300 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /*
1303 * Convert the data block number to a free block,
1304 * read in the free block.
1305 */
Dave Chinner8f661932014-06-06 15:15:59 +10001306 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001307 error = xfs_dir2_free_read(tp, dp,
1308 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001309 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001310 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001312 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001313#ifdef DEBUG
1314 {
1315 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001316 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001317 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001318 (fdb - xfs_dir2_byte_to_db(args->geo,
1319 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001320 }
1321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 /*
1323 * Calculate which entry we need to fix.
1324 */
Dave Chinner8f661932014-06-06 15:15:59 +10001325 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001326 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * If the data block is now empty we can get rid of it
1329 * (usually).
1330 */
Dave Chinner8f661932014-06-06 15:15:59 +10001331 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001332 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Try to punch out the data block.
1335 */
1336 error = xfs_dir2_shrink_inode(args, db, dbp);
1337 if (error == 0) {
1338 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001339 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 /*
1342 * We can get ENOSPC if there's no space reservation.
1343 * In this case just drop the buffer and some one else
1344 * will eventually get rid of the empty block.
1345 */
Dave Chinner24513372014-06-25 14:58:08 +10001346 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return error;
1348 }
1349 /*
1350 * If we got rid of the data block, we can eliminate that entry
1351 * in the free block.
1352 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001353 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001354 fdb, findex, fbp, longest);
1355 if (error)
1356 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
Dave Chinner20252072012-11-12 22:54:13 +11001358
Dave Chinner41419562013-10-29 22:11:50 +11001359 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001361 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 * to justify trying to join it with a neighbor.
1363 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001364 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001365 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001366 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369
1370/*
1371 * Split the leaf entries in the old block into old and new blocks.
1372 */
1373int /* error */
1374xfs_dir2_leafn_split(
1375 xfs_da_state_t *state, /* btree cursor */
1376 xfs_da_state_blk_t *oldblk, /* original block */
1377 xfs_da_state_blk_t *newblk) /* newly created block */
1378{
1379 xfs_da_args_t *args; /* operation arguments */
1380 xfs_dablk_t blkno; /* new leaf block number */
1381 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001382 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
1385 * Allocate space for a new leaf node.
1386 */
1387 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001388 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1390 error = xfs_da_grow_inode(args, &blkno);
1391 if (error) {
1392 return error;
1393 }
1394 /*
1395 * Initialize the new leaf block.
1396 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001397 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001398 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1399 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 newblk->blkno = blkno;
1403 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1404 /*
1405 * Rebalance the entries across the two leaves, link the new
1406 * block into the leaves.
1407 */
1408 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001409 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (error) {
1411 return error;
1412 }
1413 /*
1414 * Insert the new entry in the correct block.
1415 */
1416 if (state->inleaf)
1417 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1418 else
1419 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1420 /*
1421 * Update last hashval in each block since we added the name.
1422 */
Darrick J. Wong8e8877e2017-06-16 11:00:13 -07001423 oldblk->hashval = xfs_dir2_leaf_lasthash(dp, oldblk->bp, NULL);
1424 newblk->hashval = xfs_dir2_leaf_lasthash(dp, newblk->bp, NULL);
Dave Chinner41419562013-10-29 22:11:50 +11001425 xfs_dir3_leaf_check(dp, oldblk->bp);
1426 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return error;
1428}
1429
1430/*
1431 * Check a leaf block and its neighbors to see if the block should be
1432 * collapsed into one or the other neighbor. Always keep the block
1433 * with the smaller block number.
1434 * If the current block is over 50% full, don't try to join it, return 0.
1435 * If the block is empty, fill in the state structure and return 2.
1436 * If it can be collapsed, fill in the state structure and return 1.
1437 * If nothing can be done, return 0.
1438 */
1439int /* error */
1440xfs_dir2_leafn_toosmall(
1441 xfs_da_state_t *state, /* btree cursor */
1442 int *action) /* resulting action to take */
1443{
1444 xfs_da_state_blk_t *blk; /* leaf block */
1445 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001446 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 int bytes; /* bytes in use */
1448 int count; /* leaf live entry count */
1449 int error; /* error return value */
1450 int forward; /* sibling block direction */
1451 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 xfs_dir2_leaf_t *leaf; /* leaf structure */
1453 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001454 struct xfs_dir3_icleaf_hdr leafhdr;
1455 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001456 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /*
1459 * Check for the degenerate case of the block being over 50% full.
1460 * If so, it's not worth even looking to see if we might be able
1461 * to coalesce with a sibling.
1462 */
1463 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001464 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001465 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001466 ents = dp->d_ops->leaf_ents_p(leaf);
1467 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001468
1469 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001470 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001471 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /*
1473 * Blk over 50%, don't try to join.
1474 */
1475 *action = 0;
1476 return 0;
1477 }
1478 /*
1479 * Check for the degenerate case of the block being empty.
1480 * If the block is empty, we'll simply delete it, no need to
1481 * coalesce it with a sibling block. We choose (arbitrarily)
1482 * to merge with the forward block unless it is NULL.
1483 */
1484 if (count == 0) {
1485 /*
1486 * Make altpath point to the block we want to keep and
1487 * path point to the block we want to drop (this one).
1488 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001489 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001491 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 &rval);
1493 if (error)
1494 return error;
1495 *action = rval ? 2 : 0;
1496 return 0;
1497 }
1498 /*
1499 * Examine each sibling block to see if we can coalesce with
1500 * at least 25% free space to spare. We need to figure out
1501 * whether to merge with the forward or the backward block.
1502 * We prefer coalescing with the lower numbered sibling so as
1503 * to shrink a directory over time.
1504 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001505 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001507 struct xfs_dir3_icleaf_hdr hdr2;
1508
1509 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (blkno == 0)
1511 continue;
1512 /*
1513 * Read the sibling leaf block.
1514 */
Dave Chinner41419562013-10-29 22:11:50 +11001515 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001516 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001517 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /*
1521 * Count bytes in the two blocks combined.
1522 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001523 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001524 bytes = state->args->geo->blksize -
1525 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001526
Dave Chinner1d9025e2012-06-22 18:50:14 +10001527 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001528 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001529 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001530 count += hdr2.count - hdr2.stale;
1531 bytes -= count * sizeof(ents[0]);
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * Fits with at least 25% to spare.
1535 */
1536 if (bytes >= 0)
1537 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001538 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 /*
1541 * Didn't like either block, give up.
1542 */
1543 if (i >= 2) {
1544 *action = 0;
1545 return 0;
1546 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /*
1549 * Make altpath point to the block we want to keep (the lower
1550 * numbered block) and path point to the block we want to drop.
1551 */
1552 memcpy(&state->altpath, &state->path, sizeof(state->path));
1553 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001554 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 &rval);
1556 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001557 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 &rval);
1559 if (error) {
1560 return error;
1561 }
1562 *action = rval ? 0 : 1;
1563 return 0;
1564}
1565
1566/*
1567 * Move all the leaf entries from drop_blk to save_blk.
1568 * This is done as part of a join operation.
1569 */
1570void
1571xfs_dir2_leafn_unbalance(
1572 xfs_da_state_t *state, /* cursor */
1573 xfs_da_state_blk_t *drop_blk, /* dead block */
1574 xfs_da_state_blk_t *save_blk) /* surviving block */
1575{
1576 xfs_da_args_t *args; /* operation arguments */
1577 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1578 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001579 struct xfs_dir3_icleaf_hdr savehdr;
1580 struct xfs_dir3_icleaf_hdr drophdr;
1581 struct xfs_dir2_leaf_entry *sents;
1582 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001583 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 args = state->args;
1586 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1587 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001588 drop_leaf = drop_blk->bp->b_addr;
1589 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001590
Dave Chinner01ba43b2013-10-29 22:11:52 +11001591 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1592 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1593 sents = dp->d_ops->leaf_ents_p(save_leaf);
1594 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /*
1597 * If there are any stale leaf entries, take this opportunity
1598 * to purge them.
1599 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001600 if (drophdr.stale)
1601 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1602 if (savehdr.stale)
1603 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
1606 * Move the entries from drop to the appropriate end of save.
1607 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001608 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001609 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001610 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1611 save_blk->bp, &savehdr, sents, 0,
1612 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001614 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1615 save_blk->bp, &savehdr, sents,
1616 savehdr.count, drophdr.count);
1617 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1618
1619 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001620 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1621 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001622 xfs_dir3_leaf_log_header(args, save_blk->bp);
1623 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001624
Dave Chinner41419562013-10-29 22:11:50 +11001625 xfs_dir3_leaf_check(dp, save_blk->bp);
1626 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
1629/*
1630 * Top-level node form directory addname routine.
1631 */
1632int /* error */
1633xfs_dir2_node_addname(
1634 xfs_da_args_t *args) /* operation arguments */
1635{
1636 xfs_da_state_blk_t *blk; /* leaf block for insert */
1637 int error; /* error return value */
1638 int rval; /* sub-return value */
1639 xfs_da_state_t *state; /* btree cursor */
1640
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001641 trace_xfs_dir2_node_addname(args);
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * Allocate and initialize the state (btree cursor).
1645 */
1646 state = xfs_da_state_alloc();
1647 state->args = args;
1648 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /*
1650 * Look up the name. We're not supposed to find it, but
1651 * this gives us the insertion point.
1652 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001653 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 if (error)
1655 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001656 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto done;
1658 }
1659 /*
1660 * Add the data entry to a data block.
1661 * Extravalid is set to a freeblock found by lookup.
1662 */
1663 rval = xfs_dir2_node_addname_int(args,
1664 state->extravalid ? &state->extrablk : NULL);
1665 if (rval) {
1666 goto done;
1667 }
1668 blk = &state->path.blk[state->path.active - 1];
1669 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1670 /*
1671 * Add the new leaf entry.
1672 */
1673 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1674 if (rval == 0) {
1675 /*
1676 * It worked, fix the hash values up the btree.
1677 */
Barry Naujok6a178102008-05-21 16:42:05 +10001678 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001679 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 } else {
1681 /*
1682 * It didn't work, we need to split the leaf block.
1683 */
1684 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001685 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto done;
1687 }
1688 /*
1689 * Split the leaf block and insert the new entry.
1690 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001691 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693done:
1694 xfs_da_state_free(state);
1695 return rval;
1696}
1697
1698/*
1699 * Add the data entry for a node-format directory name addition.
1700 * The leaf entry is added in xfs_dir2_leafn_add.
1701 * We may enter with a freespace block that the lookup found.
1702 */
1703static int /* error */
1704xfs_dir2_node_addname_int(
1705 xfs_da_args_t *args, /* operation arguments */
1706 xfs_da_state_blk_t *fblk) /* optional freespace block */
1707{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001708 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001710 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1712 xfs_inode_t *dp; /* incore directory inode */
1713 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1714 int error; /* error return value */
1715 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001716 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 int findex; /* freespace entry index */
1718 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1719 xfs_dir2_db_t ifbno; /* initial freespace block no */
1720 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1721 int length; /* length of the new entry */
1722 int logfree; /* need to log free entry */
1723 xfs_mount_t *mp; /* filesystem mount point */
1724 int needlog; /* need to log data header */
1725 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001726 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001728 __be16 *bests;
1729 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001730 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 dp = args->dp;
1733 mp = dp->i_mount;
1734 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001735 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * If we came in with a freespace block that means that lookup
1738 * found an entry with our hash value. This is the freespace
1739 * block for that data entry.
1740 */
1741 if (fblk) {
1742 fbp = fblk->bp;
1743 /*
1744 * Remember initial freespace block number.
1745 */
1746 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001747 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001749 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001750 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /*
1753 * This means the free entry showed that the data block had
1754 * space for our entry, so we remembered it.
1755 * Use that data block.
1756 */
1757 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001758 ASSERT(findex < freehdr.nvalid);
1759 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1760 ASSERT(be16_to_cpu(bests[findex]) >= length);
1761 dbno = freehdr.firstdb + findex;
1762 } else {
1763 /*
1764 * The data block looked at didn't have enough room.
1765 * We'll start at the beginning of the freespace entries.
1766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dbno = -1;
1768 findex = 0;
1769 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001770 } else {
1771 /*
1772 * Didn't come in with a freespace block, so no data block.
1773 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 ifbno = dbno = -1;
1775 fbp = NULL;
1776 findex = 0;
1777 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * If we don't have a data block yet, we're going to scan the
1781 * freespace blocks looking for one. Figure out what the
1782 * highest freespace block number is.
1783 */
1784 if (dbno == -1) {
1785 xfs_fileoff_t fo; /* freespace block number */
1786
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001787 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001789 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 fbno = ifbno;
1791 }
1792 /*
1793 * While we haven't identified a data block, search the freeblock
1794 * data for a good data block. If we find a null freeblock entry,
1795 * indicating a hole in the data blocks, remember that.
1796 */
1797 while (dbno == -1) {
1798 /*
1799 * If we don't have a freeblock in hand, get the next one.
1800 */
1801 if (fbp == NULL) {
1802 /*
1803 * Happens the first time through unless lookup gave
1804 * us a freespace block to start with.
1805 */
1806 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001807 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001808 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * If it's ifbno we already looked at it.
1811 */
1812 if (fbno == ifbno)
1813 fbno++;
1814 /*
1815 * If it's off the end we're done.
1816 */
1817 if (fbno >= lastfbno)
1818 break;
1819 /*
1820 * Read the block. There can be holes in the
1821 * freespace blocks, so this might not succeed.
1822 * This should be really rare, so there's no reason
1823 * to avoid it.
1824 */
Dave Chinner20252072012-11-12 22:54:13 +11001825 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001826 xfs_dir2_db_to_da(args->geo, fbno),
1827 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001828 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001830 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 findex = 0;
1834 }
1835 /*
1836 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001837 *
1838 * The bests initialisation should be where the bufer is read in
1839 * the above branch. But gcc is too stupid to realise that bests
1840 * and the freehdr are actually initialised if they are placed
1841 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001843 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001844 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001845 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1846 be16_to_cpu(bests[findex]) >= length)
1847 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 else {
1849 /*
1850 * Are we done with the freeblock?
1851 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001852 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /*
1854 * Drop the block.
1855 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001856 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 fbp = NULL;
1858 if (fblk && fblk->bp)
1859 fblk->bp = NULL;
1860 }
1861 }
1862 }
1863 /*
1864 * If we don't have a data block, we need to allocate one and make
1865 * the freespace entries refer to it.
1866 */
1867 if (unlikely(dbno == -1)) {
1868 /*
1869 * Not allowed to allocate, return failure.
1870 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001871 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001872 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * Allocate and initialize the new data block.
1876 */
1877 if (unlikely((error = xfs_dir2_grow_inode(args,
1878 XFS_DIR2_DATA_SPACE,
1879 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001880 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /*
1884 * If (somehow) we have a freespace block, get rid of it.
1885 */
1886 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001887 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (fblk && fblk->bp)
1889 fblk->bp = NULL;
1890
1891 /*
1892 * Get the freespace block corresponding to the data block
1893 * that was just allocated.
1894 */
Dave Chinner8f661932014-06-06 15:15:59 +10001895 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001896 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001897 xfs_dir2_db_to_da(args->geo, fbno),
1898 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001899 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * If there wasn't a freespace block, the read will
1904 * return a NULL fbp. Allocate and initialize a new one.
1905 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001906 if (!fbp) {
1907 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1908 &fbno);
1909 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Dave Chinner8f661932014-06-06 15:15:59 +10001912 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001913 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001914"%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 +11001915 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001916 (long long)dp->d_ops->db_to_fdb(
1917 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 (long long)dbno, (long long)fbno,
1919 (unsigned long long)ifbno, lastfbno);
1920 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001921 xfs_alert(mp,
1922 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 fblk,
1924 (unsigned long long)fblk->blkno,
1925 fblk->index,
1926 fblk->magic);
1927 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001928 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1931 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001932 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 /*
1936 * Get a buffer for the new block.
1937 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001938 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001941 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 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001946 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001948 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001949 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001950 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001951 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001953 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001954 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001955 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957
1958 /*
1959 * Set the freespace block index from the data block number.
1960 */
Dave Chinner8f661932014-06-06 15:15:59 +10001961 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * If it's after the end of the current entries in the
1964 * freespace block, extend that table.
1965 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001966 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001967 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001968 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /*
1970 * Tag new entry so nused will go up.
1971 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001972 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 /*
1975 * If this entry was for an empty data block
1976 * (this should always be true) then update the header.
1977 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001978 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1979 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001980 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001981 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 /*
1984 * Update the real value in the table.
1985 * We haven't allocated the data entry yet so this will
1986 * change again.
1987 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001988 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001989 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001990 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 logfree = 1;
1992 }
1993 /*
1994 * We had a data block so we don't have to make a new one.
1995 */
1996 else {
1997 /*
1998 * If just checking, we succeeded.
1999 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002000 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /*
2004 * Read the data block in.
2005 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002006 error = xfs_dir3_data_read(tp, dp,
2007 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11002008 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002009 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002011 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11002012 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 logfree = 0;
2014 }
Dave Chinner33363fe2013-04-03 16:11:22 +11002015 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 /*
2017 * Point to the existing unused space.
2018 */
2019 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11002020 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 needscan = needlog = 0;
2022 /*
2023 * Mark the first part of the unused space, inuse for us.
2024 */
Dave Chinnerbc851782014-06-06 15:20:54 +10002025 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002026 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 &needlog, &needscan);
2028 /*
2029 * Fill in the new entry and log it.
2030 */
2031 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002032 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 dep->namelen = args->namelen;
2034 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002035 dp->d_ops->data_put_ftype(dep, args->filetype);
2036 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002037 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10002038 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /*
2040 * Rescan the block for bestfree if needed.
2041 */
2042 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11002043 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
2045 * Log the data block header if needed.
2046 */
2047 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10002048 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /*
2050 * If the freespace entry is now wrong, update it.
2051 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05002052 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002053 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2054 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 logfree = 1;
2056 }
2057 /*
2058 * Log the freespace entry if needed.
2059 */
2060 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002061 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * Return the data block and offset in args, then drop the data block.
2064 */
2065 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002066 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068}
2069
2070/*
2071 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002072 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 * The only real output is the inode number of the entry.
2074 */
2075int /* error */
2076xfs_dir2_node_lookup(
2077 xfs_da_args_t *args) /* operation arguments */
2078{
2079 int error; /* error return value */
2080 int i; /* btree level */
2081 int rval; /* operation return value */
2082 xfs_da_state_t *state; /* btree cursor */
2083
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002084 trace_xfs_dir2_node_lookup(args);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Allocate and initialize the btree cursor.
2088 */
2089 state = xfs_da_state_alloc();
2090 state->args = args;
2091 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /*
2093 * Fill in the path to the entry in the cursor.
2094 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002095 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (error)
2097 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002098 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2099 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002100 xfs_dir2_data_entry_t *dep;
2101
Dave Chinner1d9025e2012-06-22 18:50:14 +10002102 dep = (xfs_dir2_data_entry_t *)
2103 ((char *)state->extrablk.bp->b_addr +
2104 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002105 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Release the btree blocks and leaf block.
2109 */
2110 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002111 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 state->path.blk[i].bp = NULL;
2113 }
2114 /*
2115 * Release the data block if we have it.
2116 */
2117 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002118 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 state->extrablk.bp = NULL;
2120 }
2121 xfs_da_state_free(state);
2122 return rval;
2123}
2124
2125/*
2126 * Remove an entry from a node-format directory.
2127 */
2128int /* error */
2129xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002130 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002132 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int error; /* error return value */
2134 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002135 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002137 trace_xfs_dir2_node_removename(args);
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
2140 * Allocate and initialize the btree cursor.
2141 */
2142 state = xfs_da_state_alloc();
2143 state->args = args;
2144 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002145
2146 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002147 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002148 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002149 goto out_free;
2150
2151 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002152 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002153 error = rval;
2154 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 blk = &state->path.blk[state->path.active - 1];
2158 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2159 ASSERT(state->extravalid);
2160 /*
2161 * Remove the leaf and data entries.
2162 * Extrablk refers to the data block.
2163 */
2164 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2165 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002166 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002167 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /*
2169 * Fix the hash values up the btree.
2170 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002171 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * If we need to join leaf blocks, do it.
2174 */
2175 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002176 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /*
2178 * If no errors so far, try conversion to leaf format.
2179 */
2180 if (!error)
2181 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002182out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 xfs_da_state_free(state);
2184 return error;
2185}
2186
2187/*
2188 * Replace an entry's inode number in a node-format directory.
2189 */
2190int /* error */
2191xfs_dir2_node_replace(
2192 xfs_da_args_t *args) /* operation arguments */
2193{
2194 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002195 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 xfs_dir2_data_entry_t *dep; /* data entry changed */
2197 int error; /* error return value */
2198 int i; /* btree level */
2199 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002200 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 xfs_dir2_leaf_t *leaf; /* leaf structure */
2202 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2203 int rval; /* internal return value */
2204 xfs_da_state_t *state; /* btree cursor */
2205
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002206 trace_xfs_dir2_node_replace(args);
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Allocate and initialize the btree cursor.
2210 */
2211 state = xfs_da_state_alloc();
2212 state->args = args;
2213 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002214
2215 /*
2216 * We have to save new inode number and ftype since
2217 * xfs_da3_node_lookup_int() is going to overwrite them
2218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002220 ftype = args->filetype;
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
2223 * Lookup the entry to change in the btree.
2224 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002225 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (error) {
2227 rval = error;
2228 }
2229 /*
2230 * It should be found, since the vnodeops layer has looked it up
2231 * and locked it. But paranoia is good.
2232 */
Dave Chinner24513372014-06-25 14:58:08 +10002233 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002234 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /*
2236 * Find the leaf entry.
2237 */
2238 blk = &state->path.blk[state->path.active - 1];
2239 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002240 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002241 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002242 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 ASSERT(state->extravalid);
2244 /*
2245 * Point to the data entry.
2246 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002247 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002248 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2249 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002251 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002252 xfs_dir2_dataptr_to_off(args->geo,
2253 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002254 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /*
2256 * Fill in the new inode number and log the entry.
2257 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002258 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002259 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002260 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 rval = 0;
2262 }
2263 /*
2264 * Didn't find it, and we're holding a data block. Drop it.
2265 */
2266 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002267 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 state->extrablk.bp = NULL;
2269 }
2270 /*
2271 * Release all the buffers in the cursor.
2272 */
2273 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002274 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 state->path.blk[i].bp = NULL;
2276 }
2277 xfs_da_state_free(state);
2278 return rval;
2279}
2280
2281/*
2282 * Trim off a trailing empty freespace block.
2283 * Return (in rvalp) 1 if we did it, 0 if not.
2284 */
2285int /* error */
2286xfs_dir2_node_trim_free(
2287 xfs_da_args_t *args, /* operation arguments */
2288 xfs_fileoff_t fo, /* free block number */
2289 int *rvalp) /* out: did something */
2290{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002291 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 xfs_inode_t *dp; /* incore directory inode */
2293 int error; /* error return code */
2294 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002296 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 tp = args->trans;
Christoph Hellwig355cced2016-03-15 11:44:18 +11002300
2301 *rvalp = 0;
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /*
2304 * Read the freespace block.
2305 */
Dave Chinner20252072012-11-12 22:54:13 +11002306 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002307 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * There can be holes in freespace. If fo is a hole, there's
2311 * nothing to do.
2312 */
Dave Chinner20252072012-11-12 22:54:13 +11002313 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002315 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002316 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /*
2319 * If there are used entries, there's nothing to do.
2320 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002321 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002322 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return 0;
2324 }
2325 /*
2326 * Blow the block away.
2327 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002328 error = xfs_dir2_shrink_inode(args,
2329 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2330 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /*
2332 * Can't fail with ENOSPC since that only happens with no
2333 * space reservation, when breaking up an extent into two
2334 * pieces. This is the last block of an extent.
2335 */
Dave Chinner24513372014-06-25 14:58:08 +10002336 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002337 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return error;
2339 }
2340 /*
2341 * Return that we succeeded.
2342 */
2343 *rvalp = 1;
2344 return 0;
2345}