blob: a0aab7d3294fd4874df67f70014f4f5a7df7dcb0 [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;
Nathan Scott0ba962e2006-03-17 17:27:07 +110076 ASSERT(be32_to_cpu(free->hdr.magic) == 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;
Nathan Scott0ba962e2006-03-17 17:27:07 +110094 ASSERT(be32_to_cpu(free->hdr.magic) == 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 }
247 /*
248 * No stale entries, just insert a space for the new entry.
249 */
250 if (!leaf->hdr.stale) {
251 lep = &leaf->ents[index];
Nathan Scotta818e5d2006-03-17 17:28:07 +1100252 if (index < be16_to_cpu(leaf->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 memmove(lep + 1, lep,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100254 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 lfloglow = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100256 lfloghigh = be16_to_cpu(leaf->hdr.count);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800257 be16_add_cpu(&leaf->hdr.count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 /*
260 * There are stale entries. We'll use one for the new entry.
261 */
262 else {
263 /*
264 * If we didn't do a compact then we need to figure out
265 * which stale entry will be used.
266 */
267 if (compact == 0) {
268 /*
269 * Find first stale entry before our insertion point.
270 */
271 for (lowstale = index - 1;
272 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100273 be32_to_cpu(leaf->ents[lowstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 XFS_DIR2_NULL_DATAPTR;
275 lowstale--)
276 continue;
277 /*
278 * Find next stale entry after insertion point.
279 * Stop looking if the answer would be worse than
280 * lowstale already found.
281 */
282 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100283 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100284 be32_to_cpu(leaf->ents[highstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 XFS_DIR2_NULL_DATAPTR &&
286 (lowstale < 0 ||
287 index - lowstale - 1 >= highstale - index);
288 highstale++)
289 continue;
290 }
291 /*
292 * Using the low stale entry.
293 * Shift entries up toward the stale slot.
294 */
295 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100296 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 index - lowstale - 1 < highstale - index)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100298 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 XFS_DIR2_NULL_DATAPTR);
300 ASSERT(index - lowstale - 1 >= 0);
301 if (index - lowstale - 1 > 0)
302 memmove(&leaf->ents[lowstale],
303 &leaf->ents[lowstale + 1],
304 (index - lowstale - 1) * sizeof(*lep));
305 lep = &leaf->ents[index - 1];
306 lfloglow = MIN(lowstale, lfloglow);
307 lfloghigh = MAX(index - 1, lfloghigh);
308 }
309 /*
310 * Using the high stale entry.
311 * Shift entries down toward the stale slot.
312 */
313 else {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100314 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 XFS_DIR2_NULL_DATAPTR);
316 ASSERT(highstale - index >= 0);
317 if (highstale - index > 0)
318 memmove(&leaf->ents[index + 1],
319 &leaf->ents[index],
320 (highstale - index) * sizeof(*lep));
321 lep = &leaf->ents[index];
322 lfloglow = MIN(index, lfloglow);
323 lfloghigh = MAX(highstale, lfloghigh);
324 }
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800325 be16_add_cpu(&leaf->hdr.stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327 /*
328 * Insert the new entry, log everything.
329 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100330 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000331 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100332 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 xfs_dir2_leaf_log_header(tp, bp);
334 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
335 xfs_dir2_leafn_check(dp, bp);
336 return 0;
337}
338
339#ifdef DEBUG
340/*
341 * Check internal consistency of a leafn block.
342 */
343void
344xfs_dir2_leafn_check(
345 xfs_inode_t *dp, /* incore directory inode */
346 xfs_dabuf_t *bp) /* leaf buffer */
347{
348 int i; /* leaf index */
349 xfs_dir2_leaf_t *leaf; /* leaf structure */
350 xfs_mount_t *mp; /* filesystem mount point */
351 int stale; /* count of stale leaves */
352
353 leaf = bp->data;
354 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100355 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000356 ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp));
Nathan Scotta818e5d2006-03-17 17:28:07 +1100357 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
358 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100359 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
360 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100362 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 stale++;
364 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100365 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367#endif /* DEBUG */
368
369/*
370 * Return the last hash value in the leaf.
371 * Stale entries are ok.
372 */
373xfs_dahash_t /* hash value */
374xfs_dir2_leafn_lasthash(
375 xfs_dabuf_t *bp, /* leaf buffer */
376 int *count) /* count of entries in leaf */
377{
378 xfs_dir2_leaf_t *leaf; /* leaf structure */
379
380 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100381 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100383 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!leaf->hdr.count)
385 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100386 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000390 * Look up a leaf entry for space to add a name in a node-format leaf block.
391 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000393STATIC int
394xfs_dir2_leafn_lookup_for_addname(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 xfs_dabuf_t *bp, /* leaf buffer */
396 xfs_da_args_t *args, /* operation arguments */
397 int *indexp, /* out: leaf entry index */
398 xfs_da_state_t *state) /* state to fill in */
399{
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000400 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
401 xfs_dir2_db_t curdb = -1; /* current data block number */
402 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 xfs_inode_t *dp; /* incore directory inode */
404 int error; /* error return value */
405 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000406 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 int index; /* leaf entry index */
408 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000409 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
411 xfs_mount_t *mp; /* filesystem mount point */
412 xfs_dir2_db_t newdb; /* new data block number */
413 xfs_dir2_db_t newfdb; /* new free block number */
414 xfs_trans_t *tp; /* transaction pointer */
415
416 dp = args->dp;
417 tp = args->trans;
418 mp = dp->i_mount;
419 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100420 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100422 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#endif
424 xfs_dir2_leafn_check(dp, bp);
425 /*
426 * Look up the hash value in the leaf entries.
427 */
428 index = xfs_dir2_leaf_search_hash(args, bp);
429 /*
430 * Do we have a buffer coming in?
431 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000432 if (state->extravalid) {
433 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000435 curfdb = state->extrablk.blkno;
436 free = curbp->data;
437 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000439 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /*
441 * Loop over leaf entries with the right hash value.
442 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000443 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
444 be32_to_cpu(lep->hashval) == args->hashval;
445 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * Skip stale leaf entries.
448 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100449 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 continue;
451 /*
452 * Pull the data block number from the entry.
453 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000454 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 * For addname, we're looking for a place to put the new entry.
457 * We want to use a data block with an entry of equal
458 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000459 *
460 * If this block isn't the data block we already have
461 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000463 if (newdb != curdb) {
464 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000466 * Convert the data block to the free block
467 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000469 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000471 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000473 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000475 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
477 if (curbp)
478 xfs_da_brelse(tp, curbp);
479 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000480 * Read the free block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000482 error = xfs_da_read_buf(tp, dp,
483 xfs_dir2_db_to_da(mp, newfdb),
484 -1, &curbp, XFS_DATA_FORK);
485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return error;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000487 free = curbp->data;
488 ASSERT(be32_to_cpu(free->hdr.magic) ==
489 XFS_DIR2_FREE_MAGIC);
490 ASSERT((be32_to_cpu(free->hdr.firstdb) %
491 XFS_DIR2_MAX_FREE_BESTS(mp)) == 0);
492 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
493 ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) +
494 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000497 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000499 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000501 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000503 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
504 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
505 XFS_ERRLEVEL_LOW, mp);
506 if (curfdb != newfdb)
507 xfs_da_brelse(tp, curbp);
508 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000510 curfdb = newfdb;
511 if (be16_to_cpu(free->bests[fi]) >= length)
512 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000515 /* Didn't find any space */
516 fi = -1;
517out:
Barry Naujok6a178102008-05-21 16:42:05 +1000518 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000519 if (curbp) {
520 /* Giving back a free block. */
521 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000523 state->extrablk.index = fi;
524 state->extrablk.blkno = curfdb;
525 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
526 } else {
527 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000530 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
532 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return XFS_ERROR(ENOENT);
534}
535
536/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000537 * Look up a leaf entry in a node-format leaf block.
538 * The extrablk in state a data block.
539 */
540STATIC int
541xfs_dir2_leafn_lookup_for_entry(
542 xfs_dabuf_t *bp, /* leaf buffer */
543 xfs_da_args_t *args, /* operation arguments */
544 int *indexp, /* out: leaf entry index */
545 xfs_da_state_t *state) /* state to fill in */
546{
547 xfs_dabuf_t *curbp = NULL; /* current data/free buffer */
548 xfs_dir2_db_t curdb = -1; /* current data block number */
549 xfs_dir2_data_entry_t *dep; /* data block entry */
550 xfs_inode_t *dp; /* incore directory inode */
551 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000552 int index; /* leaf entry index */
553 xfs_dir2_leaf_t *leaf; /* leaf structure */
554 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
555 xfs_mount_t *mp; /* filesystem mount point */
556 xfs_dir2_db_t newdb; /* new data block number */
557 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000558 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000559
560 dp = args->dp;
561 tp = args->trans;
562 mp = dp->i_mount;
563 leaf = bp->data;
564 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
565#ifdef __KERNEL__
566 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
567#endif
568 xfs_dir2_leafn_check(dp, bp);
569 /*
570 * Look up the hash value in the leaf entries.
571 */
572 index = xfs_dir2_leaf_search_hash(args, bp);
573 /*
574 * Do we have a buffer coming in?
575 */
576 if (state->extravalid) {
577 curbp = state->extrablk.bp;
578 curdb = state->extrablk.blkno;
579 }
580 /*
581 * Loop over leaf entries with the right hash value.
582 */
583 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
584 be32_to_cpu(lep->hashval) == args->hashval;
585 lep++, index++) {
586 /*
587 * Skip stale leaf entries.
588 */
589 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
590 continue;
591 /*
592 * Pull the data block number from the entry.
593 */
594 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
595 /*
596 * Not adding a new entry, so we really want to find
597 * the name given to us.
598 *
599 * If it's a different data block, go get it.
600 */
601 if (newdb != curdb) {
602 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000603 * If we had a block before that we aren't saving
604 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000605 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000606 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
607 curdb != state->extrablk.blkno))
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000608 xfs_da_brelse(tp, curbp);
609 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000610 * If needing the block that is saved with a CI match,
611 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000612 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000613 if (args->cmpresult != XFS_CMP_DIFFERENT &&
614 newdb == state->extrablk.blkno) {
615 ASSERT(state->extravalid);
616 curbp = state->extrablk.bp;
617 } else {
618 error = xfs_da_read_buf(tp, dp,
619 xfs_dir2_db_to_da(mp, newdb),
620 -1, &curbp, XFS_DATA_FORK);
621 if (error)
622 return error;
623 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000624 xfs_dir2_data_check(dp, curbp);
625 curdb = newdb;
626 }
627 /*
628 * Point to the data entry.
629 */
630 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
631 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
632 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000633 * Compare the entry and if it's an exact match, return
634 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000635 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000636 */
Barry Naujok5163f952008-05-21 16:41:01 +1000637 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
638 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000639 /* If there is a CI match block, drop it */
640 if (args->cmpresult != XFS_CMP_DIFFERENT &&
641 curdb != state->extrablk.blkno)
642 xfs_da_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000643 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000644 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000645 *indexp = index;
646 state->extravalid = 1;
647 state->extrablk.bp = curbp;
648 state->extrablk.blkno = curdb;
649 state->extrablk.index = (int)((char *)dep -
650 (char *)curbp->data);
651 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Barry Naujok5163f952008-05-21 16:41:01 +1000652 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000653 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000654 }
655 }
Barry Naujok6a178102008-05-21 16:42:05 +1000656 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
657 (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000658 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000659 if (args->cmpresult == XFS_CMP_DIFFERENT) {
660 /* Giving back last used data block. */
661 state->extravalid = 1;
662 state->extrablk.bp = curbp;
663 state->extrablk.index = -1;
664 state->extrablk.blkno = curdb;
665 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
666 } else {
667 /* If the curbp is not the CI match block, drop it */
668 if (state->extrablk.bp != curbp)
669 xfs_da_brelse(tp, curbp);
670 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000671 } else {
672 state->extravalid = 0;
673 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000675 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676}
677
678/*
679 * Look up a leaf entry in a node-format leaf block.
680 * If this is an addname then the extrablk in state is a freespace block,
681 * otherwise it's a data block.
682 */
683int
684xfs_dir2_leafn_lookup_int(
685 xfs_dabuf_t *bp, /* leaf buffer */
686 xfs_da_args_t *args, /* operation arguments */
687 int *indexp, /* out: leaf entry index */
688 xfs_da_state_t *state) /* state to fill in */
689{
Barry Naujok6a178102008-05-21 16:42:05 +1000690 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000691 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
692 state);
693 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
694}
695
696/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * Move count leaf entries from source to destination leaf.
698 * Log entries and headers. Stale entries are preserved.
699 */
700static void
701xfs_dir2_leafn_moveents(
702 xfs_da_args_t *args, /* operation arguments */
703 xfs_dabuf_t *bp_s, /* source leaf buffer */
704 int start_s, /* source leaf index */
705 xfs_dabuf_t *bp_d, /* destination leaf buffer */
706 int start_d, /* destination leaf index */
707 int count) /* count of leaves to copy */
708{
709 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
710 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
711 int stale; /* count stale leaves copied */
712 xfs_trans_t *tp; /* transaction pointer */
713
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000714 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /*
717 * Silently return if nothing to do.
718 */
719 if (count == 0) {
720 return;
721 }
722 tp = args->trans;
723 leaf_s = bp_s->data;
724 leaf_d = bp_d->data;
725 /*
726 * If the destination index is not the end of the current
727 * destination leaf entries, open up a hole in the destination
728 * to hold the new entries.
729 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100730 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100732 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sizeof(xfs_dir2_leaf_entry_t));
734 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100735 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 /*
738 * If the source has stale leaves, count the ones in the copy range
739 * so we can update the header correctly.
740 */
741 if (leaf_s->hdr.stale) {
742 int i; /* temp leaf index */
743
744 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100745 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 stale++;
747 }
748 } else
749 stale = 0;
750 /*
751 * Copy the leaf entries from source to destination.
752 */
753 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
754 count * sizeof(xfs_dir2_leaf_entry_t));
755 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
756 /*
757 * If there are source entries after the ones we copied,
758 * delete the ones we copied by sliding the next ones down.
759 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100760 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
762 count * sizeof(xfs_dir2_leaf_entry_t));
763 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
764 }
765 /*
766 * Update the headers and log them.
767 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800768 be16_add_cpu(&leaf_s->hdr.count, -(count));
769 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
770 be16_add_cpu(&leaf_d->hdr.count, count);
771 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 xfs_dir2_leaf_log_header(tp, bp_s);
773 xfs_dir2_leaf_log_header(tp, bp_d);
774 xfs_dir2_leafn_check(args->dp, bp_s);
775 xfs_dir2_leafn_check(args->dp, bp_d);
776}
777
778/*
779 * Determine the sort order of two leaf blocks.
780 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
781 */
782int /* sort order */
783xfs_dir2_leafn_order(
784 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
785 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
786{
787 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
788 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
789
790 leaf1 = leaf1_bp->data;
791 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100792 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
793 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100794 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
795 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100796 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
797 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
798 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 1;
800 return 0;
801}
802
803/*
804 * Rebalance leaf entries between two leaf blocks.
805 * This is actually only called when the second block is new,
806 * though the code deals with the general case.
807 * A new entry will be inserted in one of the blocks, and that
808 * entry is taken into account when balancing.
809 */
810static void
811xfs_dir2_leafn_rebalance(
812 xfs_da_state_t *state, /* btree cursor */
813 xfs_da_state_blk_t *blk1, /* first btree block */
814 xfs_da_state_blk_t *blk2) /* second btree block */
815{
816 xfs_da_args_t *args; /* operation arguments */
817 int count; /* count (& direction) leaves */
818 int isleft; /* new goes in left leaf */
819 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
820 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
821 int mid; /* midpoint leaf index */
822#ifdef DEBUG
823 int oldstale; /* old count of stale leaves */
824#endif
825 int oldsum; /* old total leaf count */
826 int swap; /* swapped leaf blocks */
827
828 args = state->args;
829 /*
830 * If the block order is wrong, swap the arguments.
831 */
832 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
833 xfs_da_state_blk_t *tmp; /* temp for block swap */
834
835 tmp = blk1;
836 blk1 = blk2;
837 blk2 = tmp;
838 }
839 leaf1 = blk1->bp->data;
840 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100841 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100843 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#endif
845 mid = oldsum >> 1;
846 /*
847 * If the old leaf count was odd then the new one will be even,
848 * so we need to divide the new count evenly.
849 */
850 if (oldsum & 1) {
851 xfs_dahash_t midhash; /* middle entry hash value */
852
Nathan Scotta818e5d2006-03-17 17:28:07 +1100853 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100854 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100856 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 isleft = args->hashval <= midhash;
858 }
859 /*
860 * If the old count is even then the new count is odd, so there's
861 * no preferred side for the new entry.
862 * Pick the left one.
863 */
864 else
865 isleft = 1;
866 /*
867 * Calculate moved entry count. Positive means left-to-right,
868 * negative means right-to-left. Then move the entries.
869 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100870 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (count > 0)
872 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100873 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 else if (count < 0)
875 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100876 be16_to_cpu(leaf1->hdr.count), count);
877 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
878 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
880 * Mark whether we're inserting into the old or new leaf.
881 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100882 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100884 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 state->inleaf = !swap;
886 else
887 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100888 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /*
890 * Adjust the expected index for insertion.
891 */
892 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100893 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000894
895 /*
896 * Finally sanity check just to make sure we are not returning a
897 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
899 if(blk2->index < 0) {
900 state->inleaf = 1;
901 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +1100902 xfs_alert(args->dp->i_mount,
903 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
904 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906}
907
908/*
909 * Remove an entry from a node directory.
910 * This removes the leaf entry and the data entry,
911 * and updates the free block if necessary.
912 */
913static int /* error */
914xfs_dir2_leafn_remove(
915 xfs_da_args_t *args, /* operation arguments */
916 xfs_dabuf_t *bp, /* leaf buffer */
917 int index, /* leaf entry index */
918 xfs_da_state_blk_t *dblk, /* data block */
919 int *rval) /* resulting block needs join */
920{
921 xfs_dir2_data_t *data; /* data block structure */
922 xfs_dir2_db_t db; /* data block number */
923 xfs_dabuf_t *dbp; /* data block buffer */
924 xfs_dir2_data_entry_t *dep; /* data block entry */
925 xfs_inode_t *dp; /* incore directory inode */
926 xfs_dir2_leaf_t *leaf; /* leaf structure */
927 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
928 int longest; /* longest data free entry */
929 int off; /* data block entry offset */
930 xfs_mount_t *mp; /* filesystem mount point */
931 int needlog; /* need to log data header */
932 int needscan; /* need to rescan data frees */
933 xfs_trans_t *tp; /* transaction pointer */
934
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000935 trace_xfs_dir2_leafn_remove(args, index);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 dp = args->dp;
938 tp = args->trans;
939 mp = dp->i_mount;
940 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100941 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * Point to the entry we're removing.
944 */
945 lep = &leaf->ents[index];
946 /*
947 * Extract the data block and offset from the entry.
948 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000949 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000951 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ASSERT(dblk->index == off);
953 /*
954 * Kill the leaf entry by marking it stale.
955 * Log the leaf block changes.
956 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800957 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100959 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 xfs_dir2_leaf_log_ents(tp, bp, index, index);
961 /*
962 * Make the data entry free. Keep track of the longest freespace
963 * in the data block in case it changes.
964 */
965 dbp = dblk->bp;
966 data = dbp->data;
967 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100968 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 needlog = needscan = 0;
970 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000971 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /*
973 * Rescan the data block freespaces for bestfree.
974 * Log the data block header if needed.
975 */
976 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000977 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (needlog)
979 xfs_dir2_data_log_header(tp, dbp);
980 xfs_dir2_data_check(dp, dbp);
981 /*
982 * If the longest data block freespace changes, need to update
983 * the corresponding freeblock entry.
984 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100985 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 int error; /* error return value */
987 xfs_dabuf_t *fbp; /* freeblock buffer */
988 xfs_dir2_db_t fdb; /* freeblock block number */
989 int findex; /* index in freeblock entries */
990 xfs_dir2_free_t *free; /* freeblock structure */
991 int logfree; /* need to log free entry */
992
993 /*
994 * Convert the data block number to a free block,
995 * read in the free block.
996 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000997 fdb = xfs_dir2_db_to_fdb(mp, db);
998 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 -1, &fbp, XFS_DATA_FORK))) {
1000 return error;
1001 }
1002 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001003 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1004 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 XFS_DIR2_MAX_FREE_BESTS(mp) *
1006 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1007 /*
1008 * Calculate which entry we need to fix.
1009 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001010 findex = xfs_dir2_db_to_fdindex(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +11001011 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * If the data block is now empty we can get rid of it
1014 * (usually).
1015 */
1016 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
1017 /*
1018 * Try to punch out the data block.
1019 */
1020 error = xfs_dir2_shrink_inode(args, db, dbp);
1021 if (error == 0) {
1022 dblk->bp = NULL;
1023 data = NULL;
1024 }
1025 /*
1026 * We can get ENOSPC if there's no space reservation.
1027 * In this case just drop the buffer and some one else
1028 * will eventually get rid of the empty block.
1029 */
1030 else if (error == ENOSPC && args->total == 0)
1031 xfs_da_buf_done(dbp);
1032 else
1033 return error;
1034 }
1035 /*
1036 * If we got rid of the data block, we can eliminate that entry
1037 * in the free block.
1038 */
1039 if (data == NULL) {
1040 /*
1041 * One less used entry in the free table.
1042 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001043 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 xfs_dir2_free_log_header(tp, fbp);
1045 /*
1046 * If this was the last entry in the table, we can
1047 * trim the table size back. There might be other
1048 * entries at the end referring to non-existent
1049 * data blocks, get those too.
1050 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001051 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int i; /* free entry index */
1053
1054 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001055 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 i--)
1057 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001058 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 logfree = 0;
1060 }
1061 /*
1062 * Not the last entry, just punch it out.
1063 */
1064 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001065 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 logfree = 1;
1067 }
1068 /*
1069 * If there are no useful entries left in the block,
1070 * get rid of the block if we can.
1071 */
1072 if (!free->hdr.nused) {
1073 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1074 if (error == 0) {
1075 fbp = NULL;
1076 logfree = 0;
1077 } else if (error != ENOSPC || args->total != 0)
1078 return error;
1079 /*
1080 * It's possible to get ENOSPC if there is no
1081 * space reservation. In this case some one
1082 * else will eventually get rid of this block.
1083 */
1084 }
1085 }
1086 /*
1087 * Data block is not empty, just set the free entry to
1088 * the new value.
1089 */
1090 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001091 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 logfree = 1;
1093 }
1094 /*
1095 * Log the free entry that changed, unless we got rid of it.
1096 */
1097 if (logfree)
1098 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1099 /*
1100 * Drop the buffer if we still have it.
1101 */
1102 if (fbp)
1103 xfs_da_buf_done(fbp);
1104 }
1105 xfs_dir2_leafn_check(dp, bp);
1106 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001107 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * to justify trying to join it with a neighbor.
1109 */
1110 *rval =
1111 ((uint)sizeof(leaf->hdr) +
1112 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001113 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 mp->m_dir_magicpct;
1115 return 0;
1116}
1117
1118/*
1119 * Split the leaf entries in the old block into old and new blocks.
1120 */
1121int /* error */
1122xfs_dir2_leafn_split(
1123 xfs_da_state_t *state, /* btree cursor */
1124 xfs_da_state_blk_t *oldblk, /* original block */
1125 xfs_da_state_blk_t *newblk) /* newly created block */
1126{
1127 xfs_da_args_t *args; /* operation arguments */
1128 xfs_dablk_t blkno; /* new leaf block number */
1129 int error; /* error return value */
1130 xfs_mount_t *mp; /* filesystem mount point */
1131
1132 /*
1133 * Allocate space for a new leaf node.
1134 */
1135 args = state->args;
1136 mp = args->dp->i_mount;
1137 ASSERT(args != NULL);
1138 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1139 error = xfs_da_grow_inode(args, &blkno);
1140 if (error) {
1141 return error;
1142 }
1143 /*
1144 * Initialize the new leaf block.
1145 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001146 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1148 if (error) {
1149 return error;
1150 }
1151 newblk->blkno = blkno;
1152 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1153 /*
1154 * Rebalance the entries across the two leaves, link the new
1155 * block into the leaves.
1156 */
1157 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1158 error = xfs_da_blk_link(state, oldblk, newblk);
1159 if (error) {
1160 return error;
1161 }
1162 /*
1163 * Insert the new entry in the correct block.
1164 */
1165 if (state->inleaf)
1166 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1167 else
1168 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1169 /*
1170 * Update last hashval in each block since we added the name.
1171 */
1172 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1173 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1174 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1175 xfs_dir2_leafn_check(args->dp, newblk->bp);
1176 return error;
1177}
1178
1179/*
1180 * Check a leaf block and its neighbors to see if the block should be
1181 * collapsed into one or the other neighbor. Always keep the block
1182 * with the smaller block number.
1183 * If the current block is over 50% full, don't try to join it, return 0.
1184 * If the block is empty, fill in the state structure and return 2.
1185 * If it can be collapsed, fill in the state structure and return 1.
1186 * If nothing can be done, return 0.
1187 */
1188int /* error */
1189xfs_dir2_leafn_toosmall(
1190 xfs_da_state_t *state, /* btree cursor */
1191 int *action) /* resulting action to take */
1192{
1193 xfs_da_state_blk_t *blk; /* leaf block */
1194 xfs_dablk_t blkno; /* leaf block number */
1195 xfs_dabuf_t *bp; /* leaf buffer */
1196 int bytes; /* bytes in use */
1197 int count; /* leaf live entry count */
1198 int error; /* error return value */
1199 int forward; /* sibling block direction */
1200 int i; /* sibling counter */
1201 xfs_da_blkinfo_t *info; /* leaf block header */
1202 xfs_dir2_leaf_t *leaf; /* leaf structure */
1203 int rval; /* result from path_shift */
1204
1205 /*
1206 * Check for the degenerate case of the block being over 50% full.
1207 * If so, it's not worth even looking to see if we might be able
1208 * to coalesce with a sibling.
1209 */
1210 blk = &state->path.blk[state->path.active - 1];
1211 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001212 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001214 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1216 if (bytes > (state->blocksize >> 1)) {
1217 /*
1218 * Blk over 50%, don't try to join.
1219 */
1220 *action = 0;
1221 return 0;
1222 }
1223 /*
1224 * Check for the degenerate case of the block being empty.
1225 * If the block is empty, we'll simply delete it, no need to
1226 * coalesce it with a sibling block. We choose (arbitrarily)
1227 * to merge with the forward block unless it is NULL.
1228 */
1229 if (count == 0) {
1230 /*
1231 * Make altpath point to the block we want to keep and
1232 * path point to the block we want to drop (this one).
1233 */
Nathan Scott89da0542006-03-17 17:28:40 +11001234 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 memcpy(&state->altpath, &state->path, sizeof(state->path));
1236 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1237 &rval);
1238 if (error)
1239 return error;
1240 *action = rval ? 2 : 0;
1241 return 0;
1242 }
1243 /*
1244 * Examine each sibling block to see if we can coalesce with
1245 * at least 25% free space to spare. We need to figure out
1246 * whether to merge with the forward or the backward block.
1247 * We prefer coalescing with the lower numbered sibling so as
1248 * to shrink a directory over time.
1249 */
Nathan Scott89da0542006-03-17 17:28:40 +11001250 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001252 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (blkno == 0)
1254 continue;
1255 /*
1256 * Read the sibling leaf block.
1257 */
1258 if ((error =
1259 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1260 -1, &bp, XFS_DATA_FORK))) {
1261 return error;
1262 }
1263 ASSERT(bp != NULL);
1264 /*
1265 * Count bytes in the two blocks combined.
1266 */
1267 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001268 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 bytes = state->blocksize - (state->blocksize >> 2);
1270 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001271 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001272 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 bytes -= count * (uint)sizeof(leaf->ents[0]);
1274 /*
1275 * Fits with at least 25% to spare.
1276 */
1277 if (bytes >= 0)
1278 break;
1279 xfs_da_brelse(state->args->trans, bp);
1280 }
1281 /*
1282 * Didn't like either block, give up.
1283 */
1284 if (i >= 2) {
1285 *action = 0;
1286 return 0;
1287 }
1288 /*
1289 * Done with the sibling leaf block here, drop the dabuf
1290 * so path_shift can get it.
1291 */
1292 xfs_da_buf_done(bp);
1293 /*
1294 * Make altpath point to the block we want to keep (the lower
1295 * numbered block) and path point to the block we want to drop.
1296 */
1297 memcpy(&state->altpath, &state->path, sizeof(state->path));
1298 if (blkno < blk->blkno)
1299 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1300 &rval);
1301 else
1302 error = xfs_da_path_shift(state, &state->path, forward, 0,
1303 &rval);
1304 if (error) {
1305 return error;
1306 }
1307 *action = rval ? 0 : 1;
1308 return 0;
1309}
1310
1311/*
1312 * Move all the leaf entries from drop_blk to save_blk.
1313 * This is done as part of a join operation.
1314 */
1315void
1316xfs_dir2_leafn_unbalance(
1317 xfs_da_state_t *state, /* cursor */
1318 xfs_da_state_blk_t *drop_blk, /* dead block */
1319 xfs_da_state_blk_t *save_blk) /* surviving block */
1320{
1321 xfs_da_args_t *args; /* operation arguments */
1322 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1323 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1324
1325 args = state->args;
1326 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1327 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 drop_leaf = drop_blk->bp->data;
1329 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001330 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1331 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /*
1333 * If there are any stale leaf entries, take this opportunity
1334 * to purge them.
1335 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001336 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001338 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 xfs_dir2_leaf_compact(args, save_blk->bp);
1340 /*
1341 * Move the entries from drop to the appropriate end of save.
1342 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001343 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 -07001344 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1345 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001346 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 else
1348 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001349 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001350 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 -07001351 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1352}
1353
1354/*
1355 * Top-level node form directory addname routine.
1356 */
1357int /* error */
1358xfs_dir2_node_addname(
1359 xfs_da_args_t *args) /* operation arguments */
1360{
1361 xfs_da_state_blk_t *blk; /* leaf block for insert */
1362 int error; /* error return value */
1363 int rval; /* sub-return value */
1364 xfs_da_state_t *state; /* btree cursor */
1365
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001366 trace_xfs_dir2_node_addname(args);
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * Allocate and initialize the state (btree cursor).
1370 */
1371 state = xfs_da_state_alloc();
1372 state->args = args;
1373 state->mp = args->dp->i_mount;
1374 state->blocksize = state->mp->m_dirblksize;
1375 state->node_ents = state->mp->m_dir_node_ents;
1376 /*
1377 * Look up the name. We're not supposed to find it, but
1378 * this gives us the insertion point.
1379 */
1380 error = xfs_da_node_lookup_int(state, &rval);
1381 if (error)
1382 rval = error;
1383 if (rval != ENOENT) {
1384 goto done;
1385 }
1386 /*
1387 * Add the data entry to a data block.
1388 * Extravalid is set to a freeblock found by lookup.
1389 */
1390 rval = xfs_dir2_node_addname_int(args,
1391 state->extravalid ? &state->extrablk : NULL);
1392 if (rval) {
1393 goto done;
1394 }
1395 blk = &state->path.blk[state->path.active - 1];
1396 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1397 /*
1398 * Add the new leaf entry.
1399 */
1400 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1401 if (rval == 0) {
1402 /*
1403 * It worked, fix the hash values up the btree.
1404 */
Barry Naujok6a178102008-05-21 16:42:05 +10001405 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 xfs_da_fixhashpath(state, &state->path);
1407 } else {
1408 /*
1409 * It didn't work, we need to split the leaf block.
1410 */
1411 if (args->total == 0) {
1412 ASSERT(rval == ENOSPC);
1413 goto done;
1414 }
1415 /*
1416 * Split the leaf block and insert the new entry.
1417 */
1418 rval = xfs_da_split(state);
1419 }
1420done:
1421 xfs_da_state_free(state);
1422 return rval;
1423}
1424
1425/*
1426 * Add the data entry for a node-format directory name addition.
1427 * The leaf entry is added in xfs_dir2_leafn_add.
1428 * We may enter with a freespace block that the lookup found.
1429 */
1430static int /* error */
1431xfs_dir2_node_addname_int(
1432 xfs_da_args_t *args, /* operation arguments */
1433 xfs_da_state_blk_t *fblk) /* optional freespace block */
1434{
1435 xfs_dir2_data_t *data; /* data block structure */
1436 xfs_dir2_db_t dbno; /* data block number */
1437 xfs_dabuf_t *dbp; /* data block buffer */
1438 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1439 xfs_inode_t *dp; /* incore directory inode */
1440 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1441 int error; /* error return value */
1442 xfs_dir2_db_t fbno; /* freespace block number */
1443 xfs_dabuf_t *fbp; /* freespace buffer */
1444 int findex; /* freespace entry index */
1445 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1446 xfs_dir2_db_t ifbno; /* initial freespace block no */
1447 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1448 int length; /* length of the new entry */
1449 int logfree; /* need to log free entry */
1450 xfs_mount_t *mp; /* filesystem mount point */
1451 int needlog; /* need to log data header */
1452 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001453 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 xfs_trans_t *tp; /* transaction pointer */
1455
1456 dp = args->dp;
1457 mp = dp->i_mount;
1458 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001459 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /*
1461 * If we came in with a freespace block that means that lookup
1462 * found an entry with our hash value. This is the freespace
1463 * block for that data entry.
1464 */
1465 if (fblk) {
1466 fbp = fblk->bp;
1467 /*
1468 * Remember initial freespace block number.
1469 */
1470 ifbno = fblk->blkno;
1471 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001472 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 findex = fblk->index;
1474 /*
1475 * This means the free entry showed that the data block had
1476 * space for our entry, so we remembered it.
1477 * Use that data block.
1478 */
1479 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001480 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1481 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1482 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1483 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 /*
1486 * The data block looked at didn't have enough room.
1487 * We'll start at the beginning of the freespace entries.
1488 */
1489 else {
1490 dbno = -1;
1491 findex = 0;
1492 }
1493 }
1494 /*
1495 * Didn't come in with a freespace block, so don't have a data block.
1496 */
1497 else {
1498 ifbno = dbno = -1;
1499 fbp = NULL;
1500 findex = 0;
1501 }
1502 /*
1503 * If we don't have a data block yet, we're going to scan the
1504 * freespace blocks looking for one. Figure out what the
1505 * highest freespace block number is.
1506 */
1507 if (dbno == -1) {
1508 xfs_fileoff_t fo; /* freespace block number */
1509
1510 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1511 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001512 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 fbno = ifbno;
1514 }
1515 /*
1516 * While we haven't identified a data block, search the freeblock
1517 * data for a good data block. If we find a null freeblock entry,
1518 * indicating a hole in the data blocks, remember that.
1519 */
1520 while (dbno == -1) {
1521 /*
1522 * If we don't have a freeblock in hand, get the next one.
1523 */
1524 if (fbp == NULL) {
1525 /*
1526 * Happens the first time through unless lookup gave
1527 * us a freespace block to start with.
1528 */
1529 if (++fbno == 0)
1530 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1531 /*
1532 * If it's ifbno we already looked at it.
1533 */
1534 if (fbno == ifbno)
1535 fbno++;
1536 /*
1537 * If it's off the end we're done.
1538 */
1539 if (fbno >= lastfbno)
1540 break;
1541 /*
1542 * Read the block. There can be holes in the
1543 * freespace blocks, so this might not succeed.
1544 * This should be really rare, so there's no reason
1545 * to avoid it.
1546 */
1547 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001548 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 XFS_DATA_FORK))) {
1550 return error;
1551 }
1552 if (unlikely(fbp == NULL)) {
1553 continue;
1554 }
1555 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001556 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 findex = 0;
1558 }
1559 /*
1560 * Look at the current free entry. Is it good enough?
1561 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001562 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1563 be16_to_cpu(free->bests[findex]) >= length)
1564 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 else {
1566 /*
1567 * Are we done with the freeblock?
1568 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001569 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /*
1571 * Drop the block.
1572 */
1573 xfs_da_brelse(tp, fbp);
1574 fbp = NULL;
1575 if (fblk && fblk->bp)
1576 fblk->bp = NULL;
1577 }
1578 }
1579 }
1580 /*
1581 * If we don't have a data block, we need to allocate one and make
1582 * the freespace entries refer to it.
1583 */
1584 if (unlikely(dbno == -1)) {
1585 /*
1586 * Not allowed to allocate, return failure.
1587 */
Barry Naujok6a178102008-05-21 16:42:05 +10001588 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
1589 args->total == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * Drop the freespace buffer unless it came from our
1592 * caller.
1593 */
1594 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1595 xfs_da_buf_done(fbp);
1596 return XFS_ERROR(ENOSPC);
1597 }
1598 /*
1599 * Allocate and initialize the new data block.
1600 */
1601 if (unlikely((error = xfs_dir2_grow_inode(args,
1602 XFS_DIR2_DATA_SPACE,
1603 &dbno)) ||
1604 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1605 /*
1606 * Drop the freespace buffer unless it came from our
1607 * caller.
1608 */
1609 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1610 xfs_da_buf_done(fbp);
1611 return error;
1612 }
1613 /*
1614 * If (somehow) we have a freespace block, get rid of it.
1615 */
1616 if (fbp)
1617 xfs_da_brelse(tp, fbp);
1618 if (fblk && fblk->bp)
1619 fblk->bp = NULL;
1620
1621 /*
1622 * Get the freespace block corresponding to the data block
1623 * that was just allocated.
1624 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001625 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001627 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 XFS_DATA_FORK))) {
1629 xfs_da_buf_done(dbp);
1630 return error;
1631 }
1632 /*
1633 * If there wasn't a freespace block, the read will
1634 * return a NULL fbp. Allocate and initialize a new one.
1635 */
1636 if( fbp == NULL ) {
1637 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1638 &fbno))) {
1639 return error;
1640 }
1641
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001642 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001643 xfs_alert(mp,
1644 "%s: dir ino " "%llu needed freesp block %lld for\n"
1645 " data block %lld, got %lld ifbno %llu lastfbno %d",
1646 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001647 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 (long long)dbno, (long long)fbno,
1649 (unsigned long long)ifbno, lastfbno);
1650 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001651 xfs_alert(mp,
1652 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 fblk,
1654 (unsigned long long)fblk->blkno,
1655 fblk->index,
1656 fblk->magic);
1657 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001658 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1661 XFS_ERRLEVEL_LOW, mp);
1662 return XFS_ERROR(EFSCORRUPTED);
1663 }
1664
1665 /*
1666 * Get a buffer for the new block.
1667 */
1668 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001669 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 -1, &fbp, XFS_DATA_FORK))) {
1671 return error;
1672 }
1673 ASSERT(fbp != NULL);
1674
1675 /*
1676 * Initialize the new block to be empty, and remember
1677 * its first slot as our empty slot.
1678 */
1679 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001680 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1681 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1683 XFS_DIR2_MAX_FREE_BESTS(mp));
1684 free->hdr.nvalid = 0;
1685 free->hdr.nused = 0;
1686 } else {
1687 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001688 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
1690
1691 /*
1692 * Set the freespace block index from the data block number.
1693 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001694 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * If it's after the end of the current entries in the
1697 * freespace block, extend that table.
1698 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001699 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001701 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * Tag new entry so nused will go up.
1704 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001705 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 /*
1708 * If this entry was for an empty data block
1709 * (this should always be true) then update the header.
1710 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001711 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001712 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 xfs_dir2_free_log_header(tp, fbp);
1714 }
1715 /*
1716 * Update the real value in the table.
1717 * We haven't allocated the data entry yet so this will
1718 * change again.
1719 */
1720 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001721 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 logfree = 1;
1723 }
1724 /*
1725 * We had a data block so we don't have to make a new one.
1726 */
1727 else {
1728 /*
1729 * If just checking, we succeeded.
1730 */
Barry Naujok6a178102008-05-21 16:42:05 +10001731 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1733 xfs_da_buf_done(fbp);
1734 return 0;
1735 }
1736 /*
1737 * Read the data block in.
1738 */
1739 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001740 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 -1, &dbp, XFS_DATA_FORK))) {
1742 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1743 xfs_da_buf_done(fbp);
1744 return error;
1745 }
1746 data = dbp->data;
1747 logfree = 0;
1748 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001749 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /*
1751 * Point to the existing unused space.
1752 */
1753 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001754 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 needscan = needlog = 0;
1756 /*
1757 * Mark the first part of the unused space, inuse for us.
1758 */
1759 xfs_dir2_data_use_free(tp, dbp, dup,
1760 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1761 &needlog, &needscan);
1762 /*
1763 * Fill in the new entry and log it.
1764 */
1765 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001766 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dep->namelen = args->namelen;
1768 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001769 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001770 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 xfs_dir2_data_log_entry(tp, dbp, dep);
1772 /*
1773 * Rescan the block for bestfree if needed.
1774 */
1775 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001776 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /*
1778 * Log the data block header if needed.
1779 */
1780 if (needlog)
1781 xfs_dir2_data_log_header(tp, dbp);
1782 /*
1783 * If the freespace entry is now wrong, update it.
1784 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001785 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001786 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 logfree = 1;
1788 }
1789 /*
1790 * Log the freespace entry if needed.
1791 */
1792 if (logfree)
1793 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1794 /*
1795 * If the caller didn't hand us the freespace block, drop it.
1796 */
1797 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1798 xfs_da_buf_done(fbp);
1799 /*
1800 * Return the data block and offset in args, then drop the data block.
1801 */
1802 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001803 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 xfs_da_buf_done(dbp);
1805 return 0;
1806}
1807
1808/*
1809 * Lookup an entry in a node-format directory.
1810 * All the real work happens in xfs_da_node_lookup_int.
1811 * The only real output is the inode number of the entry.
1812 */
1813int /* error */
1814xfs_dir2_node_lookup(
1815 xfs_da_args_t *args) /* operation arguments */
1816{
1817 int error; /* error return value */
1818 int i; /* btree level */
1819 int rval; /* operation return value */
1820 xfs_da_state_t *state; /* btree cursor */
1821
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001822 trace_xfs_dir2_node_lookup(args);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Allocate and initialize the btree cursor.
1826 */
1827 state = xfs_da_state_alloc();
1828 state->args = args;
1829 state->mp = args->dp->i_mount;
1830 state->blocksize = state->mp->m_dirblksize;
1831 state->node_ents = state->mp->m_dir_node_ents;
1832 /*
1833 * Fill in the path to the entry in the cursor.
1834 */
1835 error = xfs_da_node_lookup_int(state, &rval);
1836 if (error)
1837 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10001838 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
1839 /* If a CI match, dup the actual name and return EEXIST */
1840 xfs_dir2_data_entry_t *dep;
1841
1842 dep = (xfs_dir2_data_entry_t *)((char *)state->extrablk.bp->
1843 data + state->extrablk.index);
1844 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Release the btree blocks and leaf block.
1848 */
1849 for (i = 0; i < state->path.active; i++) {
1850 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1851 state->path.blk[i].bp = NULL;
1852 }
1853 /*
1854 * Release the data block if we have it.
1855 */
1856 if (state->extravalid && state->extrablk.bp) {
1857 xfs_da_brelse(args->trans, state->extrablk.bp);
1858 state->extrablk.bp = NULL;
1859 }
1860 xfs_da_state_free(state);
1861 return rval;
1862}
1863
1864/*
1865 * Remove an entry from a node-format directory.
1866 */
1867int /* error */
1868xfs_dir2_node_removename(
1869 xfs_da_args_t *args) /* operation arguments */
1870{
1871 xfs_da_state_blk_t *blk; /* leaf block */
1872 int error; /* error return value */
1873 int rval; /* operation return value */
1874 xfs_da_state_t *state; /* btree cursor */
1875
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001876 trace_xfs_dir2_node_removename(args);
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /*
1879 * Allocate and initialize the btree cursor.
1880 */
1881 state = xfs_da_state_alloc();
1882 state->args = args;
1883 state->mp = args->dp->i_mount;
1884 state->blocksize = state->mp->m_dirblksize;
1885 state->node_ents = state->mp->m_dir_node_ents;
1886 /*
1887 * Look up the entry we're deleting, set up the cursor.
1888 */
1889 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001890 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * Didn't find it, upper layer screwed up.
1894 */
1895 if (rval != EEXIST) {
1896 xfs_da_state_free(state);
1897 return rval;
1898 }
1899 blk = &state->path.blk[state->path.active - 1];
1900 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1901 ASSERT(state->extravalid);
1902 /*
1903 * Remove the leaf and data entries.
1904 * Extrablk refers to the data block.
1905 */
1906 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1907 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001908 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
1911 * Fix the hash values up the btree.
1912 */
1913 xfs_da_fixhashpath(state, &state->path);
1914 /*
1915 * If we need to join leaf blocks, do it.
1916 */
1917 if (rval && state->path.active > 1)
1918 error = xfs_da_join(state);
1919 /*
1920 * If no errors so far, try conversion to leaf format.
1921 */
1922 if (!error)
1923 error = xfs_dir2_node_to_leaf(state);
1924 xfs_da_state_free(state);
1925 return error;
1926}
1927
1928/*
1929 * Replace an entry's inode number in a node-format directory.
1930 */
1931int /* error */
1932xfs_dir2_node_replace(
1933 xfs_da_args_t *args) /* operation arguments */
1934{
1935 xfs_da_state_blk_t *blk; /* leaf block */
1936 xfs_dir2_data_t *data; /* data block structure */
1937 xfs_dir2_data_entry_t *dep; /* data entry changed */
1938 int error; /* error return value */
1939 int i; /* btree level */
1940 xfs_ino_t inum; /* new inode number */
1941 xfs_dir2_leaf_t *leaf; /* leaf structure */
1942 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1943 int rval; /* internal return value */
1944 xfs_da_state_t *state; /* btree cursor */
1945
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001946 trace_xfs_dir2_node_replace(args);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /*
1949 * Allocate and initialize the btree cursor.
1950 */
1951 state = xfs_da_state_alloc();
1952 state->args = args;
1953 state->mp = args->dp->i_mount;
1954 state->blocksize = state->mp->m_dirblksize;
1955 state->node_ents = state->mp->m_dir_node_ents;
1956 inum = args->inumber;
1957 /*
1958 * Lookup the entry to change in the btree.
1959 */
1960 error = xfs_da_node_lookup_int(state, &rval);
1961 if (error) {
1962 rval = error;
1963 }
1964 /*
1965 * It should be found, since the vnodeops layer has looked it up
1966 * and locked it. But paranoia is good.
1967 */
1968 if (rval == EEXIST) {
1969 /*
1970 * Find the leaf entry.
1971 */
1972 blk = &state->path.blk[state->path.active - 1];
1973 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1974 leaf = blk->bp->data;
1975 lep = &leaf->ents[blk->index];
1976 ASSERT(state->extravalid);
1977 /*
1978 * Point to the data entry.
1979 */
1980 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001981 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 dep = (xfs_dir2_data_entry_t *)
1983 ((char *)data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001984 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001985 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /*
1987 * Fill in the new inode number and log the entry.
1988 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001989 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1991 rval = 0;
1992 }
1993 /*
1994 * Didn't find it, and we're holding a data block. Drop it.
1995 */
1996 else if (state->extravalid) {
1997 xfs_da_brelse(args->trans, state->extrablk.bp);
1998 state->extrablk.bp = NULL;
1999 }
2000 /*
2001 * Release all the buffers in the cursor.
2002 */
2003 for (i = 0; i < state->path.active; i++) {
2004 xfs_da_brelse(args->trans, state->path.blk[i].bp);
2005 state->path.blk[i].bp = NULL;
2006 }
2007 xfs_da_state_free(state);
2008 return rval;
2009}
2010
2011/*
2012 * Trim off a trailing empty freespace block.
2013 * Return (in rvalp) 1 if we did it, 0 if not.
2014 */
2015int /* error */
2016xfs_dir2_node_trim_free(
2017 xfs_da_args_t *args, /* operation arguments */
2018 xfs_fileoff_t fo, /* free block number */
2019 int *rvalp) /* out: did something */
2020{
2021 xfs_dabuf_t *bp; /* freespace buffer */
2022 xfs_inode_t *dp; /* incore directory inode */
2023 int error; /* error return code */
2024 xfs_dir2_free_t *free; /* freespace structure */
2025 xfs_mount_t *mp; /* filesystem mount point */
2026 xfs_trans_t *tp; /* transaction pointer */
2027
2028 dp = args->dp;
2029 mp = dp->i_mount;
2030 tp = args->trans;
2031 /*
2032 * Read the freespace block.
2033 */
2034 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2035 XFS_DATA_FORK))) {
2036 return error;
2037 }
2038
2039 /*
2040 * There can be holes in freespace. If fo is a hole, there's
2041 * nothing to do.
2042 */
2043 if (bp == NULL) {
2044 return 0;
2045 }
2046 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11002047 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
2049 * If there are used entries, there's nothing to do.
2050 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11002051 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_da_brelse(tp, bp);
2053 *rvalp = 0;
2054 return 0;
2055 }
2056 /*
2057 * Blow the block away.
2058 */
2059 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002060 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 bp))) {
2062 /*
2063 * Can't fail with ENOSPC since that only happens with no
2064 * space reservation, when breaking up an extent into two
2065 * pieces. This is the last block of an extent.
2066 */
2067 ASSERT(error != ENOSPC);
2068 xfs_da_brelse(tp, bp);
2069 return error;
2070 }
2071 /*
2072 * Return that we succeeded.
2073 */
2074 *rvalp = 1;
2075 return 0;
2076}