blob: 70b0cb2fd55606422eaf08ce9e016e33f66fe6a2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110025#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_inode.h"
28#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000032#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110034#include "xfs_buf_item.h"
35#include "xfs_cksum.h"
Brian Fostera45086e2015-10-12 15:59:25 +110036#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Function declarations.
40 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100041static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
42 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
44 xfs_da_state_blk_t *blk1,
45 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 int index, xfs_da_state_blk_t *dblk,
48 int *rval);
49static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
50 xfs_da_state_blk_t *fblk);
51
Dave Chinner24df33b2013-04-12 07:30:21 +100052/*
53 * Check internal consistency of a leafn block.
54 */
55#ifdef DEBUG
Dave Chinner41419562013-10-29 22:11:50 +110056#define xfs_dir3_leaf_check(dp, bp) \
Dave Chinner24df33b2013-04-12 07:30:21 +100057do { \
Dave Chinner41419562013-10-29 22:11:50 +110058 if (!xfs_dir3_leafn_check((dp), (bp))) \
Dave Chinner24df33b2013-04-12 07:30:21 +100059 ASSERT(0); \
60} while (0);
61
62static bool
63xfs_dir3_leafn_check(
Dave Chinner41419562013-10-29 22:11:50 +110064 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +100065 struct xfs_buf *bp)
66{
67 struct xfs_dir2_leaf *leaf = bp->b_addr;
68 struct xfs_dir3_icleaf_hdr leafhdr;
69
Dave Chinner01ba43b2013-10-29 22:11:52 +110070 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100071
72 if (leafhdr.magic == XFS_DIR3_LEAFN_MAGIC) {
73 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
74 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
75 return false;
76 } else if (leafhdr.magic != XFS_DIR2_LEAFN_MAGIC)
77 return false;
78
Dave Chinner41419562013-10-29 22:11:50 +110079 return xfs_dir3_leaf_check_int(dp->i_mount, dp, &leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +100080}
81#else
Dave Chinner41419562013-10-29 22:11:50 +110082#define xfs_dir3_leaf_check(dp, bp)
Dave Chinner24df33b2013-04-12 07:30:21 +100083#endif
84
Dave Chinnercbc8adf2013-04-03 16:11:21 +110085static bool
86xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110087 struct xfs_buf *bp)
88{
89 struct xfs_mount *mp = bp->b_target->bt_mount;
90 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
Dave Chinner20252072012-11-12 22:54:13 +110091
Dave Chinnercbc8adf2013-04-03 16:11:21 +110092 if (xfs_sb_version_hascrc(&mp->m_sb)) {
93 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
94
95 if (hdr3->magic != cpu_to_be32(XFS_DIR3_FREE_MAGIC))
96 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +100097 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinnercbc8adf2013-04-03 16:11:21 +110098 return false;
99 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
100 return false;
Brian Fostera45086e2015-10-12 15:59:25 +1100101 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn)))
102 return false;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100103 } else {
104 if (hdr->magic != cpu_to_be32(XFS_DIR2_FREE_MAGIC))
105 return false;
106 }
107
108 /* XXX: should bounds check the xfs_dir3_icfree_hdr here */
109
110 return true;
111}
112
113static void
114xfs_dir3_free_read_verify(
115 struct xfs_buf *bp)
116{
117 struct xfs_mount *mp = bp->b_target->bt_mount;
118
Eric Sandeence5028c2014-02-27 15:23:10 +1100119 if (xfs_sb_version_hascrc(&mp->m_sb) &&
120 !xfs_buf_verify_cksum(bp, XFS_DIR3_FREE_CRC_OFF))
Dave Chinner24513372014-06-25 14:58:08 +1000121 xfs_buf_ioerror(bp, -EFSBADCRC);
Eric Sandeence5028c2014-02-27 15:23:10 +1100122 else if (!xfs_dir3_free_verify(bp))
Dave Chinner24513372014-06-25 14:58:08 +1000123 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100124
125 if (bp->b_error)
126 xfs_verifier_error(bp);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100127}
Dave Chinner20252072012-11-12 22:54:13 +1100128
Dave Chinner612cfbf2012-11-14 17:52:32 +1100129static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100130xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100131 struct xfs_buf *bp)
132{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100133 struct xfs_mount *mp = bp->b_target->bt_mount;
134 struct xfs_buf_log_item *bip = bp->b_fspriv;
135 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
136
137 if (!xfs_dir3_free_verify(bp)) {
Dave Chinner24513372014-06-25 14:58:08 +1000138 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100139 xfs_verifier_error(bp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100140 return;
141 }
142
143 if (!xfs_sb_version_hascrc(&mp->m_sb))
144 return;
145
146 if (bip)
147 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
148
Eric Sandeenf1dbcd72014-02-27 15:18:23 +1100149 xfs_buf_update_cksum(bp, XFS_DIR3_FREE_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100150}
151
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100152const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100153 .verify_read = xfs_dir3_free_read_verify,
154 .verify_write = xfs_dir3_free_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100155};
Dave Chinner20252072012-11-12 22:54:13 +1100156
Dave Chinner612cfbf2012-11-14 17:52:32 +1100157
Dave Chinner20252072012-11-12 22:54:13 +1100158static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100159__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100160 struct xfs_trans *tp,
161 struct xfs_inode *dp,
162 xfs_dablk_t fbno,
163 xfs_daddr_t mappedbno,
164 struct xfs_buf **bpp)
165{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100166 int err;
167
168 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100169 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100170
171 /* try read returns without an error or *bpp if it lands in a hole */
172 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100173 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100174 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100175}
176
177int
178xfs_dir2_free_read(
179 struct xfs_trans *tp,
180 struct xfs_inode *dp,
181 xfs_dablk_t fbno,
182 struct xfs_buf **bpp)
183{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100184 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100185}
186
187static int
188xfs_dir2_free_try_read(
189 struct xfs_trans *tp,
190 struct xfs_inode *dp,
191 xfs_dablk_t fbno,
192 struct xfs_buf **bpp)
193{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100194 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
195}
196
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100197static int
198xfs_dir3_free_get_buf(
Dave Chinner2998ab12014-06-06 15:07:53 +1000199 xfs_da_args_t *args,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100200 xfs_dir2_db_t fbno,
201 struct xfs_buf **bpp)
202{
Dave Chinner2998ab12014-06-06 15:07:53 +1000203 struct xfs_trans *tp = args->trans;
204 struct xfs_inode *dp = args->dp;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100205 struct xfs_mount *mp = dp->i_mount;
206 struct xfs_buf *bp;
207 int error;
208 struct xfs_dir3_icfree_hdr hdr;
209
Dave Chinner2998ab12014-06-06 15:07:53 +1000210 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, fbno),
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100211 -1, &bp, XFS_DATA_FORK);
212 if (error)
213 return error;
214
Dave Chinner61fe1352013-04-03 16:11:30 +1100215 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100216 bp->b_ops = &xfs_dir3_free_buf_ops;
217
218 /*
219 * Initialize the new block to be empty, and remember
220 * its first slot as our empty slot.
221 */
Dave Chinnere400d272013-05-28 18:37:17 +1000222 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
223 memset(&hdr, 0, sizeof(hdr));
224
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100225 if (xfs_sb_version_hascrc(&mp->m_sb)) {
226 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
227
228 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000229
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100230 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
231 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
Eric Sandeence748ea2015-07-29 11:53:31 +1000232 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000233 } else
234 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100235 dp->d_ops->free_hdr_to_disk(bp->b_addr, &hdr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100236 *bpp = bp;
237 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Log entries from a freespace block.
242 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000243STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244xfs_dir2_free_log_bests(
Dave Chinnerbc851782014-06-06 15:20:54 +1000245 struct xfs_da_args *args,
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 Chinnerbc851782014-06-06 15:20:54 +1000254 bests = args->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 Chinnerbc851782014-06-06 15:20:54 +1000257 xfs_trans_log_buf(args->trans, 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 Chinnerbc851782014-06-06 15:20:54 +1000268 struct xfs_da_args *args,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000269 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Dave Chinner836a94a2013-08-12 20:49:44 +1000271#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 xfs_dir2_free_t *free; /* freespace structure */
273
Dave Chinner1d9025e2012-06-22 18:50:14 +1000274 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100275 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
276 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000277#endif
Dave Chinnerbc851782014-06-06 15:20:54 +1000278 xfs_trans_log_buf(args->trans, bp, 0,
279 args->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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int n; /* count of live freespc ents */
302 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100303 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100305 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000307 trace_xfs_dir2_leaf_to_node(args);
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 tp = args->trans;
311 /*
312 * Add a freespace block to the directory.
313 */
314 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
315 return error;
316 }
Dave Chinner30028032014-06-06 15:08:18 +1000317 ASSERT(fdb == xfs_dir2_byte_to_db(args->geo, XFS_DIR2_FREE_OFFSET));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Get the buffer for the new freespace block.
320 */
Dave Chinner2998ab12014-06-06 15:07:53 +1000321 error = xfs_dir3_free_get_buf(args, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100322 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100324
Dave Chinner1d9025e2012-06-22 18:50:14 +1000325 free = fbp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100326 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000327 leaf = lbp->b_addr;
Dave Chinner8f661932014-06-06 15:15:59 +1000328 ltp = xfs_dir2_leaf_tail_p(args->geo, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100329 ASSERT(be32_to_cpu(ltp->bestcount) <=
Dave Chinner8f661932014-06-06 15:15:59 +1000330 (uint)dp->i_d.di_size / args->geo->blksize);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * Copy freespace entries from the leaf block to the new block.
334 * Count active entries.
335 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100336 from = xfs_dir2_leaf_bests_p(ltp);
Dave Chinner24dd0f52013-10-30 13:48:41 -0500337 to = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100338 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100339 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100341 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100345 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100347 freehdr.nused = n;
348 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
349
Dave Chinner01ba43b2013-10-29 22:11:52 +1100350 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000351 xfs_dir2_free_log_bests(args, fbp, 0, freehdr.nvalid - 1);
352 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100353
Dave Chinner24df33b2013-04-12 07:30:21 +1000354 /*
355 * Converting the leaf to a leafnode is just a matter of changing the
356 * magic number and the ops. Do the change directly to the buffer as
357 * it's less work (and less code) than decoding the header to host
358 * format and back again.
359 */
360 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
361 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
362 else
363 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
364 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100365 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerbc851782014-06-06 15:20:54 +1000366 xfs_dir3_leaf_log_header(args, lbp);
Dave Chinner41419562013-10-29 22:11:50 +1100367 xfs_dir3_leaf_check(dp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371/*
372 * Add a leaf entry to a leaf block in a node-form directory.
373 * The other work necessary is done from the caller.
374 */
375static int /* error */
376xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000377 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 xfs_da_args_t *args, /* operation arguments */
379 int index) /* insertion pt for new entry */
380{
381 int compact; /* compacting stale leaves */
382 xfs_inode_t *dp; /* incore directory inode */
383 int highstale; /* next stale entry */
384 xfs_dir2_leaf_t *leaf; /* leaf structure */
385 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
386 int lfloghigh; /* high leaf entry logging */
387 int lfloglow; /* low leaf entry logging */
388 int lowstale; /* previous stale entry */
Dave Chinner24df33b2013-04-12 07:30:21 +1000389 struct xfs_dir3_icleaf_hdr leafhdr;
390 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000392 trace_xfs_dir2_leafn_add(args, index);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000395 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100396 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100397 ents = dp->d_ops->leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /*
400 * Quick check just to make sure we are not going to index
401 * into other peoples memory
402 */
403 if (index < 0)
Dave Chinner24513372014-06-25 14:58:08 +1000404 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /*
407 * If there are already the maximum number of leaf entries in
408 * the block, if there are no stale entries it won't fit.
409 * Caller will do a split. If there are stale entries we'll do
410 * a compact.
411 */
412
Dave Chinner8f661932014-06-06 15:15:59 +1000413 if (leafhdr.count == dp->d_ops->leaf_max_ents(args->geo)) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000414 if (!leafhdr.stale)
Dave Chinner24513372014-06-25 14:58:08 +1000415 return -ENOSPC;
Dave Chinner24df33b2013-04-12 07:30:21 +1000416 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 } else
418 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000419 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
420 ASSERT(index == leafhdr.count ||
421 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Barry Naujok6a178102008-05-21 16:42:05 +1000423 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425
426 /*
427 * Compact out all but one stale leaf entry. Leaves behind
428 * the entry closest to index.
429 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000430 if (compact)
431 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
432 &highstale, &lfloglow, &lfloghigh);
433 else if (leafhdr.stale) {
434 /*
435 * Set impossible logging indices for this case.
436 */
437 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 lfloghigh = -1;
439 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /*
442 * Insert the new entry, log everything.
443 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000444 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200445 highstale, &lfloglow, &lfloghigh);
446
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100447 lep->hashval = cpu_to_be32(args->hashval);
Dave Chinner30028032014-06-06 15:08:18 +1000448 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(args->geo,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100449 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000450
Dave Chinner01ba43b2013-10-29 22:11:52 +1100451 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +1000452 xfs_dir3_leaf_log_header(args, bp);
453 xfs_dir3_leaf_log_ents(args, bp, lfloglow, lfloghigh);
Dave Chinner41419562013-10-29 22:11:50 +1100454 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
456}
457
458#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100459static void
460xfs_dir2_free_hdr_check(
Dave Chinner01ba43b2013-10-29 22:11:52 +1100461 struct xfs_inode *dp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100462 struct xfs_buf *bp,
463 xfs_dir2_db_t db)
464{
465 struct xfs_dir3_icfree_hdr hdr;
466
Dave Chinner01ba43b2013-10-29 22:11:52 +1100467 dp->d_ops->free_hdr_from_disk(&hdr, bp->b_addr);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100468
Dave Chinner8f661932014-06-06 15:15:59 +1000469 ASSERT((hdr.firstdb %
470 dp->d_ops->free_max_bests(dp->i_mount->m_dir_geo)) == 0);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100471 ASSERT(hdr.firstdb <= db);
472 ASSERT(db < hdr.firstdb + hdr.nvalid);
473}
474#else
Dave Chinner01ba43b2013-10-29 22:11:52 +1100475#define xfs_dir2_free_hdr_check(dp, bp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif /* DEBUG */
477
478/*
479 * Return the last hash value in the leaf.
480 * Stale entries are ok.
481 */
482xfs_dahash_t /* hash value */
483xfs_dir2_leafn_lasthash(
Dave Chinner41419562013-10-29 22:11:50 +1100484 struct xfs_inode *dp,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000485 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int *count) /* count of entries in leaf */
487{
Dave Chinner24df33b2013-04-12 07:30:21 +1000488 struct xfs_dir2_leaf *leaf = bp->b_addr;
489 struct xfs_dir2_leaf_entry *ents;
490 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dave Chinner01ba43b2013-10-29 22:11:52 +1100492 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000493
494 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
495 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000498 *count = leafhdr.count;
499 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000501
Dave Chinner41419562013-10-29 22:11:50 +1100502 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000503 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000507 * Look up a leaf entry for space to add a name in a node-format leaf block.
508 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000510STATIC int
511xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000512 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 xfs_da_args_t *args, /* operation arguments */
514 int *indexp, /* out: leaf entry index */
515 xfs_da_state_t *state) /* state to fill in */
516{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000517 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000518 xfs_dir2_db_t curdb = -1; /* current data block number */
519 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 xfs_inode_t *dp; /* incore directory inode */
521 int error; /* error return value */
522 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000523 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int index; /* leaf entry index */
525 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000526 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
528 xfs_mount_t *mp; /* filesystem mount point */
529 xfs_dir2_db_t newdb; /* new data block number */
530 xfs_dir2_db_t newfdb; /* new free block number */
531 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000532 struct xfs_dir2_leaf_entry *ents;
533 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 dp = args->dp;
536 tp = args->trans;
537 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000538 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100539 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100540 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000541
Dave Chinner41419562013-10-29 22:11:50 +1100542 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000543 ASSERT(leafhdr.count > 0);
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /*
546 * Look up the hash value in the leaf entries.
547 */
548 index = xfs_dir2_leaf_search_hash(args, bp);
549 /*
550 * Do we have a buffer coming in?
551 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000552 if (state->extravalid) {
553 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000555 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000556 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100557 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
558 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Dave Chinner9d23fc82013-10-29 22:11:48 +1100560 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * Loop over leaf entries with the right hash value.
563 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000564 for (lep = &ents[index];
565 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
566 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * Skip stale leaf entries.
569 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100570 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 continue;
572 /*
573 * Pull the data block number from the entry.
574 */
Dave Chinner30028032014-06-06 15:08:18 +1000575 newdb = xfs_dir2_dataptr_to_db(args->geo,
576 be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * For addname, we're looking for a place to put the new entry.
579 * We want to use a data block with an entry of equal
580 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000581 *
582 * If this block isn't the data block we already have
583 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000585 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100586 __be16 *bests;
587
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000588 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000590 * Convert the data block to the free block
591 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 */
Dave Chinner8f661932014-06-06 15:15:59 +1000593 newfdb = dp->d_ops->db_to_fdb(args->geo, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000595 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000597 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
601 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000602 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100603
604 error = xfs_dir2_free_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +1000605 xfs_dir2_db_to_da(args->geo,
606 newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100607 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000608 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000610 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100611
Dave Chinner01ba43b2013-10-29 22:11:52 +1100612 xfs_dir2_free_hdr_check(dp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000615 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Dave Chinner8f661932014-06-06 15:15:59 +1000617 fi = dp->d_ops->db_to_fdindex(args->geo, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000619 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Dave Chinner24dd0f52013-10-30 13:48:41 -0500621 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100622 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000623 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
624 XFS_ERRLEVEL_LOW, mp);
625 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000626 xfs_trans_brelse(tp, curbp);
Dave Chinner24513372014-06-25 14:58:08 +1000627 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000629 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100630 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000634 /* Didn't find any space */
635 fi = -1;
636out:
Barry Naujok6a178102008-05-21 16:42:05 +1000637 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000638 if (curbp) {
639 /* Giving back a free block. */
640 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 state->extrablk.index = fi;
643 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100644
645 /*
646 * Important: this magic number is not in the buffer - it's for
647 * buffer type information and therefore only the free/data type
648 * matters here, not whether CRCs are enabled or not.
649 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
651 } else {
652 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000655 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000658 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 * Look up a leaf entry in a node-format leaf block.
663 * The extrablk in state a data block.
664 */
665STATIC int
666xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000667 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000668 xfs_da_args_t *args, /* operation arguments */
669 int *indexp, /* out: leaf entry index */
670 xfs_da_state_t *state) /* state to fill in */
671{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000672 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000673 xfs_dir2_db_t curdb = -1; /* current data block number */
674 xfs_dir2_data_entry_t *dep; /* data block entry */
675 xfs_inode_t *dp; /* incore directory inode */
676 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000677 int index; /* leaf entry index */
678 xfs_dir2_leaf_t *leaf; /* leaf structure */
679 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
680 xfs_mount_t *mp; /* filesystem mount point */
681 xfs_dir2_db_t newdb; /* new data block number */
682 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000683 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000684 struct xfs_dir2_leaf_entry *ents;
685 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000686
687 dp = args->dp;
688 tp = args->trans;
689 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000690 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100691 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +1100692 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000693
Dave Chinner41419562013-10-29 22:11:50 +1100694 xfs_dir3_leaf_check(dp, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000695 ASSERT(leafhdr.count > 0);
696
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000697 /*
698 * Look up the hash value in the leaf entries.
699 */
700 index = xfs_dir2_leaf_search_hash(args, bp);
701 /*
702 * Do we have a buffer coming in?
703 */
704 if (state->extravalid) {
705 curbp = state->extrablk.bp;
706 curdb = state->extrablk.blkno;
707 }
708 /*
709 * Loop over leaf entries with the right hash value.
710 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000711 for (lep = &ents[index];
712 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
713 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000714 /*
715 * Skip stale leaf entries.
716 */
717 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
718 continue;
719 /*
720 * Pull the data block number from the entry.
721 */
Dave Chinner30028032014-06-06 15:08:18 +1000722 newdb = xfs_dir2_dataptr_to_db(args->geo,
723 be32_to_cpu(lep->address));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000724 /*
725 * Not adding a new entry, so we really want to find
726 * the name given to us.
727 *
728 * If it's a different data block, go get it.
729 */
730 if (newdb != curdb) {
731 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000732 * If we had a block before that we aren't saving
733 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000734 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000735 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
736 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000737 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000738 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000739 * If needing the block that is saved with a CI match,
740 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000741 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000742 if (args->cmpresult != XFS_CMP_DIFFERENT &&
743 newdb == state->extrablk.blkno) {
744 ASSERT(state->extravalid);
745 curbp = state->extrablk.bp;
746 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100747 error = xfs_dir3_data_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +1000748 xfs_dir2_db_to_da(args->geo,
749 newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100750 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000751 if (error)
752 return error;
753 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100754 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000755 curdb = newdb;
756 }
757 /*
758 * Point to the data entry.
759 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000760 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Dave Chinner30028032014-06-06 15:08:18 +1000761 xfs_dir2_dataptr_to_off(args->geo,
762 be32_to_cpu(lep->address)));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000763 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000764 * Compare the entry and if it's an exact match, return
765 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000766 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000767 */
Barry Naujok5163f952008-05-21 16:41:01 +1000768 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
769 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000770 /* If there is a CI match block, drop it */
771 if (args->cmpresult != XFS_CMP_DIFFERENT &&
772 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000773 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000774 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000775 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner9d23fc82013-10-29 22:11:48 +1100776 args->filetype = dp->d_ops->data_get_ftype(dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000777 *indexp = index;
778 state->extravalid = 1;
779 state->extrablk.bp = curbp;
780 state->extrablk.blkno = curdb;
781 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000782 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000783 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100784 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100785 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000786 if (cmp == XFS_CMP_EXACT)
Dave Chinner24513372014-06-25 14:58:08 +1000787 return -EEXIST;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000788 }
789 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000790 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000791 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000792 if (args->cmpresult == XFS_CMP_DIFFERENT) {
793 /* Giving back last used data block. */
794 state->extravalid = 1;
795 state->extrablk.bp = curbp;
796 state->extrablk.index = -1;
797 state->extrablk.blkno = curdb;
798 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100799 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100800 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000801 } else {
802 /* If the curbp is not the CI match block, drop it */
803 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000804 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000805 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000806 } else {
807 state->extravalid = 0;
808 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809 *indexp = index;
Dave Chinner24513372014-06-25 14:58:08 +1000810 return -ENOENT;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000811}
812
813/*
814 * Look up a leaf entry in a node-format leaf block.
815 * If this is an addname then the extrablk in state is a freespace block,
816 * otherwise it's a data block.
817 */
818int
819xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000820 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000821 xfs_da_args_t *args, /* operation arguments */
822 int *indexp, /* out: leaf entry index */
823 xfs_da_state_t *state) /* state to fill in */
824{
Barry Naujok6a178102008-05-21 16:42:05 +1000825 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000826 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
827 state);
828 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
829}
830
831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * Move count leaf entries from source to destination leaf.
833 * Log entries and headers. Stale entries are preserved.
834 */
835static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000836xfs_dir3_leafn_moveents(
837 xfs_da_args_t *args, /* operation arguments */
838 struct xfs_buf *bp_s, /* source */
839 struct xfs_dir3_icleaf_hdr *shdr,
840 struct xfs_dir2_leaf_entry *sents,
841 int start_s,/* source leaf index */
842 struct xfs_buf *bp_d, /* destination */
843 struct xfs_dir3_icleaf_hdr *dhdr,
844 struct xfs_dir2_leaf_entry *dents,
845 int start_d,/* destination leaf index */
846 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Dave Chinner24df33b2013-04-12 07:30:21 +1000848 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000850 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
853 * Silently return if nothing to do.
854 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000855 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /*
859 * If the destination index is not the end of the current
860 * destination leaf entries, open up a hole in the destination
861 * to hold the new entries.
862 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000863 if (start_d < dhdr->count) {
864 memmove(&dents[start_d + count], &dents[start_d],
865 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000866 xfs_dir3_leaf_log_ents(args, bp_d, start_d + count,
Dave Chinner24df33b2013-04-12 07:30:21 +1000867 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 /*
870 * If the source has stale leaves, count the ones in the copy range
871 * so we can update the header correctly.
872 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000873 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 int i; /* temp leaf index */
875
876 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000877 if (sents[i].address ==
878 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 stale++;
880 }
881 } else
882 stale = 0;
883 /*
884 * Copy the leaf entries from source to destination.
885 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000886 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000888 xfs_dir3_leaf_log_ents(args, bp_d, start_d, start_d + count - 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * If there are source entries after the ones we copied,
892 * delete the ones we copied by sliding the next ones down.
893 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000894 if (start_s + count < shdr->count) {
895 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinnerbc851782014-06-06 15:20:54 +1000897 xfs_dir3_leaf_log_ents(args, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Update the headers and log them.
902 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000903 shdr->count -= count;
904 shdr->stale -= stale;
905 dhdr->count += count;
906 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
910 * Determine the sort order of two leaf blocks.
911 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
912 */
913int /* sort order */
914xfs_dir2_leafn_order(
Dave Chinner41419562013-10-29 22:11:50 +1100915 struct xfs_inode *dp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
917 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Dave Chinner24df33b2013-04-12 07:30:21 +1000919 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
920 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
921 struct xfs_dir2_leaf_entry *ents1;
922 struct xfs_dir2_leaf_entry *ents2;
923 struct xfs_dir3_icleaf_hdr hdr1;
924 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Dave Chinner01ba43b2013-10-29 22:11:52 +1100926 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
927 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100928 ents1 = dp->d_ops->leaf_ents_p(leaf1);
929 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000930
931 if (hdr1.count > 0 && hdr2.count > 0 &&
932 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
933 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
934 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 1;
936 return 0;
937}
938
939/*
940 * Rebalance leaf entries between two leaf blocks.
941 * This is actually only called when the second block is new,
942 * though the code deals with the general case.
943 * A new entry will be inserted in one of the blocks, and that
944 * entry is taken into account when balancing.
945 */
946static void
947xfs_dir2_leafn_rebalance(
948 xfs_da_state_t *state, /* btree cursor */
949 xfs_da_state_blk_t *blk1, /* first btree block */
950 xfs_da_state_blk_t *blk2) /* second btree block */
951{
952 xfs_da_args_t *args; /* operation arguments */
953 int count; /* count (& direction) leaves */
954 int isleft; /* new goes in left leaf */
955 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
956 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
957 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +1000958#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int oldstale; /* old count of stale leaves */
960#endif
961 int oldsum; /* old total leaf count */
962 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +1000963 struct xfs_dir2_leaf_entry *ents1;
964 struct xfs_dir2_leaf_entry *ents2;
965 struct xfs_dir3_icleaf_hdr hdr1;
966 struct xfs_dir3_icleaf_hdr hdr2;
Dave Chinner41419562013-10-29 22:11:50 +1100967 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 args = state->args;
970 /*
971 * If the block order is wrong, swap the arguments.
972 */
Dave Chinner41419562013-10-29 22:11:50 +1100973 if ((swap = xfs_dir2_leafn_order(dp, blk1->bp, blk2->bp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 xfs_da_state_blk_t *tmp; /* temp for block swap */
975
976 tmp = blk1;
977 blk1 = blk2;
978 blk2 = tmp;
979 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000980 leaf1 = blk1->bp->b_addr;
981 leaf2 = blk2->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +1100982 dp->d_ops->leaf_hdr_from_disk(&hdr1, leaf1);
983 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf2);
Dave Chinner41419562013-10-29 22:11:50 +1100984 ents1 = dp->d_ops->leaf_ents_p(leaf1);
985 ents2 = dp->d_ops->leaf_ents_p(leaf2);
Dave Chinner24df33b2013-04-12 07:30:21 +1000986
987 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +1000988#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +1000989 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990#endif
991 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /*
994 * If the old leaf count was odd then the new one will be even,
995 * so we need to divide the new count evenly.
996 */
997 if (oldsum & 1) {
998 xfs_dahash_t midhash; /* middle entry hash value */
999
Dave Chinner24df33b2013-04-12 07:30:21 +10001000 if (mid >= hdr1.count)
1001 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001003 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 isleft = args->hashval <= midhash;
1005 }
1006 /*
1007 * If the old count is even then the new count is odd, so there's
1008 * no preferred side for the new entry.
1009 * Pick the left one.
1010 */
1011 else
1012 isleft = 1;
1013 /*
1014 * Calculate moved entry count. Positive means left-to-right,
1015 * negative means right-to-left. Then move the entries.
1016 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001017 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001019 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1020 hdr1.count - count, blk2->bp,
1021 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001023 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1024 blk1->bp, &hdr1, ents1,
1025 hdr1.count, count);
1026
1027 ASSERT(hdr1.count + hdr2.count == oldsum);
1028 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1029
1030 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001031 dp->d_ops->leaf_hdr_to_disk(leaf1, &hdr1);
1032 dp->d_ops->leaf_hdr_to_disk(leaf2, &hdr2);
Dave Chinnerbc851782014-06-06 15:20:54 +10001033 xfs_dir3_leaf_log_header(args, blk1->bp);
1034 xfs_dir3_leaf_log_header(args, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001035
Dave Chinner41419562013-10-29 22:11:50 +11001036 xfs_dir3_leaf_check(dp, blk1->bp);
1037 xfs_dir3_leaf_check(dp, blk2->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /*
1040 * Mark whether we're inserting into the old or new leaf.
1041 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001042 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 state->inleaf = !swap;
1046 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001047 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Adjust the expected index for insertion.
1050 */
1051 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001052 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001053
1054 /*
1055 * Finally sanity check just to make sure we are not returning a
1056 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Dave Chinner41419562013-10-29 22:11:50 +11001058 if (blk2->index < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 state->inleaf = 1;
1060 blk2->index = 0;
Dave Chinner41419562013-10-29 22:11:50 +11001061 xfs_alert(dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001062 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001063 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065}
1066
Dave Chinner20252072012-11-12 22:54:13 +11001067static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001068xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001069 xfs_da_args_t *args,
1070 struct xfs_dir2_data_hdr *hdr,
1071 struct xfs_dir2_free *free,
1072 xfs_dir2_db_t fdb,
1073 int findex,
1074 struct xfs_buf *fbp,
1075 int longest)
1076{
Dave Chinner20252072012-11-12 22:54:13 +11001077 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001078 __be16 *bests;
1079 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001080 struct xfs_inode *dp = args->dp;
Dave Chinner20252072012-11-12 22:54:13 +11001081
Dave Chinner01ba43b2013-10-29 22:11:52 +11001082 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner24dd0f52013-10-30 13:48:41 -05001083 bests = dp->d_ops->free_bests_p(free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001084 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001085 /*
1086 * Data block is not empty, just set the free entry to the new
1087 * value.
1088 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001089 bests[findex] = cpu_to_be16(longest);
Dave Chinnerbc851782014-06-06 15:20:54 +10001090 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001091 return 0;
1092 }
1093
1094 /* One less used entry in the free table. */
1095 freehdr.nused--;
1096
1097 /*
1098 * If this was the last entry in the table, we can trim the table size
1099 * back. There might be other entries at the end referring to
1100 * non-existent data blocks, get those too.
1101 */
1102 if (findex == freehdr.nvalid - 1) {
1103 int i; /* free entry index */
1104
1105 for (i = findex - 1; i >= 0; i--) {
1106 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1107 break;
1108 }
1109 freehdr.nvalid = i + 1;
1110 logfree = 0;
1111 } else {
1112 /* Not the last entry, just punch it out. */
1113 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001114 logfree = 1;
1115 }
1116
Dave Chinner01ba43b2013-10-29 22:11:52 +11001117 dp->d_ops->free_hdr_to_disk(free, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001118 xfs_dir2_free_log_header(args, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001119
1120 /*
1121 * If there are no useful entries left in the block, get rid of the
1122 * block if we can.
1123 */
1124 if (!freehdr.nused) {
1125 int error;
1126
1127 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1128 if (error == 0) {
1129 fbp = NULL;
1130 logfree = 0;
Dave Chinner24513372014-06-25 14:58:08 +10001131 } else if (error != -ENOSPC || args->total != 0)
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001132 return error;
1133 /*
1134 * It's possible to get ENOSPC if there is no
1135 * space reservation. In this case some one
1136 * else will eventually get rid of this block.
1137 */
1138 }
1139
Dave Chinner20252072012-11-12 22:54:13 +11001140 /* Log the free entry that changed, unless we got rid of it. */
1141 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001142 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Dave Chinner20252072012-11-12 22:54:13 +11001143 return 0;
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/*
1147 * Remove an entry from a node directory.
1148 * This removes the leaf entry and the data entry,
1149 * and updates the free block if necessary.
1150 */
1151static int /* error */
1152xfs_dir2_leafn_remove(
1153 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001154 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 int index, /* leaf entry index */
1156 xfs_da_state_blk_t *dblk, /* data block */
1157 int *rval) /* resulting block needs join */
1158{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001159 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001161 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 xfs_dir2_data_entry_t *dep; /* data block entry */
1163 xfs_inode_t *dp; /* incore directory inode */
1164 xfs_dir2_leaf_t *leaf; /* leaf structure */
1165 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1166 int longest; /* longest data free entry */
1167 int off; /* data block entry offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int needlog; /* need to log data header */
1169 int needscan; /* need to rescan data frees */
1170 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001171 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001172 struct xfs_dir3_icleaf_hdr leafhdr;
1173 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001175 trace_xfs_dir2_leafn_remove(args, index);
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 dp = args->dp;
1178 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001179 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001180 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001181 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /*
1184 * Point to the entry we're removing.
1185 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001186 lep = &ents[index];
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * Extract the data block and offset from the entry.
1190 */
Dave Chinner30028032014-06-06 15:08:18 +10001191 db = xfs_dir2_dataptr_to_db(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ASSERT(dblk->blkno == db);
Dave Chinner30028032014-06-06 15:08:18 +10001193 off = xfs_dir2_dataptr_to_off(args->geo, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /*
1197 * Kill the leaf entry by marking it stale.
1198 * Log the leaf block changes.
1199 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001200 leafhdr.stale++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001201 dp->d_ops->leaf_hdr_to_disk(leaf, &leafhdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001202 xfs_dir3_leaf_log_header(args, bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001203
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001204 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinnerbc851782014-06-06 15:20:54 +10001205 xfs_dir3_leaf_log_ents(args, bp, index, index);
Dave Chinner24df33b2013-04-12 07:30:21 +10001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 /*
1208 * Make the data entry free. Keep track of the longest freespace
1209 * in the data block in case it changes.
1210 */
1211 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001212 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001213 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner2ca98772013-10-29 22:11:49 +11001214 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001215 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 needlog = needscan = 0;
Dave Chinnerbc851782014-06-06 15:20:54 +10001217 xfs_dir2_data_make_free(args, dbp, off,
Dave Chinner9d23fc82013-10-29 22:11:48 +11001218 dp->d_ops->data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 /*
1220 * Rescan the data block freespaces for bestfree.
1221 * Log the data block header if needed.
1222 */
1223 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001224 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001226 xfs_dir2_data_log_header(args, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001227 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 /*
1229 * If the longest data block freespace changes, need to update
1230 * the corresponding freeblock entry.
1231 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001232 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001234 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 xfs_dir2_db_t fdb; /* freeblock block number */
1236 int findex; /* index in freeblock entries */
1237 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
1240 * Convert the data block number to a free block,
1241 * read in the free block.
1242 */
Dave Chinner8f661932014-06-06 15:15:59 +10001243 fdb = dp->d_ops->db_to_fdb(args->geo, db);
Dave Chinner2998ab12014-06-06 15:07:53 +10001244 error = xfs_dir2_free_read(tp, dp,
1245 xfs_dir2_db_to_da(args->geo, fdb),
Dave Chinner20252072012-11-12 22:54:13 +11001246 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001247 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001249 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001250#ifdef DEBUG
1251 {
1252 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001253 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinner8f661932014-06-06 15:15:59 +10001254 ASSERT(freehdr.firstdb == dp->d_ops->free_max_bests(args->geo) *
Dave Chinner30028032014-06-06 15:08:18 +10001255 (fdb - xfs_dir2_byte_to_db(args->geo,
1256 XFS_DIR2_FREE_OFFSET)));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001257 }
1258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Calculate which entry we need to fix.
1261 */
Dave Chinner8f661932014-06-06 15:15:59 +10001262 findex = dp->d_ops->db_to_fdindex(args->geo, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001263 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
1265 * If the data block is now empty we can get rid of it
1266 * (usually).
1267 */
Dave Chinner8f661932014-06-06 15:15:59 +10001268 if (longest == args->geo->blksize -
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001269 dp->d_ops->data_entry_offset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /*
1271 * Try to punch out the data block.
1272 */
1273 error = xfs_dir2_shrink_inode(args, db, dbp);
1274 if (error == 0) {
1275 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001276 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 /*
1279 * We can get ENOSPC if there's no space reservation.
1280 * In this case just drop the buffer and some one else
1281 * will eventually get rid of the empty block.
1282 */
Dave Chinner24513372014-06-25 14:58:08 +10001283 else if (!(error == -ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return error;
1285 }
1286 /*
1287 * If we got rid of the data block, we can eliminate that entry
1288 * in the free block.
1289 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001290 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001291 fdb, findex, fbp, longest);
1292 if (error)
1293 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Dave Chinner20252072012-11-12 22:54:13 +11001295
Dave Chinner41419562013-10-29 22:11:50 +11001296 xfs_dir3_leaf_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001298 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * to justify trying to join it with a neighbor.
1300 */
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001301 *rval = (dp->d_ops->leaf_hdr_size +
Dave Chinner24df33b2013-04-12 07:30:21 +10001302 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Dave Chinnered358c02014-06-06 15:18:10 +10001303 args->geo->magicpct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
1307/*
1308 * Split the leaf entries in the old block into old and new blocks.
1309 */
1310int /* error */
1311xfs_dir2_leafn_split(
1312 xfs_da_state_t *state, /* btree cursor */
1313 xfs_da_state_blk_t *oldblk, /* original block */
1314 xfs_da_state_blk_t *newblk) /* newly created block */
1315{
1316 xfs_da_args_t *args; /* operation arguments */
1317 xfs_dablk_t blkno; /* new leaf block number */
1318 int error; /* error return value */
Dave Chinner41419562013-10-29 22:11:50 +11001319 struct xfs_inode *dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /*
1322 * Allocate space for a new leaf node.
1323 */
1324 args = state->args;
Dave Chinner41419562013-10-29 22:11:50 +11001325 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1327 error = xfs_da_grow_inode(args, &blkno);
1328 if (error) {
1329 return error;
1330 }
1331 /*
1332 * Initialize the new leaf block.
1333 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001334 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(args->geo, blkno),
Dave Chinner24df33b2013-04-12 07:30:21 +10001335 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1336 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 newblk->blkno = blkno;
1340 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1341 /*
1342 * Rebalance the entries across the two leaves, link the new
1343 * block into the leaves.
1344 */
1345 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001346 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 if (error) {
1348 return error;
1349 }
1350 /*
1351 * Insert the new entry in the correct block.
1352 */
1353 if (state->inleaf)
1354 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1355 else
1356 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1357 /*
1358 * Update last hashval in each block since we added the name.
1359 */
Dave Chinner41419562013-10-29 22:11:50 +11001360 oldblk->hashval = xfs_dir2_leafn_lasthash(dp, oldblk->bp, NULL);
1361 newblk->hashval = xfs_dir2_leafn_lasthash(dp, newblk->bp, NULL);
1362 xfs_dir3_leaf_check(dp, oldblk->bp);
1363 xfs_dir3_leaf_check(dp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return error;
1365}
1366
1367/*
1368 * Check a leaf block and its neighbors to see if the block should be
1369 * collapsed into one or the other neighbor. Always keep the block
1370 * with the smaller block number.
1371 * If the current block is over 50% full, don't try to join it, return 0.
1372 * If the block is empty, fill in the state structure and return 2.
1373 * If it can be collapsed, fill in the state structure and return 1.
1374 * If nothing can be done, return 0.
1375 */
1376int /* error */
1377xfs_dir2_leafn_toosmall(
1378 xfs_da_state_t *state, /* btree cursor */
1379 int *action) /* resulting action to take */
1380{
1381 xfs_da_state_blk_t *blk; /* leaf block */
1382 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001383 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int bytes; /* bytes in use */
1385 int count; /* leaf live entry count */
1386 int error; /* error return value */
1387 int forward; /* sibling block direction */
1388 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 xfs_dir2_leaf_t *leaf; /* leaf structure */
1390 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001391 struct xfs_dir3_icleaf_hdr leafhdr;
1392 struct xfs_dir2_leaf_entry *ents;
Dave Chinner41419562013-10-29 22:11:50 +11001393 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /*
1396 * Check for the degenerate case of the block being over 50% full.
1397 * If so, it's not worth even looking to see if we might be able
1398 * to coalesce with a sibling.
1399 */
1400 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001401 leaf = blk->bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001402 dp->d_ops->leaf_hdr_from_disk(&leafhdr, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001403 ents = dp->d_ops->leaf_ents_p(leaf);
1404 xfs_dir3_leaf_check(dp, blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001405
1406 count = leafhdr.count - leafhdr.stale;
Dave Chinner1c9a5b22013-10-30 09:15:02 +11001407 bytes = dp->d_ops->leaf_hdr_size + count * sizeof(ents[0]);
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001408 if (bytes > (state->args->geo->blksize >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /*
1410 * Blk over 50%, don't try to join.
1411 */
1412 *action = 0;
1413 return 0;
1414 }
1415 /*
1416 * Check for the degenerate case of the block being empty.
1417 * If the block is empty, we'll simply delete it, no need to
1418 * coalesce it with a sibling block. We choose (arbitrarily)
1419 * to merge with the forward block unless it is NULL.
1420 */
1421 if (count == 0) {
1422 /*
1423 * Make altpath point to the block we want to keep and
1424 * path point to the block we want to drop (this one).
1425 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001426 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001428 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 &rval);
1430 if (error)
1431 return error;
1432 *action = rval ? 2 : 0;
1433 return 0;
1434 }
1435 /*
1436 * Examine each sibling block to see if we can coalesce with
1437 * at least 25% free space to spare. We need to figure out
1438 * whether to merge with the forward or the backward block.
1439 * We prefer coalescing with the lower numbered sibling so as
1440 * to shrink a directory over time.
1441 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001442 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001444 struct xfs_dir3_icleaf_hdr hdr2;
1445
1446 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (blkno == 0)
1448 continue;
1449 /*
1450 * Read the sibling leaf block.
1451 */
Dave Chinner41419562013-10-29 22:11:50 +11001452 error = xfs_dir3_leafn_read(state->args->trans, dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001453 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001454 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /*
1458 * Count bytes in the two blocks combined.
1459 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001460 count = leafhdr.count - leafhdr.stale;
Dave Chinnerb2a21e72014-06-06 15:22:04 +10001461 bytes = state->args->geo->blksize -
1462 (state->args->geo->blksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001463
Dave Chinner1d9025e2012-06-22 18:50:14 +10001464 leaf = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001465 dp->d_ops->leaf_hdr_from_disk(&hdr2, leaf);
Dave Chinner41419562013-10-29 22:11:50 +11001466 ents = dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001467 count += hdr2.count - hdr2.stale;
1468 bytes -= count * sizeof(ents[0]);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /*
1471 * Fits with at least 25% to spare.
1472 */
1473 if (bytes >= 0)
1474 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001475 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477 /*
1478 * Didn't like either block, give up.
1479 */
1480 if (i >= 2) {
1481 *action = 0;
1482 return 0;
1483 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /*
1486 * Make altpath point to the block we want to keep (the lower
1487 * numbered block) and path point to the block we want to drop.
1488 */
1489 memcpy(&state->altpath, &state->path, sizeof(state->path));
1490 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001491 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 &rval);
1493 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001494 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 &rval);
1496 if (error) {
1497 return error;
1498 }
1499 *action = rval ? 0 : 1;
1500 return 0;
1501}
1502
1503/*
1504 * Move all the leaf entries from drop_blk to save_blk.
1505 * This is done as part of a join operation.
1506 */
1507void
1508xfs_dir2_leafn_unbalance(
1509 xfs_da_state_t *state, /* cursor */
1510 xfs_da_state_blk_t *drop_blk, /* dead block */
1511 xfs_da_state_blk_t *save_blk) /* surviving block */
1512{
1513 xfs_da_args_t *args; /* operation arguments */
1514 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1515 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001516 struct xfs_dir3_icleaf_hdr savehdr;
1517 struct xfs_dir3_icleaf_hdr drophdr;
1518 struct xfs_dir2_leaf_entry *sents;
1519 struct xfs_dir2_leaf_entry *dents;
Dave Chinner41419562013-10-29 22:11:50 +11001520 struct xfs_inode *dp = state->args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 args = state->args;
1523 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1524 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001525 drop_leaf = drop_blk->bp->b_addr;
1526 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001527
Dave Chinner01ba43b2013-10-29 22:11:52 +11001528 dp->d_ops->leaf_hdr_from_disk(&savehdr, save_leaf);
1529 dp->d_ops->leaf_hdr_from_disk(&drophdr, drop_leaf);
1530 sents = dp->d_ops->leaf_ents_p(save_leaf);
1531 dents = dp->d_ops->leaf_ents_p(drop_leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 * If there are any stale leaf entries, take this opportunity
1535 * to purge them.
1536 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001537 if (drophdr.stale)
1538 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1539 if (savehdr.stale)
1540 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /*
1543 * Move the entries from drop to the appropriate end of save.
1544 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001545 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Dave Chinner41419562013-10-29 22:11:50 +11001546 if (xfs_dir2_leafn_order(dp, save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001547 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1548 save_blk->bp, &savehdr, sents, 0,
1549 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001551 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1552 save_blk->bp, &savehdr, sents,
1553 savehdr.count, drophdr.count);
1554 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1555
1556 /* log the changes made when moving the entries */
Dave Chinner01ba43b2013-10-29 22:11:52 +11001557 dp->d_ops->leaf_hdr_to_disk(save_leaf, &savehdr);
1558 dp->d_ops->leaf_hdr_to_disk(drop_leaf, &drophdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001559 xfs_dir3_leaf_log_header(args, save_blk->bp);
1560 xfs_dir3_leaf_log_header(args, drop_blk->bp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001561
Dave Chinner41419562013-10-29 22:11:50 +11001562 xfs_dir3_leaf_check(dp, save_blk->bp);
1563 xfs_dir3_leaf_check(dp, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * Top-level node form directory addname routine.
1568 */
1569int /* error */
1570xfs_dir2_node_addname(
1571 xfs_da_args_t *args) /* operation arguments */
1572{
1573 xfs_da_state_blk_t *blk; /* leaf block for insert */
1574 int error; /* error return value */
1575 int rval; /* sub-return value */
1576 xfs_da_state_t *state; /* btree cursor */
1577
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001578 trace_xfs_dir2_node_addname(args);
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /*
1581 * Allocate and initialize the state (btree cursor).
1582 */
1583 state = xfs_da_state_alloc();
1584 state->args = args;
1585 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Look up the name. We're not supposed to find it, but
1588 * this gives us the insertion point.
1589 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001590 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (error)
1592 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10001593 if (rval != -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto done;
1595 }
1596 /*
1597 * Add the data entry to a data block.
1598 * Extravalid is set to a freeblock found by lookup.
1599 */
1600 rval = xfs_dir2_node_addname_int(args,
1601 state->extravalid ? &state->extrablk : NULL);
1602 if (rval) {
1603 goto done;
1604 }
1605 blk = &state->path.blk[state->path.active - 1];
1606 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1607 /*
1608 * Add the new leaf entry.
1609 */
1610 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1611 if (rval == 0) {
1612 /*
1613 * It worked, fix the hash values up the btree.
1614 */
Barry Naujok6a178102008-05-21 16:42:05 +10001615 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001616 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 } else {
1618 /*
1619 * It didn't work, we need to split the leaf block.
1620 */
1621 if (args->total == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001622 ASSERT(rval == -ENOSPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto done;
1624 }
1625 /*
1626 * Split the leaf block and insert the new entry.
1627 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001628 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630done:
1631 xfs_da_state_free(state);
1632 return rval;
1633}
1634
1635/*
1636 * Add the data entry for a node-format directory name addition.
1637 * The leaf entry is added in xfs_dir2_leafn_add.
1638 * We may enter with a freespace block that the lookup found.
1639 */
1640static int /* error */
1641xfs_dir2_node_addname_int(
1642 xfs_da_args_t *args, /* operation arguments */
1643 xfs_da_state_blk_t *fblk) /* optional freespace block */
1644{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001645 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001647 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1649 xfs_inode_t *dp; /* incore directory inode */
1650 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1651 int error; /* error return value */
1652 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001653 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int findex; /* freespace entry index */
1655 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1656 xfs_dir2_db_t ifbno; /* initial freespace block no */
1657 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1658 int length; /* length of the new entry */
1659 int logfree; /* need to log free entry */
1660 xfs_mount_t *mp; /* filesystem mount point */
1661 int needlog; /* need to log data header */
1662 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001663 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001665 __be16 *bests;
1666 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001667 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 dp = args->dp;
1670 mp = dp->i_mount;
1671 tp = args->trans;
Dave Chinner9d23fc82013-10-29 22:11:48 +11001672 length = dp->d_ops->data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /*
1674 * If we came in with a freespace block that means that lookup
1675 * found an entry with our hash value. This is the freespace
1676 * block for that data entry.
1677 */
1678 if (fblk) {
1679 fbp = fblk->bp;
1680 /*
1681 * Remember initial freespace block number.
1682 */
1683 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001684 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 findex = fblk->index;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001686 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001687 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
1690 * This means the free entry showed that the data block had
1691 * space for our entry, so we remembered it.
1692 * Use that data block.
1693 */
1694 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001695 ASSERT(findex < freehdr.nvalid);
1696 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1697 ASSERT(be16_to_cpu(bests[findex]) >= length);
1698 dbno = freehdr.firstdb + findex;
1699 } else {
1700 /*
1701 * The data block looked at didn't have enough room.
1702 * We'll start at the beginning of the freespace entries.
1703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 dbno = -1;
1705 findex = 0;
1706 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001707 } else {
1708 /*
1709 * Didn't come in with a freespace block, so no data block.
1710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 ifbno = dbno = -1;
1712 fbp = NULL;
1713 findex = 0;
1714 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * If we don't have a data block yet, we're going to scan the
1718 * freespace blocks looking for one. Figure out what the
1719 * highest freespace block number is.
1720 */
1721 if (dbno == -1) {
1722 xfs_fileoff_t fo; /* freespace block number */
1723
Eric Sandeen7fb2cd42014-04-14 18:58:05 +10001724 if ((error = xfs_bmap_last_offset(dp, &fo, XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return error;
Dave Chinner2998ab12014-06-06 15:07:53 +10001726 lastfbno = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 fbno = ifbno;
1728 }
1729 /*
1730 * While we haven't identified a data block, search the freeblock
1731 * data for a good data block. If we find a null freeblock entry,
1732 * indicating a hole in the data blocks, remember that.
1733 */
1734 while (dbno == -1) {
1735 /*
1736 * If we don't have a freeblock in hand, get the next one.
1737 */
1738 if (fbp == NULL) {
1739 /*
1740 * Happens the first time through unless lookup gave
1741 * us a freespace block to start with.
1742 */
1743 if (++fbno == 0)
Dave Chinner30028032014-06-06 15:08:18 +10001744 fbno = xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001745 XFS_DIR2_FREE_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /*
1747 * If it's ifbno we already looked at it.
1748 */
1749 if (fbno == ifbno)
1750 fbno++;
1751 /*
1752 * If it's off the end we're done.
1753 */
1754 if (fbno >= lastfbno)
1755 break;
1756 /*
1757 * Read the block. There can be holes in the
1758 * freespace blocks, so this might not succeed.
1759 * This should be really rare, so there's no reason
1760 * to avoid it.
1761 */
Dave Chinner20252072012-11-12 22:54:13 +11001762 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001763 xfs_dir2_db_to_da(args->geo, fbno),
1764 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001765 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001767 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001769 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 findex = 0;
1771 }
1772 /*
1773 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001774 *
1775 * The bests initialisation should be where the bufer is read in
1776 * the above branch. But gcc is too stupid to realise that bests
1777 * and the freehdr are actually initialised if they are placed
1778 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001780 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001781 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001782 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1783 be16_to_cpu(bests[findex]) >= length)
1784 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 else {
1786 /*
1787 * Are we done with the freeblock?
1788 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001789 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Drop the block.
1792 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001793 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 fbp = NULL;
1795 if (fblk && fblk->bp)
1796 fblk->bp = NULL;
1797 }
1798 }
1799 }
1800 /*
1801 * If we don't have a data block, we need to allocate one and make
1802 * the freespace entries refer to it.
1803 */
1804 if (unlikely(dbno == -1)) {
1805 /*
1806 * Not allowed to allocate, return failure.
1807 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001808 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Dave Chinner24513372014-06-25 14:58:08 +10001809 return -ENOSPC;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /*
1812 * Allocate and initialize the new data block.
1813 */
1814 if (unlikely((error = xfs_dir2_grow_inode(args,
1815 XFS_DIR2_DATA_SPACE,
1816 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001817 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * If (somehow) we have a freespace block, get rid of it.
1822 */
1823 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001824 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (fblk && fblk->bp)
1826 fblk->bp = NULL;
1827
1828 /*
1829 * Get the freespace block corresponding to the data block
1830 * that was just allocated.
1831 */
Dave Chinner8f661932014-06-06 15:15:59 +10001832 fbno = dp->d_ops->db_to_fdb(args->geo, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001833 error = xfs_dir2_free_try_read(tp, dp,
Dave Chinner2998ab12014-06-06 15:07:53 +10001834 xfs_dir2_db_to_da(args->geo, fbno),
1835 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * If there wasn't a freespace block, the read will
1841 * return a NULL fbp. Allocate and initialize a new one.
1842 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001843 if (!fbp) {
1844 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1845 &fbno);
1846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Dave Chinner8f661932014-06-06 15:15:59 +10001849 if (dp->d_ops->db_to_fdb(args->geo, dbno) != fbno) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001850 xfs_alert(mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10001851"%s: dir ino %llu needed freesp block %lld for data block %lld, got %lld ifbno %llu lastfbno %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001852 __func__, (unsigned long long)dp->i_ino,
Dave Chinner8f661932014-06-06 15:15:59 +10001853 (long long)dp->d_ops->db_to_fdb(
1854 args->geo, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 (long long)dbno, (long long)fbno,
1856 (unsigned long long)ifbno, lastfbno);
1857 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001858 xfs_alert(mp,
1859 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 fblk,
1861 (unsigned long long)fblk->blkno,
1862 fblk->index,
1863 fblk->magic);
1864 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001865 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1868 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001869 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
1872 /*
1873 * Get a buffer for the new block.
1874 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001875 error = xfs_dir3_free_get_buf(args, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001878 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001879 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001880 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
1882 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001883 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Dave Chinner8c44a282014-06-06 15:04:41 +10001885 freehdr.firstdb =
Dave Chinner30028032014-06-06 15:08:18 +10001886 (fbno - xfs_dir2_byte_to_db(args->geo,
Dave Chinner8c44a282014-06-06 15:04:41 +10001887 XFS_DIR2_FREE_OFFSET)) *
Dave Chinner8f661932014-06-06 15:15:59 +10001888 dp->d_ops->free_max_bests(args->geo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001890 free = fbp->b_addr;
Dave Chinner24dd0f52013-10-30 13:48:41 -05001891 bests = dp->d_ops->free_bests_p(free);
Dave Chinner01ba43b2013-10-29 22:11:52 +11001892 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
1895 /*
1896 * Set the freespace block index from the data block number.
1897 */
Dave Chinner8f661932014-06-06 15:15:59 +10001898 findex = dp->d_ops->db_to_fdindex(args->geo, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 /*
1900 * If it's after the end of the current entries in the
1901 * freespace block, extend that table.
1902 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001903 if (findex >= freehdr.nvalid) {
Dave Chinner8f661932014-06-06 15:15:59 +10001904 ASSERT(findex < dp->d_ops->free_max_bests(args->geo));
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001905 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /*
1907 * Tag new entry so nused will go up.
1908 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001909 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 /*
1912 * If this entry was for an empty data block
1913 * (this should always be true) then update the header.
1914 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001915 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1916 freehdr.nused++;
Dave Chinner01ba43b2013-10-29 22:11:52 +11001917 dp->d_ops->free_hdr_to_disk(fbp->b_addr, &freehdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001918 xfs_dir2_free_log_header(args, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920 /*
1921 * Update the real value in the table.
1922 * We haven't allocated the data entry yet so this will
1923 * change again.
1924 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001925 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001926 bf = dp->d_ops->data_bestfree_p(hdr);
Dave Chinner33363fe2013-04-03 16:11:22 +11001927 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 logfree = 1;
1929 }
1930 /*
1931 * We had a data block so we don't have to make a new one.
1932 */
1933 else {
1934 /*
1935 * If just checking, we succeeded.
1936 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001937 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /*
1941 * Read the data block in.
1942 */
Dave Chinner2998ab12014-06-06 15:07:53 +10001943 error = xfs_dir3_data_read(tp, dp,
1944 xfs_dir2_db_to_da(args->geo, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001945 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001946 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001948 hdr = dbp->b_addr;
Dave Chinner2ca98772013-10-29 22:11:49 +11001949 bf = dp->d_ops->data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 logfree = 0;
1951 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001952 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /*
1954 * Point to the existing unused space.
1955 */
1956 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001957 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 needscan = needlog = 0;
1959 /*
1960 * Mark the first part of the unused space, inuse for us.
1961 */
Dave Chinnerbc851782014-06-06 15:20:54 +10001962 xfs_dir2_data_use_free(args, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001963 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 &needlog, &needscan);
1965 /*
1966 * Fill in the new entry and log it.
1967 */
1968 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001969 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 dep->namelen = args->namelen;
1971 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner9d23fc82013-10-29 22:11:48 +11001972 dp->d_ops->data_put_ftype(dep, args->filetype);
1973 tagp = dp->d_ops->data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001974 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Dave Chinnerbc851782014-06-06 15:20:54 +10001975 xfs_dir2_data_log_entry(args, dbp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /*
1977 * Rescan the block for bestfree if needed.
1978 */
1979 if (needscan)
Dave Chinner9d23fc82013-10-29 22:11:48 +11001980 xfs_dir2_data_freescan(dp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * Log the data block header if needed.
1983 */
1984 if (needlog)
Dave Chinnerbc851782014-06-06 15:20:54 +10001985 xfs_dir2_data_log_header(args, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * If the freespace entry is now wrong, update it.
1988 */
Dave Chinner24dd0f52013-10-30 13:48:41 -05001989 bests = dp->d_ops->free_bests_p(free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11001990 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
1991 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 logfree = 1;
1993 }
1994 /*
1995 * Log the freespace entry if needed.
1996 */
1997 if (logfree)
Dave Chinnerbc851782014-06-06 15:20:54 +10001998 xfs_dir2_free_log_bests(args, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * Return the data block and offset in args, then drop the data block.
2001 */
2002 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002003 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 return 0;
2005}
2006
2007/*
2008 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002009 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * The only real output is the inode number of the entry.
2011 */
2012int /* error */
2013xfs_dir2_node_lookup(
2014 xfs_da_args_t *args) /* operation arguments */
2015{
2016 int error; /* error return value */
2017 int i; /* btree level */
2018 int rval; /* operation return value */
2019 xfs_da_state_t *state; /* btree cursor */
2020
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002021 trace_xfs_dir2_node_lookup(args);
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
2024 * Allocate and initialize the btree cursor.
2025 */
2026 state = xfs_da_state_alloc();
2027 state->args = args;
2028 state->mp = args->dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /*
2030 * Fill in the path to the entry in the cursor.
2031 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002032 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (error)
2034 rval = error;
Dave Chinner24513372014-06-25 14:58:08 +10002035 else if (rval == -ENOENT && args->cmpresult == XFS_CMP_CASE) {
2036 /* If a CI match, dup the actual name and return -EEXIST */
Barry Naujok384f3ce2008-05-21 16:58:22 +10002037 xfs_dir2_data_entry_t *dep;
2038
Dave Chinner1d9025e2012-06-22 18:50:14 +10002039 dep = (xfs_dir2_data_entry_t *)
2040 ((char *)state->extrablk.bp->b_addr +
2041 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002042 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
2045 * Release the btree blocks and leaf block.
2046 */
2047 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002048 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 state->path.blk[i].bp = NULL;
2050 }
2051 /*
2052 * Release the data block if we have it.
2053 */
2054 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002055 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 state->extrablk.bp = NULL;
2057 }
2058 xfs_da_state_free(state);
2059 return rval;
2060}
2061
2062/*
2063 * Remove an entry from a node-format directory.
2064 */
2065int /* error */
2066xfs_dir2_node_removename(
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002067 struct xfs_da_args *args) /* operation arguments */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002069 struct xfs_da_state_blk *blk; /* leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 int error; /* error return value */
2071 int rval; /* operation return value */
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002072 struct xfs_da_state *state; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002074 trace_xfs_dir2_node_removename(args);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Allocate and initialize the btree cursor.
2078 */
2079 state = xfs_da_state_alloc();
2080 state->args = args;
2081 state->mp = args->dp->i_mount;
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002082
2083 /* Look up the entry we're deleting, set up the cursor. */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002084 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002085 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002086 goto out_free;
2087
2088 /* Didn't find it, upper layer screwed up. */
Dave Chinner24513372014-06-25 14:58:08 +10002089 if (rval != -EEXIST) {
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002090 error = rval;
2091 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 blk = &state->path.blk[state->path.active - 1];
2095 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2096 ASSERT(state->extravalid);
2097 /*
2098 * Remove the leaf and data entries.
2099 * Extrablk refers to the data block.
2100 */
2101 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2102 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002103 if (error)
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002104 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 /*
2106 * Fix the hash values up the btree.
2107 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002108 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /*
2110 * If we need to join leaf blocks, do it.
2111 */
2112 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002113 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * If no errors so far, try conversion to leaf format.
2116 */
2117 if (!error)
2118 error = xfs_dir2_node_to_leaf(state);
Mark Tinguely3a8c9202013-10-05 21:48:25 -05002119out_free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 xfs_da_state_free(state);
2121 return error;
2122}
2123
2124/*
2125 * Replace an entry's inode number in a node-format directory.
2126 */
2127int /* error */
2128xfs_dir2_node_replace(
2129 xfs_da_args_t *args) /* operation arguments */
2130{
2131 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002132 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 xfs_dir2_data_entry_t *dep; /* data entry changed */
2134 int error; /* error return value */
2135 int i; /* btree level */
2136 xfs_ino_t inum; /* new inode number */
Jan Kara03754232015-08-25 10:05:13 +10002137 int ftype; /* new file type */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 xfs_dir2_leaf_t *leaf; /* leaf structure */
2139 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2140 int rval; /* internal return value */
2141 xfs_da_state_t *state; /* btree cursor */
2142
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002143 trace_xfs_dir2_node_replace(args);
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /*
2146 * Allocate and initialize the btree cursor.
2147 */
2148 state = xfs_da_state_alloc();
2149 state->args = args;
2150 state->mp = args->dp->i_mount;
Jan Kara03754232015-08-25 10:05:13 +10002151
2152 /*
2153 * We have to save new inode number and ftype since
2154 * xfs_da3_node_lookup_int() is going to overwrite them
2155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 inum = args->inumber;
Jan Kara03754232015-08-25 10:05:13 +10002157 ftype = args->filetype;
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /*
2160 * Lookup the entry to change in the btree.
2161 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002162 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (error) {
2164 rval = error;
2165 }
2166 /*
2167 * It should be found, since the vnodeops layer has looked it up
2168 * and locked it. But paranoia is good.
2169 */
Dave Chinner24513372014-06-25 14:58:08 +10002170 if (rval == -EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002171 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
2173 * Find the leaf entry.
2174 */
2175 blk = &state->path.blk[state->path.active - 1];
2176 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002177 leaf = blk->bp->b_addr;
Dave Chinner41419562013-10-29 22:11:50 +11002178 ents = args->dp->d_ops->leaf_ents_p(leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +10002179 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 ASSERT(state->extravalid);
2181 /*
2182 * Point to the data entry.
2183 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002184 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002185 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2186 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002188 ((char *)hdr +
Dave Chinner30028032014-06-06 15:08:18 +10002189 xfs_dir2_dataptr_to_off(args->geo,
2190 be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002191 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
2193 * Fill in the new inode number and log the entry.
2194 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002195 dep->inumber = cpu_to_be64(inum);
Jan Kara03754232015-08-25 10:05:13 +10002196 args->dp->d_ops->data_put_ftype(dep, ftype);
Dave Chinnerbc851782014-06-06 15:20:54 +10002197 xfs_dir2_data_log_entry(args, state->extrablk.bp, dep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 rval = 0;
2199 }
2200 /*
2201 * Didn't find it, and we're holding a data block. Drop it.
2202 */
2203 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002204 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 state->extrablk.bp = NULL;
2206 }
2207 /*
2208 * Release all the buffers in the cursor.
2209 */
2210 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002211 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 state->path.blk[i].bp = NULL;
2213 }
2214 xfs_da_state_free(state);
2215 return rval;
2216}
2217
2218/*
2219 * Trim off a trailing empty freespace block.
2220 * Return (in rvalp) 1 if we did it, 0 if not.
2221 */
2222int /* error */
2223xfs_dir2_node_trim_free(
2224 xfs_da_args_t *args, /* operation arguments */
2225 xfs_fileoff_t fo, /* free block number */
2226 int *rvalp) /* out: did something */
2227{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002228 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 xfs_inode_t *dp; /* incore directory inode */
2230 int error; /* error return code */
2231 xfs_dir2_free_t *free; /* freespace structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002233 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
2235 dp = args->dp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 tp = args->trans;
2237 /*
2238 * Read the freespace block.
2239 */
Dave Chinner20252072012-11-12 22:54:13 +11002240 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002241 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * There can be holes in freespace. If fo is a hole, there's
2245 * nothing to do.
2246 */
Dave Chinner20252072012-11-12 22:54:13 +11002247 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002249 free = bp->b_addr;
Dave Chinner01ba43b2013-10-29 22:11:52 +11002250 dp->d_ops->free_hdr_from_disk(&freehdr, free);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * If there are used entries, there's nothing to do.
2254 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002255 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002256 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 *rvalp = 0;
2258 return 0;
2259 }
2260 /*
2261 * Blow the block away.
2262 */
Dave Chinner2998ab12014-06-06 15:07:53 +10002263 error = xfs_dir2_shrink_inode(args,
2264 xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)fo), bp);
2265 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * Can't fail with ENOSPC since that only happens with no
2268 * space reservation, when breaking up an extent into two
2269 * pieces. This is the last block of an extent.
2270 */
Dave Chinner24513372014-06-25 14:58:08 +10002271 ASSERT(error != -ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002272 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return error;
2274 }
2275 /*
2276 * Return that we succeeded.
2277 */
2278 *rvalp = 1;
2279 return 0;
2280}