blob: 14571cdeb33fc88c6cf98af913644ef0af27c8d7 [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"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dir2_data.h"
35#include "xfs_dir2_leaf.h"
36#include "xfs_dir2_block.h"
37#include "xfs_dir2_node.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000039#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Function declarations.
43 */
44static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
45static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
46#ifdef DEBUG
47static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
48#else
49#define xfs_dir2_leafn_check(dp, bp)
50#endif
51static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
52 int start_s, xfs_dabuf_t *bp_d, int start_d,
53 int count);
54static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
55 xfs_da_state_blk_t *blk1,
56 xfs_da_state_blk_t *blk2);
57static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
58 int index, xfs_da_state_blk_t *dblk,
59 int *rval);
60static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
61 xfs_da_state_blk_t *fblk);
62
63/*
64 * Log entries from a freespace block.
65 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +000066STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -070067xfs_dir2_free_log_bests(
68 xfs_trans_t *tp, /* transaction pointer */
69 xfs_dabuf_t *bp, /* freespace buffer */
70 int first, /* first entry to log */
71 int last) /* last entry to log */
72{
73 xfs_dir2_free_t *free; /* freespace structure */
74
75 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020076 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 xfs_da_log_buf(tp, bp,
78 (uint)((char *)&free->bests[first] - (char *)free),
79 (uint)((char *)&free->bests[last] - (char *)free +
80 sizeof(free->bests[0]) - 1));
81}
82
83/*
84 * Log header from a freespace block.
85 */
86static void
87xfs_dir2_free_log_header(
88 xfs_trans_t *tp, /* transaction pointer */
89 xfs_dabuf_t *bp) /* freespace buffer */
90{
91 xfs_dir2_free_t *free; /* freespace structure */
92
93 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +020094 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
96 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
97}
98
99/*
100 * Convert a leaf-format directory to a node-format directory.
101 * We need to change the magic number of the leaf block, and copy
102 * the freespace table out of the leaf block into its own block.
103 */
104int /* error */
105xfs_dir2_leaf_to_node(
106 xfs_da_args_t *args, /* operation arguments */
107 xfs_dabuf_t *lbp) /* leaf buffer */
108{
109 xfs_inode_t *dp; /* incore directory inode */
110 int error; /* error return value */
111 xfs_dabuf_t *fbp; /* freespace buffer */
112 xfs_dir2_db_t fdb; /* freespace block number */
113 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100114 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int i; /* leaf freespace index */
116 xfs_dir2_leaf_t *leaf; /* leaf structure */
117 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
118 xfs_mount_t *mp; /* filesystem mount point */
119 int n; /* count of live freespc ents */
120 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100121 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 xfs_trans_t *tp; /* transaction pointer */
123
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000124 trace_xfs_dir2_leaf_to_node(args);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dp = args->dp;
127 mp = dp->i_mount;
128 tp = args->trans;
129 /*
130 * Add a freespace block to the directory.
131 */
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
133 return error;
134 }
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
136 /*
137 * Get the buffer for the new freespace block.
138 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000139 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 XFS_DATA_FORK))) {
141 return error;
142 }
143 ASSERT(fbp != NULL);
144 free = fbp->data;
145 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000146 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /*
148 * Initialize the freespace block header.
149 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100153 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /*
155 * Copy freespace entries from the leaf block to the new block.
156 * Count active entries.
157 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000158 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100162 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100164 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Log everything.
168 */
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
174 return 0;
175}
176
177/*
178 * Add a leaf entry to a leaf block in a node-form directory.
179 * The other work necessary is done from the caller.
180 */
181static int /* error */
182xfs_dir2_leafn_add(
183 xfs_dabuf_t *bp, /* leaf buffer */
184 xfs_da_args_t *args, /* operation arguments */
185 int index) /* insertion pt for new entry */
186{
187 int compact; /* compacting stale leaves */
188 xfs_inode_t *dp; /* incore directory inode */
189 int highstale; /* next stale entry */
190 xfs_dir2_leaf_t *leaf; /* leaf structure */
191 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
192 int lfloghigh; /* high leaf entry logging */
193 int lfloglow; /* low leaf entry logging */
194 int lowstale; /* previous stale entry */
195 xfs_mount_t *mp; /* filesystem mount point */
196 xfs_trans_t *tp; /* transaction pointer */
197
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000198 trace_xfs_dir2_leafn_add(args, index);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dp = args->dp;
201 mp = dp->i_mount;
202 tp = args->trans;
203 leaf = bp->data;
204
205 /*
206 * Quick check just to make sure we are not going to index
207 * into other peoples memory
208 */
209 if (index < 0)
210 return XFS_ERROR(EFSCORRUPTED);
211
212 /*
213 * If there are already the maximum number of leaf entries in
214 * the block, if there are no stale entries it won't fit.
215 * Caller will do a split. If there are stale entries we'll do
216 * a compact.
217 */
218
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000219 if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!leaf->hdr.stale)
221 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100222 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else
224 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100225 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100226 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100227 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Barry Naujok6a178102008-05-21 16:42:05 +1000229 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231
232 /*
233 * Compact out all but one stale leaf entry. Leaves behind
234 * the entry closest to index.
235 */
236 if (compact) {
237 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
238 &lfloglow, &lfloghigh);
239 }
240 /*
241 * Set impossible logging indices for this case.
242 */
243 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100244 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 lfloghigh = -1;
246 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /*
249 * Insert the new entry, log everything.
250 */
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200251 lep = xfs_dir2_leaf_find_entry(leaf, index, compact, lowstale,
252 highstale, &lfloglow, &lfloghigh);
253
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100254 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000255 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100256 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 xfs_dir2_leaf_log_header(tp, bp);
258 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
259 xfs_dir2_leafn_check(dp, bp);
260 return 0;
261}
262
263#ifdef DEBUG
264/*
265 * Check internal consistency of a leafn block.
266 */
267void
268xfs_dir2_leafn_check(
269 xfs_inode_t *dp, /* incore directory inode */
270 xfs_dabuf_t *bp) /* leaf buffer */
271{
272 int i; /* leaf index */
273 xfs_dir2_leaf_t *leaf; /* leaf structure */
274 xfs_mount_t *mp; /* filesystem mount point */
275 int stale; /* count of stale leaves */
276
277 leaf = bp->data;
278 mp = dp->i_mount;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200279 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000280 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100281 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
282 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100283 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
284 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200286 if (leaf->ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 stale++;
288 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100289 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291#endif /* DEBUG */
292
293/*
294 * Return the last hash value in the leaf.
295 * Stale entries are ok.
296 */
297xfs_dahash_t /* hash value */
298xfs_dir2_leafn_lasthash(
299 xfs_dabuf_t *bp, /* leaf buffer */
300 int *count) /* count of entries in leaf */
301{
302 xfs_dir2_leaf_t *leaf; /* leaf structure */
303
304 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200305 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100307 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!leaf->hdr.count)
309 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100310 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000314 * Look up a leaf entry for space to add a name in a node-format leaf block.
315 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000317STATIC int
318xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 xfs_dabuf_t *bp, /* leaf buffer */
320 xfs_da_args_t *args, /* operation arguments */
321 int *indexp, /* out: leaf entry index */
322 xfs_da_state_t *state) /* state to fill in */
323{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000324 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
325 xfs_dir2_db_t curdb = -1; /* current data block number */
326 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_inode_t *dp; /* incore directory inode */
328 int error; /* error return value */
329 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000330 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int index; /* leaf entry index */
332 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000333 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
335 xfs_mount_t *mp; /* filesystem mount point */
336 xfs_dir2_db_t newdb; /* new data block number */
337 xfs_dir2_db_t newfdb; /* new free block number */
338 xfs_trans_t *tp; /* transaction pointer */
339
340 dp = args->dp;
341 tp = args->trans;
342 mp = dp->i_mount;
343 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200344 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100346 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#endif
348 xfs_dir2_leafn_check(dp, bp);
349 /*
350 * Look up the hash value in the leaf entries.
351 */
352 index = xfs_dir2_leaf_search_hash(args, bp);
353 /*
354 * Do we have a buffer coming in?
355 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000356 if (state->extravalid) {
357 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000359 curfdb = state->extrablk.blkno;
360 free = curbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200361 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000363 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Loop over leaf entries with the right hash value.
366 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000367 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
368 be32_to_cpu(lep->hashval) == args->hashval;
369 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Skip stale leaf entries.
372 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100373 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 continue;
375 /*
376 * Pull the data block number from the entry.
377 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000378 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /*
380 * For addname, we're looking for a place to put the new entry.
381 * We want to use a data block with an entry of equal
382 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000383 *
384 * If this block isn't the data block we already have
385 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000387 if (newdb != curdb) {
388 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000390 * Convert the data block to the free block
391 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000393 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000395 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000397 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000399 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 */
401 if (curbp)
402 xfs_da_brelse(tp, curbp);
403 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000404 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 error = xfs_da_read_buf(tp, dp,
407 xfs_dir2_db_to_da(mp, newfdb),
408 -1, &curbp, XFS_DATA_FORK);
409 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000411 free = curbp->data;
412 ASSERT(be32_to_cpu(free->hdr.magic) ==
413 XFS_DIR2_FREE_MAGIC);
414 ASSERT((be32_to_cpu(free->hdr.firstdb) %
415 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
416 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
417 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
418 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000421 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000423 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000425 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200427 if (unlikely(free->bests[fi] ==
428 cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000429 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
430 XFS_ERRLEVEL_LOW, mp);
431 if (curfdb != newfdb)
432 xfs_da_brelse(tp, curbp);
433 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000435 curfdb = newfdb;
436 if (be16_to_cpu(free->bests[fi]) >= length)
437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000440 /* Didn't find any space */
441 fi = -1;
442out:
Barry Naujok6a178102008-05-21 16:42:05 +1000443 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000444 if (curbp) {
445 /* Giving back a free block. */
446 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000448 state->extrablk.index = fi;
449 state->extrablk.blkno = curfdb;
450 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
451 } else {
452 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000455 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 */
457 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return XFS_ERROR(ENOENT);
459}
460
461/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000462 * Look up a leaf entry in a node-format leaf block.
463 * The extrablk in state a data block.
464 */
465STATIC int
466xfs_dir2_leafn_lookup_for_entry(
467 xfs_dabuf_t *bp, /* leaf buffer */
468 xfs_da_args_t *args, /* operation arguments */
469 int *indexp, /* out: leaf entry index */
470 xfs_da_state_t *state) /* state to fill in */
471{
472 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
473 xfs_dir2_db_t curdb = -1; /* current data block number */
474 xfs_dir2_data_entry_t *dep; /* data block entry */
475 xfs_inode_t *dp; /* incore directory inode */
476 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000477 int index; /* leaf entry index */
478 xfs_dir2_leaf_t *leaf; /* leaf structure */
479 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
480 xfs_mount_t *mp; /* filesystem mount point */
481 xfs_dir2_db_t newdb; /* new data block number */
482 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000483 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000484
485 dp = args->dp;
486 tp = args->trans;
487 mp = dp->i_mount;
488 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200489 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000490#ifdef __KERNEL__
491 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
492#endif
493 xfs_dir2_leafn_check(dp, bp);
494 /*
495 * Look up the hash value in the leaf entries.
496 */
497 index = xfs_dir2_leaf_search_hash(args, bp);
498 /*
499 * Do we have a buffer coming in?
500 */
501 if (state->extravalid) {
502 curbp = state->extrablk.bp;
503 curdb = state->extrablk.blkno;
504 }
505 /*
506 * Loop over leaf entries with the right hash value.
507 */
508 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
509 be32_to_cpu(lep->hashval) == args->hashval;
510 lep++, index++) {
511 /*
512 * Skip stale leaf entries.
513 */
514 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
515 continue;
516 /*
517 * Pull the data block number from the entry.
518 */
519 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
520 /*
521 * Not adding a new entry, so we really want to find
522 * the name given to us.
523 *
524 * If it's a different data block, go get it.
525 */
526 if (newdb != curdb) {
527 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000528 * If we had a block before that we aren't saving
529 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000530 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000531 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
532 curdb != state->extrablk.blkno))
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000533 xfs_da_brelse(tp, curbp);
534 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000535 * If needing the block that is saved with a CI match,
536 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000537 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000538 if (args->cmpresult != XFS_CMP_DIFFERENT &&
539 newdb == state->extrablk.blkno) {
540 ASSERT(state->extravalid);
541 curbp = state->extrablk.bp;
542 } else {
543 error = xfs_da_read_buf(tp, dp,
544 xfs_dir2_db_to_da(mp, newdb),
545 -1, &curbp, XFS_DATA_FORK);
546 if (error)
547 return error;
548 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000549 xfs_dir2_data_check(dp, curbp);
550 curdb = newdb;
551 }
552 /*
553 * Point to the data entry.
554 */
555 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
556 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
557 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000558 * Compare the entry and if it's an exact match, return
559 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000560 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000561 */
Barry Naujok5163f952008-05-21 16:41:01 +1000562 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
563 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000564 /* If there is a CI match block, drop it */
565 if (args->cmpresult != XFS_CMP_DIFFERENT &&
566 curdb != state->extrablk.blkno)
567 xfs_da_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000568 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000569 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000570 *indexp = index;
571 state->extravalid = 1;
572 state->extrablk.bp = curbp;
573 state->extrablk.blkno = curdb;
574 state->extrablk.index = (int)((char *)dep -
575 (char *)curbp->data);
576 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000577 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000578 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000579 }
580 }
Barry Naujok6a178102008-05-21 16:42:05 +1000581 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
582 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000583 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000584 if (args->cmpresult == XFS_CMP_DIFFERENT) {
585 /* Giving back last used data block. */
586 state->extravalid = 1;
587 state->extrablk.bp = curbp;
588 state->extrablk.index = -1;
589 state->extrablk.blkno = curdb;
590 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
591 } else {
592 /* If the curbp is not the CI match block, drop it */
593 if (state->extrablk.bp != curbp)
594 xfs_da_brelse(tp, curbp);
595 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000596 } else {
597 state->extravalid = 0;
598 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000600 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000601}
602
603/*
604 * Look up a leaf entry in a node-format leaf block.
605 * If this is an addname then the extrablk in state is a freespace block,
606 * otherwise it's a data block.
607 */
608int
609xfs_dir2_leafn_lookup_int(
610 xfs_dabuf_t *bp, /* leaf buffer */
611 xfs_da_args_t *args, /* operation arguments */
612 int *indexp, /* out: leaf entry index */
613 xfs_da_state_t *state) /* state to fill in */
614{
Barry Naujok6a178102008-05-21 16:42:05 +1000615 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000616 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
617 state);
618 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
619}
620
621/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * Move count leaf entries from source to destination leaf.
623 * Log entries and headers. Stale entries are preserved.
624 */
625static void
626xfs_dir2_leafn_moveents(
627 xfs_da_args_t *args, /* operation arguments */
628 xfs_dabuf_t *bp_s, /* source leaf buffer */
629 int start_s, /* source leaf index */
630 xfs_dabuf_t *bp_d, /* destination leaf buffer */
631 int start_d, /* destination leaf index */
632 int count) /* count of leaves to copy */
633{
634 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
635 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
636 int stale; /* count stale leaves copied */
637 xfs_trans_t *tp; /* transaction pointer */
638
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000639 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Silently return if nothing to do.
643 */
644 if (count == 0) {
645 return;
646 }
647 tp = args->trans;
648 leaf_s = bp_s->data;
649 leaf_d = bp_d->data;
650 /*
651 * If the destination index is not the end of the current
652 * destination leaf entries, open up a hole in the destination
653 * to hold the new entries.
654 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100655 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100657 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 sizeof(xfs_dir2_leaf_entry_t));
659 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100660 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662 /*
663 * If the source has stale leaves, count the ones in the copy range
664 * so we can update the header correctly.
665 */
666 if (leaf_s->hdr.stale) {
667 int i; /* temp leaf index */
668
669 for (i = start_s, stale = 0; i < start_s + count; i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200670 if (leaf_s->ents[i].address ==
671 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 stale++;
673 }
674 } else
675 stale = 0;
676 /*
677 * Copy the leaf entries from source to destination.
678 */
679 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
680 count * sizeof(xfs_dir2_leaf_entry_t));
681 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
682 /*
683 * If there are source entries after the ones we copied,
684 * delete the ones we copied by sliding the next ones down.
685 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100686 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
688 count * sizeof(xfs_dir2_leaf_entry_t));
689 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
690 }
691 /*
692 * Update the headers and log them.
693 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800694 be16_add_cpu(&leaf_s->hdr.count, -(count));
695 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
696 be16_add_cpu(&leaf_d->hdr.count, count);
697 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 xfs_dir2_leaf_log_header(tp, bp_s);
699 xfs_dir2_leaf_log_header(tp, bp_d);
700 xfs_dir2_leafn_check(args->dp, bp_s);
701 xfs_dir2_leafn_check(args->dp, bp_d);
702}
703
704/*
705 * Determine the sort order of two leaf blocks.
706 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
707 */
708int /* sort order */
709xfs_dir2_leafn_order(
710 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
711 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
712{
713 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
714 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
715
716 leaf1 = leaf1_bp->data;
717 leaf2 = leaf2_bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200718 ASSERT(leaf1->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
719 ASSERT(leaf2->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100720 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
721 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100722 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
723 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
724 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 1;
726 return 0;
727}
728
729/*
730 * Rebalance leaf entries between two leaf blocks.
731 * This is actually only called when the second block is new,
732 * though the code deals with the general case.
733 * A new entry will be inserted in one of the blocks, and that
734 * entry is taken into account when balancing.
735 */
736static void
737xfs_dir2_leafn_rebalance(
738 xfs_da_state_t *state, /* btree cursor */
739 xfs_da_state_blk_t *blk1, /* first btree block */
740 xfs_da_state_blk_t *blk2) /* second btree block */
741{
742 xfs_da_args_t *args; /* operation arguments */
743 int count; /* count (& direction) leaves */
744 int isleft; /* new goes in left leaf */
745 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
746 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
747 int mid; /* midpoint leaf index */
748#ifdef DEBUG
749 int oldstale; /* old count of stale leaves */
750#endif
751 int oldsum; /* old total leaf count */
752 int swap; /* swapped leaf blocks */
753
754 args = state->args;
755 /*
756 * If the block order is wrong, swap the arguments.
757 */
758 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
759 xfs_da_state_blk_t *tmp; /* temp for block swap */
760
761 tmp = blk1;
762 blk1 = blk2;
763 blk2 = tmp;
764 }
765 leaf1 = blk1->bp->data;
766 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100767 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100769 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770#endif
771 mid = oldsum >> 1;
772 /*
773 * If the old leaf count was odd then the new one will be even,
774 * so we need to divide the new count evenly.
775 */
776 if (oldsum & 1) {
777 xfs_dahash_t midhash; /* middle entry hash value */
778
Nathan Scotta818e5d2006-03-17 17:28:07 +1100779 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100780 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100782 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 isleft = args->hashval <= midhash;
784 }
785 /*
786 * If the old count is even then the new count is odd, so there's
787 * no preferred side for the new entry.
788 * Pick the left one.
789 */
790 else
791 isleft = 1;
792 /*
793 * Calculate moved entry count. Positive means left-to-right,
794 * negative means right-to-left. Then move the entries.
795 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100796 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (count > 0)
798 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100799 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 else if (count < 0)
801 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100802 be16_to_cpu(leaf1->hdr.count), count);
803 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
804 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /*
806 * Mark whether we're inserting into the old or new leaf.
807 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100808 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100810 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 state->inleaf = !swap;
812 else
813 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100814 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /*
816 * Adjust the expected index for insertion.
817 */
818 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100819 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000820
821 /*
822 * Finally sanity check just to make sure we are not returning a
823 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
825 if(blk2->index < 0) {
826 state->inleaf = 1;
827 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100828 xfs_alert(args->dp->i_mount,
829 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
830 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832}
833
834/*
835 * Remove an entry from a node directory.
836 * This removes the leaf entry and the data entry,
837 * and updates the free block if necessary.
838 */
839static int /* error */
840xfs_dir2_leafn_remove(
841 xfs_da_args_t *args, /* operation arguments */
842 xfs_dabuf_t *bp, /* leaf buffer */
843 int index, /* leaf entry index */
844 xfs_da_state_blk_t *dblk, /* data block */
845 int *rval) /* resulting block needs join */
846{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200847 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 xfs_dir2_db_t db; /* data block number */
849 xfs_dabuf_t *dbp; /* data block buffer */
850 xfs_dir2_data_entry_t *dep; /* data block entry */
851 xfs_inode_t *dp; /* incore directory inode */
852 xfs_dir2_leaf_t *leaf; /* leaf structure */
853 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
854 int longest; /* longest data free entry */
855 int off; /* data block entry offset */
856 xfs_mount_t *mp; /* filesystem mount point */
857 int needlog; /* need to log data header */
858 int needscan; /* need to rescan data frees */
859 xfs_trans_t *tp; /* transaction pointer */
860
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000861 trace_xfs_dir2_leafn_remove(args, index);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 dp = args->dp;
864 tp = args->trans;
865 mp = dp->i_mount;
866 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200867 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /*
869 * Point to the entry we're removing.
870 */
871 lep = &leaf->ents[index];
872 /*
873 * Extract the data block and offset from the entry.
874 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000875 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000877 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ASSERT(dblk->index == off);
879 /*
880 * Kill the leaf entry by marking it stale.
881 * Log the leaf block changes.
882 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800883 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100885 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 xfs_dir2_leaf_log_ents(tp, bp, index, index);
887 /*
888 * Make the data entry free. Keep track of the longest freespace
889 * in the data block in case it changes.
890 */
891 dbp = dblk->bp;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200892 hdr = dbp->data;
893 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
894 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 needlog = needscan = 0;
896 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000897 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * Rescan the data block freespaces for bestfree.
900 * Log the data block header if needed.
901 */
902 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200903 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (needlog)
905 xfs_dir2_data_log_header(tp, dbp);
906 xfs_dir2_data_check(dp, dbp);
907 /*
908 * If the longest data block freespace changes, need to update
909 * the corresponding freeblock entry.
910 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200911 if (longest < be16_to_cpu(hdr->bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int error; /* error return value */
913 xfs_dabuf_t *fbp; /* freeblock buffer */
914 xfs_dir2_db_t fdb; /* freeblock block number */
915 int findex; /* index in freeblock entries */
916 xfs_dir2_free_t *free; /* freeblock structure */
917 int logfree; /* need to log free entry */
918
919 /*
920 * Convert the data block number to a free block,
921 * read in the free block.
922 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000923 fdb = xfs_dir2_db_to_fdb(mp, db);
924 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 -1, &fbp, XFS_DATA_FORK))) {
926 return error;
927 }
928 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200929 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Nathan Scott0ba962e2006-03-17 17:27:07 +1100930 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 XFS_DIR2_MAX_FREE_BESTS(mp) *
932 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
933 /*
934 * Calculate which entry we need to fix.
935 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000936 findex = xfs_dir2_db_to_fdindex(mp, db);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200937 longest = be16_to_cpu(hdr->bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * If the data block is now empty we can get rid of it
940 * (usually).
941 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200942 if (longest == mp->m_dirblksize - (uint)sizeof(*hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * Try to punch out the data block.
945 */
946 error = xfs_dir2_shrink_inode(args, db, dbp);
947 if (error == 0) {
948 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200949 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 /*
952 * We can get ENOSPC if there's no space reservation.
953 * In this case just drop the buffer and some one else
954 * will eventually get rid of the empty block.
955 */
956 else if (error == ENOSPC && args->total == 0)
957 xfs_da_buf_done(dbp);
958 else
959 return error;
960 }
961 /*
962 * If we got rid of the data block, we can eliminate that entry
963 * in the free block.
964 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200965 if (hdr == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /*
967 * One less used entry in the free table.
968 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800969 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xfs_dir2_free_log_header(tp, fbp);
971 /*
972 * If this was the last entry in the table, we can
973 * trim the table size back. There might be other
974 * entries at the end referring to non-existent
975 * data blocks, get those too.
976 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100977 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int i; /* free entry index */
979
980 for (i = findex - 1;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200981 i >= 0 &&
982 free->bests[i] == cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 i--)
984 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100985 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 logfree = 0;
987 }
988 /*
989 * Not the last entry, just punch it out.
990 */
991 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100992 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 logfree = 1;
994 }
995 /*
996 * If there are no useful entries left in the block,
997 * get rid of the block if we can.
998 */
999 if (!free->hdr.nused) {
1000 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1001 if (error == 0) {
1002 fbp = NULL;
1003 logfree = 0;
1004 } else if (error != ENOSPC || args->total != 0)
1005 return error;
1006 /*
1007 * It's possible to get ENOSPC if there is no
1008 * space reservation. In this case some one
1009 * else will eventually get rid of this block.
1010 */
1011 }
1012 }
1013 /*
1014 * Data block is not empty, just set the free entry to
1015 * the new value.
1016 */
1017 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001018 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 logfree = 1;
1020 }
1021 /*
1022 * Log the free entry that changed, unless we got rid of it.
1023 */
1024 if (logfree)
1025 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1026 /*
1027 * Drop the buffer if we still have it.
1028 */
1029 if (fbp)
1030 xfs_da_buf_done(fbp);
1031 }
1032 xfs_dir2_leafn_check(dp, bp);
1033 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001034 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * to justify trying to join it with a neighbor.
1036 */
1037 *rval =
1038 ((uint)sizeof(leaf->hdr) +
1039 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001040 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mp->m_dir_magicpct;
1042 return 0;
1043}
1044
1045/*
1046 * Split the leaf entries in the old block into old and new blocks.
1047 */
1048int /* error */
1049xfs_dir2_leafn_split(
1050 xfs_da_state_t *state, /* btree cursor */
1051 xfs_da_state_blk_t *oldblk, /* original block */
1052 xfs_da_state_blk_t *newblk) /* newly created block */
1053{
1054 xfs_da_args_t *args; /* operation arguments */
1055 xfs_dablk_t blkno; /* new leaf block number */
1056 int error; /* error return value */
1057 xfs_mount_t *mp; /* filesystem mount point */
1058
1059 /*
1060 * Allocate space for a new leaf node.
1061 */
1062 args = state->args;
1063 mp = args->dp->i_mount;
1064 ASSERT(args != NULL);
1065 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1066 error = xfs_da_grow_inode(args, &blkno);
1067 if (error) {
1068 return error;
1069 }
1070 /*
1071 * Initialize the new leaf block.
1072 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001073 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1075 if (error) {
1076 return error;
1077 }
1078 newblk->blkno = blkno;
1079 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1080 /*
1081 * Rebalance the entries across the two leaves, link the new
1082 * block into the leaves.
1083 */
1084 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1085 error = xfs_da_blk_link(state, oldblk, newblk);
1086 if (error) {
1087 return error;
1088 }
1089 /*
1090 * Insert the new entry in the correct block.
1091 */
1092 if (state->inleaf)
1093 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1094 else
1095 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1096 /*
1097 * Update last hashval in each block since we added the name.
1098 */
1099 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1100 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1101 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1102 xfs_dir2_leafn_check(args->dp, newblk->bp);
1103 return error;
1104}
1105
1106/*
1107 * Check a leaf block and its neighbors to see if the block should be
1108 * collapsed into one or the other neighbor. Always keep the block
1109 * with the smaller block number.
1110 * If the current block is over 50% full, don't try to join it, return 0.
1111 * If the block is empty, fill in the state structure and return 2.
1112 * If it can be collapsed, fill in the state structure and return 1.
1113 * If nothing can be done, return 0.
1114 */
1115int /* error */
1116xfs_dir2_leafn_toosmall(
1117 xfs_da_state_t *state, /* btree cursor */
1118 int *action) /* resulting action to take */
1119{
1120 xfs_da_state_blk_t *blk; /* leaf block */
1121 xfs_dablk_t blkno; /* leaf block number */
1122 xfs_dabuf_t *bp; /* leaf buffer */
1123 int bytes; /* bytes in use */
1124 int count; /* leaf live entry count */
1125 int error; /* error return value */
1126 int forward; /* sibling block direction */
1127 int i; /* sibling counter */
1128 xfs_da_blkinfo_t *info; /* leaf block header */
1129 xfs_dir2_leaf_t *leaf; /* leaf structure */
1130 int rval; /* result from path_shift */
1131
1132 /*
1133 * Check for the degenerate case of the block being over 50% full.
1134 * If so, it's not worth even looking to see if we might be able
1135 * to coalesce with a sibling.
1136 */
1137 blk = &state->path.blk[state->path.active - 1];
1138 info = blk->bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001139 ASSERT(info->magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001141 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1143 if (bytes > (state->blocksize >> 1)) {
1144 /*
1145 * Blk over 50%, don't try to join.
1146 */
1147 *action = 0;
1148 return 0;
1149 }
1150 /*
1151 * Check for the degenerate case of the block being empty.
1152 * If the block is empty, we'll simply delete it, no need to
1153 * coalesce it with a sibling block. We choose (arbitrarily)
1154 * to merge with the forward block unless it is NULL.
1155 */
1156 if (count == 0) {
1157 /*
1158 * Make altpath point to the block we want to keep and
1159 * path point to the block we want to drop (this one).
1160 */
Nathan Scott89da0542006-03-17 17:28:40 +11001161 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 memcpy(&state->altpath, &state->path, sizeof(state->path));
1163 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1164 &rval);
1165 if (error)
1166 return error;
1167 *action = rval ? 2 : 0;
1168 return 0;
1169 }
1170 /*
1171 * Examine each sibling block to see if we can coalesce with
1172 * at least 25% free space to spare. We need to figure out
1173 * whether to merge with the forward or the backward block.
1174 * We prefer coalescing with the lower numbered sibling so as
1175 * to shrink a directory over time.
1176 */
Nathan Scott89da0542006-03-17 17:28:40 +11001177 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001179 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (blkno == 0)
1181 continue;
1182 /*
1183 * Read the sibling leaf block.
1184 */
1185 if ((error =
1186 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1187 -1, &bp, XFS_DATA_FORK))) {
1188 return error;
1189 }
1190 ASSERT(bp != NULL);
1191 /*
1192 * Count bytes in the two blocks combined.
1193 */
1194 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001195 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 bytes = state->blocksize - (state->blocksize >> 2);
1197 leaf = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001198 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Nathan Scotta818e5d2006-03-17 17:28:07 +11001199 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bytes -= count * (uint)sizeof(leaf->ents[0]);
1201 /*
1202 * Fits with at least 25% to spare.
1203 */
1204 if (bytes >= 0)
1205 break;
1206 xfs_da_brelse(state->args->trans, bp);
1207 }
1208 /*
1209 * Didn't like either block, give up.
1210 */
1211 if (i >= 2) {
1212 *action = 0;
1213 return 0;
1214 }
1215 /*
1216 * Done with the sibling leaf block here, drop the dabuf
1217 * so path_shift can get it.
1218 */
1219 xfs_da_buf_done(bp);
1220 /*
1221 * Make altpath point to the block we want to keep (the lower
1222 * numbered block) and path point to the block we want to drop.
1223 */
1224 memcpy(&state->altpath, &state->path, sizeof(state->path));
1225 if (blkno < blk->blkno)
1226 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1227 &rval);
1228 else
1229 error = xfs_da_path_shift(state, &state->path, forward, 0,
1230 &rval);
1231 if (error) {
1232 return error;
1233 }
1234 *action = rval ? 0 : 1;
1235 return 0;
1236}
1237
1238/*
1239 * Move all the leaf entries from drop_blk to save_blk.
1240 * This is done as part of a join operation.
1241 */
1242void
1243xfs_dir2_leafn_unbalance(
1244 xfs_da_state_t *state, /* cursor */
1245 xfs_da_state_blk_t *drop_blk, /* dead block */
1246 xfs_da_state_blk_t *save_blk) /* surviving block */
1247{
1248 xfs_da_args_t *args; /* operation arguments */
1249 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1250 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1251
1252 args = state->args;
1253 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1254 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1255 drop_leaf = drop_blk->bp->data;
1256 save_leaf = save_blk->bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001257 ASSERT(drop_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
1258 ASSERT(save_leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * If there are any stale leaf entries, take this opportunity
1261 * to purge them.
1262 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001263 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001265 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 xfs_dir2_leaf_compact(args, save_blk->bp);
1267 /*
1268 * Move the entries from drop to the appropriate end of save.
1269 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001270 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 -07001271 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1272 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001273 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 else
1275 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001276 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001277 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 -07001278 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1279}
1280
1281/*
1282 * Top-level node form directory addname routine.
1283 */
1284int /* error */
1285xfs_dir2_node_addname(
1286 xfs_da_args_t *args) /* operation arguments */
1287{
1288 xfs_da_state_blk_t *blk; /* leaf block for insert */
1289 int error; /* error return value */
1290 int rval; /* sub-return value */
1291 xfs_da_state_t *state; /* btree cursor */
1292
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001293 trace_xfs_dir2_node_addname(args);
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /*
1296 * Allocate and initialize the state (btree cursor).
1297 */
1298 state = xfs_da_state_alloc();
1299 state->args = args;
1300 state->mp = args->dp->i_mount;
1301 state->blocksize = state->mp->m_dirblksize;
1302 state->node_ents = state->mp->m_dir_node_ents;
1303 /*
1304 * Look up the name. We're not supposed to find it, but
1305 * this gives us the insertion point.
1306 */
1307 error = xfs_da_node_lookup_int(state, &rval);
1308 if (error)
1309 rval = error;
1310 if (rval != ENOENT) {
1311 goto done;
1312 }
1313 /*
1314 * Add the data entry to a data block.
1315 * Extravalid is set to a freeblock found by lookup.
1316 */
1317 rval = xfs_dir2_node_addname_int(args,
1318 state->extravalid ? &state->extrablk : NULL);
1319 if (rval) {
1320 goto done;
1321 }
1322 blk = &state->path.blk[state->path.active - 1];
1323 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1324 /*
1325 * Add the new leaf entry.
1326 */
1327 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1328 if (rval == 0) {
1329 /*
1330 * It worked, fix the hash values up the btree.
1331 */
Barry Naujok6a178102008-05-21 16:42:05 +10001332 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 xfs_da_fixhashpath(state, &state->path);
1334 } else {
1335 /*
1336 * It didn't work, we need to split the leaf block.
1337 */
1338 if (args->total == 0) {
1339 ASSERT(rval == ENOSPC);
1340 goto done;
1341 }
1342 /*
1343 * Split the leaf block and insert the new entry.
1344 */
1345 rval = xfs_da_split(state);
1346 }
1347done:
1348 xfs_da_state_free(state);
1349 return rval;
1350}
1351
1352/*
1353 * Add the data entry for a node-format directory name addition.
1354 * The leaf entry is added in xfs_dir2_leafn_add.
1355 * We may enter with a freespace block that the lookup found.
1356 */
1357static int /* error */
1358xfs_dir2_node_addname_int(
1359 xfs_da_args_t *args, /* operation arguments */
1360 xfs_da_state_blk_t *fblk) /* optional freespace block */
1361{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001362 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 xfs_dir2_db_t dbno; /* data block number */
1364 xfs_dabuf_t *dbp; /* data block buffer */
1365 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1366 xfs_inode_t *dp; /* incore directory inode */
1367 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1368 int error; /* error return value */
1369 xfs_dir2_db_t fbno; /* freespace block number */
1370 xfs_dabuf_t *fbp; /* freespace buffer */
1371 int findex; /* freespace entry index */
1372 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1373 xfs_dir2_db_t ifbno; /* initial freespace block no */
1374 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1375 int length; /* length of the new entry */
1376 int logfree; /* need to log free entry */
1377 xfs_mount_t *mp; /* filesystem mount point */
1378 int needlog; /* need to log data header */
1379 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001380 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 xfs_trans_t *tp; /* transaction pointer */
1382
1383 dp = args->dp;
1384 mp = dp->i_mount;
1385 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001386 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /*
1388 * If we came in with a freespace block that means that lookup
1389 * found an entry with our hash value. This is the freespace
1390 * block for that data entry.
1391 */
1392 if (fblk) {
1393 fbp = fblk->bp;
1394 /*
1395 * Remember initial freespace block number.
1396 */
1397 ifbno = fblk->blkno;
1398 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001399 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 findex = fblk->index;
1401 /*
1402 * This means the free entry showed that the data block had
1403 * space for our entry, so we remembered it.
1404 * Use that data block.
1405 */
1406 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001407 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1408 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1409 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1410 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 /*
1413 * The data block looked at didn't have enough room.
1414 * We'll start at the beginning of the freespace entries.
1415 */
1416 else {
1417 dbno = -1;
1418 findex = 0;
1419 }
1420 }
1421 /*
1422 * Didn't come in with a freespace block, so don't have a data block.
1423 */
1424 else {
1425 ifbno = dbno = -1;
1426 fbp = NULL;
1427 findex = 0;
1428 }
1429 /*
1430 * If we don't have a data block yet, we're going to scan the
1431 * freespace blocks looking for one. Figure out what the
1432 * highest freespace block number is.
1433 */
1434 if (dbno == -1) {
1435 xfs_fileoff_t fo; /* freespace block number */
1436
1437 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1438 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001439 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 fbno = ifbno;
1441 }
1442 /*
1443 * While we haven't identified a data block, search the freeblock
1444 * data for a good data block. If we find a null freeblock entry,
1445 * indicating a hole in the data blocks, remember that.
1446 */
1447 while (dbno == -1) {
1448 /*
1449 * If we don't have a freeblock in hand, get the next one.
1450 */
1451 if (fbp == NULL) {
1452 /*
1453 * Happens the first time through unless lookup gave
1454 * us a freespace block to start with.
1455 */
1456 if (++fbno == 0)
1457 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1458 /*
1459 * If it's ifbno we already looked at it.
1460 */
1461 if (fbno == ifbno)
1462 fbno++;
1463 /*
1464 * If it's off the end we're done.
1465 */
1466 if (fbno >= lastfbno)
1467 break;
1468 /*
1469 * Read the block. There can be holes in the
1470 * freespace blocks, so this might not succeed.
1471 * This should be really rare, so there's no reason
1472 * to avoid it.
1473 */
1474 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001475 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 XFS_DATA_FORK))) {
1477 return error;
1478 }
1479 if (unlikely(fbp == NULL)) {
1480 continue;
1481 }
1482 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001483 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 findex = 0;
1485 }
1486 /*
1487 * Look at the current free entry. Is it good enough?
1488 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001489 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1490 be16_to_cpu(free->bests[findex]) >= length)
1491 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 else {
1493 /*
1494 * Are we done with the freeblock?
1495 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001496 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /*
1498 * Drop the block.
1499 */
1500 xfs_da_brelse(tp, fbp);
1501 fbp = NULL;
1502 if (fblk && fblk->bp)
1503 fblk->bp = NULL;
1504 }
1505 }
1506 }
1507 /*
1508 * If we don't have a data block, we need to allocate one and make
1509 * the freespace entries refer to it.
1510 */
1511 if (unlikely(dbno == -1)) {
1512 /*
1513 * Not allowed to allocate, return failure.
1514 */
Barry Naujok6a178102008-05-21 16:42:05 +10001515 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1516 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /*
1518 * Drop the freespace buffer unless it came from our
1519 * caller.
1520 */
1521 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1522 xfs_da_buf_done(fbp);
1523 return XFS_ERROR(ENOSPC);
1524 }
1525 /*
1526 * Allocate and initialize the new data block.
1527 */
1528 if (unlikely((error = xfs_dir2_grow_inode(args,
1529 XFS_DIR2_DATA_SPACE,
1530 &dbno)) ||
1531 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1532 /*
1533 * Drop the freespace buffer unless it came from our
1534 * caller.
1535 */
1536 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1537 xfs_da_buf_done(fbp);
1538 return error;
1539 }
1540 /*
1541 * If (somehow) we have a freespace block, get rid of it.
1542 */
1543 if (fbp)
1544 xfs_da_brelse(tp, fbp);
1545 if (fblk && fblk->bp)
1546 fblk->bp = NULL;
1547
1548 /*
1549 * Get the freespace block corresponding to the data block
1550 * that was just allocated.
1551 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001552 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001554 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 XFS_DATA_FORK))) {
1556 xfs_da_buf_done(dbp);
1557 return error;
1558 }
1559 /*
1560 * If there wasn't a freespace block, the read will
1561 * return a NULL fbp. Allocate and initialize a new one.
1562 */
1563 if( fbp == NULL ) {
1564 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1565 &fbno))) {
1566 return error;
1567 }
1568
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001569 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001570 xfs_alert(mp,
1571 "%s: dir ino " "%llu needed freesp block %lld for\n"
1572 " data block %lld, got %lld ifbno %llu lastfbno %d",
1573 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001574 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 (long long)dbno, (long long)fbno,
1576 (unsigned long long)ifbno, lastfbno);
1577 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001578 xfs_alert(mp,
1579 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 fblk,
1581 (unsigned long long)fblk->blkno,
1582 fblk->index,
1583 fblk->magic);
1584 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001585 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1588 XFS_ERRLEVEL_LOW, mp);
1589 return XFS_ERROR(EFSCORRUPTED);
1590 }
1591
1592 /*
1593 * Get a buffer for the new block.
1594 */
1595 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001596 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 -1, &fbp, XFS_DATA_FORK))) {
1598 return error;
1599 }
1600 ASSERT(fbp != NULL);
1601
1602 /*
1603 * Initialize the new block to be empty, and remember
1604 * its first slot as our empty slot.
1605 */
1606 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001607 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1608 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1610 XFS_DIR2_MAX_FREE_BESTS(mp));
1611 free->hdr.nvalid = 0;
1612 free->hdr.nused = 0;
1613 } else {
1614 free = fbp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001615 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 /*
1619 * Set the freespace block index from the data block number.
1620 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001621 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /*
1623 * If it's after the end of the current entries in the
1624 * freespace block, extend that table.
1625 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001626 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001628 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /*
1630 * Tag new entry so nused will go up.
1631 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001632 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634 /*
1635 * If this entry was for an empty data block
1636 * (this should always be true) then update the header.
1637 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001638 if (free->bests[findex] == cpu_to_be16(NULLDATAOFF)) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001639 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 xfs_dir2_free_log_header(tp, fbp);
1641 }
1642 /*
1643 * Update the real value in the table.
1644 * We haven't allocated the data entry yet so this will
1645 * change again.
1646 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001647 hdr = dbp->data;
1648 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 logfree = 1;
1650 }
1651 /*
1652 * We had a data block so we don't have to make a new one.
1653 */
1654 else {
1655 /*
1656 * If just checking, we succeeded.
1657 */
Barry Naujok6a178102008-05-21 16:42:05 +10001658 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1660 xfs_da_buf_done(fbp);
1661 return 0;
1662 }
1663 /*
1664 * Read the data block in.
1665 */
1666 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001667 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 -1, &dbp, XFS_DATA_FORK))) {
1669 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1670 xfs_da_buf_done(fbp);
1671 return error;
1672 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001673 hdr = dbp->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 logfree = 0;
1675 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001676 ASSERT(be16_to_cpu(hdr->bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Point to the existing unused space.
1679 */
1680 dup = (xfs_dir2_data_unused_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001681 ((char *)hdr + be16_to_cpu(hdr->bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 needscan = needlog = 0;
1683 /*
1684 * Mark the first part of the unused space, inuse for us.
1685 */
1686 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001687 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 &needlog, &needscan);
1689 /*
1690 * Fill in the new entry and log it.
1691 */
1692 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001693 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 dep->namelen = args->namelen;
1695 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001696 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001697 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 xfs_dir2_data_log_entry(tp, dbp, dep);
1699 /*
1700 * Rescan the block for bestfree if needed.
1701 */
1702 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001703 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /*
1705 * Log the data block header if needed.
1706 */
1707 if (needlog)
1708 xfs_dir2_data_log_header(tp, dbp);
1709 /*
1710 * If the freespace entry is now wrong, update it.
1711 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001712 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(hdr->bestfree[0].length)) {
1713 free->bests[findex] = hdr->bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 logfree = 1;
1715 }
1716 /*
1717 * Log the freespace entry if needed.
1718 */
1719 if (logfree)
1720 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1721 /*
1722 * If the caller didn't hand us the freespace block, drop it.
1723 */
1724 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1725 xfs_da_buf_done(fbp);
1726 /*
1727 * Return the data block and offset in args, then drop the data block.
1728 */
1729 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001730 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 xfs_da_buf_done(dbp);
1732 return 0;
1733}
1734
1735/*
1736 * Lookup an entry in a node-format directory.
1737 * All the real work happens in xfs_da_node_lookup_int.
1738 * The only real output is the inode number of the entry.
1739 */
1740int /* error */
1741xfs_dir2_node_lookup(
1742 xfs_da_args_t *args) /* operation arguments */
1743{
1744 int error; /* error return value */
1745 int i; /* btree level */
1746 int rval; /* operation return value */
1747 xfs_da_state_t *state; /* btree cursor */
1748
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001749 trace_xfs_dir2_node_lookup(args);
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Allocate and initialize the btree cursor.
1753 */
1754 state = xfs_da_state_alloc();
1755 state->args = args;
1756 state->mp = args->dp->i_mount;
1757 state->blocksize = state->mp->m_dirblksize;
1758 state->node_ents = state->mp->m_dir_node_ents;
1759 /*
1760 * Fill in the path to the entry in the cursor.
1761 */
1762 error = xfs_da_node_lookup_int(state, &rval);
1763 if (error)
1764 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001765 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1766 /* If a CI match, dup the actual name and return EEXIST */
1767 xfs_dir2_data_entry_t *dep;
1768
1769 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1770 data + state->extrablk.index);
1771 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
1774 * Release the btree blocks and leaf block.
1775 */
1776 for (i = 0; i < state->path.active; i++) {
1777 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1778 state->path.blk[i].bp = NULL;
1779 }
1780 /*
1781 * Release the data block if we have it.
1782 */
1783 if (state->extravalid && state->extrablk.bp) {
1784 xfs_da_brelse(args->trans, state->extrablk.bp);
1785 state->extrablk.bp = NULL;
1786 }
1787 xfs_da_state_free(state);
1788 return rval;
1789}
1790
1791/*
1792 * Remove an entry from a node-format directory.
1793 */
1794int /* error */
1795xfs_dir2_node_removename(
1796 xfs_da_args_t *args) /* operation arguments */
1797{
1798 xfs_da_state_blk_t *blk; /* leaf block */
1799 int error; /* error return value */
1800 int rval; /* operation return value */
1801 xfs_da_state_t *state; /* btree cursor */
1802
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001803 trace_xfs_dir2_node_removename(args);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /*
1806 * Allocate and initialize the btree cursor.
1807 */
1808 state = xfs_da_state_alloc();
1809 state->args = args;
1810 state->mp = args->dp->i_mount;
1811 state->blocksize = state->mp->m_dirblksize;
1812 state->node_ents = state->mp->m_dir_node_ents;
1813 /*
1814 * Look up the entry we're deleting, set up the cursor.
1815 */
1816 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001817 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Didn't find it, upper layer screwed up.
1821 */
1822 if (rval != EEXIST) {
1823 xfs_da_state_free(state);
1824 return rval;
1825 }
1826 blk = &state->path.blk[state->path.active - 1];
1827 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1828 ASSERT(state->extravalid);
1829 /*
1830 * Remove the leaf and data entries.
1831 * Extrablk refers to the data block.
1832 */
1833 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1834 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001835 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /*
1838 * Fix the hash values up the btree.
1839 */
1840 xfs_da_fixhashpath(state, &state->path);
1841 /*
1842 * If we need to join leaf blocks, do it.
1843 */
1844 if (rval && state->path.active > 1)
1845 error = xfs_da_join(state);
1846 /*
1847 * If no errors so far, try conversion to leaf format.
1848 */
1849 if (!error)
1850 error = xfs_dir2_node_to_leaf(state);
1851 xfs_da_state_free(state);
1852 return error;
1853}
1854
1855/*
1856 * Replace an entry's inode number in a node-format directory.
1857 */
1858int /* error */
1859xfs_dir2_node_replace(
1860 xfs_da_args_t *args) /* operation arguments */
1861{
1862 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001863 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 xfs_dir2_data_entry_t *dep; /* data entry changed */
1865 int error; /* error return value */
1866 int i; /* btree level */
1867 xfs_ino_t inum; /* new inode number */
1868 xfs_dir2_leaf_t *leaf; /* leaf structure */
1869 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1870 int rval; /* internal return value */
1871 xfs_da_state_t *state; /* btree cursor */
1872
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001873 trace_xfs_dir2_node_replace(args);
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /*
1876 * Allocate and initialize the btree cursor.
1877 */
1878 state = xfs_da_state_alloc();
1879 state->args = args;
1880 state->mp = args->dp->i_mount;
1881 state->blocksize = state->mp->m_dirblksize;
1882 state->node_ents = state->mp->m_dir_node_ents;
1883 inum = args->inumber;
1884 /*
1885 * Lookup the entry to change in the btree.
1886 */
1887 error = xfs_da_node_lookup_int(state, &rval);
1888 if (error) {
1889 rval = error;
1890 }
1891 /*
1892 * It should be found, since the vnodeops layer has looked it up
1893 * and locked it. But paranoia is good.
1894 */
1895 if (rval == EEXIST) {
1896 /*
1897 * Find the leaf entry.
1898 */
1899 blk = &state->path.blk[state->path.active - 1];
1900 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1901 leaf = blk->bp->data;
1902 lep = &leaf->ents[blk->index];
1903 ASSERT(state->extravalid);
1904 /*
1905 * Point to the data entry.
1906 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001907 hdr = state->extrablk.bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001908 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001910 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001911 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001912 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
1914 * Fill in the new inode number and log the entry.
1915 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001916 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1918 rval = 0;
1919 }
1920 /*
1921 * Didn't find it, and we're holding a data block. Drop it.
1922 */
1923 else if (state->extravalid) {
1924 xfs_da_brelse(args->trans, state->extrablk.bp);
1925 state->extrablk.bp = NULL;
1926 }
1927 /*
1928 * Release all the buffers in the cursor.
1929 */
1930 for (i = 0; i < state->path.active; i++) {
1931 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1932 state->path.blk[i].bp = NULL;
1933 }
1934 xfs_da_state_free(state);
1935 return rval;
1936}
1937
1938/*
1939 * Trim off a trailing empty freespace block.
1940 * Return (in rvalp) 1 if we did it, 0 if not.
1941 */
1942int /* error */
1943xfs_dir2_node_trim_free(
1944 xfs_da_args_t *args, /* operation arguments */
1945 xfs_fileoff_t fo, /* free block number */
1946 int *rvalp) /* out: did something */
1947{
1948 xfs_dabuf_t *bp; /* freespace buffer */
1949 xfs_inode_t *dp; /* incore directory inode */
1950 int error; /* error return code */
1951 xfs_dir2_free_t *free; /* freespace structure */
1952 xfs_mount_t *mp; /* filesystem mount point */
1953 xfs_trans_t *tp; /* transaction pointer */
1954
1955 dp = args->dp;
1956 mp = dp->i_mount;
1957 tp = args->trans;
1958 /*
1959 * Read the freespace block.
1960 */
1961 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1962 XFS_DATA_FORK))) {
1963 return error;
1964 }
1965
1966 /*
1967 * There can be holes in freespace. If fo is a hole, there's
1968 * nothing to do.
1969 */
1970 if (bp == NULL) {
1971 return 0;
1972 }
1973 free = bp->data;
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001974 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /*
1976 * If there are used entries, there's nothing to do.
1977 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001978 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 xfs_da_brelse(tp, bp);
1980 *rvalp = 0;
1981 return 0;
1982 }
1983 /*
1984 * Blow the block away.
1985 */
1986 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001987 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 bp))) {
1989 /*
1990 * Can't fail with ENOSPC since that only happens with no
1991 * space reservation, when breaking up an extent into two
1992 * pieces. This is the last block of an extent.
1993 */
1994 ASSERT(error != ENOSPC);
1995 xfs_da_brelse(tp, bp);
1996 return error;
1997 }
1998 /*
1999 * Return that we succeeded.
2000 */
2001 *rvalp = 1;
2002 return 0;
2003}