blob: 853798519ae92d2fbb2979d080fba2350c5c798d [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"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
30#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110035#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110036#include "xfs_buf_item.h"
37#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Function declarations.
41 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
43 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
45 xfs_da_state_blk_t *blk1,
46 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100047static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 int index, xfs_da_state_blk_t *dblk,
49 int *rval);
50static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
51 xfs_da_state_blk_t *fblk);
52
Dave Chinner24df33b2013-04-12 07:30:21 +100053/*
54 * Check internal consistency of a leafn block.
55 */
56#ifdef DEBUG
Dave Chinner41419562013-10-29 22:11:50 +110057#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100058do { \
Dave Chinner41419562013-10-29 22:11:50 +110059 if (!xfs_dir3_leafn_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100060 ASSERT(0); \
61} while (0);
62
63static bool
64xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110065 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100066 struct xfs_buf *bp)
67{
68 struct xfs_dir2_leaf *leaf = bp->b_addr;
69 struct xfs_dir3_icleaf_hdr leafhdr;
70
Dave Chinner01ba43b2013-10-29 22:11:52 +110071 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100072
73 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
74 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
75 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
76 return false;
77 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
78 return false;
79
Dave Chinner41419562013-10-29 22:11:50 +110080 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100081}
82#else
Dave Chinner41419562013-10-29 22:11:50 +110083#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100084#endif
85
Dave Chinnercbc8adf2013-04-03 16:11:21 +110086static bool
87xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110088 struct xfs_buf *bp)
89{
90 struct xfs_mount *mp = bp->b_target->bt_mount;
91 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110092
Dave Chinnercbc8adf2013-04-03 16:11:21 +110093 if (xfs_sb_version_hascrc(&mp->m_sb)) {
94 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
95
96 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
97 return false;
98 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
99 return false;
100 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
101 return false;
102 } else {
103 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
104 return false;
105 }
106
107 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
108
109 return true;
110}
111
112static void
113xfs_dir3_free_read_verify(
114 struct xfs_buf *bp)
115{
116 struct xfs_mount *mp = bp->b_target->bt_mount;
117
118 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
119 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
120 XFS_DIR3_FREE_CRC_OFF)) ||
121 !xfs_dir3_free_verify(bp)) {
122 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinner20252072012-11-12 22:54:13 +1100123 xfs_buf_ioerror(bp, EFSCORRUPTED);
124 }
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)) {
136 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
137 xfs_buf_ioerror(bp, EFSCORRUPTED);
138 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
147 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), 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(
197 struct xfs_trans *tp,
198 struct xfs_inode *dp,
199 xfs_dir2_db_t fbno,
200 struct xfs_buf **bpp)
201{
202 struct xfs_mount *mp = dp->i_mount;
203 struct xfs_buf *bp;
204 int error;
205 struct xfs_dir3_icfree_hdr hdr;
206
207 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fbno),
208 -1, &bp, XFS_DATA_FORK);
209 if (error)
210 return error;
211
Dave Chinner61fe1352013-04-03 16:11:30 +1100212 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100213 bp->b_ops = &xfs_dir3_free_buf_ops;
214
215 /*
216 * Initialize the new block to be empty, and remember
217 * its first slot as our empty slot.
218 */
Dave Chinnere400d272013-05-28 18:37:17 +1000219 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
220 memset(&hdr, 0, sizeof(hdr));
221
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100222 if (xfs_sb_version_hascrc(&mp->m_sb)) {
223 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
224
225 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000226
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100227 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
228 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
229 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000230 } else
231 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100232 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100233 *bpp = bp;
234 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * Log entries from a freespace block.
239 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000240STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000242 struct xfs_trans *tp,
Dave Chinner24dd0f52013-10-30 13:48:41 -0500243 struct xfs_inode *dp,
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 Chinner24dd0f52013-10-30 13:48:41 -0500252 bests = 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 Chinner1d9025e2012-06-22 18:50:14 +1000255 xfs_trans_log_buf(tp, 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 Chinner1d9025e2012-06-22 18:50:14 +1000266 struct xfs_trans *tp,
Dave Chinner24dd0f52013-10-30 13:48:41 -0500267 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000268 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Dave Chinner836a94a2013-08-12 20:49:44 +1000270#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 xfs_dir2_free_t *free; /* freespace structure */
272
Dave Chinner1d9025e2012-06-22 18:50:14 +1000273 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100274 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
275 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000276#endif
Dave Chinner24dd0f52013-10-30 13:48:41 -0500277 xfs_trans_log_buf(tp, bp, 0, 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 }
317 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
318 /*
319 * Get the buffer for the new freespace block.
320 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100321 error = xfs_dir3_free_get_buf(tp, dp, 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;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000328 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100329 ASSERT(be32_to_cpu(ltp->bestcount) <=
330 (uint)dp->i_d.di_size / mp->m_dirblksize);
331
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 Chinner24dd0f52013-10-30 13:48:41 -0500351 xfs_dir2_free_log_bests(tp, dp, fbp, 0, freehdr.nvalid - 1);
352 xfs_dir2_free_log_header(tp, dp, 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 Chinner41419562013-10-29 22:11:50 +1100366 xfs_dir3_leaf_log_header(tp, dp, lbp);
367 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)
408 return XFS_ERROR(EFSCORRUPTED);
409
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 Chinner41419562013-10-29 22:11:50 +1100417 if (leafhdr.count == dp->d_ops->leaf_max_ents(mp)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000418 if (!leafhdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return XFS_ERROR(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);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000452 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
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 Chinner41419562013-10-29 22:11:50 +1100456 xfs_dir3_leaf_log_header(tp, dp, bp);
457 xfs_dir3_leaf_log_ents(tp, dp, bp, lfloglow, lfloghigh);
458 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 Chinner24dd0f52013-10-30 13:48:41 -0500473 ASSERT((hdr.firstdb % dp->d_ops->free_max_bests(dp->i_mount)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100474 ASSERT(hdr.firstdb <= db);
475 ASSERT(db < hdr.firstdb + hdr.nvalid);
476}
477#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100478#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479#endif /* DEBUG */
480
481/*
482 * Return the last hash value in the leaf.
483 * Stale entries are ok.
484 */
485xfs_dahash_t /* hash value */
486xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100487 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000488 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int *count) /* count of entries in leaf */
490{
Dave Chinner24df33b2013-04-12 07:30:21 +1000491 struct xfs_dir2_leaf *leaf = bp->b_addr;
492 struct xfs_dir2_leaf_entry *ents;
493 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dave Chinner01ba43b2013-10-29 22:11:52 +1100495 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000496
497 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
498 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000501 *count = leafhdr.count;
502 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000504
Dave Chinner41419562013-10-29 22:11:50 +1100505 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000506 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000510 * Look up a leaf entry for space to add a name in a node-format leaf block.
511 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000513STATIC int
514xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000515 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 xfs_da_args_t *args, /* operation arguments */
517 int *indexp, /* out: leaf entry index */
518 xfs_da_state_t *state) /* state to fill in */
519{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000520 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000521 xfs_dir2_db_t curdb = -1; /* current data block number */
522 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 xfs_inode_t *dp; /* incore directory inode */
524 int error; /* error return value */
525 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000526 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int index; /* leaf entry index */
528 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000529 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
531 xfs_mount_t *mp; /* filesystem mount point */
532 xfs_dir2_db_t newdb; /* new data block number */
533 xfs_dir2_db_t newfdb; /* new free block number */
534 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000535 struct xfs_dir2_leaf_entry *ents;
536 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 dp = args->dp;
539 tp = args->trans;
540 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000541 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100542 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100543 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000544
Dave Chinner41419562013-10-29 22:11:50 +1100545 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000546 ASSERT(leafhdr.count > 0);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * Look up the hash value in the leaf entries.
550 */
551 index = xfs_dir2_leaf_search_hash(args, bp);
552 /*
553 * Do we have a buffer coming in?
554 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000555 if (state->extravalid) {
556 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000558 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000559 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100560 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
561 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100563 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Loop over leaf entries with the right hash value.
566 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000567 for (lep = &ents[index];
568 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
569 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
571 * Skip stale leaf entries.
572 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100573 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 continue;
575 /*
576 * Pull the data block number from the entry.
577 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000578 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /*
580 * For addname, we're looking for a place to put the new entry.
581 * We want to use a data block with an entry of equal
582 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000583 *
584 * If this block isn't the data block we already have
585 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000587 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100588 __be16 *bests;
589
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000590 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 * Convert the data block to the free block
593 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500595 newfdb = dp->d_ops->db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000597 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
603 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000604 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100605
606 error = xfs_dir2_free_read(tp, dp,
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000607 xfs_dir2_db_to_da(mp, newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100608 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000609 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000611 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100612
Dave Chinner01ba43b2013-10-29 22:11:52 +1100613 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000616 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500618 fi = dp->d_ops->db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000620 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500622 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100623 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000624 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
625 XFS_ERRLEVEL_LOW, mp);
626 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000627 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000628 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000630 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100631 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 /* Didn't find any space */
636 fi = -1;
637out:
Barry Naujok6a178102008-05-21 16:42:05 +1000638 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 if (curbp) {
640 /* Giving back a free block. */
641 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643 state->extrablk.index = fi;
644 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100645
646 /*
647 * Important: this magic number is not in the buffer - it's for
648 * buffer type information and therefore only the free/data type
649 * matters here, not whether CRCs are enabled or not.
650 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000651 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
652 } else {
653 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000656 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
658 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return XFS_ERROR(ENOENT);
660}
661
662/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000663 * Look up a leaf entry in a node-format leaf block.
664 * The extrablk in state a data block.
665 */
666STATIC int
667xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000668 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000669 xfs_da_args_t *args, /* operation arguments */
670 int *indexp, /* out: leaf entry index */
671 xfs_da_state_t *state) /* state to fill in */
672{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 xfs_dir2_db_t curdb = -1; /* current data block number */
675 xfs_dir2_data_entry_t *dep; /* data block entry */
676 xfs_inode_t *dp; /* incore directory inode */
677 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000678 int index; /* leaf entry index */
679 xfs_dir2_leaf_t *leaf; /* leaf structure */
680 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
681 xfs_mount_t *mp; /* filesystem mount point */
682 xfs_dir2_db_t newdb; /* new data block number */
683 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000684 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000685 struct xfs_dir2_leaf_entry *ents;
686 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687
688 dp = args->dp;
689 tp = args->trans;
690 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000691 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100692 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100693 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000694
Dave Chinner41419562013-10-29 22:11:50 +1100695 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000696 ASSERT(leafhdr.count > 0);
697
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000698 /*
699 * Look up the hash value in the leaf entries.
700 */
701 index = xfs_dir2_leaf_search_hash(args, bp);
702 /*
703 * Do we have a buffer coming in?
704 */
705 if (state->extravalid) {
706 curbp = state->extrablk.bp;
707 curdb = state->extrablk.blkno;
708 }
709 /*
710 * Loop over leaf entries with the right hash value.
711 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000712 for (lep = &ents[index];
713 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
714 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000715 /*
716 * Skip stale leaf entries.
717 */
718 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
719 continue;
720 /*
721 * Pull the data block number from the entry.
722 */
723 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
724 /*
725 * Not adding a new entry, so we really want to find
726 * the name given to us.
727 *
728 * If it's a different data block, go get it.
729 */
730 if (newdb != curdb) {
731 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000732 * If we had a block before that we aren't saving
733 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000734 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000735 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
736 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000737 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000738 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000739 * If needing the block that is saved with a CI match,
740 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000741 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000742 if (args->cmpresult != XFS_CMP_DIFFERENT &&
743 newdb == state->extrablk.blkno) {
744 ASSERT(state->extravalid);
745 curbp = state->extrablk.bp;
746 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100747 error = xfs_dir3_data_read(tp, dp,
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000748 xfs_dir2_db_to_da(mp, newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100749 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000750 if (error)
751 return error;
752 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100753 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000754 curdb = newdb;
755 }
756 /*
757 * Point to the data entry.
758 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000759 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000760 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
761 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000762 * Compare the entry and if it's an exact match, return
763 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000764 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000765 */
Barry Naujok5163f952008-05-21 16:41:01 +1000766 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
767 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000768 /* If there is a CI match block, drop it */
769 if (args->cmpresult != XFS_CMP_DIFFERENT &&
770 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000771 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000772 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000773 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100774 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000775 *indexp = index;
776 state->extravalid = 1;
777 state->extrablk.bp = curbp;
778 state->extrablk.blkno = curdb;
779 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000780 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000781 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100782 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100783 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000784 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000785 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000786 }
787 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000788 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000789 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000790 if (args->cmpresult == XFS_CMP_DIFFERENT) {
791 /* Giving back last used data block. */
792 state->extravalid = 1;
793 state->extrablk.bp = curbp;
794 state->extrablk.index = -1;
795 state->extrablk.blkno = curdb;
796 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100797 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100798 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000799 } else {
800 /* If the curbp is not the CI match block, drop it */
801 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000802 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000803 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000804 } else {
805 state->extravalid = 0;
806 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000807 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000808 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809}
810
811/*
812 * Look up a leaf entry in a node-format leaf block.
813 * If this is an addname then the extrablk in state is a freespace block,
814 * otherwise it's a data block.
815 */
816int
817xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000818 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000819 xfs_da_args_t *args, /* operation arguments */
820 int *indexp, /* out: leaf entry index */
821 xfs_da_state_t *state) /* state to fill in */
822{
Barry Naujok6a178102008-05-21 16:42:05 +1000823 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000824 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
825 state);
826 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
827}
828
829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * Move count leaf entries from source to destination leaf.
831 * Log entries and headers. Stale entries are preserved.
832 */
833static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000834xfs_dir3_leafn_moveents(
835 xfs_da_args_t *args, /* operation arguments */
836 struct xfs_buf *bp_s, /* source */
837 struct xfs_dir3_icleaf_hdr *shdr,
838 struct xfs_dir2_leaf_entry *sents,
839 int start_s,/* source leaf index */
840 struct xfs_buf *bp_d, /* destination */
841 struct xfs_dir3_icleaf_hdr *dhdr,
842 struct xfs_dir2_leaf_entry *dents,
843 int start_d,/* destination leaf index */
844 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Chinner24df33b2013-04-12 07:30:21 +1000846 struct xfs_trans *tp = args->trans;
847 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000849 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /*
852 * Silently return if nothing to do.
853 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000854 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
858 * If the destination index is not the end of the current
859 * destination leaf entries, open up a hole in the destination
860 * to hold the new entries.
861 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000862 if (start_d < dhdr->count) {
863 memmove(&dents[start_d + count], &dents[start_d],
864 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100865 xfs_dir3_leaf_log_ents(tp, args->dp, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000866 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 /*
869 * If the source has stale leaves, count the ones in the copy range
870 * so we can update the header correctly.
871 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000872 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int i; /* temp leaf index */
874
875 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000876 if (sents[i].address ==
877 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 stale++;
879 }
880 } else
881 stale = 0;
882 /*
883 * Copy the leaf entries from source to destination.
884 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000885 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100887 xfs_dir3_leaf_log_ents(tp, args->dp, bp_d,
888 start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * If there are source entries after the ones we copied,
892 * delete the ones we copied by sliding the next ones down.
893 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000894 if (start_s + count < shdr->count) {
895 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100897 xfs_dir3_leaf_log_ents(tp, args->dp, bp_s,
898 start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Update the headers and log them.
903 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000904 shdr->count -= count;
905 shdr->stale -= stale;
906 dhdr->count += count;
907 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/*
911 * Determine the sort order of two leaf blocks.
912 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
913 */
914int /* sort order */
915xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100916 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000917 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
918 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
921 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
922 struct xfs_dir2_leaf_entry *ents1;
923 struct xfs_dir2_leaf_entry *ents2;
924 struct xfs_dir3_icleaf_hdr hdr1;
925 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Dave Chinner01ba43b2013-10-29 22:11:52 +1100927 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
928 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100929 ents1 = dp->d_ops->leaf_ents_p(leaf1);
930 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000931
932 if (hdr1.count > 0 && hdr2.count > 0 &&
933 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
934 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
935 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 1;
937 return 0;
938}
939
940/*
941 * Rebalance leaf entries between two leaf blocks.
942 * This is actually only called when the second block is new,
943 * though the code deals with the general case.
944 * A new entry will be inserted in one of the blocks, and that
945 * entry is taken into account when balancing.
946 */
947static void
948xfs_dir2_leafn_rebalance(
949 xfs_da_state_t *state, /* btree cursor */
950 xfs_da_state_blk_t *blk1, /* first btree block */
951 xfs_da_state_blk_t *blk2) /* second btree block */
952{
953 xfs_da_args_t *args; /* operation arguments */
954 int count; /* count (& direction) leaves */
955 int isleft; /* new goes in left leaf */
956 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
957 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
958 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000959#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 int oldstale; /* old count of stale leaves */
961#endif
962 int oldsum; /* old total leaf count */
963 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000964 struct xfs_dir2_leaf_entry *ents1;
965 struct xfs_dir2_leaf_entry *ents2;
966 struct xfs_dir3_icleaf_hdr hdr1;
967 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100968 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 args = state->args;
971 /*
972 * If the block order is wrong, swap the arguments.
973 */
Dave Chinner41419562013-10-29 22:11:50 +1100974 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_da_state_blk_t *tmp; /* temp for block swap */
976
977 tmp = blk1;
978 blk1 = blk2;
979 blk2 = tmp;
980 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000981 leaf1 = blk1->bp->b_addr;
982 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100983 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
984 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100985 ents1 = dp->d_ops->leaf_ents_p(leaf1);
986 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000987
988 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000989#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991#endif
992 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * If the old leaf count was odd then the new one will be even,
996 * so we need to divide the new count evenly.
997 */
998 if (oldsum & 1) {
999 xfs_dahash_t midhash; /* middle entry hash value */
1000
Dave Chinner24df33b2013-04-12 07:30:21 +10001001 if (mid >= hdr1.count)
1002 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001004 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 isleft = args->hashval <= midhash;
1006 }
1007 /*
1008 * If the old count is even then the new count is odd, so there's
1009 * no preferred side for the new entry.
1010 * Pick the left one.
1011 */
1012 else
1013 isleft = 1;
1014 /*
1015 * Calculate moved entry count. Positive means left-to-right,
1016 * negative means right-to-left. Then move the entries.
1017 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001018 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001020 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1021 hdr1.count - count, blk2->bp,
1022 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001024 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1025 blk1->bp, &hdr1, ents1,
1026 hdr1.count, count);
1027
1028 ASSERT(hdr1.count + hdr2.count == oldsum);
1029 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1030
1031 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001032 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1033 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinner41419562013-10-29 22:11:50 +11001034 xfs_dir3_leaf_log_header(args->trans, dp, blk1->bp);
1035 xfs_dir3_leaf_log_header(args->trans, dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001036
Dave Chinner41419562013-10-29 22:11:50 +11001037 xfs_dir3_leaf_check(dp, blk1->bp);
1038 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 /*
1041 * Mark whether we're inserting into the old or new leaf.
1042 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001043 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001045 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 state->inleaf = !swap;
1047 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001048 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * Adjust the expected index for insertion.
1051 */
1052 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001053 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001054
1055 /*
1056 * Finally sanity check just to make sure we are not returning a
1057 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Dave Chinner41419562013-10-29 22:11:50 +11001059 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 state->inleaf = 1;
1061 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001062 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001063 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001064 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066}
1067
Dave Chinner20252072012-11-12 22:54:13 +11001068static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001069xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001070 xfs_da_args_t *args,
1071 struct xfs_dir2_data_hdr *hdr,
1072 struct xfs_dir2_free *free,
1073 xfs_dir2_db_t fdb,
1074 int findex,
1075 struct xfs_buf *fbp,
1076 int longest)
1077{
1078 struct xfs_trans *tp = args->trans;
1079 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001080 __be16 *bests;
1081 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001082 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001083
Dave Chinner01ba43b2013-10-29 22:11:52 +11001084 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001085 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001086 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001087 /*
1088 * Data block is not empty, just set the free entry to the new
1089 * value.
1090 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001091 bests[findex] = cpu_to_be16(longest);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001092 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001093 return 0;
1094 }
1095
1096 /* One less used entry in the free table. */
1097 freehdr.nused--;
1098
1099 /*
1100 * If this was the last entry in the table, we can trim the table size
1101 * back. There might be other entries at the end referring to
1102 * non-existent data blocks, get those too.
1103 */
1104 if (findex == freehdr.nvalid - 1) {
1105 int i; /* free entry index */
1106
1107 for (i = findex - 1; i >= 0; i--) {
1108 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1109 break;
1110 }
1111 freehdr.nvalid = i + 1;
1112 logfree = 0;
1113 } else {
1114 /* Not the last entry, just punch it out. */
1115 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001116 logfree = 1;
1117 }
1118
Dave Chinner01ba43b2013-10-29 22:11:52 +11001119 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001120 xfs_dir2_free_log_header(tp, dp, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001121
1122 /*
1123 * If there are no useful entries left in the block, get rid of the
1124 * block if we can.
1125 */
1126 if (!freehdr.nused) {
1127 int error;
1128
1129 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1130 if (error == 0) {
1131 fbp = NULL;
1132 logfree = 0;
1133 } else if (error != ENOSPC || args->total != 0)
1134 return error;
1135 /*
1136 * It's possible to get ENOSPC if there is no
1137 * space reservation. In this case some one
1138 * else will eventually get rid of this block.
1139 */
1140 }
1141
Dave Chinner20252072012-11-12 22:54:13 +11001142 /* Log the free entry that changed, unless we got rid of it. */
1143 if (logfree)
Dave Chinner24dd0f52013-10-30 13:48:41 -05001144 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001145 return 0;
1146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/*
1149 * Remove an entry from a node directory.
1150 * This removes the leaf entry and the data entry,
1151 * and updates the free block if necessary.
1152 */
1153static int /* error */
1154xfs_dir2_leafn_remove(
1155 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001156 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int index, /* leaf entry index */
1158 xfs_da_state_blk_t *dblk, /* data block */
1159 int *rval) /* resulting block needs join */
1160{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001161 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001163 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 xfs_dir2_data_entry_t *dep; /* data block entry */
1165 xfs_inode_t *dp; /* incore directory inode */
1166 xfs_dir2_leaf_t *leaf; /* leaf structure */
1167 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1168 int longest; /* longest data free entry */
1169 int off; /* data block entry offset */
1170 xfs_mount_t *mp; /* filesystem mount point */
1171 int needlog; /* need to log data header */
1172 int needscan; /* need to rescan data frees */
1173 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001174 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001175 struct xfs_dir3_icleaf_hdr leafhdr;
1176 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001178 trace_xfs_dir2_leafn_remove(args, index);
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 dp = args->dp;
1181 tp = args->trans;
1182 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001183 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001184 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001185 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 /*
1188 * Point to the entry we're removing.
1189 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001190 lep = &ents[index];
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /*
1193 * Extract the data block and offset from the entry.
1194 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001195 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001197 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * Kill the leaf entry by marking it stale.
1202 * Log the leaf block changes.
1203 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001204 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001205 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinner41419562013-10-29 22:11:50 +11001206 xfs_dir3_leaf_log_header(tp, dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001207
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001208 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner41419562013-10-29 22:11:50 +11001209 xfs_dir3_leaf_log_ents(tp, dp, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Make the data entry free. Keep track of the longest freespace
1213 * in the data block in case it changes.
1214 */
1215 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001216 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001217 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001218 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001219 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 needlog = needscan = 0;
Dave Chinner2ca98772013-10-29 22:11:49 +11001221 xfs_dir2_data_make_free(tp, dp, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001222 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 /*
1224 * Rescan the data block freespaces for bestfree.
1225 * Log the data block header if needed.
1226 */
1227 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001228 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (needlog)
Dave Chinner2ca98772013-10-29 22:11:49 +11001230 xfs_dir2_data_log_header(tp, dp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001231 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * If the longest data block freespace changes, need to update
1234 * the corresponding freeblock entry.
1235 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001236 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001238 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 xfs_dir2_db_t fdb; /* freeblock block number */
1240 int findex; /* index in freeblock entries */
1241 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /*
1244 * Convert the data block number to a free block,
1245 * read in the free block.
1246 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001247 fdb = dp->d_ops->db_to_fdb(mp, db);
Dave Chinner20252072012-11-12 22:54:13 +11001248 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1249 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001250 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001252 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001253#ifdef DEBUG
1254 {
1255 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001256 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001257 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(mp) *
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001258 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1259 }
1260#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 /*
1262 * Calculate which entry we need to fix.
1263 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001264 findex = dp->d_ops->db_to_fdindex(mp, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001265 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * If the data block is now empty we can get rid of it
1268 * (usually).
1269 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001270 if (longest == mp->m_dirblksize -
Dave Chinner2ca98772013-10-29 22:11:49 +11001271 dp->d_ops->data_entry_offset()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * Try to punch out the data block.
1274 */
1275 error = xfs_dir2_shrink_inode(args, db, dbp);
1276 if (error == 0) {
1277 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001278 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280 /*
1281 * We can get ENOSPC if there's no space reservation.
1282 * In this case just drop the buffer and some one else
1283 * will eventually get rid of the empty block.
1284 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001285 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return error;
1287 }
1288 /*
1289 * If we got rid of the data block, we can eliminate that entry
1290 * in the free block.
1291 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001292 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001293 fdb, findex, fbp, longest);
1294 if (error)
1295 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Dave Chinner20252072012-11-12 22:54:13 +11001297
Dave Chinner41419562013-10-29 22:11:50 +11001298 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001300 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * to justify trying to join it with a neighbor.
1302 */
Dave Chinner41419562013-10-29 22:11:50 +11001303 *rval = (dp->d_ops->leaf_hdr_size() +
Dave Chinner24df33b2013-04-12 07:30:21 +10001304 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 mp->m_dir_magicpct;
1306 return 0;
1307}
1308
1309/*
1310 * Split the leaf entries in the old block into old and new blocks.
1311 */
1312int /* error */
1313xfs_dir2_leafn_split(
1314 xfs_da_state_t *state, /* btree cursor */
1315 xfs_da_state_blk_t *oldblk, /* original block */
1316 xfs_da_state_blk_t *newblk) /* newly created block */
1317{
1318 xfs_da_args_t *args; /* operation arguments */
1319 xfs_dablk_t blkno; /* new leaf block number */
1320 int error; /* error return value */
1321 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner41419562013-10-29 22:11:50 +11001322 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 /*
1325 * Allocate space for a new leaf node.
1326 */
1327 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001328 dp = args->dp;
1329 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ASSERT(args != NULL);
1331 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1332 error = xfs_da_grow_inode(args, &blkno);
1333 if (error) {
1334 return error;
1335 }
1336 /*
1337 * Initialize the new leaf block.
1338 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001339 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
1340 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1341 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 newblk->blkno = blkno;
1345 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1346 /*
1347 * Rebalance the entries across the two leaves, link the new
1348 * block into the leaves.
1349 */
1350 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001351 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (error) {
1353 return error;
1354 }
1355 /*
1356 * Insert the new entry in the correct block.
1357 */
1358 if (state->inleaf)
1359 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1360 else
1361 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1362 /*
1363 * Update last hashval in each block since we added the name.
1364 */
Dave Chinner41419562013-10-29 22:11:50 +11001365 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1366 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1367 xfs_dir3_leaf_check(dp, oldblk->bp);
1368 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return error;
1370}
1371
1372/*
1373 * Check a leaf block and its neighbors to see if the block should be
1374 * collapsed into one or the other neighbor. Always keep the block
1375 * with the smaller block number.
1376 * If the current block is over 50% full, don't try to join it, return 0.
1377 * If the block is empty, fill in the state structure and return 2.
1378 * If it can be collapsed, fill in the state structure and return 1.
1379 * If nothing can be done, return 0.
1380 */
1381int /* error */
1382xfs_dir2_leafn_toosmall(
1383 xfs_da_state_t *state, /* btree cursor */
1384 int *action) /* resulting action to take */
1385{
1386 xfs_da_state_blk_t *blk; /* leaf block */
1387 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001388 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int bytes; /* bytes in use */
1390 int count; /* leaf live entry count */
1391 int error; /* error return value */
1392 int forward; /* sibling block direction */
1393 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 xfs_dir2_leaf_t *leaf; /* leaf structure */
1395 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001396 struct xfs_dir3_icleaf_hdr leafhdr;
1397 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001398 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Check for the degenerate case of the block being over 50% full.
1402 * If so, it's not worth even looking to see if we might be able
1403 * to coalesce with a sibling.
1404 */
1405 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001406 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001407 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001408 ents = dp->d_ops->leaf_ents_p(leaf);
1409 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001410
1411 count = leafhdr.count - leafhdr.stale;
Dave Chinner41419562013-10-29 22:11:50 +11001412 bytes = dp->d_ops->leaf_hdr_size() + count * sizeof(ents[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (bytes > (state->blocksize >> 1)) {
1414 /*
1415 * Blk over 50%, don't try to join.
1416 */
1417 *action = 0;
1418 return 0;
1419 }
1420 /*
1421 * Check for the degenerate case of the block being empty.
1422 * If the block is empty, we'll simply delete it, no need to
1423 * coalesce it with a sibling block. We choose (arbitrarily)
1424 * to merge with the forward block unless it is NULL.
1425 */
1426 if (count == 0) {
1427 /*
1428 * Make altpath point to the block we want to keep and
1429 * path point to the block we want to drop (this one).
1430 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001431 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001433 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 &rval);
1435 if (error)
1436 return error;
1437 *action = rval ? 2 : 0;
1438 return 0;
1439 }
1440 /*
1441 * Examine each sibling block to see if we can coalesce with
1442 * at least 25% free space to spare. We need to figure out
1443 * whether to merge with the forward or the backward block.
1444 * We prefer coalescing with the lower numbered sibling so as
1445 * to shrink a directory over time.
1446 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001447 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001449 struct xfs_dir3_icleaf_hdr hdr2;
1450
1451 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (blkno == 0)
1453 continue;
1454 /*
1455 * Read the sibling leaf block.
1456 */
Dave Chinner41419562013-10-29 22:11:50 +11001457 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001458 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001459 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /*
1463 * Count bytes in the two blocks combined.
1464 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001465 count = leafhdr.count - leafhdr.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001467
Dave Chinner1d9025e2012-06-22 18:50:14 +10001468 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001469 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001470 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001471 count += hdr2.count - hdr2.stale;
1472 bytes -= count * sizeof(ents[0]);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /*
1475 * Fits with at least 25% to spare.
1476 */
1477 if (bytes >= 0)
1478 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001479 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 /*
1482 * Didn't like either block, give up.
1483 */
1484 if (i >= 2) {
1485 *action = 0;
1486 return 0;
1487 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /*
1490 * Make altpath point to the block we want to keep (the lower
1491 * numbered block) and path point to the block we want to drop.
1492 */
1493 memcpy(&state->altpath, &state->path, sizeof(state->path));
1494 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001495 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 &rval);
1497 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001498 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 &rval);
1500 if (error) {
1501 return error;
1502 }
1503 *action = rval ? 0 : 1;
1504 return 0;
1505}
1506
1507/*
1508 * Move all the leaf entries from drop_blk to save_blk.
1509 * This is done as part of a join operation.
1510 */
1511void
1512xfs_dir2_leafn_unbalance(
1513 xfs_da_state_t *state, /* cursor */
1514 xfs_da_state_blk_t *drop_blk, /* dead block */
1515 xfs_da_state_blk_t *save_blk) /* surviving block */
1516{
1517 xfs_da_args_t *args; /* operation arguments */
1518 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1519 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001520 struct xfs_dir3_icleaf_hdr savehdr;
1521 struct xfs_dir3_icleaf_hdr drophdr;
1522 struct xfs_dir2_leaf_entry *sents;
1523 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001524 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 args = state->args;
1527 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1528 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001529 drop_leaf = drop_blk->bp->b_addr;
1530 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001531
Dave Chinner01ba43b2013-10-29 22:11:52 +11001532 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1533 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1534 sents = dp->d_ops->leaf_ents_p(save_leaf);
1535 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /*
1538 * If there are any stale leaf entries, take this opportunity
1539 * to purge them.
1540 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001541 if (drophdr.stale)
1542 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1543 if (savehdr.stale)
1544 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 /*
1547 * Move the entries from drop to the appropriate end of save.
1548 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001549 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001550 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001551 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1552 save_blk->bp, &savehdr, sents, 0,
1553 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 else
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,
1557 savehdr.count, drophdr.count);
1558 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1559
1560 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001561 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1562 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinner41419562013-10-29 22:11:50 +11001563 xfs_dir3_leaf_log_header(args->trans, dp, save_blk->bp);
1564 xfs_dir3_leaf_log_header(args->trans, dp, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001565
Dave Chinner41419562013-10-29 22:11:50 +11001566 xfs_dir3_leaf_check(dp, save_blk->bp);
1567 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
1571 * Top-level node form directory addname routine.
1572 */
1573int /* error */
1574xfs_dir2_node_addname(
1575 xfs_da_args_t *args) /* operation arguments */
1576{
1577 xfs_da_state_blk_t *blk; /* leaf block for insert */
1578 int error; /* error return value */
1579 int rval; /* sub-return value */
1580 xfs_da_state_t *state; /* btree cursor */
1581
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001582 trace_xfs_dir2_node_addname(args);
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /*
1585 * Allocate and initialize the state (btree cursor).
1586 */
1587 state = xfs_da_state_alloc();
1588 state->args = args;
1589 state->mp = args->dp->i_mount;
1590 state->blocksize = state->mp->m_dirblksize;
1591 state->node_ents = state->mp->m_dir_node_ents;
1592 /*
1593 * Look up the name. We're not supposed to find it, but
1594 * this gives us the insertion point.
1595 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001596 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (error)
1598 rval = error;
1599 if (rval != ENOENT) {
1600 goto done;
1601 }
1602 /*
1603 * Add the data entry to a data block.
1604 * Extravalid is set to a freeblock found by lookup.
1605 */
1606 rval = xfs_dir2_node_addname_int(args,
1607 state->extravalid ? &state->extrablk : NULL);
1608 if (rval) {
1609 goto done;
1610 }
1611 blk = &state->path.blk[state->path.active - 1];
1612 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1613 /*
1614 * Add the new leaf entry.
1615 */
1616 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1617 if (rval == 0) {
1618 /*
1619 * It worked, fix the hash values up the btree.
1620 */
Barry Naujok6a178102008-05-21 16:42:05 +10001621 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001622 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
1624 /*
1625 * It didn't work, we need to split the leaf block.
1626 */
1627 if (args->total == 0) {
1628 ASSERT(rval == ENOSPC);
1629 goto done;
1630 }
1631 /*
1632 * Split the leaf block and insert the new entry.
1633 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001634 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636done:
1637 xfs_da_state_free(state);
1638 return rval;
1639}
1640
1641/*
1642 * Add the data entry for a node-format directory name addition.
1643 * The leaf entry is added in xfs_dir2_leafn_add.
1644 * We may enter with a freespace block that the lookup found.
1645 */
1646static int /* error */
1647xfs_dir2_node_addname_int(
1648 xfs_da_args_t *args, /* operation arguments */
1649 xfs_da_state_blk_t *fblk) /* optional freespace block */
1650{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001651 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001653 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1655 xfs_inode_t *dp; /* incore directory inode */
1656 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1657 int error; /* error return value */
1658 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001659 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int findex; /* freespace entry index */
1661 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1662 xfs_dir2_db_t ifbno; /* initial freespace block no */
1663 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1664 int length; /* length of the new entry */
1665 int logfree; /* need to log free entry */
1666 xfs_mount_t *mp; /* filesystem mount point */
1667 int needlog; /* need to log data header */
1668 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001669 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001671 __be16 *bests;
1672 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001673 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 dp = args->dp;
1676 mp = dp->i_mount;
1677 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001678 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * If we came in with a freespace block that means that lookup
1681 * found an entry with our hash value. This is the freespace
1682 * block for that data entry.
1683 */
1684 if (fblk) {
1685 fbp = fblk->bp;
1686 /*
1687 * Remember initial freespace block number.
1688 */
1689 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001690 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001692 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001693 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * This means the free entry showed that the data block had
1697 * space for our entry, so we remembered it.
1698 * Use that data block.
1699 */
1700 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001701 ASSERT(findex < freehdr.nvalid);
1702 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1703 ASSERT(be16_to_cpu(bests[findex]) >= length);
1704 dbno = freehdr.firstdb + findex;
1705 } else {
1706 /*
1707 * The data block looked at didn't have enough room.
1708 * We'll start at the beginning of the freespace entries.
1709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 dbno = -1;
1711 findex = 0;
1712 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001713 } else {
1714 /*
1715 * Didn't come in with a freespace block, so no data block.
1716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 ifbno = dbno = -1;
1718 fbp = NULL;
1719 findex = 0;
1720 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /*
1723 * If we don't have a data block yet, we're going to scan the
1724 * freespace blocks looking for one. Figure out what the
1725 * highest freespace block number is.
1726 */
1727 if (dbno == -1) {
1728 xfs_fileoff_t fo; /* freespace block number */
1729
1730 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1731 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001732 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 fbno = ifbno;
1734 }
1735 /*
1736 * While we haven't identified a data block, search the freeblock
1737 * data for a good data block. If we find a null freeblock entry,
1738 * indicating a hole in the data blocks, remember that.
1739 */
1740 while (dbno == -1) {
1741 /*
1742 * If we don't have a freeblock in hand, get the next one.
1743 */
1744 if (fbp == NULL) {
1745 /*
1746 * Happens the first time through unless lookup gave
1747 * us a freespace block to start with.
1748 */
1749 if (++fbno == 0)
1750 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1751 /*
1752 * If it's ifbno we already looked at it.
1753 */
1754 if (fbno == ifbno)
1755 fbno++;
1756 /*
1757 * If it's off the end we're done.
1758 */
1759 if (fbno >= lastfbno)
1760 break;
1761 /*
1762 * Read the block. There can be holes in the
1763 * freespace blocks, so this might not succeed.
1764 * This should be really rare, so there's no reason
1765 * to avoid it.
1766 */
Dave Chinner20252072012-11-12 22:54:13 +11001767 error = xfs_dir2_free_try_read(tp, dp,
1768 xfs_dir2_db_to_da(mp, fbno),
1769 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001772 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001774 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 findex = 0;
1776 }
1777 /*
1778 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001779 *
1780 * The bests initialisation should be where the bufer is read in
1781 * the above branch. But gcc is too stupid to realise that bests
1782 * and the freehdr are actually initialised if they are placed
1783 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001785 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001786 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001787 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1788 be16_to_cpu(bests[findex]) >= length)
1789 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 else {
1791 /*
1792 * Are we done with the freeblock?
1793 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001794 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Drop the block.
1797 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001798 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 fbp = NULL;
1800 if (fblk && fblk->bp)
1801 fblk->bp = NULL;
1802 }
1803 }
1804 }
1805 /*
1806 * If we don't have a data block, we need to allocate one and make
1807 * the freespace entries refer to it.
1808 */
1809 if (unlikely(dbno == -1)) {
1810 /*
1811 * Not allowed to allocate, return failure.
1812 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001813 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Allocate and initialize the new data block.
1818 */
1819 if (unlikely((error = xfs_dir2_grow_inode(args,
1820 XFS_DIR2_DATA_SPACE,
1821 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001822 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * If (somehow) we have a freespace block, get rid of it.
1827 */
1828 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001829 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (fblk && fblk->bp)
1831 fblk->bp = NULL;
1832
1833 /*
1834 * Get the freespace block corresponding to the data block
1835 * that was just allocated.
1836 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001837 fbno = dp->d_ops->db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001838 error = xfs_dir2_free_try_read(tp, dp,
1839 xfs_dir2_db_to_da(mp, fbno),
1840 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * If there wasn't a freespace block, the read will
1846 * return a NULL fbp. Allocate and initialize a new one.
1847 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001848 if (!fbp) {
1849 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1850 &fbno);
1851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Dave Chinner24dd0f52013-10-30 13:48:41 -05001854 if (unlikely(dp->d_ops->db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001855 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001856 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001857 " data block %lld, got %lld ifbno %llu lastfbno %d",
1858 __func__, (unsigned long long)dp->i_ino,
Dave Chinner24dd0f52013-10-30 13:48:41 -05001859 (long long)dp->d_ops->db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 (long long)dbno, (long long)fbno,
1861 (unsigned long long)ifbno, lastfbno);
1862 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001863 xfs_alert(mp,
1864 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 fblk,
1866 (unsigned long long)fblk->blkno,
1867 fblk->index,
1868 fblk->magic);
1869 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001870 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1873 XFS_ERRLEVEL_LOW, mp);
1874 return XFS_ERROR(EFSCORRUPTED);
1875 }
1876
1877 /*
1878 * Get a buffer for the new block.
1879 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001880 error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001881 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001883 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001884 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001885 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001888 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001890 freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
Dave Chinner24dd0f52013-10-30 13:48:41 -05001891 dp->d_ops->free_max_bests(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001893 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001894 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001895 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
1898 /*
1899 * Set the freespace block index from the data block number.
1900 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001901 findex = dp->d_ops->db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 /*
1903 * If it's after the end of the current entries in the
1904 * freespace block, extend that table.
1905 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001906 if (findex >= freehdr.nvalid) {
Dave Chinner24dd0f52013-10-30 13:48:41 -05001907 ASSERT(findex < dp->d_ops->free_max_bests(mp));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001908 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * Tag new entry so nused will go up.
1911 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001912 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 /*
1915 * If this entry was for an empty data block
1916 * (this should always be true) then update the header.
1917 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001918 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1919 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001920 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001921 xfs_dir2_free_log_header(tp, dp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923 /*
1924 * Update the real value in the table.
1925 * We haven't allocated the data entry yet so this will
1926 * change again.
1927 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001928 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001929 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001930 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 logfree = 1;
1932 }
1933 /*
1934 * We had a data block so we don't have to make a new one.
1935 */
1936 else {
1937 /*
1938 * If just checking, we succeeded.
1939 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001940 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /*
1944 * Read the data block in.
1945 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001946 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001947 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001950 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001951 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 logfree = 0;
1953 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001954 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /*
1956 * Point to the existing unused space.
1957 */
1958 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001959 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 needscan = needlog = 0;
1961 /*
1962 * Mark the first part of the unused space, inuse for us.
1963 */
Dave Chinner2ca98772013-10-29 22:11:49 +11001964 xfs_dir2_data_use_free(tp, dp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001965 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 &needlog, &needscan);
1967 /*
1968 * Fill in the new entry and log it.
1969 */
1970 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001971 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dep->namelen = args->namelen;
1973 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001974 dp->d_ops->data_put_ftype(dep, args->filetype);
1975 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001976 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001977 xfs_dir2_data_log_entry(tp, dp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /*
1979 * Rescan the block for bestfree if needed.
1980 */
1981 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001982 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /*
1984 * Log the data block header if needed.
1985 */
1986 if (needlog)
Dave Chinner2ca98772013-10-29 22:11:49 +11001987 xfs_dir2_data_log_header(tp, dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * If the freespace entry is now wrong, update it.
1990 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001991 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11001992 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1993 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 logfree = 1;
1995 }
1996 /*
1997 * Log the freespace entry if needed.
1998 */
1999 if (logfree)
Dave Chinner24dd0f52013-10-30 13:48:41 -05002000 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 * Return the data block and offset in args, then drop the data block.
2003 */
2004 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002005 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
2007}
2008
2009/*
2010 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002011 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * The only real output is the inode number of the entry.
2013 */
2014int /* error */
2015xfs_dir2_node_lookup(
2016 xfs_da_args_t *args) /* operation arguments */
2017{
2018 int error; /* error return value */
2019 int i; /* btree level */
2020 int rval; /* operation return value */
2021 xfs_da_state_t *state; /* btree cursor */
2022
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002023 trace_xfs_dir2_node_lookup(args);
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 /*
2026 * Allocate and initialize the btree cursor.
2027 */
2028 state = xfs_da_state_alloc();
2029 state->args = args;
2030 state->mp = args->dp->i_mount;
2031 state->blocksize = state->mp->m_dirblksize;
2032 state->node_ents = state->mp->m_dir_node_ents;
2033 /*
2034 * Fill in the path to the entry in the cursor.
2035 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002036 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (error)
2038 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10002039 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
2040 /* If a CI match, dup the actual name and return EEXIST */
2041 xfs_dir2_data_entry_t *dep;
2042
Dave Chinner1d9025e2012-06-22 18:50:14 +10002043 dep = (xfs_dir2_data_entry_t *)
2044 ((char *)state->extrablk.bp->b_addr +
2045 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002046 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
2049 * Release the btree blocks and leaf block.
2050 */
2051 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002052 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 state->path.blk[i].bp = NULL;
2054 }
2055 /*
2056 * Release the data block if we have it.
2057 */
2058 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002059 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 state->extrablk.bp = NULL;
2061 }
2062 xfs_da_state_free(state);
2063 return rval;
2064}
2065
2066/*
2067 * Remove an entry from a node-format directory.
2068 */
2069int /* error */
2070xfs_dir2_node_removename(
2071 xfs_da_args_t *args) /* operation arguments */
2072{
2073 xfs_da_state_blk_t *blk; /* leaf block */
2074 int error; /* error return value */
2075 int rval; /* operation return value */
2076 xfs_da_state_t *state; /* btree cursor */
2077
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002078 trace_xfs_dir2_node_removename(args);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /*
2081 * Allocate and initialize the btree cursor.
2082 */
2083 state = xfs_da_state_alloc();
2084 state->args = args;
2085 state->mp = args->dp->i_mount;
2086 state->blocksize = state->mp->m_dirblksize;
2087 state->node_ents = state->mp->m_dir_node_ents;
2088 /*
2089 * Look up the entry we're deleting, set up the cursor.
2090 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002091 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002092 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Didn't find it, upper layer screwed up.
2096 */
2097 if (rval != EEXIST) {
2098 xfs_da_state_free(state);
2099 return rval;
2100 }
2101 blk = &state->path.blk[state->path.active - 1];
2102 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2103 ASSERT(state->extravalid);
2104 /*
2105 * Remove the leaf and data entries.
2106 * Extrablk refers to the data block.
2107 */
2108 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2109 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002110 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /*
2113 * Fix the hash values up the btree.
2114 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002115 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /*
2117 * If we need to join leaf blocks, do it.
2118 */
2119 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002120 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /*
2122 * If no errors so far, try conversion to leaf format.
2123 */
2124 if (!error)
2125 error = xfs_dir2_node_to_leaf(state);
2126 xfs_da_state_free(state);
2127 return error;
2128}
2129
2130/*
2131 * Replace an entry's inode number in a node-format directory.
2132 */
2133int /* error */
2134xfs_dir2_node_replace(
2135 xfs_da_args_t *args) /* operation arguments */
2136{
2137 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002138 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 xfs_dir2_data_entry_t *dep; /* data entry changed */
2140 int error; /* error return value */
2141 int i; /* btree level */
2142 xfs_ino_t inum; /* new inode number */
2143 xfs_dir2_leaf_t *leaf; /* leaf structure */
2144 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2145 int rval; /* internal return value */
2146 xfs_da_state_t *state; /* btree cursor */
2147
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002148 trace_xfs_dir2_node_replace(args);
2149
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * Allocate and initialize the btree cursor.
2152 */
2153 state = xfs_da_state_alloc();
2154 state->args = args;
2155 state->mp = args->dp->i_mount;
2156 state->blocksize = state->mp->m_dirblksize;
2157 state->node_ents = state->mp->m_dir_node_ents;
2158 inum = args->inumber;
2159 /*
2160 * Lookup the entry to change in the btree.
2161 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002162 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (error) {
2164 rval = error;
2165 }
2166 /*
2167 * It should be found, since the vnodeops layer has looked it up
2168 * and locked it. But paranoia is good.
2169 */
2170 if (rval == EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002171 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * Find the leaf entry.
2174 */
2175 blk = &state->path.blk[state->path.active - 1];
2176 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002177 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002178 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002179 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 ASSERT(state->extravalid);
2181 /*
2182 * Point to the data entry.
2183 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002184 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002185 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2186 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002188 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002189 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002190 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /*
2192 * Fill in the new inode number and log the entry.
2193 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002194 dep->inumber = cpu_to_be64(inum);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002195 args->dp->d_ops->data_put_ftype(dep, args->filetype);
2196 xfs_dir2_data_log_entry(args->trans, args->dp,
2197 state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 rval = 0;
2199 }
2200 /*
2201 * Didn't find it, and we're holding a data block. Drop it.
2202 */
2203 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002204 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 state->extrablk.bp = NULL;
2206 }
2207 /*
2208 * Release all the buffers in the cursor.
2209 */
2210 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002211 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 state->path.blk[i].bp = NULL;
2213 }
2214 xfs_da_state_free(state);
2215 return rval;
2216}
2217
2218/*
2219 * Trim off a trailing empty freespace block.
2220 * Return (in rvalp) 1 if we did it, 0 if not.
2221 */
2222int /* error */
2223xfs_dir2_node_trim_free(
2224 xfs_da_args_t *args, /* operation arguments */
2225 xfs_fileoff_t fo, /* free block number */
2226 int *rvalp) /* out: did something */
2227{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002228 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 xfs_inode_t *dp; /* incore directory inode */
2230 int error; /* error return code */
2231 xfs_dir2_free_t *free; /* freespace structure */
2232 xfs_mount_t *mp; /* filesystem mount point */
2233 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002234 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
2236 dp = args->dp;
2237 mp = dp->i_mount;
2238 tp = args->trans;
2239 /*
2240 * Read the freespace block.
2241 */
Dave Chinner20252072012-11-12 22:54:13 +11002242 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /*
2246 * There can be holes in freespace. If fo is a hole, there's
2247 * nothing to do.
2248 */
Dave Chinner20252072012-11-12 22:54:13 +11002249 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002251 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002252 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /*
2255 * If there are used entries, there's nothing to do.
2256 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002257 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002258 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 *rvalp = 0;
2260 return 0;
2261 }
2262 /*
2263 * Blow the block away.
2264 */
2265 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002266 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 bp))) {
2268 /*
2269 * Can't fail with ENOSPC since that only happens with no
2270 * space reservation, when breaking up an extent into two
2271 * pieces. This is the last block of an extent.
2272 */
2273 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002274 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return error;
2276 }
2277 /*
2278 * Return that we succeeded.
2279 */
2280 *rvalp = 1;
2281 return 0;
2282}