blob: 527b7337a43be4e9d43a1509c6c848dea49925ae [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
38 * Function declarations.
39 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100040static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
41 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
43 xfs_da_state_blk_t *blk1,
44 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100045static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 int index, xfs_da_state_blk_t *dblk,
47 int *rval);
48static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
49 xfs_da_state_blk_t *fblk);
50
Dave Chinner24df33b2013-04-12 07:30:21 +100051/*
52 * Check internal consistency of a leafn block.
53 */
54#ifdef DEBUG
Dave Chinner41419562013-10-29 22:11:50 +110055#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100056do { \
Dave Chinner41419562013-10-29 22:11:50 +110057 if (!xfs_dir3_leafn_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100058 ASSERT(0); \
59} while (0);
60
61static bool
62xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110063 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100064 struct xfs_buf *bp)
65{
66 struct xfs_dir2_leaf *leaf = bp->b_addr;
67 struct xfs_dir3_icleaf_hdr leafhdr;
68
Dave Chinner01ba43b2013-10-29 22:11:52 +110069 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100070
71 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
72 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
73 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
74 return false;
75 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
76 return false;
77
Dave Chinner41419562013-10-29 22:11:50 +110078 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100079}
80#else
Dave Chinner41419562013-10-29 22:11:50 +110081#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100082#endif
83
Dave Chinnercbc8adf2013-04-03 16:11:21 +110084static bool
85xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110086 struct xfs_buf *bp)
87{
88 struct xfs_mount *mp = bp->b_target->bt_mount;
89 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110090
Dave Chinnercbc8adf2013-04-03 16:11:21 +110091 if (xfs_sb_version_hascrc(&mp->m_sb)) {
92 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
93
94 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
95 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +100096 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinnercbc8adf2013-04-03 16:11:21 +110097 return false;
98 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
99 return false;
100 } else {
101 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
102 return false;
103 }
104
105 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
106
107 return true;
108}
109
110static void
111xfs_dir3_free_read_verify(
112 struct xfs_buf *bp)
113{
114 struct xfs_mount *mp = bp->b_target->bt_mount;
115
Eric Sandeence5028c2014-02-27 15:23:10 +1100116 if (xfs_sb_version_hascrc(&mp->m_sb) &&
117 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000118 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100119 else if (!xfs_dir3_free_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000120 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100121
122 if (bp->b_error)
123 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100124}
Dave Chinner20252072012-11-12 22:54:13 +1100125
Dave Chinner612cfbf2012-11-14 17:52:32 +1100126static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100127xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100128 struct xfs_buf *bp)
129{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130 struct xfs_mount *mp = bp->b_target->bt_mount;
131 struct xfs_buf_log_item *bip = bp->b_fspriv;
132 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
133
134 if (!xfs_dir3_free_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000135 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100136 xfs_verifier_error(bp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100137 return;
138 }
139
140 if (!xfs_sb_version_hascrc(&mp->m_sb))
141 return;
142
143 if (bip)
144 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
145
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100146 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100147}
148
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100149const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100150 .verify_read = xfs_dir3_free_read_verify,
151 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100152};
Dave Chinner20252072012-11-12 22:54:13 +1100153
Dave Chinner612cfbf2012-11-14 17:52:32 +1100154
Dave Chinner20252072012-11-12 22:54:13 +1100155static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100156__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100157 struct xfs_trans *tp,
158 struct xfs_inode *dp,
159 xfs_dablk_t fbno,
160 xfs_daddr_t mappedbno,
161 struct xfs_buf **bpp)
162{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100163 int err;
164
165 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100166 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100167
168 /* try read returns without an error or *bpp if it lands in a hole */
169 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100170 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100171 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100172}
173
174int
175xfs_dir2_free_read(
176 struct xfs_trans *tp,
177 struct xfs_inode *dp,
178 xfs_dablk_t fbno,
179 struct xfs_buf **bpp)
180{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100181 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100182}
183
184static int
185xfs_dir2_free_try_read(
186 struct xfs_trans *tp,
187 struct xfs_inode *dp,
188 xfs_dablk_t fbno,
189 struct xfs_buf **bpp)
190{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100191 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
192}
193
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100194static int
195xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000196 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100197 xfs_dir2_db_t fbno,
198 struct xfs_buf **bpp)
199{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000200 struct xfs_trans *tp = args->trans;
201 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100202 struct xfs_mount *mp = dp->i_mount;
203 struct xfs_buf *bp;
204 int error;
205 struct xfs_dir3_icfree_hdr hdr;
206
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000207 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100208 -1, &bp, XFS_DATA_FORK);
209 if (error)
210 return error;
211
Dave Chinner61fe1352013-04-03 16:11:30 +1100212 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100213 bp->b_ops = &xfs_dir3_free_buf_ops;
214
215 /*
216 * Initialize the new block to be empty, and remember
217 * its first slot as our empty slot.
218 */
Dave Chinnere400d272013-05-28 18:37:17 +1000219 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
220 memset(&hdr, 0, sizeof(hdr));
221
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100222 if (xfs_sb_version_hascrc(&mp->m_sb)) {
223 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
224
225 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000226
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100227 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
228 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000229 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000230 } else
231 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100232 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100233 *bpp = bp;
234 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * Log entries from a freespace block.
239 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000240STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000242 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000243 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int first, /* first entry to log */
245 int last) /* last entry to log */
246{
247 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100248 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Dave Chinner1d9025e2012-06-22 18:50:14 +1000250 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000251 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100252 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
253 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000254 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100255 (uint)((char *)&bests[first] - (char *)free),
256 (uint)((char *)&bests[last] - (char *)free +
257 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * Log header from a freespace block.
262 */
263static void
264xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000265 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000266 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Dave Chinner836a94a2013-08-12 20:49:44 +1000268#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 xfs_dir2_free_t *free; /* freespace structure */
270
Dave Chinner1d9025e2012-06-22 18:50:14 +1000271 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100272 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
273 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000274#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000275 xfs_trans_log_buf(args->trans, bp, 0,
276 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/*
280 * Convert a leaf-format directory to a node-format directory.
281 * We need to change the magic number of the leaf block, and copy
282 * the freespace table out of the leaf block into its own block.
283 */
284int /* error */
285xfs_dir2_leaf_to_node(
286 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000287 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 xfs_inode_t *dp; /* incore directory inode */
290 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000291 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xfs_dir2_db_t fdb; /* freespace block number */
293 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100294 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 int i; /* leaf freespace index */
296 xfs_dir2_leaf_t *leaf; /* leaf structure */
297 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int n; /* count of live freespc ents */
299 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100300 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100302 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000304 trace_xfs_dir2_leaf_to_node(args);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 tp = args->trans;
308 /*
309 * Add a freespace block to the directory.
310 */
311 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
312 return error;
313 }
Dave Chinner30028032014-06-06 15:08:18 +1000314 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Get the buffer for the new freespace block.
317 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000318 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100319 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100321
Dave Chinner1d9025e2012-06-22 18:50:14 +1000322 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100323 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000324 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000325 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100326 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000327 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /*
330 * Copy freespace entries from the leaf block to the new block.
331 * Count active entries.
332 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100333 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500334 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100335 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100336 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100338 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100342 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100344 freehdr.nused = n;
345 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
346
Dave Chinner01ba43b2013-10-29 22:11:52 +1100347 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000348 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
349 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100350
Dave Chinner24df33b2013-04-12 07:30:21 +1000351 /*
352 * Converting the leaf to a leafnode is just a matter of changing the
353 * magic number and the ops. Do the change directly to the buffer as
354 * it's less work (and less code) than decoding the header to host
355 * format and back again.
356 */
357 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
358 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
359 else
360 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
361 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100362 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000363 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100364 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
368/*
369 * Add a leaf entry to a leaf block in a node-form directory.
370 * The other work necessary is done from the caller.
371 */
372static int /* error */
373xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000374 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 xfs_da_args_t *args, /* operation arguments */
376 int index) /* insertion pt for new entry */
377{
378 int compact; /* compacting stale leaves */
379 xfs_inode_t *dp; /* incore directory inode */
380 int highstale; /* next stale entry */
381 xfs_dir2_leaf_t *leaf; /* leaf structure */
382 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
383 int lfloghigh; /* high leaf entry logging */
384 int lfloglow; /* low leaf entry logging */
385 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000386 struct xfs_dir3_icleaf_hdr leafhdr;
387 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000389 trace_xfs_dir2_leafn_add(args, index);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000392 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100393 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100394 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /*
397 * Quick check just to make sure we are not going to index
398 * into other peoples memory
399 */
400 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000401 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * If there are already the maximum number of leaf entries in
405 * the block, if there are no stale entries it won't fit.
406 * Caller will do a split. If there are stale entries we'll do
407 * a compact.
408 */
409
Dave Chinner8f661932014-06-06 15:15:59 +1000410 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000411 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000412 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000413 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else
415 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000416 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
417 ASSERT(index == leafhdr.count ||
418 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Barry Naujok6a178102008-05-21 16:42:05 +1000420 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422
423 /*
424 * Compact out all but one stale leaf entry. Leaves behind
425 * the entry closest to index.
426 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000427 if (compact)
428 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
429 &highstale, &lfloglow, &lfloghigh);
430 else if (leafhdr.stale) {
431 /*
432 * Set impossible logging indices for this case.
433 */
434 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 lfloghigh = -1;
436 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /*
439 * Insert the new entry, log everything.
440 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000441 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200442 highstale, &lfloglow, &lfloghigh);
443
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100444 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000445 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100446 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000447
Dave Chinner01ba43b2013-10-29 22:11:52 +1100448 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000449 xfs_dir3_leaf_log_header(args, bp);
450 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100451 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return 0;
453}
454
455#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100456static void
457xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100458 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100459 struct xfs_buf *bp,
460 xfs_dir2_db_t db)
461{
462 struct xfs_dir3_icfree_hdr hdr;
463
Dave Chinner01ba43b2013-10-29 22:11:52 +1100464 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100465
Dave Chinner8f661932014-06-06 15:15:59 +1000466 ASSERT((hdr.firstdb %
467 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100468 ASSERT(hdr.firstdb <= db);
469 ASSERT(db < hdr.firstdb + hdr.nvalid);
470}
471#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100472#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#endif /* DEBUG */
474
475/*
476 * Return the last hash value in the leaf.
477 * Stale entries are ok.
478 */
479xfs_dahash_t /* hash value */
480xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100481 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000482 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int *count) /* count of entries in leaf */
484{
Dave Chinner24df33b2013-04-12 07:30:21 +1000485 struct xfs_dir2_leaf *leaf = bp->b_addr;
486 struct xfs_dir2_leaf_entry *ents;
487 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dave Chinner01ba43b2013-10-29 22:11:52 +1100489 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000490
491 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
492 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000495 *count = leafhdr.count;
496 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000498
Dave Chinner41419562013-10-29 22:11:50 +1100499 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000500 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000504 * Look up a leaf entry for space to add a name in a node-format leaf block.
505 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000507STATIC int
508xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000509 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 xfs_da_args_t *args, /* operation arguments */
511 int *indexp, /* out: leaf entry index */
512 xfs_da_state_t *state) /* state to fill in */
513{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000515 xfs_dir2_db_t curdb = -1; /* current data block number */
516 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xfs_inode_t *dp; /* incore directory inode */
518 int error; /* error return value */
519 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000520 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int index; /* leaf entry index */
522 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000523 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
525 xfs_mount_t *mp; /* filesystem mount point */
526 xfs_dir2_db_t newdb; /* new data block number */
527 xfs_dir2_db_t newfdb; /* new free block number */
528 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000529 struct xfs_dir2_leaf_entry *ents;
530 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 dp = args->dp;
533 tp = args->trans;
534 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000535 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100536 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100537 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000538
Dave Chinner41419562013-10-29 22:11:50 +1100539 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000540 ASSERT(leafhdr.count > 0);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
543 * Look up the hash value in the leaf entries.
544 */
545 index = xfs_dir2_leaf_search_hash(args, bp);
546 /*
547 * Do we have a buffer coming in?
548 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000549 if (state->extravalid) {
550 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000552 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000553 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100554 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
555 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100557 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /*
559 * Loop over leaf entries with the right hash value.
560 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000561 for (lep = &ents[index];
562 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
563 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Skip stale leaf entries.
566 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100567 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 continue;
569 /*
570 * Pull the data block number from the entry.
571 */
Dave Chinner30028032014-06-06 15:08:18 +1000572 newdb = xfs_dir2_dataptr_to_db(args->geo,
573 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
575 * For addname, we're looking for a place to put the new entry.
576 * We want to use a data block with an entry of equal
577 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000578 *
579 * If this block isn't the data block we already have
580 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000582 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100583 __be16 *bests;
584
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000585 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000587 * Convert the data block to the free block
588 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Dave Chinner8f661932014-06-06 15:15:59 +1000590 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000594 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000596 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000599 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100600
601 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000602 xfs_dir2_db_to_da(args->geo,
603 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100604 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000605 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000607 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100608
Dave Chinner01ba43b2013-10-29 22:11:52 +1100609 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Dave Chinner8f661932014-06-06 15:15:59 +1000614 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000616 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500618 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100619 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000620 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
621 XFS_ERRLEVEL_LOW, mp);
622 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000623 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000624 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000626 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100627 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 /* Didn't find any space */
632 fi = -1;
633out:
Barry Naujok6a178102008-05-21 16:42:05 +1000634 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 if (curbp) {
636 /* Giving back a free block. */
637 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 state->extrablk.index = fi;
640 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100641
642 /*
643 * Important: this magic number is not in the buffer - it's for
644 * buffer type information and therefore only the free/data type
645 * matters here, not whether CRCs are enabled or not.
646 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000647 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
648 } else {
649 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000652 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
654 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000655 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 * Look up a leaf entry in a node-format leaf block.
660 * The extrablk in state a data block.
661 */
662STATIC int
663xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000664 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000665 xfs_da_args_t *args, /* operation arguments */
666 int *indexp, /* out: leaf entry index */
667 xfs_da_state_t *state) /* state to fill in */
668{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000669 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000670 xfs_dir2_db_t curdb = -1; /* current data block number */
671 xfs_dir2_data_entry_t *dep; /* data block entry */
672 xfs_inode_t *dp; /* incore directory inode */
673 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 int index; /* leaf entry index */
675 xfs_dir2_leaf_t *leaf; /* leaf structure */
676 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
677 xfs_mount_t *mp; /* filesystem mount point */
678 xfs_dir2_db_t newdb; /* new data block number */
679 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000680 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000681 struct xfs_dir2_leaf_entry *ents;
682 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683
684 dp = args->dp;
685 tp = args->trans;
686 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000687 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100688 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100689 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000690
Dave Chinner41419562013-10-29 22:11:50 +1100691 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000692 ASSERT(leafhdr.count > 0);
693
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000694 /*
695 * Look up the hash value in the leaf entries.
696 */
697 index = xfs_dir2_leaf_search_hash(args, bp);
698 /*
699 * Do we have a buffer coming in?
700 */
701 if (state->extravalid) {
702 curbp = state->extrablk.bp;
703 curdb = state->extrablk.blkno;
704 }
705 /*
706 * Loop over leaf entries with the right hash value.
707 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000708 for (lep = &ents[index];
709 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
710 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000711 /*
712 * Skip stale leaf entries.
713 */
714 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
715 continue;
716 /*
717 * Pull the data block number from the entry.
718 */
Dave Chinner30028032014-06-06 15:08:18 +1000719 newdb = xfs_dir2_dataptr_to_db(args->geo,
720 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000721 /*
722 * Not adding a new entry, so we really want to find
723 * the name given to us.
724 *
725 * If it's a different data block, go get it.
726 */
727 if (newdb != curdb) {
728 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000729 * If we had a block before that we aren't saving
730 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000732 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
733 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000734 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000735 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000736 * If needing the block that is saved with a CI match,
737 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000738 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000739 if (args->cmpresult != XFS_CMP_DIFFERENT &&
740 newdb == state->extrablk.blkno) {
741 ASSERT(state->extravalid);
742 curbp = state->extrablk.bp;
743 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100744 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000745 xfs_dir2_db_to_da(args->geo,
746 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100747 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000748 if (error)
749 return error;
750 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100751 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000752 curdb = newdb;
753 }
754 /*
755 * Point to the data entry.
756 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000757 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000758 xfs_dir2_dataptr_to_off(args->geo,
759 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000760 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000761 * Compare the entry and if it's an exact match, return
762 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000763 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000764 */
Barry Naujok5163f952008-05-21 16:41:01 +1000765 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
766 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000767 /* If there is a CI match block, drop it */
768 if (args->cmpresult != XFS_CMP_DIFFERENT &&
769 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000770 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000771 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000772 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100773 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000774 *indexp = index;
775 state->extravalid = 1;
776 state->extrablk.bp = curbp;
777 state->extrablk.blkno = curdb;
778 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000779 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000780 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100781 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100782 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000783 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000784 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000785 }
786 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000787 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000788 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000789 if (args->cmpresult == XFS_CMP_DIFFERENT) {
790 /* Giving back last used data block. */
791 state->extravalid = 1;
792 state->extrablk.bp = curbp;
793 state->extrablk.index = -1;
794 state->extrablk.blkno = curdb;
795 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100796 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100797 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000798 } else {
799 /* If the curbp is not the CI match block, drop it */
800 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000801 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000802 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000803 } else {
804 state->extravalid = 0;
805 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000806 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000807 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000808}
809
810/*
811 * Look up a leaf entry in a node-format leaf block.
812 * If this is an addname then the extrablk in state is a freespace block,
813 * otherwise it's a data block.
814 */
815int
816xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000817 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000818 xfs_da_args_t *args, /* operation arguments */
819 int *indexp, /* out: leaf entry index */
820 xfs_da_state_t *state) /* state to fill in */
821{
Barry Naujok6a178102008-05-21 16:42:05 +1000822 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000823 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
824 state);
825 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
826}
827
828/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * Move count leaf entries from source to destination leaf.
830 * Log entries and headers. Stale entries are preserved.
831 */
832static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000833xfs_dir3_leafn_moveents(
834 xfs_da_args_t *args, /* operation arguments */
835 struct xfs_buf *bp_s, /* source */
836 struct xfs_dir3_icleaf_hdr *shdr,
837 struct xfs_dir2_leaf_entry *sents,
838 int start_s,/* source leaf index */
839 struct xfs_buf *bp_d, /* destination */
840 struct xfs_dir3_icleaf_hdr *dhdr,
841 struct xfs_dir2_leaf_entry *dents,
842 int start_d,/* destination leaf index */
843 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Dave Chinner24df33b2013-04-12 07:30:21 +1000845 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000847 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /*
850 * Silently return if nothing to do.
851 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000852 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * If the destination index is not the end of the current
857 * destination leaf entries, open up a hole in the destination
858 * to hold the new entries.
859 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000860 if (start_d < dhdr->count) {
861 memmove(&dents[start_d + count], &dents[start_d],
862 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000863 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000864 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 /*
867 * If the source has stale leaves, count the ones in the copy range
868 * so we can update the header correctly.
869 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000870 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 int i; /* temp leaf index */
872
873 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000874 if (sents[i].address ==
875 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 stale++;
877 }
878 } else
879 stale = 0;
880 /*
881 * Copy the leaf entries from source to destination.
882 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000883 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000885 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
888 * If there are source entries after the ones we copied,
889 * delete the ones we copied by sliding the next ones down.
890 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000891 if (start_s + count < shdr->count) {
892 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000894 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
898 * Update the headers and log them.
899 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000900 shdr->count -= count;
901 shdr->stale -= stale;
902 dhdr->count += count;
903 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/*
907 * Determine the sort order of two leaf blocks.
908 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
909 */
910int /* sort order */
911xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100912 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000913 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
914 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
917 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
918 struct xfs_dir2_leaf_entry *ents1;
919 struct xfs_dir2_leaf_entry *ents2;
920 struct xfs_dir3_icleaf_hdr hdr1;
921 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Chinner01ba43b2013-10-29 22:11:52 +1100923 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
924 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100925 ents1 = dp->d_ops->leaf_ents_p(leaf1);
926 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000927
928 if (hdr1.count > 0 && hdr2.count > 0 &&
929 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
930 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
931 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return 1;
933 return 0;
934}
935
936/*
937 * Rebalance leaf entries between two leaf blocks.
938 * This is actually only called when the second block is new,
939 * though the code deals with the general case.
940 * A new entry will be inserted in one of the blocks, and that
941 * entry is taken into account when balancing.
942 */
943static void
944xfs_dir2_leafn_rebalance(
945 xfs_da_state_t *state, /* btree cursor */
946 xfs_da_state_blk_t *blk1, /* first btree block */
947 xfs_da_state_blk_t *blk2) /* second btree block */
948{
949 xfs_da_args_t *args; /* operation arguments */
950 int count; /* count (& direction) leaves */
951 int isleft; /* new goes in left leaf */
952 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
953 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
954 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000955#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int oldstale; /* old count of stale leaves */
957#endif
958 int oldsum; /* old total leaf count */
959 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000960 struct xfs_dir2_leaf_entry *ents1;
961 struct xfs_dir2_leaf_entry *ents2;
962 struct xfs_dir3_icleaf_hdr hdr1;
963 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100964 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 args = state->args;
967 /*
968 * If the block order is wrong, swap the arguments.
969 */
Dave Chinner41419562013-10-29 22:11:50 +1100970 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 xfs_da_state_blk_t *tmp; /* temp for block swap */
972
973 tmp = blk1;
974 blk1 = blk2;
975 blk2 = tmp;
976 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000977 leaf1 = blk1->bp->b_addr;
978 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100979 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
980 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100981 ents1 = dp->d_ops->leaf_ents_p(leaf1);
982 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000983
984 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000985#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000986 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987#endif
988 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * If the old leaf count was odd then the new one will be even,
992 * so we need to divide the new count evenly.
993 */
994 if (oldsum & 1) {
995 xfs_dahash_t midhash; /* middle entry hash value */
996
Dave Chinner24df33b2013-04-12 07:30:21 +1000997 if (mid >= hdr1.count)
998 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001000 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 isleft = args->hashval <= midhash;
1002 }
1003 /*
1004 * If the old count is even then the new count is odd, so there's
1005 * no preferred side for the new entry.
1006 * Pick the left one.
1007 */
1008 else
1009 isleft = 1;
1010 /*
1011 * Calculate moved entry count. Positive means left-to-right,
1012 * negative means right-to-left. Then move the entries.
1013 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001014 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001016 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1017 hdr1.count - count, blk2->bp,
1018 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001020 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1021 blk1->bp, &hdr1, ents1,
1022 hdr1.count, count);
1023
1024 ASSERT(hdr1.count + hdr2.count == oldsum);
1025 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1026
1027 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001028 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1029 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001030 xfs_dir3_leaf_log_header(args, blk1->bp);
1031 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001032
Dave Chinner41419562013-10-29 22:11:50 +11001033 xfs_dir3_leaf_check(dp, blk1->bp);
1034 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /*
1037 * Mark whether we're inserting into the old or new leaf.
1038 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001039 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001041 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 state->inleaf = !swap;
1043 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * Adjust the expected index for insertion.
1047 */
1048 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001049 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001050
1051 /*
1052 * Finally sanity check just to make sure we are not returning a
1053 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
Dave Chinner41419562013-10-29 22:11:50 +11001055 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 state->inleaf = 1;
1057 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001058 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001059 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001060 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062}
1063
Dave Chinner20252072012-11-12 22:54:13 +11001064static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001065xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001066 xfs_da_args_t *args,
1067 struct xfs_dir2_data_hdr *hdr,
1068 struct xfs_dir2_free *free,
1069 xfs_dir2_db_t fdb,
1070 int findex,
1071 struct xfs_buf *fbp,
1072 int longest)
1073{
Dave Chinner20252072012-11-12 22:54:13 +11001074 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001075 __be16 *bests;
1076 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001077 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001078
Dave Chinner01ba43b2013-10-29 22:11:52 +11001079 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001080 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001081 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001082 /*
1083 * Data block is not empty, just set the free entry to the new
1084 * value.
1085 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001086 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001087 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001088 return 0;
1089 }
1090
1091 /* One less used entry in the free table. */
1092 freehdr.nused--;
1093
1094 /*
1095 * If this was the last entry in the table, we can trim the table size
1096 * back. There might be other entries at the end referring to
1097 * non-existent data blocks, get those too.
1098 */
1099 if (findex == freehdr.nvalid - 1) {
1100 int i; /* free entry index */
1101
1102 for (i = findex - 1; i >= 0; i--) {
1103 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1104 break;
1105 }
1106 freehdr.nvalid = i + 1;
1107 logfree = 0;
1108 } else {
1109 /* Not the last entry, just punch it out. */
1110 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001111 logfree = 1;
1112 }
1113
Dave Chinner01ba43b2013-10-29 22:11:52 +11001114 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001115 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001116
1117 /*
1118 * If there are no useful entries left in the block, get rid of the
1119 * block if we can.
1120 */
1121 if (!freehdr.nused) {
1122 int error;
1123
1124 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1125 if (error == 0) {
1126 fbp = NULL;
1127 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001128 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001129 return error;
1130 /*
1131 * It's possible to get ENOSPC if there is no
1132 * space reservation. In this case some one
1133 * else will eventually get rid of this block.
1134 */
1135 }
1136
Dave Chinner20252072012-11-12 22:54:13 +11001137 /* Log the free entry that changed, unless we got rid of it. */
1138 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001139 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001140 return 0;
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * Remove an entry from a node directory.
1145 * This removes the leaf entry and the data entry,
1146 * and updates the free block if necessary.
1147 */
1148static int /* error */
1149xfs_dir2_leafn_remove(
1150 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001151 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int index, /* leaf entry index */
1153 xfs_da_state_blk_t *dblk, /* data block */
1154 int *rval) /* resulting block needs join */
1155{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001156 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 xfs_dir2_data_entry_t *dep; /* data block entry */
1160 xfs_inode_t *dp; /* incore directory inode */
1161 xfs_dir2_leaf_t *leaf; /* leaf structure */
1162 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1163 int longest; /* longest data free entry */
1164 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 int needlog; /* need to log data header */
1166 int needscan; /* need to rescan data frees */
1167 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001168 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001169 struct xfs_dir3_icleaf_hdr leafhdr;
1170 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001172 trace_xfs_dir2_leafn_remove(args, index);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 dp = args->dp;
1175 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001176 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001177 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001178 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /*
1181 * Point to the entry we're removing.
1182 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001183 lep = &ents[index];
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /*
1186 * Extract the data block and offset from the entry.
1187 */
Dave Chinner30028032014-06-06 15:08:18 +10001188 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001190 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Kill the leaf entry by marking it stale.
1195 * Log the leaf block changes.
1196 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001197 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001198 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001199 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001200
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001201 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001202 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * Make the data entry free. Keep track of the longest freespace
1206 * in the data block in case it changes.
1207 */
1208 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001209 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001210 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001211 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001212 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001214 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001215 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * Rescan the data block freespaces for bestfree.
1218 * Log the data block header if needed.
1219 */
1220 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001221 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001223 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001224 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * If the longest data block freespace changes, need to update
1227 * the corresponding freeblock entry.
1228 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001229 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001231 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 xfs_dir2_db_t fdb; /* freeblock block number */
1233 int findex; /* index in freeblock entries */
1234 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
1237 * Convert the data block number to a free block,
1238 * read in the free block.
1239 */
Dave Chinner8f661932014-06-06 15:15:59 +10001240 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001241 error = xfs_dir2_free_read(tp, dp,
1242 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001243 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001244 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001246 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001247#ifdef DEBUG
1248 {
1249 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001250 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001251 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001252 (fdb - xfs_dir2_byte_to_db(args->geo,
1253 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001254 }
1255#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /*
1257 * Calculate which entry we need to fix.
1258 */
Dave Chinner8f661932014-06-06 15:15:59 +10001259 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001260 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * If the data block is now empty we can get rid of it
1263 * (usually).
1264 */
Dave Chinner8f661932014-06-06 15:15:59 +10001265 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001266 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * Try to punch out the data block.
1269 */
1270 error = xfs_dir2_shrink_inode(args, db, dbp);
1271 if (error == 0) {
1272 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001273 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 /*
1276 * We can get ENOSPC if there's no space reservation.
1277 * In this case just drop the buffer and some one else
1278 * will eventually get rid of the empty block.
1279 */
Dave Chinner24513372014-06-25 14:58:08 +10001280 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return error;
1282 }
1283 /*
1284 * If we got rid of the data block, we can eliminate that entry
1285 * in the free block.
1286 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001287 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001288 fdb, findex, fbp, longest);
1289 if (error)
1290 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Dave Chinner20252072012-11-12 22:54:13 +11001292
Dave Chinner41419562013-10-29 22:11:50 +11001293 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001295 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * to justify trying to join it with a neighbor.
1297 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001298 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001299 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001300 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302}
1303
1304/*
1305 * Split the leaf entries in the old block into old and new blocks.
1306 */
1307int /* error */
1308xfs_dir2_leafn_split(
1309 xfs_da_state_t *state, /* btree cursor */
1310 xfs_da_state_blk_t *oldblk, /* original block */
1311 xfs_da_state_blk_t *newblk) /* newly created block */
1312{
1313 xfs_da_args_t *args; /* operation arguments */
1314 xfs_dablk_t blkno; /* new leaf block number */
1315 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001316 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /*
1319 * Allocate space for a new leaf node.
1320 */
1321 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001322 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1324 error = xfs_da_grow_inode(args, &blkno);
1325 if (error) {
1326 return error;
1327 }
1328 /*
1329 * Initialize the new leaf block.
1330 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001331 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001332 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1333 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 newblk->blkno = blkno;
1337 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1338 /*
1339 * Rebalance the entries across the two leaves, link the new
1340 * block into the leaves.
1341 */
1342 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001343 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (error) {
1345 return error;
1346 }
1347 /*
1348 * Insert the new entry in the correct block.
1349 */
1350 if (state->inleaf)
1351 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1352 else
1353 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1354 /*
1355 * Update last hashval in each block since we added the name.
1356 */
Dave Chinner41419562013-10-29 22:11:50 +11001357 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1358 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1359 xfs_dir3_leaf_check(dp, oldblk->bp);
1360 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return error;
1362}
1363
1364/*
1365 * Check a leaf block and its neighbors to see if the block should be
1366 * collapsed into one or the other neighbor. Always keep the block
1367 * with the smaller block number.
1368 * If the current block is over 50% full, don't try to join it, return 0.
1369 * If the block is empty, fill in the state structure and return 2.
1370 * If it can be collapsed, fill in the state structure and return 1.
1371 * If nothing can be done, return 0.
1372 */
1373int /* error */
1374xfs_dir2_leafn_toosmall(
1375 xfs_da_state_t *state, /* btree cursor */
1376 int *action) /* resulting action to take */
1377{
1378 xfs_da_state_blk_t *blk; /* leaf block */
1379 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001380 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int bytes; /* bytes in use */
1382 int count; /* leaf live entry count */
1383 int error; /* error return value */
1384 int forward; /* sibling block direction */
1385 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 xfs_dir2_leaf_t *leaf; /* leaf structure */
1387 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001388 struct xfs_dir3_icleaf_hdr leafhdr;
1389 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001390 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
1393 * Check for the degenerate case of the block being over 50% full.
1394 * If so, it's not worth even looking to see if we might be able
1395 * to coalesce with a sibling.
1396 */
1397 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001398 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001399 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001400 ents = dp->d_ops->leaf_ents_p(leaf);
1401 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001402
1403 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001404 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001405 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /*
1407 * Blk over 50%, don't try to join.
1408 */
1409 *action = 0;
1410 return 0;
1411 }
1412 /*
1413 * Check for the degenerate case of the block being empty.
1414 * If the block is empty, we'll simply delete it, no need to
1415 * coalesce it with a sibling block. We choose (arbitrarily)
1416 * to merge with the forward block unless it is NULL.
1417 */
1418 if (count == 0) {
1419 /*
1420 * Make altpath point to the block we want to keep and
1421 * path point to the block we want to drop (this one).
1422 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001423 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001425 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 &rval);
1427 if (error)
1428 return error;
1429 *action = rval ? 2 : 0;
1430 return 0;
1431 }
1432 /*
1433 * Examine each sibling block to see if we can coalesce with
1434 * at least 25% free space to spare. We need to figure out
1435 * whether to merge with the forward or the backward block.
1436 * We prefer coalescing with the lower numbered sibling so as
1437 * to shrink a directory over time.
1438 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001439 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001441 struct xfs_dir3_icleaf_hdr hdr2;
1442
1443 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (blkno == 0)
1445 continue;
1446 /*
1447 * Read the sibling leaf block.
1448 */
Dave Chinner41419562013-10-29 22:11:50 +11001449 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001450 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001451 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /*
1455 * Count bytes in the two blocks combined.
1456 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001457 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001458 bytes = state->args->geo->blksize -
1459 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001460
Dave Chinner1d9025e2012-06-22 18:50:14 +10001461 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001462 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001463 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001464 count += hdr2.count - hdr2.stale;
1465 bytes -= count * sizeof(ents[0]);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 /*
1468 * Fits with at least 25% to spare.
1469 */
1470 if (bytes >= 0)
1471 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001472 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 /*
1475 * Didn't like either block, give up.
1476 */
1477 if (i >= 2) {
1478 *action = 0;
1479 return 0;
1480 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /*
1483 * Make altpath point to the block we want to keep (the lower
1484 * numbered block) and path point to the block we want to drop.
1485 */
1486 memcpy(&state->altpath, &state->path, sizeof(state->path));
1487 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001488 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 &rval);
1490 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001491 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 &rval);
1493 if (error) {
1494 return error;
1495 }
1496 *action = rval ? 0 : 1;
1497 return 0;
1498}
1499
1500/*
1501 * Move all the leaf entries from drop_blk to save_blk.
1502 * This is done as part of a join operation.
1503 */
1504void
1505xfs_dir2_leafn_unbalance(
1506 xfs_da_state_t *state, /* cursor */
1507 xfs_da_state_blk_t *drop_blk, /* dead block */
1508 xfs_da_state_blk_t *save_blk) /* surviving block */
1509{
1510 xfs_da_args_t *args; /* operation arguments */
1511 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1512 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001513 struct xfs_dir3_icleaf_hdr savehdr;
1514 struct xfs_dir3_icleaf_hdr drophdr;
1515 struct xfs_dir2_leaf_entry *sents;
1516 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001517 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 args = state->args;
1520 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1521 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001522 drop_leaf = drop_blk->bp->b_addr;
1523 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001524
Dave Chinner01ba43b2013-10-29 22:11:52 +11001525 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1526 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1527 sents = dp->d_ops->leaf_ents_p(save_leaf);
1528 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * If there are any stale leaf entries, take this opportunity
1532 * to purge them.
1533 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001534 if (drophdr.stale)
1535 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1536 if (savehdr.stale)
1537 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /*
1540 * Move the entries from drop to the appropriate end of save.
1541 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001542 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001543 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001544 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1545 save_blk->bp, &savehdr, sents, 0,
1546 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 else
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,
1550 savehdr.count, drophdr.count);
1551 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1552
1553 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001554 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1555 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001556 xfs_dir3_leaf_log_header(args, save_blk->bp);
1557 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001558
Dave Chinner41419562013-10-29 22:11:50 +11001559 xfs_dir3_leaf_check(dp, save_blk->bp);
1560 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563/*
1564 * Top-level node form directory addname routine.
1565 */
1566int /* error */
1567xfs_dir2_node_addname(
1568 xfs_da_args_t *args) /* operation arguments */
1569{
1570 xfs_da_state_blk_t *blk; /* leaf block for insert */
1571 int error; /* error return value */
1572 int rval; /* sub-return value */
1573 xfs_da_state_t *state; /* btree cursor */
1574
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001575 trace_xfs_dir2_node_addname(args);
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /*
1578 * Allocate and initialize the state (btree cursor).
1579 */
1580 state = xfs_da_state_alloc();
1581 state->args = args;
1582 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /*
1584 * Look up the name. We're not supposed to find it, but
1585 * this gives us the insertion point.
1586 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001587 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (error)
1589 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001590 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto done;
1592 }
1593 /*
1594 * Add the data entry to a data block.
1595 * Extravalid is set to a freeblock found by lookup.
1596 */
1597 rval = xfs_dir2_node_addname_int(args,
1598 state->extravalid ? &state->extrablk : NULL);
1599 if (rval) {
1600 goto done;
1601 }
1602 blk = &state->path.blk[state->path.active - 1];
1603 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1604 /*
1605 * Add the new leaf entry.
1606 */
1607 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1608 if (rval == 0) {
1609 /*
1610 * It worked, fix the hash values up the btree.
1611 */
Barry Naujok6a178102008-05-21 16:42:05 +10001612 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001613 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 } else {
1615 /*
1616 * It didn't work, we need to split the leaf block.
1617 */
1618 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001619 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto done;
1621 }
1622 /*
1623 * Split the leaf block and insert the new entry.
1624 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001625 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627done:
1628 xfs_da_state_free(state);
1629 return rval;
1630}
1631
1632/*
1633 * Add the data entry for a node-format directory name addition.
1634 * The leaf entry is added in xfs_dir2_leafn_add.
1635 * We may enter with a freespace block that the lookup found.
1636 */
1637static int /* error */
1638xfs_dir2_node_addname_int(
1639 xfs_da_args_t *args, /* operation arguments */
1640 xfs_da_state_blk_t *fblk) /* optional freespace block */
1641{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001642 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001644 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1646 xfs_inode_t *dp; /* incore directory inode */
1647 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1648 int error; /* error return value */
1649 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001650 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int findex; /* freespace entry index */
1652 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1653 xfs_dir2_db_t ifbno; /* initial freespace block no */
1654 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1655 int length; /* length of the new entry */
1656 int logfree; /* need to log free entry */
1657 xfs_mount_t *mp; /* filesystem mount point */
1658 int needlog; /* need to log data header */
1659 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001660 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001662 __be16 *bests;
1663 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001664 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 dp = args->dp;
1667 mp = dp->i_mount;
1668 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001669 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * If we came in with a freespace block that means that lookup
1672 * found an entry with our hash value. This is the freespace
1673 * block for that data entry.
1674 */
1675 if (fblk) {
1676 fbp = fblk->bp;
1677 /*
1678 * Remember initial freespace block number.
1679 */
1680 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001681 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001683 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001684 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * This means the free entry showed that the data block had
1688 * space for our entry, so we remembered it.
1689 * Use that data block.
1690 */
1691 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001692 ASSERT(findex < freehdr.nvalid);
1693 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1694 ASSERT(be16_to_cpu(bests[findex]) >= length);
1695 dbno = freehdr.firstdb + findex;
1696 } else {
1697 /*
1698 * The data block looked at didn't have enough room.
1699 * We'll start at the beginning of the freespace entries.
1700 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 dbno = -1;
1702 findex = 0;
1703 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001704 } else {
1705 /*
1706 * Didn't come in with a freespace block, so no data block.
1707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 ifbno = dbno = -1;
1709 fbp = NULL;
1710 findex = 0;
1711 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
1714 * If we don't have a data block yet, we're going to scan the
1715 * freespace blocks looking for one. Figure out what the
1716 * highest freespace block number is.
1717 */
1718 if (dbno == -1) {
1719 xfs_fileoff_t fo; /* freespace block number */
1720
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001721 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001723 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 fbno = ifbno;
1725 }
1726 /*
1727 * While we haven't identified a data block, search the freeblock
1728 * data for a good data block. If we find a null freeblock entry,
1729 * indicating a hole in the data blocks, remember that.
1730 */
1731 while (dbno == -1) {
1732 /*
1733 * If we don't have a freeblock in hand, get the next one.
1734 */
1735 if (fbp == NULL) {
1736 /*
1737 * Happens the first time through unless lookup gave
1738 * us a freespace block to start with.
1739 */
1740 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001741 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001742 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * If it's ifbno we already looked at it.
1745 */
1746 if (fbno == ifbno)
1747 fbno++;
1748 /*
1749 * If it's off the end we're done.
1750 */
1751 if (fbno >= lastfbno)
1752 break;
1753 /*
1754 * Read the block. There can be holes in the
1755 * freespace blocks, so this might not succeed.
1756 * This should be really rare, so there's no reason
1757 * to avoid it.
1758 */
Dave Chinner20252072012-11-12 22:54:13 +11001759 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001760 xfs_dir2_db_to_da(args->geo, fbno),
1761 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001762 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001764 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001766 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 findex = 0;
1768 }
1769 /*
1770 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001771 *
1772 * The bests initialisation should be where the bufer is read in
1773 * the above branch. But gcc is too stupid to realise that bests
1774 * and the freehdr are actually initialised if they are placed
1775 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001777 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001778 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001779 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1780 be16_to_cpu(bests[findex]) >= length)
1781 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 else {
1783 /*
1784 * Are we done with the freeblock?
1785 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001786 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * Drop the block.
1789 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001790 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 fbp = NULL;
1792 if (fblk && fblk->bp)
1793 fblk->bp = NULL;
1794 }
1795 }
1796 }
1797 /*
1798 * If we don't have a data block, we need to allocate one and make
1799 * the freespace entries refer to it.
1800 */
1801 if (unlikely(dbno == -1)) {
1802 /*
1803 * Not allowed to allocate, return failure.
1804 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001805 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001806 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /*
1809 * Allocate and initialize the new data block.
1810 */
1811 if (unlikely((error = xfs_dir2_grow_inode(args,
1812 XFS_DIR2_DATA_SPACE,
1813 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001814 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /*
1818 * If (somehow) we have a freespace block, get rid of it.
1819 */
1820 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001821 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (fblk && fblk->bp)
1823 fblk->bp = NULL;
1824
1825 /*
1826 * Get the freespace block corresponding to the data block
1827 * that was just allocated.
1828 */
Dave Chinner8f661932014-06-06 15:15:59 +10001829 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001830 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001831 xfs_dir2_db_to_da(args->geo, fbno),
1832 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 /*
1837 * If there wasn't a freespace block, the read will
1838 * return a NULL fbp. Allocate and initialize a new one.
1839 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001840 if (!fbp) {
1841 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1842 &fbno);
1843 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Dave Chinner8f661932014-06-06 15:15:59 +10001846 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001847 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001848 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001849 " data block %lld, got %lld ifbno %llu lastfbno %d",
1850 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001851 (long long)dp->d_ops->db_to_fdb(
1852 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 (long long)dbno, (long long)fbno,
1854 (unsigned long long)ifbno, lastfbno);
1855 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001856 xfs_alert(mp,
1857 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 fblk,
1859 (unsigned long long)fblk->blkno,
1860 fblk->index,
1861 fblk->magic);
1862 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001863 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1866 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001867 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
1870 /*
1871 * Get a buffer for the new block.
1872 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001873 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001876 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001877 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001878 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001881 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001883 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001884 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001885 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001886 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001888 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001889 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001890 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892
1893 /*
1894 * Set the freespace block index from the data block number.
1895 */
Dave Chinner8f661932014-06-06 15:15:59 +10001896 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * If it's after the end of the current entries in the
1899 * freespace block, extend that table.
1900 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001901 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001902 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001903 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /*
1905 * Tag new entry so nused will go up.
1906 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001907 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909 /*
1910 * If this entry was for an empty data block
1911 * (this should always be true) then update the header.
1912 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001913 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1914 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001915 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001916 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 }
1918 /*
1919 * Update the real value in the table.
1920 * We haven't allocated the data entry yet so this will
1921 * change again.
1922 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001923 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001924 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001925 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 logfree = 1;
1927 }
1928 /*
1929 * We had a data block so we don't have to make a new one.
1930 */
1931 else {
1932 /*
1933 * If just checking, we succeeded.
1934 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001935 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * Read the data block in.
1940 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001941 error = xfs_dir3_data_read(tp, dp,
1942 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001943 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001944 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001946 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001947 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 logfree = 0;
1949 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001950 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Point to the existing unused space.
1953 */
1954 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001955 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 needscan = needlog = 0;
1957 /*
1958 * Mark the first part of the unused space, inuse for us.
1959 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001960 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001961 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 &needlog, &needscan);
1963 /*
1964 * Fill in the new entry and log it.
1965 */
1966 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001967 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 dep->namelen = args->namelen;
1969 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001970 dp->d_ops->data_put_ftype(dep, args->filetype);
1971 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001972 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001973 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Rescan the block for bestfree if needed.
1976 */
1977 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001978 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /*
1980 * Log the data block header if needed.
1981 */
1982 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001983 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /*
1985 * If the freespace entry is now wrong, update it.
1986 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001987 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11001988 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1989 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 logfree = 1;
1991 }
1992 /*
1993 * Log the freespace entry if needed.
1994 */
1995 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001996 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * Return the data block and offset in args, then drop the data block.
1999 */
2000 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002001 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
2003}
2004
2005/*
2006 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002007 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 * The only real output is the inode number of the entry.
2009 */
2010int /* error */
2011xfs_dir2_node_lookup(
2012 xfs_da_args_t *args) /* operation arguments */
2013{
2014 int error; /* error return value */
2015 int i; /* btree level */
2016 int rval; /* operation return value */
2017 xfs_da_state_t *state; /* btree cursor */
2018
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002019 trace_xfs_dir2_node_lookup(args);
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /*
2022 * Allocate and initialize the btree cursor.
2023 */
2024 state = xfs_da_state_alloc();
2025 state->args = args;
2026 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /*
2028 * Fill in the path to the entry in the cursor.
2029 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002030 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (error)
2032 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002033 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2034 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002035 xfs_dir2_data_entry_t *dep;
2036
Dave Chinner1d9025e2012-06-22 18:50:14 +10002037 dep = (xfs_dir2_data_entry_t *)
2038 ((char *)state->extrablk.bp->b_addr +
2039 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002040 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Release the btree blocks and leaf block.
2044 */
2045 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002046 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 state->path.blk[i].bp = NULL;
2048 }
2049 /*
2050 * Release the data block if we have it.
2051 */
2052 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002053 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 state->extrablk.bp = NULL;
2055 }
2056 xfs_da_state_free(state);
2057 return rval;
2058}
2059
2060/*
2061 * Remove an entry from a node-format directory.
2062 */
2063int /* error */
2064xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002065 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002067 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int error; /* error return value */
2069 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002070 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002072 trace_xfs_dir2_node_removename(args);
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
2075 * Allocate and initialize the btree cursor.
2076 */
2077 state = xfs_da_state_alloc();
2078 state->args = args;
2079 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002080
2081 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002082 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002083 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002084 goto out_free;
2085
2086 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002087 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002088 error = rval;
2089 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 blk = &state->path.blk[state->path.active - 1];
2093 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2094 ASSERT(state->extravalid);
2095 /*
2096 * Remove the leaf and data entries.
2097 * Extrablk refers to the data block.
2098 */
2099 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2100 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002101 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002102 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Fix the hash values up the btree.
2105 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002106 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * If we need to join leaf blocks, do it.
2109 */
2110 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002111 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /*
2113 * If no errors so far, try conversion to leaf format.
2114 */
2115 if (!error)
2116 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002117out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 xfs_da_state_free(state);
2119 return error;
2120}
2121
2122/*
2123 * Replace an entry's inode number in a node-format directory.
2124 */
2125int /* error */
2126xfs_dir2_node_replace(
2127 xfs_da_args_t *args) /* operation arguments */
2128{
2129 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002130 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 xfs_dir2_data_entry_t *dep; /* data entry changed */
2132 int error; /* error return value */
2133 int i; /* btree level */
2134 xfs_ino_t inum; /* new inode number */
2135 xfs_dir2_leaf_t *leaf; /* leaf structure */
2136 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2137 int rval; /* internal return value */
2138 xfs_da_state_t *state; /* btree cursor */
2139
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002140 trace_xfs_dir2_node_replace(args);
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Allocate and initialize the btree cursor.
2144 */
2145 state = xfs_da_state_alloc();
2146 state->args = args;
2147 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 inum = args->inumber;
2149 /*
2150 * Lookup the entry to change in the btree.
2151 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002152 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (error) {
2154 rval = error;
2155 }
2156 /*
2157 * It should be found, since the vnodeops layer has looked it up
2158 * and locked it. But paranoia is good.
2159 */
Dave Chinner24513372014-06-25 14:58:08 +10002160 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002161 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /*
2163 * Find the leaf entry.
2164 */
2165 blk = &state->path.blk[state->path.active - 1];
2166 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002167 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002168 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002169 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 ASSERT(state->extravalid);
2171 /*
2172 * Point to the data entry.
2173 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002174 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002175 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2176 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002178 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002179 xfs_dir2_dataptr_to_off(args->geo,
2180 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002181 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * Fill in the new inode number and log the entry.
2184 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002185 dep->inumber = cpu_to_be64(inum);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002186 args->dp->d_ops->data_put_ftype(dep, args->filetype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002187 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rval = 0;
2189 }
2190 /*
2191 * Didn't find it, and we're holding a data block. Drop it.
2192 */
2193 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002194 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 state->extrablk.bp = NULL;
2196 }
2197 /*
2198 * Release all the buffers in the cursor.
2199 */
2200 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002201 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 state->path.blk[i].bp = NULL;
2203 }
2204 xfs_da_state_free(state);
2205 return rval;
2206}
2207
2208/*
2209 * Trim off a trailing empty freespace block.
2210 * Return (in rvalp) 1 if we did it, 0 if not.
2211 */
2212int /* error */
2213xfs_dir2_node_trim_free(
2214 xfs_da_args_t *args, /* operation arguments */
2215 xfs_fileoff_t fo, /* free block number */
2216 int *rvalp) /* out: did something */
2217{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002218 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 xfs_inode_t *dp; /* incore directory inode */
2220 int error; /* error return code */
2221 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002223 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 tp = args->trans;
2227 /*
2228 * Read the freespace block.
2229 */
Dave Chinner20252072012-11-12 22:54:13 +11002230 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002231 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /*
2234 * There can be holes in freespace. If fo is a hole, there's
2235 * nothing to do.
2236 */
Dave Chinner20252072012-11-12 22:54:13 +11002237 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002239 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002240 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /*
2243 * If there are used entries, there's nothing to do.
2244 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002245 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002246 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 *rvalp = 0;
2248 return 0;
2249 }
2250 /*
2251 * Blow the block away.
2252 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002253 error = xfs_dir2_shrink_inode(args,
2254 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2255 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 /*
2257 * Can't fail with ENOSPC since that only happens with no
2258 * space reservation, when breaking up an extent into two
2259 * pieces. This is the last block of an extent.
2260 */
Dave Chinner24513372014-06-25 14:58:08 +10002261 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002262 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return error;
2264 }
2265 /*
2266 * Return that we succeeded.
2267 */
2268 *rvalp = 1;
2269 return 0;
2270}