blob: c0a0e194fbf77cc16997a4cbf8a38f5e248e6ebb [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_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110026#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_inode.h"
29#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000033#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110034#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110035#include "xfs_buf_item.h"
36#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Function declarations.
40 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100041static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 xfs_da_state_blk_t *blk1,
45 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int index, xfs_da_state_blk_t *dblk,
48 int *rval);
49static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 xfs_da_state_blk_t *fblk);
51
Dave Chinner24df33b2013-04-12 07:30:21 +100052/*
53 * Check internal consistency of a leafn block.
54 */
55#ifdef DEBUG
Dave Chinner41419562013-10-29 22:11:50 +110056#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100057do { \
Dave Chinner41419562013-10-29 22:11:50 +110058 if (!xfs_dir3_leafn_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100059 ASSERT(0); \
60} while (0);
61
62static bool
63xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110064 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100065 struct xfs_buf *bp)
66{
67 struct xfs_dir2_leaf *leaf = bp->b_addr;
68 struct xfs_dir3_icleaf_hdr leafhdr;
69
Dave Chinner01ba43b2013-10-29 22:11:52 +110070 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100071
72 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
73 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
74 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
75 return false;
76 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
77 return false;
78
Dave Chinner41419562013-10-29 22:11:50 +110079 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100080}
81#else
Dave Chinner41419562013-10-29 22:11:50 +110082#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100083#endif
84
Dave Chinnercbc8adf2013-04-03 16:11:21 +110085static bool
86xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110087 struct xfs_buf *bp)
88{
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110091
Dave Chinnercbc8adf2013-04-03 16:11:21 +110092 if (xfs_sb_version_hascrc(&mp->m_sb)) {
93 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94
95 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
96 return false;
97 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
98 return false;
99 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
100 return false;
101 } else {
102 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
103 return false;
104 }
105
106 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
107
108 return true;
109}
110
111static void
112xfs_dir3_free_read_verify(
113 struct xfs_buf *bp)
114{
115 struct xfs_mount *mp = bp->b_target->bt_mount;
116
Eric Sandeence5028c2014-02-27 15:23:10 +1100117 if (xfs_sb_version_hascrc(&mp->m_sb) &&
118 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000119 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100120 else if (!xfs_dir3_free_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000121 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100122
123 if (bp->b_error)
124 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100125}
Dave Chinner20252072012-11-12 22:54:13 +1100126
Dave Chinner612cfbf2012-11-14 17:52:32 +1100127static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100129 struct xfs_buf *bp)
130{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100131 struct xfs_mount *mp = bp->b_target->bt_mount;
132 struct xfs_buf_log_item *bip = bp->b_fspriv;
133 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
134
135 if (!xfs_dir3_free_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000136 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100137 xfs_verifier_error(bp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100138 return;
139 }
140
141 if (!xfs_sb_version_hascrc(&mp->m_sb))
142 return;
143
144 if (bip)
145 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
146
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100147 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100148}
149
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100150const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100151 .verify_read = xfs_dir3_free_read_verify,
152 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100153};
Dave Chinner20252072012-11-12 22:54:13 +1100154
Dave Chinner612cfbf2012-11-14 17:52:32 +1100155
Dave Chinner20252072012-11-12 22:54:13 +1100156static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100157__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100158 struct xfs_trans *tp,
159 struct xfs_inode *dp,
160 xfs_dablk_t fbno,
161 xfs_daddr_t mappedbno,
162 struct xfs_buf **bpp)
163{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100164 int err;
165
166 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100167 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100168
169 /* try read returns without an error or *bpp if it lands in a hole */
170 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100171 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100172 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100173}
174
175int
176xfs_dir2_free_read(
177 struct xfs_trans *tp,
178 struct xfs_inode *dp,
179 xfs_dablk_t fbno,
180 struct xfs_buf **bpp)
181{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100182 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100183}
184
185static int
186xfs_dir2_free_try_read(
187 struct xfs_trans *tp,
188 struct xfs_inode *dp,
189 xfs_dablk_t fbno,
190 struct xfs_buf **bpp)
191{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100192 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
193}
194
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100195static int
196xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000197 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100198 xfs_dir2_db_t fbno,
199 struct xfs_buf **bpp)
200{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000201 struct xfs_trans *tp = args->trans;
202 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100203 struct xfs_mount *mp = dp->i_mount;
204 struct xfs_buf *bp;
205 int error;
206 struct xfs_dir3_icfree_hdr hdr;
207
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000208 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100209 -1, &bp, XFS_DATA_FORK);
210 if (error)
211 return error;
212
Dave Chinner61fe1352013-04-03 16:11:30 +1100213 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100214 bp->b_ops = &xfs_dir3_free_buf_ops;
215
216 /*
217 * Initialize the new block to be empty, and remember
218 * its first slot as our empty slot.
219 */
Dave Chinnere400d272013-05-28 18:37:17 +1000220 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
221 memset(&hdr, 0, sizeof(hdr));
222
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100223 if (xfs_sb_version_hascrc(&mp->m_sb)) {
224 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
225
226 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000227
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100228 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
229 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
230 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000231 } else
232 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100233 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100234 *bpp = bp;
235 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Log entries from a freespace block.
240 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000241STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000243 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000244 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int first, /* first entry to log */
246 int last) /* last entry to log */
247{
248 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100249 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Dave Chinner1d9025e2012-06-22 18:50:14 +1000251 free = bp->b_addr;
Dave Chinnerbc851782014-06-06 15:20:54 +1000252 bests = args->dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100253 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
254 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinnerbc851782014-06-06 15:20:54 +1000255 xfs_trans_log_buf(args->trans, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100256 (uint)((char *)&bests[first] - (char *)free),
257 (uint)((char *)&bests[last] - (char *)free +
258 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261/*
262 * Log header from a freespace block.
263 */
264static void
265xfs_dir2_free_log_header(
Dave Chinnerbc851782014-06-06 15:20:54 +1000266 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000267 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Dave Chinner836a94a2013-08-12 20:49:44 +1000269#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 xfs_dir2_free_t *free; /* freespace structure */
271
Dave Chinner1d9025e2012-06-22 18:50:14 +1000272 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100273 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
274 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000275#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000276 xfs_trans_log_buf(args->trans, bp, 0,
277 args->dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Convert a leaf-format directory to a node-format directory.
282 * We need to change the magic number of the leaf block, and copy
283 * the freespace table out of the leaf block into its own block.
284 */
285int /* error */
286xfs_dir2_leaf_to_node(
287 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000288 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 xfs_inode_t *dp; /* incore directory inode */
291 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000292 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 xfs_dir2_db_t fdb; /* freespace block number */
294 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100295 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int i; /* leaf freespace index */
297 xfs_dir2_leaf_t *leaf; /* leaf structure */
298 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
299 xfs_mount_t *mp; /* filesystem mount point */
300 int n; /* count of live freespc ents */
301 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100302 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100304 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000306 trace_xfs_dir2_leaf_to_node(args);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 dp = args->dp;
309 mp = dp->i_mount;
310 tp = args->trans;
311 /*
312 * Add a freespace block to the directory.
313 */
314 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
315 return error;
316 }
Dave Chinner30028032014-06-06 15:08:18 +1000317 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Get the buffer for the new freespace block.
320 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000321 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100322 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100324
Dave Chinner1d9025e2012-06-22 18:50:14 +1000325 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100326 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000327 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000328 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100329 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000330 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * Copy freespace entries from the leaf block to the new block.
334 * Count active entries.
335 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100336 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500337 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100338 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100339 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100341 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100345 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100347 freehdr.nused = n;
348 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
349
Dave Chinner01ba43b2013-10-29 22:11:52 +1100350 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000351 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
352 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100353
Dave Chinner24df33b2013-04-12 07:30:21 +1000354 /*
355 * Converting the leaf to a leafnode is just a matter of changing the
356 * magic number and the ops. Do the change directly to the buffer as
357 * it's less work (and less code) than decoding the header to host
358 * format and back again.
359 */
360 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
361 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
362 else
363 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
364 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100365 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000366 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100367 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371/*
372 * Add a leaf entry to a leaf block in a node-form directory.
373 * The other work necessary is done from the caller.
374 */
375static int /* error */
376xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000377 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 xfs_da_args_t *args, /* operation arguments */
379 int index) /* insertion pt for new entry */
380{
381 int compact; /* compacting stale leaves */
382 xfs_inode_t *dp; /* incore directory inode */
383 int highstale; /* next stale entry */
384 xfs_dir2_leaf_t *leaf; /* leaf structure */
385 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
386 int lfloghigh; /* high leaf entry logging */
387 int lfloglow; /* low leaf entry logging */
388 int lowstale; /* previous stale entry */
389 xfs_mount_t *mp; /* filesystem mount point */
390 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000391 struct xfs_dir3_icleaf_hdr leafhdr;
392 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000394 trace_xfs_dir2_leafn_add(args, index);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 dp = args->dp;
397 mp = dp->i_mount;
398 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000399 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100400 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100401 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * Quick check just to make sure we are not going to index
405 * into other peoples memory
406 */
407 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000408 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /*
411 * If there are already the maximum number of leaf entries in
412 * the block, if there are no stale entries it won't fit.
413 * Caller will do a split. If there are stale entries we'll do
414 * a compact.
415 */
416
Dave Chinner8f661932014-06-06 15:15:59 +1000417 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000418 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000419 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000420 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 } else
422 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000423 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
424 ASSERT(index == leafhdr.count ||
425 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Barry Naujok6a178102008-05-21 16:42:05 +1000427 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return 0;
429
430 /*
431 * Compact out all but one stale leaf entry. Leaves behind
432 * the entry closest to index.
433 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000434 if (compact)
435 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
436 &highstale, &lfloglow, &lfloghigh);
437 else if (leafhdr.stale) {
438 /*
439 * Set impossible logging indices for this case.
440 */
441 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 lfloghigh = -1;
443 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 /*
446 * Insert the new entry, log everything.
447 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000448 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200449 highstale, &lfloglow, &lfloghigh);
450
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100451 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000452 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100453 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000454
Dave Chinner01ba43b2013-10-29 22:11:52 +1100455 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000456 xfs_dir3_leaf_log_header(args, bp);
457 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100458 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460}
461
462#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100463static void
464xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100465 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100466 struct xfs_buf *bp,
467 xfs_dir2_db_t db)
468{
469 struct xfs_dir3_icfree_hdr hdr;
470
Dave Chinner01ba43b2013-10-29 22:11:52 +1100471 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100472
Dave Chinner8f661932014-06-06 15:15:59 +1000473 ASSERT((hdr.firstdb %
474 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100475 ASSERT(hdr.firstdb <= db);
476 ASSERT(db < hdr.firstdb + hdr.nvalid);
477}
478#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100479#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#endif /* DEBUG */
481
482/*
483 * Return the last hash value in the leaf.
484 * Stale entries are ok.
485 */
486xfs_dahash_t /* hash value */
487xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100488 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000489 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int *count) /* count of entries in leaf */
491{
Dave Chinner24df33b2013-04-12 07:30:21 +1000492 struct xfs_dir2_leaf *leaf = bp->b_addr;
493 struct xfs_dir2_leaf_entry *ents;
494 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dave Chinner01ba43b2013-10-29 22:11:52 +1100496 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000497
498 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
499 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000502 *count = leafhdr.count;
503 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000505
Dave Chinner41419562013-10-29 22:11:50 +1100506 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000507 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000511 * Look up a leaf entry for space to add a name in a node-format leaf block.
512 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000514STATIC int
515xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000516 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 xfs_da_args_t *args, /* operation arguments */
518 int *indexp, /* out: leaf entry index */
519 xfs_da_state_t *state) /* state to fill in */
520{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000521 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000522 xfs_dir2_db_t curdb = -1; /* current data block number */
523 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 xfs_inode_t *dp; /* incore directory inode */
525 int error; /* error return value */
526 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000527 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int index; /* leaf entry index */
529 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000530 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
532 xfs_mount_t *mp; /* filesystem mount point */
533 xfs_dir2_db_t newdb; /* new data block number */
534 xfs_dir2_db_t newfdb; /* new free block number */
535 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000536 struct xfs_dir2_leaf_entry *ents;
537 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 dp = args->dp;
540 tp = args->trans;
541 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000542 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100543 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100544 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000545
Dave Chinner41419562013-10-29 22:11:50 +1100546 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000547 ASSERT(leafhdr.count > 0);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * Look up the hash value in the leaf entries.
551 */
552 index = xfs_dir2_leaf_search_hash(args, bp);
553 /*
554 * Do we have a buffer coming in?
555 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000556 if (state->extravalid) {
557 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000559 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000560 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100561 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
562 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100564 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * Loop over leaf entries with the right hash value.
567 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000568 for (lep = &ents[index];
569 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
570 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
572 * Skip stale leaf entries.
573 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100574 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 continue;
576 /*
577 * Pull the data block number from the entry.
578 */
Dave Chinner30028032014-06-06 15:08:18 +1000579 newdb = xfs_dir2_dataptr_to_db(args->geo,
580 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * For addname, we're looking for a place to put the new entry.
583 * We want to use a data block with an entry of equal
584 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000585 *
586 * If this block isn't the data block we already have
587 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000589 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100590 __be16 *bests;
591
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000594 * Convert the data block to the free block
595 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Dave Chinner8f661932014-06-06 15:15:59 +1000597 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000603 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000606 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100607
608 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000609 xfs_dir2_db_to_da(args->geo,
610 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100611 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000614 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100615
Dave Chinner01ba43b2013-10-29 22:11:52 +1100616 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000619 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Dave Chinner8f661932014-06-06 15:15:59 +1000621 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000623 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500625 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100626 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
628 XFS_ERRLEVEL_LOW, mp);
629 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000630 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000631 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000633 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100634 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000638 /* Didn't find any space */
639 fi = -1;
640out:
Barry Naujok6a178102008-05-21 16:42:05 +1000641 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 if (curbp) {
643 /* Giving back a free block. */
644 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 state->extrablk.index = fi;
647 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100648
649 /*
650 * Important: this magic number is not in the buffer - it's for
651 * buffer type information and therefore only the free/data type
652 * matters here, not whether CRCs are enabled or not.
653 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000654 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
655 } else {
656 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
661 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000662 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000666 * Look up a leaf entry in a node-format leaf block.
667 * The extrablk in state a data block.
668 */
669STATIC int
670xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000672 xfs_da_args_t *args, /* operation arguments */
673 int *indexp, /* out: leaf entry index */
674 xfs_da_state_t *state) /* state to fill in */
675{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000676 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 xfs_dir2_db_t curdb = -1; /* current data block number */
678 xfs_dir2_data_entry_t *dep; /* data block entry */
679 xfs_inode_t *dp; /* incore directory inode */
680 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000681 int index; /* leaf entry index */
682 xfs_dir2_leaf_t *leaf; /* leaf structure */
683 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
684 xfs_mount_t *mp; /* filesystem mount point */
685 xfs_dir2_db_t newdb; /* new data block number */
686 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000687 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000688 struct xfs_dir2_leaf_entry *ents;
689 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000690
691 dp = args->dp;
692 tp = args->trans;
693 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100695 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100696 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000697
Dave Chinner41419562013-10-29 22:11:50 +1100698 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000699 ASSERT(leafhdr.count > 0);
700
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000701 /*
702 * Look up the hash value in the leaf entries.
703 */
704 index = xfs_dir2_leaf_search_hash(args, bp);
705 /*
706 * Do we have a buffer coming in?
707 */
708 if (state->extravalid) {
709 curbp = state->extrablk.bp;
710 curdb = state->extrablk.blkno;
711 }
712 /*
713 * Loop over leaf entries with the right hash value.
714 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000715 for (lep = &ents[index];
716 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
717 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000718 /*
719 * Skip stale leaf entries.
720 */
721 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
722 continue;
723 /*
724 * Pull the data block number from the entry.
725 */
Dave Chinner30028032014-06-06 15:08:18 +1000726 newdb = xfs_dir2_dataptr_to_db(args->geo,
727 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000728 /*
729 * Not adding a new entry, so we really want to find
730 * the name given to us.
731 *
732 * If it's a different data block, go get it.
733 */
734 if (newdb != curdb) {
735 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000736 * If we had a block before that we aren't saving
737 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000738 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000739 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
740 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000741 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000742 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000743 * If needing the block that is saved with a CI match,
744 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000745 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000746 if (args->cmpresult != XFS_CMP_DIFFERENT &&
747 newdb == state->extrablk.blkno) {
748 ASSERT(state->extravalid);
749 curbp = state->extrablk.bp;
750 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100751 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000752 xfs_dir2_db_to_da(args->geo,
753 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100754 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000755 if (error)
756 return error;
757 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100758 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000759 curdb = newdb;
760 }
761 /*
762 * Point to the data entry.
763 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000764 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000765 xfs_dir2_dataptr_to_off(args->geo,
766 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000767 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000768 * Compare the entry and if it's an exact match, return
769 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000770 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000771 */
Barry Naujok5163f952008-05-21 16:41:01 +1000772 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
773 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000774 /* If there is a CI match block, drop it */
775 if (args->cmpresult != XFS_CMP_DIFFERENT &&
776 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000777 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000778 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000779 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100780 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000781 *indexp = index;
782 state->extravalid = 1;
783 state->extrablk.bp = curbp;
784 state->extrablk.blkno = curdb;
785 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000786 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000787 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100788 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100789 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000790 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000791 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000792 }
793 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000794 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000795 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000796 if (args->cmpresult == XFS_CMP_DIFFERENT) {
797 /* Giving back last used data block. */
798 state->extravalid = 1;
799 state->extrablk.bp = curbp;
800 state->extrablk.index = -1;
801 state->extrablk.blkno = curdb;
802 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100803 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100804 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000805 } else {
806 /* If the curbp is not the CI match block, drop it */
807 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000809 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000810 } else {
811 state->extravalid = 0;
812 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000813 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000814 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000815}
816
817/*
818 * Look up a leaf entry in a node-format leaf block.
819 * If this is an addname then the extrablk in state is a freespace block,
820 * otherwise it's a data block.
821 */
822int
823xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000825 xfs_da_args_t *args, /* operation arguments */
826 int *indexp, /* out: leaf entry index */
827 xfs_da_state_t *state) /* state to fill in */
828{
Barry Naujok6a178102008-05-21 16:42:05 +1000829 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000830 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
831 state);
832 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
833}
834
835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * Move count leaf entries from source to destination leaf.
837 * Log entries and headers. Stale entries are preserved.
838 */
839static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000840xfs_dir3_leafn_moveents(
841 xfs_da_args_t *args, /* operation arguments */
842 struct xfs_buf *bp_s, /* source */
843 struct xfs_dir3_icleaf_hdr *shdr,
844 struct xfs_dir2_leaf_entry *sents,
845 int start_s,/* source leaf index */
846 struct xfs_buf *bp_d, /* destination */
847 struct xfs_dir3_icleaf_hdr *dhdr,
848 struct xfs_dir2_leaf_entry *dents,
849 int start_d,/* destination leaf index */
850 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Dave Chinner24df33b2013-04-12 07:30:21 +1000852 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000854 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 /*
857 * Silently return if nothing to do.
858 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000859 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * If the destination index is not the end of the current
864 * destination leaf entries, open up a hole in the destination
865 * to hold the new entries.
866 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000867 if (start_d < dhdr->count) {
868 memmove(&dents[start_d + count], &dents[start_d],
869 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000870 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000871 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 /*
874 * If the source has stale leaves, count the ones in the copy range
875 * so we can update the header correctly.
876 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000877 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 int i; /* temp leaf index */
879
880 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000881 if (sents[i].address ==
882 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 stale++;
884 }
885 } else
886 stale = 0;
887 /*
888 * Copy the leaf entries from source to destination.
889 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000890 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000892 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * If there are source entries after the ones we copied,
896 * delete the ones we copied by sliding the next ones down.
897 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000898 if (start_s + count < shdr->count) {
899 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000901 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /*
905 * Update the headers and log them.
906 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000907 shdr->count -= count;
908 shdr->stale -= stale;
909 dhdr->count += count;
910 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * Determine the sort order of two leaf blocks.
915 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
916 */
917int /* sort order */
918xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100919 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
921 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Dave Chinner24df33b2013-04-12 07:30:21 +1000923 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
924 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
925 struct xfs_dir2_leaf_entry *ents1;
926 struct xfs_dir2_leaf_entry *ents2;
927 struct xfs_dir3_icleaf_hdr hdr1;
928 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Chinner01ba43b2013-10-29 22:11:52 +1100930 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
931 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100932 ents1 = dp->d_ops->leaf_ents_p(leaf1);
933 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000934
935 if (hdr1.count > 0 && hdr2.count > 0 &&
936 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
937 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
938 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 1;
940 return 0;
941}
942
943/*
944 * Rebalance leaf entries between two leaf blocks.
945 * This is actually only called when the second block is new,
946 * though the code deals with the general case.
947 * A new entry will be inserted in one of the blocks, and that
948 * entry is taken into account when balancing.
949 */
950static void
951xfs_dir2_leafn_rebalance(
952 xfs_da_state_t *state, /* btree cursor */
953 xfs_da_state_blk_t *blk1, /* first btree block */
954 xfs_da_state_blk_t *blk2) /* second btree block */
955{
956 xfs_da_args_t *args; /* operation arguments */
957 int count; /* count (& direction) leaves */
958 int isleft; /* new goes in left leaf */
959 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
960 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
961 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000962#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 int oldstale; /* old count of stale leaves */
964#endif
965 int oldsum; /* old total leaf count */
966 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000967 struct xfs_dir2_leaf_entry *ents1;
968 struct xfs_dir2_leaf_entry *ents2;
969 struct xfs_dir3_icleaf_hdr hdr1;
970 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100971 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 args = state->args;
974 /*
975 * If the block order is wrong, swap the arguments.
976 */
Dave Chinner41419562013-10-29 22:11:50 +1100977 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 xfs_da_state_blk_t *tmp; /* temp for block swap */
979
980 tmp = blk1;
981 blk1 = blk2;
982 blk2 = tmp;
983 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000984 leaf1 = blk1->bp->b_addr;
985 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100986 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
987 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100988 ents1 = dp->d_ops->leaf_ents_p(leaf1);
989 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000990
991 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000992#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000993 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#endif
995 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * If the old leaf count was odd then the new one will be even,
999 * so we need to divide the new count evenly.
1000 */
1001 if (oldsum & 1) {
1002 xfs_dahash_t midhash; /* middle entry hash value */
1003
Dave Chinner24df33b2013-04-12 07:30:21 +10001004 if (mid >= hdr1.count)
1005 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001007 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 isleft = args->hashval <= midhash;
1009 }
1010 /*
1011 * If the old count is even then the new count is odd, so there's
1012 * no preferred side for the new entry.
1013 * Pick the left one.
1014 */
1015 else
1016 isleft = 1;
1017 /*
1018 * Calculate moved entry count. Positive means left-to-right,
1019 * negative means right-to-left. Then move the entries.
1020 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001021 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001023 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1024 hdr1.count - count, blk2->bp,
1025 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001027 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1028 blk1->bp, &hdr1, ents1,
1029 hdr1.count, count);
1030
1031 ASSERT(hdr1.count + hdr2.count == oldsum);
1032 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1033
1034 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001035 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1036 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001037 xfs_dir3_leaf_log_header(args, blk1->bp);
1038 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001039
Dave Chinner41419562013-10-29 22:11:50 +11001040 xfs_dir3_leaf_check(dp, blk1->bp);
1041 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 /*
1044 * Mark whether we're inserting into the old or new leaf.
1045 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001046 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001048 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 state->inleaf = !swap;
1050 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001051 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 /*
1053 * Adjust the expected index for insertion.
1054 */
1055 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001056 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001057
1058 /*
1059 * Finally sanity check just to make sure we are not returning a
1060 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Dave Chinner41419562013-10-29 22:11:50 +11001062 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 state->inleaf = 1;
1064 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001065 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001066 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001067 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069}
1070
Dave Chinner20252072012-11-12 22:54:13 +11001071static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001072xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001073 xfs_da_args_t *args,
1074 struct xfs_dir2_data_hdr *hdr,
1075 struct xfs_dir2_free *free,
1076 xfs_dir2_db_t fdb,
1077 int findex,
1078 struct xfs_buf *fbp,
1079 int longest)
1080{
Dave Chinner20252072012-11-12 22:54:13 +11001081 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001082 __be16 *bests;
1083 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001084 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001085
Dave Chinner01ba43b2013-10-29 22:11:52 +11001086 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001087 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001088 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001089 /*
1090 * Data block is not empty, just set the free entry to the new
1091 * value.
1092 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001093 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001094 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001095 return 0;
1096 }
1097
1098 /* One less used entry in the free table. */
1099 freehdr.nused--;
1100
1101 /*
1102 * If this was the last entry in the table, we can trim the table size
1103 * back. There might be other entries at the end referring to
1104 * non-existent data blocks, get those too.
1105 */
1106 if (findex == freehdr.nvalid - 1) {
1107 int i; /* free entry index */
1108
1109 for (i = findex - 1; i >= 0; i--) {
1110 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1111 break;
1112 }
1113 freehdr.nvalid = i + 1;
1114 logfree = 0;
1115 } else {
1116 /* Not the last entry, just punch it out. */
1117 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001118 logfree = 1;
1119 }
1120
Dave Chinner01ba43b2013-10-29 22:11:52 +11001121 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001122 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001123
1124 /*
1125 * If there are no useful entries left in the block, get rid of the
1126 * block if we can.
1127 */
1128 if (!freehdr.nused) {
1129 int error;
1130
1131 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1132 if (error == 0) {
1133 fbp = NULL;
1134 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001135 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001136 return error;
1137 /*
1138 * It's possible to get ENOSPC if there is no
1139 * space reservation. In this case some one
1140 * else will eventually get rid of this block.
1141 */
1142 }
1143
Dave Chinner20252072012-11-12 22:54:13 +11001144 /* Log the free entry that changed, unless we got rid of it. */
1145 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001146 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001147 return 0;
1148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150/*
1151 * Remove an entry from a node directory.
1152 * This removes the leaf entry and the data entry,
1153 * and updates the free block if necessary.
1154 */
1155static int /* error */
1156xfs_dir2_leafn_remove(
1157 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001158 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int index, /* leaf entry index */
1160 xfs_da_state_blk_t *dblk, /* data block */
1161 int *rval) /* resulting block needs join */
1162{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001163 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001165 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_dir2_data_entry_t *dep; /* data block entry */
1167 xfs_inode_t *dp; /* incore directory inode */
1168 xfs_dir2_leaf_t *leaf; /* leaf structure */
1169 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1170 int longest; /* longest data free entry */
1171 int off; /* data block entry offset */
1172 xfs_mount_t *mp; /* filesystem mount point */
1173 int needlog; /* need to log data header */
1174 int needscan; /* need to rescan data frees */
1175 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001176 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001177 struct xfs_dir3_icleaf_hdr leafhdr;
1178 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001180 trace_xfs_dir2_leafn_remove(args, index);
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 dp = args->dp;
1183 tp = args->trans;
1184 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001185 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001186 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001187 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /*
1190 * Point to the entry we're removing.
1191 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001192 lep = &ents[index];
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Extract the data block and offset from the entry.
1196 */
Dave Chinner30028032014-06-06 15:08:18 +10001197 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001199 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Kill the leaf entry by marking it stale.
1204 * Log the leaf block changes.
1205 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001206 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001207 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001208 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001209
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001210 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001211 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * Make the data entry free. Keep track of the longest freespace
1215 * in the data block in case it changes.
1216 */
1217 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001218 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001219 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001220 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001221 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001223 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001224 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Rescan the data block freespaces for bestfree.
1227 * Log the data block header if needed.
1228 */
1229 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001230 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001232 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001233 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 /*
1235 * If the longest data block freespace changes, need to update
1236 * the corresponding freeblock entry.
1237 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001238 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001240 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 xfs_dir2_db_t fdb; /* freeblock block number */
1242 int findex; /* index in freeblock entries */
1243 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * Convert the data block number to a free block,
1247 * read in the free block.
1248 */
Dave Chinner8f661932014-06-06 15:15:59 +10001249 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001250 error = xfs_dir2_free_read(tp, dp,
1251 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001252 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001253 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001255 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001256#ifdef DEBUG
1257 {
1258 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001259 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001260 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001261 (fdb - xfs_dir2_byte_to_db(args->geo,
1262 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001263 }
1264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /*
1266 * Calculate which entry we need to fix.
1267 */
Dave Chinner8f661932014-06-06 15:15:59 +10001268 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001269 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * If the data block is now empty we can get rid of it
1272 * (usually).
1273 */
Dave Chinner8f661932014-06-06 15:15:59 +10001274 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001275 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /*
1277 * Try to punch out the data block.
1278 */
1279 error = xfs_dir2_shrink_inode(args, db, dbp);
1280 if (error == 0) {
1281 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001282 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 /*
1285 * We can get ENOSPC if there's no space reservation.
1286 * In this case just drop the buffer and some one else
1287 * will eventually get rid of the empty block.
1288 */
Dave Chinner24513372014-06-25 14:58:08 +10001289 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return error;
1291 }
1292 /*
1293 * If we got rid of the data block, we can eliminate that entry
1294 * in the free block.
1295 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001296 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001297 fdb, findex, fbp, longest);
1298 if (error)
1299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Dave Chinner20252072012-11-12 22:54:13 +11001301
Dave Chinner41419562013-10-29 22:11:50 +11001302 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001304 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * to justify trying to join it with a neighbor.
1306 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001307 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001308 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001309 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312
1313/*
1314 * Split the leaf entries in the old block into old and new blocks.
1315 */
1316int /* error */
1317xfs_dir2_leafn_split(
1318 xfs_da_state_t *state, /* btree cursor */
1319 xfs_da_state_blk_t *oldblk, /* original block */
1320 xfs_da_state_blk_t *newblk) /* newly created block */
1321{
1322 xfs_da_args_t *args; /* operation arguments */
1323 xfs_dablk_t blkno; /* new leaf block number */
1324 int error; /* error return value */
1325 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner41419562013-10-29 22:11:50 +11001326 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /*
1329 * Allocate space for a new leaf node.
1330 */
1331 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001332 dp = args->dp;
1333 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1335 error = xfs_da_grow_inode(args, &blkno);
1336 if (error) {
1337 return error;
1338 }
1339 /*
1340 * Initialize the new leaf block.
1341 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001342 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001343 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1344 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 newblk->blkno = blkno;
1348 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1349 /*
1350 * Rebalance the entries across the two leaves, link the new
1351 * block into the leaves.
1352 */
1353 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001354 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (error) {
1356 return error;
1357 }
1358 /*
1359 * Insert the new entry in the correct block.
1360 */
1361 if (state->inleaf)
1362 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1363 else
1364 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1365 /*
1366 * Update last hashval in each block since we added the name.
1367 */
Dave Chinner41419562013-10-29 22:11:50 +11001368 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1369 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1370 xfs_dir3_leaf_check(dp, oldblk->bp);
1371 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return error;
1373}
1374
1375/*
1376 * Check a leaf block and its neighbors to see if the block should be
1377 * collapsed into one or the other neighbor. Always keep the block
1378 * with the smaller block number.
1379 * If the current block is over 50% full, don't try to join it, return 0.
1380 * If the block is empty, fill in the state structure and return 2.
1381 * If it can be collapsed, fill in the state structure and return 1.
1382 * If nothing can be done, return 0.
1383 */
1384int /* error */
1385xfs_dir2_leafn_toosmall(
1386 xfs_da_state_t *state, /* btree cursor */
1387 int *action) /* resulting action to take */
1388{
1389 xfs_da_state_blk_t *blk; /* leaf block */
1390 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001391 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int bytes; /* bytes in use */
1393 int count; /* leaf live entry count */
1394 int error; /* error return value */
1395 int forward; /* sibling block direction */
1396 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 xfs_dir2_leaf_t *leaf; /* leaf structure */
1398 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001399 struct xfs_dir3_icleaf_hdr leafhdr;
1400 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001401 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /*
1404 * Check for the degenerate case of the block being over 50% full.
1405 * If so, it's not worth even looking to see if we might be able
1406 * to coalesce with a sibling.
1407 */
1408 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001409 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001410 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001411 ents = dp->d_ops->leaf_ents_p(leaf);
1412 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001413
1414 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001415 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001416 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /*
1418 * Blk over 50%, don't try to join.
1419 */
1420 *action = 0;
1421 return 0;
1422 }
1423 /*
1424 * Check for the degenerate case of the block being empty.
1425 * If the block is empty, we'll simply delete it, no need to
1426 * coalesce it with a sibling block. We choose (arbitrarily)
1427 * to merge with the forward block unless it is NULL.
1428 */
1429 if (count == 0) {
1430 /*
1431 * Make altpath point to the block we want to keep and
1432 * path point to the block we want to drop (this one).
1433 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001434 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001436 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 &rval);
1438 if (error)
1439 return error;
1440 *action = rval ? 2 : 0;
1441 return 0;
1442 }
1443 /*
1444 * Examine each sibling block to see if we can coalesce with
1445 * at least 25% free space to spare. We need to figure out
1446 * whether to merge with the forward or the backward block.
1447 * We prefer coalescing with the lower numbered sibling so as
1448 * to shrink a directory over time.
1449 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001450 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001452 struct xfs_dir3_icleaf_hdr hdr2;
1453
1454 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (blkno == 0)
1456 continue;
1457 /*
1458 * Read the sibling leaf block.
1459 */
Dave Chinner41419562013-10-29 22:11:50 +11001460 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001461 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001462 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /*
1466 * Count bytes in the two blocks combined.
1467 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001468 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001469 bytes = state->args->geo->blksize -
1470 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001471
Dave Chinner1d9025e2012-06-22 18:50:14 +10001472 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001473 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001474 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001475 count += hdr2.count - hdr2.stale;
1476 bytes -= count * sizeof(ents[0]);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Fits with at least 25% to spare.
1480 */
1481 if (bytes >= 0)
1482 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001483 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 /*
1486 * Didn't like either block, give up.
1487 */
1488 if (i >= 2) {
1489 *action = 0;
1490 return 0;
1491 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * Make altpath point to the block we want to keep (the lower
1495 * numbered block) and path point to the block we want to drop.
1496 */
1497 memcpy(&state->altpath, &state->path, sizeof(state->path));
1498 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001499 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 &rval);
1501 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001502 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 &rval);
1504 if (error) {
1505 return error;
1506 }
1507 *action = rval ? 0 : 1;
1508 return 0;
1509}
1510
1511/*
1512 * Move all the leaf entries from drop_blk to save_blk.
1513 * This is done as part of a join operation.
1514 */
1515void
1516xfs_dir2_leafn_unbalance(
1517 xfs_da_state_t *state, /* cursor */
1518 xfs_da_state_blk_t *drop_blk, /* dead block */
1519 xfs_da_state_blk_t *save_blk) /* surviving block */
1520{
1521 xfs_da_args_t *args; /* operation arguments */
1522 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1523 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001524 struct xfs_dir3_icleaf_hdr savehdr;
1525 struct xfs_dir3_icleaf_hdr drophdr;
1526 struct xfs_dir2_leaf_entry *sents;
1527 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001528 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 args = state->args;
1531 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1532 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001533 drop_leaf = drop_blk->bp->b_addr;
1534 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001535
Dave Chinner01ba43b2013-10-29 22:11:52 +11001536 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1537 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1538 sents = dp->d_ops->leaf_ents_p(save_leaf);
1539 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
1542 * If there are any stale leaf entries, take this opportunity
1543 * to purge them.
1544 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001545 if (drophdr.stale)
1546 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1547 if (savehdr.stale)
1548 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * Move the entries from drop to the appropriate end of save.
1552 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001553 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001554 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001555 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1556 save_blk->bp, &savehdr, sents, 0,
1557 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001559 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1560 save_blk->bp, &savehdr, sents,
1561 savehdr.count, drophdr.count);
1562 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1563
1564 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001565 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1566 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001567 xfs_dir3_leaf_log_header(args, save_blk->bp);
1568 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001569
Dave Chinner41419562013-10-29 22:11:50 +11001570 xfs_dir3_leaf_check(dp, save_blk->bp);
1571 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574/*
1575 * Top-level node form directory addname routine.
1576 */
1577int /* error */
1578xfs_dir2_node_addname(
1579 xfs_da_args_t *args) /* operation arguments */
1580{
1581 xfs_da_state_blk_t *blk; /* leaf block for insert */
1582 int error; /* error return value */
1583 int rval; /* sub-return value */
1584 xfs_da_state_t *state; /* btree cursor */
1585
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001586 trace_xfs_dir2_node_addname(args);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Allocate and initialize the state (btree cursor).
1590 */
1591 state = xfs_da_state_alloc();
1592 state->args = args;
1593 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * Look up the name. We're not supposed to find it, but
1596 * this gives us the insertion point.
1597 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001598 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (error)
1600 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001601 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto done;
1603 }
1604 /*
1605 * Add the data entry to a data block.
1606 * Extravalid is set to a freeblock found by lookup.
1607 */
1608 rval = xfs_dir2_node_addname_int(args,
1609 state->extravalid ? &state->extrablk : NULL);
1610 if (rval) {
1611 goto done;
1612 }
1613 blk = &state->path.blk[state->path.active - 1];
1614 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1615 /*
1616 * Add the new leaf entry.
1617 */
1618 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1619 if (rval == 0) {
1620 /*
1621 * It worked, fix the hash values up the btree.
1622 */
Barry Naujok6a178102008-05-21 16:42:05 +10001623 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001624 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 } else {
1626 /*
1627 * It didn't work, we need to split the leaf block.
1628 */
1629 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001630 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto done;
1632 }
1633 /*
1634 * Split the leaf block and insert the new entry.
1635 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001636 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638done:
1639 xfs_da_state_free(state);
1640 return rval;
1641}
1642
1643/*
1644 * Add the data entry for a node-format directory name addition.
1645 * The leaf entry is added in xfs_dir2_leafn_add.
1646 * We may enter with a freespace block that the lookup found.
1647 */
1648static int /* error */
1649xfs_dir2_node_addname_int(
1650 xfs_da_args_t *args, /* operation arguments */
1651 xfs_da_state_blk_t *fblk) /* optional freespace block */
1652{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001653 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001655 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1657 xfs_inode_t *dp; /* incore directory inode */
1658 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1659 int error; /* error return value */
1660 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001661 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int findex; /* freespace entry index */
1663 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1664 xfs_dir2_db_t ifbno; /* initial freespace block no */
1665 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1666 int length; /* length of the new entry */
1667 int logfree; /* need to log free entry */
1668 xfs_mount_t *mp; /* filesystem mount point */
1669 int needlog; /* need to log data header */
1670 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001671 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001673 __be16 *bests;
1674 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001675 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 dp = args->dp;
1678 mp = dp->i_mount;
1679 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001680 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * If we came in with a freespace block that means that lookup
1683 * found an entry with our hash value. This is the freespace
1684 * block for that data entry.
1685 */
1686 if (fblk) {
1687 fbp = fblk->bp;
1688 /*
1689 * Remember initial freespace block number.
1690 */
1691 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001692 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001694 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001695 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /*
1698 * This means the free entry showed that the data block had
1699 * space for our entry, so we remembered it.
1700 * Use that data block.
1701 */
1702 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001703 ASSERT(findex < freehdr.nvalid);
1704 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1705 ASSERT(be16_to_cpu(bests[findex]) >= length);
1706 dbno = freehdr.firstdb + findex;
1707 } else {
1708 /*
1709 * The data block looked at didn't have enough room.
1710 * We'll start at the beginning of the freespace entries.
1711 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 dbno = -1;
1713 findex = 0;
1714 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001715 } else {
1716 /*
1717 * Didn't come in with a freespace block, so no data block.
1718 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 ifbno = dbno = -1;
1720 fbp = NULL;
1721 findex = 0;
1722 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /*
1725 * If we don't have a data block yet, we're going to scan the
1726 * freespace blocks looking for one. Figure out what the
1727 * highest freespace block number is.
1728 */
1729 if (dbno == -1) {
1730 xfs_fileoff_t fo; /* freespace block number */
1731
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001732 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001734 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 fbno = ifbno;
1736 }
1737 /*
1738 * While we haven't identified a data block, search the freeblock
1739 * data for a good data block. If we find a null freeblock entry,
1740 * indicating a hole in the data blocks, remember that.
1741 */
1742 while (dbno == -1) {
1743 /*
1744 * If we don't have a freeblock in hand, get the next one.
1745 */
1746 if (fbp == NULL) {
1747 /*
1748 * Happens the first time through unless lookup gave
1749 * us a freespace block to start with.
1750 */
1751 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001752 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001753 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 /*
1755 * If it's ifbno we already looked at it.
1756 */
1757 if (fbno == ifbno)
1758 fbno++;
1759 /*
1760 * If it's off the end we're done.
1761 */
1762 if (fbno >= lastfbno)
1763 break;
1764 /*
1765 * Read the block. There can be holes in the
1766 * freespace blocks, so this might not succeed.
1767 * This should be really rare, so there's no reason
1768 * to avoid it.
1769 */
Dave Chinner20252072012-11-12 22:54:13 +11001770 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001771 xfs_dir2_db_to_da(args->geo, fbno),
1772 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001775 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001777 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 findex = 0;
1779 }
1780 /*
1781 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001782 *
1783 * The bests initialisation should be where the bufer is read in
1784 * the above branch. But gcc is too stupid to realise that bests
1785 * and the freehdr are actually initialised if they are placed
1786 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001788 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001789 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001790 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1791 be16_to_cpu(bests[findex]) >= length)
1792 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 else {
1794 /*
1795 * Are we done with the freeblock?
1796 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001797 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
1799 * Drop the block.
1800 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001801 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 fbp = NULL;
1803 if (fblk && fblk->bp)
1804 fblk->bp = NULL;
1805 }
1806 }
1807 }
1808 /*
1809 * If we don't have a data block, we need to allocate one and make
1810 * the freespace entries refer to it.
1811 */
1812 if (unlikely(dbno == -1)) {
1813 /*
1814 * Not allowed to allocate, return failure.
1815 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001816 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001817 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Allocate and initialize the new data block.
1821 */
1822 if (unlikely((error = xfs_dir2_grow_inode(args,
1823 XFS_DIR2_DATA_SPACE,
1824 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001825 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * If (somehow) we have a freespace block, get rid of it.
1830 */
1831 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001832 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (fblk && fblk->bp)
1834 fblk->bp = NULL;
1835
1836 /*
1837 * Get the freespace block corresponding to the data block
1838 * that was just allocated.
1839 */
Dave Chinner8f661932014-06-06 15:15:59 +10001840 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001841 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001842 xfs_dir2_db_to_da(args->geo, fbno),
1843 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
1848 * If there wasn't a freespace block, the read will
1849 * return a NULL fbp. Allocate and initialize a new one.
1850 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001851 if (!fbp) {
1852 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1853 &fbno);
1854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Dave Chinner8f661932014-06-06 15:15:59 +10001857 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001858 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001859 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001860 " data block %lld, got %lld ifbno %llu lastfbno %d",
1861 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001862 (long long)dp->d_ops->db_to_fdb(
1863 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 (long long)dbno, (long long)fbno,
1865 (unsigned long long)ifbno, lastfbno);
1866 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001867 xfs_alert(mp,
1868 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 fblk,
1870 (unsigned long long)fblk->blkno,
1871 fblk->index,
1872 fblk->magic);
1873 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001874 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 }
1876 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1877 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001878 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
1881 /*
1882 * Get a buffer for the new block.
1883 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001884 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001887 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001888 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001889 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001892 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001894 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001895 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001896 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001897 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001899 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001900 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001901 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903
1904 /*
1905 * Set the freespace block index from the data block number.
1906 */
Dave Chinner8f661932014-06-06 15:15:59 +10001907 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /*
1909 * If it's after the end of the current entries in the
1910 * freespace block, extend that table.
1911 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001912 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001913 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001914 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /*
1916 * Tag new entry so nused will go up.
1917 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001918 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 /*
1921 * If this entry was for an empty data block
1922 * (this should always be true) then update the header.
1923 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001924 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1925 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001926 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001927 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 /*
1930 * Update the real value in the table.
1931 * We haven't allocated the data entry yet so this will
1932 * change again.
1933 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001934 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001935 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001936 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 logfree = 1;
1938 }
1939 /*
1940 * We had a data block so we don't have to make a new one.
1941 */
1942 else {
1943 /*
1944 * If just checking, we succeeded.
1945 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001946 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
1950 * Read the data block in.
1951 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001952 error = xfs_dir3_data_read(tp, dp,
1953 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001954 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001957 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001958 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 logfree = 0;
1960 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001961 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * Point to the existing unused space.
1964 */
1965 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001966 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 needscan = needlog = 0;
1968 /*
1969 * Mark the first part of the unused space, inuse for us.
1970 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001971 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001972 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 &needlog, &needscan);
1974 /*
1975 * Fill in the new entry and log it.
1976 */
1977 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001978 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 dep->namelen = args->namelen;
1980 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001981 dp->d_ops->data_put_ftype(dep, args->filetype);
1982 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001983 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001984 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * Rescan the block for bestfree if needed.
1987 */
1988 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001989 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /*
1991 * Log the data block header if needed.
1992 */
1993 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001994 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /*
1996 * If the freespace entry is now wrong, update it.
1997 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001998 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11001999 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2000 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 logfree = 1;
2002 }
2003 /*
2004 * Log the freespace entry if needed.
2005 */
2006 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10002007 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 * Return the data block and offset in args, then drop the data block.
2010 */
2011 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002012 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 return 0;
2014}
2015
2016/*
2017 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002018 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * The only real output is the inode number of the entry.
2020 */
2021int /* error */
2022xfs_dir2_node_lookup(
2023 xfs_da_args_t *args) /* operation arguments */
2024{
2025 int error; /* error return value */
2026 int i; /* btree level */
2027 int rval; /* operation return value */
2028 xfs_da_state_t *state; /* btree cursor */
2029
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002030 trace_xfs_dir2_node_lookup(args);
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * Allocate and initialize the btree cursor.
2034 */
2035 state = xfs_da_state_alloc();
2036 state->args = args;
2037 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /*
2039 * Fill in the path to the entry in the cursor.
2040 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002041 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (error)
2043 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002044 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2045 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002046 xfs_dir2_data_entry_t *dep;
2047
Dave Chinner1d9025e2012-06-22 18:50:14 +10002048 dep = (xfs_dir2_data_entry_t *)
2049 ((char *)state->extrablk.bp->b_addr +
2050 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002051 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /*
2054 * Release the btree blocks and leaf block.
2055 */
2056 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002057 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 state->path.blk[i].bp = NULL;
2059 }
2060 /*
2061 * Release the data block if we have it.
2062 */
2063 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002064 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 state->extrablk.bp = NULL;
2066 }
2067 xfs_da_state_free(state);
2068 return rval;
2069}
2070
2071/*
2072 * Remove an entry from a node-format directory.
2073 */
2074int /* error */
2075xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002076 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002078 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int error; /* error return value */
2080 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002081 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002083 trace_xfs_dir2_node_removename(args);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Allocate and initialize the btree cursor.
2087 */
2088 state = xfs_da_state_alloc();
2089 state->args = args;
2090 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002091
2092 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002093 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002094 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002095 goto out_free;
2096
2097 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002098 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002099 error = rval;
2100 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 blk = &state->path.blk[state->path.active - 1];
2104 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2105 ASSERT(state->extravalid);
2106 /*
2107 * Remove the leaf and data entries.
2108 * Extrablk refers to the data block.
2109 */
2110 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2111 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002112 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002113 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * Fix the hash values up the btree.
2116 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002117 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 /*
2119 * If we need to join leaf blocks, do it.
2120 */
2121 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002122 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /*
2124 * If no errors so far, try conversion to leaf format.
2125 */
2126 if (!error)
2127 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002128out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 xfs_da_state_free(state);
2130 return error;
2131}
2132
2133/*
2134 * Replace an entry's inode number in a node-format directory.
2135 */
2136int /* error */
2137xfs_dir2_node_replace(
2138 xfs_da_args_t *args) /* operation arguments */
2139{
2140 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002141 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 xfs_dir2_data_entry_t *dep; /* data entry changed */
2143 int error; /* error return value */
2144 int i; /* btree level */
2145 xfs_ino_t inum; /* new inode number */
2146 xfs_dir2_leaf_t *leaf; /* leaf structure */
2147 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2148 int rval; /* internal return value */
2149 xfs_da_state_t *state; /* btree cursor */
2150
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002151 trace_xfs_dir2_node_replace(args);
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 /*
2154 * Allocate and initialize the btree cursor.
2155 */
2156 state = xfs_da_state_alloc();
2157 state->args = args;
2158 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 inum = args->inumber;
2160 /*
2161 * Lookup the entry to change in the btree.
2162 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002163 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (error) {
2165 rval = error;
2166 }
2167 /*
2168 * It should be found, since the vnodeops layer has looked it up
2169 * and locked it. But paranoia is good.
2170 */
Dave Chinner24513372014-06-25 14:58:08 +10002171 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002172 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
2174 * Find the leaf entry.
2175 */
2176 blk = &state->path.blk[state->path.active - 1];
2177 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002178 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002179 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002180 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 ASSERT(state->extravalid);
2182 /*
2183 * Point to the data entry.
2184 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002185 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002186 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2187 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002189 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002190 xfs_dir2_dataptr_to_off(args->geo,
2191 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002192 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Fill in the new inode number and log the entry.
2195 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002196 dep->inumber = cpu_to_be64(inum);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002197 args->dp->d_ops->data_put_ftype(dep, args->filetype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002198 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 rval = 0;
2200 }
2201 /*
2202 * Didn't find it, and we're holding a data block. Drop it.
2203 */
2204 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002205 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 state->extrablk.bp = NULL;
2207 }
2208 /*
2209 * Release all the buffers in the cursor.
2210 */
2211 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002212 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 state->path.blk[i].bp = NULL;
2214 }
2215 xfs_da_state_free(state);
2216 return rval;
2217}
2218
2219/*
2220 * Trim off a trailing empty freespace block.
2221 * Return (in rvalp) 1 if we did it, 0 if not.
2222 */
2223int /* error */
2224xfs_dir2_node_trim_free(
2225 xfs_da_args_t *args, /* operation arguments */
2226 xfs_fileoff_t fo, /* free block number */
2227 int *rvalp) /* out: did something */
2228{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002229 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 xfs_inode_t *dp; /* incore directory inode */
2231 int error; /* error return code */
2232 xfs_dir2_free_t *free; /* freespace structure */
2233 xfs_mount_t *mp; /* filesystem mount point */
2234 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002235 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 dp = args->dp;
2238 mp = dp->i_mount;
2239 tp = args->trans;
2240 /*
2241 * Read the freespace block.
2242 */
Dave Chinner20252072012-11-12 22:54:13 +11002243 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002244 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /*
2247 * There can be holes in freespace. If fo is a hole, there's
2248 * nothing to do.
2249 */
Dave Chinner20252072012-11-12 22:54:13 +11002250 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002252 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002253 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /*
2256 * If there are used entries, there's nothing to do.
2257 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002258 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002259 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 *rvalp = 0;
2261 return 0;
2262 }
2263 /*
2264 * Blow the block away.
2265 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002266 error = xfs_dir2_shrink_inode(args,
2267 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2268 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /*
2270 * Can't fail with ENOSPC since that only happens with no
2271 * space reservation, when breaking up an extent into two
2272 * pieces. This is the last block of an extent.
2273 */
Dave Chinner24513372014-06-25 14:58:08 +10002274 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002275 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return error;
2277 }
2278 /*
2279 * Return that we succeeded.
2280 */
2281 *rvalp = 1;
2282 return 0;
2283}