blob: 8dade711f0995c4cfe2d74f46abd41ff624d7953 [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/*
390 * Look up a leaf entry in a node-format leaf block.
391 * If this is an addname then the extrablk in state is a freespace block,
392 * otherwise it's a data block.
393 */
394int
395xfs_dir2_leafn_lookup_int(
396 xfs_dabuf_t *bp, /* leaf buffer */
397 xfs_da_args_t *args, /* operation arguments */
398 int *indexp, /* out: leaf entry index */
399 xfs_da_state_t *state) /* state to fill in */
400{
401 xfs_dabuf_t *curbp; /* current data/free buffer */
402 xfs_dir2_db_t curdb; /* current data block number */
403 xfs_dir2_db_t curfdb; /* current free block number */
404 xfs_dir2_data_entry_t *dep; /* data block entry */
405 xfs_inode_t *dp; /* incore directory inode */
406 int error; /* error return value */
407 int fi; /* free entry index */
408 xfs_dir2_free_t *free=NULL; /* free block structure */
409 int index; /* leaf entry index */
410 xfs_dir2_leaf_t *leaf; /* leaf structure */
411 int length=0; /* length of new data entry */
412 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
413 xfs_mount_t *mp; /* filesystem mount point */
414 xfs_dir2_db_t newdb; /* new data block number */
415 xfs_dir2_db_t newfdb; /* new free block number */
416 xfs_trans_t *tp; /* transaction pointer */
417
418 dp = args->dp;
419 tp = args->trans;
420 mp = dp->i_mount;
421 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100422 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100424 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#endif
426 xfs_dir2_leafn_check(dp, bp);
427 /*
428 * Look up the hash value in the leaf entries.
429 */
430 index = xfs_dir2_leaf_search_hash(args, bp);
431 /*
432 * Do we have a buffer coming in?
433 */
434 if (state->extravalid)
435 curbp = state->extrablk.bp;
436 else
437 curbp = NULL;
438 /*
439 * For addname, it's a free block buffer, get the block number.
440 */
441 if (args->addname) {
442 curfdb = curbp ? state->extrablk.blkno : -1;
443 curdb = -1;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000444 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if ((free = (curbp ? curbp->data : NULL)))
Nathan Scott0ba962e2006-03-17 17:27:07 +1100446 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448 /*
449 * For others, it's a data block buffer, get the block number.
450 */
451 else {
452 curfdb = -1;
453 curdb = curbp ? state->extrablk.blkno : -1;
454 }
455 /*
456 * Loop over leaf entries with the right hash value.
457 */
458 for (lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100459 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 lep++, index++) {
461 /*
462 * Skip stale leaf entries.
463 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100464 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 continue;
466 /*
467 * Pull the data block number from the entry.
468 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000469 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /*
471 * For addname, we're looking for a place to put the new entry.
472 * We want to use a data block with an entry of equal
473 * hash value to ours if there is one with room.
474 */
475 if (args->addname) {
476 /*
477 * If this block isn't the data block we already have
478 * in hand, take a look at it.
479 */
480 if (newdb != curdb) {
481 curdb = newdb;
482 /*
483 * Convert the data block to the free block
484 * holding its freespace information.
485 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000486 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * If it's not the one we have in hand,
489 * read it in.
490 */
491 if (newfdb != curfdb) {
492 /*
493 * If we had one before, drop it.
494 */
495 if (curbp)
496 xfs_da_brelse(tp, curbp);
497 /*
498 * Read the free block.
499 */
500 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000501 xfs_dir2_db_to_da(mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 newfdb),
503 -1, &curbp,
504 XFS_DATA_FORK))) {
505 return error;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 free = curbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100508 ASSERT(be32_to_cpu(free->hdr.magic) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 XFS_DIR2_FREE_MAGIC);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100510 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
512 0);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100513 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 ASSERT(curdb <
Nathan Scott0ba962e2006-03-17 17:27:07 +1100515 be32_to_cpu(free->hdr.firstdb) +
516 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 /*
519 * Get the index for our entry.
520 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000521 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /*
523 * If it has room, return it.
524 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100525 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
527 XFS_ERRLEVEL_LOW, mp);
Nathan Scotte1090072006-06-09 14:58:48 +1000528 if (curfdb != newfdb)
529 xfs_da_brelse(tp, curbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return XFS_ERROR(EFSCORRUPTED);
531 }
Nathan Scotte1090072006-06-09 14:58:48 +1000532 curfdb = newfdb;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100533 if (be16_to_cpu(free->bests[fi]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *indexp = index;
535 state->extravalid = 1;
536 state->extrablk.bp = curbp;
537 state->extrablk.blkno = curfdb;
538 state->extrablk.index = fi;
539 state->extrablk.magic =
540 XFS_DIR2_FREE_MAGIC;
541 ASSERT(args->oknoent);
542 return XFS_ERROR(ENOENT);
543 }
544 }
545 }
546 /*
547 * Not adding a new entry, so we really want to find
548 * the name given to us.
549 */
550 else {
551 /*
552 * If it's a different data block, go get it.
553 */
554 if (newdb != curdb) {
555 /*
556 * If we had a block before, drop it.
557 */
558 if (curbp)
559 xfs_da_brelse(tp, curbp);
560 /*
561 * Read the data block.
562 */
563 if ((error =
564 xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000565 xfs_dir2_db_to_da(mp, newdb), -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 &curbp, XFS_DATA_FORK))) {
567 return error;
568 }
569 xfs_dir2_data_check(dp, curbp);
570 curdb = newdb;
571 }
572 /*
573 * Point to the data entry.
574 */
575 dep = (xfs_dir2_data_entry_t *)
576 ((char *)curbp->data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000577 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
579 * Compare the entry, return it if it matches.
580 */
581 if (dep->namelen == args->namelen &&
582 dep->name[0] == args->name[0] &&
583 memcmp(dep->name, args->name, args->namelen) == 0) {
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000584 args->inumber = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *indexp = index;
586 state->extravalid = 1;
587 state->extrablk.bp = curbp;
588 state->extrablk.blkno = curdb;
589 state->extrablk.index =
590 (int)((char *)dep -
591 (char *)curbp->data);
592 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
593 return XFS_ERROR(EEXIST);
594 }
595 }
596 }
597 /*
598 * Didn't find a match.
599 * If we are holding a buffer, give it back in case our caller
600 * finds it useful.
601 */
602 if ((state->extravalid = (curbp != NULL))) {
603 state->extrablk.bp = curbp;
604 state->extrablk.index = -1;
605 /*
606 * For addname, giving back a free block.
607 */
608 if (args->addname) {
609 state->extrablk.blkno = curfdb;
610 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
611 }
612 /*
613 * For other callers, giving back a data block.
614 */
615 else {
616 state->extrablk.blkno = curdb;
617 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
618 }
619 }
620 /*
621 * Return the final index, that will be the insertion point.
622 */
623 *indexp = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100624 ASSERT(index == be16_to_cpu(leaf->hdr.count) || args->oknoent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return XFS_ERROR(ENOENT);
626}
627
628/*
629 * Move count leaf entries from source to destination leaf.
630 * Log entries and headers. Stale entries are preserved.
631 */
632static void
633xfs_dir2_leafn_moveents(
634 xfs_da_args_t *args, /* operation arguments */
635 xfs_dabuf_t *bp_s, /* source leaf buffer */
636 int start_s, /* source leaf index */
637 xfs_dabuf_t *bp_d, /* destination leaf buffer */
638 int start_d, /* destination leaf index */
639 int count) /* count of leaves to copy */
640{
641 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
642 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
643 int stale; /* count stale leaves copied */
644 xfs_trans_t *tp; /* transaction pointer */
645
646 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
647 start_d, count);
648 /*
649 * Silently return if nothing to do.
650 */
651 if (count == 0) {
652 return;
653 }
654 tp = args->trans;
655 leaf_s = bp_s->data;
656 leaf_d = bp_d->data;
657 /*
658 * If the destination index is not the end of the current
659 * destination leaf entries, open up a hole in the destination
660 * to hold the new entries.
661 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100662 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100664 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 sizeof(xfs_dir2_leaf_entry_t));
666 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100667 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669 /*
670 * If the source has stale leaves, count the ones in the copy range
671 * so we can update the header correctly.
672 */
673 if (leaf_s->hdr.stale) {
674 int i; /* temp leaf index */
675
676 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100677 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 stale++;
679 }
680 } else
681 stale = 0;
682 /*
683 * Copy the leaf entries from source to destination.
684 */
685 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
686 count * sizeof(xfs_dir2_leaf_entry_t));
687 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
688 /*
689 * If there are source entries after the ones we copied,
690 * delete the ones we copied by sliding the next ones down.
691 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100692 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
694 count * sizeof(xfs_dir2_leaf_entry_t));
695 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
696 }
697 /*
698 * Update the headers and log them.
699 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800700 be16_add_cpu(&leaf_s->hdr.count, -(count));
701 be16_add_cpu(&leaf_s->hdr.stale, -(stale));
702 be16_add_cpu(&leaf_d->hdr.count, count);
703 be16_add_cpu(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 xfs_dir2_leaf_log_header(tp, bp_s);
705 xfs_dir2_leaf_log_header(tp, bp_d);
706 xfs_dir2_leafn_check(args->dp, bp_s);
707 xfs_dir2_leafn_check(args->dp, bp_d);
708}
709
710/*
711 * Determine the sort order of two leaf blocks.
712 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
713 */
714int /* sort order */
715xfs_dir2_leafn_order(
716 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
717 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
718{
719 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
720 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
721
722 leaf1 = leaf1_bp->data;
723 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100724 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
725 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100726 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
727 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100728 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
729 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
730 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return 1;
732 return 0;
733}
734
735/*
736 * Rebalance leaf entries between two leaf blocks.
737 * This is actually only called when the second block is new,
738 * though the code deals with the general case.
739 * A new entry will be inserted in one of the blocks, and that
740 * entry is taken into account when balancing.
741 */
742static void
743xfs_dir2_leafn_rebalance(
744 xfs_da_state_t *state, /* btree cursor */
745 xfs_da_state_blk_t *blk1, /* first btree block */
746 xfs_da_state_blk_t *blk2) /* second btree block */
747{
748 xfs_da_args_t *args; /* operation arguments */
749 int count; /* count (& direction) leaves */
750 int isleft; /* new goes in left leaf */
751 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
752 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
753 int mid; /* midpoint leaf index */
754#ifdef DEBUG
755 int oldstale; /* old count of stale leaves */
756#endif
757 int oldsum; /* old total leaf count */
758 int swap; /* swapped leaf blocks */
759
760 args = state->args;
761 /*
762 * If the block order is wrong, swap the arguments.
763 */
764 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
765 xfs_da_state_blk_t *tmp; /* temp for block swap */
766
767 tmp = blk1;
768 blk1 = blk2;
769 blk2 = tmp;
770 }
771 leaf1 = blk1->bp->data;
772 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100773 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100775 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
777 mid = oldsum >> 1;
778 /*
779 * If the old leaf count was odd then the new one will be even,
780 * so we need to divide the new count evenly.
781 */
782 if (oldsum & 1) {
783 xfs_dahash_t midhash; /* middle entry hash value */
784
Nathan Scotta818e5d2006-03-17 17:28:07 +1100785 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100786 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100788 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 isleft = args->hashval <= midhash;
790 }
791 /*
792 * If the old count is even then the new count is odd, so there's
793 * no preferred side for the new entry.
794 * Pick the left one.
795 */
796 else
797 isleft = 1;
798 /*
799 * Calculate moved entry count. Positive means left-to-right,
800 * negative means right-to-left. Then move the entries.
801 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100802 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (count > 0)
804 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100805 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 else if (count < 0)
807 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100808 be16_to_cpu(leaf1->hdr.count), count);
809 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
810 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /*
812 * Mark whether we're inserting into the old or new leaf.
813 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100814 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100816 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 state->inleaf = !swap;
818 else
819 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100820 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /*
822 * Adjust the expected index for insertion.
823 */
824 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100825 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /*
828 * Finally sanity check just to make sure we are not returning a negative index
829 */
830 if(blk2->index < 0) {
831 state->inleaf = 1;
832 blk2->index = 0;
833 cmn_err(CE_ALERT,
Nathan Scottc41564b2006-03-29 08:55:14 +1000834 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 "blk1->index %d\n",
836 blk1->index);
837 }
838}
839
840/*
841 * Remove an entry from a node directory.
842 * This removes the leaf entry and the data entry,
843 * and updates the free block if necessary.
844 */
845static int /* error */
846xfs_dir2_leafn_remove(
847 xfs_da_args_t *args, /* operation arguments */
848 xfs_dabuf_t *bp, /* leaf buffer */
849 int index, /* leaf entry index */
850 xfs_da_state_blk_t *dblk, /* data block */
851 int *rval) /* resulting block needs join */
852{
853 xfs_dir2_data_t *data; /* data block structure */
854 xfs_dir2_db_t db; /* data block number */
855 xfs_dabuf_t *dbp; /* data block buffer */
856 xfs_dir2_data_entry_t *dep; /* data block entry */
857 xfs_inode_t *dp; /* incore directory inode */
858 xfs_dir2_leaf_t *leaf; /* leaf structure */
859 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
860 int longest; /* longest data free entry */
861 int off; /* data block entry offset */
862 xfs_mount_t *mp; /* filesystem mount point */
863 int needlog; /* need to log data header */
864 int needscan; /* need to rescan data frees */
865 xfs_trans_t *tp; /* transaction pointer */
866
867 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
868 dp = args->dp;
869 tp = args->trans;
870 mp = dp->i_mount;
871 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100872 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * Point to the entry we're removing.
875 */
876 lep = &leaf->ents[index];
877 /*
878 * Extract the data block and offset from the entry.
879 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000880 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000882 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ASSERT(dblk->index == off);
884 /*
885 * Kill the leaf entry by marking it stale.
886 * Log the leaf block changes.
887 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800888 be16_add_cpu(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100890 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 xfs_dir2_leaf_log_ents(tp, bp, index, index);
892 /*
893 * Make the data entry free. Keep track of the longest freespace
894 * in the data block in case it changes.
895 */
896 dbp = dblk->bp;
897 data = dbp->data;
898 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100899 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 needlog = needscan = 0;
901 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000902 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /*
904 * Rescan the data block freespaces for bestfree.
905 * Log the data block header if needed.
906 */
907 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000908 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (needlog)
910 xfs_dir2_data_log_header(tp, dbp);
911 xfs_dir2_data_check(dp, dbp);
912 /*
913 * If the longest data block freespace changes, need to update
914 * the corresponding freeblock entry.
915 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100916 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int error; /* error return value */
918 xfs_dabuf_t *fbp; /* freeblock buffer */
919 xfs_dir2_db_t fdb; /* freeblock block number */
920 int findex; /* index in freeblock entries */
921 xfs_dir2_free_t *free; /* freeblock structure */
922 int logfree; /* need to log free entry */
923
924 /*
925 * Convert the data block number to a free block,
926 * read in the free block.
927 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000928 fdb = xfs_dir2_db_to_fdb(mp, db);
929 if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 -1, &fbp, XFS_DATA_FORK))) {
931 return error;
932 }
933 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100934 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
935 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 XFS_DIR2_MAX_FREE_BESTS(mp) *
937 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
938 /*
939 * Calculate which entry we need to fix.
940 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000941 findex = xfs_dir2_db_to_fdindex(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +1100942 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * If the data block is now empty we can get rid of it
945 * (usually).
946 */
947 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
948 /*
949 * Try to punch out the data block.
950 */
951 error = xfs_dir2_shrink_inode(args, db, dbp);
952 if (error == 0) {
953 dblk->bp = NULL;
954 data = NULL;
955 }
956 /*
957 * We can get ENOSPC if there's no space reservation.
958 * In this case just drop the buffer and some one else
959 * will eventually get rid of the empty block.
960 */
961 else if (error == ENOSPC && args->total == 0)
962 xfs_da_buf_done(dbp);
963 else
964 return error;
965 }
966 /*
967 * If we got rid of the data block, we can eliminate that entry
968 * in the free block.
969 */
970 if (data == NULL) {
971 /*
972 * One less used entry in the free table.
973 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800974 be32_add_cpu(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 xfs_dir2_free_log_header(tp, fbp);
976 /*
977 * If this was the last entry in the table, we can
978 * trim the table size back. There might be other
979 * entries at the end referring to non-existent
980 * data blocks, get those too.
981 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100982 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 int i; /* free entry index */
984
985 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100986 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 i--)
988 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100989 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 logfree = 0;
991 }
992 /*
993 * Not the last entry, just punch it out.
994 */
995 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100996 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 logfree = 1;
998 }
999 /*
1000 * If there are no useful entries left in the block,
1001 * get rid of the block if we can.
1002 */
1003 if (!free->hdr.nused) {
1004 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1005 if (error == 0) {
1006 fbp = NULL;
1007 logfree = 0;
1008 } else if (error != ENOSPC || args->total != 0)
1009 return error;
1010 /*
1011 * It's possible to get ENOSPC if there is no
1012 * space reservation. In this case some one
1013 * else will eventually get rid of this block.
1014 */
1015 }
1016 }
1017 /*
1018 * Data block is not empty, just set the free entry to
1019 * the new value.
1020 */
1021 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001022 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 logfree = 1;
1024 }
1025 /*
1026 * Log the free entry that changed, unless we got rid of it.
1027 */
1028 if (logfree)
1029 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1030 /*
1031 * Drop the buffer if we still have it.
1032 */
1033 if (fbp)
1034 xfs_da_buf_done(fbp);
1035 }
1036 xfs_dir2_leafn_check(dp, bp);
1037 /*
1038 * Return indication of whether this leaf block is emtpy enough
1039 * to justify trying to join it with a neighbor.
1040 */
1041 *rval =
1042 ((uint)sizeof(leaf->hdr) +
1043 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001044 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 mp->m_dir_magicpct;
1046 return 0;
1047}
1048
1049/*
1050 * Split the leaf entries in the old block into old and new blocks.
1051 */
1052int /* error */
1053xfs_dir2_leafn_split(
1054 xfs_da_state_t *state, /* btree cursor */
1055 xfs_da_state_blk_t *oldblk, /* original block */
1056 xfs_da_state_blk_t *newblk) /* newly created block */
1057{
1058 xfs_da_args_t *args; /* operation arguments */
1059 xfs_dablk_t blkno; /* new leaf block number */
1060 int error; /* error return value */
1061 xfs_mount_t *mp; /* filesystem mount point */
1062
1063 /*
1064 * Allocate space for a new leaf node.
1065 */
1066 args = state->args;
1067 mp = args->dp->i_mount;
1068 ASSERT(args != NULL);
1069 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1070 error = xfs_da_grow_inode(args, &blkno);
1071 if (error) {
1072 return error;
1073 }
1074 /*
1075 * Initialize the new leaf block.
1076 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001077 error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1079 if (error) {
1080 return error;
1081 }
1082 newblk->blkno = blkno;
1083 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1084 /*
1085 * Rebalance the entries across the two leaves, link the new
1086 * block into the leaves.
1087 */
1088 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1089 error = xfs_da_blk_link(state, oldblk, newblk);
1090 if (error) {
1091 return error;
1092 }
1093 /*
1094 * Insert the new entry in the correct block.
1095 */
1096 if (state->inleaf)
1097 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1098 else
1099 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1100 /*
1101 * Update last hashval in each block since we added the name.
1102 */
1103 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1104 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1105 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1106 xfs_dir2_leafn_check(args->dp, newblk->bp);
1107 return error;
1108}
1109
1110/*
1111 * Check a leaf block and its neighbors to see if the block should be
1112 * collapsed into one or the other neighbor. Always keep the block
1113 * with the smaller block number.
1114 * If the current block is over 50% full, don't try to join it, return 0.
1115 * If the block is empty, fill in the state structure and return 2.
1116 * If it can be collapsed, fill in the state structure and return 1.
1117 * If nothing can be done, return 0.
1118 */
1119int /* error */
1120xfs_dir2_leafn_toosmall(
1121 xfs_da_state_t *state, /* btree cursor */
1122 int *action) /* resulting action to take */
1123{
1124 xfs_da_state_blk_t *blk; /* leaf block */
1125 xfs_dablk_t blkno; /* leaf block number */
1126 xfs_dabuf_t *bp; /* leaf buffer */
1127 int bytes; /* bytes in use */
1128 int count; /* leaf live entry count */
1129 int error; /* error return value */
1130 int forward; /* sibling block direction */
1131 int i; /* sibling counter */
1132 xfs_da_blkinfo_t *info; /* leaf block header */
1133 xfs_dir2_leaf_t *leaf; /* leaf structure */
1134 int rval; /* result from path_shift */
1135
1136 /*
1137 * Check for the degenerate case of the block being over 50% full.
1138 * If so, it's not worth even looking to see if we might be able
1139 * to coalesce with a sibling.
1140 */
1141 blk = &state->path.blk[state->path.active - 1];
1142 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001143 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001145 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1147 if (bytes > (state->blocksize >> 1)) {
1148 /*
1149 * Blk over 50%, don't try to join.
1150 */
1151 *action = 0;
1152 return 0;
1153 }
1154 /*
1155 * Check for the degenerate case of the block being empty.
1156 * If the block is empty, we'll simply delete it, no need to
1157 * coalesce it with a sibling block. We choose (arbitrarily)
1158 * to merge with the forward block unless it is NULL.
1159 */
1160 if (count == 0) {
1161 /*
1162 * Make altpath point to the block we want to keep and
1163 * path point to the block we want to drop (this one).
1164 */
Nathan Scott89da0542006-03-17 17:28:40 +11001165 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 memcpy(&state->altpath, &state->path, sizeof(state->path));
1167 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1168 &rval);
1169 if (error)
1170 return error;
1171 *action = rval ? 2 : 0;
1172 return 0;
1173 }
1174 /*
1175 * Examine each sibling block to see if we can coalesce with
1176 * at least 25% free space to spare. We need to figure out
1177 * whether to merge with the forward or the backward block.
1178 * We prefer coalescing with the lower numbered sibling so as
1179 * to shrink a directory over time.
1180 */
Nathan Scott89da0542006-03-17 17:28:40 +11001181 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001183 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (blkno == 0)
1185 continue;
1186 /*
1187 * Read the sibling leaf block.
1188 */
1189 if ((error =
1190 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1191 -1, &bp, XFS_DATA_FORK))) {
1192 return error;
1193 }
1194 ASSERT(bp != NULL);
1195 /*
1196 * Count bytes in the two blocks combined.
1197 */
1198 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001199 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 bytes = state->blocksize - (state->blocksize >> 2);
1201 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001202 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001203 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 bytes -= count * (uint)sizeof(leaf->ents[0]);
1205 /*
1206 * Fits with at least 25% to spare.
1207 */
1208 if (bytes >= 0)
1209 break;
1210 xfs_da_brelse(state->args->trans, bp);
1211 }
1212 /*
1213 * Didn't like either block, give up.
1214 */
1215 if (i >= 2) {
1216 *action = 0;
1217 return 0;
1218 }
1219 /*
1220 * Done with the sibling leaf block here, drop the dabuf
1221 * so path_shift can get it.
1222 */
1223 xfs_da_buf_done(bp);
1224 /*
1225 * Make altpath point to the block we want to keep (the lower
1226 * numbered block) and path point to the block we want to drop.
1227 */
1228 memcpy(&state->altpath, &state->path, sizeof(state->path));
1229 if (blkno < blk->blkno)
1230 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1231 &rval);
1232 else
1233 error = xfs_da_path_shift(state, &state->path, forward, 0,
1234 &rval);
1235 if (error) {
1236 return error;
1237 }
1238 *action = rval ? 0 : 1;
1239 return 0;
1240}
1241
1242/*
1243 * Move all the leaf entries from drop_blk to save_blk.
1244 * This is done as part of a join operation.
1245 */
1246void
1247xfs_dir2_leafn_unbalance(
1248 xfs_da_state_t *state, /* cursor */
1249 xfs_da_state_blk_t *drop_blk, /* dead block */
1250 xfs_da_state_blk_t *save_blk) /* surviving block */
1251{
1252 xfs_da_args_t *args; /* operation arguments */
1253 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1254 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1255
1256 args = state->args;
1257 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1258 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1259 drop_leaf = drop_blk->bp->data;
1260 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001261 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1262 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * If there are any stale leaf entries, take this opportunity
1265 * to purge them.
1266 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001267 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001269 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 xfs_dir2_leaf_compact(args, save_blk->bp);
1271 /*
1272 * Move the entries from drop to the appropriate end of save.
1273 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001274 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 -07001275 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1276 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001277 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 else
1279 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001280 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001281 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 -07001282 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1283}
1284
1285/*
1286 * Top-level node form directory addname routine.
1287 */
1288int /* error */
1289xfs_dir2_node_addname(
1290 xfs_da_args_t *args) /* operation arguments */
1291{
1292 xfs_da_state_blk_t *blk; /* leaf block for insert */
1293 int error; /* error return value */
1294 int rval; /* sub-return value */
1295 xfs_da_state_t *state; /* btree cursor */
1296
1297 xfs_dir2_trace_args("node_addname", args);
1298 /*
1299 * Allocate and initialize the state (btree cursor).
1300 */
1301 state = xfs_da_state_alloc();
1302 state->args = args;
1303 state->mp = args->dp->i_mount;
1304 state->blocksize = state->mp->m_dirblksize;
1305 state->node_ents = state->mp->m_dir_node_ents;
1306 /*
1307 * Look up the name. We're not supposed to find it, but
1308 * this gives us the insertion point.
1309 */
1310 error = xfs_da_node_lookup_int(state, &rval);
1311 if (error)
1312 rval = error;
1313 if (rval != ENOENT) {
1314 goto done;
1315 }
1316 /*
1317 * Add the data entry to a data block.
1318 * Extravalid is set to a freeblock found by lookup.
1319 */
1320 rval = xfs_dir2_node_addname_int(args,
1321 state->extravalid ? &state->extrablk : NULL);
1322 if (rval) {
1323 goto done;
1324 }
1325 blk = &state->path.blk[state->path.active - 1];
1326 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1327 /*
1328 * Add the new leaf entry.
1329 */
1330 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1331 if (rval == 0) {
1332 /*
1333 * It worked, fix the hash values up the btree.
1334 */
1335 if (!args->justcheck)
1336 xfs_da_fixhashpath(state, &state->path);
1337 } else {
1338 /*
1339 * It didn't work, we need to split the leaf block.
1340 */
1341 if (args->total == 0) {
1342 ASSERT(rval == ENOSPC);
1343 goto done;
1344 }
1345 /*
1346 * Split the leaf block and insert the new entry.
1347 */
1348 rval = xfs_da_split(state);
1349 }
1350done:
1351 xfs_da_state_free(state);
1352 return rval;
1353}
1354
1355/*
1356 * Add the data entry for a node-format directory name addition.
1357 * The leaf entry is added in xfs_dir2_leafn_add.
1358 * We may enter with a freespace block that the lookup found.
1359 */
1360static int /* error */
1361xfs_dir2_node_addname_int(
1362 xfs_da_args_t *args, /* operation arguments */
1363 xfs_da_state_blk_t *fblk) /* optional freespace block */
1364{
1365 xfs_dir2_data_t *data; /* data block structure */
1366 xfs_dir2_db_t dbno; /* data block number */
1367 xfs_dabuf_t *dbp; /* data block buffer */
1368 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1369 xfs_inode_t *dp; /* incore directory inode */
1370 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1371 int error; /* error return value */
1372 xfs_dir2_db_t fbno; /* freespace block number */
1373 xfs_dabuf_t *fbp; /* freespace buffer */
1374 int findex; /* freespace entry index */
1375 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1376 xfs_dir2_db_t ifbno; /* initial freespace block no */
1377 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1378 int length; /* length of the new entry */
1379 int logfree; /* need to log free entry */
1380 xfs_mount_t *mp; /* filesystem mount point */
1381 int needlog; /* need to log data header */
1382 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001383 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 xfs_trans_t *tp; /* transaction pointer */
1385
1386 dp = args->dp;
1387 mp = dp->i_mount;
1388 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001389 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /*
1391 * If we came in with a freespace block that means that lookup
1392 * found an entry with our hash value. This is the freespace
1393 * block for that data entry.
1394 */
1395 if (fblk) {
1396 fbp = fblk->bp;
1397 /*
1398 * Remember initial freespace block number.
1399 */
1400 ifbno = fblk->blkno;
1401 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001402 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 findex = fblk->index;
1404 /*
1405 * This means the free entry showed that the data block had
1406 * space for our entry, so we remembered it.
1407 * Use that data block.
1408 */
1409 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001410 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1411 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1412 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1413 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 /*
1416 * The data block looked at didn't have enough room.
1417 * We'll start at the beginning of the freespace entries.
1418 */
1419 else {
1420 dbno = -1;
1421 findex = 0;
1422 }
1423 }
1424 /*
1425 * Didn't come in with a freespace block, so don't have a data block.
1426 */
1427 else {
1428 ifbno = dbno = -1;
1429 fbp = NULL;
1430 findex = 0;
1431 }
1432 /*
1433 * If we don't have a data block yet, we're going to scan the
1434 * freespace blocks looking for one. Figure out what the
1435 * highest freespace block number is.
1436 */
1437 if (dbno == -1) {
1438 xfs_fileoff_t fo; /* freespace block number */
1439
1440 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1441 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001442 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 fbno = ifbno;
1444 }
1445 /*
1446 * While we haven't identified a data block, search the freeblock
1447 * data for a good data block. If we find a null freeblock entry,
1448 * indicating a hole in the data blocks, remember that.
1449 */
1450 while (dbno == -1) {
1451 /*
1452 * If we don't have a freeblock in hand, get the next one.
1453 */
1454 if (fbp == NULL) {
1455 /*
1456 * Happens the first time through unless lookup gave
1457 * us a freespace block to start with.
1458 */
1459 if (++fbno == 0)
1460 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1461 /*
1462 * If it's ifbno we already looked at it.
1463 */
1464 if (fbno == ifbno)
1465 fbno++;
1466 /*
1467 * If it's off the end we're done.
1468 */
1469 if (fbno >= lastfbno)
1470 break;
1471 /*
1472 * Read the block. There can be holes in the
1473 * freespace blocks, so this might not succeed.
1474 * This should be really rare, so there's no reason
1475 * to avoid it.
1476 */
1477 if ((error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001478 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 XFS_DATA_FORK))) {
1480 return error;
1481 }
1482 if (unlikely(fbp == NULL)) {
1483 continue;
1484 }
1485 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001486 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 findex = 0;
1488 }
1489 /*
1490 * Look at the current free entry. Is it good enough?
1491 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001492 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1493 be16_to_cpu(free->bests[findex]) >= length)
1494 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 else {
1496 /*
1497 * Are we done with the freeblock?
1498 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001499 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 /*
1501 * Drop the block.
1502 */
1503 xfs_da_brelse(tp, fbp);
1504 fbp = NULL;
1505 if (fblk && fblk->bp)
1506 fblk->bp = NULL;
1507 }
1508 }
1509 }
1510 /*
1511 * If we don't have a data block, we need to allocate one and make
1512 * the freespace entries refer to it.
1513 */
1514 if (unlikely(dbno == -1)) {
1515 /*
1516 * Not allowed to allocate, return failure.
1517 */
1518 if (args->justcheck || args->total == 0) {
1519 /*
1520 * Drop the freespace buffer unless it came from our
1521 * caller.
1522 */
1523 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1524 xfs_da_buf_done(fbp);
1525 return XFS_ERROR(ENOSPC);
1526 }
1527 /*
1528 * Allocate and initialize the new data block.
1529 */
1530 if (unlikely((error = xfs_dir2_grow_inode(args,
1531 XFS_DIR2_DATA_SPACE,
1532 &dbno)) ||
1533 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1534 /*
1535 * Drop the freespace buffer unless it came from our
1536 * caller.
1537 */
1538 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1539 xfs_da_buf_done(fbp);
1540 return error;
1541 }
1542 /*
1543 * If (somehow) we have a freespace block, get rid of it.
1544 */
1545 if (fbp)
1546 xfs_da_brelse(tp, fbp);
1547 if (fblk && fblk->bp)
1548 fblk->bp = NULL;
1549
1550 /*
1551 * Get the freespace block corresponding to the data block
1552 * that was just allocated.
1553 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001554 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (unlikely(error = xfs_da_read_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001556 xfs_dir2_db_to_da(mp, fbno), -2, &fbp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 XFS_DATA_FORK))) {
1558 xfs_da_buf_done(dbp);
1559 return error;
1560 }
1561 /*
1562 * If there wasn't a freespace block, the read will
1563 * return a NULL fbp. Allocate and initialize a new one.
1564 */
1565 if( fbp == NULL ) {
1566 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1567 &fbno))) {
1568 return error;
1569 }
1570
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001571 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 cmn_err(CE_ALERT,
1573 "xfs_dir2_node_addname_int: dir ino "
1574 "%llu needed freesp block %lld for\n"
1575 " data block %lld, got %lld\n"
1576 " ifbno %llu lastfbno %d\n",
1577 (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001578 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 (long long)dbno, (long long)fbno,
1580 (unsigned long long)ifbno, lastfbno);
1581 if (fblk) {
1582 cmn_err(CE_ALERT,
1583 " fblk 0x%p blkno %llu "
1584 "index %d magic 0x%x\n",
1585 fblk,
1586 (unsigned long long)fblk->blkno,
1587 fblk->index,
1588 fblk->magic);
1589 } else {
1590 cmn_err(CE_ALERT,
1591 " ... fblk is NULL\n");
1592 }
1593 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1594 XFS_ERRLEVEL_LOW, mp);
1595 return XFS_ERROR(EFSCORRUPTED);
1596 }
1597
1598 /*
1599 * Get a buffer for the new block.
1600 */
1601 if ((error = xfs_da_get_buf(tp, dp,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001602 xfs_dir2_db_to_da(mp, fbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 -1, &fbp, XFS_DATA_FORK))) {
1604 return error;
1605 }
1606 ASSERT(fbp != NULL);
1607
1608 /*
1609 * Initialize the new block to be empty, and remember
1610 * its first slot as our empty slot.
1611 */
1612 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001613 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1614 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1616 XFS_DIR2_MAX_FREE_BESTS(mp));
1617 free->hdr.nvalid = 0;
1618 free->hdr.nused = 0;
1619 } else {
1620 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001621 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 /*
1625 * Set the freespace block index from the data block number.
1626 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001627 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * If it's after the end of the current entries in the
1630 * freespace block, extend that table.
1631 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001632 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001634 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Tag new entry so nused will go up.
1637 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001638 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640 /*
1641 * If this entry was for an empty data block
1642 * (this should always be true) then update the header.
1643 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001644 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001645 be32_add_cpu(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 xfs_dir2_free_log_header(tp, fbp);
1647 }
1648 /*
1649 * Update the real value in the table.
1650 * We haven't allocated the data entry yet so this will
1651 * change again.
1652 */
1653 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001654 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 logfree = 1;
1656 }
1657 /*
1658 * We had a data block so we don't have to make a new one.
1659 */
1660 else {
1661 /*
1662 * If just checking, we succeeded.
1663 */
1664 if (args->justcheck) {
1665 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1666 xfs_da_buf_done(fbp);
1667 return 0;
1668 }
1669 /*
1670 * Read the data block in.
1671 */
1672 if (unlikely(
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001673 error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 -1, &dbp, XFS_DATA_FORK))) {
1675 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1676 xfs_da_buf_done(fbp);
1677 return error;
1678 }
1679 data = dbp->data;
1680 logfree = 0;
1681 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001682 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Point to the existing unused space.
1685 */
1686 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001687 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 needscan = needlog = 0;
1689 /*
1690 * Mark the first part of the unused space, inuse for us.
1691 */
1692 xfs_dir2_data_use_free(tp, dbp, dup,
1693 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1694 &needlog, &needscan);
1695 /*
1696 * Fill in the new entry and log it.
1697 */
1698 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001699 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 dep->namelen = args->namelen;
1701 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001702 tagp = xfs_dir2_data_entry_tag_p(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001703 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 xfs_dir2_data_log_entry(tp, dbp, dep);
1705 /*
1706 * Rescan the block for bestfree if needed.
1707 */
1708 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001709 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /*
1711 * Log the data block header if needed.
1712 */
1713 if (needlog)
1714 xfs_dir2_data_log_header(tp, dbp);
1715 /*
1716 * If the freespace entry is now wrong, update it.
1717 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001718 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001719 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 logfree = 1;
1721 }
1722 /*
1723 * Log the freespace entry if needed.
1724 */
1725 if (logfree)
1726 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1727 /*
1728 * If the caller didn't hand us the freespace block, drop it.
1729 */
1730 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1731 xfs_da_buf_done(fbp);
1732 /*
1733 * Return the data block and offset in args, then drop the data block.
1734 */
1735 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001736 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 xfs_da_buf_done(dbp);
1738 return 0;
1739}
1740
1741/*
1742 * Lookup an entry in a node-format directory.
1743 * All the real work happens in xfs_da_node_lookup_int.
1744 * The only real output is the inode number of the entry.
1745 */
1746int /* error */
1747xfs_dir2_node_lookup(
1748 xfs_da_args_t *args) /* operation arguments */
1749{
1750 int error; /* error return value */
1751 int i; /* btree level */
1752 int rval; /* operation return value */
1753 xfs_da_state_t *state; /* btree cursor */
1754
1755 xfs_dir2_trace_args("node_lookup", args);
1756 /*
1757 * Allocate and initialize the btree cursor.
1758 */
1759 state = xfs_da_state_alloc();
1760 state->args = args;
1761 state->mp = args->dp->i_mount;
1762 state->blocksize = state->mp->m_dirblksize;
1763 state->node_ents = state->mp->m_dir_node_ents;
1764 /*
1765 * Fill in the path to the entry in the cursor.
1766 */
1767 error = xfs_da_node_lookup_int(state, &rval);
1768 if (error)
1769 rval = error;
1770 /*
1771 * Release the btree blocks and leaf block.
1772 */
1773 for (i = 0; i < state->path.active; i++) {
1774 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1775 state->path.blk[i].bp = NULL;
1776 }
1777 /*
1778 * Release the data block if we have it.
1779 */
1780 if (state->extravalid && state->extrablk.bp) {
1781 xfs_da_brelse(args->trans, state->extrablk.bp);
1782 state->extrablk.bp = NULL;
1783 }
1784 xfs_da_state_free(state);
1785 return rval;
1786}
1787
1788/*
1789 * Remove an entry from a node-format directory.
1790 */
1791int /* error */
1792xfs_dir2_node_removename(
1793 xfs_da_args_t *args) /* operation arguments */
1794{
1795 xfs_da_state_blk_t *blk; /* leaf block */
1796 int error; /* error return value */
1797 int rval; /* operation return value */
1798 xfs_da_state_t *state; /* btree cursor */
1799
1800 xfs_dir2_trace_args("node_removename", args);
1801 /*
1802 * Allocate and initialize the btree cursor.
1803 */
1804 state = xfs_da_state_alloc();
1805 state->args = args;
1806 state->mp = args->dp->i_mount;
1807 state->blocksize = state->mp->m_dirblksize;
1808 state->node_ents = state->mp->m_dir_node_ents;
1809 /*
1810 * Look up the entry we're deleting, set up the cursor.
1811 */
1812 error = xfs_da_node_lookup_int(state, &rval);
1813 if (error) {
1814 rval = error;
1815 }
1816 /*
1817 * Didn't find it, upper layer screwed up.
1818 */
1819 if (rval != EEXIST) {
1820 xfs_da_state_free(state);
1821 return rval;
1822 }
1823 blk = &state->path.blk[state->path.active - 1];
1824 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1825 ASSERT(state->extravalid);
1826 /*
1827 * Remove the leaf and data entries.
1828 * Extrablk refers to the data block.
1829 */
1830 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1831 &state->extrablk, &rval);
1832 if (error) {
1833 return error;
1834 }
1835 /*
1836 * Fix the hash values up the btree.
1837 */
1838 xfs_da_fixhashpath(state, &state->path);
1839 /*
1840 * If we need to join leaf blocks, do it.
1841 */
1842 if (rval && state->path.active > 1)
1843 error = xfs_da_join(state);
1844 /*
1845 * If no errors so far, try conversion to leaf format.
1846 */
1847 if (!error)
1848 error = xfs_dir2_node_to_leaf(state);
1849 xfs_da_state_free(state);
1850 return error;
1851}
1852
1853/*
1854 * Replace an entry's inode number in a node-format directory.
1855 */
1856int /* error */
1857xfs_dir2_node_replace(
1858 xfs_da_args_t *args) /* operation arguments */
1859{
1860 xfs_da_state_blk_t *blk; /* leaf block */
1861 xfs_dir2_data_t *data; /* data block structure */
1862 xfs_dir2_data_entry_t *dep; /* data entry changed */
1863 int error; /* error return value */
1864 int i; /* btree level */
1865 xfs_ino_t inum; /* new inode number */
1866 xfs_dir2_leaf_t *leaf; /* leaf structure */
1867 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1868 int rval; /* internal return value */
1869 xfs_da_state_t *state; /* btree cursor */
1870
1871 xfs_dir2_trace_args("node_replace", args);
1872 /*
1873 * Allocate and initialize the btree cursor.
1874 */
1875 state = xfs_da_state_alloc();
1876 state->args = args;
1877 state->mp = args->dp->i_mount;
1878 state->blocksize = state->mp->m_dirblksize;
1879 state->node_ents = state->mp->m_dir_node_ents;
1880 inum = args->inumber;
1881 /*
1882 * Lookup the entry to change in the btree.
1883 */
1884 error = xfs_da_node_lookup_int(state, &rval);
1885 if (error) {
1886 rval = error;
1887 }
1888 /*
1889 * It should be found, since the vnodeops layer has looked it up
1890 * and locked it. But paranoia is good.
1891 */
1892 if (rval == EEXIST) {
1893 /*
1894 * Find the leaf entry.
1895 */
1896 blk = &state->path.blk[state->path.active - 1];
1897 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1898 leaf = blk->bp->data;
1899 lep = &leaf->ents[blk->index];
1900 ASSERT(state->extravalid);
1901 /*
1902 * Point to the data entry.
1903 */
1904 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001905 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 dep = (xfs_dir2_data_entry_t *)
1907 ((char *)data +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001908 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001909 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
1911 * Fill in the new inode number and log the entry.
1912 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001913 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1915 rval = 0;
1916 }
1917 /*
1918 * Didn't find it, and we're holding a data block. Drop it.
1919 */
1920 else if (state->extravalid) {
1921 xfs_da_brelse(args->trans, state->extrablk.bp);
1922 state->extrablk.bp = NULL;
1923 }
1924 /*
1925 * Release all the buffers in the cursor.
1926 */
1927 for (i = 0; i < state->path.active; i++) {
1928 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1929 state->path.blk[i].bp = NULL;
1930 }
1931 xfs_da_state_free(state);
1932 return rval;
1933}
1934
1935/*
1936 * Trim off a trailing empty freespace block.
1937 * Return (in rvalp) 1 if we did it, 0 if not.
1938 */
1939int /* error */
1940xfs_dir2_node_trim_free(
1941 xfs_da_args_t *args, /* operation arguments */
1942 xfs_fileoff_t fo, /* free block number */
1943 int *rvalp) /* out: did something */
1944{
1945 xfs_dabuf_t *bp; /* freespace buffer */
1946 xfs_inode_t *dp; /* incore directory inode */
1947 int error; /* error return code */
1948 xfs_dir2_free_t *free; /* freespace structure */
1949 xfs_mount_t *mp; /* filesystem mount point */
1950 xfs_trans_t *tp; /* transaction pointer */
1951
1952 dp = args->dp;
1953 mp = dp->i_mount;
1954 tp = args->trans;
1955 /*
1956 * Read the freespace block.
1957 */
1958 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1959 XFS_DATA_FORK))) {
1960 return error;
1961 }
1962
1963 /*
1964 * There can be holes in freespace. If fo is a hole, there's
1965 * nothing to do.
1966 */
1967 if (bp == NULL) {
1968 return 0;
1969 }
1970 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001971 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
1973 * If there are used entries, there's nothing to do.
1974 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001975 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 xfs_da_brelse(tp, bp);
1977 *rvalp = 0;
1978 return 0;
1979 }
1980 /*
1981 * Blow the block away.
1982 */
1983 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001984 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 bp))) {
1986 /*
1987 * Can't fail with ENOSPC since that only happens with no
1988 * space reservation, when breaking up an extent into two
1989 * pieces. This is the last block of an extent.
1990 */
1991 ASSERT(error != ENOSPC);
1992 xfs_da_brelse(tp, bp);
1993 return error;
1994 }
1995 /*
1996 * Return that we succeeded.
1997 */
1998 *rvalp = 1;
1999 return 0;
2000}