blob: 3b70d56f4e58c61789839f25d9fc2ed73b1a05ad [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
Eric Sandeence5028c2014-02-27 15:23:10 +1100118 if (xfs_sb_version_hascrc(&mp->m_sb) &&
119 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
120 xfs_buf_ioerror(bp, EFSBADCRC);
121 else if (!xfs_dir3_free_verify(bp))
Dave Chinner20252072012-11-12 22:54:13 +1100122 xfs_buf_ioerror(bp, EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100123
124 if (bp->b_error)
125 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100126}
Dave Chinner20252072012-11-12 22:54:13 +1100127
Dave Chinner612cfbf2012-11-14 17:52:32 +1100128static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100129xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100130 struct xfs_buf *bp)
131{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100132 struct xfs_mount *mp = bp->b_target->bt_mount;
133 struct xfs_buf_log_item *bip = bp->b_fspriv;
134 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
135
136 if (!xfs_dir3_free_verify(bp)) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100137 xfs_buf_ioerror(bp, EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100138 xfs_verifier_error(bp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100139 return;
140 }
141
142 if (!xfs_sb_version_hascrc(&mp->m_sb))
143 return;
144
145 if (bip)
146 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
147
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100148 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100149}
150
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100151const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100152 .verify_read = xfs_dir3_free_read_verify,
153 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100154};
Dave Chinner20252072012-11-12 22:54:13 +1100155
Dave Chinner612cfbf2012-11-14 17:52:32 +1100156
Dave Chinner20252072012-11-12 22:54:13 +1100157static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100158__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100159 struct xfs_trans *tp,
160 struct xfs_inode *dp,
161 xfs_dablk_t fbno,
162 xfs_daddr_t mappedbno,
163 struct xfs_buf **bpp)
164{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100165 int err;
166
167 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100168 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100169
170 /* try read returns without an error or *bpp if it lands in a hole */
171 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100172 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100173 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100174}
175
176int
177xfs_dir2_free_read(
178 struct xfs_trans *tp,
179 struct xfs_inode *dp,
180 xfs_dablk_t fbno,
181 struct xfs_buf **bpp)
182{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100183 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100184}
185
186static int
187xfs_dir2_free_try_read(
188 struct xfs_trans *tp,
189 struct xfs_inode *dp,
190 xfs_dablk_t fbno,
191 struct xfs_buf **bpp)
192{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100193 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
194}
195
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100196static int
197xfs_dir3_free_get_buf(
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000198 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100199 xfs_dir2_db_t fbno,
200 struct xfs_buf **bpp)
201{
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000202 struct xfs_trans *tp = args->trans;
203 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100204 struct xfs_mount *mp = dp->i_mount;
205 struct xfs_buf *bp;
206 int error;
207 struct xfs_dir3_icfree_hdr hdr;
208
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000209 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100210 -1, &bp, XFS_DATA_FORK);
211 if (error)
212 return error;
213
Dave Chinner61fe1352013-04-03 16:11:30 +1100214 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100215 bp->b_ops = &xfs_dir3_free_buf_ops;
216
217 /*
218 * Initialize the new block to be empty, and remember
219 * its first slot as our empty slot.
220 */
Dave Chinnere400d272013-05-28 18:37:17 +1000221 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
222 memset(&hdr, 0, sizeof(hdr));
223
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100224 if (xfs_sb_version_hascrc(&mp->m_sb)) {
225 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
226
227 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000228
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100229 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
230 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
231 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000232 } else
233 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100234 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100235 *bpp = bp;
236 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
240 * Log entries from a freespace block.
241 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000242STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000244 struct xfs_trans *tp,
Dave Chinner24dd0f52013-10-30 13:48:41 -0500245 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000246 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int first, /* first entry to log */
248 int last) /* last entry to log */
249{
250 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100251 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dave Chinner1d9025e2012-06-22 18:50:14 +1000253 free = bp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -0500254 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100255 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
256 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000257 xfs_trans_log_buf(tp, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100258 (uint)((char *)&bests[first] - (char *)free),
259 (uint)((char *)&bests[last] - (char *)free +
260 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * Log header from a freespace block.
265 */
266static void
267xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000268 struct xfs_trans *tp,
Dave Chinner24dd0f52013-10-30 13:48:41 -0500269 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000270 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Dave Chinner836a94a2013-08-12 20:49:44 +1000272#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 xfs_dir2_free_t *free; /* freespace structure */
274
Dave Chinner1d9025e2012-06-22 18:50:14 +1000275 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100276 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
277 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000278#endif
Dave Chinner1c9a5b22013-10-30 09:15:02 +1100279 xfs_trans_log_buf(tp, bp, 0, dp->d_ops->free_hdr_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Convert a leaf-format directory to a node-format directory.
284 * We need to change the magic number of the leaf block, and copy
285 * the freespace table out of the leaf block into its own block.
286 */
287int /* error */
288xfs_dir2_leaf_to_node(
289 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000290 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 xfs_inode_t *dp; /* incore directory inode */
293 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000294 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 xfs_dir2_db_t fdb; /* freespace block number */
296 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100297 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int i; /* leaf freespace index */
299 xfs_dir2_leaf_t *leaf; /* leaf structure */
300 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
301 xfs_mount_t *mp; /* filesystem mount point */
302 int n; /* count of live freespc ents */
303 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100304 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100306 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000308 trace_xfs_dir2_leaf_to_node(args);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dp = args->dp;
311 mp = dp->i_mount;
312 tp = args->trans;
313 /*
314 * Add a freespace block to the directory.
315 */
316 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
317 return error;
318 }
Dave Chinner8c44a282014-06-06 15:04:41 +1000319 ASSERT(fdb == xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 /*
321 * Get the buffer for the new freespace block.
322 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000323 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100326
Dave Chinner1d9025e2012-06-22 18:50:14 +1000327 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100328 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000329 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000330 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100331 ASSERT(be32_to_cpu(ltp->bestcount) <=
332 (uint)dp->i_d.di_size / mp->m_dirblksize);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /*
335 * Copy freespace entries from the leaf block to the new block.
336 * Count active entries.
337 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100338 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500339 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100340 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100341 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100343 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100347 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100349 freehdr.nused = n;
350 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
351
Dave Chinner01ba43b2013-10-29 22:11:52 +1100352 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500353 xfs_dir2_free_log_bests(tp, dp, fbp, 0, freehdr.nvalid - 1);
354 xfs_dir2_free_log_header(tp, dp, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100355
Dave Chinner24df33b2013-04-12 07:30:21 +1000356 /*
357 * Converting the leaf to a leafnode is just a matter of changing the
358 * magic number and the ops. Do the change directly to the buffer as
359 * it's less work (and less code) than decoding the header to host
360 * format and back again.
361 */
362 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
363 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
364 else
365 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
366 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100367 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinner41419562013-10-29 22:11:50 +1100368 xfs_dir3_leaf_log_header(tp, dp, lbp);
369 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 0;
371}
372
373/*
374 * Add a leaf entry to a leaf block in a node-form directory.
375 * The other work necessary is done from the caller.
376 */
377static int /* error */
378xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000379 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 xfs_da_args_t *args, /* operation arguments */
381 int index) /* insertion pt for new entry */
382{
383 int compact; /* compacting stale leaves */
384 xfs_inode_t *dp; /* incore directory inode */
385 int highstale; /* next stale entry */
386 xfs_dir2_leaf_t *leaf; /* leaf structure */
387 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
388 int lfloghigh; /* high leaf entry logging */
389 int lfloglow; /* low leaf entry logging */
390 int lowstale; /* previous stale entry */
391 xfs_mount_t *mp; /* filesystem mount point */
392 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000393 struct xfs_dir3_icleaf_hdr leafhdr;
394 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000396 trace_xfs_dir2_leafn_add(args, index);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dp = args->dp;
399 mp = dp->i_mount;
400 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000401 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100402 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100403 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /*
406 * Quick check just to make sure we are not going to index
407 * into other peoples memory
408 */
409 if (index < 0)
410 return XFS_ERROR(EFSCORRUPTED);
411
412 /*
413 * If there are already the maximum number of leaf entries in
414 * the block, if there are no stale entries it won't fit.
415 * Caller will do a split. If there are stale entries we'll do
416 * a compact.
417 */
418
Dave Chinner41419562013-10-29 22:11:50 +1100419 if (leafhdr.count == dp->d_ops->leaf_max_ents(mp)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000420 if (!leafhdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return XFS_ERROR(ENOSPC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000422 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 } else
424 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000425 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
426 ASSERT(index == leafhdr.count ||
427 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Barry Naujok6a178102008-05-21 16:42:05 +1000429 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return 0;
431
432 /*
433 * Compact out all but one stale leaf entry. Leaves behind
434 * the entry closest to index.
435 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000436 if (compact)
437 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
438 &highstale, &lfloglow, &lfloghigh);
439 else if (leafhdr.stale) {
440 /*
441 * Set impossible logging indices for this case.
442 */
443 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 lfloghigh = -1;
445 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Insert the new entry, log everything.
449 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200451 highstale, &lfloglow, &lfloghigh);
452
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100453 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000454 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100455 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000456
Dave Chinner01ba43b2013-10-29 22:11:52 +1100457 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinner41419562013-10-29 22:11:50 +1100458 xfs_dir3_leaf_log_header(tp, dp, bp);
459 xfs_dir3_leaf_log_ents(tp, dp, bp, lfloglow, lfloghigh);
460 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
464#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100465static void
466xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100467 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100468 struct xfs_buf *bp,
469 xfs_dir2_db_t db)
470{
471 struct xfs_dir3_icfree_hdr hdr;
472
Dave Chinner01ba43b2013-10-29 22:11:52 +1100473 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100474
Dave Chinner24dd0f52013-10-30 13:48:41 -0500475 ASSERT((hdr.firstdb % dp->d_ops->free_max_bests(dp->i_mount)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100476 ASSERT(hdr.firstdb <= db);
477 ASSERT(db < hdr.firstdb + hdr.nvalid);
478}
479#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100480#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#endif /* DEBUG */
482
483/*
484 * Return the last hash value in the leaf.
485 * Stale entries are ok.
486 */
487xfs_dahash_t /* hash value */
488xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100489 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000490 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 int *count) /* count of entries in leaf */
492{
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 struct xfs_dir2_leaf *leaf = bp->b_addr;
494 struct xfs_dir2_leaf_entry *ents;
495 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dave Chinner01ba43b2013-10-29 22:11:52 +1100497 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000498
499 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
500 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000503 *count = leafhdr.count;
504 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000506
Dave Chinner41419562013-10-29 22:11:50 +1100507 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000508 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000512 * Look up a leaf entry for space to add a name in a node-format leaf block.
513 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000515STATIC int
516xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000517 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_da_args_t *args, /* operation arguments */
519 int *indexp, /* out: leaf entry index */
520 xfs_da_state_t *state) /* state to fill in */
521{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000522 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000523 xfs_dir2_db_t curdb = -1; /* current data block number */
524 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 xfs_inode_t *dp; /* incore directory inode */
526 int error; /* error return value */
527 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000528 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int index; /* leaf entry index */
530 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000531 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
533 xfs_mount_t *mp; /* filesystem mount point */
534 xfs_dir2_db_t newdb; /* new data block number */
535 xfs_dir2_db_t newfdb; /* new free block number */
536 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000537 struct xfs_dir2_leaf_entry *ents;
538 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 dp = args->dp;
541 tp = args->trans;
542 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000543 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100544 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100545 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000546
Dave Chinner41419562013-10-29 22:11:50 +1100547 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000548 ASSERT(leafhdr.count > 0);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /*
551 * Look up the hash value in the leaf entries.
552 */
553 index = xfs_dir2_leaf_search_hash(args, bp);
554 /*
555 * Do we have a buffer coming in?
556 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000557 if (state->extravalid) {
558 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000560 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000561 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100562 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
563 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100565 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /*
567 * Loop over leaf entries with the right hash value.
568 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000569 for (lep = &ents[index];
570 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
571 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
573 * Skip stale leaf entries.
574 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100575 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 continue;
577 /*
578 * Pull the data block number from the entry.
579 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000580 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * For addname, we're looking for a place to put the new entry.
583 * We want to use a data block with an entry of equal
584 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000585 *
586 * If this block isn't the data block we already have
587 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000589 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100590 __be16 *bests;
591
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000594 * Convert the data block to the free block
595 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500597 newfdb = dp->d_ops->db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000603 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000606 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100607
608 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000609 xfs_dir2_db_to_da(args->geo,
610 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100611 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000614 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100615
Dave Chinner01ba43b2013-10-29 22:11:52 +1100616 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000619 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500621 fi = dp->d_ops->db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000623 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500625 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100626 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
628 XFS_ERRLEVEL_LOW, mp);
629 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000630 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000633 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100634 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000638 /* Didn't find any space */
639 fi = -1;
640out:
Barry Naujok6a178102008-05-21 16:42:05 +1000641 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 if (curbp) {
643 /* Giving back a free block. */
644 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000646 state->extrablk.index = fi;
647 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100648
649 /*
650 * Important: this magic number is not in the buffer - it's for
651 * buffer type information and therefore only the free/data type
652 * matters here, not whether CRCs are enabled or not.
653 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000654 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
655 } else {
656 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
661 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return XFS_ERROR(ENOENT);
663}
664
665/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000666 * Look up a leaf entry in a node-format leaf block.
667 * The extrablk in state a data block.
668 */
669STATIC int
670xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000672 xfs_da_args_t *args, /* operation arguments */
673 int *indexp, /* out: leaf entry index */
674 xfs_da_state_t *state) /* state to fill in */
675{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000676 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 xfs_dir2_db_t curdb = -1; /* current data block number */
678 xfs_dir2_data_entry_t *dep; /* data block entry */
679 xfs_inode_t *dp; /* incore directory inode */
680 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000681 int index; /* leaf entry index */
682 xfs_dir2_leaf_t *leaf; /* leaf structure */
683 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
684 xfs_mount_t *mp; /* filesystem mount point */
685 xfs_dir2_db_t newdb; /* new data block number */
686 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000687 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000688 struct xfs_dir2_leaf_entry *ents;
689 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000690
691 dp = args->dp;
692 tp = args->trans;
693 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100695 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100696 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000697
Dave Chinner41419562013-10-29 22:11:50 +1100698 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000699 ASSERT(leafhdr.count > 0);
700
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000701 /*
702 * Look up the hash value in the leaf entries.
703 */
704 index = xfs_dir2_leaf_search_hash(args, bp);
705 /*
706 * Do we have a buffer coming in?
707 */
708 if (state->extravalid) {
709 curbp = state->extrablk.bp;
710 curdb = state->extrablk.blkno;
711 }
712 /*
713 * Loop over leaf entries with the right hash value.
714 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000715 for (lep = &ents[index];
716 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
717 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000718 /*
719 * Skip stale leaf entries.
720 */
721 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
722 continue;
723 /*
724 * Pull the data block number from the entry.
725 */
726 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
727 /*
728 * Not adding a new entry, so we really want to find
729 * the name given to us.
730 *
731 * If it's a different data block, go get it.
732 */
733 if (newdb != curdb) {
734 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000735 * If we had a block before that we aren't saving
736 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000737 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000738 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
739 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000740 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000741 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000742 * If needing the block that is saved with a CI match,
743 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000744 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000745 if (args->cmpresult != XFS_CMP_DIFFERENT &&
746 newdb == state->extrablk.blkno) {
747 ASSERT(state->extravalid);
748 curbp = state->extrablk.bp;
749 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100750 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +1000751 xfs_dir2_db_to_da(args->geo,
752 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100753 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000754 if (error)
755 return error;
756 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100757 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000758 curdb = newdb;
759 }
760 /*
761 * Point to the data entry.
762 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000763 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000764 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
765 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000766 * Compare the entry and if it's an exact match, return
767 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000768 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000769 */
Barry Naujok5163f952008-05-21 16:41:01 +1000770 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
771 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000772 /* If there is a CI match block, drop it */
773 if (args->cmpresult != XFS_CMP_DIFFERENT &&
774 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000775 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000776 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000777 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100778 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000779 *indexp = index;
780 state->extravalid = 1;
781 state->extrablk.bp = curbp;
782 state->extrablk.blkno = curdb;
783 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000784 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000785 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100786 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100787 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000788 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000789 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000790 }
791 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000792 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000793 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000794 if (args->cmpresult == XFS_CMP_DIFFERENT) {
795 /* Giving back last used data block. */
796 state->extravalid = 1;
797 state->extrablk.bp = curbp;
798 state->extrablk.index = -1;
799 state->extrablk.blkno = curdb;
800 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100801 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100802 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000803 } else {
804 /* If the curbp is not the CI match block, drop it */
805 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000806 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000807 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000808 } else {
809 state->extravalid = 0;
810 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000811 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000812 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000813}
814
815/*
816 * Look up a leaf entry in a node-format leaf block.
817 * If this is an addname then the extrablk in state is a freespace block,
818 * otherwise it's a data block.
819 */
820int
821xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000822 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000823 xfs_da_args_t *args, /* operation arguments */
824 int *indexp, /* out: leaf entry index */
825 xfs_da_state_t *state) /* state to fill in */
826{
Barry Naujok6a178102008-05-21 16:42:05 +1000827 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000828 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
829 state);
830 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
831}
832
833/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * Move count leaf entries from source to destination leaf.
835 * Log entries and headers. Stale entries are preserved.
836 */
837static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000838xfs_dir3_leafn_moveents(
839 xfs_da_args_t *args, /* operation arguments */
840 struct xfs_buf *bp_s, /* source */
841 struct xfs_dir3_icleaf_hdr *shdr,
842 struct xfs_dir2_leaf_entry *sents,
843 int start_s,/* source leaf index */
844 struct xfs_buf *bp_d, /* destination */
845 struct xfs_dir3_icleaf_hdr *dhdr,
846 struct xfs_dir2_leaf_entry *dents,
847 int start_d,/* destination leaf index */
848 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Dave Chinner24df33b2013-04-12 07:30:21 +1000850 struct xfs_trans *tp = args->trans;
851 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000853 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Silently return if nothing to do.
857 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000858 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * If the destination index is not the end of the current
863 * destination leaf entries, open up a hole in the destination
864 * to hold the new entries.
865 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000866 if (start_d < dhdr->count) {
867 memmove(&dents[start_d + count], &dents[start_d],
868 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100869 xfs_dir3_leaf_log_ents(tp, args->dp, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000870 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872 /*
873 * If the source has stale leaves, count the ones in the copy range
874 * so we can update the header correctly.
875 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000876 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 int i; /* temp leaf index */
878
879 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000880 if (sents[i].address ==
881 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 stale++;
883 }
884 } else
885 stale = 0;
886 /*
887 * Copy the leaf entries from source to destination.
888 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000889 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100891 xfs_dir3_leaf_log_ents(tp, args->dp, bp_d,
892 start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * If there are source entries after the ones we copied,
896 * delete the ones we copied by sliding the next ones down.
897 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000898 if (start_s + count < shdr->count) {
899 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner41419562013-10-29 22:11:50 +1100901 xfs_dir3_leaf_log_ents(tp, args->dp, bp_s,
902 start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /*
906 * Update the headers and log them.
907 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000908 shdr->count -= count;
909 shdr->stale -= stale;
910 dhdr->count += count;
911 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/*
915 * Determine the sort order of two leaf blocks.
916 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
917 */
918int /* sort order */
919xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100920 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000921 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
922 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Dave Chinner24df33b2013-04-12 07:30:21 +1000924 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
925 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
926 struct xfs_dir2_leaf_entry *ents1;
927 struct xfs_dir2_leaf_entry *ents2;
928 struct xfs_dir3_icleaf_hdr hdr1;
929 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Chinner01ba43b2013-10-29 22:11:52 +1100931 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
932 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100933 ents1 = dp->d_ops->leaf_ents_p(leaf1);
934 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000935
936 if (hdr1.count > 0 && hdr2.count > 0 &&
937 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
938 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
939 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 1;
941 return 0;
942}
943
944/*
945 * Rebalance leaf entries between two leaf blocks.
946 * This is actually only called when the second block is new,
947 * though the code deals with the general case.
948 * A new entry will be inserted in one of the blocks, and that
949 * entry is taken into account when balancing.
950 */
951static void
952xfs_dir2_leafn_rebalance(
953 xfs_da_state_t *state, /* btree cursor */
954 xfs_da_state_blk_t *blk1, /* first btree block */
955 xfs_da_state_blk_t *blk2) /* second btree block */
956{
957 xfs_da_args_t *args; /* operation arguments */
958 int count; /* count (& direction) leaves */
959 int isleft; /* new goes in left leaf */
960 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
961 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
962 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000963#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int oldstale; /* old count of stale leaves */
965#endif
966 int oldsum; /* old total leaf count */
967 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000968 struct xfs_dir2_leaf_entry *ents1;
969 struct xfs_dir2_leaf_entry *ents2;
970 struct xfs_dir3_icleaf_hdr hdr1;
971 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100972 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 args = state->args;
975 /*
976 * If the block order is wrong, swap the arguments.
977 */
Dave Chinner41419562013-10-29 22:11:50 +1100978 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 xfs_da_state_blk_t *tmp; /* temp for block swap */
980
981 tmp = blk1;
982 blk1 = blk2;
983 blk2 = tmp;
984 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000985 leaf1 = blk1->bp->b_addr;
986 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100987 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
988 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100989 ents1 = dp->d_ops->leaf_ents_p(leaf1);
990 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000991
992 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000993#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000994 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995#endif
996 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
999 * If the old leaf count was odd then the new one will be even,
1000 * so we need to divide the new count evenly.
1001 */
1002 if (oldsum & 1) {
1003 xfs_dahash_t midhash; /* middle entry hash value */
1004
Dave Chinner24df33b2013-04-12 07:30:21 +10001005 if (mid >= hdr1.count)
1006 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001008 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 isleft = args->hashval <= midhash;
1010 }
1011 /*
1012 * If the old count is even then the new count is odd, so there's
1013 * no preferred side for the new entry.
1014 * Pick the left one.
1015 */
1016 else
1017 isleft = 1;
1018 /*
1019 * Calculate moved entry count. Positive means left-to-right,
1020 * negative means right-to-left. Then move the entries.
1021 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001022 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001024 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1025 hdr1.count - count, blk2->bp,
1026 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1029 blk1->bp, &hdr1, ents1,
1030 hdr1.count, count);
1031
1032 ASSERT(hdr1.count + hdr2.count == oldsum);
1033 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1034
1035 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001036 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1037 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinner41419562013-10-29 22:11:50 +11001038 xfs_dir3_leaf_log_header(args->trans, dp, blk1->bp);
1039 xfs_dir3_leaf_log_header(args->trans, dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001040
Dave Chinner41419562013-10-29 22:11:50 +11001041 xfs_dir3_leaf_check(dp, blk1->bp);
1042 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * Mark whether we're inserting into the old or new leaf.
1046 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001047 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001049 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 state->inleaf = !swap;
1051 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001052 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /*
1054 * Adjust the expected index for insertion.
1055 */
1056 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001057 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001058
1059 /*
1060 * Finally sanity check just to make sure we are not returning a
1061 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Dave Chinner41419562013-10-29 22:11:50 +11001063 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 state->inleaf = 1;
1065 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001066 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001067 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001068 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070}
1071
Dave Chinner20252072012-11-12 22:54:13 +11001072static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001073xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001074 xfs_da_args_t *args,
1075 struct xfs_dir2_data_hdr *hdr,
1076 struct xfs_dir2_free *free,
1077 xfs_dir2_db_t fdb,
1078 int findex,
1079 struct xfs_buf *fbp,
1080 int longest)
1081{
1082 struct xfs_trans *tp = args->trans;
1083 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001084 __be16 *bests;
1085 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001086 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001087
Dave Chinner01ba43b2013-10-29 22:11:52 +11001088 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001089 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001090 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001091 /*
1092 * Data block is not empty, just set the free entry to the new
1093 * value.
1094 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001095 bests[findex] = cpu_to_be16(longest);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001096 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001097 return 0;
1098 }
1099
1100 /* One less used entry in the free table. */
1101 freehdr.nused--;
1102
1103 /*
1104 * If this was the last entry in the table, we can trim the table size
1105 * back. There might be other entries at the end referring to
1106 * non-existent data blocks, get those too.
1107 */
1108 if (findex == freehdr.nvalid - 1) {
1109 int i; /* free entry index */
1110
1111 for (i = findex - 1; i >= 0; i--) {
1112 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1113 break;
1114 }
1115 freehdr.nvalid = i + 1;
1116 logfree = 0;
1117 } else {
1118 /* Not the last entry, just punch it out. */
1119 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001120 logfree = 1;
1121 }
1122
Dave Chinner01ba43b2013-10-29 22:11:52 +11001123 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001124 xfs_dir2_free_log_header(tp, dp, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001125
1126 /*
1127 * If there are no useful entries left in the block, get rid of the
1128 * block if we can.
1129 */
1130 if (!freehdr.nused) {
1131 int error;
1132
1133 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1134 if (error == 0) {
1135 fbp = NULL;
1136 logfree = 0;
1137 } else if (error != ENOSPC || args->total != 0)
1138 return error;
1139 /*
1140 * It's possible to get ENOSPC if there is no
1141 * space reservation. In this case some one
1142 * else will eventually get rid of this block.
1143 */
1144 }
1145
Dave Chinner20252072012-11-12 22:54:13 +11001146 /* Log the free entry that changed, unless we got rid of it. */
1147 if (logfree)
Dave Chinner24dd0f52013-10-30 13:48:41 -05001148 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001149 return 0;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/*
1153 * Remove an entry from a node directory.
1154 * This removes the leaf entry and the data entry,
1155 * and updates the free block if necessary.
1156 */
1157static int /* error */
1158xfs_dir2_leafn_remove(
1159 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001160 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 int index, /* leaf entry index */
1162 xfs_da_state_blk_t *dblk, /* data block */
1163 int *rval) /* resulting block needs join */
1164{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001165 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001167 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 xfs_dir2_data_entry_t *dep; /* data block entry */
1169 xfs_inode_t *dp; /* incore directory inode */
1170 xfs_dir2_leaf_t *leaf; /* leaf structure */
1171 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1172 int longest; /* longest data free entry */
1173 int off; /* data block entry offset */
1174 xfs_mount_t *mp; /* filesystem mount point */
1175 int needlog; /* need to log data header */
1176 int needscan; /* need to rescan data frees */
1177 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001178 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001179 struct xfs_dir3_icleaf_hdr leafhdr;
1180 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001182 trace_xfs_dir2_leafn_remove(args, index);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 dp = args->dp;
1185 tp = args->trans;
1186 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001187 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001188 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001189 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * Point to the entry we're removing.
1193 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001194 lep = &ents[index];
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Extract the data block and offset from the entry.
1198 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001199 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001201 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /*
1205 * Kill the leaf entry by marking it stale.
1206 * Log the leaf block changes.
1207 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001208 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001209 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinner41419562013-10-29 22:11:50 +11001210 xfs_dir3_leaf_log_header(tp, dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001211
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001212 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner41419562013-10-29 22:11:50 +11001213 xfs_dir3_leaf_log_ents(tp, dp, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Make the data entry free. Keep track of the longest freespace
1217 * in the data block in case it changes.
1218 */
1219 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001220 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001221 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001222 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001223 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 needlog = needscan = 0;
Dave Chinner2ca98772013-10-29 22:11:49 +11001225 xfs_dir2_data_make_free(tp, dp, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001226 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Rescan the data block freespaces for bestfree.
1229 * Log the data block header if needed.
1230 */
1231 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001232 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (needlog)
Dave Chinner2ca98772013-10-29 22:11:49 +11001234 xfs_dir2_data_log_header(tp, dp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001235 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * If the longest data block freespace changes, need to update
1238 * the corresponding freeblock entry.
1239 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001240 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001242 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 xfs_dir2_db_t fdb; /* freeblock block number */
1244 int findex; /* index in freeblock entries */
1245 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /*
1248 * Convert the data block number to a free block,
1249 * read in the free block.
1250 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001251 fdb = dp->d_ops->db_to_fdb(mp, db);
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001252 error = xfs_dir2_free_read(tp, dp,
1253 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001254 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001255 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001257 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001258#ifdef DEBUG
1259 {
1260 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001261 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001262 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(mp) *
Dave Chinner8c44a282014-06-06 15:04:41 +10001263 (fdb - xfs_dir2_byte_to_db(mp, XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001264 }
1265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /*
1267 * Calculate which entry we need to fix.
1268 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001269 findex = dp->d_ops->db_to_fdindex(mp, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001270 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * If the data block is now empty we can get rid of it
1273 * (usually).
1274 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001275 if (longest == mp->m_dirblksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001276 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /*
1278 * Try to punch out the data block.
1279 */
1280 error = xfs_dir2_shrink_inode(args, db, dbp);
1281 if (error == 0) {
1282 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001283 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 /*
1286 * We can get ENOSPC if there's no space reservation.
1287 * In this case just drop the buffer and some one else
1288 * will eventually get rid of the empty block.
1289 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001290 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return error;
1292 }
1293 /*
1294 * If we got rid of the data block, we can eliminate that entry
1295 * in the free block.
1296 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001297 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001298 fdb, findex, fbp, longest);
1299 if (error)
1300 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
Dave Chinner20252072012-11-12 22:54:13 +11001302
Dave Chinner41419562013-10-29 22:11:50 +11001303 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001305 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * to justify trying to join it with a neighbor.
1307 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001308 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001309 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 mp->m_dir_magicpct;
1311 return 0;
1312}
1313
1314/*
1315 * Split the leaf entries in the old block into old and new blocks.
1316 */
1317int /* error */
1318xfs_dir2_leafn_split(
1319 xfs_da_state_t *state, /* btree cursor */
1320 xfs_da_state_blk_t *oldblk, /* original block */
1321 xfs_da_state_blk_t *newblk) /* newly created block */
1322{
1323 xfs_da_args_t *args; /* operation arguments */
1324 xfs_dablk_t blkno; /* new leaf block number */
1325 int error; /* error return value */
1326 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner41419562013-10-29 22:11:50 +11001327 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /*
1330 * Allocate space for a new leaf node.
1331 */
1332 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001333 dp = args->dp;
1334 mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1336 error = xfs_da_grow_inode(args, &blkno);
1337 if (error) {
1338 return error;
1339 }
1340 /*
1341 * Initialize the new leaf block.
1342 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001343 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001344 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1345 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 newblk->blkno = blkno;
1349 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1350 /*
1351 * Rebalance the entries across the two leaves, link the new
1352 * block into the leaves.
1353 */
1354 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001355 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (error) {
1357 return error;
1358 }
1359 /*
1360 * Insert the new entry in the correct block.
1361 */
1362 if (state->inleaf)
1363 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1364 else
1365 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1366 /*
1367 * Update last hashval in each block since we added the name.
1368 */
Dave Chinner41419562013-10-29 22:11:50 +11001369 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1370 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1371 xfs_dir3_leaf_check(dp, oldblk->bp);
1372 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return error;
1374}
1375
1376/*
1377 * Check a leaf block and its neighbors to see if the block should be
1378 * collapsed into one or the other neighbor. Always keep the block
1379 * with the smaller block number.
1380 * If the current block is over 50% full, don't try to join it, return 0.
1381 * If the block is empty, fill in the state structure and return 2.
1382 * If it can be collapsed, fill in the state structure and return 1.
1383 * If nothing can be done, return 0.
1384 */
1385int /* error */
1386xfs_dir2_leafn_toosmall(
1387 xfs_da_state_t *state, /* btree cursor */
1388 int *action) /* resulting action to take */
1389{
1390 xfs_da_state_blk_t *blk; /* leaf block */
1391 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001392 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int bytes; /* bytes in use */
1394 int count; /* leaf live entry count */
1395 int error; /* error return value */
1396 int forward; /* sibling block direction */
1397 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 xfs_dir2_leaf_t *leaf; /* leaf structure */
1399 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001400 struct xfs_dir3_icleaf_hdr leafhdr;
1401 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001402 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
1405 * Check for the degenerate case of the block being over 50% full.
1406 * If so, it's not worth even looking to see if we might be able
1407 * to coalesce with a sibling.
1408 */
1409 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001410 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001411 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001412 ents = dp->d_ops->leaf_ents_p(leaf);
1413 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001414
1415 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001416 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 if (bytes > (state->blocksize >> 1)) {
1418 /*
1419 * Blk over 50%, don't try to join.
1420 */
1421 *action = 0;
1422 return 0;
1423 }
1424 /*
1425 * Check for the degenerate case of the block being empty.
1426 * If the block is empty, we'll simply delete it, no need to
1427 * coalesce it with a sibling block. We choose (arbitrarily)
1428 * to merge with the forward block unless it is NULL.
1429 */
1430 if (count == 0) {
1431 /*
1432 * Make altpath point to the block we want to keep and
1433 * path point to the block we want to drop (this one).
1434 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001435 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001437 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 &rval);
1439 if (error)
1440 return error;
1441 *action = rval ? 2 : 0;
1442 return 0;
1443 }
1444 /*
1445 * Examine each sibling block to see if we can coalesce with
1446 * at least 25% free space to spare. We need to figure out
1447 * whether to merge with the forward or the backward block.
1448 * We prefer coalescing with the lower numbered sibling so as
1449 * to shrink a directory over time.
1450 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001451 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001453 struct xfs_dir3_icleaf_hdr hdr2;
1454
1455 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (blkno == 0)
1457 continue;
1458 /*
1459 * Read the sibling leaf block.
1460 */
Dave Chinner41419562013-10-29 22:11:50 +11001461 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001462 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001463 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /*
1467 * Count bytes in the two blocks combined.
1468 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001469 count = leafhdr.count - leafhdr.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001471
Dave Chinner1d9025e2012-06-22 18:50:14 +10001472 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001473 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001474 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001475 count += hdr2.count - hdr2.stale;
1476 bytes -= count * sizeof(ents[0]);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
1479 * Fits with at least 25% to spare.
1480 */
1481 if (bytes >= 0)
1482 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001483 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 /*
1486 * Didn't like either block, give up.
1487 */
1488 if (i >= 2) {
1489 *action = 0;
1490 return 0;
1491 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 /*
1494 * Make altpath point to the block we want to keep (the lower
1495 * numbered block) and path point to the block we want to drop.
1496 */
1497 memcpy(&state->altpath, &state->path, sizeof(state->path));
1498 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001499 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 &rval);
1501 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001502 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 &rval);
1504 if (error) {
1505 return error;
1506 }
1507 *action = rval ? 0 : 1;
1508 return 0;
1509}
1510
1511/*
1512 * Move all the leaf entries from drop_blk to save_blk.
1513 * This is done as part of a join operation.
1514 */
1515void
1516xfs_dir2_leafn_unbalance(
1517 xfs_da_state_t *state, /* cursor */
1518 xfs_da_state_blk_t *drop_blk, /* dead block */
1519 xfs_da_state_blk_t *save_blk) /* surviving block */
1520{
1521 xfs_da_args_t *args; /* operation arguments */
1522 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1523 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001524 struct xfs_dir3_icleaf_hdr savehdr;
1525 struct xfs_dir3_icleaf_hdr drophdr;
1526 struct xfs_dir2_leaf_entry *sents;
1527 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001528 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 args = state->args;
1531 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1532 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001533 drop_leaf = drop_blk->bp->b_addr;
1534 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001535
Dave Chinner01ba43b2013-10-29 22:11:52 +11001536 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1537 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1538 sents = dp->d_ops->leaf_ents_p(save_leaf);
1539 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
1542 * If there are any stale leaf entries, take this opportunity
1543 * to purge them.
1544 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001545 if (drophdr.stale)
1546 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1547 if (savehdr.stale)
1548 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * Move the entries from drop to the appropriate end of save.
1552 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001553 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001554 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001555 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1556 save_blk->bp, &savehdr, sents, 0,
1557 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001559 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1560 save_blk->bp, &savehdr, sents,
1561 savehdr.count, drophdr.count);
1562 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1563
1564 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001565 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1566 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinner41419562013-10-29 22:11:50 +11001567 xfs_dir3_leaf_log_header(args->trans, dp, save_blk->bp);
1568 xfs_dir3_leaf_log_header(args->trans, dp, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001569
Dave Chinner41419562013-10-29 22:11:50 +11001570 xfs_dir3_leaf_check(dp, save_blk->bp);
1571 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574/*
1575 * Top-level node form directory addname routine.
1576 */
1577int /* error */
1578xfs_dir2_node_addname(
1579 xfs_da_args_t *args) /* operation arguments */
1580{
1581 xfs_da_state_blk_t *blk; /* leaf block for insert */
1582 int error; /* error return value */
1583 int rval; /* sub-return value */
1584 xfs_da_state_t *state; /* btree cursor */
1585
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001586 trace_xfs_dir2_node_addname(args);
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * Allocate and initialize the state (btree cursor).
1590 */
1591 state = xfs_da_state_alloc();
1592 state->args = args;
1593 state->mp = args->dp->i_mount;
1594 state->blocksize = state->mp->m_dirblksize;
1595 state->node_ents = state->mp->m_dir_node_ents;
1596 /*
1597 * Look up the name. We're not supposed to find it, but
1598 * this gives us the insertion point.
1599 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001600 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (error)
1602 rval = error;
1603 if (rval != ENOENT) {
1604 goto done;
1605 }
1606 /*
1607 * Add the data entry to a data block.
1608 * Extravalid is set to a freeblock found by lookup.
1609 */
1610 rval = xfs_dir2_node_addname_int(args,
1611 state->extravalid ? &state->extrablk : NULL);
1612 if (rval) {
1613 goto done;
1614 }
1615 blk = &state->path.blk[state->path.active - 1];
1616 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1617 /*
1618 * Add the new leaf entry.
1619 */
1620 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1621 if (rval == 0) {
1622 /*
1623 * It worked, fix the hash values up the btree.
1624 */
Barry Naujok6a178102008-05-21 16:42:05 +10001625 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001626 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 } else {
1628 /*
1629 * It didn't work, we need to split the leaf block.
1630 */
1631 if (args->total == 0) {
1632 ASSERT(rval == ENOSPC);
1633 goto done;
1634 }
1635 /*
1636 * Split the leaf block and insert the new entry.
1637 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001638 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640done:
1641 xfs_da_state_free(state);
1642 return rval;
1643}
1644
1645/*
1646 * Add the data entry for a node-format directory name addition.
1647 * The leaf entry is added in xfs_dir2_leafn_add.
1648 * We may enter with a freespace block that the lookup found.
1649 */
1650static int /* error */
1651xfs_dir2_node_addname_int(
1652 xfs_da_args_t *args, /* operation arguments */
1653 xfs_da_state_blk_t *fblk) /* optional freespace block */
1654{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001655 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001657 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1659 xfs_inode_t *dp; /* incore directory inode */
1660 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1661 int error; /* error return value */
1662 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001663 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int findex; /* freespace entry index */
1665 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1666 xfs_dir2_db_t ifbno; /* initial freespace block no */
1667 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1668 int length; /* length of the new entry */
1669 int logfree; /* need to log free entry */
1670 xfs_mount_t *mp; /* filesystem mount point */
1671 int needlog; /* need to log data header */
1672 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001673 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001675 __be16 *bests;
1676 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001677 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 dp = args->dp;
1680 mp = dp->i_mount;
1681 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001682 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * If we came in with a freespace block that means that lookup
1685 * found an entry with our hash value. This is the freespace
1686 * block for that data entry.
1687 */
1688 if (fblk) {
1689 fbp = fblk->bp;
1690 /*
1691 * Remember initial freespace block number.
1692 */
1693 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001694 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001696 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001697 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * This means the free entry showed that the data block had
1701 * space for our entry, so we remembered it.
1702 * Use that data block.
1703 */
1704 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001705 ASSERT(findex < freehdr.nvalid);
1706 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1707 ASSERT(be16_to_cpu(bests[findex]) >= length);
1708 dbno = freehdr.firstdb + findex;
1709 } else {
1710 /*
1711 * The data block looked at didn't have enough room.
1712 * We'll start at the beginning of the freespace entries.
1713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 dbno = -1;
1715 findex = 0;
1716 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001717 } else {
1718 /*
1719 * Didn't come in with a freespace block, so no data block.
1720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 ifbno = dbno = -1;
1722 fbp = NULL;
1723 findex = 0;
1724 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /*
1727 * If we don't have a data block yet, we're going to scan the
1728 * freespace blocks looking for one. Figure out what the
1729 * highest freespace block number is.
1730 */
1731 if (dbno == -1) {
1732 xfs_fileoff_t fo; /* freespace block number */
1733
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001734 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return error;
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001736 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 fbno = ifbno;
1738 }
1739 /*
1740 * While we haven't identified a data block, search the freeblock
1741 * data for a good data block. If we find a null freeblock entry,
1742 * indicating a hole in the data blocks, remember that.
1743 */
1744 while (dbno == -1) {
1745 /*
1746 * If we don't have a freeblock in hand, get the next one.
1747 */
1748 if (fbp == NULL) {
1749 /*
1750 * Happens the first time through unless lookup gave
1751 * us a freespace block to start with.
1752 */
1753 if (++fbno == 0)
Dave Chinner8c44a282014-06-06 15:04:41 +10001754 fbno = xfs_dir2_byte_to_db(mp,
1755 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * If it's ifbno we already looked at it.
1758 */
1759 if (fbno == ifbno)
1760 fbno++;
1761 /*
1762 * If it's off the end we're done.
1763 */
1764 if (fbno >= lastfbno)
1765 break;
1766 /*
1767 * Read the block. There can be holes in the
1768 * freespace blocks, so this might not succeed.
1769 * This should be really rare, so there's no reason
1770 * to avoid it.
1771 */
Dave Chinner20252072012-11-12 22:54:13 +11001772 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001773 xfs_dir2_db_to_da(args->geo, fbno),
1774 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001777 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001779 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 findex = 0;
1781 }
1782 /*
1783 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001784 *
1785 * The bests initialisation should be where the bufer is read in
1786 * the above branch. But gcc is too stupid to realise that bests
1787 * and the freehdr are actually initialised if they are placed
1788 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001790 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001791 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001792 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1793 be16_to_cpu(bests[findex]) >= length)
1794 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 else {
1796 /*
1797 * Are we done with the freeblock?
1798 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001799 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
1801 * Drop the block.
1802 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001803 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 fbp = NULL;
1805 if (fblk && fblk->bp)
1806 fblk->bp = NULL;
1807 }
1808 }
1809 }
1810 /*
1811 * If we don't have a data block, we need to allocate one and make
1812 * the freespace entries refer to it.
1813 */
1814 if (unlikely(dbno == -1)) {
1815 /*
1816 * Not allowed to allocate, return failure.
1817 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001818 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /*
1822 * Allocate and initialize the new data block.
1823 */
1824 if (unlikely((error = xfs_dir2_grow_inode(args,
1825 XFS_DIR2_DATA_SPACE,
1826 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001827 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * If (somehow) we have a freespace block, get rid of it.
1832 */
1833 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001834 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (fblk && fblk->bp)
1836 fblk->bp = NULL;
1837
1838 /*
1839 * Get the freespace block corresponding to the data block
1840 * that was just allocated.
1841 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001842 fbno = dp->d_ops->db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001843 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001844 xfs_dir2_db_to_da(args->geo, fbno),
1845 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /*
1850 * If there wasn't a freespace block, the read will
1851 * return a NULL fbp. Allocate and initialize a new one.
1852 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001853 if (!fbp) {
1854 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1855 &fbno);
1856 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Dave Chinner24dd0f52013-10-30 13:48:41 -05001859 if (unlikely(dp->d_ops->db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001860 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001861 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001862 " data block %lld, got %lld ifbno %llu lastfbno %d",
1863 __func__, (unsigned long long)dp->i_ino,
Dave Chinner24dd0f52013-10-30 13:48:41 -05001864 (long long)dp->d_ops->db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 (long long)dbno, (long long)fbno,
1866 (unsigned long long)ifbno, lastfbno);
1867 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001868 xfs_alert(mp,
1869 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 fblk,
1871 (unsigned long long)fblk->blkno,
1872 fblk->index,
1873 fblk->magic);
1874 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001875 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
1877 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1878 XFS_ERRLEVEL_LOW, mp);
1879 return XFS_ERROR(EFSCORRUPTED);
1880 }
1881
1882 /*
1883 * Get a buffer for the new block.
1884 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001885 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001886 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001888 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001889 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001890 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001893 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001895 freehdr.firstdb =
1896 (fbno - xfs_dir2_byte_to_db(mp,
1897 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner24dd0f52013-10-30 13:48:41 -05001898 dp->d_ops->free_max_bests(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001900 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001901 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001902 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
1905 /*
1906 * Set the freespace block index from the data block number.
1907 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001908 findex = dp->d_ops->db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * If it's after the end of the current entries in the
1911 * freespace block, extend that table.
1912 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001913 if (findex >= freehdr.nvalid) {
Dave Chinner24dd0f52013-10-30 13:48:41 -05001914 ASSERT(findex < dp->d_ops->free_max_bests(mp));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001915 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
1917 * Tag new entry so nused will go up.
1918 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001919 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921 /*
1922 * If this entry was for an empty data block
1923 * (this should always be true) then update the header.
1924 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001925 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1926 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001927 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001928 xfs_dir2_free_log_header(tp, dp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930 /*
1931 * Update the real value in the table.
1932 * We haven't allocated the data entry yet so this will
1933 * change again.
1934 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001935 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001936 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001937 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 logfree = 1;
1939 }
1940 /*
1941 * We had a data block so we don't have to make a new one.
1942 */
1943 else {
1944 /*
1945 * If just checking, we succeeded.
1946 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001947 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 /*
1951 * Read the data block in.
1952 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10001953 error = xfs_dir3_data_read(tp, dp,
1954 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001955 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001956 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001958 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001959 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 logfree = 0;
1961 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001962 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * Point to the existing unused space.
1965 */
1966 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001967 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 needscan = needlog = 0;
1969 /*
1970 * Mark the first part of the unused space, inuse for us.
1971 */
Dave Chinner2ca98772013-10-29 22:11:49 +11001972 xfs_dir2_data_use_free(tp, dp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001973 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 &needlog, &needscan);
1975 /*
1976 * Fill in the new entry and log it.
1977 */
1978 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001979 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 dep->namelen = args->namelen;
1981 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001982 dp->d_ops->data_put_ftype(dep, args->filetype);
1983 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001984 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001985 xfs_dir2_data_log_entry(tp, dp, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * Rescan the block for bestfree if needed.
1988 */
1989 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001990 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Log the data block header if needed.
1993 */
1994 if (needlog)
Dave Chinner2ca98772013-10-29 22:11:49 +11001995 xfs_dir2_data_log_header(tp, dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /*
1997 * If the freespace entry is now wrong, update it.
1998 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001999 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002000 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2001 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 logfree = 1;
2003 }
2004 /*
2005 * Log the freespace entry if needed.
2006 */
2007 if (logfree)
Dave Chinner24dd0f52013-10-30 13:48:41 -05002008 xfs_dir2_free_log_bests(tp, dp, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * Return the data block and offset in args, then drop the data block.
2011 */
2012 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002013 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return 0;
2015}
2016
2017/*
2018 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002019 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * The only real output is the inode number of the entry.
2021 */
2022int /* error */
2023xfs_dir2_node_lookup(
2024 xfs_da_args_t *args) /* operation arguments */
2025{
2026 int error; /* error return value */
2027 int i; /* btree level */
2028 int rval; /* operation return value */
2029 xfs_da_state_t *state; /* btree cursor */
2030
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002031 trace_xfs_dir2_node_lookup(args);
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /*
2034 * Allocate and initialize the btree cursor.
2035 */
2036 state = xfs_da_state_alloc();
2037 state->args = args;
2038 state->mp = args->dp->i_mount;
2039 state->blocksize = state->mp->m_dirblksize;
2040 state->node_ents = state->mp->m_dir_node_ents;
2041 /*
2042 * Fill in the path to the entry in the cursor.
2043 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002044 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (error)
2046 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10002047 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
2048 /* If a CI match, dup the actual name and return EEXIST */
2049 xfs_dir2_data_entry_t *dep;
2050
Dave Chinner1d9025e2012-06-22 18:50:14 +10002051 dep = (xfs_dir2_data_entry_t *)
2052 ((char *)state->extrablk.bp->b_addr +
2053 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002054 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Release the btree blocks and leaf block.
2058 */
2059 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002060 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 state->path.blk[i].bp = NULL;
2062 }
2063 /*
2064 * Release the data block if we have it.
2065 */
2066 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002067 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 state->extrablk.bp = NULL;
2069 }
2070 xfs_da_state_free(state);
2071 return rval;
2072}
2073
2074/*
2075 * Remove an entry from a node-format directory.
2076 */
2077int /* error */
2078xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002079 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002081 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int error; /* error return value */
2083 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002084 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002086 trace_xfs_dir2_node_removename(args);
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * Allocate and initialize the btree cursor.
2090 */
2091 state = xfs_da_state_alloc();
2092 state->args = args;
2093 state->mp = args->dp->i_mount;
2094 state->blocksize = state->mp->m_dirblksize;
2095 state->node_ents = state->mp->m_dir_node_ents;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002096
2097 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002098 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002099 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002100 goto out_free;
2101
2102 /* Didn't find it, upper layer screwed up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (rval != EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002104 error = rval;
2105 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 blk = &state->path.blk[state->path.active - 1];
2109 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2110 ASSERT(state->extravalid);
2111 /*
2112 * Remove the leaf and data entries.
2113 * Extrablk refers to the data block.
2114 */
2115 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2116 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002117 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002118 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /*
2120 * Fix the hash values up the btree.
2121 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002122 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /*
2124 * If we need to join leaf blocks, do it.
2125 */
2126 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002127 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * If no errors so far, try conversion to leaf format.
2130 */
2131 if (!error)
2132 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002133out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 xfs_da_state_free(state);
2135 return error;
2136}
2137
2138/*
2139 * Replace an entry's inode number in a node-format directory.
2140 */
2141int /* error */
2142xfs_dir2_node_replace(
2143 xfs_da_args_t *args) /* operation arguments */
2144{
2145 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002146 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 xfs_dir2_data_entry_t *dep; /* data entry changed */
2148 int error; /* error return value */
2149 int i; /* btree level */
2150 xfs_ino_t inum; /* new inode number */
2151 xfs_dir2_leaf_t *leaf; /* leaf structure */
2152 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2153 int rval; /* internal return value */
2154 xfs_da_state_t *state; /* btree cursor */
2155
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002156 trace_xfs_dir2_node_replace(args);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * Allocate and initialize the btree cursor.
2160 */
2161 state = xfs_da_state_alloc();
2162 state->args = args;
2163 state->mp = args->dp->i_mount;
2164 state->blocksize = state->mp->m_dirblksize;
2165 state->node_ents = state->mp->m_dir_node_ents;
2166 inum = args->inumber;
2167 /*
2168 * Lookup the entry to change in the btree.
2169 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002170 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (error) {
2172 rval = error;
2173 }
2174 /*
2175 * It should be found, since the vnodeops layer has looked it up
2176 * and locked it. But paranoia is good.
2177 */
2178 if (rval == EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002179 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /*
2181 * Find the leaf entry.
2182 */
2183 blk = &state->path.blk[state->path.active - 1];
2184 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002185 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002186 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002187 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 ASSERT(state->extravalid);
2189 /*
2190 * Point to the data entry.
2191 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002192 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002193 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2194 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002196 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002197 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002198 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * Fill in the new inode number and log the entry.
2201 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002202 dep->inumber = cpu_to_be64(inum);
Dave Chinner9d23fc82013-10-29 22:11:48 +11002203 args->dp->d_ops->data_put_ftype(dep, args->filetype);
2204 xfs_dir2_data_log_entry(args->trans, args->dp,
2205 state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 rval = 0;
2207 }
2208 /*
2209 * Didn't find it, and we're holding a data block. Drop it.
2210 */
2211 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002212 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 state->extrablk.bp = NULL;
2214 }
2215 /*
2216 * Release all the buffers in the cursor.
2217 */
2218 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002219 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 state->path.blk[i].bp = NULL;
2221 }
2222 xfs_da_state_free(state);
2223 return rval;
2224}
2225
2226/*
2227 * Trim off a trailing empty freespace block.
2228 * Return (in rvalp) 1 if we did it, 0 if not.
2229 */
2230int /* error */
2231xfs_dir2_node_trim_free(
2232 xfs_da_args_t *args, /* operation arguments */
2233 xfs_fileoff_t fo, /* free block number */
2234 int *rvalp) /* out: did something */
2235{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002236 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 xfs_inode_t *dp; /* incore directory inode */
2238 int error; /* error return code */
2239 xfs_dir2_free_t *free; /* freespace structure */
2240 xfs_mount_t *mp; /* filesystem mount point */
2241 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002242 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
2244 dp = args->dp;
2245 mp = dp->i_mount;
2246 tp = args->trans;
2247 /*
2248 * Read the freespace block.
2249 */
Dave Chinner20252072012-11-12 22:54:13 +11002250 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002251 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /*
2254 * There can be holes in freespace. If fo is a hole, there's
2255 * nothing to do.
2256 */
Dave Chinner20252072012-11-12 22:54:13 +11002257 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002259 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002260 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /*
2263 * If there are used entries, there's nothing to do.
2264 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002265 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002266 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 *rvalp = 0;
2268 return 0;
2269 }
2270 /*
2271 * Blow the block away.
2272 */
Dave Chinner2998ab1d2014-06-06 15:07:53 +10002273 error = xfs_dir2_shrink_inode(args,
2274 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2275 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 /*
2277 * Can't fail with ENOSPC since that only happens with no
2278 * space reservation, when breaking up an extent into two
2279 * pieces. This is the last block of an extent.
2280 */
2281 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002282 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return error;
2284 }
2285 /*
2286 * Return that we succeeded.
2287 */
2288 *rvalp = 1;
2289 return 0;
2290}