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