[XFS] Fix a buffer refcount leak in dir2 code on a forced shutdown.
[deliverable/linux.git] / fs / xfs / xfs_dir2_node.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
1da177e4 21#include "xfs_log.h"
a844f451 22#include "xfs_inum.h"
1da177e4
LT
23#include "xfs_trans.h"
24#include "xfs_sb.h"
25#include "xfs_dir.h"
26#include "xfs_dir2.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
a844f451 29#include "xfs_da_btree.h"
1da177e4 30#include "xfs_bmap_btree.h"
1da177e4
LT
31#include "xfs_dir_sf.h"
32#include "xfs_dir2_sf.h"
a844f451 33#include "xfs_attr_sf.h"
1da177e4
LT
34#include "xfs_dinode.h"
35#include "xfs_inode.h"
36#include "xfs_bmap.h"
1da177e4
LT
37#include "xfs_dir2_data.h"
38#include "xfs_dir2_leaf.h"
39#include "xfs_dir2_block.h"
40#include "xfs_dir2_node.h"
41#include "xfs_dir2_trace.h"
42#include "xfs_error.h"
43
44/*
45 * Function declarations.
46 */
47static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp);
48static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index);
49#ifdef DEBUG
50static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp);
51#else
52#define xfs_dir2_leafn_check(dp, bp)
53#endif
54static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s,
55 int start_s, xfs_dabuf_t *bp_d, int start_d,
56 int count);
57static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
58 xfs_da_state_blk_t *blk1,
59 xfs_da_state_blk_t *blk2);
60static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp,
61 int index, xfs_da_state_blk_t *dblk,
62 int *rval);
63static int xfs_dir2_node_addname_int(xfs_da_args_t *args,
64 xfs_da_state_blk_t *fblk);
65
66/*
67 * Log entries from a freespace block.
68 */
69void
70xfs_dir2_free_log_bests(
71 xfs_trans_t *tp, /* transaction pointer */
72 xfs_dabuf_t *bp, /* freespace buffer */
73 int first, /* first entry to log */
74 int last) /* last entry to log */
75{
76 xfs_dir2_free_t *free; /* freespace structure */
77
78 free = bp->data;
0ba962ef 79 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
80 xfs_da_log_buf(tp, bp,
81 (uint)((char *)&free->bests[first] - (char *)free),
82 (uint)((char *)&free->bests[last] - (char *)free +
83 sizeof(free->bests[0]) - 1));
84}
85
86/*
87 * Log header from a freespace block.
88 */
89static void
90xfs_dir2_free_log_header(
91 xfs_trans_t *tp, /* transaction pointer */
92 xfs_dabuf_t *bp) /* freespace buffer */
93{
94 xfs_dir2_free_t *free; /* freespace structure */
95
96 free = bp->data;
0ba962ef 97 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
98 xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free),
99 (uint)(sizeof(xfs_dir2_free_hdr_t) - 1));
100}
101
102/*
103 * Convert a leaf-format directory to a node-format directory.
104 * We need to change the magic number of the leaf block, and copy
105 * the freespace table out of the leaf block into its own block.
106 */
107int /* error */
108xfs_dir2_leaf_to_node(
109 xfs_da_args_t *args, /* operation arguments */
110 xfs_dabuf_t *lbp) /* leaf buffer */
111{
112 xfs_inode_t *dp; /* incore directory inode */
113 int error; /* error return value */
114 xfs_dabuf_t *fbp; /* freespace buffer */
115 xfs_dir2_db_t fdb; /* freespace block number */
116 xfs_dir2_free_t *free; /* freespace structure */
68b3a102 117 __be16 *from; /* pointer to freespace entry */
1da177e4
LT
118 int i; /* leaf freespace index */
119 xfs_dir2_leaf_t *leaf; /* leaf structure */
120 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
121 xfs_mount_t *mp; /* filesystem mount point */
122 int n; /* count of live freespc ents */
123 xfs_dir2_data_off_t off; /* freespace entry value */
0ba962ef 124 __be16 *to; /* pointer to freespace entry */
1da177e4
LT
125 xfs_trans_t *tp; /* transaction pointer */
126
127 xfs_dir2_trace_args_b("leaf_to_node", args, lbp);
128 dp = args->dp;
129 mp = dp->i_mount;
130 tp = args->trans;
131 /*
132 * Add a freespace block to the directory.
133 */
134 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
135 return error;
136 }
137 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
138 /*
139 * Get the buffer for the new freespace block.
140 */
141 if ((error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb), -1, &fbp,
142 XFS_DATA_FORK))) {
143 return error;
144 }
145 ASSERT(fbp != NULL);
146 free = fbp->data;
147 leaf = lbp->data;
148 ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf);
149 /*
150 * Initialize the freespace block header.
151 */
0ba962ef 152 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1da177e4 153 free->hdr.firstdb = 0;
afbcb3f9 154 ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize);
0ba962ef 155 free->hdr.nvalid = ltp->bestcount;
1da177e4
LT
156 /*
157 * Copy freespace entries from the leaf block to the new block.
158 * Count active entries.
159 */
160 for (i = n = 0, from = XFS_DIR2_LEAF_BESTS_P(ltp), to = free->bests;
afbcb3f9 161 i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
68b3a102 162 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
1da177e4 163 n++;
0ba962ef 164 *to = cpu_to_be16(off);
1da177e4 165 }
0ba962ef 166 free->hdr.nused = cpu_to_be32(n);
89da0544 167 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
168 /*
169 * Log everything.
170 */
171 xfs_dir2_leaf_log_header(tp, lbp);
172 xfs_dir2_free_log_header(tp, fbp);
0ba962ef 173 xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1);
1da177e4
LT
174 xfs_da_buf_done(fbp);
175 xfs_dir2_leafn_check(dp, lbp);
176 return 0;
177}
178
179/*
180 * Add a leaf entry to a leaf block in a node-form directory.
181 * The other work necessary is done from the caller.
182 */
183static int /* error */
184xfs_dir2_leafn_add(
185 xfs_dabuf_t *bp, /* leaf buffer */
186 xfs_da_args_t *args, /* operation arguments */
187 int index) /* insertion pt for new entry */
188{
189 int compact; /* compacting stale leaves */
190 xfs_inode_t *dp; /* incore directory inode */
191 int highstale; /* next stale entry */
192 xfs_dir2_leaf_t *leaf; /* leaf structure */
193 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
194 int lfloghigh; /* high leaf entry logging */
195 int lfloglow; /* low leaf entry logging */
196 int lowstale; /* previous stale entry */
197 xfs_mount_t *mp; /* filesystem mount point */
198 xfs_trans_t *tp; /* transaction pointer */
199
200 xfs_dir2_trace_args_sb("leafn_add", args, index, bp);
201 dp = args->dp;
202 mp = dp->i_mount;
203 tp = args->trans;
204 leaf = bp->data;
205
206 /*
207 * Quick check just to make sure we are not going to index
208 * into other peoples memory
209 */
210 if (index < 0)
211 return XFS_ERROR(EFSCORRUPTED);
212
213 /*
214 * If there are already the maximum number of leaf entries in
215 * the block, if there are no stale entries it won't fit.
216 * Caller will do a split. If there are stale entries we'll do
217 * a compact.
218 */
219
a818e5de 220 if (be16_to_cpu(leaf->hdr.count) == XFS_DIR2_MAX_LEAF_ENTS(mp)) {
1da177e4
LT
221 if (!leaf->hdr.stale)
222 return XFS_ERROR(ENOSPC);
a818e5de 223 compact = be16_to_cpu(leaf->hdr.stale) > 1;
1da177e4
LT
224 } else
225 compact = 0;
3c1f9c15 226 ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval);
a818e5de 227 ASSERT(index == be16_to_cpu(leaf->hdr.count) ||
3c1f9c15 228 be32_to_cpu(leaf->ents[index].hashval) >= args->hashval);
1da177e4
LT
229
230 if (args->justcheck)
231 return 0;
232
233 /*
234 * Compact out all but one stale leaf entry. Leaves behind
235 * the entry closest to index.
236 */
237 if (compact) {
238 xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale,
239 &lfloglow, &lfloghigh);
240 }
241 /*
242 * Set impossible logging indices for this case.
243 */
244 else if (leaf->hdr.stale) {
a818e5de 245 lfloglow = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
246 lfloghigh = -1;
247 }
248 /*
249 * No stale entries, just insert a space for the new entry.
250 */
251 if (!leaf->hdr.stale) {
252 lep = &leaf->ents[index];
a818e5de 253 if (index < be16_to_cpu(leaf->hdr.count))
1da177e4 254 memmove(lep + 1, lep,
a818e5de 255 (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep));
1da177e4 256 lfloglow = index;
a818e5de
NS
257 lfloghigh = be16_to_cpu(leaf->hdr.count);
258 be16_add(&leaf->hdr.count, 1);
1da177e4
LT
259 }
260 /*
261 * There are stale entries. We'll use one for the new entry.
262 */
263 else {
264 /*
265 * If we didn't do a compact then we need to figure out
266 * which stale entry will be used.
267 */
268 if (compact == 0) {
269 /*
270 * Find first stale entry before our insertion point.
271 */
272 for (lowstale = index - 1;
273 lowstale >= 0 &&
3c1f9c15 274 be32_to_cpu(leaf->ents[lowstale].address) !=
1da177e4
LT
275 XFS_DIR2_NULL_DATAPTR;
276 lowstale--)
277 continue;
278 /*
279 * Find next stale entry after insertion point.
280 * Stop looking if the answer would be worse than
281 * lowstale already found.
282 */
283 for (highstale = index;
a818e5de 284 highstale < be16_to_cpu(leaf->hdr.count) &&
3c1f9c15 285 be32_to_cpu(leaf->ents[highstale].address) !=
1da177e4
LT
286 XFS_DIR2_NULL_DATAPTR &&
287 (lowstale < 0 ||
288 index - lowstale - 1 >= highstale - index);
289 highstale++)
290 continue;
291 }
292 /*
293 * Using the low stale entry.
294 * Shift entries up toward the stale slot.
295 */
296 if (lowstale >= 0 &&
a818e5de 297 (highstale == be16_to_cpu(leaf->hdr.count) ||
1da177e4 298 index - lowstale - 1 < highstale - index)) {
3c1f9c15 299 ASSERT(be32_to_cpu(leaf->ents[lowstale].address) ==
1da177e4
LT
300 XFS_DIR2_NULL_DATAPTR);
301 ASSERT(index - lowstale - 1 >= 0);
302 if (index - lowstale - 1 > 0)
303 memmove(&leaf->ents[lowstale],
304 &leaf->ents[lowstale + 1],
305 (index - lowstale - 1) * sizeof(*lep));
306 lep = &leaf->ents[index - 1];
307 lfloglow = MIN(lowstale, lfloglow);
308 lfloghigh = MAX(index - 1, lfloghigh);
309 }
310 /*
311 * Using the high stale entry.
312 * Shift entries down toward the stale slot.
313 */
314 else {
3c1f9c15 315 ASSERT(be32_to_cpu(leaf->ents[highstale].address) ==
1da177e4
LT
316 XFS_DIR2_NULL_DATAPTR);
317 ASSERT(highstale - index >= 0);
318 if (highstale - index > 0)
319 memmove(&leaf->ents[index + 1],
320 &leaf->ents[index],
321 (highstale - index) * sizeof(*lep));
322 lep = &leaf->ents[index];
323 lfloglow = MIN(index, lfloglow);
324 lfloghigh = MAX(highstale, lfloghigh);
325 }
a818e5de 326 be16_add(&leaf->hdr.stale, -1);
1da177e4
LT
327 }
328 /*
329 * Insert the new entry, log everything.
330 */
3c1f9c15
NS
331 lep->hashval = cpu_to_be32(args->hashval);
332 lep->address = cpu_to_be32(XFS_DIR2_DB_OFF_TO_DATAPTR(mp,
333 args->blkno, args->index));
1da177e4
LT
334 xfs_dir2_leaf_log_header(tp, bp);
335 xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
336 xfs_dir2_leafn_check(dp, bp);
337 return 0;
338}
339
340#ifdef DEBUG
341/*
342 * Check internal consistency of a leafn block.
343 */
344void
345xfs_dir2_leafn_check(
346 xfs_inode_t *dp, /* incore directory inode */
347 xfs_dabuf_t *bp) /* leaf buffer */
348{
349 int i; /* leaf index */
350 xfs_dir2_leaf_t *leaf; /* leaf structure */
351 xfs_mount_t *mp; /* filesystem mount point */
352 int stale; /* count of stale leaves */
353
354 leaf = bp->data;
355 mp = dp->i_mount;
89da0544 356 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
a818e5de
NS
357 ASSERT(be16_to_cpu(leaf->hdr.count) <= XFS_DIR2_MAX_LEAF_ENTS(mp));
358 for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) {
359 if (i + 1 < be16_to_cpu(leaf->hdr.count)) {
3c1f9c15
NS
360 ASSERT(be32_to_cpu(leaf->ents[i].hashval) <=
361 be32_to_cpu(leaf->ents[i + 1].hashval));
1da177e4 362 }
3c1f9c15 363 if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
364 stale++;
365 }
a818e5de 366 ASSERT(be16_to_cpu(leaf->hdr.stale) == stale);
1da177e4
LT
367}
368#endif /* DEBUG */
369
370/*
371 * Return the last hash value in the leaf.
372 * Stale entries are ok.
373 */
374xfs_dahash_t /* hash value */
375xfs_dir2_leafn_lasthash(
376 xfs_dabuf_t *bp, /* leaf buffer */
377 int *count) /* count of entries in leaf */
378{
379 xfs_dir2_leaf_t *leaf; /* leaf structure */
380
381 leaf = bp->data;
89da0544 382 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 383 if (count)
a818e5de 384 *count = be16_to_cpu(leaf->hdr.count);
1da177e4
LT
385 if (!leaf->hdr.count)
386 return 0;
3c1f9c15 387 return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval);
1da177e4
LT
388}
389
390/*
391 * Look up a leaf entry in a node-format leaf block.
392 * If this is an addname then the extrablk in state is a freespace block,
393 * otherwise it's a data block.
394 */
395int
396xfs_dir2_leafn_lookup_int(
397 xfs_dabuf_t *bp, /* leaf buffer */
398 xfs_da_args_t *args, /* operation arguments */
399 int *indexp, /* out: leaf entry index */
400 xfs_da_state_t *state) /* state to fill in */
401{
402 xfs_dabuf_t *curbp; /* current data/free buffer */
403 xfs_dir2_db_t curdb; /* current data block number */
404 xfs_dir2_db_t curfdb; /* current free block number */
405 xfs_dir2_data_entry_t *dep; /* data block entry */
406 xfs_inode_t *dp; /* incore directory inode */
407 int error; /* error return value */
408 int fi; /* free entry index */
409 xfs_dir2_free_t *free=NULL; /* free block structure */
410 int index; /* leaf entry index */
411 xfs_dir2_leaf_t *leaf; /* leaf structure */
412 int length=0; /* length of new data entry */
413 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
414 xfs_mount_t *mp; /* filesystem mount point */
415 xfs_dir2_db_t newdb; /* new data block number */
416 xfs_dir2_db_t newfdb; /* new free block number */
417 xfs_trans_t *tp; /* transaction pointer */
418
419 dp = args->dp;
420 tp = args->trans;
421 mp = dp->i_mount;
422 leaf = bp->data;
89da0544 423 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 424#ifdef __KERNEL__
a818e5de 425 ASSERT(be16_to_cpu(leaf->hdr.count) > 0);
1da177e4
LT
426#endif
427 xfs_dir2_leafn_check(dp, bp);
428 /*
429 * Look up the hash value in the leaf entries.
430 */
431 index = xfs_dir2_leaf_search_hash(args, bp);
432 /*
433 * Do we have a buffer coming in?
434 */
435 if (state->extravalid)
436 curbp = state->extrablk.bp;
437 else
438 curbp = NULL;
439 /*
440 * For addname, it's a free block buffer, get the block number.
441 */
442 if (args->addname) {
443 curfdb = curbp ? state->extrablk.blkno : -1;
444 curdb = -1;
445 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
446 if ((free = (curbp ? curbp->data : NULL)))
0ba962ef 447 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
448 }
449 /*
450 * For others, it's a data block buffer, get the block number.
451 */
452 else {
453 curfdb = -1;
454 curdb = curbp ? state->extrablk.blkno : -1;
455 }
456 /*
457 * Loop over leaf entries with the right hash value.
458 */
459 for (lep = &leaf->ents[index];
3c1f9c15 460 index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval;
1da177e4
LT
461 lep++, index++) {
462 /*
463 * Skip stale leaf entries.
464 */
3c1f9c15 465 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
466 continue;
467 /*
468 * Pull the data block number from the entry.
469 */
3c1f9c15 470 newdb = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address));
1da177e4
LT
471 /*
472 * For addname, we're looking for a place to put the new entry.
473 * We want to use a data block with an entry of equal
474 * hash value to ours if there is one with room.
475 */
476 if (args->addname) {
477 /*
478 * If this block isn't the data block we already have
479 * in hand, take a look at it.
480 */
481 if (newdb != curdb) {
482 curdb = newdb;
483 /*
484 * Convert the data block to the free block
485 * holding its freespace information.
486 */
487 newfdb = XFS_DIR2_DB_TO_FDB(mp, newdb);
488 /*
489 * If it's not the one we have in hand,
490 * read it in.
491 */
492 if (newfdb != curfdb) {
493 /*
494 * If we had one before, drop it.
495 */
496 if (curbp)
497 xfs_da_brelse(tp, curbp);
498 /*
499 * Read the free block.
500 */
501 if ((error = xfs_da_read_buf(tp, dp,
502 XFS_DIR2_DB_TO_DA(mp,
503 newfdb),
504 -1, &curbp,
505 XFS_DATA_FORK))) {
506 return error;
507 }
1da177e4 508 free = curbp->data;
0ba962ef 509 ASSERT(be32_to_cpu(free->hdr.magic) ==
1da177e4 510 XFS_DIR2_FREE_MAGIC);
0ba962ef 511 ASSERT((be32_to_cpu(free->hdr.firstdb) %
1da177e4
LT
512 XFS_DIR2_MAX_FREE_BESTS(mp)) ==
513 0);
0ba962ef 514 ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb);
1da177e4 515 ASSERT(curdb <
0ba962ef
NS
516 be32_to_cpu(free->hdr.firstdb) +
517 be32_to_cpu(free->hdr.nvalid));
1da177e4
LT
518 }
519 /*
520 * Get the index for our entry.
521 */
522 fi = XFS_DIR2_DB_TO_FDINDEX(mp, curdb);
523 /*
524 * If it has room, return it.
525 */
0ba962ef 526 if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) {
1da177e4
LT
527 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
528 XFS_ERRLEVEL_LOW, mp);
e1090074
NS
529 if (curfdb != newfdb)
530 xfs_da_brelse(tp, curbp);
1da177e4
LT
531 return XFS_ERROR(EFSCORRUPTED);
532 }
e1090074 533 curfdb = newfdb;
0ba962ef 534 if (be16_to_cpu(free->bests[fi]) >= length) {
1da177e4
LT
535 *indexp = index;
536 state->extravalid = 1;
537 state->extrablk.bp = curbp;
538 state->extrablk.blkno = curfdb;
539 state->extrablk.index = fi;
540 state->extrablk.magic =
541 XFS_DIR2_FREE_MAGIC;
542 ASSERT(args->oknoent);
543 return XFS_ERROR(ENOENT);
544 }
545 }
546 }
547 /*
548 * Not adding a new entry, so we really want to find
549 * the name given to us.
550 */
551 else {
552 /*
553 * If it's a different data block, go get it.
554 */
555 if (newdb != curdb) {
556 /*
557 * If we had a block before, drop it.
558 */
559 if (curbp)
560 xfs_da_brelse(tp, curbp);
561 /*
562 * Read the data block.
563 */
564 if ((error =
565 xfs_da_read_buf(tp, dp,
566 XFS_DIR2_DB_TO_DA(mp, newdb), -1,
567 &curbp, XFS_DATA_FORK))) {
568 return error;
569 }
570 xfs_dir2_data_check(dp, curbp);
571 curdb = newdb;
572 }
573 /*
574 * Point to the data entry.
575 */
576 dep = (xfs_dir2_data_entry_t *)
577 ((char *)curbp->data +
3c1f9c15 578 XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address)));
1da177e4
LT
579 /*
580 * Compare the entry, return it if it matches.
581 */
582 if (dep->namelen == args->namelen &&
583 dep->name[0] == args->name[0] &&
584 memcmp(dep->name, args->name, args->namelen) == 0) {
ff9901c1 585 args->inumber = be64_to_cpu(dep->inumber);
1da177e4
LT
586 *indexp = index;
587 state->extravalid = 1;
588 state->extrablk.bp = curbp;
589 state->extrablk.blkno = curdb;
590 state->extrablk.index =
591 (int)((char *)dep -
592 (char *)curbp->data);
593 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
594 return XFS_ERROR(EEXIST);
595 }
596 }
597 }
598 /*
599 * Didn't find a match.
600 * If we are holding a buffer, give it back in case our caller
601 * finds it useful.
602 */
603 if ((state->extravalid = (curbp != NULL))) {
604 state->extrablk.bp = curbp;
605 state->extrablk.index = -1;
606 /*
607 * For addname, giving back a free block.
608 */
609 if (args->addname) {
610 state->extrablk.blkno = curfdb;
611 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
612 }
613 /*
614 * For other callers, giving back a data block.
615 */
616 else {
617 state->extrablk.blkno = curdb;
618 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
619 }
620 }
621 /*
622 * Return the final index, that will be the insertion point.
623 */
624 *indexp = index;
a818e5de 625 ASSERT(index == be16_to_cpu(leaf->hdr.count) || args->oknoent);
1da177e4
LT
626 return XFS_ERROR(ENOENT);
627}
628
629/*
630 * Move count leaf entries from source to destination leaf.
631 * Log entries and headers. Stale entries are preserved.
632 */
633static void
634xfs_dir2_leafn_moveents(
635 xfs_da_args_t *args, /* operation arguments */
636 xfs_dabuf_t *bp_s, /* source leaf buffer */
637 int start_s, /* source leaf index */
638 xfs_dabuf_t *bp_d, /* destination leaf buffer */
639 int start_d, /* destination leaf index */
640 int count) /* count of leaves to copy */
641{
642 xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */
643 xfs_dir2_leaf_t *leaf_s; /* source leaf structure */
644 int stale; /* count stale leaves copied */
645 xfs_trans_t *tp; /* transaction pointer */
646
647 xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d,
648 start_d, count);
649 /*
650 * Silently return if nothing to do.
651 */
652 if (count == 0) {
653 return;
654 }
655 tp = args->trans;
656 leaf_s = bp_s->data;
657 leaf_d = bp_d->data;
658 /*
659 * If the destination index is not the end of the current
660 * destination leaf entries, open up a hole in the destination
661 * to hold the new entries.
662 */
a818e5de 663 if (start_d < be16_to_cpu(leaf_d->hdr.count)) {
1da177e4 664 memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d],
a818e5de 665 (be16_to_cpu(leaf_d->hdr.count) - start_d) *
1da177e4
LT
666 sizeof(xfs_dir2_leaf_entry_t));
667 xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count,
a818e5de 668 count + be16_to_cpu(leaf_d->hdr.count) - 1);
1da177e4
LT
669 }
670 /*
671 * If the source has stale leaves, count the ones in the copy range
672 * so we can update the header correctly.
673 */
674 if (leaf_s->hdr.stale) {
675 int i; /* temp leaf index */
676
677 for (i = start_s, stale = 0; i < start_s + count; i++) {
3c1f9c15 678 if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR)
1da177e4
LT
679 stale++;
680 }
681 } else
682 stale = 0;
683 /*
684 * Copy the leaf entries from source to destination.
685 */
686 memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s],
687 count * sizeof(xfs_dir2_leaf_entry_t));
688 xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
689 /*
690 * If there are source entries after the ones we copied,
691 * delete the ones we copied by sliding the next ones down.
692 */
a818e5de 693 if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) {
1da177e4
LT
694 memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count],
695 count * sizeof(xfs_dir2_leaf_entry_t));
696 xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
697 }
698 /*
699 * Update the headers and log them.
700 */
a818e5de
NS
701 be16_add(&leaf_s->hdr.count, -(count));
702 be16_add(&leaf_s->hdr.stale, -(stale));
703 be16_add(&leaf_d->hdr.count, count);
704 be16_add(&leaf_d->hdr.stale, stale);
1da177e4
LT
705 xfs_dir2_leaf_log_header(tp, bp_s);
706 xfs_dir2_leaf_log_header(tp, bp_d);
707 xfs_dir2_leafn_check(args->dp, bp_s);
708 xfs_dir2_leafn_check(args->dp, bp_d);
709}
710
711/*
712 * Determine the sort order of two leaf blocks.
713 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
714 */
715int /* sort order */
716xfs_dir2_leafn_order(
717 xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */
718 xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */
719{
720 xfs_dir2_leaf_t *leaf1; /* leaf1 structure */
721 xfs_dir2_leaf_t *leaf2; /* leaf2 structure */
722
723 leaf1 = leaf1_bp->data;
724 leaf2 = leaf2_bp->data;
89da0544
NS
725 ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
726 ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
a818e5de
NS
727 if (be16_to_cpu(leaf1->hdr.count) > 0 &&
728 be16_to_cpu(leaf2->hdr.count) > 0 &&
3c1f9c15
NS
729 (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) ||
730 be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) <
731 be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval)))
1da177e4
LT
732 return 1;
733 return 0;
734}
735
736/*
737 * Rebalance leaf entries between two leaf blocks.
738 * This is actually only called when the second block is new,
739 * though the code deals with the general case.
740 * A new entry will be inserted in one of the blocks, and that
741 * entry is taken into account when balancing.
742 */
743static void
744xfs_dir2_leafn_rebalance(
745 xfs_da_state_t *state, /* btree cursor */
746 xfs_da_state_blk_t *blk1, /* first btree block */
747 xfs_da_state_blk_t *blk2) /* second btree block */
748{
749 xfs_da_args_t *args; /* operation arguments */
750 int count; /* count (& direction) leaves */
751 int isleft; /* new goes in left leaf */
752 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
753 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
754 int mid; /* midpoint leaf index */
755#ifdef DEBUG
756 int oldstale; /* old count of stale leaves */
757#endif
758 int oldsum; /* old total leaf count */
759 int swap; /* swapped leaf blocks */
760
761 args = state->args;
762 /*
763 * If the block order is wrong, swap the arguments.
764 */
765 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
766 xfs_da_state_blk_t *tmp; /* temp for block swap */
767
768 tmp = blk1;
769 blk1 = blk2;
770 blk2 = tmp;
771 }
772 leaf1 = blk1->bp->data;
773 leaf2 = blk2->bp->data;
a818e5de 774 oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count);
1da177e4 775#ifdef DEBUG
a818e5de 776 oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale);
1da177e4
LT
777#endif
778 mid = oldsum >> 1;
779 /*
780 * If the old leaf count was odd then the new one will be even,
781 * so we need to divide the new count evenly.
782 */
783 if (oldsum & 1) {
784 xfs_dahash_t midhash; /* middle entry hash value */
785
a818e5de 786 if (mid >= be16_to_cpu(leaf1->hdr.count))
3c1f9c15 787 midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval);
1da177e4 788 else
3c1f9c15 789 midhash = be32_to_cpu(leaf1->ents[mid].hashval);
1da177e4
LT
790 isleft = args->hashval <= midhash;
791 }
792 /*
793 * If the old count is even then the new count is odd, so there's
794 * no preferred side for the new entry.
795 * Pick the left one.
796 */
797 else
798 isleft = 1;
799 /*
800 * Calculate moved entry count. Positive means left-to-right,
801 * negative means right-to-left. Then move the entries.
802 */
a818e5de 803 count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0);
1da177e4
LT
804 if (count > 0)
805 xfs_dir2_leafn_moveents(args, blk1->bp,
a818e5de 806 be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count);
1da177e4
LT
807 else if (count < 0)
808 xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp,
a818e5de
NS
809 be16_to_cpu(leaf1->hdr.count), count);
810 ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum);
811 ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale);
1da177e4
LT
812 /*
813 * Mark whether we're inserting into the old or new leaf.
814 */
a818e5de 815 if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count))
1da177e4 816 state->inleaf = swap;
a818e5de 817 else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count))
1da177e4
LT
818 state->inleaf = !swap;
819 else
820 state->inleaf =
a818e5de 821 swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count));
1da177e4
LT
822 /*
823 * Adjust the expected index for insertion.
824 */
825 if (!state->inleaf)
a818e5de 826 blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count);
1da177e4
LT
827
828 /*
829 * Finally sanity check just to make sure we are not returning a negative index
830 */
831 if(blk2->index < 0) {
832 state->inleaf = 1;
833 blk2->index = 0;
834 cmn_err(CE_ALERT,
c41564b5 835 "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: "
1da177e4
LT
836 "blk1->index %d\n",
837 blk1->index);
838 }
839}
840
841/*
842 * Remove an entry from a node directory.
843 * This removes the leaf entry and the data entry,
844 * and updates the free block if necessary.
845 */
846static int /* error */
847xfs_dir2_leafn_remove(
848 xfs_da_args_t *args, /* operation arguments */
849 xfs_dabuf_t *bp, /* leaf buffer */
850 int index, /* leaf entry index */
851 xfs_da_state_blk_t *dblk, /* data block */
852 int *rval) /* resulting block needs join */
853{
854 xfs_dir2_data_t *data; /* data block structure */
855 xfs_dir2_db_t db; /* data block number */
856 xfs_dabuf_t *dbp; /* data block buffer */
857 xfs_dir2_data_entry_t *dep; /* data block entry */
858 xfs_inode_t *dp; /* incore directory inode */
859 xfs_dir2_leaf_t *leaf; /* leaf structure */
860 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
861 int longest; /* longest data free entry */
862 int off; /* data block entry offset */
863 xfs_mount_t *mp; /* filesystem mount point */
864 int needlog; /* need to log data header */
865 int needscan; /* need to rescan data frees */
866 xfs_trans_t *tp; /* transaction pointer */
867
868 xfs_dir2_trace_args_sb("leafn_remove", args, index, bp);
869 dp = args->dp;
870 tp = args->trans;
871 mp = dp->i_mount;
872 leaf = bp->data;
89da0544 873 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
874 /*
875 * Point to the entry we're removing.
876 */
877 lep = &leaf->ents[index];
878 /*
879 * Extract the data block and offset from the entry.
880 */
3c1f9c15 881 db = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address));
1da177e4 882 ASSERT(dblk->blkno == db);
3c1f9c15 883 off = XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address));
1da177e4
LT
884 ASSERT(dblk->index == off);
885 /*
886 * Kill the leaf entry by marking it stale.
887 * Log the leaf block changes.
888 */
a818e5de 889 be16_add(&leaf->hdr.stale, 1);
1da177e4 890 xfs_dir2_leaf_log_header(tp, bp);
3c1f9c15 891 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
1da177e4
LT
892 xfs_dir2_leaf_log_ents(tp, bp, index, index);
893 /*
894 * Make the data entry free. Keep track of the longest freespace
895 * in the data block in case it changes.
896 */
897 dbp = dblk->bp;
898 data = dbp->data;
899 dep = (xfs_dir2_data_entry_t *)((char *)data + off);
70e73f59 900 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1da177e4
LT
901 needlog = needscan = 0;
902 xfs_dir2_data_make_free(tp, dbp, off,
903 XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan);
904 /*
905 * Rescan the data block freespaces for bestfree.
906 * Log the data block header if needed.
907 */
908 if (needscan)
909 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
910 if (needlog)
911 xfs_dir2_data_log_header(tp, dbp);
912 xfs_dir2_data_check(dp, dbp);
913 /*
914 * If the longest data block freespace changes, need to update
915 * the corresponding freeblock entry.
916 */
70e73f59 917 if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) {
1da177e4
LT
918 int error; /* error return value */
919 xfs_dabuf_t *fbp; /* freeblock buffer */
920 xfs_dir2_db_t fdb; /* freeblock block number */
921 int findex; /* index in freeblock entries */
922 xfs_dir2_free_t *free; /* freeblock structure */
923 int logfree; /* need to log free entry */
924
925 /*
926 * Convert the data block number to a free block,
927 * read in the free block.
928 */
929 fdb = XFS_DIR2_DB_TO_FDB(mp, db);
930 if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, fdb),
931 -1, &fbp, XFS_DATA_FORK))) {
932 return error;
933 }
934 free = fbp->data;
0ba962ef
NS
935 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
936 ASSERT(be32_to_cpu(free->hdr.firstdb) ==
1da177e4
LT
937 XFS_DIR2_MAX_FREE_BESTS(mp) *
938 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
939 /*
940 * Calculate which entry we need to fix.
941 */
942 findex = XFS_DIR2_DB_TO_FDINDEX(mp, db);
70e73f59 943 longest = be16_to_cpu(data->hdr.bestfree[0].length);
1da177e4
LT
944 /*
945 * If the data block is now empty we can get rid of it
946 * (usually).
947 */
948 if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) {
949 /*
950 * Try to punch out the data block.
951 */
952 error = xfs_dir2_shrink_inode(args, db, dbp);
953 if (error == 0) {
954 dblk->bp = NULL;
955 data = NULL;
956 }
957 /*
958 * We can get ENOSPC if there's no space reservation.
959 * In this case just drop the buffer and some one else
960 * will eventually get rid of the empty block.
961 */
962 else if (error == ENOSPC && args->total == 0)
963 xfs_da_buf_done(dbp);
964 else
965 return error;
966 }
967 /*
968 * If we got rid of the data block, we can eliminate that entry
969 * in the free block.
970 */
971 if (data == NULL) {
972 /*
973 * One less used entry in the free table.
974 */
0ba962ef 975 free->hdr.nused = cpu_to_be32(-1);
1da177e4
LT
976 xfs_dir2_free_log_header(tp, fbp);
977 /*
978 * If this was the last entry in the table, we can
979 * trim the table size back. There might be other
980 * entries at the end referring to non-existent
981 * data blocks, get those too.
982 */
0ba962ef 983 if (findex == be32_to_cpu(free->hdr.nvalid) - 1) {
1da177e4
LT
984 int i; /* free entry index */
985
986 for (i = findex - 1;
0ba962ef 987 i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF;
1da177e4
LT
988 i--)
989 continue;
0ba962ef 990 free->hdr.nvalid = cpu_to_be32(i + 1);
1da177e4
LT
991 logfree = 0;
992 }
993 /*
994 * Not the last entry, just punch it out.
995 */
996 else {
0ba962ef 997 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
998 logfree = 1;
999 }
1000 /*
1001 * If there are no useful entries left in the block,
1002 * get rid of the block if we can.
1003 */
1004 if (!free->hdr.nused) {
1005 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1006 if (error == 0) {
1007 fbp = NULL;
1008 logfree = 0;
1009 } else if (error != ENOSPC || args->total != 0)
1010 return error;
1011 /*
1012 * It's possible to get ENOSPC if there is no
1013 * space reservation. In this case some one
1014 * else will eventually get rid of this block.
1015 */
1016 }
1017 }
1018 /*
1019 * Data block is not empty, just set the free entry to
1020 * the new value.
1021 */
1022 else {
0ba962ef 1023 free->bests[findex] = cpu_to_be16(longest);
1da177e4
LT
1024 logfree = 1;
1025 }
1026 /*
1027 * Log the free entry that changed, unless we got rid of it.
1028 */
1029 if (logfree)
1030 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1031 /*
1032 * Drop the buffer if we still have it.
1033 */
1034 if (fbp)
1035 xfs_da_buf_done(fbp);
1036 }
1037 xfs_dir2_leafn_check(dp, bp);
1038 /*
1039 * Return indication of whether this leaf block is emtpy enough
1040 * to justify trying to join it with a neighbor.
1041 */
1042 *rval =
1043 ((uint)sizeof(leaf->hdr) +
1044 (uint)sizeof(leaf->ents[0]) *
a818e5de 1045 (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) <
1da177e4
LT
1046 mp->m_dir_magicpct;
1047 return 0;
1048}
1049
1050/*
1051 * Split the leaf entries in the old block into old and new blocks.
1052 */
1053int /* error */
1054xfs_dir2_leafn_split(
1055 xfs_da_state_t *state, /* btree cursor */
1056 xfs_da_state_blk_t *oldblk, /* original block */
1057 xfs_da_state_blk_t *newblk) /* newly created block */
1058{
1059 xfs_da_args_t *args; /* operation arguments */
1060 xfs_dablk_t blkno; /* new leaf block number */
1061 int error; /* error return value */
1062 xfs_mount_t *mp; /* filesystem mount point */
1063
1064 /*
1065 * Allocate space for a new leaf node.
1066 */
1067 args = state->args;
1068 mp = args->dp->i_mount;
1069 ASSERT(args != NULL);
1070 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1071 error = xfs_da_grow_inode(args, &blkno);
1072 if (error) {
1073 return error;
1074 }
1075 /*
1076 * Initialize the new leaf block.
1077 */
1078 error = xfs_dir2_leaf_init(args, XFS_DIR2_DA_TO_DB(mp, blkno),
1079 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1080 if (error) {
1081 return error;
1082 }
1083 newblk->blkno = blkno;
1084 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1085 /*
1086 * Rebalance the entries across the two leaves, link the new
1087 * block into the leaves.
1088 */
1089 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
1090 error = xfs_da_blk_link(state, oldblk, newblk);
1091 if (error) {
1092 return error;
1093 }
1094 /*
1095 * Insert the new entry in the correct block.
1096 */
1097 if (state->inleaf)
1098 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1099 else
1100 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1101 /*
1102 * Update last hashval in each block since we added the name.
1103 */
1104 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1105 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
1106 xfs_dir2_leafn_check(args->dp, oldblk->bp);
1107 xfs_dir2_leafn_check(args->dp, newblk->bp);
1108 return error;
1109}
1110
1111/*
1112 * Check a leaf block and its neighbors to see if the block should be
1113 * collapsed into one or the other neighbor. Always keep the block
1114 * with the smaller block number.
1115 * If the current block is over 50% full, don't try to join it, return 0.
1116 * If the block is empty, fill in the state structure and return 2.
1117 * If it can be collapsed, fill in the state structure and return 1.
1118 * If nothing can be done, return 0.
1119 */
1120int /* error */
1121xfs_dir2_leafn_toosmall(
1122 xfs_da_state_t *state, /* btree cursor */
1123 int *action) /* resulting action to take */
1124{
1125 xfs_da_state_blk_t *blk; /* leaf block */
1126 xfs_dablk_t blkno; /* leaf block number */
1127 xfs_dabuf_t *bp; /* leaf buffer */
1128 int bytes; /* bytes in use */
1129 int count; /* leaf live entry count */
1130 int error; /* error return value */
1131 int forward; /* sibling block direction */
1132 int i; /* sibling counter */
1133 xfs_da_blkinfo_t *info; /* leaf block header */
1134 xfs_dir2_leaf_t *leaf; /* leaf structure */
1135 int rval; /* result from path_shift */
1136
1137 /*
1138 * Check for the degenerate case of the block being over 50% full.
1139 * If so, it's not worth even looking to see if we might be able
1140 * to coalesce with a sibling.
1141 */
1142 blk = &state->path.blk[state->path.active - 1];
1143 info = blk->bp->data;
89da0544 1144 ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4 1145 leaf = (xfs_dir2_leaf_t *)info;
a818e5de 1146 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1147 bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]);
1148 if (bytes > (state->blocksize >> 1)) {
1149 /*
1150 * Blk over 50%, don't try to join.
1151 */
1152 *action = 0;
1153 return 0;
1154 }
1155 /*
1156 * Check for the degenerate case of the block being empty.
1157 * If the block is empty, we'll simply delete it, no need to
1158 * coalesce it with a sibling block. We choose (arbitrarily)
1159 * to merge with the forward block unless it is NULL.
1160 */
1161 if (count == 0) {
1162 /*
1163 * Make altpath point to the block we want to keep and
1164 * path point to the block we want to drop (this one).
1165 */
89da0544 1166 forward = (info->forw != 0);
1da177e4
LT
1167 memcpy(&state->altpath, &state->path, sizeof(state->path));
1168 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1169 &rval);
1170 if (error)
1171 return error;
1172 *action = rval ? 2 : 0;
1173 return 0;
1174 }
1175 /*
1176 * Examine each sibling block to see if we can coalesce with
1177 * at least 25% free space to spare. We need to figure out
1178 * whether to merge with the forward or the backward block.
1179 * We prefer coalescing with the lower numbered sibling so as
1180 * to shrink a directory over time.
1181 */
89da0544 1182 forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back);
1da177e4 1183 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
89da0544 1184 blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back);
1da177e4
LT
1185 if (blkno == 0)
1186 continue;
1187 /*
1188 * Read the sibling leaf block.
1189 */
1190 if ((error =
1191 xfs_da_read_buf(state->args->trans, state->args->dp, blkno,
1192 -1, &bp, XFS_DATA_FORK))) {
1193 return error;
1194 }
1195 ASSERT(bp != NULL);
1196 /*
1197 * Count bytes in the two blocks combined.
1198 */
1199 leaf = (xfs_dir2_leaf_t *)info;
a818e5de 1200 count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1201 bytes = state->blocksize - (state->blocksize >> 2);
1202 leaf = bp->data;
89da0544 1203 ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
a818e5de 1204 count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale);
1da177e4
LT
1205 bytes -= count * (uint)sizeof(leaf->ents[0]);
1206 /*
1207 * Fits with at least 25% to spare.
1208 */
1209 if (bytes >= 0)
1210 break;
1211 xfs_da_brelse(state->args->trans, bp);
1212 }
1213 /*
1214 * Didn't like either block, give up.
1215 */
1216 if (i >= 2) {
1217 *action = 0;
1218 return 0;
1219 }
1220 /*
1221 * Done with the sibling leaf block here, drop the dabuf
1222 * so path_shift can get it.
1223 */
1224 xfs_da_buf_done(bp);
1225 /*
1226 * Make altpath point to the block we want to keep (the lower
1227 * numbered block) and path point to the block we want to drop.
1228 */
1229 memcpy(&state->altpath, &state->path, sizeof(state->path));
1230 if (blkno < blk->blkno)
1231 error = xfs_da_path_shift(state, &state->altpath, forward, 0,
1232 &rval);
1233 else
1234 error = xfs_da_path_shift(state, &state->path, forward, 0,
1235 &rval);
1236 if (error) {
1237 return error;
1238 }
1239 *action = rval ? 0 : 1;
1240 return 0;
1241}
1242
1243/*
1244 * Move all the leaf entries from drop_blk to save_blk.
1245 * This is done as part of a join operation.
1246 */
1247void
1248xfs_dir2_leafn_unbalance(
1249 xfs_da_state_t *state, /* cursor */
1250 xfs_da_state_blk_t *drop_blk, /* dead block */
1251 xfs_da_state_blk_t *save_blk) /* surviving block */
1252{
1253 xfs_da_args_t *args; /* operation arguments */
1254 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1255 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
1256
1257 args = state->args;
1258 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1259 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1260 drop_leaf = drop_blk->bp->data;
1261 save_leaf = save_blk->bp->data;
89da0544
NS
1262 ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1263 ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC);
1da177e4
LT
1264 /*
1265 * If there are any stale leaf entries, take this opportunity
1266 * to purge them.
1267 */
a818e5de 1268 if (drop_leaf->hdr.stale)
1da177e4 1269 xfs_dir2_leaf_compact(args, drop_blk->bp);
a818e5de 1270 if (save_leaf->hdr.stale)
1da177e4
LT
1271 xfs_dir2_leaf_compact(args, save_blk->bp);
1272 /*
1273 * Move the entries from drop to the appropriate end of save.
1274 */
3c1f9c15 1275 drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval);
1da177e4
LT
1276 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
1277 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0,
a818e5de 1278 be16_to_cpu(drop_leaf->hdr.count));
1da177e4
LT
1279 else
1280 xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp,
a818e5de 1281 be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count));
3c1f9c15 1282 save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval);
1da177e4
LT
1283 xfs_dir2_leafn_check(args->dp, save_blk->bp);
1284}
1285
1286/*
1287 * Top-level node form directory addname routine.
1288 */
1289int /* error */
1290xfs_dir2_node_addname(
1291 xfs_da_args_t *args) /* operation arguments */
1292{
1293 xfs_da_state_blk_t *blk; /* leaf block for insert */
1294 int error; /* error return value */
1295 int rval; /* sub-return value */
1296 xfs_da_state_t *state; /* btree cursor */
1297
1298 xfs_dir2_trace_args("node_addname", args);
1299 /*
1300 * Allocate and initialize the state (btree cursor).
1301 */
1302 state = xfs_da_state_alloc();
1303 state->args = args;
1304 state->mp = args->dp->i_mount;
1305 state->blocksize = state->mp->m_dirblksize;
1306 state->node_ents = state->mp->m_dir_node_ents;
1307 /*
1308 * Look up the name. We're not supposed to find it, but
1309 * this gives us the insertion point.
1310 */
1311 error = xfs_da_node_lookup_int(state, &rval);
1312 if (error)
1313 rval = error;
1314 if (rval != ENOENT) {
1315 goto done;
1316 }
1317 /*
1318 * Add the data entry to a data block.
1319 * Extravalid is set to a freeblock found by lookup.
1320 */
1321 rval = xfs_dir2_node_addname_int(args,
1322 state->extravalid ? &state->extrablk : NULL);
1323 if (rval) {
1324 goto done;
1325 }
1326 blk = &state->path.blk[state->path.active - 1];
1327 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1328 /*
1329 * Add the new leaf entry.
1330 */
1331 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1332 if (rval == 0) {
1333 /*
1334 * It worked, fix the hash values up the btree.
1335 */
1336 if (!args->justcheck)
1337 xfs_da_fixhashpath(state, &state->path);
1338 } else {
1339 /*
1340 * It didn't work, we need to split the leaf block.
1341 */
1342 if (args->total == 0) {
1343 ASSERT(rval == ENOSPC);
1344 goto done;
1345 }
1346 /*
1347 * Split the leaf block and insert the new entry.
1348 */
1349 rval = xfs_da_split(state);
1350 }
1351done:
1352 xfs_da_state_free(state);
1353 return rval;
1354}
1355
1356/*
1357 * Add the data entry for a node-format directory name addition.
1358 * The leaf entry is added in xfs_dir2_leafn_add.
1359 * We may enter with a freespace block that the lookup found.
1360 */
1361static int /* error */
1362xfs_dir2_node_addname_int(
1363 xfs_da_args_t *args, /* operation arguments */
1364 xfs_da_state_blk_t *fblk) /* optional freespace block */
1365{
1366 xfs_dir2_data_t *data; /* data block structure */
1367 xfs_dir2_db_t dbno; /* data block number */
1368 xfs_dabuf_t *dbp; /* data block buffer */
1369 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1370 xfs_inode_t *dp; /* incore directory inode */
1371 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1372 int error; /* error return value */
1373 xfs_dir2_db_t fbno; /* freespace block number */
1374 xfs_dabuf_t *fbp; /* freespace buffer */
1375 int findex; /* freespace entry index */
1376 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1377 xfs_dir2_db_t ifbno; /* initial freespace block no */
1378 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1379 int length; /* length of the new entry */
1380 int logfree; /* need to log free entry */
1381 xfs_mount_t *mp; /* filesystem mount point */
1382 int needlog; /* need to log data header */
1383 int needscan; /* need to rescan data frees */
3d693c6e 1384 __be16 *tagp; /* data entry tag pointer */
1da177e4
LT
1385 xfs_trans_t *tp; /* transaction pointer */
1386
1387 dp = args->dp;
1388 mp = dp->i_mount;
1389 tp = args->trans;
1390 length = XFS_DIR2_DATA_ENTSIZE(args->namelen);
1391 /*
1392 * If we came in with a freespace block that means that lookup
1393 * found an entry with our hash value. This is the freespace
1394 * block for that data entry.
1395 */
1396 if (fblk) {
1397 fbp = fblk->bp;
1398 /*
1399 * Remember initial freespace block number.
1400 */
1401 ifbno = fblk->blkno;
1402 free = fbp->data;
0ba962ef 1403 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1404 findex = fblk->index;
1405 /*
1406 * This means the free entry showed that the data block had
1407 * space for our entry, so we remembered it.
1408 * Use that data block.
1409 */
1410 if (findex >= 0) {
0ba962ef
NS
1411 ASSERT(findex < be32_to_cpu(free->hdr.nvalid));
1412 ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF);
1413 ASSERT(be16_to_cpu(free->bests[findex]) >= length);
1414 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1da177e4
LT
1415 }
1416 /*
1417 * The data block looked at didn't have enough room.
1418 * We'll start at the beginning of the freespace entries.
1419 */
1420 else {
1421 dbno = -1;
1422 findex = 0;
1423 }
1424 }
1425 /*
1426 * Didn't come in with a freespace block, so don't have a data block.
1427 */
1428 else {
1429 ifbno = dbno = -1;
1430 fbp = NULL;
1431 findex = 0;
1432 }
1433 /*
1434 * If we don't have a data block yet, we're going to scan the
1435 * freespace blocks looking for one. Figure out what the
1436 * highest freespace block number is.
1437 */
1438 if (dbno == -1) {
1439 xfs_fileoff_t fo; /* freespace block number */
1440
1441 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1442 return error;
1443 lastfbno = XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo);
1444 fbno = ifbno;
1445 }
1446 /*
1447 * While we haven't identified a data block, search the freeblock
1448 * data for a good data block. If we find a null freeblock entry,
1449 * indicating a hole in the data blocks, remember that.
1450 */
1451 while (dbno == -1) {
1452 /*
1453 * If we don't have a freeblock in hand, get the next one.
1454 */
1455 if (fbp == NULL) {
1456 /*
1457 * Happens the first time through unless lookup gave
1458 * us a freespace block to start with.
1459 */
1460 if (++fbno == 0)
1461 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1462 /*
1463 * If it's ifbno we already looked at it.
1464 */
1465 if (fbno == ifbno)
1466 fbno++;
1467 /*
1468 * If it's off the end we're done.
1469 */
1470 if (fbno >= lastfbno)
1471 break;
1472 /*
1473 * Read the block. There can be holes in the
1474 * freespace blocks, so this might not succeed.
1475 * This should be really rare, so there's no reason
1476 * to avoid it.
1477 */
1478 if ((error = xfs_da_read_buf(tp, dp,
1479 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1480 XFS_DATA_FORK))) {
1481 return error;
1482 }
1483 if (unlikely(fbp == NULL)) {
1484 continue;
1485 }
1486 free = fbp->data;
0ba962ef 1487 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1488 findex = 0;
1489 }
1490 /*
1491 * Look at the current free entry. Is it good enough?
1492 */
0ba962ef
NS
1493 if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF &&
1494 be16_to_cpu(free->bests[findex]) >= length)
1495 dbno = be32_to_cpu(free->hdr.firstdb) + findex;
1da177e4
LT
1496 else {
1497 /*
1498 * Are we done with the freeblock?
1499 */
0ba962ef 1500 if (++findex == be32_to_cpu(free->hdr.nvalid)) {
1da177e4
LT
1501 /*
1502 * Drop the block.
1503 */
1504 xfs_da_brelse(tp, fbp);
1505 fbp = NULL;
1506 if (fblk && fblk->bp)
1507 fblk->bp = NULL;
1508 }
1509 }
1510 }
1511 /*
1512 * If we don't have a data block, we need to allocate one and make
1513 * the freespace entries refer to it.
1514 */
1515 if (unlikely(dbno == -1)) {
1516 /*
1517 * Not allowed to allocate, return failure.
1518 */
1519 if (args->justcheck || args->total == 0) {
1520 /*
1521 * Drop the freespace buffer unless it came from our
1522 * caller.
1523 */
1524 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1525 xfs_da_buf_done(fbp);
1526 return XFS_ERROR(ENOSPC);
1527 }
1528 /*
1529 * Allocate and initialize the new data block.
1530 */
1531 if (unlikely((error = xfs_dir2_grow_inode(args,
1532 XFS_DIR2_DATA_SPACE,
1533 &dbno)) ||
1534 (error = xfs_dir2_data_init(args, dbno, &dbp)))) {
1535 /*
1536 * Drop the freespace buffer unless it came from our
1537 * caller.
1538 */
1539 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1540 xfs_da_buf_done(fbp);
1541 return error;
1542 }
1543 /*
1544 * If (somehow) we have a freespace block, get rid of it.
1545 */
1546 if (fbp)
1547 xfs_da_brelse(tp, fbp);
1548 if (fblk && fblk->bp)
1549 fblk->bp = NULL;
1550
1551 /*
1552 * Get the freespace block corresponding to the data block
1553 * that was just allocated.
1554 */
1555 fbno = XFS_DIR2_DB_TO_FDB(mp, dbno);
1556 if (unlikely(error = xfs_da_read_buf(tp, dp,
1557 XFS_DIR2_DB_TO_DA(mp, fbno), -2, &fbp,
1558 XFS_DATA_FORK))) {
1559 xfs_da_buf_done(dbp);
1560 return error;
1561 }
1562 /*
1563 * If there wasn't a freespace block, the read will
1564 * return a NULL fbp. Allocate and initialize a new one.
1565 */
1566 if( fbp == NULL ) {
1567 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1568 &fbno))) {
1569 return error;
1570 }
1571
1572 if (unlikely(XFS_DIR2_DB_TO_FDB(mp, dbno) != fbno)) {
1573 cmn_err(CE_ALERT,
1574 "xfs_dir2_node_addname_int: dir ino "
1575 "%llu needed freesp block %lld for\n"
1576 " data block %lld, got %lld\n"
1577 " ifbno %llu lastfbno %d\n",
1578 (unsigned long long)dp->i_ino,
1579 (long long)XFS_DIR2_DB_TO_FDB(mp, dbno),
1580 (long long)dbno, (long long)fbno,
1581 (unsigned long long)ifbno, lastfbno);
1582 if (fblk) {
1583 cmn_err(CE_ALERT,
1584 " fblk 0x%p blkno %llu "
1585 "index %d magic 0x%x\n",
1586 fblk,
1587 (unsigned long long)fblk->blkno,
1588 fblk->index,
1589 fblk->magic);
1590 } else {
1591 cmn_err(CE_ALERT,
1592 " ... fblk is NULL\n");
1593 }
1594 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1595 XFS_ERRLEVEL_LOW, mp);
1596 return XFS_ERROR(EFSCORRUPTED);
1597 }
1598
1599 /*
1600 * Get a buffer for the new block.
1601 */
1602 if ((error = xfs_da_get_buf(tp, dp,
1603 XFS_DIR2_DB_TO_DA(mp, fbno),
1604 -1, &fbp, XFS_DATA_FORK))) {
1605 return error;
1606 }
1607 ASSERT(fbp != NULL);
1608
1609 /*
1610 * Initialize the new block to be empty, and remember
1611 * its first slot as our empty slot.
1612 */
1613 free = fbp->data;
0ba962ef
NS
1614 free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC);
1615 free->hdr.firstdb = cpu_to_be32(
1da177e4
LT
1616 (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1617 XFS_DIR2_MAX_FREE_BESTS(mp));
1618 free->hdr.nvalid = 0;
1619 free->hdr.nused = 0;
1620 } else {
1621 free = fbp->data;
0ba962ef 1622 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1623 }
1624
1625 /*
1626 * Set the freespace block index from the data block number.
1627 */
1628 findex = XFS_DIR2_DB_TO_FDINDEX(mp, dbno);
1629 /*
1630 * If it's after the end of the current entries in the
1631 * freespace block, extend that table.
1632 */
0ba962ef 1633 if (findex >= be32_to_cpu(free->hdr.nvalid)) {
1da177e4 1634 ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp));
0ba962ef 1635 free->hdr.nvalid = cpu_to_be32(findex + 1);
1da177e4
LT
1636 /*
1637 * Tag new entry so nused will go up.
1638 */
0ba962ef 1639 free->bests[findex] = cpu_to_be16(NULLDATAOFF);
1da177e4
LT
1640 }
1641 /*
1642 * If this entry was for an empty data block
1643 * (this should always be true) then update the header.
1644 */
0ba962ef
NS
1645 if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) {
1646 be32_add(&free->hdr.nused, 1);
1da177e4
LT
1647 xfs_dir2_free_log_header(tp, fbp);
1648 }
1649 /*
1650 * Update the real value in the table.
1651 * We haven't allocated the data entry yet so this will
1652 * change again.
1653 */
1654 data = dbp->data;
70e73f59 1655 free->bests[findex] = data->hdr.bestfree[0].length;
1da177e4
LT
1656 logfree = 1;
1657 }
1658 /*
1659 * We had a data block so we don't have to make a new one.
1660 */
1661 else {
1662 /*
1663 * If just checking, we succeeded.
1664 */
1665 if (args->justcheck) {
1666 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1667 xfs_da_buf_done(fbp);
1668 return 0;
1669 }
1670 /*
1671 * Read the data block in.
1672 */
1673 if (unlikely(
1674 error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, dbno),
1675 -1, &dbp, XFS_DATA_FORK))) {
1676 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1677 xfs_da_buf_done(fbp);
1678 return error;
1679 }
1680 data = dbp->data;
1681 logfree = 0;
1682 }
70e73f59 1683 ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length);
1da177e4
LT
1684 /*
1685 * Point to the existing unused space.
1686 */
1687 dup = (xfs_dir2_data_unused_t *)
70e73f59 1688 ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset));
1da177e4
LT
1689 needscan = needlog = 0;
1690 /*
1691 * Mark the first part of the unused space, inuse for us.
1692 */
1693 xfs_dir2_data_use_free(tp, dbp, dup,
1694 (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length,
1695 &needlog, &needscan);
1696 /*
1697 * Fill in the new entry and log it.
1698 */
1699 dep = (xfs_dir2_data_entry_t *)dup;
ff9901c1 1700 dep->inumber = cpu_to_be64(args->inumber);
1da177e4
LT
1701 dep->namelen = args->namelen;
1702 memcpy(dep->name, args->name, dep->namelen);
1703 tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep);
3d693c6e 1704 *tagp = cpu_to_be16((char *)dep - (char *)data);
1da177e4
LT
1705 xfs_dir2_data_log_entry(tp, dbp, dep);
1706 /*
1707 * Rescan the block for bestfree if needed.
1708 */
1709 if (needscan)
1710 xfs_dir2_data_freescan(mp, data, &needlog, NULL);
1711 /*
1712 * Log the data block header if needed.
1713 */
1714 if (needlog)
1715 xfs_dir2_data_log_header(tp, dbp);
1716 /*
1717 * If the freespace entry is now wrong, update it.
1718 */
0ba962ef 1719 if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) {
70e73f59 1720 free->bests[findex] = data->hdr.bestfree[0].length;
1da177e4
LT
1721 logfree = 1;
1722 }
1723 /*
1724 * Log the freespace entry if needed.
1725 */
1726 if (logfree)
1727 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1728 /*
1729 * If the caller didn't hand us the freespace block, drop it.
1730 */
1731 if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL)
1732 xfs_da_buf_done(fbp);
1733 /*
1734 * Return the data block and offset in args, then drop the data block.
1735 */
1736 args->blkno = (xfs_dablk_t)dbno;
3d693c6e 1737 args->index = be16_to_cpu(*tagp);
1da177e4
LT
1738 xfs_da_buf_done(dbp);
1739 return 0;
1740}
1741
1742/*
1743 * Lookup an entry in a node-format directory.
1744 * All the real work happens in xfs_da_node_lookup_int.
1745 * The only real output is the inode number of the entry.
1746 */
1747int /* error */
1748xfs_dir2_node_lookup(
1749 xfs_da_args_t *args) /* operation arguments */
1750{
1751 int error; /* error return value */
1752 int i; /* btree level */
1753 int rval; /* operation return value */
1754 xfs_da_state_t *state; /* btree cursor */
1755
1756 xfs_dir2_trace_args("node_lookup", args);
1757 /*
1758 * Allocate and initialize the btree cursor.
1759 */
1760 state = xfs_da_state_alloc();
1761 state->args = args;
1762 state->mp = args->dp->i_mount;
1763 state->blocksize = state->mp->m_dirblksize;
1764 state->node_ents = state->mp->m_dir_node_ents;
1765 /*
1766 * Fill in the path to the entry in the cursor.
1767 */
1768 error = xfs_da_node_lookup_int(state, &rval);
1769 if (error)
1770 rval = error;
1771 /*
1772 * Release the btree blocks and leaf block.
1773 */
1774 for (i = 0; i < state->path.active; i++) {
1775 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1776 state->path.blk[i].bp = NULL;
1777 }
1778 /*
1779 * Release the data block if we have it.
1780 */
1781 if (state->extravalid && state->extrablk.bp) {
1782 xfs_da_brelse(args->trans, state->extrablk.bp);
1783 state->extrablk.bp = NULL;
1784 }
1785 xfs_da_state_free(state);
1786 return rval;
1787}
1788
1789/*
1790 * Remove an entry from a node-format directory.
1791 */
1792int /* error */
1793xfs_dir2_node_removename(
1794 xfs_da_args_t *args) /* operation arguments */
1795{
1796 xfs_da_state_blk_t *blk; /* leaf block */
1797 int error; /* error return value */
1798 int rval; /* operation return value */
1799 xfs_da_state_t *state; /* btree cursor */
1800
1801 xfs_dir2_trace_args("node_removename", args);
1802 /*
1803 * Allocate and initialize the btree cursor.
1804 */
1805 state = xfs_da_state_alloc();
1806 state->args = args;
1807 state->mp = args->dp->i_mount;
1808 state->blocksize = state->mp->m_dirblksize;
1809 state->node_ents = state->mp->m_dir_node_ents;
1810 /*
1811 * Look up the entry we're deleting, set up the cursor.
1812 */
1813 error = xfs_da_node_lookup_int(state, &rval);
1814 if (error) {
1815 rval = error;
1816 }
1817 /*
1818 * Didn't find it, upper layer screwed up.
1819 */
1820 if (rval != EEXIST) {
1821 xfs_da_state_free(state);
1822 return rval;
1823 }
1824 blk = &state->path.blk[state->path.active - 1];
1825 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1826 ASSERT(state->extravalid);
1827 /*
1828 * Remove the leaf and data entries.
1829 * Extrablk refers to the data block.
1830 */
1831 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
1832 &state->extrablk, &rval);
1833 if (error) {
1834 return error;
1835 }
1836 /*
1837 * Fix the hash values up the btree.
1838 */
1839 xfs_da_fixhashpath(state, &state->path);
1840 /*
1841 * If we need to join leaf blocks, do it.
1842 */
1843 if (rval && state->path.active > 1)
1844 error = xfs_da_join(state);
1845 /*
1846 * If no errors so far, try conversion to leaf format.
1847 */
1848 if (!error)
1849 error = xfs_dir2_node_to_leaf(state);
1850 xfs_da_state_free(state);
1851 return error;
1852}
1853
1854/*
1855 * Replace an entry's inode number in a node-format directory.
1856 */
1857int /* error */
1858xfs_dir2_node_replace(
1859 xfs_da_args_t *args) /* operation arguments */
1860{
1861 xfs_da_state_blk_t *blk; /* leaf block */
1862 xfs_dir2_data_t *data; /* data block structure */
1863 xfs_dir2_data_entry_t *dep; /* data entry changed */
1864 int error; /* error return value */
1865 int i; /* btree level */
1866 xfs_ino_t inum; /* new inode number */
1867 xfs_dir2_leaf_t *leaf; /* leaf structure */
1868 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
1869 int rval; /* internal return value */
1870 xfs_da_state_t *state; /* btree cursor */
1871
1872 xfs_dir2_trace_args("node_replace", args);
1873 /*
1874 * Allocate and initialize the btree cursor.
1875 */
1876 state = xfs_da_state_alloc();
1877 state->args = args;
1878 state->mp = args->dp->i_mount;
1879 state->blocksize = state->mp->m_dirblksize;
1880 state->node_ents = state->mp->m_dir_node_ents;
1881 inum = args->inumber;
1882 /*
1883 * Lookup the entry to change in the btree.
1884 */
1885 error = xfs_da_node_lookup_int(state, &rval);
1886 if (error) {
1887 rval = error;
1888 }
1889 /*
1890 * It should be found, since the vnodeops layer has looked it up
1891 * and locked it. But paranoia is good.
1892 */
1893 if (rval == EEXIST) {
1894 /*
1895 * Find the leaf entry.
1896 */
1897 blk = &state->path.blk[state->path.active - 1];
1898 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1899 leaf = blk->bp->data;
1900 lep = &leaf->ents[blk->index];
1901 ASSERT(state->extravalid);
1902 /*
1903 * Point to the data entry.
1904 */
1905 data = state->extrablk.bp->data;
70e73f59 1906 ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC);
1da177e4
LT
1907 dep = (xfs_dir2_data_entry_t *)
1908 ((char *)data +
3c1f9c15 1909 XFS_DIR2_DATAPTR_TO_OFF(state->mp, be32_to_cpu(lep->address)));
ff9901c1 1910 ASSERT(inum != be64_to_cpu(dep->inumber));
1da177e4
LT
1911 /*
1912 * Fill in the new inode number and log the entry.
1913 */
ff9901c1 1914 dep->inumber = cpu_to_be64(inum);
1da177e4
LT
1915 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
1916 rval = 0;
1917 }
1918 /*
1919 * Didn't find it, and we're holding a data block. Drop it.
1920 */
1921 else if (state->extravalid) {
1922 xfs_da_brelse(args->trans, state->extrablk.bp);
1923 state->extrablk.bp = NULL;
1924 }
1925 /*
1926 * Release all the buffers in the cursor.
1927 */
1928 for (i = 0; i < state->path.active; i++) {
1929 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1930 state->path.blk[i].bp = NULL;
1931 }
1932 xfs_da_state_free(state);
1933 return rval;
1934}
1935
1936/*
1937 * Trim off a trailing empty freespace block.
1938 * Return (in rvalp) 1 if we did it, 0 if not.
1939 */
1940int /* error */
1941xfs_dir2_node_trim_free(
1942 xfs_da_args_t *args, /* operation arguments */
1943 xfs_fileoff_t fo, /* free block number */
1944 int *rvalp) /* out: did something */
1945{
1946 xfs_dabuf_t *bp; /* freespace buffer */
1947 xfs_inode_t *dp; /* incore directory inode */
1948 int error; /* error return code */
1949 xfs_dir2_free_t *free; /* freespace structure */
1950 xfs_mount_t *mp; /* filesystem mount point */
1951 xfs_trans_t *tp; /* transaction pointer */
1952
1953 dp = args->dp;
1954 mp = dp->i_mount;
1955 tp = args->trans;
1956 /*
1957 * Read the freespace block.
1958 */
1959 if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp,
1960 XFS_DATA_FORK))) {
1961 return error;
1962 }
1963
1964 /*
1965 * There can be holes in freespace. If fo is a hole, there's
1966 * nothing to do.
1967 */
1968 if (bp == NULL) {
1969 return 0;
1970 }
1971 free = bp->data;
0ba962ef 1972 ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC);
1da177e4
LT
1973 /*
1974 * If there are used entries, there's nothing to do.
1975 */
0ba962ef 1976 if (be32_to_cpu(free->hdr.nused) > 0) {
1da177e4
LT
1977 xfs_da_brelse(tp, bp);
1978 *rvalp = 0;
1979 return 0;
1980 }
1981 /*
1982 * Blow the block away.
1983 */
1984 if ((error =
1985 xfs_dir2_shrink_inode(args, XFS_DIR2_DA_TO_DB(mp, (xfs_dablk_t)fo),
1986 bp))) {
1987 /*
1988 * Can't fail with ENOSPC since that only happens with no
1989 * space reservation, when breaking up an extent into two
1990 * pieces. This is the last block of an extent.
1991 */
1992 ASSERT(error != ENOSPC);
1993 xfs_da_brelse(tp, bp);
1994 return error;
1995 }
1996 /*
1997 * Return that we succeeded.
1998 */
1999 *rvalp = 1;
2000 return 0;
2001}
This page took 0.209714 seconds and 5 git commands to generate.