blob: 122449b33f5e34bc3afa9dc606c1be87caf6eb1e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnercbc8adf2013-04-03 16:11:21 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
31#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_format.h"
33#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000035#include "xfs_trace.h"
Dave Chinnercbc8adf2013-04-03 16:11:21 +110036#include "xfs_buf_item.h"
37#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * Function declarations.
41 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100042static int xfs_dir2_leafn_add(struct xfs_buf *bp, xfs_da_args_t *args,
43 int index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state,
45 xfs_da_state_blk_t *blk1,
46 xfs_da_state_blk_t *blk2);
Dave Chinner1d9025e2012-06-22 18:50:14 +100047static int xfs_dir2_leafn_remove(xfs_da_args_t *args, struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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
Dave Chinner24df33b2013-04-12 07:30:21 +100053/*
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
Dave Chinnercbc8adf2013-04-03 16:11:21 +110086static bool
87xfs_dir3_free_verify(
Dave Chinner20252072012-11-12 22:54:13 +110088 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;
Dave Chinner20252072012-11-12 22:54:13 +110092
Dave Chinnercbc8adf2013-04-03 16:11:21 +110093 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
112static void
113xfs_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_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);
Dave Chinner20252072012-11-12 22:54:13 +1100123 xfs_buf_ioerror(bp, EFSCORRUPTED);
124 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100125}
Dave Chinner20252072012-11-12 22:54:13 +1100126
Dave Chinner612cfbf2012-11-14 17:52:32 +1100127static void
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100128xfs_dir3_free_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100129 struct xfs_buf *bp)
130{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100131 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);
Dave Chinner1813dd62012-11-14 17:54:40 +1100148}
149
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100150static 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,
Dave Chinner1813dd62012-11-14 17:54:40 +1100153};
Dave Chinner20252072012-11-12 22:54:13 +1100154
Dave Chinner612cfbf2012-11-14 17:52:32 +1100155
Dave Chinner20252072012-11-12 22:54:13 +1100156static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100157__xfs_dir3_free_read(
Dave Chinner20252072012-11-12 22:54:13 +1100158 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,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100165 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinner20252072012-11-12 22:54:13 +1100166}
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{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100175 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100176}
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{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100185 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;
Dave Chinner20252072012-11-12 22:54:13 +1100273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*
276 * Log entries from a freespace block.
277 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000278STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000280 struct xfs_trans *tp,
281 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int first, /* first entry to log */
283 int last) /* last entry to log */
284{
285 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100286 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dave Chinner1d9025e2012-06-22 18:50:14 +1000288 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100289 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));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000292 xfs_trans_log_buf(tp, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100293 (uint)((char *)&bests[first] - (char *)free),
294 (uint)((char *)&bests[last] - (char *)free +
295 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298/*
299 * Log header from a freespace block.
300 */
301static void
302xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000303 struct xfs_trans *tp,
304 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 xfs_dir2_free_t *free; /* freespace structure */
307
Dave Chinner1d9025e2012-06-22 18:50:14 +1000308 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100309 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000322 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 xfs_inode_t *dp; /* incore directory inode */
325 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000326 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_dir2_db_t fdb; /* freespace block number */
328 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100329 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 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 */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100336 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100338 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000340 trace_xfs_dir2_leaf_to_node(args);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 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 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100355 error = xfs_dir3_free_get_buf(tp, dp, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100356 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100358
Dave Chinner1d9025e2012-06-22 18:50:14 +1000359 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100360 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000361 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000362 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100363 ASSERT(be32_to_cpu(ltp->bestcount) <=
364 (uint)dp->i_d.di_size / mp->m_dirblksize);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * Copy freespace entries from the leaf block to the new block.
368 * Count active entries.
369 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100370 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++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100373 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100375 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100379 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100381 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 xfs_dir2_free_log_header(tp, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100387
Dave Chinner24df33b2013-04-12 07:30:21 +1000388 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 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(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000410 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000424 struct xfs_dir3_icleaf_hdr leafhdr;
425 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000427 trace_xfs_dir2_leafn_add(args, index);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 dp = args->dp;
430 mp = dp->i_mount;
431 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000432 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000433 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
434 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
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
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 if (leafhdr.count == xfs_dir3_max_leaf_ents(mp, leaf)) {
451 if (!leafhdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return XFS_ERROR(ENOSPC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000453 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 } else
455 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000456 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Barry Naujok6a178102008-05-21 16:42:05 +1000460 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462
463 /*
464 * Compact out all but one stale leaf entry. Leaves behind
465 * the entry closest to index.
466 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000467 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 lfloghigh = -1;
476 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 /*
479 * Insert the new entry, log everything.
480 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000481 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200482 highstale, &lfloglow, &lfloghigh);
483
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100484 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000485 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100486 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000487
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
495#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100496static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512#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(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000520 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int *count) /* count of entries in leaf */
522{
Dave Chinner24df33b2013-04-12 07:30:21 +1000523 struct xfs_dir2_leaf *leaf = bp->b_addr;
524 struct xfs_dir2_leaf_entry *ents;
525 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dave Chinner24df33b2013-04-12 07:30:21 +1000527 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
528
529 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
530 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000533 *count = leafhdr.count;
534 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000536
537 ents = xfs_dir3_leaf_ents_p(leaf);
538 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000542 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000545STATIC int
546xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000547 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 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{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000552 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000553 xfs_dir2_db_t curdb = -1; /* current data block number */
554 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 xfs_inode_t *dp; /* incore directory inode */
556 int error; /* error return value */
557 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000558 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int index; /* leaf entry index */
560 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000561 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000567 struct xfs_dir2_leaf_entry *ents;
568 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 dp = args->dp;
571 tp = args->trans;
572 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000573 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000574 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
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 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000587 if (state->extravalid) {
588 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000590 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000591 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100592 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
593 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000595 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /*
597 * Loop over leaf entries with the right hash value.
598 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000599 for (lep = &ents[index];
600 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
601 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /*
603 * Skip stale leaf entries.
604 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100605 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 continue;
607 /*
608 * Pull the data block number from the entry.
609 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000610 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
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.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000615 *
616 * If this block isn't the data block we already have
617 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000619 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100620 __be16 *bests;
621
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000622 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000624 * Convert the data block to the free block
625 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000629 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000633 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
635 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000636 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100637
638 error = xfs_dir2_free_read(tp, dp,
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 xfs_dir2_db_to_da(mp, newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100640 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000641 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000643 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100644
645 xfs_dir2_free_hdr_check(mp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000648 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000650 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000652 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100654 bests = xfs_dir3_free_bests_p(mp, free);
655 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000656 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
657 XFS_ERRLEVEL_LOW, mp);
658 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000659 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100663 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000667 /* Didn't find any space */
668 fi = -1;
669out:
Barry Naujok6a178102008-05-21 16:42:05 +1000670 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000671 if (curbp) {
672 /* Giving back a free block. */
673 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000675 state->extrablk.index = fi;
676 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100677
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 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
684 } else {
685 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000688 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return XFS_ERROR(ENOENT);
692}
693
694/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000695 * 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(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000700 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000701 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{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000705 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000706 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 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000710 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 */
Barry Naujok5163f952008-05-21 16:41:01 +1000716 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000717 struct xfs_dir2_leaf_entry *ents;
718 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000719
720 dp = args->dp;
721 tp = args->trans;
722 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000723 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000724 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
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000730 /*
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 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000744 for (lep = &ents[index];
745 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
746 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000747 /*
748 * Skip stale leaf entries.
749 */
750 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
751 continue;
752 /*
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.
761 */
762 if (newdb != curdb) {
763 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000764 * If we had a block before that we aren't saving
765 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000766 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000767 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
768 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000769 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000770 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000771 * If needing the block that is saved with a CI match,
772 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000773 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000774 if (args->cmpresult != XFS_CMP_DIFFERENT &&
775 newdb == state->extrablk.blkno) {
776 ASSERT(state->extravalid);
777 curbp = state->extrablk.bp;
778 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100779 error = xfs_dir3_data_read(tp, dp,
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000780 xfs_dir2_db_to_da(mp, newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100781 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000782 if (error)
783 return error;
784 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100785 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000786 curdb = newdb;
787 }
788 /*
789 * Point to the data entry.
790 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000791 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000792 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
793 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000794 * Compare the entry and if it's an exact match, return
795 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000796 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000797 */
Barry Naujok5163f952008-05-21 16:41:01 +1000798 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
799 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000800 /* If there is a CI match block, drop it */
801 if (args->cmpresult != XFS_CMP_DIFFERENT &&
802 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000803 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000804 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000805 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000806 *indexp = index;
807 state->extravalid = 1;
808 state->extrablk.bp = curbp;
809 state->extrablk.blkno = curdb;
810 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000811 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000812 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100813 curbp->b_ops = &xfs_dir3_data_buf_ops;
Barry Naujok5163f952008-05-21 16:41:01 +1000814 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000815 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000816 }
817 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000818 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000819 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000820 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;
Dave Chinner33363fe2013-04-03 16:11:22 +1100827 curbp->b_ops = &xfs_dir3_data_buf_ops;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000828 } else {
829 /* If the curbp is not the CI match block, drop it */
830 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000831 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000832 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000833 } else {
834 state->extravalid = 0;
835 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000836 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000837 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000838}
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(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000847 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000848 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{
Barry Naujok6a178102008-05-21 16:42:05 +1000852 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000853 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
854 state);
855 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
856}
857
858/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * Move count leaf entries from source to destination leaf.
860 * Log entries and headers. Stale entries are preserved.
861 */
862static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000863xfs_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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Dave Chinner24df33b2013-04-12 07:30:21 +1000875 struct xfs_trans *tp = args->trans;
876 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000878 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /*
881 * Silently return if nothing to do.
882 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000883 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
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 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000891 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 /*
898 * If the source has stale leaves, count the ones in the copy range
899 * so we can update the header correctly.
900 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000901 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 int i; /* temp leaf index */
903
904 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000905 if (sents[i].address ==
906 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 stale++;
908 }
909 } else
910 stale = 0;
911 /*
912 * Copy the leaf entries from source to destination.
913 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000914 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 xfs_dir3_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 /*
919 * If there are source entries after the ones we copied,
920 * delete the ones we copied by sliding the next ones down.
921 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000922 if (start_s + count < shdr->count) {
923 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner24df33b2013-04-12 07:30:21 +1000925 xfs_dir3_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /*
929 * Update the headers and log them.
930 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000931 shdr->count -= count;
932 shdr->stale -= stale;
933 dhdr->count += count;
934 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
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(
Dave Chinner24df33b2013-04-12 07:30:21 +1000943 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
944 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Dave Chinner24df33b2013-04-12 07:30:21 +1000946 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dave Chinner24df33b2013-04-12 07:30:21 +1000953 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)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
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 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001006 leaf1 = blk1->bp->b_addr;
1007 leaf2 = blk2->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001008 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014#ifdef DEBUG
Dave Chinner24df33b2013-04-12 07:30:21 +10001015 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016#endif
1017 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
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
Dave Chinner24df33b2013-04-12 07:30:21 +10001026 if (mid >= hdr1.count)
1027 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001029 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001043 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001045 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1046 hdr1.count - count, blk2->bp,
1047 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001049 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 /*
1066 * Mark whether we're inserting into the old or new leaf.
1067 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001068 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001070 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 state->inleaf = !swap;
1072 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001073 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Adjust the expected index for insertion.
1076 */
1077 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001078 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001079
1080 /*
1081 * Finally sanity check just to make sure we are not returning a
1082 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
1084 if(blk2->index < 0) {
1085 state->inleaf = 1;
1086 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +11001087 xfs_alert(args->dp->i_mount,
1088 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d\n",
1089 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091}
1092
Dave Chinner20252072012-11-12 22:54:13 +11001093static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001094xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001095 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;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001105 __be16 *bests;
1106 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner20252072012-11-12 22:54:13 +11001107
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001108 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Dave Chinner20252072012-11-12 22:54:13 +11001109
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001110 bests = xfs_dir3_free_bests_p(tp->t_mountp, free);
1111 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001112 /*
1113 * Data block is not empty, just set the free entry to the new
1114 * value.
1115 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001116 bests[findex] = cpu_to_be16(longest);
1117 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1118 return 0;
1119 }
1120
1121 /* One less used entry in the free table. */
1122 freehdr.nused--;
1123
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;
1135 }
1136 freehdr.nvalid = i + 1;
1137 logfree = 0;
1138 } else {
1139 /* Not the last entry, just punch it out. */
1140 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001141 logfree = 1;
1142 }
1143
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001144 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;
1160 /*
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.
1164 */
1165 }
1166
Dave Chinner20252072012-11-12 22:54:13 +11001167 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173/*
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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001181 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 int index, /* leaf entry index */
1183 xfs_da_state_blk_t *dblk, /* data block */
1184 int *rval) /* resulting block needs join */
1185{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001186 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001188 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 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 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001199 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001200 struct xfs_dir3_icleaf_hdr leafhdr;
1201 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001203 trace_xfs_dir2_leafn_remove(args, index);
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 dp = args->dp;
1206 tp = args->trans;
1207 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001208 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001209 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1210 ents = xfs_dir3_leaf_ents_p(leaf);
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /*
1213 * Point to the entry we're removing.
1214 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001215 lep = &ents[index];
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /*
1218 * Extract the data block and offset from the entry.
1219 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001220 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001222 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 /*
1226 * Kill the leaf entry by marking it stale.
1227 * Log the leaf block changes.
1228 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001229 leafhdr.stale++;
1230 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1231 xfs_dir3_leaf_log_header(tp, bp);
1232
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001233 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001234 xfs_dir3_leaf_log_ents(tp, bp, index, index);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
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;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001241 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001242 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner33363fe2013-04-03 16:11:22 +11001243 bf = xfs_dir3_data_bestfree_p(hdr);
1244 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 needlog = needscan = 0;
1246 xfs_dir2_data_make_free(tp, dbp, off,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001247 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Rescan the data block freespaces for bestfree.
1250 * Log the data block header if needed.
1251 */
1252 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001253 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (needlog)
1255 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001256 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 /*
1258 * If the longest data block freespace changes, need to update
1259 * the corresponding freeblock entry.
1260 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001261 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001263 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 xfs_dir2_db_t fdb; /* freeblock block number */
1265 int findex; /* index in freeblock entries */
1266 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /*
1269 * Convert the data block number to a free block,
1270 * read in the free block.
1271 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001272 fdb = xfs_dir2_db_to_fdb(mp, db);
Dave Chinner20252072012-11-12 22:54:13 +11001273 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1274 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001275 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001277 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001278#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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /*
1287 * Calculate which entry we need to fix.
1288 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001289 findex = xfs_dir2_db_to_fdindex(mp, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001290 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * If the data block is now empty we can get rid of it
1293 * (usually).
1294 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001295 if (longest == mp->m_dirblksize -
1296 xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /*
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;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001303 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001310 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return error;
1312 }
1313 /*
1314 * If we got rid of the data block, we can eliminate that entry
1315 * in the free block.
1316 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001317 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001318 fdb, findex, fbp, longest);
1319 if (error)
1320 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Dave Chinner20252072012-11-12 22:54:13 +11001322
Dave Chinner24df33b2013-04-12 07:30:21 +10001323 xfs_dir3_leaf_check(mp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001325 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 * to justify trying to join it with a neighbor.
1327 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001328 *rval = (xfs_dir3_leaf_hdr_size(leaf) +
1329 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001362 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
1363 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 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);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001374 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 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);
Dave Chinner24df33b2013-04-12 07:30:21 +10001390 xfs_dir3_leaf_check(mp, oldblk->bp);
1391 xfs_dir3_leaf_check(mp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001411 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 xfs_dir2_leaf_t *leaf; /* leaf structure */
1418 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001419 struct xfs_dir3_icleaf_hdr leafhdr;
1420 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
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];
Dave Chinner24df33b2013-04-12 07:30:21 +10001428 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 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 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001453 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001455 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 &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 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001469 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001471 struct xfs_dir3_icleaf_hdr hdr2;
1472
1473 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (blkno == 0)
1475 continue;
1476 /*
1477 * Read the sibling leaf block.
1478 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001479 error = xfs_dir3_leafn_read(state->args->trans, state->args->dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001480 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001481 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /*
1485 * Count bytes in the two blocks combined.
1486 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001487 count = leafhdr.count - leafhdr.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001489
Dave Chinner1d9025e2012-06-22 18:50:14 +10001490 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001491 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /*
1497 * Fits with at least 25% to spare.
1498 */
1499 if (bytes >= 0)
1500 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001501 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 /*
1504 * Didn't like either block, give up.
1505 */
1506 if (i >= 2) {
1507 *action = 0;
1508 return 0;
1509 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /*
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)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001517 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 &rval);
1519 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001520 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 &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 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001542 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 args = state->args;
1548 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1549 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001550 drop_leaf = drop_blk->bp->b_addr;
1551 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001552
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /*
1559 * If there are any stale leaf entries, take this opportunity
1560 * to purge them.
1561 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001562 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /*
1568 * Move the entries from drop to the appropriate end of save.
1569 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001570 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001572 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1573 save_blk->bp, &savehdr, sents, 0,
1574 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001576 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
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
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001603 trace_xfs_dir2_node_addname(args);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
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 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001617 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 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 */
Barry Naujok6a178102008-05-21 16:42:05 +10001642 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001643 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 } 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 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001655 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
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{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001672 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001674 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001680 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 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 */
Nathan Scott3d693c62006-03-17 17:28:27 +11001690 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001692 __be16 *bests;
1693 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001694 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 dp = args->dp;
1697 mp = dp->i_mount;
1698 tp = args->trans;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001699 length = xfs_dir2_data_entsize(args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
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;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001711 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 findex = fblk->index;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001713 bests = xfs_dir3_free_bests_p(mp, free);
1714 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
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) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001722 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 dbno = -1;
1732 findex = 0;
1733 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001734 } else {
1735 /*
1736 * Didn't come in with a freespace block, so no data block.
1737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 ifbno = dbno = -1;
1739 fbp = NULL;
1740 findex = 0;
1741 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
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;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001753 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 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 */
Dave Chinner20252072012-11-12 22:54:13 +11001788 error = xfs_dir2_free_try_read(tp, dp,
1789 xfs_dir2_db_to_da(mp, fbno),
1790 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001793 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001795 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 findex = 0;
1797 }
1798 /*
1799 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001800 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001806 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 else {
1812 /*
1813 * Are we done with the freeblock?
1814 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001815 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /*
1817 * Drop the block.
1818 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001819 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001834 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /*
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)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001843 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * If (somehow) we have a freespace block, get rid of it.
1848 */
1849 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001850 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 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 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001858 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001859 error = xfs_dir2_free_try_read(tp, dp,
1860 xfs_dir2_db_to_da(mp, fbno),
1861 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * If there wasn't a freespace block, the read will
1867 * return a NULL fbp. Allocate and initialize a new one.
1868 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001869 if (!fbp) {
1870 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1871 &fbno);
1872 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001875 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001876 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001877 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001878 " data block %lld, got %lld ifbno %llu lastfbno %d",
1879 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001880 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 (long long)dbno, (long long)fbno,
1882 (unsigned long long)ifbno, lastfbno);
1883 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001884 xfs_alert(mp,
1885 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 fblk,
1887 (unsigned long long)fblk->blkno,
1888 fblk->index,
1889 fblk->magic);
1890 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001891 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
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 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001901 error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001902 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001904 free = fbp->b_addr;
1905 bests = xfs_dir3_free_bests_p(mp, free);
1906 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001909 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001911 freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1912 xfs_dir3_free_max_bests(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 free->hdr.nvalid = 0;
1914 free->hdr.nused = 0;
1915 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001916 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001917 bests = xfs_dir3_free_bests_p(mp, free);
1918 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 /*
1922 * Set the freespace block index from the data block number.
1923 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001924 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /*
1926 * If it's after the end of the current entries in the
1927 * freespace block, extend that table.
1928 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001929 if (findex >= freehdr.nvalid) {
1930 ASSERT(findex < xfs_dir3_free_max_bests(mp));
1931 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /*
1933 * Tag new entry so nused will go up.
1934 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001935 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 /*
1938 * If this entry was for an empty data block
1939 * (this should always be true) then update the header.
1940 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001941 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1942 freehdr.nused++;
1943 xfs_dir3_free_hdr_to_disk(fbp->b_addr, &freehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001951 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001952 bf = xfs_dir3_data_bestfree_p(hdr);
1953 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 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 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001963 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /*
1967 * Read the data block in.
1968 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001969 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001970 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001971 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001973 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001974 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 logfree = 0;
1976 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001977 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /*
1979 * Point to the existing unused space.
1980 */
1981 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001982 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 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,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001988 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 &needlog, &needscan);
1990 /*
1991 * Fill in the new entry and log it.
1992 */
1993 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001994 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 dep->namelen = args->namelen;
1996 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001997 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001998 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 xfs_dir2_data_log_entry(tp, dbp, dep);
2000 /*
2001 * Rescan the block for bestfree if needed.
2002 */
2003 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002004 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
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 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002013 bests = xfs_dir3_free_bests_p(mp, free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002014 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2015 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 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);
2023 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 * Return the data block and offset in args, then drop the data block.
2025 */
2026 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002027 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return 0;
2029}
2030
2031/*
2032 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002033 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * 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
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002045 trace_xfs_dir2_node_lookup(args);
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /*
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 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002058 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (error)
2060 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10002061 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
Dave Chinner1d9025e2012-06-22 18:50:14 +10002065 dep = (xfs_dir2_data_entry_t *)
2066 ((char *)state->extrablk.bp->b_addr +
2067 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002068 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * Release the btree blocks and leaf block.
2072 */
2073 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002074 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 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) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002081 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 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
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002100 trace_xfs_dir2_node_removename(args);
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /*
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 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002113 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002114 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /*
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);
Barry Naujok5163f952008-05-21 16:41:01 +10002132 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /*
2135 * Fix the hash values up the btree.
2136 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002137 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * If we need to join leaf blocks, do it.
2140 */
2141 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002142 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
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 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002160 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 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
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002170 trace_xfs_dir2_node_replace(args);
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /*
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 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002184 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 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) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002193 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /*
2195 * Find the leaf entry.
2196 */
2197 blk = &state->path.blk[state->path.active - 1];
2198 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002199 leaf = blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10002200 ents = xfs_dir3_leaf_ents_p(leaf);
2201 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 ASSERT(state->extravalid);
2203 /*
2204 * Point to the data entry.
2205 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002206 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002207 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2208 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002210 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002211 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002212 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /*
2214 * Fill in the new inode number and log the entry.
2215 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002216 dep->inumber = cpu_to_be64(inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 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) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002224 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 state->extrablk.bp = NULL;
2226 }
2227 /*
2228 * Release all the buffers in the cursor.
2229 */
2230 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002231 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 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{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002248 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 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 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002254 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 dp = args->dp;
2257 mp = dp->i_mount;
2258 tp = args->trans;
2259 /*
2260 * Read the freespace block.
2261 */
Dave Chinner20252072012-11-12 22:54:13 +11002262 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002263 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 /*
2266 * There can be holes in freespace. If fo is a hole, there's
2267 * nothing to do.
2268 */
Dave Chinner20252072012-11-12 22:54:13 +11002269 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002271 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002272 xfs_dir3_free_hdr_from_disk(&freehdr, free);
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /*
2275 * If there are used entries, there's nothing to do.
2276 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002277 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002278 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *rvalp = 0;
2280 return 0;
2281 }
2282 /*
2283 * Blow the block away.
2284 */
2285 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002286 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 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);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002294 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return error;
2296 }
2297 /*
2298 * Return that we succeeded.
2299 */
2300 *rvalp = 1;
2301 return 0;
2302}