blob: 6c70524066051381f823d1043b857b5421a6490e [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.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
29#include "xfs_inode.h"
30#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_format.h"
32#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * Function declarations.
38 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100039static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
40 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef DEBUG
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static void xfs_dir2_leafn_check(struct xfs_inode *dp, struct xfs_buf *bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#else
44#define xfs_dir2_leafn_check(dp, bp)
45#endif
Dave Chinner1d9025e2012-06-22 18:50:14 +100046static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, struct xfs_buf *bp_s,
47 int start_s, struct xfs_buf *bp_d,
48 int start_d, int count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
50 xfs_da_state_blk_t *blk1,
51 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100052static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int index, xfs_da_state_blk_t *dblk,
54 int *rval);
55static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
56 xfs_da_state_blk_t *fblk);
57
58/*
59 * Log entries from a freespace block.
60 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000061STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070062xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +100063 struct xfs_trans *tp,
64 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int first, /* first entry to log */
66 int last) /* last entry to log */
67{
68 xfs_dir2_free_t *free; /* freespace structure */
69
Dave Chinner1d9025e2012-06-22 18:50:14 +100070 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020071 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +100072 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 (uint)((char *)&free->bests[first] - (char *)free),
74 (uint)((char *)&free->bests[last] - (char *)free +
75 sizeof(free->bests[0]) - 1));
76}
77
78/*
79 * Log header from a freespace block.
80 */
81static void
82xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +100083 struct xfs_trans *tp,
84 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 xfs_dir2_free_t *free; /* freespace structure */
87
Dave Chinner1d9025e2012-06-22 18:50:14 +100088 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020089 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +100090 xfs_trans_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
92}
93
94/*
95 * Convert a leaf-format directory to a node-format directory.
96 * We need to change the magic number of the leaf block, and copy
97 * the freespace table out of the leaf block into its own block.
98 */
99int /* error */
100xfs_dir2_leaf_to_node(
101 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000102 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 xfs_inode_t *dp; /* incore directory inode */
105 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000106 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 xfs_dir2_db_t fdb; /* freespace block number */
108 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100109 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 int i; /* leaf freespace index */
111 xfs_dir2_leaf_t *leaf; /* leaf structure */
112 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
113 xfs_mount_t *mp; /* filesystem mount point */
114 int n; /* count of live freespc ents */
115 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100116 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 xfs_trans_t *tp; /* transaction pointer */
118
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000119 trace_xfs_dir2_leaf_to_node(args);
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 dp = args->dp;
122 mp = dp->i_mount;
123 tp = args->trans;
124 /*
125 * Add a freespace block to the directory.
126 */
127 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
128 return error;
129 }
130 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
131 /*
132 * Get the buffer for the new freespace block.
133 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000134 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 XFS_DATA_FORK))) {
136 return error;
137 }
138 ASSERT(fbp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000139 free = fbp->b_addr;
140 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000141 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /*
143 * Initialize the freespace block header.
144 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100145 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100147 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100148 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 /*
150 * Copy freespace entries from the leaf block to the new block.
151 * Count active entries.
152 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000153 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100154 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100155 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100157 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100159 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100160 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /*
162 * Log everything.
163 */
164 xfs_dir2_leaf_log_header(tp, lbp);
165 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100166 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 xfs_dir2_leafn_check(dp, lbp);
168 return 0;
169}
170
171/*
172 * Add a leaf entry to a leaf block in a node-form directory.
173 * The other work necessary is done from the caller.
174 */
175static int /* error */
176xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000177 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 xfs_da_args_t *args, /* operation arguments */
179 int index) /* insertion pt for new entry */
180{
181 int compact; /* compacting stale leaves */
182 xfs_inode_t *dp; /* incore directory inode */
183 int highstale; /* next stale entry */
184 xfs_dir2_leaf_t *leaf; /* leaf structure */
185 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
186 int lfloghigh; /* high leaf entry logging */
187 int lfloglow; /* low leaf entry logging */
188 int lowstale; /* previous stale entry */
189 xfs_mount_t *mp; /* filesystem mount point */
190 xfs_trans_t *tp; /* transaction pointer */
191
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000192 trace_xfs_dir2_leafn_add(args, index);
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 dp = args->dp;
195 mp = dp->i_mount;
196 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000197 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /*
200 * Quick check just to make sure we are not going to index
201 * into other peoples memory
202 */
203 if (index < 0)
204 return XFS_ERROR(EFSCORRUPTED);
205
206 /*
207 * If there are already the maximum number of leaf entries in
208 * the block, if there are no stale entries it won't fit.
209 * Caller will do a split. If there are stale entries we'll do
210 * a compact.
211 */
212
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000213 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!leaf->hdr.stale)
215 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100216 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else
218 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100219 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100220 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100221 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Barry Naujok6a178102008-05-21 16:42:05 +1000223 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225
226 /*
227 * Compact out all but one stale leaf entry. Leaves behind
228 * the entry closest to index.
229 */
230 if (compact) {
231 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
232 &lfloglow, &lfloghigh);
233 }
234 /*
235 * Set impossible logging indices for this case.
236 */
237 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100238 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 lfloghigh = -1;
240 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 /*
243 * Insert the new entry, log everything.
244 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200245 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
246 highstale, &lfloglow, &lfloghigh);
247
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100248 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000249 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100250 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 xfs_dir2_leaf_log_header(tp, bp);
252 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
253 xfs_dir2_leafn_check(dp, bp);
254 return 0;
255}
256
257#ifdef DEBUG
258/*
259 * Check internal consistency of a leafn block.
260 */
261void
262xfs_dir2_leafn_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000263 struct xfs_inode *dp,
264 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int i; /* leaf index */
267 xfs_dir2_leaf_t *leaf; /* leaf structure */
268 xfs_mount_t *mp; /* filesystem mount point */
269 int stale; /* count of stale leaves */
270
Dave Chinner1d9025e2012-06-22 18:50:14 +1000271 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200273 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000274 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100275 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
276 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100277 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
278 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200280 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 stale++;
282 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100283 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285#endif /* DEBUG */
286
287/*
288 * Return the last hash value in the leaf.
289 * Stale entries are ok.
290 */
291xfs_dahash_t /* hash value */
292xfs_dir2_leafn_lasthash(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000293 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int *count) /* count of entries in leaf */
295{
296 xfs_dir2_leaf_t *leaf; /* leaf structure */
297
Dave Chinner1d9025e2012-06-22 18:50:14 +1000298 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200299 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100301 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!leaf->hdr.count)
303 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100304 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000308 * Look up a leaf entry for space to add a name in a node-format leaf block.
309 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000311STATIC int
312xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000313 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 xfs_da_args_t *args, /* operation arguments */
315 int *indexp, /* out: leaf entry index */
316 xfs_da_state_t *state) /* state to fill in */
317{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000318 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000319 xfs_dir2_db_t curdb = -1; /* current data block number */
320 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 xfs_inode_t *dp; /* incore directory inode */
322 int error; /* error return value */
323 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000324 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int index; /* leaf entry index */
326 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000327 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
329 xfs_mount_t *mp; /* filesystem mount point */
330 xfs_dir2_db_t newdb; /* new data block number */
331 xfs_dir2_db_t newfdb; /* new free block number */
332 xfs_trans_t *tp; /* transaction pointer */
333
334 dp = args->dp;
335 tp = args->trans;
336 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000337 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200338 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100340 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#endif
342 xfs_dir2_leafn_check(dp, bp);
343 /*
344 * Look up the hash value in the leaf entries.
345 */
346 index = xfs_dir2_leaf_search_hash(args, bp);
347 /*
348 * Do we have a buffer coming in?
349 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000350 if (state->extravalid) {
351 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000353 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000354 free = curbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200355 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000357 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /*
359 * Loop over leaf entries with the right hash value.
360 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000361 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
362 be32_to_cpu(lep->hashval) == args->hashval;
363 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Skip stale leaf entries.
366 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100367 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 continue;
369 /*
370 * Pull the data block number from the entry.
371 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000372 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /*
374 * For addname, we're looking for a place to put the new entry.
375 * We want to use a data block with an entry of equal
376 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000377 *
378 * If this block isn't the data block we already have
379 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000381 if (newdb != curdb) {
382 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000384 * Convert the data block to the free block
385 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000387 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000389 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000391 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000393 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
395 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000396 xfs_trans_brelse(tp, curbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000398 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000400 error = xfs_da_read_buf(tp, dp,
401 xfs_dir2_db_to_da(mp, newfdb),
402 -1, &curbp, XFS_DATA_FORK);
403 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000405 free = curbp->b_addr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 ASSERT(be32_to_cpu(free->hdr.magic) ==
407 XFS_DIR2_FREE_MAGIC);
408 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200409 xfs_dir2_free_max_bests(mp)) == 0);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000410 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
411 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
412 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000415 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000417 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000419 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200421 if (unlikely(free->bests[fi] ==
422 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000423 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
424 XFS_ERRLEVEL_LOW, mp);
425 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000426 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000427 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000429 curfdb = newfdb;
430 if (be16_to_cpu(free->bests[fi]) >= length)
431 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000434 /* Didn't find any space */
435 fi = -1;
436out:
Barry Naujok6a178102008-05-21 16:42:05 +1000437 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000438 if (curbp) {
439 /* Giving back a free block. */
440 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000442 state->extrablk.index = fi;
443 state->extrablk.blkno = curfdb;
444 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
445 } else {
446 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000449 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return XFS_ERROR(ENOENT);
453}
454
455/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000456 * Look up a leaf entry in a node-format leaf block.
457 * The extrablk in state a data block.
458 */
459STATIC int
460xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000461 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000462 xfs_da_args_t *args, /* operation arguments */
463 int *indexp, /* out: leaf entry index */
464 xfs_da_state_t *state) /* state to fill in */
465{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000466 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000467 xfs_dir2_db_t curdb = -1; /* current data block number */
468 xfs_dir2_data_entry_t *dep; /* data block entry */
469 xfs_inode_t *dp; /* incore directory inode */
470 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000471 int index; /* leaf entry index */
472 xfs_dir2_leaf_t *leaf; /* leaf structure */
473 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
474 xfs_mount_t *mp; /* filesystem mount point */
475 xfs_dir2_db_t newdb; /* new data block number */
476 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000477 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000478
479 dp = args->dp;
480 tp = args->trans;
481 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000482 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200483 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000484#ifdef __KERNEL__
485 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
486#endif
487 xfs_dir2_leafn_check(dp, bp);
488 /*
489 * Look up the hash value in the leaf entries.
490 */
491 index = xfs_dir2_leaf_search_hash(args, bp);
492 /*
493 * Do we have a buffer coming in?
494 */
495 if (state->extravalid) {
496 curbp = state->extrablk.bp;
497 curdb = state->extrablk.blkno;
498 }
499 /*
500 * Loop over leaf entries with the right hash value.
501 */
502 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
503 be32_to_cpu(lep->hashval) == args->hashval;
504 lep++, index++) {
505 /*
506 * Skip stale leaf entries.
507 */
508 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
509 continue;
510 /*
511 * Pull the data block number from the entry.
512 */
513 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
514 /*
515 * Not adding a new entry, so we really want to find
516 * the name given to us.
517 *
518 * If it's a different data block, go get it.
519 */
520 if (newdb != curdb) {
521 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000522 * If we had a block before that we aren't saving
523 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000524 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000525 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
526 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000527 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000528 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000529 * If needing the block that is saved with a CI match,
530 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000531 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000532 if (args->cmpresult != XFS_CMP_DIFFERENT &&
533 newdb == state->extrablk.blkno) {
534 ASSERT(state->extravalid);
535 curbp = state->extrablk.bp;
536 } else {
537 error = xfs_da_read_buf(tp, dp,
538 xfs_dir2_db_to_da(mp, newdb),
539 -1, &curbp, XFS_DATA_FORK);
540 if (error)
541 return error;
542 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000543 xfs_dir2_data_check(dp, curbp);
544 curdb = newdb;
545 }
546 /*
547 * Point to the data entry.
548 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000549 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000550 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
551 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000552 * Compare the entry and if it's an exact match, return
553 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000554 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000555 */
Barry Naujok5163f952008-05-21 16:41:01 +1000556 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
557 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000558 /* If there is a CI match block, drop it */
559 if (args->cmpresult != XFS_CMP_DIFFERENT &&
560 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000561 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000562 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000563 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000564 *indexp = index;
565 state->extravalid = 1;
566 state->extrablk.bp = curbp;
567 state->extrablk.blkno = curdb;
568 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000569 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000570 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000571 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000572 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000573 }
574 }
Barry Naujok6a178102008-05-21 16:42:05 +1000575 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
576 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000577 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000578 if (args->cmpresult == XFS_CMP_DIFFERENT) {
579 /* Giving back last used data block. */
580 state->extravalid = 1;
581 state->extrablk.bp = curbp;
582 state->extrablk.index = -1;
583 state->extrablk.blkno = curdb;
584 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
585 } else {
586 /* If the curbp is not the CI match block, drop it */
587 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000588 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000589 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000590 } else {
591 state->extravalid = 0;
592 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000593 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000594 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000595}
596
597/*
598 * Look up a leaf entry in a node-format leaf block.
599 * If this is an addname then the extrablk in state is a freespace block,
600 * otherwise it's a data block.
601 */
602int
603xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000604 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000605 xfs_da_args_t *args, /* operation arguments */
606 int *indexp, /* out: leaf entry index */
607 xfs_da_state_t *state) /* state to fill in */
608{
Barry Naujok6a178102008-05-21 16:42:05 +1000609 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000610 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
611 state);
612 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
613}
614
615/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * Move count leaf entries from source to destination leaf.
617 * Log entries and headers. Stale entries are preserved.
618 */
619static void
620xfs_dir2_leafn_moveents(
621 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000622 struct xfs_buf *bp_s, /* source leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int start_s, /* source leaf index */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000624 struct xfs_buf *bp_d, /* destination leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int start_d, /* destination leaf index */
626 int count) /* count of leaves to copy */
627{
628 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
629 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
630 int stale; /* count stale leaves copied */
631 xfs_trans_t *tp; /* transaction pointer */
632
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000633 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
636 * Silently return if nothing to do.
637 */
638 if (count == 0) {
639 return;
640 }
641 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000642 leaf_s = bp_s->b_addr;
643 leaf_d = bp_d->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * If the destination index is not the end of the current
646 * destination leaf entries, open up a hole in the destination
647 * to hold the new entries.
648 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100649 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100651 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 sizeof(xfs_dir2_leaf_entry_t));
653 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100654 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 /*
657 * If the source has stale leaves, count the ones in the copy range
658 * so we can update the header correctly.
659 */
660 if (leaf_s->hdr.stale) {
661 int i; /* temp leaf index */
662
663 for (i = start_s, stale = 0; i < start_s + count; i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200664 if (leaf_s->ents[i].address ==
665 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 stale++;
667 }
668 } else
669 stale = 0;
670 /*
671 * Copy the leaf entries from source to destination.
672 */
673 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
674 count * sizeof(xfs_dir2_leaf_entry_t));
675 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
676 /*
677 * If there are source entries after the ones we copied,
678 * delete the ones we copied by sliding the next ones down.
679 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100680 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
682 count * sizeof(xfs_dir2_leaf_entry_t));
683 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
684 }
685 /*
686 * Update the headers and log them.
687 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800688 be16_add_cpu(&leaf_s->hdr.count, -(count));
689 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
690 be16_add_cpu(&leaf_d->hdr.count, count);
691 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 xfs_dir2_leaf_log_header(tp, bp_s);
693 xfs_dir2_leaf_log_header(tp, bp_d);
694 xfs_dir2_leafn_check(args->dp, bp_s);
695 xfs_dir2_leafn_check(args->dp, bp_d);
696}
697
698/*
699 * Determine the sort order of two leaf blocks.
700 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
701 */
702int /* sort order */
703xfs_dir2_leafn_order(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000704 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
705 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
708 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
709
Dave Chinner1d9025e2012-06-22 18:50:14 +1000710 leaf1 = leaf1_bp->b_addr;
711 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200712 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
713 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100714 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
715 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100716 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
717 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
718 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 1;
720 return 0;
721}
722
723/*
724 * Rebalance leaf entries between two leaf blocks.
725 * This is actually only called when the second block is new,
726 * though the code deals with the general case.
727 * A new entry will be inserted in one of the blocks, and that
728 * entry is taken into account when balancing.
729 */
730static void
731xfs_dir2_leafn_rebalance(
732 xfs_da_state_t *state, /* btree cursor */
733 xfs_da_state_blk_t *blk1, /* first btree block */
734 xfs_da_state_blk_t *blk2) /* second btree block */
735{
736 xfs_da_args_t *args; /* operation arguments */
737 int count; /* count (& direction) leaves */
738 int isleft; /* new goes in left leaf */
739 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
740 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
741 int mid; /* midpoint leaf index */
742#ifdef DEBUG
743 int oldstale; /* old count of stale leaves */
744#endif
745 int oldsum; /* old total leaf count */
746 int swap; /* swapped leaf blocks */
747
748 args = state->args;
749 /*
750 * If the block order is wrong, swap the arguments.
751 */
752 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
753 xfs_da_state_blk_t *tmp; /* temp for block swap */
754
755 tmp = blk1;
756 blk1 = blk2;
757 blk2 = tmp;
758 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000759 leaf1 = blk1->bp->b_addr;
760 leaf2 = blk2->bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100761 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100763 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764#endif
765 mid = oldsum >> 1;
766 /*
767 * If the old leaf count was odd then the new one will be even,
768 * so we need to divide the new count evenly.
769 */
770 if (oldsum & 1) {
771 xfs_dahash_t midhash; /* middle entry hash value */
772
Nathan Scotta818e5d2006-03-17 17:28:07 +1100773 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100774 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100776 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 isleft = args->hashval <= midhash;
778 }
779 /*
780 * If the old count is even then the new count is odd, so there's
781 * no preferred side for the new entry.
782 * Pick the left one.
783 */
784 else
785 isleft = 1;
786 /*
787 * Calculate moved entry count. Positive means left-to-right,
788 * negative means right-to-left. Then move the entries.
789 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100790 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (count > 0)
792 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100793 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 else if (count < 0)
795 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100796 be16_to_cpu(leaf1->hdr.count), count);
797 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
798 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /*
800 * Mark whether we're inserting into the old or new leaf.
801 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100802 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100804 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 state->inleaf = !swap;
806 else
807 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100808 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /*
810 * Adjust the expected index for insertion.
811 */
812 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100813 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000814
815 /*
816 * Finally sanity check just to make sure we are not returning a
817 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 */
819 if(blk2->index < 0) {
820 state->inleaf = 1;
821 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100822 xfs_alert(args->dp->i_mount,
823 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
824 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826}
827
828/*
829 * Remove an entry from a node directory.
830 * This removes the leaf entry and the data entry,
831 * and updates the free block if necessary.
832 */
833static int /* error */
834xfs_dir2_leafn_remove(
835 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000836 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int index, /* leaf entry index */
838 xfs_da_state_blk_t *dblk, /* data block */
839 int *rval) /* resulting block needs join */
840{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200841 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000843 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 xfs_dir2_data_entry_t *dep; /* data block entry */
845 xfs_inode_t *dp; /* incore directory inode */
846 xfs_dir2_leaf_t *leaf; /* leaf structure */
847 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
848 int longest; /* longest data free entry */
849 int off; /* data block entry offset */
850 xfs_mount_t *mp; /* filesystem mount point */
851 int needlog; /* need to log data header */
852 int needscan; /* need to rescan data frees */
853 xfs_trans_t *tp; /* transaction pointer */
854
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000855 trace_xfs_dir2_leafn_remove(args, index);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 dp = args->dp;
858 tp = args->trans;
859 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000860 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200861 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * Point to the entry we're removing.
864 */
865 lep = &leaf->ents[index];
866 /*
867 * Extract the data block and offset from the entry.
868 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000869 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000871 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 ASSERT(dblk->index == off);
873 /*
874 * Kill the leaf entry by marking it stale.
875 * Log the leaf block changes.
876 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800877 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100879 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 xfs_dir2_leaf_log_ents(tp, bp, index, index);
881 /*
882 * Make the data entry free. Keep track of the longest freespace
883 * in the data block in case it changes.
884 */
885 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000886 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200887 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
888 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 needlog = needscan = 0;
890 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000891 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 /*
893 * Rescan the data block freespaces for bestfree.
894 * Log the data block header if needed.
895 */
896 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200897 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (needlog)
899 xfs_dir2_data_log_header(tp, dbp);
900 xfs_dir2_data_check(dp, dbp);
901 /*
902 * If the longest data block freespace changes, need to update
903 * the corresponding freeblock entry.
904 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200905 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000907 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 xfs_dir2_db_t fdb; /* freeblock block number */
909 int findex; /* index in freeblock entries */
910 xfs_dir2_free_t *free; /* freeblock structure */
911 int logfree; /* need to log free entry */
912
913 /*
914 * Convert the data block number to a free block,
915 * read in the free block.
916 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000917 fdb = xfs_dir2_db_to_fdb(mp, db);
918 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 -1, &fbp, XFS_DATA_FORK))) {
920 return error;
921 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000922 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200923 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Nathan Scott0ba962e2006-03-17 17:27:07 +1100924 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200925 xfs_dir2_free_max_bests(mp) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
927 /*
928 * Calculate which entry we need to fix.
929 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000930 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200931 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * If the data block is now empty we can get rid of it
934 * (usually).
935 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200936 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /*
938 * Try to punch out the data block.
939 */
940 error = xfs_dir2_shrink_inode(args, db, dbp);
941 if (error == 0) {
942 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200943 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 /*
946 * We can get ENOSPC if there's no space reservation.
947 * In this case just drop the buffer and some one else
948 * will eventually get rid of the empty block.
949 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000950 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return error;
952 }
953 /*
954 * If we got rid of the data block, we can eliminate that entry
955 * in the free block.
956 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200957 if (hdr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /*
959 * One less used entry in the free table.
960 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800961 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 xfs_dir2_free_log_header(tp, fbp);
963 /*
964 * If this was the last entry in the table, we can
965 * trim the table size back. There might be other
966 * entries at the end referring to non-existent
967 * data blocks, get those too.
968 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100969 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int i; /* free entry index */
971
972 for (i = findex - 1;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200973 i >= 0 &&
974 free->bests[i] == cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 i--)
976 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100977 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 logfree = 0;
979 }
980 /*
981 * Not the last entry, just punch it out.
982 */
983 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100984 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 logfree = 1;
986 }
987 /*
988 * If there are no useful entries left in the block,
989 * get rid of the block if we can.
990 */
991 if (!free->hdr.nused) {
992 error = xfs_dir2_shrink_inode(args, fdb, fbp);
993 if (error == 0) {
994 fbp = NULL;
995 logfree = 0;
996 } else if (error != ENOSPC || args->total != 0)
997 return error;
998 /*
999 * It's possible to get ENOSPC if there is no
1000 * space reservation. In this case some one
1001 * else will eventually get rid of this block.
1002 */
1003 }
1004 }
1005 /*
1006 * Data block is not empty, just set the free entry to
1007 * the new value.
1008 */
1009 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001010 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 logfree = 1;
1012 }
1013 /*
1014 * Log the free entry that changed, unless we got rid of it.
1015 */
1016 if (logfree)
1017 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019 xfs_dir2_leafn_check(dp, bp);
1020 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001021 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * to justify trying to join it with a neighbor.
1023 */
1024 *rval =
1025 ((uint)sizeof(leaf->hdr) +
1026 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001027 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 mp->m_dir_magicpct;
1029 return 0;
1030}
1031
1032/*
1033 * Split the leaf entries in the old block into old and new blocks.
1034 */
1035int /* error */
1036xfs_dir2_leafn_split(
1037 xfs_da_state_t *state, /* btree cursor */
1038 xfs_da_state_blk_t *oldblk, /* original block */
1039 xfs_da_state_blk_t *newblk) /* newly created block */
1040{
1041 xfs_da_args_t *args; /* operation arguments */
1042 xfs_dablk_t blkno; /* new leaf block number */
1043 int error; /* error return value */
1044 xfs_mount_t *mp; /* filesystem mount point */
1045
1046 /*
1047 * Allocate space for a new leaf node.
1048 */
1049 args = state->args;
1050 mp = args->dp->i_mount;
1051 ASSERT(args != NULL);
1052 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1053 error = xfs_da_grow_inode(args, &blkno);
1054 if (error) {
1055 return error;
1056 }
1057 /*
1058 * Initialize the new leaf block.
1059 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001060 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1062 if (error) {
1063 return error;
1064 }
1065 newblk->blkno = blkno;
1066 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1067 /*
1068 * Rebalance the entries across the two leaves, link the new
1069 * block into the leaves.
1070 */
1071 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1072 error = xfs_da_blk_link(state, oldblk, newblk);
1073 if (error) {
1074 return error;
1075 }
1076 /*
1077 * Insert the new entry in the correct block.
1078 */
1079 if (state->inleaf)
1080 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1081 else
1082 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1083 /*
1084 * Update last hashval in each block since we added the name.
1085 */
1086 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1087 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1088 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1089 xfs_dir2_leafn_check(args->dp, newblk->bp);
1090 return error;
1091}
1092
1093/*
1094 * Check a leaf block and its neighbors to see if the block should be
1095 * collapsed into one or the other neighbor. Always keep the block
1096 * with the smaller block number.
1097 * If the current block is over 50% full, don't try to join it, return 0.
1098 * If the block is empty, fill in the state structure and return 2.
1099 * If it can be collapsed, fill in the state structure and return 1.
1100 * If nothing can be done, return 0.
1101 */
1102int /* error */
1103xfs_dir2_leafn_toosmall(
1104 xfs_da_state_t *state, /* btree cursor */
1105 int *action) /* resulting action to take */
1106{
1107 xfs_da_state_blk_t *blk; /* leaf block */
1108 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001109 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int bytes; /* bytes in use */
1111 int count; /* leaf live entry count */
1112 int error; /* error return value */
1113 int forward; /* sibling block direction */
1114 int i; /* sibling counter */
1115 xfs_da_blkinfo_t *info; /* leaf block header */
1116 xfs_dir2_leaf_t *leaf; /* leaf structure */
1117 int rval; /* result from path_shift */
1118
1119 /*
1120 * Check for the degenerate case of the block being over 50% full.
1121 * If so, it's not worth even looking to see if we might be able
1122 * to coalesce with a sibling.
1123 */
1124 blk = &state->path.blk[state->path.active - 1];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001125 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001126 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001128 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1130 if (bytes > (state->blocksize >> 1)) {
1131 /*
1132 * Blk over 50%, don't try to join.
1133 */
1134 *action = 0;
1135 return 0;
1136 }
1137 /*
1138 * Check for the degenerate case of the block being empty.
1139 * If the block is empty, we'll simply delete it, no need to
1140 * coalesce it with a sibling block. We choose (arbitrarily)
1141 * to merge with the forward block unless it is NULL.
1142 */
1143 if (count == 0) {
1144 /*
1145 * Make altpath point to the block we want to keep and
1146 * path point to the block we want to drop (this one).
1147 */
Nathan Scott89da0542006-03-17 17:28:40 +11001148 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 memcpy(&state->altpath, &state->path, sizeof(state->path));
1150 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1151 &rval);
1152 if (error)
1153 return error;
1154 *action = rval ? 2 : 0;
1155 return 0;
1156 }
1157 /*
1158 * Examine each sibling block to see if we can coalesce with
1159 * at least 25% free space to spare. We need to figure out
1160 * whether to merge with the forward or the backward block.
1161 * We prefer coalescing with the lower numbered sibling so as
1162 * to shrink a directory over time.
1163 */
Nathan Scott89da0542006-03-17 17:28:40 +11001164 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001166 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (blkno == 0)
1168 continue;
1169 /*
1170 * Read the sibling leaf block.
1171 */
1172 if ((error =
1173 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1174 -1, &bp, XFS_DATA_FORK))) {
1175 return error;
1176 }
1177 ASSERT(bp != NULL);
1178 /*
1179 * Count bytes in the two blocks combined.
1180 */
1181 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001182 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001184 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001185 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +11001186 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 bytes -= count * (uint)sizeof(leaf->ents[0]);
1188 /*
1189 * Fits with at least 25% to spare.
1190 */
1191 if (bytes >= 0)
1192 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001193 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195 /*
1196 * Didn't like either block, give up.
1197 */
1198 if (i >= 2) {
1199 *action = 0;
1200 return 0;
1201 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
1204 * Make altpath point to the block we want to keep (the lower
1205 * numbered block) and path point to the block we want to drop.
1206 */
1207 memcpy(&state->altpath, &state->path, sizeof(state->path));
1208 if (blkno < blk->blkno)
1209 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1210 &rval);
1211 else
1212 error = xfs_da_path_shift(state, &state->path, forward, 0,
1213 &rval);
1214 if (error) {
1215 return error;
1216 }
1217 *action = rval ? 0 : 1;
1218 return 0;
1219}
1220
1221/*
1222 * Move all the leaf entries from drop_blk to save_blk.
1223 * This is done as part of a join operation.
1224 */
1225void
1226xfs_dir2_leafn_unbalance(
1227 xfs_da_state_t *state, /* cursor */
1228 xfs_da_state_blk_t *drop_blk, /* dead block */
1229 xfs_da_state_blk_t *save_blk) /* surviving block */
1230{
1231 xfs_da_args_t *args; /* operation arguments */
1232 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1233 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1234
1235 args = state->args;
1236 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1237 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001238 drop_leaf = drop_blk->bp->b_addr;
1239 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001240 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1241 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * If there are any stale leaf entries, take this opportunity
1244 * to purge them.
1245 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001246 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001248 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 xfs_dir2_leaf_compact(args, save_blk->bp);
1250 /*
1251 * Move the entries from drop to the appropriate end of save.
1252 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001253 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1255 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001256 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 else
1258 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001259 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001260 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1262}
1263
1264/*
1265 * Top-level node form directory addname routine.
1266 */
1267int /* error */
1268xfs_dir2_node_addname(
1269 xfs_da_args_t *args) /* operation arguments */
1270{
1271 xfs_da_state_blk_t *blk; /* leaf block for insert */
1272 int error; /* error return value */
1273 int rval; /* sub-return value */
1274 xfs_da_state_t *state; /* btree cursor */
1275
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001276 trace_xfs_dir2_node_addname(args);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * Allocate and initialize the state (btree cursor).
1280 */
1281 state = xfs_da_state_alloc();
1282 state->args = args;
1283 state->mp = args->dp->i_mount;
1284 state->blocksize = state->mp->m_dirblksize;
1285 state->node_ents = state->mp->m_dir_node_ents;
1286 /*
1287 * Look up the name. We're not supposed to find it, but
1288 * this gives us the insertion point.
1289 */
1290 error = xfs_da_node_lookup_int(state, &rval);
1291 if (error)
1292 rval = error;
1293 if (rval != ENOENT) {
1294 goto done;
1295 }
1296 /*
1297 * Add the data entry to a data block.
1298 * Extravalid is set to a freeblock found by lookup.
1299 */
1300 rval = xfs_dir2_node_addname_int(args,
1301 state->extravalid ? &state->extrablk : NULL);
1302 if (rval) {
1303 goto done;
1304 }
1305 blk = &state->path.blk[state->path.active - 1];
1306 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1307 /*
1308 * Add the new leaf entry.
1309 */
1310 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1311 if (rval == 0) {
1312 /*
1313 * It worked, fix the hash values up the btree.
1314 */
Barry Naujok6a178102008-05-21 16:42:05 +10001315 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 xfs_da_fixhashpath(state, &state->path);
1317 } else {
1318 /*
1319 * It didn't work, we need to split the leaf block.
1320 */
1321 if (args->total == 0) {
1322 ASSERT(rval == ENOSPC);
1323 goto done;
1324 }
1325 /*
1326 * Split the leaf block and insert the new entry.
1327 */
1328 rval = xfs_da_split(state);
1329 }
1330done:
1331 xfs_da_state_free(state);
1332 return rval;
1333}
1334
1335/*
1336 * Add the data entry for a node-format directory name addition.
1337 * The leaf entry is added in xfs_dir2_leafn_add.
1338 * We may enter with a freespace block that the lookup found.
1339 */
1340static int /* error */
1341xfs_dir2_node_addname_int(
1342 xfs_da_args_t *args, /* operation arguments */
1343 xfs_da_state_blk_t *fblk) /* optional freespace block */
1344{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001345 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001347 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1349 xfs_inode_t *dp; /* incore directory inode */
1350 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1351 int error; /* error return value */
1352 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001353 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int findex; /* freespace entry index */
1355 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1356 xfs_dir2_db_t ifbno; /* initial freespace block no */
1357 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1358 int length; /* length of the new entry */
1359 int logfree; /* need to log free entry */
1360 xfs_mount_t *mp; /* filesystem mount point */
1361 int needlog; /* need to log data header */
1362 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001363 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 xfs_trans_t *tp; /* transaction pointer */
1365
1366 dp = args->dp;
1367 mp = dp->i_mount;
1368 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001369 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /*
1371 * If we came in with a freespace block that means that lookup
1372 * found an entry with our hash value. This is the freespace
1373 * block for that data entry.
1374 */
1375 if (fblk) {
1376 fbp = fblk->bp;
1377 /*
1378 * Remember initial freespace block number.
1379 */
1380 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001381 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001382 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 findex = fblk->index;
1384 /*
1385 * This means the free entry showed that the data block had
1386 * space for our entry, so we remembered it.
1387 * Use that data block.
1388 */
1389 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001390 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1391 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1392 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1393 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395 /*
1396 * The data block looked at didn't have enough room.
1397 * We'll start at the beginning of the freespace entries.
1398 */
1399 else {
1400 dbno = -1;
1401 findex = 0;
1402 }
1403 }
1404 /*
1405 * Didn't come in with a freespace block, so don't have a data block.
1406 */
1407 else {
1408 ifbno = dbno = -1;
1409 fbp = NULL;
1410 findex = 0;
1411 }
1412 /*
1413 * If we don't have a data block yet, we're going to scan the
1414 * freespace blocks looking for one. Figure out what the
1415 * highest freespace block number is.
1416 */
1417 if (dbno == -1) {
1418 xfs_fileoff_t fo; /* freespace block number */
1419
1420 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1421 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001422 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 fbno = ifbno;
1424 }
1425 /*
1426 * While we haven't identified a data block, search the freeblock
1427 * data for a good data block. If we find a null freeblock entry,
1428 * indicating a hole in the data blocks, remember that.
1429 */
1430 while (dbno == -1) {
1431 /*
1432 * If we don't have a freeblock in hand, get the next one.
1433 */
1434 if (fbp == NULL) {
1435 /*
1436 * Happens the first time through unless lookup gave
1437 * us a freespace block to start with.
1438 */
1439 if (++fbno == 0)
1440 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1441 /*
1442 * If it's ifbno we already looked at it.
1443 */
1444 if (fbno == ifbno)
1445 fbno++;
1446 /*
1447 * If it's off the end we're done.
1448 */
1449 if (fbno >= lastfbno)
1450 break;
1451 /*
1452 * Read the block. There can be holes in the
1453 * freespace blocks, so this might not succeed.
1454 * This should be really rare, so there's no reason
1455 * to avoid it.
1456 */
1457 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001458 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 XFS_DATA_FORK))) {
1460 return error;
1461 }
1462 if (unlikely(fbp == NULL)) {
1463 continue;
1464 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001465 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001466 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 findex = 0;
1468 }
1469 /*
1470 * Look at the current free entry. Is it good enough?
1471 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001472 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1473 be16_to_cpu(free->bests[findex]) >= length)
1474 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 else {
1476 /*
1477 * Are we done with the freeblock?
1478 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001479 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /*
1481 * Drop the block.
1482 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001483 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 fbp = NULL;
1485 if (fblk && fblk->bp)
1486 fblk->bp = NULL;
1487 }
1488 }
1489 }
1490 /*
1491 * If we don't have a data block, we need to allocate one and make
1492 * the freespace entries refer to it.
1493 */
1494 if (unlikely(dbno == -1)) {
1495 /*
1496 * Not allowed to allocate, return failure.
1497 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001498 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /*
1502 * Allocate and initialize the new data block.
1503 */
1504 if (unlikely((error = xfs_dir2_grow_inode(args,
1505 XFS_DIR2_DATA_SPACE,
1506 &dbno)) ||
Dave Chinner1d9025e2012-06-22 18:50:14 +10001507 (error = xfs_dir2_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /*
1511 * If (somehow) we have a freespace block, get rid of it.
1512 */
1513 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001514 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (fblk && fblk->bp)
1516 fblk->bp = NULL;
1517
1518 /*
1519 * Get the freespace block corresponding to the data block
1520 * that was just allocated.
1521 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001522 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001524 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Dave Chinner1d9025e2012-06-22 18:50:14 +10001525 XFS_DATA_FORK)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /*
1529 * If there wasn't a freespace block, the read will
1530 * return a NULL fbp. Allocate and initialize a new one.
1531 */
1532 if( fbp == NULL ) {
1533 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1534 &fbno))) {
1535 return error;
1536 }
1537
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001538 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001539 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001540 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001541 " data block %lld, got %lld ifbno %llu lastfbno %d",
1542 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001543 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 (long long)dbno, (long long)fbno,
1545 (unsigned long long)ifbno, lastfbno);
1546 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001547 xfs_alert(mp,
1548 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 fblk,
1550 (unsigned long long)fblk->blkno,
1551 fblk->index,
1552 fblk->magic);
1553 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001554 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1557 XFS_ERRLEVEL_LOW, mp);
1558 return XFS_ERROR(EFSCORRUPTED);
1559 }
1560
1561 /*
1562 * Get a buffer for the new block.
1563 */
1564 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001565 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 -1, &fbp, XFS_DATA_FORK))) {
1567 return error;
1568 }
1569 ASSERT(fbp != NULL);
1570
1571 /*
1572 * Initialize the new block to be empty, and remember
1573 * its first slot as our empty slot.
1574 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575 free = fbp->b_addr;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001576 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1577 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001579 xfs_dir2_free_max_bests(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 free->hdr.nvalid = 0;
1581 free->hdr.nused = 0;
1582 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001583 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001584 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
1587 /*
1588 * Set the freespace block index from the data block number.
1589 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001590 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * If it's after the end of the current entries in the
1593 * freespace block, extend that table.
1594 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001595 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001596 ASSERT(findex < xfs_dir2_free_max_bests(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001597 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 /*
1599 * Tag new entry so nused will go up.
1600 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001601 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 /*
1604 * If this entry was for an empty data block
1605 * (this should always be true) then update the header.
1606 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001607 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001608 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 xfs_dir2_free_log_header(tp, fbp);
1610 }
1611 /*
1612 * Update the real value in the table.
1613 * We haven't allocated the data entry yet so this will
1614 * change again.
1615 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001616 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001617 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 logfree = 1;
1619 }
1620 /*
1621 * We had a data block so we don't have to make a new one.
1622 */
1623 else {
1624 /*
1625 * If just checking, we succeeded.
1626 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001627 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Read the data block in.
1632 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001633 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
1634 -1, &dbp, XFS_DATA_FORK);
1635 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001637 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 logfree = 0;
1639 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001640 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /*
1642 * Point to the existing unused space.
1643 */
1644 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001645 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 needscan = needlog = 0;
1647 /*
1648 * Mark the first part of the unused space, inuse for us.
1649 */
1650 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001651 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 &needlog, &needscan);
1653 /*
1654 * Fill in the new entry and log it.
1655 */
1656 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001657 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 dep->namelen = args->namelen;
1659 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001660 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001661 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 xfs_dir2_data_log_entry(tp, dbp, dep);
1663 /*
1664 * Rescan the block for bestfree if needed.
1665 */
1666 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001667 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * Log the data block header if needed.
1670 */
1671 if (needlog)
1672 xfs_dir2_data_log_header(tp, dbp);
1673 /*
1674 * If the freespace entry is now wrong, update it.
1675 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001676 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1677 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 logfree = 1;
1679 }
1680 /*
1681 * Log the freespace entry if needed.
1682 */
1683 if (logfree)
1684 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1685 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * Return the data block and offset in args, then drop the data block.
1687 */
1688 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001689 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691}
1692
1693/*
1694 * Lookup an entry in a node-format directory.
1695 * All the real work happens in xfs_da_node_lookup_int.
1696 * The only real output is the inode number of the entry.
1697 */
1698int /* error */
1699xfs_dir2_node_lookup(
1700 xfs_da_args_t *args) /* operation arguments */
1701{
1702 int error; /* error return value */
1703 int i; /* btree level */
1704 int rval; /* operation return value */
1705 xfs_da_state_t *state; /* btree cursor */
1706
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001707 trace_xfs_dir2_node_lookup(args);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /*
1710 * Allocate and initialize the btree cursor.
1711 */
1712 state = xfs_da_state_alloc();
1713 state->args = args;
1714 state->mp = args->dp->i_mount;
1715 state->blocksize = state->mp->m_dirblksize;
1716 state->node_ents = state->mp->m_dir_node_ents;
1717 /*
1718 * Fill in the path to the entry in the cursor.
1719 */
1720 error = xfs_da_node_lookup_int(state, &rval);
1721 if (error)
1722 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001723 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1724 /* If a CI match, dup the actual name and return EEXIST */
1725 xfs_dir2_data_entry_t *dep;
1726
Dave Chinner1d9025e2012-06-22 18:50:14 +10001727 dep = (xfs_dir2_data_entry_t *)
1728 ((char *)state->extrablk.bp->b_addr +
1729 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001730 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
1733 * Release the btree blocks and leaf block.
1734 */
1735 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001736 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 state->path.blk[i].bp = NULL;
1738 }
1739 /*
1740 * Release the data block if we have it.
1741 */
1742 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001743 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 state->extrablk.bp = NULL;
1745 }
1746 xfs_da_state_free(state);
1747 return rval;
1748}
1749
1750/*
1751 * Remove an entry from a node-format directory.
1752 */
1753int /* error */
1754xfs_dir2_node_removename(
1755 xfs_da_args_t *args) /* operation arguments */
1756{
1757 xfs_da_state_blk_t *blk; /* leaf block */
1758 int error; /* error return value */
1759 int rval; /* operation return value */
1760 xfs_da_state_t *state; /* btree cursor */
1761
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001762 trace_xfs_dir2_node_removename(args);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Allocate and initialize the btree cursor.
1766 */
1767 state = xfs_da_state_alloc();
1768 state->args = args;
1769 state->mp = args->dp->i_mount;
1770 state->blocksize = state->mp->m_dirblksize;
1771 state->node_ents = state->mp->m_dir_node_ents;
1772 /*
1773 * Look up the entry we're deleting, set up the cursor.
1774 */
1775 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001776 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /*
1779 * Didn't find it, upper layer screwed up.
1780 */
1781 if (rval != EEXIST) {
1782 xfs_da_state_free(state);
1783 return rval;
1784 }
1785 blk = &state->path.blk[state->path.active - 1];
1786 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1787 ASSERT(state->extravalid);
1788 /*
1789 * Remove the leaf and data entries.
1790 * Extrablk refers to the data block.
1791 */
1792 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1793 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /*
1797 * Fix the hash values up the btree.
1798 */
1799 xfs_da_fixhashpath(state, &state->path);
1800 /*
1801 * If we need to join leaf blocks, do it.
1802 */
1803 if (rval && state->path.active > 1)
1804 error = xfs_da_join(state);
1805 /*
1806 * If no errors so far, try conversion to leaf format.
1807 */
1808 if (!error)
1809 error = xfs_dir2_node_to_leaf(state);
1810 xfs_da_state_free(state);
1811 return error;
1812}
1813
1814/*
1815 * Replace an entry's inode number in a node-format directory.
1816 */
1817int /* error */
1818xfs_dir2_node_replace(
1819 xfs_da_args_t *args) /* operation arguments */
1820{
1821 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001822 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 xfs_dir2_data_entry_t *dep; /* data entry changed */
1824 int error; /* error return value */
1825 int i; /* btree level */
1826 xfs_ino_t inum; /* new inode number */
1827 xfs_dir2_leaf_t *leaf; /* leaf structure */
1828 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1829 int rval; /* internal return value */
1830 xfs_da_state_t *state; /* btree cursor */
1831
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001832 trace_xfs_dir2_node_replace(args);
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /*
1835 * Allocate and initialize the btree cursor.
1836 */
1837 state = xfs_da_state_alloc();
1838 state->args = args;
1839 state->mp = args->dp->i_mount;
1840 state->blocksize = state->mp->m_dirblksize;
1841 state->node_ents = state->mp->m_dir_node_ents;
1842 inum = args->inumber;
1843 /*
1844 * Lookup the entry to change in the btree.
1845 */
1846 error = xfs_da_node_lookup_int(state, &rval);
1847 if (error) {
1848 rval = error;
1849 }
1850 /*
1851 * It should be found, since the vnodeops layer has looked it up
1852 * and locked it. But paranoia is good.
1853 */
1854 if (rval == EEXIST) {
1855 /*
1856 * Find the leaf entry.
1857 */
1858 blk = &state->path.blk[state->path.active - 1];
1859 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001860 leaf = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 lep = &leaf->ents[blk->index];
1862 ASSERT(state->extravalid);
1863 /*
1864 * Point to the data entry.
1865 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001866 hdr = state->extrablk.bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001867 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001869 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001870 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001871 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /*
1873 * Fill in the new inode number and log the entry.
1874 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001875 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1877 rval = 0;
1878 }
1879 /*
1880 * Didn't find it, and we're holding a data block. Drop it.
1881 */
1882 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001883 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 state->extrablk.bp = NULL;
1885 }
1886 /*
1887 * Release all the buffers in the cursor.
1888 */
1889 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001890 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 state->path.blk[i].bp = NULL;
1892 }
1893 xfs_da_state_free(state);
1894 return rval;
1895}
1896
1897/*
1898 * Trim off a trailing empty freespace block.
1899 * Return (in rvalp) 1 if we did it, 0 if not.
1900 */
1901int /* error */
1902xfs_dir2_node_trim_free(
1903 xfs_da_args_t *args, /* operation arguments */
1904 xfs_fileoff_t fo, /* free block number */
1905 int *rvalp) /* out: did something */
1906{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001907 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 xfs_inode_t *dp; /* incore directory inode */
1909 int error; /* error return code */
1910 xfs_dir2_free_t *free; /* freespace structure */
1911 xfs_mount_t *mp; /* filesystem mount point */
1912 xfs_trans_t *tp; /* transaction pointer */
1913
1914 dp = args->dp;
1915 mp = dp->i_mount;
1916 tp = args->trans;
1917 /*
1918 * Read the freespace block.
1919 */
1920 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1921 XFS_DATA_FORK))) {
1922 return error;
1923 }
1924
1925 /*
1926 * There can be holes in freespace. If fo is a hole, there's
1927 * nothing to do.
1928 */
1929 if (bp == NULL) {
1930 return 0;
1931 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001932 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001933 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /*
1935 * If there are used entries, there's nothing to do.
1936 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001937 if (be32_to_cpu(free->hdr.nused) > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001938 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 *rvalp = 0;
1940 return 0;
1941 }
1942 /*
1943 * Blow the block away.
1944 */
1945 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001946 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 bp))) {
1948 /*
1949 * Can't fail with ENOSPC since that only happens with no
1950 * space reservation, when breaking up an extent into two
1951 * pieces. This is the last block of an extent.
1952 */
1953 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001954 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 return error;
1956 }
1957 /*
1958 * Return that we succeeded.
1959 */
1960 *rvalp = 1;
1961 return 0;
1962}