blob: fedf8f976a1078f24415367a4c4b6e46803be570 [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"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
34#include "xfs_inode.h"
35#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_dir2_data.h"
37#include "xfs_dir2_leaf.h"
38#include "xfs_dir2_block.h"
39#include "xfs_dir2_node.h"
40#include "xfs_dir2_trace.h"
41#include "xfs_error.h"
42
43/*
44 * Function declarations.
45 */
46static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
47static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
48#ifdef DEBUG
49static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
50#else
51#define xfs_dir2_leafn_check(dp, bp)
52#endif
53static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
54 int start_s, xfs_dabuf_t *bp_d, int start_d,
55 int count);
56static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
57 xfs_da_state_blk_t *blk1,
58 xfs_da_state_blk_t *blk2);
59static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
60 int index, xfs_da_state_blk_t *dblk,
61 int *rval);
62static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
63 xfs_da_state_blk_t *fblk);
64
65/*
66 * Log entries from a freespace block.
67 */
68void
69xfs_dir2_free_log_bests(
70 xfs_trans_t *tp, /* transaction pointer */
71 xfs_dabuf_t *bp, /* freespace buffer */
72 int first, /* first entry to log */
73 int last) /* last entry to log */
74{
75 xfs_dir2_free_t *free; /* freespace structure */
76
77 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110078 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 xfs_da_log_buf(tp, bp,
80 (uint)((char *)&free->bests[first] - (char *)free),
81 (uint)((char *)&free->bests[last] - (char *)free +
82 sizeof(free->bests[0]) - 1));
83}
84
85/*
86 * Log header from a freespace block.
87 */
88static void
89xfs_dir2_free_log_header(
90 xfs_trans_t *tp, /* transaction pointer */
91 xfs_dabuf_t *bp) /* freespace buffer */
92{
93 xfs_dir2_free_t *free; /* freespace structure */
94
95 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110096 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
98 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
99}
100
101/*
102 * Convert a leaf-format directory to a node-format directory.
103 * We need to change the magic number of the leaf block, and copy
104 * the freespace table out of the leaf block into its own block.
105 */
106int /* error */
107xfs_dir2_leaf_to_node(
108 xfs_da_args_t *args, /* operation arguments */
109 xfs_dabuf_t *lbp) /* leaf buffer */
110{
111 xfs_inode_t *dp; /* incore directory inode */
112 int error; /* error return value */
113 xfs_dabuf_t *fbp; /* freespace buffer */
114 xfs_dir2_db_t fdb; /* freespace block number */
115 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100116 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 int i; /* leaf freespace index */
118 xfs_dir2_leaf_t *leaf; /* leaf structure */
119 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
120 xfs_mount_t *mp; /* filesystem mount point */
121 int n; /* count of live freespc ents */
122 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100123 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 xfs_trans_t *tp; /* transaction pointer */
125
126 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
127 dp = args->dp;
128 mp = dp->i_mount;
129 tp = args->trans;
130 /*
131 * Add a freespace block to the directory.
132 */
133 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
134 return error;
135 }
136 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
137 /*
138 * Get the buffer for the new freespace block.
139 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000140 if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 XFS_DATA_FORK))) {
142 return error;
143 }
144 ASSERT(fbp != NULL);
145 free = fbp->data;
146 leaf = lbp->data;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000147 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /*
149 * Initialize the freespace block header.
150 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100151 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100153 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100154 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /*
156 * Copy freespace entries from the leaf block to the new block.
157 * Count active entries.
158 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000159 for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100160 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100161 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100163 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100165 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100166 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /*
168 * Log everything.
169 */
170 xfs_dir2_leaf_log_header(tp, lbp);
171 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100172 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_da_buf_done(fbp);
174 xfs_dir2_leafn_check(dp, lbp);
175 return 0;
176}
177
178/*
179 * Add a leaf entry to a leaf block in a node-form directory.
180 * The other work necessary is done from the caller.
181 */
182static int /* error */
183xfs_dir2_leafn_add(
184 xfs_dabuf_t *bp, /* leaf buffer */
185 xfs_da_args_t *args, /* operation arguments */
186 int index) /* insertion pt for new entry */
187{
188 int compact; /* compacting stale leaves */
189 xfs_inode_t *dp; /* incore directory inode */
190 int highstale; /* next stale entry */
191 xfs_dir2_leaf_t *leaf; /* leaf structure */
192 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
193 int lfloghigh; /* high leaf entry logging */
194 int lfloglow; /* low leaf entry logging */
195 int lowstale; /* previous stale entry */
196 xfs_mount_t *mp; /* filesystem mount point */
197 xfs_trans_t *tp; /* transaction pointer */
198
199 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
200 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
229 if (args->justcheck)
230 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:
518 ASSERT(args->oknoent);
519 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 */
552 int di; /* data entry index */
553 int index; /* leaf entry index */
554 xfs_dir2_leaf_t *leaf; /* leaf structure */
555 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
556 xfs_mount_t *mp; /* filesystem mount point */
557 xfs_dir2_db_t newdb; /* new data block number */
558 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000559 enum xfs_dacmp cmp; /* comparison result */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000560
561 dp = args->dp;
562 tp = args->trans;
563 mp = dp->i_mount;
564 leaf = bp->data;
565 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
566#ifdef __KERNEL__
567 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
568#endif
569 xfs_dir2_leafn_check(dp, bp);
570 /*
571 * Look up the hash value in the leaf entries.
572 */
573 index = xfs_dir2_leaf_search_hash(args, bp);
574 /*
575 * Do we have a buffer coming in?
576 */
577 if (state->extravalid) {
578 curbp = state->extrablk.bp;
579 curdb = state->extrablk.blkno;
580 }
581 /*
582 * Loop over leaf entries with the right hash value.
583 */
584 for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) &&
585 be32_to_cpu(lep->hashval) == args->hashval;
586 lep++, index++) {
587 /*
588 * Skip stale leaf entries.
589 */
590 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
591 continue;
592 /*
593 * Pull the data block number from the entry.
594 */
595 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
596 /*
597 * Not adding a new entry, so we really want to find
598 * the name given to us.
599 *
600 * If it's a different data block, go get it.
601 */
602 if (newdb != curdb) {
603 /*
604 * If we had a block before, drop it.
605 */
606 if (curbp)
607 xfs_da_brelse(tp, curbp);
608 /*
609 * Read the data block.
610 */
611 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp,
612 newdb), -1, &curbp, XFS_DATA_FORK);
613 if (error)
614 return error;
615 xfs_dir2_data_check(dp, curbp);
616 curdb = newdb;
617 }
618 /*
619 * Point to the data entry.
620 */
621 dep = (xfs_dir2_data_entry_t *)((char *)curbp->data +
622 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
623 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000624 * Compare the entry and if it's an exact match, return
625 * EEXIST immediately. If it's the first case-insensitive
626 * match, store the inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 */
Barry Naujok5163f952008-05-21 16:41:01 +1000628 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
629 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
630 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 args->inumber = be64_to_cpu(dep->inumber);
632 di = (int)((char *)dep - (char *)curbp->data);
633 error = EEXIST;
Barry Naujok5163f952008-05-21 16:41:01 +1000634 if (cmp == XFS_CMP_EXACT)
635 goto out;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000636 }
637 }
Barry Naujok5163f952008-05-21 16:41:01 +1000638 /* Didn't find an exact match. */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 error = ENOENT;
640 di = -1;
641 ASSERT(index == be16_to_cpu(leaf->hdr.count) || args->oknoent);
642out:
643 if (curbp) {
644 /* Giving back a data block. */
645 state->extravalid = 1;
646 state->extrablk.bp = curbp;
647 state->extrablk.index = di;
648 state->extrablk.blkno = curdb;
649 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
650 } else {
651 state->extravalid = 0;
652 }
653 /*
654 * Return the index, that will be the insertion point.
655 */
656 *indexp = index;
657 return XFS_ERROR(error);
658}
659
660/*
661 * Look up a leaf entry in a node-format leaf block.
662 * If this is an addname then the extrablk in state is a freespace block,
663 * otherwise it's a data block.
664 */
665int
666xfs_dir2_leafn_lookup_int(
667 xfs_dabuf_t *bp, /* leaf buffer */
668 xfs_da_args_t *args, /* operation arguments */
669 int *indexp, /* out: leaf entry index */
670 xfs_da_state_t *state) /* state to fill in */
671{
672 if (args->addname)
673 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
674 state);
675 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
676}
677
678/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * Move count leaf entries from source to destination leaf.
680 * Log entries and headers. Stale entries are preserved.
681 */
682static void
683xfs_dir2_leafn_moveents(
684 xfs_da_args_t *args, /* operation arguments */
685 xfs_dabuf_t *bp_s, /* source leaf buffer */
686 int start_s, /* source leaf index */
687 xfs_dabuf_t *bp_d, /* destination leaf buffer */
688 int start_d, /* destination leaf index */
689 int count) /* count of leaves to copy */
690{
691 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
692 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
693 int stale; /* count stale leaves copied */
694 xfs_trans_t *tp; /* transaction pointer */
695
696 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
697 start_d, count);
698 /*
699 * Silently return if nothing to do.
700 */
701 if (count == 0) {
702 return;
703 }
704 tp = args->trans;
705 leaf_s = bp_s->data;
706 leaf_d = bp_d->data;
707 /*
708 * If the destination index is not the end of the current
709 * destination leaf entries, open up a hole in the destination
710 * to hold the new entries.
711 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100712 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100714 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 sizeof(xfs_dir2_leaf_entry_t));
716 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100717 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719 /*
720 * If the source has stale leaves, count the ones in the copy range
721 * so we can update the header correctly.
722 */
723 if (leaf_s->hdr.stale) {
724 int i; /* temp leaf index */
725
726 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100727 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 stale++;
729 }
730 } else
731 stale = 0;
732 /*
733 * Copy the leaf entries from source to destination.
734 */
735 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
736 count * sizeof(xfs_dir2_leaf_entry_t));
737 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
738 /*
739 * If there are source entries after the ones we copied,
740 * delete the ones we copied by sliding the next ones down.
741 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100742 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
744 count * sizeof(xfs_dir2_leaf_entry_t));
745 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
746 }
747 /*
748 * Update the headers and log them.
749 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800750 be16_add_cpu(&leaf_s->hdr.count, -(count));
751 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
752 be16_add_cpu(&leaf_d->hdr.count, count);
753 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 xfs_dir2_leaf_log_header(tp, bp_s);
755 xfs_dir2_leaf_log_header(tp, bp_d);
756 xfs_dir2_leafn_check(args->dp, bp_s);
757 xfs_dir2_leafn_check(args->dp, bp_d);
758}
759
760/*
761 * Determine the sort order of two leaf blocks.
762 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
763 */
764int /* sort order */
765xfs_dir2_leafn_order(
766 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
767 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
768{
769 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
770 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
771
772 leaf1 = leaf1_bp->data;
773 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100774 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
775 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100776 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
777 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100778 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
779 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
780 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 1;
782 return 0;
783}
784
785/*
786 * Rebalance leaf entries between two leaf blocks.
787 * This is actually only called when the second block is new,
788 * though the code deals with the general case.
789 * A new entry will be inserted in one of the blocks, and that
790 * entry is taken into account when balancing.
791 */
792static void
793xfs_dir2_leafn_rebalance(
794 xfs_da_state_t *state, /* btree cursor */
795 xfs_da_state_blk_t *blk1, /* first btree block */
796 xfs_da_state_blk_t *blk2) /* second btree block */
797{
798 xfs_da_args_t *args; /* operation arguments */
799 int count; /* count (& direction) leaves */
800 int isleft; /* new goes in left leaf */
801 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
802 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
803 int mid; /* midpoint leaf index */
804#ifdef DEBUG
805 int oldstale; /* old count of stale leaves */
806#endif
807 int oldsum; /* old total leaf count */
808 int swap; /* swapped leaf blocks */
809
810 args = state->args;
811 /*
812 * If the block order is wrong, swap the arguments.
813 */
814 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
815 xfs_da_state_blk_t *tmp; /* temp for block swap */
816
817 tmp = blk1;
818 blk1 = blk2;
819 blk2 = tmp;
820 }
821 leaf1 = blk1->bp->data;
822 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100823 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100825 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827 mid = oldsum >> 1;
828 /*
829 * If the old leaf count was odd then the new one will be even,
830 * so we need to divide the new count evenly.
831 */
832 if (oldsum & 1) {
833 xfs_dahash_t midhash; /* middle entry hash value */
834
Nathan Scotta818e5d2006-03-17 17:28:07 +1100835 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100836 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100838 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 isleft = args->hashval <= midhash;
840 }
841 /*
842 * If the old count is even then the new count is odd, so there's
843 * no preferred side for the new entry.
844 * Pick the left one.
845 */
846 else
847 isleft = 1;
848 /*
849 * Calculate moved entry count. Positive means left-to-right,
850 * negative means right-to-left. Then move the entries.
851 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100852 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (count > 0)
854 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100855 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 else if (count < 0)
857 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100858 be16_to_cpu(leaf1->hdr.count), count);
859 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
860 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
862 * Mark whether we're inserting into the old or new leaf.
863 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100864 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100866 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 state->inleaf = !swap;
868 else
869 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100870 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /*
872 * Adjust the expected index for insertion.
873 */
874 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100875 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000876
877 /*
878 * Finally sanity check just to make sure we are not returning a
879 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
881 if(blk2->index < 0) {
882 state->inleaf = 1;
883 blk2->index = 0;
884 cmn_err(CE_ALERT,
Nathan Scottc41564b2006-03-29 08:55:14 +1000885 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 "blk1->index %d\n",
887 blk1->index);
888 }
889}
890
891/*
892 * Remove an entry from a node directory.
893 * This removes the leaf entry and the data entry,
894 * and updates the free block if necessary.
895 */
896static int /* error */
897xfs_dir2_leafn_remove(
898 xfs_da_args_t *args, /* operation arguments */
899 xfs_dabuf_t *bp, /* leaf buffer */
900 int index, /* leaf entry index */
901 xfs_da_state_blk_t *dblk, /* data block */
902 int *rval) /* resulting block needs join */
903{
904 xfs_dir2_data_t *data; /* data block structure */
905 xfs_dir2_db_t db; /* data block number */
906 xfs_dabuf_t *dbp; /* data block buffer */
907 xfs_dir2_data_entry_t *dep; /* data block entry */
908 xfs_inode_t *dp; /* incore directory inode */
909 xfs_dir2_leaf_t *leaf; /* leaf structure */
910 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
911 int longest; /* longest data free entry */
912 int off; /* data block entry offset */
913 xfs_mount_t *mp; /* filesystem mount point */
914 int needlog; /* need to log data header */
915 int needscan; /* need to rescan data frees */
916 xfs_trans_t *tp; /* transaction pointer */
917
918 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
919 dp = args->dp;
920 tp = args->trans;
921 mp = dp->i_mount;
922 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100923 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 /*
925 * Point to the entry we're removing.
926 */
927 lep = &leaf->ents[index];
928 /*
929 * Extract the data block and offset from the entry.
930 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000931 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000933 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 ASSERT(dblk->index == off);
935 /*
936 * Kill the leaf entry by marking it stale.
937 * Log the leaf block changes.
938 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800939 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100941 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 xfs_dir2_leaf_log_ents(tp, bp, index, index);
943 /*
944 * Make the data entry free. Keep track of the longest freespace
945 * in the data block in case it changes.
946 */
947 dbp = dblk->bp;
948 data = dbp->data;
949 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100950 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 needlog = needscan = 0;
952 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000953 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /*
955 * Rescan the data block freespaces for bestfree.
956 * Log the data block header if needed.
957 */
958 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000959 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (needlog)
961 xfs_dir2_data_log_header(tp, dbp);
962 xfs_dir2_data_check(dp, dbp);
963 /*
964 * If the longest data block freespace changes, need to update
965 * the corresponding freeblock entry.
966 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100967 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int error; /* error return value */
969 xfs_dabuf_t *fbp; /* freeblock buffer */
970 xfs_dir2_db_t fdb; /* freeblock block number */
971 int findex; /* index in freeblock entries */
972 xfs_dir2_free_t *free; /* freeblock structure */
973 int logfree; /* need to log free entry */
974
975 /*
976 * Convert the data block number to a free block,
977 * read in the free block.
978 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000979 fdb = xfs_dir2_db_to_fdb(mp, db);
980 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 -1, &fbp, XFS_DATA_FORK))) {
982 return error;
983 }
984 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100985 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
986 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 XFS_DIR2_MAX_FREE_BESTS(mp) *
988 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
989 /*
990 * Calculate which entry we need to fix.
991 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000992 findex = xfs_dir2_db_to_fdindex(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +1100993 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * If the data block is now empty we can get rid of it
996 * (usually).
997 */
998 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
999 /*
1000 * Try to punch out the data block.
1001 */
1002 error = xfs_dir2_shrink_inode(args, db, dbp);
1003 if (error == 0) {
1004 dblk->bp = NULL;
1005 data = NULL;
1006 }
1007 /*
1008 * We can get ENOSPC if there's no space reservation.
1009 * In this case just drop the buffer and some one else
1010 * will eventually get rid of the empty block.
1011 */
1012 else if (error == ENOSPC && args->total == 0)
1013 xfs_da_buf_done(dbp);
1014 else
1015 return error;
1016 }
1017 /*
1018 * If we got rid of the data block, we can eliminate that entry
1019 * in the free block.
1020 */
1021 if (data == NULL) {
1022 /*
1023 * One less used entry in the free table.
1024 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001025 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 xfs_dir2_free_log_header(tp, fbp);
1027 /*
1028 * If this was the last entry in the table, we can
1029 * trim the table size back. There might be other
1030 * entries at the end referring to non-existent
1031 * data blocks, get those too.
1032 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001033 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int i; /* free entry index */
1035
1036 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001037 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 i--)
1039 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001040 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 logfree = 0;
1042 }
1043 /*
1044 * Not the last entry, just punch it out.
1045 */
1046 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001047 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 logfree = 1;
1049 }
1050 /*
1051 * If there are no useful entries left in the block,
1052 * get rid of the block if we can.
1053 */
1054 if (!free->hdr.nused) {
1055 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1056 if (error == 0) {
1057 fbp = NULL;
1058 logfree = 0;
1059 } else if (error != ENOSPC || args->total != 0)
1060 return error;
1061 /*
1062 * It's possible to get ENOSPC if there is no
1063 * space reservation. In this case some one
1064 * else will eventually get rid of this block.
1065 */
1066 }
1067 }
1068 /*
1069 * Data block is not empty, just set the free entry to
1070 * the new value.
1071 */
1072 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001073 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 logfree = 1;
1075 }
1076 /*
1077 * Log the free entry that changed, unless we got rid of it.
1078 */
1079 if (logfree)
1080 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1081 /*
1082 * Drop the buffer if we still have it.
1083 */
1084 if (fbp)
1085 xfs_da_buf_done(fbp);
1086 }
1087 xfs_dir2_leafn_check(dp, bp);
1088 /*
1089 * Return indication of whether this leaf block is emtpy enough
1090 * to justify trying to join it with a neighbor.
1091 */
1092 *rval =
1093 ((uint)sizeof(leaf->hdr) +
1094 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001095 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 mp->m_dir_magicpct;
1097 return 0;
1098}
1099
1100/*
1101 * Split the leaf entries in the old block into old and new blocks.
1102 */
1103int /* error */
1104xfs_dir2_leafn_split(
1105 xfs_da_state_t *state, /* btree cursor */
1106 xfs_da_state_blk_t *oldblk, /* original block */
1107 xfs_da_state_blk_t *newblk) /* newly created block */
1108{
1109 xfs_da_args_t *args; /* operation arguments */
1110 xfs_dablk_t blkno; /* new leaf block number */
1111 int error; /* error return value */
1112 xfs_mount_t *mp; /* filesystem mount point */
1113
1114 /*
1115 * Allocate space for a new leaf node.
1116 */
1117 args = state->args;
1118 mp = args->dp->i_mount;
1119 ASSERT(args != NULL);
1120 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1121 error = xfs_da_grow_inode(args, &blkno);
1122 if (error) {
1123 return error;
1124 }
1125 /*
1126 * Initialize the new leaf block.
1127 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001128 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1130 if (error) {
1131 return error;
1132 }
1133 newblk->blkno = blkno;
1134 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1135 /*
1136 * Rebalance the entries across the two leaves, link the new
1137 * block into the leaves.
1138 */
1139 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1140 error = xfs_da_blk_link(state, oldblk, newblk);
1141 if (error) {
1142 return error;
1143 }
1144 /*
1145 * Insert the new entry in the correct block.
1146 */
1147 if (state->inleaf)
1148 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1149 else
1150 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1151 /*
1152 * Update last hashval in each block since we added the name.
1153 */
1154 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1155 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1156 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1157 xfs_dir2_leafn_check(args->dp, newblk->bp);
1158 return error;
1159}
1160
1161/*
1162 * Check a leaf block and its neighbors to see if the block should be
1163 * collapsed into one or the other neighbor. Always keep the block
1164 * with the smaller block number.
1165 * If the current block is over 50% full, don't try to join it, return 0.
1166 * If the block is empty, fill in the state structure and return 2.
1167 * If it can be collapsed, fill in the state structure and return 1.
1168 * If nothing can be done, return 0.
1169 */
1170int /* error */
1171xfs_dir2_leafn_toosmall(
1172 xfs_da_state_t *state, /* btree cursor */
1173 int *action) /* resulting action to take */
1174{
1175 xfs_da_state_blk_t *blk; /* leaf block */
1176 xfs_dablk_t blkno; /* leaf block number */
1177 xfs_dabuf_t *bp; /* leaf buffer */
1178 int bytes; /* bytes in use */
1179 int count; /* leaf live entry count */
1180 int error; /* error return value */
1181 int forward; /* sibling block direction */
1182 int i; /* sibling counter */
1183 xfs_da_blkinfo_t *info; /* leaf block header */
1184 xfs_dir2_leaf_t *leaf; /* leaf structure */
1185 int rval; /* result from path_shift */
1186
1187 /*
1188 * Check for the degenerate case of the block being over 50% full.
1189 * If so, it's not worth even looking to see if we might be able
1190 * to coalesce with a sibling.
1191 */
1192 blk = &state->path.blk[state->path.active - 1];
1193 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001194 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001196 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1198 if (bytes > (state->blocksize >> 1)) {
1199 /*
1200 * Blk over 50%, don't try to join.
1201 */
1202 *action = 0;
1203 return 0;
1204 }
1205 /*
1206 * Check for the degenerate case of the block being empty.
1207 * If the block is empty, we'll simply delete it, no need to
1208 * coalesce it with a sibling block. We choose (arbitrarily)
1209 * to merge with the forward block unless it is NULL.
1210 */
1211 if (count == 0) {
1212 /*
1213 * Make altpath point to the block we want to keep and
1214 * path point to the block we want to drop (this one).
1215 */
Nathan Scott89da0542006-03-17 17:28:40 +11001216 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 memcpy(&state->altpath, &state->path, sizeof(state->path));
1218 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1219 &rval);
1220 if (error)
1221 return error;
1222 *action = rval ? 2 : 0;
1223 return 0;
1224 }
1225 /*
1226 * Examine each sibling block to see if we can coalesce with
1227 * at least 25% free space to spare. We need to figure out
1228 * whether to merge with the forward or the backward block.
1229 * We prefer coalescing with the lower numbered sibling so as
1230 * to shrink a directory over time.
1231 */
Nathan Scott89da0542006-03-17 17:28:40 +11001232 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001234 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (blkno == 0)
1236 continue;
1237 /*
1238 * Read the sibling leaf block.
1239 */
1240 if ((error =
1241 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1242 -1, &bp, XFS_DATA_FORK))) {
1243 return error;
1244 }
1245 ASSERT(bp != NULL);
1246 /*
1247 * Count bytes in the two blocks combined.
1248 */
1249 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001250 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 bytes = state->blocksize - (state->blocksize >> 2);
1252 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001253 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001254 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 bytes -= count * (uint)sizeof(leaf->ents[0]);
1256 /*
1257 * Fits with at least 25% to spare.
1258 */
1259 if (bytes >= 0)
1260 break;
1261 xfs_da_brelse(state->args->trans, bp);
1262 }
1263 /*
1264 * Didn't like either block, give up.
1265 */
1266 if (i >= 2) {
1267 *action = 0;
1268 return 0;
1269 }
1270 /*
1271 * Done with the sibling leaf block here, drop the dabuf
1272 * so path_shift can get it.
1273 */
1274 xfs_da_buf_done(bp);
1275 /*
1276 * Make altpath point to the block we want to keep (the lower
1277 * numbered block) and path point to the block we want to drop.
1278 */
1279 memcpy(&state->altpath, &state->path, sizeof(state->path));
1280 if (blkno < blk->blkno)
1281 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1282 &rval);
1283 else
1284 error = xfs_da_path_shift(state, &state->path, forward, 0,
1285 &rval);
1286 if (error) {
1287 return error;
1288 }
1289 *action = rval ? 0 : 1;
1290 return 0;
1291}
1292
1293/*
1294 * Move all the leaf entries from drop_blk to save_blk.
1295 * This is done as part of a join operation.
1296 */
1297void
1298xfs_dir2_leafn_unbalance(
1299 xfs_da_state_t *state, /* cursor */
1300 xfs_da_state_blk_t *drop_blk, /* dead block */
1301 xfs_da_state_blk_t *save_blk) /* surviving block */
1302{
1303 xfs_da_args_t *args; /* operation arguments */
1304 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1305 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1306
1307 args = state->args;
1308 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1309 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1310 drop_leaf = drop_blk->bp->data;
1311 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001312 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1313 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 /*
1315 * If there are any stale leaf entries, take this opportunity
1316 * to purge them.
1317 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001318 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001320 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 xfs_dir2_leaf_compact(args, save_blk->bp);
1322 /*
1323 * Move the entries from drop to the appropriate end of save.
1324 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001325 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 -07001326 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1327 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001328 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 else
1330 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001331 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001332 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 -07001333 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1334}
1335
1336/*
1337 * Top-level node form directory addname routine.
1338 */
1339int /* error */
1340xfs_dir2_node_addname(
1341 xfs_da_args_t *args) /* operation arguments */
1342{
1343 xfs_da_state_blk_t *blk; /* leaf block for insert */
1344 int error; /* error return value */
1345 int rval; /* sub-return value */
1346 xfs_da_state_t *state; /* btree cursor */
1347
1348 xfs_dir2_trace_args("node_addname", args);
1349 /*
1350 * Allocate and initialize the state (btree cursor).
1351 */
1352 state = xfs_da_state_alloc();
1353 state->args = args;
1354 state->mp = args->dp->i_mount;
1355 state->blocksize = state->mp->m_dirblksize;
1356 state->node_ents = state->mp->m_dir_node_ents;
1357 /*
1358 * Look up the name. We're not supposed to find it, but
1359 * this gives us the insertion point.
1360 */
1361 error = xfs_da_node_lookup_int(state, &rval);
1362 if (error)
1363 rval = error;
1364 if (rval != ENOENT) {
1365 goto done;
1366 }
1367 /*
1368 * Add the data entry to a data block.
1369 * Extravalid is set to a freeblock found by lookup.
1370 */
1371 rval = xfs_dir2_node_addname_int(args,
1372 state->extravalid ? &state->extrablk : NULL);
1373 if (rval) {
1374 goto done;
1375 }
1376 blk = &state->path.blk[state->path.active - 1];
1377 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1378 /*
1379 * Add the new leaf entry.
1380 */
1381 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1382 if (rval == 0) {
1383 /*
1384 * It worked, fix the hash values up the btree.
1385 */
1386 if (!args->justcheck)
1387 xfs_da_fixhashpath(state, &state->path);
1388 } else {
1389 /*
1390 * It didn't work, we need to split the leaf block.
1391 */
1392 if (args->total == 0) {
1393 ASSERT(rval == ENOSPC);
1394 goto done;
1395 }
1396 /*
1397 * Split the leaf block and insert the new entry.
1398 */
1399 rval = xfs_da_split(state);
1400 }
1401done:
1402 xfs_da_state_free(state);
1403 return rval;
1404}
1405
1406/*
1407 * Add the data entry for a node-format directory name addition.
1408 * The leaf entry is added in xfs_dir2_leafn_add.
1409 * We may enter with a freespace block that the lookup found.
1410 */
1411static int /* error */
1412xfs_dir2_node_addname_int(
1413 xfs_da_args_t *args, /* operation arguments */
1414 xfs_da_state_blk_t *fblk) /* optional freespace block */
1415{
1416 xfs_dir2_data_t *data; /* data block structure */
1417 xfs_dir2_db_t dbno; /* data block number */
1418 xfs_dabuf_t *dbp; /* data block buffer */
1419 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1420 xfs_inode_t *dp; /* incore directory inode */
1421 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1422 int error; /* error return value */
1423 xfs_dir2_db_t fbno; /* freespace block number */
1424 xfs_dabuf_t *fbp; /* freespace buffer */
1425 int findex; /* freespace entry index */
1426 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1427 xfs_dir2_db_t ifbno; /* initial freespace block no */
1428 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1429 int length; /* length of the new entry */
1430 int logfree; /* need to log free entry */
1431 xfs_mount_t *mp; /* filesystem mount point */
1432 int needlog; /* need to log data header */
1433 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001434 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 xfs_trans_t *tp; /* transaction pointer */
1436
1437 dp = args->dp;
1438 mp = dp->i_mount;
1439 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001440 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /*
1442 * If we came in with a freespace block that means that lookup
1443 * found an entry with our hash value. This is the freespace
1444 * block for that data entry.
1445 */
1446 if (fblk) {
1447 fbp = fblk->bp;
1448 /*
1449 * Remember initial freespace block number.
1450 */
1451 ifbno = fblk->blkno;
1452 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001453 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 findex = fblk->index;
1455 /*
1456 * This means the free entry showed that the data block had
1457 * space for our entry, so we remembered it.
1458 * Use that data block.
1459 */
1460 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001461 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1462 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1463 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1464 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 /*
1467 * The data block looked at didn't have enough room.
1468 * We'll start at the beginning of the freespace entries.
1469 */
1470 else {
1471 dbno = -1;
1472 findex = 0;
1473 }
1474 }
1475 /*
1476 * Didn't come in with a freespace block, so don't have a data block.
1477 */
1478 else {
1479 ifbno = dbno = -1;
1480 fbp = NULL;
1481 findex = 0;
1482 }
1483 /*
1484 * If we don't have a data block yet, we're going to scan the
1485 * freespace blocks looking for one. Figure out what the
1486 * highest freespace block number is.
1487 */
1488 if (dbno == -1) {
1489 xfs_fileoff_t fo; /* freespace block number */
1490
1491 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1492 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001493 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 fbno = ifbno;
1495 }
1496 /*
1497 * While we haven't identified a data block, search the freeblock
1498 * data for a good data block. If we find a null freeblock entry,
1499 * indicating a hole in the data blocks, remember that.
1500 */
1501 while (dbno == -1) {
1502 /*
1503 * If we don't have a freeblock in hand, get the next one.
1504 */
1505 if (fbp == NULL) {
1506 /*
1507 * Happens the first time through unless lookup gave
1508 * us a freespace block to start with.
1509 */
1510 if (++fbno == 0)
1511 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1512 /*
1513 * If it's ifbno we already looked at it.
1514 */
1515 if (fbno == ifbno)
1516 fbno++;
1517 /*
1518 * If it's off the end we're done.
1519 */
1520 if (fbno >= lastfbno)
1521 break;
1522 /*
1523 * Read the block. There can be holes in the
1524 * freespace blocks, so this might not succeed.
1525 * This should be really rare, so there's no reason
1526 * to avoid it.
1527 */
1528 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001529 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 XFS_DATA_FORK))) {
1531 return error;
1532 }
1533 if (unlikely(fbp == NULL)) {
1534 continue;
1535 }
1536 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001537 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 findex = 0;
1539 }
1540 /*
1541 * Look at the current free entry. Is it good enough?
1542 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001543 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1544 be16_to_cpu(free->bests[findex]) >= length)
1545 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 else {
1547 /*
1548 * Are we done with the freeblock?
1549 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001550 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 /*
1552 * Drop the block.
1553 */
1554 xfs_da_brelse(tp, fbp);
1555 fbp = NULL;
1556 if (fblk && fblk->bp)
1557 fblk->bp = NULL;
1558 }
1559 }
1560 }
1561 /*
1562 * If we don't have a data block, we need to allocate one and make
1563 * the freespace entries refer to it.
1564 */
1565 if (unlikely(dbno == -1)) {
1566 /*
1567 * Not allowed to allocate, return failure.
1568 */
1569 if (args->justcheck || args->total == 0) {
1570 /*
1571 * Drop the freespace buffer unless it came from our
1572 * caller.
1573 */
1574 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1575 xfs_da_buf_done(fbp);
1576 return XFS_ERROR(ENOSPC);
1577 }
1578 /*
1579 * Allocate and initialize the new data block.
1580 */
1581 if (unlikely((error = xfs_dir2_grow_inode(args,
1582 XFS_DIR2_DATA_SPACE,
1583 &dbno)) ||
1584 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1585 /*
1586 * Drop the freespace buffer unless it came from our
1587 * caller.
1588 */
1589 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1590 xfs_da_buf_done(fbp);
1591 return error;
1592 }
1593 /*
1594 * If (somehow) we have a freespace block, get rid of it.
1595 */
1596 if (fbp)
1597 xfs_da_brelse(tp, fbp);
1598 if (fblk && fblk->bp)
1599 fblk->bp = NULL;
1600
1601 /*
1602 * Get the freespace block corresponding to the data block
1603 * that was just allocated.
1604 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001605 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001607 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 XFS_DATA_FORK))) {
1609 xfs_da_buf_done(dbp);
1610 return error;
1611 }
1612 /*
1613 * If there wasn't a freespace block, the read will
1614 * return a NULL fbp. Allocate and initialize a new one.
1615 */
1616 if( fbp == NULL ) {
1617 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1618 &fbno))) {
1619 return error;
1620 }
1621
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001622 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 cmn_err(CE_ALERT,
1624 "xfs_dir2_node_addname_int: dir ino "
1625 "%llu needed freesp block %lld for\n"
1626 " data block %lld, got %lld\n"
1627 " ifbno %llu lastfbno %d\n",
1628 (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001629 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 (long long)dbno, (long long)fbno,
1631 (unsigned long long)ifbno, lastfbno);
1632 if (fblk) {
1633 cmn_err(CE_ALERT,
1634 " fblk 0x%p blkno %llu "
1635 "index %d magic 0x%x\n",
1636 fblk,
1637 (unsigned long long)fblk->blkno,
1638 fblk->index,
1639 fblk->magic);
1640 } else {
1641 cmn_err(CE_ALERT,
1642 " ... fblk is NULL\n");
1643 }
1644 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1645 XFS_ERRLEVEL_LOW, mp);
1646 return XFS_ERROR(EFSCORRUPTED);
1647 }
1648
1649 /*
1650 * Get a buffer for the new block.
1651 */
1652 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001653 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 -1, &fbp, XFS_DATA_FORK))) {
1655 return error;
1656 }
1657 ASSERT(fbp != NULL);
1658
1659 /*
1660 * Initialize the new block to be empty, and remember
1661 * its first slot as our empty slot.
1662 */
1663 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001664 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1665 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1667 XFS_DIR2_MAX_FREE_BESTS(mp));
1668 free->hdr.nvalid = 0;
1669 free->hdr.nused = 0;
1670 } else {
1671 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001672 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
1675 /*
1676 * Set the freespace block index from the data block number.
1677 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001678 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * If it's after the end of the current entries in the
1681 * freespace block, extend that table.
1682 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001683 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001685 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Tag new entry so nused will go up.
1688 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001689 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 /*
1692 * If this entry was for an empty data block
1693 * (this should always be true) then update the header.
1694 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001695 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001696 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 xfs_dir2_free_log_header(tp, fbp);
1698 }
1699 /*
1700 * Update the real value in the table.
1701 * We haven't allocated the data entry yet so this will
1702 * change again.
1703 */
1704 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001705 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 logfree = 1;
1707 }
1708 /*
1709 * We had a data block so we don't have to make a new one.
1710 */
1711 else {
1712 /*
1713 * If just checking, we succeeded.
1714 */
1715 if (args->justcheck) {
1716 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1717 xfs_da_buf_done(fbp);
1718 return 0;
1719 }
1720 /*
1721 * Read the data block in.
1722 */
1723 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001724 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 -1, &dbp, XFS_DATA_FORK))) {
1726 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1727 xfs_da_buf_done(fbp);
1728 return error;
1729 }
1730 data = dbp->data;
1731 logfree = 0;
1732 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001733 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * Point to the existing unused space.
1736 */
1737 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001738 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 needscan = needlog = 0;
1740 /*
1741 * Mark the first part of the unused space, inuse for us.
1742 */
1743 xfs_dir2_data_use_free(tp, dbp, dup,
1744 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1745 &needlog, &needscan);
1746 /*
1747 * Fill in the new entry and log it.
1748 */
1749 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001750 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 dep->namelen = args->namelen;
1752 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001753 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001754 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 xfs_dir2_data_log_entry(tp, dbp, dep);
1756 /*
1757 * Rescan the block for bestfree if needed.
1758 */
1759 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001760 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /*
1762 * Log the data block header if needed.
1763 */
1764 if (needlog)
1765 xfs_dir2_data_log_header(tp, dbp);
1766 /*
1767 * If the freespace entry is now wrong, update it.
1768 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001769 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001770 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 logfree = 1;
1772 }
1773 /*
1774 * Log the freespace entry if needed.
1775 */
1776 if (logfree)
1777 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1778 /*
1779 * If the caller didn't hand us the freespace block, drop it.
1780 */
1781 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1782 xfs_da_buf_done(fbp);
1783 /*
1784 * Return the data block and offset in args, then drop the data block.
1785 */
1786 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001787 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 xfs_da_buf_done(dbp);
1789 return 0;
1790}
1791
1792/*
1793 * Lookup an entry in a node-format directory.
1794 * All the real work happens in xfs_da_node_lookup_int.
1795 * The only real output is the inode number of the entry.
1796 */
1797int /* error */
1798xfs_dir2_node_lookup(
1799 xfs_da_args_t *args) /* operation arguments */
1800{
1801 int error; /* error return value */
1802 int i; /* btree level */
1803 int rval; /* operation return value */
1804 xfs_da_state_t *state; /* btree cursor */
1805
1806 xfs_dir2_trace_args("node_lookup", args);
1807 /*
1808 * Allocate and initialize the btree cursor.
1809 */
1810 state = xfs_da_state_alloc();
1811 state->args = args;
1812 state->mp = args->dp->i_mount;
1813 state->blocksize = state->mp->m_dirblksize;
1814 state->node_ents = state->mp->m_dir_node_ents;
1815 /*
1816 * Fill in the path to the entry in the cursor.
1817 */
1818 error = xfs_da_node_lookup_int(state, &rval);
1819 if (error)
1820 rval = error;
Barry Naujok5163f952008-05-21 16:41:01 +10001821 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE)
1822 rval = EEXIST; /* a case-insensitive match was found */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * Release the btree blocks and leaf block.
1825 */
1826 for (i = 0; i < state->path.active; i++) {
1827 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1828 state->path.blk[i].bp = NULL;
1829 }
1830 /*
1831 * Release the data block if we have it.
1832 */
1833 if (state->extravalid && state->extrablk.bp) {
1834 xfs_da_brelse(args->trans, state->extrablk.bp);
1835 state->extrablk.bp = NULL;
1836 }
1837 xfs_da_state_free(state);
1838 return rval;
1839}
1840
1841/*
1842 * Remove an entry from a node-format directory.
1843 */
1844int /* error */
1845xfs_dir2_node_removename(
1846 xfs_da_args_t *args) /* operation arguments */
1847{
1848 xfs_da_state_blk_t *blk; /* leaf block */
1849 int error; /* error return value */
1850 int rval; /* operation return value */
1851 xfs_da_state_t *state; /* btree cursor */
1852
1853 xfs_dir2_trace_args("node_removename", args);
1854 /*
1855 * Allocate and initialize the btree cursor.
1856 */
1857 state = xfs_da_state_alloc();
1858 state->args = args;
1859 state->mp = args->dp->i_mount;
1860 state->blocksize = state->mp->m_dirblksize;
1861 state->node_ents = state->mp->m_dir_node_ents;
1862 /*
1863 * Look up the entry we're deleting, set up the cursor.
1864 */
1865 error = xfs_da_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Didn't find it, upper layer screwed up.
1870 */
1871 if (rval != EEXIST) {
1872 xfs_da_state_free(state);
1873 return rval;
1874 }
1875 blk = &state->path.blk[state->path.active - 1];
1876 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1877 ASSERT(state->extravalid);
1878 /*
1879 * Remove the leaf and data entries.
1880 * Extrablk refers to the data block.
1881 */
1882 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1883 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10001884 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /*
1887 * Fix the hash values up the btree.
1888 */
1889 xfs_da_fixhashpath(state, &state->path);
1890 /*
1891 * If we need to join leaf blocks, do it.
1892 */
1893 if (rval && state->path.active > 1)
1894 error = xfs_da_join(state);
1895 /*
1896 * If no errors so far, try conversion to leaf format.
1897 */
1898 if (!error)
1899 error = xfs_dir2_node_to_leaf(state);
1900 xfs_da_state_free(state);
1901 return error;
1902}
1903
1904/*
1905 * Replace an entry's inode number in a node-format directory.
1906 */
1907int /* error */
1908xfs_dir2_node_replace(
1909 xfs_da_args_t *args) /* operation arguments */
1910{
1911 xfs_da_state_blk_t *blk; /* leaf block */
1912 xfs_dir2_data_t *data; /* data block structure */
1913 xfs_dir2_data_entry_t *dep; /* data entry changed */
1914 int error; /* error return value */
1915 int i; /* btree level */
1916 xfs_ino_t inum; /* new inode number */
1917 xfs_dir2_leaf_t *leaf; /* leaf structure */
1918 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1919 int rval; /* internal return value */
1920 xfs_da_state_t *state; /* btree cursor */
1921
1922 xfs_dir2_trace_args("node_replace", args);
1923 /*
1924 * Allocate and initialize the btree cursor.
1925 */
1926 state = xfs_da_state_alloc();
1927 state->args = args;
1928 state->mp = args->dp->i_mount;
1929 state->blocksize = state->mp->m_dirblksize;
1930 state->node_ents = state->mp->m_dir_node_ents;
1931 inum = args->inumber;
1932 /*
1933 * Lookup the entry to change in the btree.
1934 */
1935 error = xfs_da_node_lookup_int(state, &rval);
1936 if (error) {
1937 rval = error;
1938 }
1939 /*
1940 * It should be found, since the vnodeops layer has looked it up
1941 * and locked it. But paranoia is good.
1942 */
1943 if (rval == EEXIST) {
1944 /*
1945 * Find the leaf entry.
1946 */
1947 blk = &state->path.blk[state->path.active - 1];
1948 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1949 leaf = blk->bp->data;
1950 lep = &leaf->ents[blk->index];
1951 ASSERT(state->extravalid);
1952 /*
1953 * Point to the data entry.
1954 */
1955 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001956 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 dep = (xfs_dir2_data_entry_t *)
1958 ((char *)data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001959 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001960 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * Fill in the new inode number and log the entry.
1963 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001964 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1966 rval = 0;
1967 }
1968 /*
1969 * Didn't find it, and we're holding a data block. Drop it.
1970 */
1971 else if (state->extravalid) {
1972 xfs_da_brelse(args->trans, state->extrablk.bp);
1973 state->extrablk.bp = NULL;
1974 }
1975 /*
1976 * Release all the buffers in the cursor.
1977 */
1978 for (i = 0; i < state->path.active; i++) {
1979 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1980 state->path.blk[i].bp = NULL;
1981 }
1982 xfs_da_state_free(state);
1983 return rval;
1984}
1985
1986/*
1987 * Trim off a trailing empty freespace block.
1988 * Return (in rvalp) 1 if we did it, 0 if not.
1989 */
1990int /* error */
1991xfs_dir2_node_trim_free(
1992 xfs_da_args_t *args, /* operation arguments */
1993 xfs_fileoff_t fo, /* free block number */
1994 int *rvalp) /* out: did something */
1995{
1996 xfs_dabuf_t *bp; /* freespace buffer */
1997 xfs_inode_t *dp; /* incore directory inode */
1998 int error; /* error return code */
1999 xfs_dir2_free_t *free; /* freespace structure */
2000 xfs_mount_t *mp; /* filesystem mount point */
2001 xfs_trans_t *tp; /* transaction pointer */
2002
2003 dp = args->dp;
2004 mp = dp->i_mount;
2005 tp = args->trans;
2006 /*
2007 * Read the freespace block.
2008 */
2009 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
2010 XFS_DATA_FORK))) {
2011 return error;
2012 }
2013
2014 /*
2015 * There can be holes in freespace. If fo is a hole, there's
2016 * nothing to do.
2017 */
2018 if (bp == NULL) {
2019 return 0;
2020 }
2021 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11002022 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /*
2024 * If there are used entries, there's nothing to do.
2025 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11002026 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 xfs_da_brelse(tp, bp);
2028 *rvalp = 0;
2029 return 0;
2030 }
2031 /*
2032 * Blow the block away.
2033 */
2034 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002035 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 bp))) {
2037 /*
2038 * Can't fail with ENOSPC since that only happens with no
2039 * space reservation, when breaking up an extent into two
2040 * pieces. This is the last block of an extent.
2041 */
2042 ASSERT(error != ENOSPC);
2043 xfs_da_brelse(tp, bp);
2044 return error;
2045 }
2046 /*
2047 * Return that we succeeded.
2048 */
2049 *rvalp = 1;
2050 return 0;
2051}