blob: d7f899dfbff5a62a91098f2752870beb3e4786fa [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
Dave Chinner20252072012-11-12 22:54:13 +110058static void
59xfs_dir2_free_verify(
60 struct xfs_buf *bp)
61{
62 struct xfs_mount *mp = bp->b_target->bt_mount;
63 struct xfs_dir2_free_hdr *hdr = bp->b_addr;
64 int block_ok = 0;
65
66 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC);
67 if (!block_ok) {
68 XFS_CORRUPTION_ERROR("xfs_dir2_free_verify magic",
69 XFS_ERRLEVEL_LOW, mp, hdr);
70 xfs_buf_ioerror(bp, EFSCORRUPTED);
71 }
72
73 bp->b_iodone = NULL;
74 xfs_buf_ioend(bp, 0);
75}
76
77static int
78__xfs_dir2_free_read(
79 struct xfs_trans *tp,
80 struct xfs_inode *dp,
81 xfs_dablk_t fbno,
82 xfs_daddr_t mappedbno,
83 struct xfs_buf **bpp)
84{
85 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
86 XFS_DATA_FORK, xfs_dir2_free_verify);
87}
88
89int
90xfs_dir2_free_read(
91 struct xfs_trans *tp,
92 struct xfs_inode *dp,
93 xfs_dablk_t fbno,
94 struct xfs_buf **bpp)
95{
96 return __xfs_dir2_free_read(tp, dp, fbno, -1, bpp);
97}
98
99static int
100xfs_dir2_free_try_read(
101 struct xfs_trans *tp,
102 struct xfs_inode *dp,
103 xfs_dablk_t fbno,
104 struct xfs_buf **bpp)
105{
106 return __xfs_dir2_free_read(tp, dp, fbno, -2, bpp);
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Log entries from a freespace block.
111 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000112STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000114 struct xfs_trans *tp,
115 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int first, /* first entry to log */
117 int last) /* last entry to log */
118{
119 xfs_dir2_free_t *free; /* freespace structure */
120
Dave Chinner1d9025e2012-06-22 18:50:14 +1000121 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200122 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000123 xfs_trans_log_buf(tp, bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 (uint)((char *)&free->bests[first] - (char *)free),
125 (uint)((char *)&free->bests[last] - (char *)free +
126 sizeof(free->bests[0]) - 1));
127}
128
129/*
130 * Log header from a freespace block.
131 */
132static void
133xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000134 struct xfs_trans *tp,
135 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 xfs_dir2_free_t *free; /* freespace structure */
138
Dave Chinner1d9025e2012-06-22 18:50:14 +1000139 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200140 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000141 xfs_trans_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
143}
144
145/*
146 * Convert a leaf-format directory to a node-format directory.
147 * We need to change the magic number of the leaf block, and copy
148 * the freespace table out of the leaf block into its own block.
149 */
150int /* error */
151xfs_dir2_leaf_to_node(
152 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000153 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 xfs_inode_t *dp; /* incore directory inode */
156 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000157 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 xfs_dir2_db_t fdb; /* freespace block number */
159 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100160 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int i; /* leaf freespace index */
162 xfs_dir2_leaf_t *leaf; /* leaf structure */
163 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
164 xfs_mount_t *mp; /* filesystem mount point */
165 int n; /* count of live freespc ents */
166 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100167 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 xfs_trans_t *tp; /* transaction pointer */
169
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000170 trace_xfs_dir2_leaf_to_node(args);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 dp = args->dp;
173 mp = dp->i_mount;
174 tp = args->trans;
175 /*
176 * Add a freespace block to the directory.
177 */
178 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
179 return error;
180 }
181 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
182 /*
183 * Get the buffer for the new freespace block.
184 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000185 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 XFS_DATA_FORK))) {
187 return error;
188 }
189 ASSERT(fbp != NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000190 free = fbp->b_addr;
191 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000192 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /*
194 * Initialize the freespace block header.
195 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100196 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100198 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100199 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /*
201 * Copy freespace entries from the leaf block to the new block.
202 * Count active entries.
203 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000204 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100205 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100206 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100208 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100210 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100211 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /*
213 * Log everything.
214 */
215 xfs_dir2_leaf_log_header(tp, lbp);
216 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100217 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 xfs_dir2_leafn_check(dp, lbp);
219 return 0;
220}
221
222/*
223 * Add a leaf entry to a leaf block in a node-form directory.
224 * The other work necessary is done from the caller.
225 */
226static int /* error */
227xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000228 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 xfs_da_args_t *args, /* operation arguments */
230 int index) /* insertion pt for new entry */
231{
232 int compact; /* compacting stale leaves */
233 xfs_inode_t *dp; /* incore directory inode */
234 int highstale; /* next stale entry */
235 xfs_dir2_leaf_t *leaf; /* leaf structure */
236 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
237 int lfloghigh; /* high leaf entry logging */
238 int lfloglow; /* low leaf entry logging */
239 int lowstale; /* previous stale entry */
240 xfs_mount_t *mp; /* filesystem mount point */
241 xfs_trans_t *tp; /* transaction pointer */
242
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000243 trace_xfs_dir2_leafn_add(args, index);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 dp = args->dp;
246 mp = dp->i_mount;
247 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000248 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /*
251 * Quick check just to make sure we are not going to index
252 * into other peoples memory
253 */
254 if (index < 0)
255 return XFS_ERROR(EFSCORRUPTED);
256
257 /*
258 * If there are already the maximum number of leaf entries in
259 * the block, if there are no stale entries it won't fit.
260 * Caller will do a split. If there are stale entries we'll do
261 * a compact.
262 */
263
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000264 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!leaf->hdr.stale)
266 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100267 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 } else
269 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100270 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100271 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100272 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Barry Naujok6a178102008-05-21 16:42:05 +1000274 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276
277 /*
278 * Compact out all but one stale leaf entry. Leaves behind
279 * the entry closest to index.
280 */
281 if (compact) {
282 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
283 &lfloglow, &lfloghigh);
284 }
285 /*
286 * Set impossible logging indices for this case.
287 */
288 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100289 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 lfloghigh = -1;
291 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /*
294 * Insert the new entry, log everything.
295 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200296 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
297 highstale, &lfloglow, &lfloghigh);
298
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100299 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000300 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100301 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 xfs_dir2_leaf_log_header(tp, bp);
303 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
304 xfs_dir2_leafn_check(dp, bp);
305 return 0;
306}
307
308#ifdef DEBUG
309/*
310 * Check internal consistency of a leafn block.
311 */
312void
313xfs_dir2_leafn_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000314 struct xfs_inode *dp,
315 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 int i; /* leaf index */
318 xfs_dir2_leaf_t *leaf; /* leaf structure */
319 xfs_mount_t *mp; /* filesystem mount point */
320 int stale; /* count of stale leaves */
321
Dave Chinner1d9025e2012-06-22 18:50:14 +1000322 leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200324 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000325 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100326 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
327 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100328 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
329 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200331 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 stale++;
333 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100334 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336#endif /* DEBUG */
337
338/*
339 * Return the last hash value in the leaf.
340 * Stale entries are ok.
341 */
342xfs_dahash_t /* hash value */
343xfs_dir2_leafn_lasthash(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000344 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 int *count) /* count of entries in leaf */
346{
347 xfs_dir2_leaf_t *leaf; /* leaf structure */
348
Dave Chinner1d9025e2012-06-22 18:50:14 +1000349 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200350 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100352 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (!leaf->hdr.count)
354 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100355 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000359 * Look up a leaf entry for space to add a name in a node-format leaf block.
360 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000362STATIC int
363xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000364 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 xfs_da_args_t *args, /* operation arguments */
366 int *indexp, /* out: leaf entry index */
367 xfs_da_state_t *state) /* state to fill in */
368{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000369 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000370 xfs_dir2_db_t curdb = -1; /* current data block number */
371 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfs_inode_t *dp; /* incore directory inode */
373 int error; /* error return value */
374 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000375 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 int index; /* leaf entry index */
377 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000378 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
380 xfs_mount_t *mp; /* filesystem mount point */
381 xfs_dir2_db_t newdb; /* new data block number */
382 xfs_dir2_db_t newfdb; /* new free block number */
383 xfs_trans_t *tp; /* transaction pointer */
384
385 dp = args->dp;
386 tp = args->trans;
387 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000388 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200389 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100391 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#endif
393 xfs_dir2_leafn_check(dp, bp);
394 /*
395 * Look up the hash value in the leaf entries.
396 */
397 index = xfs_dir2_leaf_search_hash(args, bp);
398 /*
399 * Do we have a buffer coming in?
400 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000401 if (state->extravalid) {
402 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000404 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000405 free = curbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200406 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000408 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Loop over leaf entries with the right hash value.
411 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000412 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
413 be32_to_cpu(lep->hashval) == args->hashval;
414 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Skip stale leaf entries.
417 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100418 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 continue;
420 /*
421 * Pull the data block number from the entry.
422 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000423 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * For addname, we're looking for a place to put the new entry.
426 * We want to use a data block with an entry of equal
427 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000428 *
429 * If this block isn't the data block we already have
430 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000432 if (newdb != curdb) {
433 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000435 * Convert the data block to the free block
436 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000438 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000440 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000442 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000444 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
446 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000447 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100448
449 error = xfs_dir2_free_read(tp, dp,
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000450 xfs_dir2_db_to_da(mp, newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100451 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000452 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000454 free = curbp->b_addr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000455 ASSERT(be32_to_cpu(free->hdr.magic) ==
456 XFS_DIR2_FREE_MAGIC);
457 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Christoph Hellwiga00b7742011-07-13 13:43:48 +0200458 xfs_dir2_free_max_bests(mp)) == 0);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000459 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
460 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
461 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000464 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000466 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000468 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200470 if (unlikely(free->bests[fi] ==
471 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000472 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
473 XFS_ERRLEVEL_LOW, mp);
474 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000475 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000476 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000478 curfdb = newfdb;
479 if (be16_to_cpu(free->bests[fi]) >= length)
480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000483 /* Didn't find any space */
484 fi = -1;
485out:
Barry Naujok6a178102008-05-21 16:42:05 +1000486 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000487 if (curbp) {
488 /* Giving back a free block. */
489 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000491 state->extrablk.index = fi;
492 state->extrablk.blkno = curfdb;
493 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
494 } else {
495 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000498 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return XFS_ERROR(ENOENT);
502}
503
504/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000505 * Look up a leaf entry in a node-format leaf block.
506 * The extrablk in state a data block.
507 */
508STATIC int
509xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000510 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000511 xfs_da_args_t *args, /* operation arguments */
512 int *indexp, /* out: leaf entry index */
513 xfs_da_state_t *state) /* state to fill in */
514{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000515 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000516 xfs_dir2_db_t curdb = -1; /* current data block number */
517 xfs_dir2_data_entry_t *dep; /* data block entry */
518 xfs_inode_t *dp; /* incore directory inode */
519 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000520 int index; /* leaf entry index */
521 xfs_dir2_leaf_t *leaf; /* leaf structure */
522 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
523 xfs_mount_t *mp; /* filesystem mount point */
524 xfs_dir2_db_t newdb; /* new data block number */
525 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000526 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000527
528 dp = args->dp;
529 tp = args->trans;
530 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000531 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200532 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000533#ifdef __KERNEL__
534 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
535#endif
536 xfs_dir2_leafn_check(dp, bp);
537 /*
538 * Look up the hash value in the leaf entries.
539 */
540 index = xfs_dir2_leaf_search_hash(args, bp);
541 /*
542 * Do we have a buffer coming in?
543 */
544 if (state->extravalid) {
545 curbp = state->extrablk.bp;
546 curdb = state->extrablk.blkno;
547 }
548 /*
549 * Loop over leaf entries with the right hash value.
550 */
551 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
552 be32_to_cpu(lep->hashval) == args->hashval;
553 lep++, index++) {
554 /*
555 * Skip stale leaf entries.
556 */
557 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
558 continue;
559 /*
560 * Pull the data block number from the entry.
561 */
562 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
563 /*
564 * Not adding a new entry, so we really want to find
565 * the name given to us.
566 *
567 * If it's a different data block, go get it.
568 */
569 if (newdb != curdb) {
570 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000571 * If we had a block before that we aren't saving
572 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000573 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000574 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
575 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000576 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000577 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000578 * If needing the block that is saved with a CI match,
579 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000580 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000581 if (args->cmpresult != XFS_CMP_DIFFERENT &&
582 newdb == state->extrablk.blkno) {
583 ASSERT(state->extravalid);
584 curbp = state->extrablk.bp;
585 } else {
586 error = xfs_da_read_buf(tp, dp,
587 xfs_dir2_db_to_da(mp, newdb),
Dave Chinner4bb20a82012-11-12 22:54:10 +1100588 -1, &curbp, XFS_DATA_FORK, NULL);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000589 if (error)
590 return error;
591 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000592 xfs_dir2_data_check(dp, curbp);
593 curdb = newdb;
594 }
595 /*
596 * Point to the data entry.
597 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000598 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
600 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000601 * Compare the entry and if it's an exact match, return
602 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000603 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000604 */
Barry Naujok5163f952008-05-21 16:41:01 +1000605 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
606 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000607 /* If there is a CI match block, drop it */
608 if (args->cmpresult != XFS_CMP_DIFFERENT &&
609 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000610 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000611 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000613 *indexp = index;
614 state->extravalid = 1;
615 state->extrablk.bp = curbp;
616 state->extrablk.blkno = curdb;
617 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000618 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000619 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000620 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000621 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000622 }
623 }
Barry Naujok6a178102008-05-21 16:42:05 +1000624 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
625 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000626 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000627 if (args->cmpresult == XFS_CMP_DIFFERENT) {
628 /* Giving back last used data block. */
629 state->extravalid = 1;
630 state->extrablk.bp = curbp;
631 state->extrablk.index = -1;
632 state->extrablk.blkno = curdb;
633 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
634 } else {
635 /* If the curbp is not the CI match block, drop it */
636 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000637 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000638 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 } else {
640 state->extravalid = 0;
641 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000642 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000643 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644}
645
646/*
647 * Look up a leaf entry in a node-format leaf block.
648 * If this is an addname then the extrablk in state is a freespace block,
649 * otherwise it's a data block.
650 */
651int
652xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000653 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000654 xfs_da_args_t *args, /* operation arguments */
655 int *indexp, /* out: leaf entry index */
656 xfs_da_state_t *state) /* state to fill in */
657{
Barry Naujok6a178102008-05-21 16:42:05 +1000658 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000659 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
660 state);
661 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
662}
663
664/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * Move count leaf entries from source to destination leaf.
666 * Log entries and headers. Stale entries are preserved.
667 */
668static void
669xfs_dir2_leafn_moveents(
670 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 struct xfs_buf *bp_s, /* source leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 int start_s, /* source leaf index */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 struct xfs_buf *bp_d, /* destination leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int start_d, /* destination leaf index */
675 int count) /* count of leaves to copy */
676{
677 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
678 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
679 int stale; /* count stale leaves copied */
680 xfs_trans_t *tp; /* transaction pointer */
681
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000682 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /*
685 * Silently return if nothing to do.
686 */
687 if (count == 0) {
688 return;
689 }
690 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000691 leaf_s = bp_s->b_addr;
692 leaf_d = bp_d->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * If the destination index is not the end of the current
695 * destination leaf entries, open up a hole in the destination
696 * to hold the new entries.
697 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100698 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100700 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 sizeof(xfs_dir2_leaf_entry_t));
702 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100703 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 /*
706 * If the source has stale leaves, count the ones in the copy range
707 * so we can update the header correctly.
708 */
709 if (leaf_s->hdr.stale) {
710 int i; /* temp leaf index */
711
712 for (i = start_s, stale = 0; i < start_s + count; i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200713 if (leaf_s->ents[i].address ==
714 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 stale++;
716 }
717 } else
718 stale = 0;
719 /*
720 * Copy the leaf entries from source to destination.
721 */
722 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
723 count * sizeof(xfs_dir2_leaf_entry_t));
724 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
725 /*
726 * If there are source entries after the ones we copied,
727 * delete the ones we copied by sliding the next ones down.
728 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100729 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
731 count * sizeof(xfs_dir2_leaf_entry_t));
732 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
733 }
734 /*
735 * Update the headers and log them.
736 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800737 be16_add_cpu(&leaf_s->hdr.count, -(count));
738 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
739 be16_add_cpu(&leaf_d->hdr.count, count);
740 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 xfs_dir2_leaf_log_header(tp, bp_s);
742 xfs_dir2_leaf_log_header(tp, bp_d);
743 xfs_dir2_leafn_check(args->dp, bp_s);
744 xfs_dir2_leafn_check(args->dp, bp_d);
745}
746
747/*
748 * Determine the sort order of two leaf blocks.
749 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
750 */
751int /* sort order */
752xfs_dir2_leafn_order(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000753 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
754 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
757 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
758
Dave Chinner1d9025e2012-06-22 18:50:14 +1000759 leaf1 = leaf1_bp->b_addr;
760 leaf2 = leaf2_bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200761 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
762 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100763 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
764 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100765 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
766 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
767 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 1;
769 return 0;
770}
771
772/*
773 * Rebalance leaf entries between two leaf blocks.
774 * This is actually only called when the second block is new,
775 * though the code deals with the general case.
776 * A new entry will be inserted in one of the blocks, and that
777 * entry is taken into account when balancing.
778 */
779static void
780xfs_dir2_leafn_rebalance(
781 xfs_da_state_t *state, /* btree cursor */
782 xfs_da_state_blk_t *blk1, /* first btree block */
783 xfs_da_state_blk_t *blk2) /* second btree block */
784{
785 xfs_da_args_t *args; /* operation arguments */
786 int count; /* count (& direction) leaves */
787 int isleft; /* new goes in left leaf */
788 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
789 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
790 int mid; /* midpoint leaf index */
791#ifdef DEBUG
792 int oldstale; /* old count of stale leaves */
793#endif
794 int oldsum; /* old total leaf count */
795 int swap; /* swapped leaf blocks */
796
797 args = state->args;
798 /*
799 * If the block order is wrong, swap the arguments.
800 */
801 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
802 xfs_da_state_blk_t *tmp; /* temp for block swap */
803
804 tmp = blk1;
805 blk1 = blk2;
806 blk2 = tmp;
807 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000808 leaf1 = blk1->bp->b_addr;
809 leaf2 = blk2->bp->b_addr;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100810 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100812 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813#endif
814 mid = oldsum >> 1;
815 /*
816 * If the old leaf count was odd then the new one will be even,
817 * so we need to divide the new count evenly.
818 */
819 if (oldsum & 1) {
820 xfs_dahash_t midhash; /* middle entry hash value */
821
Nathan Scotta818e5d2006-03-17 17:28:07 +1100822 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100823 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100825 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 isleft = args->hashval <= midhash;
827 }
828 /*
829 * If the old count is even then the new count is odd, so there's
830 * no preferred side for the new entry.
831 * Pick the left one.
832 */
833 else
834 isleft = 1;
835 /*
836 * Calculate moved entry count. Positive means left-to-right,
837 * negative means right-to-left. Then move the entries.
838 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100839 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (count > 0)
841 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100842 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 else if (count < 0)
844 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100845 be16_to_cpu(leaf1->hdr.count), count);
846 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
847 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 /*
849 * Mark whether we're inserting into the old or new leaf.
850 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100851 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100853 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 state->inleaf = !swap;
855 else
856 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100857 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /*
859 * Adjust the expected index for insertion.
860 */
861 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100862 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000863
864 /*
865 * Finally sanity check just to make sure we are not returning a
866 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 */
868 if(blk2->index < 0) {
869 state->inleaf = 1;
870 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100871 xfs_alert(args->dp->i_mount,
872 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
873 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875}
876
Dave Chinner20252072012-11-12 22:54:13 +1100877static int
878xfs_dir2_data_block_free(
879 xfs_da_args_t *args,
880 struct xfs_dir2_data_hdr *hdr,
881 struct xfs_dir2_free *free,
882 xfs_dir2_db_t fdb,
883 int findex,
884 struct xfs_buf *fbp,
885 int longest)
886{
887 struct xfs_trans *tp = args->trans;
888 int logfree = 0;
889
890 if (!hdr) {
891 /* One less used entry in the free table. */
892 be32_add_cpu(&free->hdr.nused, -1);
893 xfs_dir2_free_log_header(tp, fbp);
894
895 /*
896 * If this was the last entry in the table, we can trim the
897 * table size back. There might be other entries at the end
898 * referring to non-existent data blocks, get those too.
899 */
900 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
901 int i; /* free entry index */
902
903 for (i = findex - 1; i >= 0; i--) {
904 if (free->bests[i] != cpu_to_be16(NULLDATAOFF))
905 break;
906 }
907 free->hdr.nvalid = cpu_to_be32(i + 1);
908 logfree = 0;
909 } else {
910 /* Not the last entry, just punch it out. */
911 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
912 logfree = 1;
913 }
914 /*
915 * If there are no useful entries left in the block,
916 * get rid of the block if we can.
917 */
918 if (!free->hdr.nused) {
919 int error;
920
921 error = xfs_dir2_shrink_inode(args, fdb, fbp);
922 if (error == 0) {
923 fbp = NULL;
924 logfree = 0;
925 } else if (error != ENOSPC || args->total != 0)
926 return error;
927 /*
928 * It's possible to get ENOSPC if there is no
929 * space reservation. In this case some one
930 * else will eventually get rid of this block.
931 */
932 }
933 } else {
934 /*
935 * Data block is not empty, just set the free entry to the new
936 * value.
937 */
938 free->bests[findex] = cpu_to_be16(longest);
939 logfree = 1;
940 }
941
942 /* Log the free entry that changed, unless we got rid of it. */
943 if (logfree)
944 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
945 return 0;
946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * Remove an entry from a node directory.
950 * This removes the leaf entry and the data entry,
951 * and updates the free block if necessary.
952 */
953static int /* error */
954xfs_dir2_leafn_remove(
955 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000956 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int index, /* leaf entry index */
958 xfs_da_state_blk_t *dblk, /* data block */
959 int *rval) /* resulting block needs join */
960{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200961 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000963 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 xfs_dir2_data_entry_t *dep; /* data block entry */
965 xfs_inode_t *dp; /* incore directory inode */
966 xfs_dir2_leaf_t *leaf; /* leaf structure */
967 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
968 int longest; /* longest data free entry */
969 int off; /* data block entry offset */
970 xfs_mount_t *mp; /* filesystem mount point */
971 int needlog; /* need to log data header */
972 int needscan; /* need to rescan data frees */
973 xfs_trans_t *tp; /* transaction pointer */
974
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000975 trace_xfs_dir2_leafn_remove(args, index);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 dp = args->dp;
978 tp = args->trans;
979 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000980 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200981 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /*
983 * Point to the entry we're removing.
984 */
985 lep = &leaf->ents[index];
986 /*
987 * Extract the data block and offset from the entry.
988 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000989 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000991 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ASSERT(dblk->index == off);
993 /*
994 * Kill the leaf entry by marking it stale.
995 * Log the leaf block changes.
996 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800997 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100999 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 xfs_dir2_leaf_log_ents(tp, bp, index, index);
1001 /*
1002 * Make the data entry free. Keep track of the longest freespace
1003 * in the data block in case it changes.
1004 */
1005 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001006 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001007 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
1008 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 needlog = needscan = 0;
1010 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001011 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Rescan the data block freespaces for bestfree.
1014 * Log the data block header if needed.
1015 */
1016 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001017 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (needlog)
1019 xfs_dir2_data_log_header(tp, dbp);
1020 xfs_dir2_data_check(dp, dbp);
1021 /*
1022 * If the longest data block freespace changes, need to update
1023 * the corresponding freeblock entry.
1024 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001025 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001027 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 xfs_dir2_db_t fdb; /* freeblock block number */
1029 int findex; /* index in freeblock entries */
1030 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 /*
1033 * Convert the data block number to a free block,
1034 * read in the free block.
1035 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001036 fdb = xfs_dir2_db_to_fdb(mp, db);
Dave Chinner20252072012-11-12 22:54:13 +11001037 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1038 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001039 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001041 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001042 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001043 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001044 xfs_dir2_free_max_bests(mp) *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1046 /*
1047 * Calculate which entry we need to fix.
1048 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001049 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001050 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
1052 * If the data block is now empty we can get rid of it
1053 * (usually).
1054 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001055 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
1057 * Try to punch out the data block.
1058 */
1059 error = xfs_dir2_shrink_inode(args, db, dbp);
1060 if (error == 0) {
1061 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001062 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064 /*
1065 * We can get ENOSPC if there's no space reservation.
1066 * In this case just drop the buffer and some one else
1067 * will eventually get rid of the empty block.
1068 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001069 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return error;
1071 }
1072 /*
1073 * If we got rid of the data block, we can eliminate that entry
1074 * in the free block.
1075 */
Dave Chinner20252072012-11-12 22:54:13 +11001076 error = xfs_dir2_data_block_free(args, hdr, free,
1077 fdb, findex, fbp, longest);
1078 if (error)
1079 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Dave Chinner20252072012-11-12 22:54:13 +11001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 xfs_dir2_leafn_check(dp, bp);
1083 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001084 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * to justify trying to join it with a neighbor.
1086 */
1087 *rval =
1088 ((uint)sizeof(leaf->hdr) +
1089 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001090 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 mp->m_dir_magicpct;
1092 return 0;
1093}
1094
1095/*
1096 * Split the leaf entries in the old block into old and new blocks.
1097 */
1098int /* error */
1099xfs_dir2_leafn_split(
1100 xfs_da_state_t *state, /* btree cursor */
1101 xfs_da_state_blk_t *oldblk, /* original block */
1102 xfs_da_state_blk_t *newblk) /* newly created block */
1103{
1104 xfs_da_args_t *args; /* operation arguments */
1105 xfs_dablk_t blkno; /* new leaf block number */
1106 int error; /* error return value */
1107 xfs_mount_t *mp; /* filesystem mount point */
1108
1109 /*
1110 * Allocate space for a new leaf node.
1111 */
1112 args = state->args;
1113 mp = args->dp->i_mount;
1114 ASSERT(args != NULL);
1115 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1116 error = xfs_da_grow_inode(args, &blkno);
1117 if (error) {
1118 return error;
1119 }
1120 /*
1121 * Initialize the new leaf block.
1122 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001123 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1125 if (error) {
1126 return error;
1127 }
1128 newblk->blkno = blkno;
1129 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1130 /*
1131 * Rebalance the entries across the two leaves, link the new
1132 * block into the leaves.
1133 */
1134 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1135 error = xfs_da_blk_link(state, oldblk, newblk);
1136 if (error) {
1137 return error;
1138 }
1139 /*
1140 * Insert the new entry in the correct block.
1141 */
1142 if (state->inleaf)
1143 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1144 else
1145 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1146 /*
1147 * Update last hashval in each block since we added the name.
1148 */
1149 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1150 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1151 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1152 xfs_dir2_leafn_check(args->dp, newblk->bp);
1153 return error;
1154}
1155
1156/*
1157 * Check a leaf block and its neighbors to see if the block should be
1158 * collapsed into one or the other neighbor. Always keep the block
1159 * with the smaller block number.
1160 * If the current block is over 50% full, don't try to join it, return 0.
1161 * If the block is empty, fill in the state structure and return 2.
1162 * If it can be collapsed, fill in the state structure and return 1.
1163 * If nothing can be done, return 0.
1164 */
1165int /* error */
1166xfs_dir2_leafn_toosmall(
1167 xfs_da_state_t *state, /* btree cursor */
1168 int *action) /* resulting action to take */
1169{
1170 xfs_da_state_blk_t *blk; /* leaf block */
1171 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001172 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int bytes; /* bytes in use */
1174 int count; /* leaf live entry count */
1175 int error; /* error return value */
1176 int forward; /* sibling block direction */
1177 int i; /* sibling counter */
1178 xfs_da_blkinfo_t *info; /* leaf block header */
1179 xfs_dir2_leaf_t *leaf; /* leaf structure */
1180 int rval; /* result from path_shift */
1181
1182 /*
1183 * Check for the degenerate case of the block being over 50% full.
1184 * If so, it's not worth even looking to see if we might be able
1185 * to coalesce with a sibling.
1186 */
1187 blk = &state->path.blk[state->path.active - 1];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001188 info = blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001189 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001191 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1193 if (bytes > (state->blocksize >> 1)) {
1194 /*
1195 * Blk over 50%, don't try to join.
1196 */
1197 *action = 0;
1198 return 0;
1199 }
1200 /*
1201 * Check for the degenerate case of the block being empty.
1202 * If the block is empty, we'll simply delete it, no need to
1203 * coalesce it with a sibling block. We choose (arbitrarily)
1204 * to merge with the forward block unless it is NULL.
1205 */
1206 if (count == 0) {
1207 /*
1208 * Make altpath point to the block we want to keep and
1209 * path point to the block we want to drop (this one).
1210 */
Nathan Scott89da0542006-03-17 17:28:40 +11001211 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 memcpy(&state->altpath, &state->path, sizeof(state->path));
1213 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1214 &rval);
1215 if (error)
1216 return error;
1217 *action = rval ? 2 : 0;
1218 return 0;
1219 }
1220 /*
1221 * Examine each sibling block to see if we can coalesce with
1222 * at least 25% free space to spare. We need to figure out
1223 * whether to merge with the forward or the backward block.
1224 * We prefer coalescing with the lower numbered sibling so as
1225 * to shrink a directory over time.
1226 */
Nathan Scott89da0542006-03-17 17:28:40 +11001227 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001229 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (blkno == 0)
1231 continue;
1232 /*
1233 * Read the sibling leaf block.
1234 */
Dave Chinner4bb20a82012-11-12 22:54:10 +11001235 error = xfs_da_read_buf(state->args->trans, state->args->dp,
1236 blkno, -1, &bp, XFS_DATA_FORK, NULL);
1237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ASSERT(bp != NULL);
1240 /*
1241 * Count bytes in the two blocks combined.
1242 */
1243 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001244 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001246 leaf = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001247 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +11001248 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 bytes -= count * (uint)sizeof(leaf->ents[0]);
1250 /*
1251 * Fits with at least 25% to spare.
1252 */
1253 if (bytes >= 0)
1254 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001255 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257 /*
1258 * Didn't like either block, give up.
1259 */
1260 if (i >= 2) {
1261 *action = 0;
1262 return 0;
1263 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 /*
1266 * Make altpath point to the block we want to keep (the lower
1267 * numbered block) and path point to the block we want to drop.
1268 */
1269 memcpy(&state->altpath, &state->path, sizeof(state->path));
1270 if (blkno < blk->blkno)
1271 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1272 &rval);
1273 else
1274 error = xfs_da_path_shift(state, &state->path, forward, 0,
1275 &rval);
1276 if (error) {
1277 return error;
1278 }
1279 *action = rval ? 0 : 1;
1280 return 0;
1281}
1282
1283/*
1284 * Move all the leaf entries from drop_blk to save_blk.
1285 * This is done as part of a join operation.
1286 */
1287void
1288xfs_dir2_leafn_unbalance(
1289 xfs_da_state_t *state, /* cursor */
1290 xfs_da_state_blk_t *drop_blk, /* dead block */
1291 xfs_da_state_blk_t *save_blk) /* surviving block */
1292{
1293 xfs_da_args_t *args; /* operation arguments */
1294 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1295 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1296
1297 args = state->args;
1298 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1299 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001300 drop_leaf = drop_blk->bp->b_addr;
1301 save_leaf = save_blk->bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001302 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1303 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * If there are any stale leaf entries, take this opportunity
1306 * to purge them.
1307 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001308 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001310 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 xfs_dir2_leaf_compact(args, save_blk->bp);
1312 /*
1313 * Move the entries from drop to the appropriate end of save.
1314 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001315 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 -07001316 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1317 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001318 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 else
1320 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001321 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001322 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 -07001323 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1324}
1325
1326/*
1327 * Top-level node form directory addname routine.
1328 */
1329int /* error */
1330xfs_dir2_node_addname(
1331 xfs_da_args_t *args) /* operation arguments */
1332{
1333 xfs_da_state_blk_t *blk; /* leaf block for insert */
1334 int error; /* error return value */
1335 int rval; /* sub-return value */
1336 xfs_da_state_t *state; /* btree cursor */
1337
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001338 trace_xfs_dir2_node_addname(args);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Allocate and initialize the state (btree cursor).
1342 */
1343 state = xfs_da_state_alloc();
1344 state->args = args;
1345 state->mp = args->dp->i_mount;
1346 state->blocksize = state->mp->m_dirblksize;
1347 state->node_ents = state->mp->m_dir_node_ents;
1348 /*
1349 * Look up the name. We're not supposed to find it, but
1350 * this gives us the insertion point.
1351 */
1352 error = xfs_da_node_lookup_int(state, &rval);
1353 if (error)
1354 rval = error;
1355 if (rval != ENOENT) {
1356 goto done;
1357 }
1358 /*
1359 * Add the data entry to a data block.
1360 * Extravalid is set to a freeblock found by lookup.
1361 */
1362 rval = xfs_dir2_node_addname_int(args,
1363 state->extravalid ? &state->extrablk : NULL);
1364 if (rval) {
1365 goto done;
1366 }
1367 blk = &state->path.blk[state->path.active - 1];
1368 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1369 /*
1370 * Add the new leaf entry.
1371 */
1372 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1373 if (rval == 0) {
1374 /*
1375 * It worked, fix the hash values up the btree.
1376 */
Barry Naujok6a178102008-05-21 16:42:05 +10001377 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 xfs_da_fixhashpath(state, &state->path);
1379 } else {
1380 /*
1381 * It didn't work, we need to split the leaf block.
1382 */
1383 if (args->total == 0) {
1384 ASSERT(rval == ENOSPC);
1385 goto done;
1386 }
1387 /*
1388 * Split the leaf block and insert the new entry.
1389 */
1390 rval = xfs_da_split(state);
1391 }
1392done:
1393 xfs_da_state_free(state);
1394 return rval;
1395}
1396
1397/*
1398 * Add the data entry for a node-format directory name addition.
1399 * The leaf entry is added in xfs_dir2_leafn_add.
1400 * We may enter with a freespace block that the lookup found.
1401 */
1402static int /* error */
1403xfs_dir2_node_addname_int(
1404 xfs_da_args_t *args, /* operation arguments */
1405 xfs_da_state_blk_t *fblk) /* optional freespace block */
1406{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001407 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001409 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1411 xfs_inode_t *dp; /* incore directory inode */
1412 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1413 int error; /* error return value */
1414 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001415 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int findex; /* freespace entry index */
1417 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1418 xfs_dir2_db_t ifbno; /* initial freespace block no */
1419 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1420 int length; /* length of the new entry */
1421 int logfree; /* need to log free entry */
1422 xfs_mount_t *mp; /* filesystem mount point */
1423 int needlog; /* need to log data header */
1424 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001425 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 xfs_trans_t *tp; /* transaction pointer */
1427
1428 dp = args->dp;
1429 mp = dp->i_mount;
1430 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001431 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * If we came in with a freespace block that means that lookup
1434 * found an entry with our hash value. This is the freespace
1435 * block for that data entry.
1436 */
1437 if (fblk) {
1438 fbp = fblk->bp;
1439 /*
1440 * Remember initial freespace block number.
1441 */
1442 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001443 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001444 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 findex = fblk->index;
1446 /*
1447 * This means the free entry showed that the data block had
1448 * space for our entry, so we remembered it.
1449 * Use that data block.
1450 */
1451 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001452 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1453 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1454 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1455 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 /*
1458 * The data block looked at didn't have enough room.
1459 * We'll start at the beginning of the freespace entries.
1460 */
1461 else {
1462 dbno = -1;
1463 findex = 0;
1464 }
1465 }
1466 /*
1467 * Didn't come in with a freespace block, so don't have a data block.
1468 */
1469 else {
1470 ifbno = dbno = -1;
1471 fbp = NULL;
1472 findex = 0;
1473 }
1474 /*
1475 * If we don't have a data block yet, we're going to scan the
1476 * freespace blocks looking for one. Figure out what the
1477 * highest freespace block number is.
1478 */
1479 if (dbno == -1) {
1480 xfs_fileoff_t fo; /* freespace block number */
1481
1482 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1483 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001484 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 fbno = ifbno;
1486 }
1487 /*
1488 * While we haven't identified a data block, search the freeblock
1489 * data for a good data block. If we find a null freeblock entry,
1490 * indicating a hole in the data blocks, remember that.
1491 */
1492 while (dbno == -1) {
1493 /*
1494 * If we don't have a freeblock in hand, get the next one.
1495 */
1496 if (fbp == NULL) {
1497 /*
1498 * Happens the first time through unless lookup gave
1499 * us a freespace block to start with.
1500 */
1501 if (++fbno == 0)
1502 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1503 /*
1504 * If it's ifbno we already looked at it.
1505 */
1506 if (fbno == ifbno)
1507 fbno++;
1508 /*
1509 * If it's off the end we're done.
1510 */
1511 if (fbno >= lastfbno)
1512 break;
1513 /*
1514 * Read the block. There can be holes in the
1515 * freespace blocks, so this might not succeed.
1516 * This should be really rare, so there's no reason
1517 * to avoid it.
1518 */
Dave Chinner20252072012-11-12 22:54:13 +11001519 error = xfs_dir2_free_try_read(tp, dp,
1520 xfs_dir2_db_to_da(mp, fbno),
1521 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001522 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001524 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001527 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 findex = 0;
1529 }
1530 /*
1531 * Look at the current free entry. Is it good enough?
1532 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001533 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1534 be16_to_cpu(free->bests[findex]) >= length)
1535 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 else {
1537 /*
1538 * Are we done with the freeblock?
1539 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001540 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
1542 * Drop the block.
1543 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001544 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 fbp = NULL;
1546 if (fblk && fblk->bp)
1547 fblk->bp = NULL;
1548 }
1549 }
1550 }
1551 /*
1552 * If we don't have a data block, we need to allocate one and make
1553 * the freespace entries refer to it.
1554 */
1555 if (unlikely(dbno == -1)) {
1556 /*
1557 * Not allowed to allocate, return failure.
1558 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001559 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 /*
1563 * Allocate and initialize the new data block.
1564 */
1565 if (unlikely((error = xfs_dir2_grow_inode(args,
1566 XFS_DIR2_DATA_SPACE,
1567 &dbno)) ||
Dave Chinner1d9025e2012-06-22 18:50:14 +10001568 (error = xfs_dir2_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /*
1572 * If (somehow) we have a freespace block, get rid of it.
1573 */
1574 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001575 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (fblk && fblk->bp)
1577 fblk->bp = NULL;
1578
1579 /*
1580 * Get the freespace block corresponding to the data block
1581 * that was just allocated.
1582 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001583 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001584 error = xfs_dir2_free_try_read(tp, dp,
1585 xfs_dir2_db_to_da(mp, fbno),
1586 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001587 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * If there wasn't a freespace block, the read will
1592 * return a NULL fbp. Allocate and initialize a new one.
1593 */
1594 if( fbp == NULL ) {
1595 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1596 &fbno))) {
1597 return error;
1598 }
1599
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001600 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001601 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001602 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001603 " data block %lld, got %lld ifbno %llu lastfbno %d",
1604 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001605 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 (long long)dbno, (long long)fbno,
1607 (unsigned long long)ifbno, lastfbno);
1608 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001609 xfs_alert(mp,
1610 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 fblk,
1612 (unsigned long long)fblk->blkno,
1613 fblk->index,
1614 fblk->magic);
1615 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001616 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1619 XFS_ERRLEVEL_LOW, mp);
1620 return XFS_ERROR(EFSCORRUPTED);
1621 }
1622
1623 /*
1624 * Get a buffer for the new block.
1625 */
1626 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001627 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 -1, &fbp, XFS_DATA_FORK))) {
1629 return error;
1630 }
1631 ASSERT(fbp != NULL);
1632
1633 /*
1634 * Initialize the new block to be empty, and remember
1635 * its first slot as our empty slot.
1636 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001637 free = fbp->b_addr;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001638 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1639 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001641 xfs_dir2_free_max_bests(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 free->hdr.nvalid = 0;
1643 free->hdr.nused = 0;
1644 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001645 free = fbp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001646 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
1649 /*
1650 * Set the freespace block index from the data block number.
1651 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001652 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /*
1654 * If it's after the end of the current entries in the
1655 * freespace block, extend that table.
1656 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001657 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Christoph Hellwiga00b7742011-07-13 13:43:48 +02001658 ASSERT(findex < xfs_dir2_free_max_bests(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001659 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 /*
1661 * Tag new entry so nused will go up.
1662 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001663 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665 /*
1666 * If this entry was for an empty data block
1667 * (this should always be true) then update the header.
1668 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001669 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001670 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 xfs_dir2_free_log_header(tp, fbp);
1672 }
1673 /*
1674 * Update the real value in the table.
1675 * We haven't allocated the data entry yet so this will
1676 * change again.
1677 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001678 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001679 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 logfree = 1;
1681 }
1682 /*
1683 * We had a data block so we don't have to make a new one.
1684 */
1685 else {
1686 /*
1687 * If just checking, we succeeded.
1688 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001689 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Read the data block in.
1694 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001695 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Dave Chinner4bb20a82012-11-12 22:54:10 +11001696 -1, &dbp, XFS_DATA_FORK, NULL);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001697 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001699 hdr = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 logfree = 0;
1701 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001702 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
1704 * Point to the existing unused space.
1705 */
1706 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001707 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 needscan = needlog = 0;
1709 /*
1710 * Mark the first part of the unused space, inuse for us.
1711 */
1712 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001713 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 &needlog, &needscan);
1715 /*
1716 * Fill in the new entry and log it.
1717 */
1718 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001719 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 dep->namelen = args->namelen;
1721 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001722 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001723 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 xfs_dir2_data_log_entry(tp, dbp, dep);
1725 /*
1726 * Rescan the block for bestfree if needed.
1727 */
1728 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001729 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Log the data block header if needed.
1732 */
1733 if (needlog)
1734 xfs_dir2_data_log_header(tp, dbp);
1735 /*
1736 * If the freespace entry is now wrong, update it.
1737 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001738 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1739 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 logfree = 1;
1741 }
1742 /*
1743 * Log the freespace entry if needed.
1744 */
1745 if (logfree)
1746 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1747 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * Return the data block and offset in args, then drop the data block.
1749 */
1750 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001751 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753}
1754
1755/*
1756 * Lookup an entry in a node-format directory.
1757 * All the real work happens in xfs_da_node_lookup_int.
1758 * The only real output is the inode number of the entry.
1759 */
1760int /* error */
1761xfs_dir2_node_lookup(
1762 xfs_da_args_t *args) /* operation arguments */
1763{
1764 int error; /* error return value */
1765 int i; /* btree level */
1766 int rval; /* operation return value */
1767 xfs_da_state_t *state; /* btree cursor */
1768
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001769 trace_xfs_dir2_node_lookup(args);
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * Allocate and initialize the btree cursor.
1773 */
1774 state = xfs_da_state_alloc();
1775 state->args = args;
1776 state->mp = args->dp->i_mount;
1777 state->blocksize = state->mp->m_dirblksize;
1778 state->node_ents = state->mp->m_dir_node_ents;
1779 /*
1780 * Fill in the path to the entry in the cursor.
1781 */
1782 error = xfs_da_node_lookup_int(state, &rval);
1783 if (error)
1784 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001785 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1786 /* If a CI match, dup the actual name and return EEXIST */
1787 xfs_dir2_data_entry_t *dep;
1788
Dave Chinner1d9025e2012-06-22 18:50:14 +10001789 dep = (xfs_dir2_data_entry_t *)
1790 ((char *)state->extrablk.bp->b_addr +
1791 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001792 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * Release the btree blocks and leaf block.
1796 */
1797 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001798 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 state->path.blk[i].bp = NULL;
1800 }
1801 /*
1802 * Release the data block if we have it.
1803 */
1804 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001805 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 state->extrablk.bp = NULL;
1807 }
1808 xfs_da_state_free(state);
1809 return rval;
1810}
1811
1812/*
1813 * Remove an entry from a node-format directory.
1814 */
1815int /* error */
1816xfs_dir2_node_removename(
1817 xfs_da_args_t *args) /* operation arguments */
1818{
1819 xfs_da_state_blk_t *blk; /* leaf block */
1820 int error; /* error return value */
1821 int rval; /* operation return value */
1822 xfs_da_state_t *state; /* btree cursor */
1823
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001824 trace_xfs_dir2_node_removename(args);
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * Allocate and initialize the btree cursor.
1828 */
1829 state = xfs_da_state_alloc();
1830 state->args = args;
1831 state->mp = args->dp->i_mount;
1832 state->blocksize = state->mp->m_dirblksize;
1833 state->node_ents = state->mp->m_dir_node_ents;
1834 /*
1835 * Look up the entry we're deleting, set up the cursor.
1836 */
1837 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * Didn't find it, upper layer screwed up.
1842 */
1843 if (rval != EEXIST) {
1844 xfs_da_state_free(state);
1845 return rval;
1846 }
1847 blk = &state->path.blk[state->path.active - 1];
1848 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1849 ASSERT(state->extravalid);
1850 /*
1851 * Remove the leaf and data entries.
1852 * Extrablk refers to the data block.
1853 */
1854 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1855 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001856 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /*
1859 * Fix the hash values up the btree.
1860 */
1861 xfs_da_fixhashpath(state, &state->path);
1862 /*
1863 * If we need to join leaf blocks, do it.
1864 */
1865 if (rval && state->path.active > 1)
1866 error = xfs_da_join(state);
1867 /*
1868 * If no errors so far, try conversion to leaf format.
1869 */
1870 if (!error)
1871 error = xfs_dir2_node_to_leaf(state);
1872 xfs_da_state_free(state);
1873 return error;
1874}
1875
1876/*
1877 * Replace an entry's inode number in a node-format directory.
1878 */
1879int /* error */
1880xfs_dir2_node_replace(
1881 xfs_da_args_t *args) /* operation arguments */
1882{
1883 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001884 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 xfs_dir2_data_entry_t *dep; /* data entry changed */
1886 int error; /* error return value */
1887 int i; /* btree level */
1888 xfs_ino_t inum; /* new inode number */
1889 xfs_dir2_leaf_t *leaf; /* leaf structure */
1890 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1891 int rval; /* internal return value */
1892 xfs_da_state_t *state; /* btree cursor */
1893
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001894 trace_xfs_dir2_node_replace(args);
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /*
1897 * Allocate and initialize the btree cursor.
1898 */
1899 state = xfs_da_state_alloc();
1900 state->args = args;
1901 state->mp = args->dp->i_mount;
1902 state->blocksize = state->mp->m_dirblksize;
1903 state->node_ents = state->mp->m_dir_node_ents;
1904 inum = args->inumber;
1905 /*
1906 * Lookup the entry to change in the btree.
1907 */
1908 error = xfs_da_node_lookup_int(state, &rval);
1909 if (error) {
1910 rval = error;
1911 }
1912 /*
1913 * It should be found, since the vnodeops layer has looked it up
1914 * and locked it. But paranoia is good.
1915 */
1916 if (rval == EEXIST) {
1917 /*
1918 * Find the leaf entry.
1919 */
1920 blk = &state->path.blk[state->path.active - 1];
1921 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001922 leaf = blk->bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 lep = &leaf->ents[blk->index];
1924 ASSERT(state->extravalid);
1925 /*
1926 * Point to the data entry.
1927 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001928 hdr = state->extrablk.bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001929 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001931 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001932 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001933 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /*
1935 * Fill in the new inode number and log the entry.
1936 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001937 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1939 rval = 0;
1940 }
1941 /*
1942 * Didn't find it, and we're holding a data block. Drop it.
1943 */
1944 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001945 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 state->extrablk.bp = NULL;
1947 }
1948 /*
1949 * Release all the buffers in the cursor.
1950 */
1951 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001952 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 state->path.blk[i].bp = NULL;
1954 }
1955 xfs_da_state_free(state);
1956 return rval;
1957}
1958
1959/*
1960 * Trim off a trailing empty freespace block.
1961 * Return (in rvalp) 1 if we did it, 0 if not.
1962 */
1963int /* error */
1964xfs_dir2_node_trim_free(
1965 xfs_da_args_t *args, /* operation arguments */
1966 xfs_fileoff_t fo, /* free block number */
1967 int *rvalp) /* out: did something */
1968{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001969 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 xfs_inode_t *dp; /* incore directory inode */
1971 int error; /* error return code */
1972 xfs_dir2_free_t *free; /* freespace structure */
1973 xfs_mount_t *mp; /* filesystem mount point */
1974 xfs_trans_t *tp; /* transaction pointer */
1975
1976 dp = args->dp;
1977 mp = dp->i_mount;
1978 tp = args->trans;
1979 /*
1980 * Read the freespace block.
1981 */
Dave Chinner20252072012-11-12 22:54:13 +11001982 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001983 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * There can be holes in freespace. If fo is a hole, there's
1987 * nothing to do.
1988 */
Dave Chinner20252072012-11-12 22:54:13 +11001989 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001991 free = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001992 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
1994 * If there are used entries, there's nothing to do.
1995 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001996 if (be32_to_cpu(free->hdr.nused) > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001997 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 *rvalp = 0;
1999 return 0;
2000 }
2001 /*
2002 * Blow the block away.
2003 */
2004 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002005 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 bp))) {
2007 /*
2008 * Can't fail with ENOSPC since that only happens with no
2009 * space reservation, when breaking up an extent into two
2010 * pieces. This is the last block of an extent.
2011 */
2012 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002013 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return error;
2015 }
2016 /*
2017 * Return that we succeeded.
2018 */
2019 *rvalp = 1;
2020 return 0;
2021}