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