blob: 63ee03db796ca9a55b9f9f10a427a6fd539c30d4 [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
Dave Chinner41419562013-10-29 22:11:50 +110056#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100057do { \
Dave Chinner41419562013-10-29 22:11:50 +110058 if (!xfs_dir3_leafn_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100059 ASSERT(0); \
60} while (0);
61
62static bool
63xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110064 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100065 struct xfs_buf *bp)
66{
67 struct xfs_dir2_leaf *leaf = bp->b_addr;
68 struct xfs_dir3_icleaf_hdr leafhdr;
69
Dave Chinner01ba43b2013-10-29 22:11:52 +110070 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100071
72 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
73 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
74 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
75 return false;
76 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
77 return false;
78
Dave Chinner41419562013-10-29 22:11:50 +110079 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100080}
81#else
Dave Chinner41419562013-10-29 22:11:50 +110082#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100083#endif
84
Dave Chinnercbc8adf2013-04-03 16:11:21 +110085static bool
86xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110087 struct xfs_buf *bp)
88{
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110091
Dave Chinnercbc8adf2013-04-03 16:11:21 +110092 if (xfs_sb_version_hascrc(&mp->m_sb)) {
93 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94
95 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
96 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +100097 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinnercbc8adf2013-04-03 16:11:21 +110098 return false;
99 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
100 return false;
Brian Fostera45086e2015-10-12 15:59:25 +1100101 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
102 return false;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100103 } else {
104 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
105 return false;
106 }
107
108 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
109
110 return true;
111}
112
113static void
114xfs_dir3_free_read_verify(
115 struct xfs_buf *bp)
116{
117 struct xfs_mount *mp = bp->b_target->bt_mount;
118
Eric Sandeence5028c2014-02-27 15:23:10 +1100119 if (xfs_sb_version_hascrc(&mp->m_sb) &&
120 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000121 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100122 else if (!xfs_dir3_free_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000123 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100124
125 if (bp->b_error)
126 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100127}
Dave Chinner20252072012-11-12 22:54:13 +1100128
Dave Chinner612cfbf2012-11-14 17:52:32 +1100129static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100131 struct xfs_buf *bp)
132{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100133 struct xfs_mount *mp = bp->b_target->bt_mount;
134 struct xfs_buf_log_item *bip = bp->b_fspriv;
135 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
136
137 if (!xfs_dir3_free_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000138 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100139 xfs_verifier_error(bp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100140 return;
141 }
142
143 if (!xfs_sb_version_hascrc(&mp->m_sb))
144 return;
145
146 if (bip)
147 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
148
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100149 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100150}
151
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100152const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100153 .name = "xfs_dir3_free",
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100154 .verify_read = xfs_dir3_free_read_verify,
155 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100156};
Dave Chinner20252072012-11-12 22:54:13 +1100157
Dave Chinner612cfbf2012-11-14 17:52:32 +1100158
Dave Chinner20252072012-11-12 22:54:13 +1100159static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100160__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100161 struct xfs_trans *tp,
162 struct xfs_inode *dp,
163 xfs_dablk_t fbno,
164 xfs_daddr_t mappedbno,
165 struct xfs_buf **bpp)
166{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100167 int err;
168
169 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100170 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100171
172 /* try read returns without an error or *bpp if it lands in a hole */
173 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100174 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100175 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100176}
177
178int
179xfs_dir2_free_read(
180 struct xfs_trans *tp,
181 struct xfs_inode *dp,
182 xfs_dablk_t fbno,
183 struct xfs_buf **bpp)
184{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100185 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100186}
187
188static int
189xfs_dir2_free_try_read(
190 struct xfs_trans *tp,
191 struct xfs_inode *dp,
192 xfs_dablk_t fbno,
193 struct xfs_buf **bpp)
194{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100195 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
196}
197
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100198static int
199xfs_dir3_free_get_buf(
Dave Chinner2998ab12014-06-06 15:07:53 +1000200 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100201 xfs_dir2_db_t fbno,
202 struct xfs_buf **bpp)
203{
Dave Chinner2998ab12014-06-06 15:07:53 +1000204 struct xfs_trans *tp = args->trans;
205 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100206 struct xfs_mount *mp = dp->i_mount;
207 struct xfs_buf *bp;
208 int error;
209 struct xfs_dir3_icfree_hdr hdr;
210
Dave Chinner2998ab12014-06-06 15:07:53 +1000211 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100212 -1, &bp, XFS_DATA_FORK);
213 if (error)
214 return error;
215
Dave Chinner61fe1352013-04-03 16:11:30 +1100216 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100217 bp->b_ops = &xfs_dir3_free_buf_ops;
218
219 /*
220 * Initialize the new block to be empty, and remember
221 * its first slot as our empty slot.
222 */
Dave Chinnere400d272013-05-28 18:37:17 +1000223 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
224 memset(&hdr, 0, sizeof(hdr));
225
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100226 if (xfs_sb_version_hascrc(&mp->m_sb)) {
227 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
228
229 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000230
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100231 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
232 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000233 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000234 } else
235 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100236 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100237 *bpp = bp;
238 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*
242 * Log entries from a freespace block.
243 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000244STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000246 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000247 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int first, /* first entry to log */
249 int last) /* last entry to log */
250{
251 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100252 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dave Chinner1d9025e2012-06-22 18:50:14 +1000254 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000255 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100256 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
257 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000258 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100259 (uint)((char *)&bests[first] - (char *)free),
260 (uint)((char *)&bests[last] - (char *)free +
261 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/*
265 * Log header from a freespace block.
266 */
267static void
268xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000269 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000270 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Dave Chinner836a94a2013-08-12 20:49:44 +1000272#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 xfs_dir2_free_t *free; /* freespace structure */
274
Dave Chinner1d9025e2012-06-22 18:50:14 +1000275 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100276 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
277 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000278#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000279 xfs_trans_log_buf(args->trans, bp, 0,
280 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283/*
284 * Convert a leaf-format directory to a node-format directory.
285 * We need to change the magic number of the leaf block, and copy
286 * the freespace table out of the leaf block into its own block.
287 */
288int /* error */
289xfs_dir2_leaf_to_node(
290 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000291 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 xfs_inode_t *dp; /* incore directory inode */
294 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000295 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xfs_dir2_db_t fdb; /* freespace block number */
297 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100298 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int i; /* leaf freespace index */
300 xfs_dir2_leaf_t *leaf; /* leaf structure */
301 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 int n; /* count of live freespc ents */
303 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100304 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100306 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000308 trace_xfs_dir2_leaf_to_node(args);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 tp = args->trans;
312 /*
313 * Add a freespace block to the directory.
314 */
315 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
316 return error;
317 }
Dave Chinner30028032014-06-06 15:08:18 +1000318 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /*
320 * Get the buffer for the new freespace block.
321 */
Dave Chinner2998ab12014-06-06 15:07:53 +1000322 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100323 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100325
Dave Chinner1d9025e2012-06-22 18:50:14 +1000326 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100327 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000328 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000329 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100330 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000331 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /*
334 * Copy freespace entries from the leaf block to the new block.
335 * Count active entries.
336 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100337 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500338 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100339 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100340 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100342 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100346 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100348 freehdr.nused = n;
349 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
350
Dave Chinner01ba43b2013-10-29 22:11:52 +1100351 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000352 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
353 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100354
Dave Chinner24df33b2013-04-12 07:30:21 +1000355 /*
356 * Converting the leaf to a leafnode is just a matter of changing the
357 * magic number and the ops. Do the change directly to the buffer as
358 * it's less work (and less code) than decoding the header to host
359 * format and back again.
360 */
361 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
362 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
363 else
364 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
365 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100366 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000367 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100368 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370}
371
372/*
373 * Add a leaf entry to a leaf block in a node-form directory.
374 * The other work necessary is done from the caller.
375 */
376static int /* error */
377xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000378 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_da_args_t *args, /* operation arguments */
380 int index) /* insertion pt for new entry */
381{
382 int compact; /* compacting stale leaves */
383 xfs_inode_t *dp; /* incore directory inode */
384 int highstale; /* next stale entry */
385 xfs_dir2_leaf_t *leaf; /* leaf structure */
386 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
387 int lfloghigh; /* high leaf entry logging */
388 int lfloglow; /* low leaf entry logging */
389 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000390 struct xfs_dir3_icleaf_hdr leafhdr;
391 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000393 trace_xfs_dir2_leafn_add(args, index);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000396 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100397 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100398 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
401 * Quick check just to make sure we are not going to index
402 * into other peoples memory
403 */
404 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000405 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
408 * If there are already the maximum number of leaf entries in
409 * the block, if there are no stale entries it won't fit.
410 * Caller will do a split. If there are stale entries we'll do
411 * a compact.
412 */
413
Dave Chinner8f661932014-06-06 15:15:59 +1000414 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000415 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000416 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000417 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } else
419 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000420 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
421 ASSERT(index == leafhdr.count ||
422 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Barry Naujok6a178102008-05-21 16:42:05 +1000424 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426
427 /*
428 * Compact out all but one stale leaf entry. Leaves behind
429 * the entry closest to index.
430 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000431 if (compact)
432 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
433 &highstale, &lfloglow, &lfloghigh);
434 else if (leafhdr.stale) {
435 /*
436 * Set impossible logging indices for this case.
437 */
438 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 lfloghigh = -1;
440 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 /*
443 * Insert the new entry, log everything.
444 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000445 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200446 highstale, &lfloglow, &lfloghigh);
447
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100448 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000449 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100450 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000451
Dave Chinner01ba43b2013-10-29 22:11:52 +1100452 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000453 xfs_dir3_leaf_log_header(args, bp);
454 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100455 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
459#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100460static void
461xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100462 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100463 struct xfs_buf *bp,
464 xfs_dir2_db_t db)
465{
466 struct xfs_dir3_icfree_hdr hdr;
467
Dave Chinner01ba43b2013-10-29 22:11:52 +1100468 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100469
Dave Chinner8f661932014-06-06 15:15:59 +1000470 ASSERT((hdr.firstdb %
471 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100472 ASSERT(hdr.firstdb <= db);
473 ASSERT(db < hdr.firstdb + hdr.nvalid);
474}
475#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100476#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477#endif /* DEBUG */
478
479/*
480 * Return the last hash value in the leaf.
481 * Stale entries are ok.
482 */
483xfs_dahash_t /* hash value */
484xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100485 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000486 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int *count) /* count of entries in leaf */
488{
Dave Chinner24df33b2013-04-12 07:30:21 +1000489 struct xfs_dir2_leaf *leaf = bp->b_addr;
490 struct xfs_dir2_leaf_entry *ents;
491 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dave Chinner01ba43b2013-10-29 22:11:52 +1100493 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000494
495 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
496 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000499 *count = leafhdr.count;
500 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000502
Dave Chinner41419562013-10-29 22:11:50 +1100503 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000504 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000508 * Look up a leaf entry for space to add a name in a node-format leaf block.
509 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000511STATIC int
512xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000513 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 xfs_da_args_t *args, /* operation arguments */
515 int *indexp, /* out: leaf entry index */
516 xfs_da_state_t *state) /* state to fill in */
517{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000518 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000519 xfs_dir2_db_t curdb = -1; /* current data block number */
520 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 xfs_inode_t *dp; /* incore directory inode */
522 int error; /* error return value */
523 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000524 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int index; /* leaf entry index */
526 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000527 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
529 xfs_mount_t *mp; /* filesystem mount point */
530 xfs_dir2_db_t newdb; /* new data block number */
531 xfs_dir2_db_t newfdb; /* new free block number */
532 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000533 struct xfs_dir2_leaf_entry *ents;
534 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 dp = args->dp;
537 tp = args->trans;
538 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000539 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100540 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100541 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000542
Dave Chinner41419562013-10-29 22:11:50 +1100543 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000544 ASSERT(leafhdr.count > 0);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * Look up the hash value in the leaf entries.
548 */
549 index = xfs_dir2_leaf_search_hash(args, bp);
550 /*
551 * Do we have a buffer coming in?
552 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000553 if (state->extravalid) {
554 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000556 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000557 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100558 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
559 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100561 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /*
563 * Loop over leaf entries with the right hash value.
564 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000565 for (lep = &ents[index];
566 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
567 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Skip stale leaf entries.
570 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100571 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 continue;
573 /*
574 * Pull the data block number from the entry.
575 */
Dave Chinner30028032014-06-06 15:08:18 +1000576 newdb = xfs_dir2_dataptr_to_db(args->geo,
577 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
579 * For addname, we're looking for a place to put the new entry.
580 * We want to use a data block with an entry of equal
581 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000582 *
583 * If this block isn't the data block we already have
584 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000586 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100587 __be16 *bests;
588
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000589 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000591 * Convert the data block to the free block
592 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Dave Chinner8f661932014-06-06 15:15:59 +1000594 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000596 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000598 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000600 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
602 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000603 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100604
605 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +1000606 xfs_dir2_db_to_da(args->geo,
607 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100608 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000609 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100612
Dave Chinner01ba43b2013-10-29 22:11:52 +1100613 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000616 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Dave Chinner8f661932014-06-06 15:15:59 +1000618 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000620 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500622 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100623 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000624 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
625 XFS_ERRLEVEL_LOW, mp);
626 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000627 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000628 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000630 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100631 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 /* Didn't find any space */
636 fi = -1;
637out:
Barry Naujok6a178102008-05-21 16:42:05 +1000638 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 if (curbp) {
640 /* Giving back a free block. */
641 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643 state->extrablk.index = fi;
644 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100645
646 /*
647 * Important: this magic number is not in the buffer - it's for
648 * buffer type information and therefore only the free/data type
649 * matters here, not whether CRCs are enabled or not.
650 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000651 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
652 } else {
653 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000656 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000659 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000663 * Look up a leaf entry in a node-format leaf block.
664 * The extrablk in state a data block.
665 */
666STATIC int
667xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000668 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000669 xfs_da_args_t *args, /* operation arguments */
670 int *indexp, /* out: leaf entry index */
671 xfs_da_state_t *state) /* state to fill in */
672{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 xfs_dir2_db_t curdb = -1; /* current data block number */
675 xfs_dir2_data_entry_t *dep; /* data block entry */
676 xfs_inode_t *dp; /* incore directory inode */
677 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678 int index; /* leaf entry index */
679 xfs_dir2_leaf_t *leaf; /* leaf structure */
680 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
681 xfs_mount_t *mp; /* filesystem mount point */
682 xfs_dir2_db_t newdb; /* new data block number */
683 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000684 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000685 struct xfs_dir2_leaf_entry *ents;
686 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687
688 dp = args->dp;
689 tp = args->trans;
690 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000691 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100692 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100693 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000694
Dave Chinner41419562013-10-29 22:11:50 +1100695 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000696 ASSERT(leafhdr.count > 0);
697
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000698 /*
699 * Look up the hash value in the leaf entries.
700 */
701 index = xfs_dir2_leaf_search_hash(args, bp);
702 /*
703 * Do we have a buffer coming in?
704 */
705 if (state->extravalid) {
706 curbp = state->extrablk.bp;
707 curdb = state->extrablk.blkno;
708 }
709 /*
710 * Loop over leaf entries with the right hash value.
711 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000712 for (lep = &ents[index];
713 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
714 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000715 /*
716 * Skip stale leaf entries.
717 */
718 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
719 continue;
720 /*
721 * Pull the data block number from the entry.
722 */
Dave Chinner30028032014-06-06 15:08:18 +1000723 newdb = xfs_dir2_dataptr_to_db(args->geo,
724 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000725 /*
726 * Not adding a new entry, so we really want to find
727 * the name given to us.
728 *
729 * If it's a different data block, go get it.
730 */
731 if (newdb != curdb) {
732 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000733 * If we had a block before that we aren't saving
734 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000735 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000736 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
737 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000738 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000739 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000740 * If needing the block that is saved with a CI match,
741 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000742 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000743 if (args->cmpresult != XFS_CMP_DIFFERENT &&
744 newdb == state->extrablk.blkno) {
745 ASSERT(state->extravalid);
746 curbp = state->extrablk.bp;
747 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100748 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +1000749 xfs_dir2_db_to_da(args->geo,
750 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100751 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000752 if (error)
753 return error;
754 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100755 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000756 curdb = newdb;
757 }
758 /*
759 * Point to the data entry.
760 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000761 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000762 xfs_dir2_dataptr_to_off(args->geo,
763 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000764 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000765 * Compare the entry and if it's an exact match, return
766 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000767 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000768 */
Barry Naujok5163f952008-05-21 16:41:01 +1000769 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
770 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000771 /* If there is a CI match block, drop it */
772 if (args->cmpresult != XFS_CMP_DIFFERENT &&
773 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000774 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000775 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000776 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100777 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000778 *indexp = index;
779 state->extravalid = 1;
780 state->extrablk.bp = curbp;
781 state->extrablk.blkno = curdb;
782 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000783 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000784 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100785 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100786 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000787 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000788 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000789 }
790 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000791 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000792 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000793 if (args->cmpresult == XFS_CMP_DIFFERENT) {
794 /* Giving back last used data block. */
795 state->extravalid = 1;
796 state->extrablk.bp = curbp;
797 state->extrablk.index = -1;
798 state->extrablk.blkno = curdb;
799 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100800 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100801 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000802 } else {
803 /* If the curbp is not the CI match block, drop it */
804 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000805 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000806 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000807 } else {
808 state->extravalid = 0;
809 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000810 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000811 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000812}
813
814/*
815 * Look up a leaf entry in a node-format leaf block.
816 * If this is an addname then the extrablk in state is a freespace block,
817 * otherwise it's a data block.
818 */
819int
820xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000821 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000822 xfs_da_args_t *args, /* operation arguments */
823 int *indexp, /* out: leaf entry index */
824 xfs_da_state_t *state) /* state to fill in */
825{
Barry Naujok6a178102008-05-21 16:42:05 +1000826 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000827 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
828 state);
829 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
830}
831
832/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 * Move count leaf entries from source to destination leaf.
834 * Log entries and headers. Stale entries are preserved.
835 */
836static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000837xfs_dir3_leafn_moveents(
838 xfs_da_args_t *args, /* operation arguments */
839 struct xfs_buf *bp_s, /* source */
840 struct xfs_dir3_icleaf_hdr *shdr,
841 struct xfs_dir2_leaf_entry *sents,
842 int start_s,/* source leaf index */
843 struct xfs_buf *bp_d, /* destination */
844 struct xfs_dir3_icleaf_hdr *dhdr,
845 struct xfs_dir2_leaf_entry *dents,
846 int start_d,/* destination leaf index */
847 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Dave Chinner24df33b2013-04-12 07:30:21 +1000849 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000851 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
854 * Silently return if nothing to do.
855 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000856 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 /*
860 * If the destination index is not the end of the current
861 * destination leaf entries, open up a hole in the destination
862 * to hold the new entries.
863 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000864 if (start_d < dhdr->count) {
865 memmove(&dents[start_d + count], &dents[start_d],
866 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000867 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000868 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
870 /*
871 * If the source has stale leaves, count the ones in the copy range
872 * so we can update the header correctly.
873 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000874 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int i; /* temp leaf index */
876
877 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000878 if (sents[i].address ==
879 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 stale++;
881 }
882 } else
883 stale = 0;
884 /*
885 * Copy the leaf entries from source to destination.
886 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000887 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000889 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * If there are source entries after the ones we copied,
893 * delete the ones we copied by sliding the next ones down.
894 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000895 if (start_s + count < shdr->count) {
896 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000898 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Update the headers and log them.
903 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000904 shdr->count -= count;
905 shdr->stale -= stale;
906 dhdr->count += count;
907 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/*
911 * Determine the sort order of two leaf blocks.
912 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
913 */
914int /* sort order */
915xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100916 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
918 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
921 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
922 struct xfs_dir2_leaf_entry *ents1;
923 struct xfs_dir2_leaf_entry *ents2;
924 struct xfs_dir3_icleaf_hdr hdr1;
925 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Chinner01ba43b2013-10-29 22:11:52 +1100927 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
928 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100929 ents1 = dp->d_ops->leaf_ents_p(leaf1);
930 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000931
932 if (hdr1.count > 0 && hdr2.count > 0 &&
933 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
934 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
935 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 1;
937 return 0;
938}
939
940/*
941 * Rebalance leaf entries between two leaf blocks.
942 * This is actually only called when the second block is new,
943 * though the code deals with the general case.
944 * A new entry will be inserted in one of the blocks, and that
945 * entry is taken into account when balancing.
946 */
947static void
948xfs_dir2_leafn_rebalance(
949 xfs_da_state_t *state, /* btree cursor */
950 xfs_da_state_blk_t *blk1, /* first btree block */
951 xfs_da_state_blk_t *blk2) /* second btree block */
952{
953 xfs_da_args_t *args; /* operation arguments */
954 int count; /* count (& direction) leaves */
955 int isleft; /* new goes in left leaf */
956 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
957 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
958 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000959#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int oldstale; /* old count of stale leaves */
961#endif
962 int oldsum; /* old total leaf count */
963 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000964 struct xfs_dir2_leaf_entry *ents1;
965 struct xfs_dir2_leaf_entry *ents2;
966 struct xfs_dir3_icleaf_hdr hdr1;
967 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100968 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 args = state->args;
971 /*
972 * If the block order is wrong, swap the arguments.
973 */
Dave Chinner41419562013-10-29 22:11:50 +1100974 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_da_state_blk_t *tmp; /* temp for block swap */
976
977 tmp = blk1;
978 blk1 = blk2;
979 blk2 = tmp;
980 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000981 leaf1 = blk1->bp->b_addr;
982 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100983 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
984 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100985 ents1 = dp->d_ops->leaf_ents_p(leaf1);
986 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000987
988 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000989#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
992 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * If the old leaf count was odd then the new one will be even,
996 * so we need to divide the new count evenly.
997 */
998 if (oldsum & 1) {
999 xfs_dahash_t midhash; /* middle entry hash value */
1000
Dave Chinner24df33b2013-04-12 07:30:21 +10001001 if (mid >= hdr1.count)
1002 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001004 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 isleft = args->hashval <= midhash;
1006 }
1007 /*
1008 * If the old count is even then the new count is odd, so there's
1009 * no preferred side for the new entry.
1010 * Pick the left one.
1011 */
1012 else
1013 isleft = 1;
1014 /*
1015 * Calculate moved entry count. Positive means left-to-right,
1016 * negative means right-to-left. Then move the entries.
1017 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001018 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001020 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1021 hdr1.count - count, blk2->bp,
1022 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001024 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1025 blk1->bp, &hdr1, ents1,
1026 hdr1.count, count);
1027
1028 ASSERT(hdr1.count + hdr2.count == oldsum);
1029 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1030
1031 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001032 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1033 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001034 xfs_dir3_leaf_log_header(args, blk1->bp);
1035 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001036
Dave Chinner41419562013-10-29 22:11:50 +11001037 xfs_dir3_leaf_check(dp, blk1->bp);
1038 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * Mark whether we're inserting into the old or new leaf.
1042 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001043 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001045 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 state->inleaf = !swap;
1047 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001048 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * Adjust the expected index for insertion.
1051 */
1052 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001053 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001054
1055 /*
1056 * Finally sanity check just to make sure we are not returning a
1057 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Dave Chinner41419562013-10-29 22:11:50 +11001059 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 state->inleaf = 1;
1061 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001062 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001063 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001064 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066}
1067
Dave Chinner20252072012-11-12 22:54:13 +11001068static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001069xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001070 xfs_da_args_t *args,
1071 struct xfs_dir2_data_hdr *hdr,
1072 struct xfs_dir2_free *free,
1073 xfs_dir2_db_t fdb,
1074 int findex,
1075 struct xfs_buf *fbp,
1076 int longest)
1077{
Dave Chinner20252072012-11-12 22:54:13 +11001078 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001079 __be16 *bests;
1080 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001081 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001082
Dave Chinner01ba43b2013-10-29 22:11:52 +11001083 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001084 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001085 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001086 /*
1087 * Data block is not empty, just set the free entry to the new
1088 * value.
1089 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001090 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001091 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001092 return 0;
1093 }
1094
1095 /* One less used entry in the free table. */
1096 freehdr.nused--;
1097
1098 /*
1099 * If this was the last entry in the table, we can trim the table size
1100 * back. There might be other entries at the end referring to
1101 * non-existent data blocks, get those too.
1102 */
1103 if (findex == freehdr.nvalid - 1) {
1104 int i; /* free entry index */
1105
1106 for (i = findex - 1; i >= 0; i--) {
1107 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1108 break;
1109 }
1110 freehdr.nvalid = i + 1;
1111 logfree = 0;
1112 } else {
1113 /* Not the last entry, just punch it out. */
1114 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001115 logfree = 1;
1116 }
1117
Dave Chinner01ba43b2013-10-29 22:11:52 +11001118 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001119 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001120
1121 /*
1122 * If there are no useful entries left in the block, get rid of the
1123 * block if we can.
1124 */
1125 if (!freehdr.nused) {
1126 int error;
1127
1128 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1129 if (error == 0) {
1130 fbp = NULL;
1131 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001132 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001133 return error;
1134 /*
1135 * It's possible to get ENOSPC if there is no
1136 * space reservation. In this case some one
1137 * else will eventually get rid of this block.
1138 */
1139 }
1140
Dave Chinner20252072012-11-12 22:54:13 +11001141 /* Log the free entry that changed, unless we got rid of it. */
1142 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001143 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001144 return 0;
1145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/*
1148 * Remove an entry from a node directory.
1149 * This removes the leaf entry and the data entry,
1150 * and updates the free block if necessary.
1151 */
1152static int /* error */
1153xfs_dir2_leafn_remove(
1154 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001155 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 int index, /* leaf entry index */
1157 xfs_da_state_blk_t *dblk, /* data block */
1158 int *rval) /* resulting block needs join */
1159{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001160 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001162 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 xfs_dir2_data_entry_t *dep; /* data block entry */
1164 xfs_inode_t *dp; /* incore directory inode */
1165 xfs_dir2_leaf_t *leaf; /* leaf structure */
1166 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1167 int longest; /* longest data free entry */
1168 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int needlog; /* need to log data header */
1170 int needscan; /* need to rescan data frees */
1171 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001172 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001173 struct xfs_dir3_icleaf_hdr leafhdr;
1174 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001176 trace_xfs_dir2_leafn_remove(args, index);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 dp = args->dp;
1179 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001180 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001181 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001182 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /*
1185 * Point to the entry we're removing.
1186 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001187 lep = &ents[index];
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /*
1190 * Extract the data block and offset from the entry.
1191 */
Dave Chinner30028032014-06-06 15:08:18 +10001192 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001194 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /*
1198 * Kill the leaf entry by marking it stale.
1199 * Log the leaf block changes.
1200 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001201 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001202 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001203 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001204
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001205 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001206 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Make the data entry free. Keep track of the longest freespace
1210 * in the data block in case it changes.
1211 */
1212 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001213 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001214 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001215 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001216 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001218 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001219 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 /*
1221 * Rescan the data block freespaces for bestfree.
1222 * Log the data block header if needed.
1223 */
1224 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001225 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001227 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001228 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /*
1230 * If the longest data block freespace changes, need to update
1231 * the corresponding freeblock entry.
1232 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001233 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001235 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 xfs_dir2_db_t fdb; /* freeblock block number */
1237 int findex; /* index in freeblock entries */
1238 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * Convert the data block number to a free block,
1242 * read in the free block.
1243 */
Dave Chinner8f661932014-06-06 15:15:59 +10001244 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab12014-06-06 15:07:53 +10001245 error = xfs_dir2_free_read(tp, dp,
1246 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001247 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001248 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001250 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001251#ifdef DEBUG
1252 {
1253 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001254 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001255 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001256 (fdb - xfs_dir2_byte_to_db(args->geo,
1257 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001258 }
1259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 /*
1261 * Calculate which entry we need to fix.
1262 */
Dave Chinner8f661932014-06-06 15:15:59 +10001263 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001264 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /*
1266 * If the data block is now empty we can get rid of it
1267 * (usually).
1268 */
Dave Chinner8f661932014-06-06 15:15:59 +10001269 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001270 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Try to punch out the data block.
1273 */
1274 error = xfs_dir2_shrink_inode(args, db, dbp);
1275 if (error == 0) {
1276 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001277 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 /*
1280 * We can get ENOSPC if there's no space reservation.
1281 * In this case just drop the buffer and some one else
1282 * will eventually get rid of the empty block.
1283 */
Dave Chinner24513372014-06-25 14:58:08 +10001284 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return error;
1286 }
1287 /*
1288 * If we got rid of the data block, we can eliminate that entry
1289 * in the free block.
1290 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001291 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001292 fdb, findex, fbp, longest);
1293 if (error)
1294 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Dave Chinner20252072012-11-12 22:54:13 +11001296
Dave Chinner41419562013-10-29 22:11:50 +11001297 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001299 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * to justify trying to join it with a neighbor.
1301 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001302 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001303 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001304 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return 0;
1306}
1307
1308/*
1309 * Split the leaf entries in the old block into old and new blocks.
1310 */
1311int /* error */
1312xfs_dir2_leafn_split(
1313 xfs_da_state_t *state, /* btree cursor */
1314 xfs_da_state_blk_t *oldblk, /* original block */
1315 xfs_da_state_blk_t *newblk) /* newly created block */
1316{
1317 xfs_da_args_t *args; /* operation arguments */
1318 xfs_dablk_t blkno; /* new leaf block number */
1319 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001320 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 /*
1323 * Allocate space for a new leaf node.
1324 */
1325 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001326 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 error = xfs_da_grow_inode(args, &blkno);
1329 if (error) {
1330 return error;
1331 }
1332 /*
1333 * Initialize the new leaf block.
1334 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001335 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001336 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1337 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 newblk->blkno = blkno;
1341 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1342 /*
1343 * Rebalance the entries across the two leaves, link the new
1344 * block into the leaves.
1345 */
1346 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001347 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (error) {
1349 return error;
1350 }
1351 /*
1352 * Insert the new entry in the correct block.
1353 */
1354 if (state->inleaf)
1355 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1356 else
1357 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1358 /*
1359 * Update last hashval in each block since we added the name.
1360 */
Dave Chinner41419562013-10-29 22:11:50 +11001361 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1362 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1363 xfs_dir3_leaf_check(dp, oldblk->bp);
1364 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return error;
1366}
1367
1368/*
1369 * Check a leaf block and its neighbors to see if the block should be
1370 * collapsed into one or the other neighbor. Always keep the block
1371 * with the smaller block number.
1372 * If the current block is over 50% full, don't try to join it, return 0.
1373 * If the block is empty, fill in the state structure and return 2.
1374 * If it can be collapsed, fill in the state structure and return 1.
1375 * If nothing can be done, return 0.
1376 */
1377int /* error */
1378xfs_dir2_leafn_toosmall(
1379 xfs_da_state_t *state, /* btree cursor */
1380 int *action) /* resulting action to take */
1381{
1382 xfs_da_state_blk_t *blk; /* leaf block */
1383 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001384 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int bytes; /* bytes in use */
1386 int count; /* leaf live entry count */
1387 int error; /* error return value */
1388 int forward; /* sibling block direction */
1389 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 xfs_dir2_leaf_t *leaf; /* leaf structure */
1391 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001392 struct xfs_dir3_icleaf_hdr leafhdr;
1393 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001394 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 /*
1397 * Check for the degenerate case of the block being over 50% full.
1398 * If so, it's not worth even looking to see if we might be able
1399 * to coalesce with a sibling.
1400 */
1401 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001402 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001403 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001404 ents = dp->d_ops->leaf_ents_p(leaf);
1405 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001406
1407 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001408 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001409 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * Blk over 50%, don't try to join.
1412 */
1413 *action = 0;
1414 return 0;
1415 }
1416 /*
1417 * Check for the degenerate case of the block being empty.
1418 * If the block is empty, we'll simply delete it, no need to
1419 * coalesce it with a sibling block. We choose (arbitrarily)
1420 * to merge with the forward block unless it is NULL.
1421 */
1422 if (count == 0) {
1423 /*
1424 * Make altpath point to the block we want to keep and
1425 * path point to the block we want to drop (this one).
1426 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001427 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001429 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 &rval);
1431 if (error)
1432 return error;
1433 *action = rval ? 2 : 0;
1434 return 0;
1435 }
1436 /*
1437 * Examine each sibling block to see if we can coalesce with
1438 * at least 25% free space to spare. We need to figure out
1439 * whether to merge with the forward or the backward block.
1440 * We prefer coalescing with the lower numbered sibling so as
1441 * to shrink a directory over time.
1442 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001443 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001445 struct xfs_dir3_icleaf_hdr hdr2;
1446
1447 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (blkno == 0)
1449 continue;
1450 /*
1451 * Read the sibling leaf block.
1452 */
Dave Chinner41419562013-10-29 22:11:50 +11001453 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001454 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /*
1459 * Count bytes in the two blocks combined.
1460 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001461 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001462 bytes = state->args->geo->blksize -
1463 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001464
Dave Chinner1d9025e2012-06-22 18:50:14 +10001465 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001466 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001467 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001468 count += hdr2.count - hdr2.stale;
1469 bytes -= count * sizeof(ents[0]);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * Fits with at least 25% to spare.
1473 */
1474 if (bytes >= 0)
1475 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001476 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 /*
1479 * Didn't like either block, give up.
1480 */
1481 if (i >= 2) {
1482 *action = 0;
1483 return 0;
1484 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /*
1487 * Make altpath point to the block we want to keep (the lower
1488 * numbered block) and path point to the block we want to drop.
1489 */
1490 memcpy(&state->altpath, &state->path, sizeof(state->path));
1491 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001492 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 &rval);
1494 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001495 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 &rval);
1497 if (error) {
1498 return error;
1499 }
1500 *action = rval ? 0 : 1;
1501 return 0;
1502}
1503
1504/*
1505 * Move all the leaf entries from drop_blk to save_blk.
1506 * This is done as part of a join operation.
1507 */
1508void
1509xfs_dir2_leafn_unbalance(
1510 xfs_da_state_t *state, /* cursor */
1511 xfs_da_state_blk_t *drop_blk, /* dead block */
1512 xfs_da_state_blk_t *save_blk) /* surviving block */
1513{
1514 xfs_da_args_t *args; /* operation arguments */
1515 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1516 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001517 struct xfs_dir3_icleaf_hdr savehdr;
1518 struct xfs_dir3_icleaf_hdr drophdr;
1519 struct xfs_dir2_leaf_entry *sents;
1520 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001521 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 args = state->args;
1524 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1525 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 drop_leaf = drop_blk->bp->b_addr;
1527 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001528
Dave Chinner01ba43b2013-10-29 22:11:52 +11001529 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1530 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1531 sents = dp->d_ops->leaf_ents_p(save_leaf);
1532 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
1535 * If there are any stale leaf entries, take this opportunity
1536 * to purge them.
1537 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001538 if (drophdr.stale)
1539 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1540 if (savehdr.stale)
1541 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * Move the entries from drop to the appropriate end of save.
1545 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001546 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001547 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001548 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1549 save_blk->bp, &savehdr, sents, 0,
1550 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001552 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1553 save_blk->bp, &savehdr, sents,
1554 savehdr.count, drophdr.count);
1555 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1556
1557 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001558 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1559 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001560 xfs_dir3_leaf_log_header(args, save_blk->bp);
1561 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001562
Dave Chinner41419562013-10-29 22:11:50 +11001563 xfs_dir3_leaf_check(dp, save_blk->bp);
1564 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * Top-level node form directory addname routine.
1569 */
1570int /* error */
1571xfs_dir2_node_addname(
1572 xfs_da_args_t *args) /* operation arguments */
1573{
1574 xfs_da_state_blk_t *blk; /* leaf block for insert */
1575 int error; /* error return value */
1576 int rval; /* sub-return value */
1577 xfs_da_state_t *state; /* btree cursor */
1578
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001579 trace_xfs_dir2_node_addname(args);
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * Allocate and initialize the state (btree cursor).
1583 */
1584 state = xfs_da_state_alloc();
1585 state->args = args;
1586 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Look up the name. We're not supposed to find it, but
1589 * this gives us the insertion point.
1590 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001591 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (error)
1593 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001594 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto done;
1596 }
1597 /*
1598 * Add the data entry to a data block.
1599 * Extravalid is set to a freeblock found by lookup.
1600 */
1601 rval = xfs_dir2_node_addname_int(args,
1602 state->extravalid ? &state->extrablk : NULL);
1603 if (rval) {
1604 goto done;
1605 }
1606 blk = &state->path.blk[state->path.active - 1];
1607 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1608 /*
1609 * Add the new leaf entry.
1610 */
1611 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1612 if (rval == 0) {
1613 /*
1614 * It worked, fix the hash values up the btree.
1615 */
Barry Naujok6a178102008-05-21 16:42:05 +10001616 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001617 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 } else {
1619 /*
1620 * It didn't work, we need to split the leaf block.
1621 */
1622 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001623 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto done;
1625 }
1626 /*
1627 * Split the leaf block and insert the new entry.
1628 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001629 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631done:
1632 xfs_da_state_free(state);
1633 return rval;
1634}
1635
1636/*
1637 * Add the data entry for a node-format directory name addition.
1638 * The leaf entry is added in xfs_dir2_leafn_add.
1639 * We may enter with a freespace block that the lookup found.
1640 */
1641static int /* error */
1642xfs_dir2_node_addname_int(
1643 xfs_da_args_t *args, /* operation arguments */
1644 xfs_da_state_blk_t *fblk) /* optional freespace block */
1645{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001646 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001648 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1650 xfs_inode_t *dp; /* incore directory inode */
1651 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1652 int error; /* error return value */
1653 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001654 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int findex; /* freespace entry index */
1656 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1657 xfs_dir2_db_t ifbno; /* initial freespace block no */
1658 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1659 int length; /* length of the new entry */
1660 int logfree; /* need to log free entry */
1661 xfs_mount_t *mp; /* filesystem mount point */
1662 int needlog; /* need to log data header */
1663 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001664 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001666 __be16 *bests;
1667 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001668 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 dp = args->dp;
1671 mp = dp->i_mount;
1672 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001673 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /*
1675 * If we came in with a freespace block that means that lookup
1676 * found an entry with our hash value. This is the freespace
1677 * block for that data entry.
1678 */
1679 if (fblk) {
1680 fbp = fblk->bp;
1681 /*
1682 * Remember initial freespace block number.
1683 */
1684 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001685 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001687 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001688 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * This means the free entry showed that the data block had
1692 * space for our entry, so we remembered it.
1693 * Use that data block.
1694 */
1695 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001696 ASSERT(findex < freehdr.nvalid);
1697 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1698 ASSERT(be16_to_cpu(bests[findex]) >= length);
1699 dbno = freehdr.firstdb + findex;
1700 } else {
1701 /*
1702 * The data block looked at didn't have enough room.
1703 * We'll start at the beginning of the freespace entries.
1704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 dbno = -1;
1706 findex = 0;
1707 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001708 } else {
1709 /*
1710 * Didn't come in with a freespace block, so no data block.
1711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 ifbno = dbno = -1;
1713 fbp = NULL;
1714 findex = 0;
1715 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 /*
1718 * If we don't have a data block yet, we're going to scan the
1719 * freespace blocks looking for one. Figure out what the
1720 * highest freespace block number is.
1721 */
1722 if (dbno == -1) {
1723 xfs_fileoff_t fo; /* freespace block number */
1724
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001725 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return error;
Dave Chinner2998ab12014-06-06 15:07:53 +10001727 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 fbno = ifbno;
1729 }
1730 /*
1731 * While we haven't identified a data block, search the freeblock
1732 * data for a good data block. If we find a null freeblock entry,
1733 * indicating a hole in the data blocks, remember that.
1734 */
1735 while (dbno == -1) {
1736 /*
1737 * If we don't have a freeblock in hand, get the next one.
1738 */
1739 if (fbp == NULL) {
1740 /*
1741 * Happens the first time through unless lookup gave
1742 * us a freespace block to start with.
1743 */
1744 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001745 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001746 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * If it's ifbno we already looked at it.
1749 */
1750 if (fbno == ifbno)
1751 fbno++;
1752 /*
1753 * If it's off the end we're done.
1754 */
1755 if (fbno >= lastfbno)
1756 break;
1757 /*
1758 * Read the block. There can be holes in the
1759 * freespace blocks, so this might not succeed.
1760 * This should be really rare, so there's no reason
1761 * to avoid it.
1762 */
Dave Chinner20252072012-11-12 22:54:13 +11001763 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001764 xfs_dir2_db_to_da(args->geo, fbno),
1765 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001766 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001768 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001770 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 findex = 0;
1772 }
1773 /*
1774 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001775 *
1776 * The bests initialisation should be where the bufer is read in
1777 * the above branch. But gcc is too stupid to realise that bests
1778 * and the freehdr are actually initialised if they are placed
1779 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001781 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001782 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001783 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1784 be16_to_cpu(bests[findex]) >= length)
1785 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 else {
1787 /*
1788 * Are we done with the freeblock?
1789 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001790 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*
1792 * Drop the block.
1793 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001794 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 fbp = NULL;
1796 if (fblk && fblk->bp)
1797 fblk->bp = NULL;
1798 }
1799 }
1800 }
1801 /*
1802 * If we don't have a data block, we need to allocate one and make
1803 * the freespace entries refer to it.
1804 */
1805 if (unlikely(dbno == -1)) {
1806 /*
1807 * Not allowed to allocate, return failure.
1808 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001809 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001810 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Allocate and initialize the new data block.
1814 */
1815 if (unlikely((error = xfs_dir2_grow_inode(args,
1816 XFS_DIR2_DATA_SPACE,
1817 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001818 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * If (somehow) we have a freespace block, get rid of it.
1823 */
1824 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001825 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (fblk && fblk->bp)
1827 fblk->bp = NULL;
1828
1829 /*
1830 * Get the freespace block corresponding to the data block
1831 * that was just allocated.
1832 */
Dave Chinner8f661932014-06-06 15:15:59 +10001833 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001834 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001835 xfs_dir2_db_to_da(args->geo, fbno),
1836 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * If there wasn't a freespace block, the read will
1842 * return a NULL fbp. Allocate and initialize a new one.
1843 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001844 if (!fbp) {
1845 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1846 &fbno);
1847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Dave Chinner8f661932014-06-06 15:15:59 +10001850 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001851 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001852"%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 +11001853 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001854 (long long)dp->d_ops->db_to_fdb(
1855 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 (long long)dbno, (long long)fbno,
1857 (unsigned long long)ifbno, lastfbno);
1858 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001859 xfs_alert(mp,
1860 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 fblk,
1862 (unsigned long long)fblk->blkno,
1863 fblk->index,
1864 fblk->magic);
1865 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001866 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1869 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001870 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
1873 /*
1874 * Get a buffer for the new block.
1875 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001876 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001879 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001880 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001881 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001884 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001886 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001887 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001888 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001889 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001891 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001892 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001893 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895
1896 /*
1897 * Set the freespace block index from the data block number.
1898 */
Dave Chinner8f661932014-06-06 15:15:59 +10001899 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /*
1901 * If it's after the end of the current entries in the
1902 * freespace block, extend that table.
1903 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001904 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001905 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001906 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
1908 * Tag new entry so nused will go up.
1909 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001910 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 /*
1913 * If this entry was for an empty data block
1914 * (this should always be true) then update the header.
1915 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001916 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1917 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001918 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001919 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 /*
1922 * Update the real value in the table.
1923 * We haven't allocated the data entry yet so this will
1924 * change again.
1925 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001926 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001927 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001928 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 logfree = 1;
1930 }
1931 /*
1932 * We had a data block so we don't have to make a new one.
1933 */
1934 else {
1935 /*
1936 * If just checking, we succeeded.
1937 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001938 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /*
1942 * Read the data block in.
1943 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001944 error = xfs_dir3_data_read(tp, dp,
1945 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001946 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001949 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001950 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 logfree = 0;
1952 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001953 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /*
1955 * Point to the existing unused space.
1956 */
1957 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001958 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 needscan = needlog = 0;
1960 /*
1961 * Mark the first part of the unused space, inuse for us.
1962 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001963 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001964 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 &needlog, &needscan);
1966 /*
1967 * Fill in the new entry and log it.
1968 */
1969 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001970 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 dep->namelen = args->namelen;
1972 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001973 dp->d_ops->data_put_ftype(dep, args->filetype);
1974 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001975 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001976 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /*
1978 * Rescan the block for bestfree if needed.
1979 */
1980 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001981 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /*
1983 * Log the data block header if needed.
1984 */
1985 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001986 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /*
1988 * If the freespace entry is now wrong, update it.
1989 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001990 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11001991 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1992 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 logfree = 1;
1994 }
1995 /*
1996 * Log the freespace entry if needed.
1997 */
1998 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001999 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * Return the data block and offset in args, then drop the data block.
2002 */
2003 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002004 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
2006}
2007
2008/*
2009 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002010 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * The only real output is the inode number of the entry.
2012 */
2013int /* error */
2014xfs_dir2_node_lookup(
2015 xfs_da_args_t *args) /* operation arguments */
2016{
2017 int error; /* error return value */
2018 int i; /* btree level */
2019 int rval; /* operation return value */
2020 xfs_da_state_t *state; /* btree cursor */
2021
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002022 trace_xfs_dir2_node_lookup(args);
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /*
2025 * Allocate and initialize the btree cursor.
2026 */
2027 state = xfs_da_state_alloc();
2028 state->args = args;
2029 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
2031 * Fill in the path to the entry in the cursor.
2032 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002033 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (error)
2035 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002036 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2037 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002038 xfs_dir2_data_entry_t *dep;
2039
Dave Chinner1d9025e2012-06-22 18:50:14 +10002040 dep = (xfs_dir2_data_entry_t *)
2041 ((char *)state->extrablk.bp->b_addr +
2042 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002043 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
2046 * Release the btree blocks and leaf block.
2047 */
2048 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002049 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 state->path.blk[i].bp = NULL;
2051 }
2052 /*
2053 * Release the data block if we have it.
2054 */
2055 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002056 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 state->extrablk.bp = NULL;
2058 }
2059 xfs_da_state_free(state);
2060 return rval;
2061}
2062
2063/*
2064 * Remove an entry from a node-format directory.
2065 */
2066int /* error */
2067xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002068 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002070 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int error; /* error return value */
2072 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002073 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002075 trace_xfs_dir2_node_removename(args);
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 /*
2078 * Allocate and initialize the btree cursor.
2079 */
2080 state = xfs_da_state_alloc();
2081 state->args = args;
2082 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002083
2084 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002085 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002086 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002087 goto out_free;
2088
2089 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002090 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002091 error = rval;
2092 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 blk = &state->path.blk[state->path.active - 1];
2096 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2097 ASSERT(state->extravalid);
2098 /*
2099 * Remove the leaf and data entries.
2100 * Extrablk refers to the data block.
2101 */
2102 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2103 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002104 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002105 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Fix the hash values up the btree.
2108 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002109 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * If we need to join leaf blocks, do it.
2112 */
2113 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002114 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
2116 * If no errors so far, try conversion to leaf format.
2117 */
2118 if (!error)
2119 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002120out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 xfs_da_state_free(state);
2122 return error;
2123}
2124
2125/*
2126 * Replace an entry's inode number in a node-format directory.
2127 */
2128int /* error */
2129xfs_dir2_node_replace(
2130 xfs_da_args_t *args) /* operation arguments */
2131{
2132 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002133 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 xfs_dir2_data_entry_t *dep; /* data entry changed */
2135 int error; /* error return value */
2136 int i; /* btree level */
2137 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002138 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 xfs_dir2_leaf_t *leaf; /* leaf structure */
2140 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2141 int rval; /* internal return value */
2142 xfs_da_state_t *state; /* btree cursor */
2143
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002144 trace_xfs_dir2_node_replace(args);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * Allocate and initialize the btree cursor.
2148 */
2149 state = xfs_da_state_alloc();
2150 state->args = args;
2151 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002152
2153 /*
2154 * We have to save new inode number and ftype since
2155 * xfs_da3_node_lookup_int() is going to overwrite them
2156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002158 ftype = args->filetype;
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * Lookup the entry to change in the btree.
2162 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002163 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (error) {
2165 rval = error;
2166 }
2167 /*
2168 * It should be found, since the vnodeops layer has looked it up
2169 * and locked it. But paranoia is good.
2170 */
Dave Chinner24513372014-06-25 14:58:08 +10002171 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002172 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
2174 * Find the leaf entry.
2175 */
2176 blk = &state->path.blk[state->path.active - 1];
2177 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002178 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002179 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002180 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 ASSERT(state->extravalid);
2182 /*
2183 * Point to the data entry.
2184 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002185 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002186 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2187 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002189 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002190 xfs_dir2_dataptr_to_off(args->geo,
2191 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002192 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Fill in the new inode number and log the entry.
2195 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002196 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002197 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002198 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 rval = 0;
2200 }
2201 /*
2202 * Didn't find it, and we're holding a data block. Drop it.
2203 */
2204 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002205 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 state->extrablk.bp = NULL;
2207 }
2208 /*
2209 * Release all the buffers in the cursor.
2210 */
2211 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002212 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 state->path.blk[i].bp = NULL;
2214 }
2215 xfs_da_state_free(state);
2216 return rval;
2217}
2218
2219/*
2220 * Trim off a trailing empty freespace block.
2221 * Return (in rvalp) 1 if we did it, 0 if not.
2222 */
2223int /* error */
2224xfs_dir2_node_trim_free(
2225 xfs_da_args_t *args, /* operation arguments */
2226 xfs_fileoff_t fo, /* free block number */
2227 int *rvalp) /* out: did something */
2228{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002229 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xfs_inode_t *dp; /* incore directory inode */
2231 int error; /* error return code */
2232 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002234 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 tp = args->trans;
2238 /*
2239 * Read the freespace block.
2240 */
Dave Chinner20252072012-11-12 22:54:13 +11002241 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 /*
2245 * There can be holes in freespace. If fo is a hole, there's
2246 * nothing to do.
2247 */
Dave Chinner20252072012-11-12 22:54:13 +11002248 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002250 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002251 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /*
2254 * If there are used entries, there's nothing to do.
2255 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002256 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002257 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *rvalp = 0;
2259 return 0;
2260 }
2261 /*
2262 * Blow the block away.
2263 */
Dave Chinner2998ab12014-06-06 15:07:53 +10002264 error = xfs_dir2_shrink_inode(args,
2265 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2266 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /*
2268 * Can't fail with ENOSPC since that only happens with no
2269 * space reservation, when breaking up an extent into two
2270 * pieces. This is the last block of an extent.
2271 */
Dave Chinner24513372014-06-25 14:58:08 +10002272 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002273 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return error;
2275 }
2276 /*
2277 * Return that we succeeded.
2278 */
2279 *rvalp = 1;
2280 return 0;
2281}