blob: d083c38199343e0eacf91007c8e41180e79fcb54 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_dir2.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dir2_data.h"
36#include "xfs_dir2_leaf.h"
37#include "xfs_dir2_block.h"
38#include "xfs_dir2_node.h"
39#include "xfs_dir2_trace.h"
40#include "xfs_error.h"
41
42/*
43 * Function declarations.
44 */
45static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
46static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
47#ifdef DEBUG
48static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
49#else
50#define xfs_dir2_leafn_check(dp, bp)
51#endif
52static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
53 int start_s, xfs_dabuf_t *bp_d, int start_d,
54 int count);
55static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
56 xfs_da_state_blk_t *blk1,
57 xfs_da_state_blk_t *blk2);
58static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
59 int index, xfs_da_state_blk_t *dblk,
60 int *rval);
61static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
62 xfs_da_state_blk_t *fblk);
63
64/*
65 * Log entries from a freespace block.
66 */
67void
68xfs_dir2_free_log_bests(
69 xfs_trans_t *tp, /* transaction pointer */
70 xfs_dabuf_t *bp, /* freespace buffer */
71 int first, /* first entry to log */
72 int last) /* last entry to log */
73{
74 xfs_dir2_free_t *free; /* freespace structure */
75
76 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110077 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 xfs_da_log_buf(tp, bp,
79 (uint)((char *)&free->bests[first] - (char *)free),
80 (uint)((char *)&free->bests[last] - (char *)free +
81 sizeof(free->bests[0]) - 1));
82}
83
84/*
85 * Log header from a freespace block.
86 */
87static void
88xfs_dir2_free_log_header(
89 xfs_trans_t *tp, /* transaction pointer */
90 xfs_dabuf_t *bp) /* freespace buffer */
91{
92 xfs_dir2_free_t *free; /* freespace structure */
93
94 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +110095 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
97 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
98}
99
100/*
101 * Convert a leaf-format directory to a node-format directory.
102 * We need to change the magic number of the leaf block, and copy
103 * the freespace table out of the leaf block into its own block.
104 */
105int /* error */
106xfs_dir2_leaf_to_node(
107 xfs_da_args_t *args, /* operation arguments */
108 xfs_dabuf_t *lbp) /* leaf buffer */
109{
110 xfs_inode_t *dp; /* incore directory inode */
111 int error; /* error return value */
112 xfs_dabuf_t *fbp; /* freespace buffer */
113 xfs_dir2_db_t fdb; /* freespace block number */
114 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100115 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int i; /* leaf freespace index */
117 xfs_dir2_leaf_t *leaf; /* leaf structure */
118 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
119 xfs_mount_t *mp; /* filesystem mount point */
120 int n; /* count of live freespc ents */
121 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100122 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 xfs_trans_t *tp; /* transaction pointer */
124
125 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
126 dp = args->dp;
127 mp = dp->i_mount;
128 tp = args->trans;
129 /*
130 * Add a freespace block to the directory.
131 */
132 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
133 return error;
134 }
135 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
136 /*
137 * Get the buffer for the new freespace block.
138 */
139 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
140 XFS_DATA_FORK))) {
141 return error;
142 }
143 ASSERT(fbp != NULL);
144 free = fbp->data;
145 leaf = lbp->data;
146 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
147 /*
148 * Initialize the freespace block header.
149 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100150 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 free->hdr.firstdb = 0;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100152 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100153 free->hdr.nvalid = ltp->bestcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /*
155 * Copy freespace entries from the leaf block to the new block.
156 * Count active entries.
157 */
158 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100159 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100160 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100162 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Nathan Scott0ba962e2006-03-17 17:27:07 +1100164 free->hdr.nused = cpu_to_be32(n);
Nathan Scott89da0542006-03-17 17:28:40 +1100165 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /*
167 * Log everything.
168 */
169 xfs_dir2_leaf_log_header(tp, lbp);
170 xfs_dir2_free_log_header(tp, fbp);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100171 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 xfs_da_buf_done(fbp);
173 xfs_dir2_leafn_check(dp, lbp);
174 return 0;
175}
176
177/*
178 * Add a leaf entry to a leaf block in a node-form directory.
179 * The other work necessary is done from the caller.
180 */
181static int /* error */
182xfs_dir2_leafn_add(
183 xfs_dabuf_t *bp, /* leaf buffer */
184 xfs_da_args_t *args, /* operation arguments */
185 int index) /* insertion pt for new entry */
186{
187 int compact; /* compacting stale leaves */
188 xfs_inode_t *dp; /* incore directory inode */
189 int highstale; /* next stale entry */
190 xfs_dir2_leaf_t *leaf; /* leaf structure */
191 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
192 int lfloghigh; /* high leaf entry logging */
193 int lfloglow; /* low leaf entry logging */
194 int lowstale; /* previous stale entry */
195 xfs_mount_t *mp; /* filesystem mount point */
196 xfs_trans_t *tp; /* transaction pointer */
197
198 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
199 dp = args->dp;
200 mp = dp->i_mount;
201 tp = args->trans;
202 leaf = bp->data;
203
204 /*
205 * Quick check just to make sure we are not going to index
206 * into other peoples memory
207 */
208 if (index < 0)
209 return XFS_ERROR(EFSCORRUPTED);
210
211 /*
212 * If there are already the maximum number of leaf entries in
213 * the block, if there are no stale entries it won't fit.
214 * Caller will do a split. If there are stale entries we'll do
215 * a compact.
216 */
217
Nathan Scotta818e5d2006-03-17 17:28:07 +1100218 if (be16_to_cpu(leaf->hdr.count) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!leaf->hdr.stale)
220 return XFS_ERROR(ENOSPC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100221 compact = be16_to_cpu(leaf->hdr.stale) > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else
223 compact = 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100224 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100225 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100226 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (args->justcheck)
229 return 0;
230
231 /*
232 * Compact out all but one stale leaf entry. Leaves behind
233 * the entry closest to index.
234 */
235 if (compact) {
236 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
237 &lfloglow, &lfloghigh);
238 }
239 /*
240 * Set impossible logging indices for this case.
241 */
242 else if (leaf->hdr.stale) {
Nathan Scotta818e5d2006-03-17 17:28:07 +1100243 lfloglow = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 lfloghigh = -1;
245 }
246 /*
247 * No stale entries, just insert a space for the new entry.
248 */
249 if (!leaf->hdr.stale) {
250 lep = &leaf->ents[index];
Nathan Scotta818e5d2006-03-17 17:28:07 +1100251 if (index < be16_to_cpu(leaf->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 memmove(lep + 1, lep,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100253 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 lfloglow = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100255 lfloghigh = be16_to_cpu(leaf->hdr.count);
256 be16_add(&leaf->hdr.count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 /*
259 * There are stale entries. We'll use one for the new entry.
260 */
261 else {
262 /*
263 * If we didn't do a compact then we need to figure out
264 * which stale entry will be used.
265 */
266 if (compact == 0) {
267 /*
268 * Find first stale entry before our insertion point.
269 */
270 for (lowstale = index - 1;
271 lowstale >= 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100272 be32_to_cpu(leaf->ents[lowstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 XFS_DIR2_NULL_DATAPTR;
274 lowstale--)
275 continue;
276 /*
277 * Find next stale entry after insertion point.
278 * Stop looking if the answer would be worse than
279 * lowstale already found.
280 */
281 for (highstale = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100282 highstale < be16_to_cpu(leaf->hdr.count) &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100283 be32_to_cpu(leaf->ents[highstale].address) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 XFS_DIR2_NULL_DATAPTR &&
285 (lowstale < 0 ||
286 index - lowstale - 1 >= highstale - index);
287 highstale++)
288 continue;
289 }
290 /*
291 * Using the low stale entry.
292 * Shift entries up toward the stale slot.
293 */
294 if (lowstale >= 0 &&
Nathan Scotta818e5d2006-03-17 17:28:07 +1100295 (highstale == be16_to_cpu(leaf->hdr.count) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 index - lowstale - 1 < highstale - index)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100297 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 XFS_DIR2_NULL_DATAPTR);
299 ASSERT(index - lowstale - 1 >= 0);
300 if (index - lowstale - 1 > 0)
301 memmove(&leaf->ents[lowstale],
302 &leaf->ents[lowstale + 1],
303 (index - lowstale - 1) * sizeof(*lep));
304 lep = &leaf->ents[index - 1];
305 lfloglow = MIN(lowstale, lfloglow);
306 lfloghigh = MAX(index - 1, lfloghigh);
307 }
308 /*
309 * Using the high stale entry.
310 * Shift entries down toward the stale slot.
311 */
312 else {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100313 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 XFS_DIR2_NULL_DATAPTR);
315 ASSERT(highstale - index >= 0);
316 if (highstale - index > 0)
317 memmove(&leaf->ents[index + 1],
318 &leaf->ents[index],
319 (highstale - index) * sizeof(*lep));
320 lep = &leaf->ents[index];
321 lfloglow = MIN(index, lfloglow);
322 lfloghigh = MAX(highstale, lfloghigh);
323 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100324 be16_add(&leaf->hdr.stale, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326 /*
327 * Insert the new entry, log everything.
328 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100329 lep->hashval = cpu_to_be32(args->hashval);
330 lep->address = cpu_to_be32(XFS_DIR2_DB_OFF_TO_DATAPTR(mp,
331 args->blkno, args->index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_dir2_leaf_log_header(tp, bp);
333 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
334 xfs_dir2_leafn_check(dp, bp);
335 return 0;
336}
337
338#ifdef DEBUG
339/*
340 * Check internal consistency of a leafn block.
341 */
342void
343xfs_dir2_leafn_check(
344 xfs_inode_t *dp, /* incore directory inode */
345 xfs_dabuf_t *bp) /* leaf buffer */
346{
347 int i; /* leaf index */
348 xfs_dir2_leaf_t *leaf; /* leaf structure */
349 xfs_mount_t *mp; /* filesystem mount point */
350 int stale; /* count of stale leaves */
351
352 leaf = bp->data;
353 mp = dp->i_mount;
Nathan Scott89da0542006-03-17 17:28:40 +1100354 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100355 ASSERT(be16_to_cpu(leaf->hdr.count) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
356 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
357 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100358 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
359 be32_to_cpu(leaf->ents[i + 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100361 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 stale++;
363 }
Nathan Scotta818e5d2006-03-17 17:28:07 +1100364 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366#endif /* DEBUG */
367
368/*
369 * Return the last hash value in the leaf.
370 * Stale entries are ok.
371 */
372xfs_dahash_t /* hash value */
373xfs_dir2_leafn_lasthash(
374 xfs_dabuf_t *bp, /* leaf buffer */
375 int *count) /* count of entries in leaf */
376{
377 xfs_dir2_leaf_t *leaf; /* leaf structure */
378
379 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100380 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (count)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100382 *count = be16_to_cpu(leaf->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!leaf->hdr.count)
384 return 0;
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100385 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
389 * Look up a leaf entry in a node-format leaf block.
390 * If this is an addname then the extrablk in state is a freespace block,
391 * otherwise it's a data block.
392 */
393int
394xfs_dir2_leafn_lookup_int(
395 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{
400 xfs_dabuf_t *curbp; /* current data/free buffer */
401 xfs_dir2_db_t curdb; /* current data block number */
402 xfs_dir2_db_t curfdb; /* current free block number */
403 xfs_dir2_data_entry_t *dep; /* data block entry */
404 xfs_inode_t *dp; /* incore directory inode */
405 int error; /* error return value */
406 int fi; /* free entry index */
407 xfs_dir2_free_t *free=NULL; /* free block structure */
408 int index; /* leaf entry index */
409 xfs_dir2_leaf_t *leaf; /* leaf structure */
410 int length=0; /* length of new data entry */
411 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
412 xfs_mount_t *mp; /* filesystem mount point */
413 xfs_dir2_db_t newdb; /* new data block number */
414 xfs_dir2_db_t newfdb; /* new free block number */
415 xfs_trans_t *tp; /* transaction pointer */
416
417 dp = args->dp;
418 tp = args->trans;
419 mp = dp->i_mount;
420 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100421 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#ifdef __KERNEL__
Nathan Scotta818e5d2006-03-17 17:28:07 +1100423 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424#endif
425 xfs_dir2_leafn_check(dp, bp);
426 /*
427 * Look up the hash value in the leaf entries.
428 */
429 index = xfs_dir2_leaf_search_hash(args, bp);
430 /*
431 * Do we have a buffer coming in?
432 */
433 if (state->extravalid)
434 curbp = state->extrablk.bp;
435 else
436 curbp = NULL;
437 /*
438 * For addname, it's a free block buffer, get the block number.
439 */
440 if (args->addname) {
441 curfdb = curbp ? state->extrablk.blkno : -1;
442 curdb = -1;
443 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
444 if ((free = (curbp ? curbp->data : NULL)))
Nathan Scott0ba962e2006-03-17 17:27:07 +1100445 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 /*
448 * For others, it's a data block buffer, get the block number.
449 */
450 else {
451 curfdb = -1;
452 curdb = curbp ? state->extrablk.blkno : -1;
453 }
454 /*
455 * Loop over leaf entries with the right hash value.
456 */
457 for (lep = &leaf->ents[index];
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100458 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 lep++, index++) {
460 /*
461 * Skip stale leaf entries.
462 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100463 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 continue;
465 /*
466 * Pull the data block number from the entry.
467 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100468 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * For addname, we're looking for a place to put the new entry.
471 * We want to use a data block with an entry of equal
472 * hash value to ours if there is one with room.
473 */
474 if (args->addname) {
475 /*
476 * If this block isn't the data block we already have
477 * in hand, take a look at it.
478 */
479 if (newdb != curdb) {
480 curdb = newdb;
481 /*
482 * Convert the data block to the free block
483 * holding its freespace information.
484 */
485 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
486 /*
487 * If it's not the one we have in hand,
488 * read it in.
489 */
490 if (newfdb != curfdb) {
491 /*
492 * If we had one before, drop it.
493 */
494 if (curbp)
495 xfs_da_brelse(tp, curbp);
496 /*
497 * Read the free block.
498 */
499 if ((error = xfs_da_read_buf(tp, dp,
500 XFS_DIR2_DB_TO_DA(mp,
501 newfdb),
502 -1, &curbp,
503 XFS_DATA_FORK))) {
504 return error;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 free = curbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100507 ASSERT(be32_to_cpu(free->hdr.magic) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 XFS_DIR2_FREE_MAGIC);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100509 ASSERT((be32_to_cpu(free->hdr.firstdb) %
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
511 0);
Nathan Scott0ba962e2006-03-17 17:27:07 +1100512 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ASSERT(curdb <
Nathan Scott0ba962e2006-03-17 17:27:07 +1100514 be32_to_cpu(free->hdr.firstdb) +
515 be32_to_cpu(free->hdr.nvalid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 /*
518 * Get the index for our entry.
519 */
520 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
521 /*
522 * If it has room, return it.
523 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100524 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
526 XFS_ERRLEVEL_LOW, mp);
Nathan Scotte10900742006-06-09 14:58:48 +1000527 if (curfdb != newfdb)
528 xfs_da_brelse(tp, curbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return XFS_ERROR(EFSCORRUPTED);
530 }
Nathan Scotte10900742006-06-09 14:58:48 +1000531 curfdb = newfdb;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100532 if (be16_to_cpu(free->bests[fi]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 *indexp = index;
534 state->extravalid = 1;
535 state->extrablk.bp = curbp;
536 state->extrablk.blkno = curfdb;
537 state->extrablk.index = fi;
538 state->extrablk.magic =
539 XFS_DIR2_FREE_MAGIC;
540 ASSERT(args->oknoent);
541 return XFS_ERROR(ENOENT);
542 }
543 }
544 }
545 /*
546 * Not adding a new entry, so we really want to find
547 * the name given to us.
548 */
549 else {
550 /*
551 * If it's a different data block, go get it.
552 */
553 if (newdb != curdb) {
554 /*
555 * If we had a block before, drop it.
556 */
557 if (curbp)
558 xfs_da_brelse(tp, curbp);
559 /*
560 * Read the data block.
561 */
562 if ((error =
563 xfs_da_read_buf(tp, dp,
564 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
565 &curbp, XFS_DATA_FORK))) {
566 return error;
567 }
568 xfs_dir2_data_check(dp, curbp);
569 curdb = newdb;
570 }
571 /*
572 * Point to the data entry.
573 */
574 dep = (xfs_dir2_data_entry_t *)
575 ((char *)curbp->data +
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100576 XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * Compare the entry, return it if it matches.
579 */
580 if (dep->namelen == args->namelen &&
581 dep->name[0] == args->name[0] &&
582 memcmp(dep->name, args->name, args->namelen) == 0) {
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000583 args->inumber = be64_to_cpu(dep->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 *indexp = index;
585 state->extravalid = 1;
586 state->extrablk.bp = curbp;
587 state->extrablk.blkno = curdb;
588 state->extrablk.index =
589 (int)((char *)dep -
590 (char *)curbp->data);
591 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
592 return XFS_ERROR(EEXIST);
593 }
594 }
595 }
596 /*
597 * Didn't find a match.
598 * If we are holding a buffer, give it back in case our caller
599 * finds it useful.
600 */
601 if ((state->extravalid = (curbp != NULL))) {
602 state->extrablk.bp = curbp;
603 state->extrablk.index = -1;
604 /*
605 * For addname, giving back a free block.
606 */
607 if (args->addname) {
608 state->extrablk.blkno = curfdb;
609 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
610 }
611 /*
612 * For other callers, giving back a data block.
613 */
614 else {
615 state->extrablk.blkno = curdb;
616 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
617 }
618 }
619 /*
620 * Return the final index, that will be the insertion point.
621 */
622 *indexp = index;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100623 ASSERT(index == be16_to_cpu(leaf->hdr.count) || args->oknoent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return XFS_ERROR(ENOENT);
625}
626
627/*
628 * Move count leaf entries from source to destination leaf.
629 * Log entries and headers. Stale entries are preserved.
630 */
631static void
632xfs_dir2_leafn_moveents(
633 xfs_da_args_t *args, /* operation arguments */
634 xfs_dabuf_t *bp_s, /* source leaf buffer */
635 int start_s, /* source leaf index */
636 xfs_dabuf_t *bp_d, /* destination leaf buffer */
637 int start_d, /* destination leaf index */
638 int count) /* count of leaves to copy */
639{
640 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
641 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
642 int stale; /* count stale leaves copied */
643 xfs_trans_t *tp; /* transaction pointer */
644
645 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
646 start_d, count);
647 /*
648 * Silently return if nothing to do.
649 */
650 if (count == 0) {
651 return;
652 }
653 tp = args->trans;
654 leaf_s = bp_s->data;
655 leaf_d = bp_d->data;
656 /*
657 * If the destination index is not the end of the current
658 * destination leaf entries, open up a hole in the destination
659 * to hold the new entries.
660 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100661 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
Nathan Scotta818e5d2006-03-17 17:28:07 +1100663 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 sizeof(xfs_dir2_leaf_entry_t));
665 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100666 count + be16_to_cpu(leaf_d->hdr.count) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 /*
669 * If the source has stale leaves, count the ones in the copy range
670 * so we can update the header correctly.
671 */
672 if (leaf_s->hdr.stale) {
673 int i; /* temp leaf index */
674
675 for (i = start_s, stale = 0; i < start_s + count; i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100676 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 stale++;
678 }
679 } else
680 stale = 0;
681 /*
682 * Copy the leaf entries from source to destination.
683 */
684 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
685 count * sizeof(xfs_dir2_leaf_entry_t));
686 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
687 /*
688 * If there are source entries after the ones we copied,
689 * delete the ones we copied by sliding the next ones down.
690 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100691 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
693 count * sizeof(xfs_dir2_leaf_entry_t));
694 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
695 }
696 /*
697 * Update the headers and log them.
698 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100699 be16_add(&leaf_s->hdr.count, -(count));
700 be16_add(&leaf_s->hdr.stale, -(stale));
701 be16_add(&leaf_d->hdr.count, count);
702 be16_add(&leaf_d->hdr.stale, stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 xfs_dir2_leaf_log_header(tp, bp_s);
704 xfs_dir2_leaf_log_header(tp, bp_d);
705 xfs_dir2_leafn_check(args->dp, bp_s);
706 xfs_dir2_leafn_check(args->dp, bp_d);
707}
708
709/*
710 * Determine the sort order of two leaf blocks.
711 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
712 */
713int /* sort order */
714xfs_dir2_leafn_order(
715 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
716 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
717{
718 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
719 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
720
721 leaf1 = leaf1_bp->data;
722 leaf2 = leaf2_bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100723 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
724 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +1100725 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
726 be16_to_cpu(leaf2->hdr.count) > 0 &&
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100727 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
728 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
729 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 1;
731 return 0;
732}
733
734/*
735 * Rebalance leaf entries between two leaf blocks.
736 * This is actually only called when the second block is new,
737 * though the code deals with the general case.
738 * A new entry will be inserted in one of the blocks, and that
739 * entry is taken into account when balancing.
740 */
741static void
742xfs_dir2_leafn_rebalance(
743 xfs_da_state_t *state, /* btree cursor */
744 xfs_da_state_blk_t *blk1, /* first btree block */
745 xfs_da_state_blk_t *blk2) /* second btree block */
746{
747 xfs_da_args_t *args; /* operation arguments */
748 int count; /* count (& direction) leaves */
749 int isleft; /* new goes in left leaf */
750 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
751 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
752 int mid; /* midpoint leaf index */
753#ifdef DEBUG
754 int oldstale; /* old count of stale leaves */
755#endif
756 int oldsum; /* old total leaf count */
757 int swap; /* swapped leaf blocks */
758
759 args = state->args;
760 /*
761 * If the block order is wrong, swap the arguments.
762 */
763 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
764 xfs_da_state_blk_t *tmp; /* temp for block swap */
765
766 tmp = blk1;
767 blk1 = blk2;
768 blk2 = tmp;
769 }
770 leaf1 = blk1->bp->data;
771 leaf2 = blk2->bp->data;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100772 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773#ifdef DEBUG
Nathan Scotta818e5d2006-03-17 17:28:07 +1100774 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775#endif
776 mid = oldsum >> 1;
777 /*
778 * If the old leaf count was odd then the new one will be even,
779 * so we need to divide the new count evenly.
780 */
781 if (oldsum & 1) {
782 xfs_dahash_t midhash; /* middle entry hash value */
783
Nathan Scotta818e5d2006-03-17 17:28:07 +1100784 if (mid >= be16_to_cpu(leaf1->hdr.count))
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100785 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 else
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100787 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 isleft = args->hashval <= midhash;
789 }
790 /*
791 * If the old count is even then the new count is odd, so there's
792 * no preferred side for the new entry.
793 * Pick the left one.
794 */
795 else
796 isleft = 1;
797 /*
798 * Calculate moved entry count. Positive means left-to-right,
799 * negative means right-to-left. Then move the entries.
800 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100801 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (count > 0)
803 xfs_dir2_leafn_moveents(args, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100804 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 else if (count < 0)
806 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +1100807 be16_to_cpu(leaf1->hdr.count), count);
808 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
809 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /*
811 * Mark whether we're inserting into the old or new leaf.
812 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100813 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 state->inleaf = swap;
Nathan Scotta818e5d2006-03-17 17:28:07 +1100815 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 state->inleaf = !swap;
817 else
818 state->inleaf =
Nathan Scotta818e5d2006-03-17 17:28:07 +1100819 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /*
821 * Adjust the expected index for insertion.
822 */
823 if (!state->inleaf)
Nathan Scotta818e5d2006-03-17 17:28:07 +1100824 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /*
827 * Finally sanity check just to make sure we are not returning a negative index
828 */
829 if(blk2->index < 0) {
830 state->inleaf = 1;
831 blk2->index = 0;
832 cmn_err(CE_ALERT,
Nathan Scottc41564b2006-03-29 08:55:14 +1000833 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 "blk1->index %d\n",
835 blk1->index);
836 }
837}
838
839/*
840 * Remove an entry from a node directory.
841 * This removes the leaf entry and the data entry,
842 * and updates the free block if necessary.
843 */
844static int /* error */
845xfs_dir2_leafn_remove(
846 xfs_da_args_t *args, /* operation arguments */
847 xfs_dabuf_t *bp, /* leaf buffer */
848 int index, /* leaf entry index */
849 xfs_da_state_blk_t *dblk, /* data block */
850 int *rval) /* resulting block needs join */
851{
852 xfs_dir2_data_t *data; /* data block structure */
853 xfs_dir2_db_t db; /* data block number */
854 xfs_dabuf_t *dbp; /* data block buffer */
855 xfs_dir2_data_entry_t *dep; /* data block entry */
856 xfs_inode_t *dp; /* incore directory inode */
857 xfs_dir2_leaf_t *leaf; /* leaf structure */
858 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
859 int longest; /* longest data free entry */
860 int off; /* data block entry offset */
861 xfs_mount_t *mp; /* filesystem mount point */
862 int needlog; /* need to log data header */
863 int needscan; /* need to rescan data frees */
864 xfs_trans_t *tp; /* transaction pointer */
865
866 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
867 dp = args->dp;
868 tp = args->trans;
869 mp = dp->i_mount;
870 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +1100871 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Point to the entry we're removing.
874 */
875 lep = &leaf->ents[index];
876 /*
877 * Extract the data block and offset from the entry.
878 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100879 db = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 ASSERT(dblk->blkno == db);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100881 off = XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ASSERT(dblk->index == off);
883 /*
884 * Kill the leaf entry by marking it stale.
885 * Log the leaf block changes.
886 */
Nathan Scotta818e5d2006-03-17 17:28:07 +1100887 be16_add(&leaf->hdr.stale, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_dir2_leaf_log_header(tp, bp);
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100889 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 xfs_dir2_leaf_log_ents(tp, bp, index, index);
891 /*
892 * Make the data entry free. Keep track of the longest freespace
893 * in the data block in case it changes.
894 */
895 dbp = dblk->bp;
896 data = dbp->data;
897 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
Nathan Scott70e73f52006-03-17 17:26:52 +1100898 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 needlog = needscan = 0;
900 xfs_dir2_data_make_free(tp, dbp, off,
901 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
902 /*
903 * Rescan the data block freespaces for bestfree.
904 * Log the data block header if needed.
905 */
906 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +1000907 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (needlog)
909 xfs_dir2_data_log_header(tp, dbp);
910 xfs_dir2_data_check(dp, dbp);
911 /*
912 * If the longest data block freespace changes, need to update
913 * the corresponding freeblock entry.
914 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100915 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int error; /* error return value */
917 xfs_dabuf_t *fbp; /* freeblock buffer */
918 xfs_dir2_db_t fdb; /* freeblock block number */
919 int findex; /* index in freeblock entries */
920 xfs_dir2_free_t *free; /* freeblock structure */
921 int logfree; /* need to log free entry */
922
923 /*
924 * Convert the data block number to a free block,
925 * read in the free block.
926 */
927 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
928 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
929 -1, &fbp, XFS_DATA_FORK))) {
930 return error;
931 }
932 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100933 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
934 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 XFS_DIR2_MAX_FREE_BESTS(mp) *
936 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
937 /*
938 * Calculate which entry we need to fix.
939 */
940 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
Nathan Scott70e73f52006-03-17 17:26:52 +1100941 longest = be16_to_cpu(data->hdr.bestfree[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * If the data block is now empty we can get rid of it
944 * (usually).
945 */
946 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
947 /*
948 * Try to punch out the data block.
949 */
950 error = xfs_dir2_shrink_inode(args, db, dbp);
951 if (error == 0) {
952 dblk->bp = NULL;
953 data = NULL;
954 }
955 /*
956 * We can get ENOSPC if there's no space reservation.
957 * In this case just drop the buffer and some one else
958 * will eventually get rid of the empty block.
959 */
960 else if (error == ENOSPC && args->total == 0)
961 xfs_da_buf_done(dbp);
962 else
963 return error;
964 }
965 /*
966 * If we got rid of the data block, we can eliminate that entry
967 * in the free block.
968 */
969 if (data == NULL) {
970 /*
971 * One less used entry in the free table.
972 */
Mandy Kirkconnell477829e2006-06-09 17:13:04 +1000973 be32_add(&free->hdr.nused, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 xfs_dir2_free_log_header(tp, fbp);
975 /*
976 * If this was the last entry in the table, we can
977 * trim the table size back. There might be other
978 * entries at the end referring to non-existent
979 * data blocks, get those too.
980 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100981 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int i; /* free entry index */
983
984 for (i = findex - 1;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100985 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 i--)
987 continue;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100988 free->hdr.nvalid = cpu_to_be32(i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 logfree = 0;
990 }
991 /*
992 * Not the last entry, just punch it out.
993 */
994 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +1100995 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 logfree = 1;
997 }
998 /*
999 * If there are no useful entries left in the block,
1000 * get rid of the block if we can.
1001 */
1002 if (!free->hdr.nused) {
1003 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1004 if (error == 0) {
1005 fbp = NULL;
1006 logfree = 0;
1007 } else if (error != ENOSPC || args->total != 0)
1008 return error;
1009 /*
1010 * It's possible to get ENOSPC if there is no
1011 * space reservation. In this case some one
1012 * else will eventually get rid of this block.
1013 */
1014 }
1015 }
1016 /*
1017 * Data block is not empty, just set the free entry to
1018 * the new value.
1019 */
1020 else {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001021 free->bests[findex] = cpu_to_be16(longest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 logfree = 1;
1023 }
1024 /*
1025 * Log the free entry that changed, unless we got rid of it.
1026 */
1027 if (logfree)
1028 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1029 /*
1030 * Drop the buffer if we still have it.
1031 */
1032 if (fbp)
1033 xfs_da_buf_done(fbp);
1034 }
1035 xfs_dir2_leafn_check(dp, bp);
1036 /*
1037 * Return indication of whether this leaf block is emtpy enough
1038 * to justify trying to join it with a neighbor.
1039 */
1040 *rval =
1041 ((uint)sizeof(leaf->hdr) +
1042 (uint)sizeof(leaf->ents[0]) *
Nathan Scotta818e5d2006-03-17 17:28:07 +11001043 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 mp->m_dir_magicpct;
1045 return 0;
1046}
1047
1048/*
1049 * Split the leaf entries in the old block into old and new blocks.
1050 */
1051int /* error */
1052xfs_dir2_leafn_split(
1053 xfs_da_state_t *state, /* btree cursor */
1054 xfs_da_state_blk_t *oldblk, /* original block */
1055 xfs_da_state_blk_t *newblk) /* newly created block */
1056{
1057 xfs_da_args_t *args; /* operation arguments */
1058 xfs_dablk_t blkno; /* new leaf block number */
1059 int error; /* error return value */
1060 xfs_mount_t *mp; /* filesystem mount point */
1061
1062 /*
1063 * Allocate space for a new leaf node.
1064 */
1065 args = state->args;
1066 mp = args->dp->i_mount;
1067 ASSERT(args != NULL);
1068 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1069 error = xfs_da_grow_inode(args, &blkno);
1070 if (error) {
1071 return error;
1072 }
1073 /*
1074 * Initialize the new leaf block.
1075 */
1076 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1077 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1078 if (error) {
1079 return error;
1080 }
1081 newblk->blkno = blkno;
1082 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1083 /*
1084 * Rebalance the entries across the two leaves, link the new
1085 * block into the leaves.
1086 */
1087 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1088 error = xfs_da_blk_link(state, oldblk, newblk);
1089 if (error) {
1090 return error;
1091 }
1092 /*
1093 * Insert the new entry in the correct block.
1094 */
1095 if (state->inleaf)
1096 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1097 else
1098 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1099 /*
1100 * Update last hashval in each block since we added the name.
1101 */
1102 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1103 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1104 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1105 xfs_dir2_leafn_check(args->dp, newblk->bp);
1106 return error;
1107}
1108
1109/*
1110 * Check a leaf block and its neighbors to see if the block should be
1111 * collapsed into one or the other neighbor. Always keep the block
1112 * with the smaller block number.
1113 * If the current block is over 50% full, don't try to join it, return 0.
1114 * If the block is empty, fill in the state structure and return 2.
1115 * If it can be collapsed, fill in the state structure and return 1.
1116 * If nothing can be done, return 0.
1117 */
1118int /* error */
1119xfs_dir2_leafn_toosmall(
1120 xfs_da_state_t *state, /* btree cursor */
1121 int *action) /* resulting action to take */
1122{
1123 xfs_da_state_blk_t *blk; /* leaf block */
1124 xfs_dablk_t blkno; /* leaf block number */
1125 xfs_dabuf_t *bp; /* leaf buffer */
1126 int bytes; /* bytes in use */
1127 int count; /* leaf live entry count */
1128 int error; /* error return value */
1129 int forward; /* sibling block direction */
1130 int i; /* sibling counter */
1131 xfs_da_blkinfo_t *info; /* leaf block header */
1132 xfs_dir2_leaf_t *leaf; /* leaf structure */
1133 int rval; /* result from path_shift */
1134
1135 /*
1136 * Check for the degenerate case of the block being over 50% full.
1137 * If so, it's not worth even looking to see if we might be able
1138 * to coalesce with a sibling.
1139 */
1140 blk = &state->path.blk[state->path.active - 1];
1141 info = blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001142 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001144 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1146 if (bytes > (state->blocksize >> 1)) {
1147 /*
1148 * Blk over 50%, don't try to join.
1149 */
1150 *action = 0;
1151 return 0;
1152 }
1153 /*
1154 * Check for the degenerate case of the block being empty.
1155 * If the block is empty, we'll simply delete it, no need to
1156 * coalesce it with a sibling block. We choose (arbitrarily)
1157 * to merge with the forward block unless it is NULL.
1158 */
1159 if (count == 0) {
1160 /*
1161 * Make altpath point to the block we want to keep and
1162 * path point to the block we want to drop (this one).
1163 */
Nathan Scott89da0542006-03-17 17:28:40 +11001164 forward = (info->forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 memcpy(&state->altpath, &state->path, sizeof(state->path));
1166 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1167 &rval);
1168 if (error)
1169 return error;
1170 *action = rval ? 2 : 0;
1171 return 0;
1172 }
1173 /*
1174 * Examine each sibling block to see if we can coalesce with
1175 * at least 25% free space to spare. We need to figure out
1176 * whether to merge with the forward or the backward block.
1177 * We prefer coalescing with the lower numbered sibling so as
1178 * to shrink a directory over time.
1179 */
Nathan Scott89da0542006-03-17 17:28:40 +11001180 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Nathan Scott89da0542006-03-17 17:28:40 +11001182 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (blkno == 0)
1184 continue;
1185 /*
1186 * Read the sibling leaf block.
1187 */
1188 if ((error =
1189 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1190 -1, &bp, XFS_DATA_FORK))) {
1191 return error;
1192 }
1193 ASSERT(bp != NULL);
1194 /*
1195 * Count bytes in the two blocks combined.
1196 */
1197 leaf = (xfs_dir2_leaf_t *)info;
Nathan Scotta818e5d2006-03-17 17:28:07 +11001198 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 bytes = state->blocksize - (state->blocksize >> 2);
1200 leaf = bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001201 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001202 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 bytes -= count * (uint)sizeof(leaf->ents[0]);
1204 /*
1205 * Fits with at least 25% to spare.
1206 */
1207 if (bytes >= 0)
1208 break;
1209 xfs_da_brelse(state->args->trans, bp);
1210 }
1211 /*
1212 * Didn't like either block, give up.
1213 */
1214 if (i >= 2) {
1215 *action = 0;
1216 return 0;
1217 }
1218 /*
1219 * Done with the sibling leaf block here, drop the dabuf
1220 * so path_shift can get it.
1221 */
1222 xfs_da_buf_done(bp);
1223 /*
1224 * Make altpath point to the block we want to keep (the lower
1225 * numbered block) and path point to the block we want to drop.
1226 */
1227 memcpy(&state->altpath, &state->path, sizeof(state->path));
1228 if (blkno < blk->blkno)
1229 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1230 &rval);
1231 else
1232 error = xfs_da_path_shift(state, &state->path, forward, 0,
1233 &rval);
1234 if (error) {
1235 return error;
1236 }
1237 *action = rval ? 0 : 1;
1238 return 0;
1239}
1240
1241/*
1242 * Move all the leaf entries from drop_blk to save_blk.
1243 * This is done as part of a join operation.
1244 */
1245void
1246xfs_dir2_leafn_unbalance(
1247 xfs_da_state_t *state, /* cursor */
1248 xfs_da_state_blk_t *drop_blk, /* dead block */
1249 xfs_da_state_blk_t *save_blk) /* surviving block */
1250{
1251 xfs_da_args_t *args; /* operation arguments */
1252 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1253 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1254
1255 args = state->args;
1256 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1257 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1258 drop_leaf = drop_blk->bp->data;
1259 save_leaf = save_blk->bp->data;
Nathan Scott89da0542006-03-17 17:28:40 +11001260 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1261 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 /*
1263 * If there are any stale leaf entries, take this opportunity
1264 * to purge them.
1265 */
Nathan Scotta818e5d2006-03-17 17:28:07 +11001266 if (drop_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 xfs_dir2_leaf_compact(args, drop_blk->bp);
Nathan Scotta818e5d2006-03-17 17:28:07 +11001268 if (save_leaf->hdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 xfs_dir2_leaf_compact(args, save_blk->bp);
1270 /*
1271 * Move the entries from drop to the appropriate end of save.
1272 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001273 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 -07001274 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1275 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001276 be16_to_cpu(drop_leaf->hdr.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 else
1278 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
Nathan Scotta818e5d2006-03-17 17:28:07 +11001279 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001280 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 -07001281 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1282}
1283
1284/*
1285 * Top-level node form directory addname routine.
1286 */
1287int /* error */
1288xfs_dir2_node_addname(
1289 xfs_da_args_t *args) /* operation arguments */
1290{
1291 xfs_da_state_blk_t *blk; /* leaf block for insert */
1292 int error; /* error return value */
1293 int rval; /* sub-return value */
1294 xfs_da_state_t *state; /* btree cursor */
1295
1296 xfs_dir2_trace_args("node_addname", args);
1297 /*
1298 * Allocate and initialize the state (btree cursor).
1299 */
1300 state = xfs_da_state_alloc();
1301 state->args = args;
1302 state->mp = args->dp->i_mount;
1303 state->blocksize = state->mp->m_dirblksize;
1304 state->node_ents = state->mp->m_dir_node_ents;
1305 /*
1306 * Look up the name. We're not supposed to find it, but
1307 * this gives us the insertion point.
1308 */
1309 error = xfs_da_node_lookup_int(state, &rval);
1310 if (error)
1311 rval = error;
1312 if (rval != ENOENT) {
1313 goto done;
1314 }
1315 /*
1316 * Add the data entry to a data block.
1317 * Extravalid is set to a freeblock found by lookup.
1318 */
1319 rval = xfs_dir2_node_addname_int(args,
1320 state->extravalid ? &state->extrablk : NULL);
1321 if (rval) {
1322 goto done;
1323 }
1324 blk = &state->path.blk[state->path.active - 1];
1325 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1326 /*
1327 * Add the new leaf entry.
1328 */
1329 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1330 if (rval == 0) {
1331 /*
1332 * It worked, fix the hash values up the btree.
1333 */
1334 if (!args->justcheck)
1335 xfs_da_fixhashpath(state, &state->path);
1336 } else {
1337 /*
1338 * It didn't work, we need to split the leaf block.
1339 */
1340 if (args->total == 0) {
1341 ASSERT(rval == ENOSPC);
1342 goto done;
1343 }
1344 /*
1345 * Split the leaf block and insert the new entry.
1346 */
1347 rval = xfs_da_split(state);
1348 }
1349done:
1350 xfs_da_state_free(state);
1351 return rval;
1352}
1353
1354/*
1355 * Add the data entry for a node-format directory name addition.
1356 * The leaf entry is added in xfs_dir2_leafn_add.
1357 * We may enter with a freespace block that the lookup found.
1358 */
1359static int /* error */
1360xfs_dir2_node_addname_int(
1361 xfs_da_args_t *args, /* operation arguments */
1362 xfs_da_state_blk_t *fblk) /* optional freespace block */
1363{
1364 xfs_dir2_data_t *data; /* data block structure */
1365 xfs_dir2_db_t dbno; /* data block number */
1366 xfs_dabuf_t *dbp; /* data block buffer */
1367 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1368 xfs_inode_t *dp; /* incore directory inode */
1369 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1370 int error; /* error return value */
1371 xfs_dir2_db_t fbno; /* freespace block number */
1372 xfs_dabuf_t *fbp; /* freespace buffer */
1373 int findex; /* freespace entry index */
1374 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1375 xfs_dir2_db_t ifbno; /* initial freespace block no */
1376 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1377 int length; /* length of the new entry */
1378 int logfree; /* need to log free entry */
1379 xfs_mount_t *mp; /* filesystem mount point */
1380 int needlog; /* need to log data header */
1381 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001382 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 xfs_trans_t *tp; /* transaction pointer */
1384
1385 dp = args->dp;
1386 mp = dp->i_mount;
1387 tp = args->trans;
1388 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1389 /*
1390 * If we came in with a freespace block that means that lookup
1391 * found an entry with our hash value. This is the freespace
1392 * block for that data entry.
1393 */
1394 if (fblk) {
1395 fbp = fblk->bp;
1396 /*
1397 * Remember initial freespace block number.
1398 */
1399 ifbno = fblk->blkno;
1400 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001401 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 findex = fblk->index;
1403 /*
1404 * This means the free entry showed that the data block had
1405 * space for our entry, so we remembered it.
1406 * Use that data block.
1407 */
1408 if (findex >= 0) {
Nathan Scott0ba962e2006-03-17 17:27:07 +11001409 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1410 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1411 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1412 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 /*
1415 * The data block looked at didn't have enough room.
1416 * We'll start at the beginning of the freespace entries.
1417 */
1418 else {
1419 dbno = -1;
1420 findex = 0;
1421 }
1422 }
1423 /*
1424 * Didn't come in with a freespace block, so don't have a data block.
1425 */
1426 else {
1427 ifbno = dbno = -1;
1428 fbp = NULL;
1429 findex = 0;
1430 }
1431 /*
1432 * If we don't have a data block yet, we're going to scan the
1433 * freespace blocks looking for one. Figure out what the
1434 * highest freespace block number is.
1435 */
1436 if (dbno == -1) {
1437 xfs_fileoff_t fo; /* freespace block number */
1438
1439 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1440 return error;
1441 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1442 fbno = ifbno;
1443 }
1444 /*
1445 * While we haven't identified a data block, search the freeblock
1446 * data for a good data block. If we find a null freeblock entry,
1447 * indicating a hole in the data blocks, remember that.
1448 */
1449 while (dbno == -1) {
1450 /*
1451 * If we don't have a freeblock in hand, get the next one.
1452 */
1453 if (fbp == NULL) {
1454 /*
1455 * Happens the first time through unless lookup gave
1456 * us a freespace block to start with.
1457 */
1458 if (++fbno == 0)
1459 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1460 /*
1461 * If it's ifbno we already looked at it.
1462 */
1463 if (fbno == ifbno)
1464 fbno++;
1465 /*
1466 * If it's off the end we're done.
1467 */
1468 if (fbno >= lastfbno)
1469 break;
1470 /*
1471 * Read the block. There can be holes in the
1472 * freespace blocks, so this might not succeed.
1473 * This should be really rare, so there's no reason
1474 * to avoid it.
1475 */
1476 if ((error = xfs_da_read_buf(tp, dp,
1477 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1478 XFS_DATA_FORK))) {
1479 return error;
1480 }
1481 if (unlikely(fbp == NULL)) {
1482 continue;
1483 }
1484 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001485 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 findex = 0;
1487 }
1488 /*
1489 * Look at the current free entry. Is it good enough?
1490 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001491 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1492 be16_to_cpu(free->bests[findex]) >= length)
1493 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 else {
1495 /*
1496 * Are we done with the freeblock?
1497 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001498 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Drop the block.
1501 */
1502 xfs_da_brelse(tp, fbp);
1503 fbp = NULL;
1504 if (fblk && fblk->bp)
1505 fblk->bp = NULL;
1506 }
1507 }
1508 }
1509 /*
1510 * If we don't have a data block, we need to allocate one and make
1511 * the freespace entries refer to it.
1512 */
1513 if (unlikely(dbno == -1)) {
1514 /*
1515 * Not allowed to allocate, return failure.
1516 */
1517 if (args->justcheck || args->total == 0) {
1518 /*
1519 * Drop the freespace buffer unless it came from our
1520 * caller.
1521 */
1522 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1523 xfs_da_buf_done(fbp);
1524 return XFS_ERROR(ENOSPC);
1525 }
1526 /*
1527 * Allocate and initialize the new data block.
1528 */
1529 if (unlikely((error = xfs_dir2_grow_inode(args,
1530 XFS_DIR2_DATA_SPACE,
1531 &dbno)) ||
1532 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1533 /*
1534 * Drop the freespace buffer unless it came from our
1535 * caller.
1536 */
1537 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1538 xfs_da_buf_done(fbp);
1539 return error;
1540 }
1541 /*
1542 * If (somehow) we have a freespace block, get rid of it.
1543 */
1544 if (fbp)
1545 xfs_da_brelse(tp, fbp);
1546 if (fblk && fblk->bp)
1547 fblk->bp = NULL;
1548
1549 /*
1550 * Get the freespace block corresponding to the data block
1551 * that was just allocated.
1552 */
1553 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1554 if (unlikely(error = xfs_da_read_buf(tp, dp,
1555 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1556 XFS_DATA_FORK))) {
1557 xfs_da_buf_done(dbp);
1558 return error;
1559 }
1560 /*
1561 * If there wasn't a freespace block, the read will
1562 * return a NULL fbp. Allocate and initialize a new one.
1563 */
1564 if( fbp == NULL ) {
1565 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1566 &fbno))) {
1567 return error;
1568 }
1569
1570 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
1571 cmn_err(CE_ALERT,
1572 "xfs_dir2_node_addname_int: dir ino "
1573 "%llu needed freesp block %lld for\n"
1574 " data block %lld, got %lld\n"
1575 " ifbno %llu lastfbno %d\n",
1576 (unsigned long long)dp->i_ino,
1577 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1578 (long long)dbno, (long long)fbno,
1579 (unsigned long long)ifbno, lastfbno);
1580 if (fblk) {
1581 cmn_err(CE_ALERT,
1582 " fblk 0x%p blkno %llu "
1583 "index %d magic 0x%x\n",
1584 fblk,
1585 (unsigned long long)fblk->blkno,
1586 fblk->index,
1587 fblk->magic);
1588 } else {
1589 cmn_err(CE_ALERT,
1590 " ... fblk is NULL\n");
1591 }
1592 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1593 XFS_ERRLEVEL_LOW, mp);
1594 return XFS_ERROR(EFSCORRUPTED);
1595 }
1596
1597 /*
1598 * Get a buffer for the new block.
1599 */
1600 if ((error = xfs_da_get_buf(tp, dp,
1601 XFS_DIR2_DB_TO_DA(mp, fbno),
1602 -1, &fbp, XFS_DATA_FORK))) {
1603 return error;
1604 }
1605 ASSERT(fbp != NULL);
1606
1607 /*
1608 * Initialize the new block to be empty, and remember
1609 * its first slot as our empty slot.
1610 */
1611 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001612 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1613 free->hdr.firstdb = cpu_to_be32(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1615 XFS_DIR2_MAX_FREE_BESTS(mp));
1616 free->hdr.nvalid = 0;
1617 free->hdr.nused = 0;
1618 } else {
1619 free = fbp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001620 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622
1623 /*
1624 * Set the freespace block index from the data block number.
1625 */
1626 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1627 /*
1628 * If it's after the end of the current entries in the
1629 * freespace block, extend that table.
1630 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001631 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
Nathan Scott0ba962e2006-03-17 17:27:07 +11001633 free->hdr.nvalid = cpu_to_be32(findex + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /*
1635 * Tag new entry so nused will go up.
1636 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001637 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 /*
1640 * If this entry was for an empty data block
1641 * (this should always be true) then update the header.
1642 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001643 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
1644 be32_add(&free->hdr.nused, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 xfs_dir2_free_log_header(tp, fbp);
1646 }
1647 /*
1648 * Update the real value in the table.
1649 * We haven't allocated the data entry yet so this will
1650 * change again.
1651 */
1652 data = dbp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001653 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 logfree = 1;
1655 }
1656 /*
1657 * We had a data block so we don't have to make a new one.
1658 */
1659 else {
1660 /*
1661 * If just checking, we succeeded.
1662 */
1663 if (args->justcheck) {
1664 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1665 xfs_da_buf_done(fbp);
1666 return 0;
1667 }
1668 /*
1669 * Read the data block in.
1670 */
1671 if (unlikely(
1672 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
1673 -1, &dbp, XFS_DATA_FORK))) {
1674 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1675 xfs_da_buf_done(fbp);
1676 return error;
1677 }
1678 data = dbp->data;
1679 logfree = 0;
1680 }
Nathan Scott70e73f52006-03-17 17:26:52 +11001681 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
1683 * Point to the existing unused space.
1684 */
1685 dup = (xfs_dir2_data_unused_t *)
Nathan Scott70e73f52006-03-17 17:26:52 +11001686 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 needscan = needlog = 0;
1688 /*
1689 * Mark the first part of the unused space, inuse for us.
1690 */
1691 xfs_dir2_data_use_free(tp, dbp, dup,
1692 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1693 &needlog, &needscan);
1694 /*
1695 * Fill in the new entry and log it.
1696 */
1697 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001698 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 dep->namelen = args->namelen;
1700 memcpy(dep->name, args->name, dep->namelen);
1701 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
Nathan Scott3d693c62006-03-17 17:28:27 +11001702 *tagp = cpu_to_be16((char *)dep - (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 xfs_dir2_data_log_entry(tp, dbp, dep);
1704 /*
1705 * Rescan the block for bestfree if needed.
1706 */
1707 if (needscan)
Eric Sandeenef497f82007-05-08 13:48:49 +10001708 xfs_dir2_data_freescan(mp, data, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 /*
1710 * Log the data block header if needed.
1711 */
1712 if (needlog)
1713 xfs_dir2_data_log_header(tp, dbp);
1714 /*
1715 * If the freespace entry is now wrong, update it.
1716 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001717 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
Nathan Scott70e73f52006-03-17 17:26:52 +11001718 free->bests[findex] = data->hdr.bestfree[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 logfree = 1;
1720 }
1721 /*
1722 * Log the freespace entry if needed.
1723 */
1724 if (logfree)
1725 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1726 /*
1727 * If the caller didn't hand us the freespace block, drop it.
1728 */
1729 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1730 xfs_da_buf_done(fbp);
1731 /*
1732 * Return the data block and offset in args, then drop the data block.
1733 */
1734 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11001735 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 xfs_da_buf_done(dbp);
1737 return 0;
1738}
1739
1740/*
1741 * Lookup an entry in a node-format directory.
1742 * All the real work happens in xfs_da_node_lookup_int.
1743 * The only real output is the inode number of the entry.
1744 */
1745int /* error */
1746xfs_dir2_node_lookup(
1747 xfs_da_args_t *args) /* operation arguments */
1748{
1749 int error; /* error return value */
1750 int i; /* btree level */
1751 int rval; /* operation return value */
1752 xfs_da_state_t *state; /* btree cursor */
1753
1754 xfs_dir2_trace_args("node_lookup", args);
1755 /*
1756 * Allocate and initialize the btree cursor.
1757 */
1758 state = xfs_da_state_alloc();
1759 state->args = args;
1760 state->mp = args->dp->i_mount;
1761 state->blocksize = state->mp->m_dirblksize;
1762 state->node_ents = state->mp->m_dir_node_ents;
1763 /*
1764 * Fill in the path to the entry in the cursor.
1765 */
1766 error = xfs_da_node_lookup_int(state, &rval);
1767 if (error)
1768 rval = error;
1769 /*
1770 * Release the btree blocks and leaf block.
1771 */
1772 for (i = 0; i < state->path.active; i++) {
1773 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1774 state->path.blk[i].bp = NULL;
1775 }
1776 /*
1777 * Release the data block if we have it.
1778 */
1779 if (state->extravalid && state->extrablk.bp) {
1780 xfs_da_brelse(args->trans, state->extrablk.bp);
1781 state->extrablk.bp = NULL;
1782 }
1783 xfs_da_state_free(state);
1784 return rval;
1785}
1786
1787/*
1788 * Remove an entry from a node-format directory.
1789 */
1790int /* error */
1791xfs_dir2_node_removename(
1792 xfs_da_args_t *args) /* operation arguments */
1793{
1794 xfs_da_state_blk_t *blk; /* leaf block */
1795 int error; /* error return value */
1796 int rval; /* operation return value */
1797 xfs_da_state_t *state; /* btree cursor */
1798
1799 xfs_dir2_trace_args("node_removename", args);
1800 /*
1801 * Allocate and initialize the btree cursor.
1802 */
1803 state = xfs_da_state_alloc();
1804 state->args = args;
1805 state->mp = args->dp->i_mount;
1806 state->blocksize = state->mp->m_dirblksize;
1807 state->node_ents = state->mp->m_dir_node_ents;
1808 /*
1809 * Look up the entry we're deleting, set up the cursor.
1810 */
1811 error = xfs_da_node_lookup_int(state, &rval);
1812 if (error) {
1813 rval = error;
1814 }
1815 /*
1816 * Didn't find it, upper layer screwed up.
1817 */
1818 if (rval != EEXIST) {
1819 xfs_da_state_free(state);
1820 return rval;
1821 }
1822 blk = &state->path.blk[state->path.active - 1];
1823 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1824 ASSERT(state->extravalid);
1825 /*
1826 * Remove the leaf and data entries.
1827 * Extrablk refers to the data block.
1828 */
1829 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1830 &state->extrablk, &rval);
1831 if (error) {
1832 return error;
1833 }
1834 /*
1835 * Fix the hash values up the btree.
1836 */
1837 xfs_da_fixhashpath(state, &state->path);
1838 /*
1839 * If we need to join leaf blocks, do it.
1840 */
1841 if (rval && state->path.active > 1)
1842 error = xfs_da_join(state);
1843 /*
1844 * If no errors so far, try conversion to leaf format.
1845 */
1846 if (!error)
1847 error = xfs_dir2_node_to_leaf(state);
1848 xfs_da_state_free(state);
1849 return error;
1850}
1851
1852/*
1853 * Replace an entry's inode number in a node-format directory.
1854 */
1855int /* error */
1856xfs_dir2_node_replace(
1857 xfs_da_args_t *args) /* operation arguments */
1858{
1859 xfs_da_state_blk_t *blk; /* leaf block */
1860 xfs_dir2_data_t *data; /* data block structure */
1861 xfs_dir2_data_entry_t *dep; /* data entry changed */
1862 int error; /* error return value */
1863 int i; /* btree level */
1864 xfs_ino_t inum; /* new inode number */
1865 xfs_dir2_leaf_t *leaf; /* leaf structure */
1866 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1867 int rval; /* internal return value */
1868 xfs_da_state_t *state; /* btree cursor */
1869
1870 xfs_dir2_trace_args("node_replace", args);
1871 /*
1872 * Allocate and initialize the btree cursor.
1873 */
1874 state = xfs_da_state_alloc();
1875 state->args = args;
1876 state->mp = args->dp->i_mount;
1877 state->blocksize = state->mp->m_dirblksize;
1878 state->node_ents = state->mp->m_dir_node_ents;
1879 inum = args->inumber;
1880 /*
1881 * Lookup the entry to change in the btree.
1882 */
1883 error = xfs_da_node_lookup_int(state, &rval);
1884 if (error) {
1885 rval = error;
1886 }
1887 /*
1888 * It should be found, since the vnodeops layer has looked it up
1889 * and locked it. But paranoia is good.
1890 */
1891 if (rval == EEXIST) {
1892 /*
1893 * Find the leaf entry.
1894 */
1895 blk = &state->path.blk[state->path.active - 1];
1896 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1897 leaf = blk->bp->data;
1898 lep = &leaf->ents[blk->index];
1899 ASSERT(state->extravalid);
1900 /*
1901 * Point to the data entry.
1902 */
1903 data = state->extrablk.bp->data;
Nathan Scott70e73f52006-03-17 17:26:52 +11001904 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 dep = (xfs_dir2_data_entry_t *)
1906 ((char *)data +
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001907 XFS_DIR2_DATAPTR_TO_OFF(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001908 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * Fill in the new inode number and log the entry.
1911 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001912 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1914 rval = 0;
1915 }
1916 /*
1917 * Didn't find it, and we're holding a data block. Drop it.
1918 */
1919 else if (state->extravalid) {
1920 xfs_da_brelse(args->trans, state->extrablk.bp);
1921 state->extrablk.bp = NULL;
1922 }
1923 /*
1924 * Release all the buffers in the cursor.
1925 */
1926 for (i = 0; i < state->path.active; i++) {
1927 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1928 state->path.blk[i].bp = NULL;
1929 }
1930 xfs_da_state_free(state);
1931 return rval;
1932}
1933
1934/*
1935 * Trim off a trailing empty freespace block.
1936 * Return (in rvalp) 1 if we did it, 0 if not.
1937 */
1938int /* error */
1939xfs_dir2_node_trim_free(
1940 xfs_da_args_t *args, /* operation arguments */
1941 xfs_fileoff_t fo, /* free block number */
1942 int *rvalp) /* out: did something */
1943{
1944 xfs_dabuf_t *bp; /* freespace buffer */
1945 xfs_inode_t *dp; /* incore directory inode */
1946 int error; /* error return code */
1947 xfs_dir2_free_t *free; /* freespace structure */
1948 xfs_mount_t *mp; /* filesystem mount point */
1949 xfs_trans_t *tp; /* transaction pointer */
1950
1951 dp = args->dp;
1952 mp = dp->i_mount;
1953 tp = args->trans;
1954 /*
1955 * Read the freespace block.
1956 */
1957 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1958 XFS_DATA_FORK))) {
1959 return error;
1960 }
1961
1962 /*
1963 * There can be holes in freespace. If fo is a hole, there's
1964 * nothing to do.
1965 */
1966 if (bp == NULL) {
1967 return 0;
1968 }
1969 free = bp->data;
Nathan Scott0ba962e2006-03-17 17:27:07 +11001970 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * If there are used entries, there's nothing to do.
1973 */
Nathan Scott0ba962e2006-03-17 17:27:07 +11001974 if (be32_to_cpu(free->hdr.nused) > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 xfs_da_brelse(tp, bp);
1976 *rvalp = 0;
1977 return 0;
1978 }
1979 /*
1980 * Blow the block away.
1981 */
1982 if ((error =
1983 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
1984 bp))) {
1985 /*
1986 * Can't fail with ENOSPC since that only happens with no
1987 * space reservation, when breaking up an extent into two
1988 * pieces. This is the last block of an extent.
1989 */
1990 ASSERT(error != ENOSPC);
1991 xfs_da_brelse(tp, bp);
1992 return error;
1993 }
1994 /*
1995 * Return that we succeeded.
1996 */
1997 *rvalp = 1;
1998 return 0;
1999}