blob: b8381646b8af6d9c6715781a84f704b558d8c25c [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_inode.h"
30#include "xfs_bmap.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020032#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner239880e2013-10-23 10:50:10 +110035#include "xfs_trans.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 Chinnerd75afeb2013-04-03 16:11:29 +1100150const struct xfs_buf_ops xfs_dir3_free_buf_ops = {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100151 .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{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100164 int err;
165
166 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100167 XFS_DATA_FORK, &xfs_dir3_free_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100168
169 /* try read returns without an error or *bpp if it lands in a hole */
170 if (!err && tp && *bpp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100171 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100172 return err;
Dave Chinner20252072012-11-12 22:54:13 +1100173}
174
175int
176xfs_dir2_free_read(
177 struct xfs_trans *tp,
178 struct xfs_inode *dp,
179 xfs_dablk_t fbno,
180 struct xfs_buf **bpp)
181{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100182 return __xfs_dir3_free_read(tp, dp, fbno, -1, bpp);
Dave Chinner20252072012-11-12 22:54:13 +1100183}
184
185static int
186xfs_dir2_free_try_read(
187 struct xfs_trans *tp,
188 struct xfs_inode *dp,
189 xfs_dablk_t fbno,
190 struct xfs_buf **bpp)
191{
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100192 return __xfs_dir3_free_read(tp, dp, fbno, -2, bpp);
193}
194
195
196void
197xfs_dir3_free_hdr_from_disk(
198 struct xfs_dir3_icfree_hdr *to,
199 struct xfs_dir2_free *from)
200{
201 if (from->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC)) {
202 to->magic = be32_to_cpu(from->hdr.magic);
203 to->firstdb = be32_to_cpu(from->hdr.firstdb);
204 to->nvalid = be32_to_cpu(from->hdr.nvalid);
205 to->nused = be32_to_cpu(from->hdr.nused);
206 } else {
207 struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)from;
208
209 to->magic = be32_to_cpu(hdr3->hdr.magic);
210 to->firstdb = be32_to_cpu(hdr3->firstdb);
211 to->nvalid = be32_to_cpu(hdr3->nvalid);
212 to->nused = be32_to_cpu(hdr3->nused);
213 }
214
215 ASSERT(to->magic == XFS_DIR2_FREE_MAGIC ||
216 to->magic == XFS_DIR3_FREE_MAGIC);
217}
218
219static void
220xfs_dir3_free_hdr_to_disk(
221 struct xfs_dir2_free *to,
222 struct xfs_dir3_icfree_hdr *from)
223{
224 ASSERT(from->magic == XFS_DIR2_FREE_MAGIC ||
225 from->magic == XFS_DIR3_FREE_MAGIC);
226
227 if (from->magic == XFS_DIR2_FREE_MAGIC) {
228 to->hdr.magic = cpu_to_be32(from->magic);
229 to->hdr.firstdb = cpu_to_be32(from->firstdb);
230 to->hdr.nvalid = cpu_to_be32(from->nvalid);
231 to->hdr.nused = cpu_to_be32(from->nused);
232 } else {
233 struct xfs_dir3_free_hdr *hdr3 = (struct xfs_dir3_free_hdr *)to;
234
235 hdr3->hdr.magic = cpu_to_be32(from->magic);
236 hdr3->firstdb = cpu_to_be32(from->firstdb);
237 hdr3->nvalid = cpu_to_be32(from->nvalid);
238 hdr3->nused = cpu_to_be32(from->nused);
239 }
240}
241
242static int
243xfs_dir3_free_get_buf(
244 struct xfs_trans *tp,
245 struct xfs_inode *dp,
246 xfs_dir2_db_t fbno,
247 struct xfs_buf **bpp)
248{
249 struct xfs_mount *mp = dp->i_mount;
250 struct xfs_buf *bp;
251 int error;
252 struct xfs_dir3_icfree_hdr hdr;
253
254 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fbno),
255 -1, &bp, XFS_DATA_FORK);
256 if (error)
257 return error;
258
Dave Chinner61fe1352013-04-03 16:11:30 +1100259 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_FREE_BUF);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100260 bp->b_ops = &xfs_dir3_free_buf_ops;
261
262 /*
263 * Initialize the new block to be empty, and remember
264 * its first slot as our empty slot.
265 */
Dave Chinnere400d272013-05-28 18:37:17 +1000266 memset(bp->b_addr, 0, sizeof(struct xfs_dir3_free_hdr));
267 memset(&hdr, 0, sizeof(hdr));
268
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100269 if (xfs_sb_version_hascrc(&mp->m_sb)) {
270 struct xfs_dir3_free_hdr *hdr3 = bp->b_addr;
271
272 hdr.magic = XFS_DIR3_FREE_MAGIC;
Dave Chinnere400d272013-05-28 18:37:17 +1000273
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100274 hdr3->hdr.blkno = cpu_to_be64(bp->b_bn);
275 hdr3->hdr.owner = cpu_to_be64(dp->i_ino);
276 uuid_copy(&hdr3->hdr.uuid, &mp->m_sb.sb_uuid);
Dave Chinnere400d272013-05-28 18:37:17 +1000277 } else
278 hdr.magic = XFS_DIR2_FREE_MAGIC;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100279 xfs_dir3_free_hdr_to_disk(bp->b_addr, &hdr);
280 *bpp = bp;
281 return 0;
Dave Chinner20252072012-11-12 22:54:13 +1100282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * Log entries from a freespace block.
286 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000287STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288xfs_dir2_free_log_bests(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000289 struct xfs_trans *tp,
290 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int first, /* first entry to log */
292 int last) /* last entry to log */
293{
294 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100295 __be16 *bests;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Dave Chinner1d9025e2012-06-22 18:50:14 +1000297 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100298 bests = xfs_dir3_free_bests_p(tp->t_mountp, free);
299 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
300 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner1d9025e2012-06-22 18:50:14 +1000301 xfs_trans_log_buf(tp, bp,
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100302 (uint)((char *)&bests[first] - (char *)free),
303 (uint)((char *)&bests[last] - (char *)free +
304 sizeof(bests[0]) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
308 * Log header from a freespace block.
309 */
310static void
311xfs_dir2_free_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000312 struct xfs_trans *tp,
313 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Dave Chinner836a94a2013-08-12 20:49:44 +1000315#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_dir2_free_t *free; /* freespace structure */
317
Dave Chinner1d9025e2012-06-22 18:50:14 +1000318 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100319 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
320 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Dave Chinner836a94a2013-08-12 20:49:44 +1000321#endif
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100322 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_free_hdr_size(tp->t_mountp) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * Convert a leaf-format directory to a node-format directory.
327 * We need to change the magic number of the leaf block, and copy
328 * the freespace table out of the leaf block into its own block.
329 */
330int /* error */
331xfs_dir2_leaf_to_node(
332 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000333 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 xfs_inode_t *dp; /* incore directory inode */
336 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000337 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 xfs_dir2_db_t fdb; /* freespace block number */
339 xfs_dir2_free_t *free; /* freespace structure */
Nathan Scott68b3a102006-03-17 17:27:19 +1100340 __be16 *from; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int i; /* leaf freespace index */
342 xfs_dir2_leaf_t *leaf; /* leaf structure */
343 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
344 xfs_mount_t *mp; /* filesystem mount point */
345 int n; /* count of live freespc ents */
346 xfs_dir2_data_off_t off; /* freespace entry value */
Nathan Scott0ba962e2006-03-17 17:27:07 +1100347 __be16 *to; /* pointer to freespace entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100349 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000351 trace_xfs_dir2_leaf_to_node(args);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 dp = args->dp;
354 mp = dp->i_mount;
355 tp = args->trans;
356 /*
357 * Add a freespace block to the directory.
358 */
359 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) {
360 return error;
361 }
362 ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp));
363 /*
364 * Get the buffer for the new freespace block.
365 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100366 error = xfs_dir3_free_get_buf(tp, dp, fdb, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100367 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100369
Dave Chinner1d9025e2012-06-22 18:50:14 +1000370 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100371 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000372 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000373 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100374 ASSERT(be32_to_cpu(ltp->bestcount) <=
375 (uint)dp->i_d.di_size / mp->m_dirblksize);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Copy freespace entries from the leaf block to the new block.
379 * Count active entries.
380 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100381 from = xfs_dir2_leaf_bests_p(ltp);
382 to = xfs_dir3_free_bests_p(mp, free);
383 for (i = n = 0; i < be32_to_cpu(ltp->bestcount); i++, from++, to++) {
Nathan Scott68b3a102006-03-17 17:27:19 +1100384 if ((off = be16_to_cpu(*from)) != NULLDATAOFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 n++;
Nathan Scott0ba962e2006-03-17 17:27:07 +1100386 *to = cpu_to_be16(off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100390 * Now initialize the freespace block header.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100392 freehdr.nused = n;
393 freehdr.nvalid = be32_to_cpu(ltp->bestcount);
394
395 xfs_dir3_free_hdr_to_disk(fbp->b_addr, &freehdr);
396 xfs_dir2_free_log_bests(tp, fbp, 0, freehdr.nvalid - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 xfs_dir2_free_log_header(tp, fbp);
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100398
Dave Chinner24df33b2013-04-12 07:30:21 +1000399 /*
400 * Converting the leaf to a leafnode is just a matter of changing the
401 * magic number and the ops. Do the change directly to the buffer as
402 * it's less work (and less code) than decoding the header to host
403 * format and back again.
404 */
405 if (leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC))
406 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC);
407 else
408 leaf->hdr.info.magic = cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
409 lbp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100410 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinner24df33b2013-04-12 07:30:21 +1000411 xfs_dir3_leaf_log_header(tp, lbp);
412 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 0;
414}
415
416/*
417 * Add a leaf entry to a leaf block in a node-form directory.
418 * The other work necessary is done from the caller.
419 */
420static int /* error */
421xfs_dir2_leafn_add(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000422 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 xfs_da_args_t *args, /* operation arguments */
424 int index) /* insertion pt for new entry */
425{
426 int compact; /* compacting stale leaves */
427 xfs_inode_t *dp; /* incore directory inode */
428 int highstale; /* next stale entry */
429 xfs_dir2_leaf_t *leaf; /* leaf structure */
430 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
431 int lfloghigh; /* high leaf entry logging */
432 int lfloglow; /* low leaf entry logging */
433 int lowstale; /* previous stale entry */
434 xfs_mount_t *mp; /* filesystem mount point */
435 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000436 struct xfs_dir3_icleaf_hdr leafhdr;
437 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000439 trace_xfs_dir2_leafn_add(args, index);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 dp = args->dp;
442 mp = dp->i_mount;
443 tp = args->trans;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000444 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000445 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
446 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 /*
449 * Quick check just to make sure we are not going to index
450 * into other peoples memory
451 */
452 if (index < 0)
453 return XFS_ERROR(EFSCORRUPTED);
454
455 /*
456 * If there are already the maximum number of leaf entries in
457 * the block, if there are no stale entries it won't fit.
458 * Caller will do a split. If there are stale entries we'll do
459 * a compact.
460 */
461
Dave Chinner24df33b2013-04-12 07:30:21 +1000462 if (leafhdr.count == xfs_dir3_max_leaf_ents(mp, leaf)) {
463 if (!leafhdr.stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return XFS_ERROR(ENOSPC);
Dave Chinner24df33b2013-04-12 07:30:21 +1000465 compact = leafhdr.stale > 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else
467 compact = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000468 ASSERT(index == 0 || be32_to_cpu(ents[index - 1].hashval) <= args->hashval);
469 ASSERT(index == leafhdr.count ||
470 be32_to_cpu(ents[index].hashval) >= args->hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Barry Naujok6a178102008-05-21 16:42:05 +1000472 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return 0;
474
475 /*
476 * Compact out all but one stale leaf entry. Leaves behind
477 * the entry closest to index.
478 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000479 if (compact)
480 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
481 &highstale, &lfloglow, &lfloghigh);
482 else if (leafhdr.stale) {
483 /*
484 * Set impossible logging indices for this case.
485 */
486 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 lfloghigh = -1;
488 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 /*
491 * Insert the new entry, log everything.
492 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000493 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200494 highstale, &lfloglow, &lfloghigh);
495
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100496 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000497 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp,
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100498 args->blkno, args->index));
Dave Chinner24df33b2013-04-12 07:30:21 +1000499
500 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
501 xfs_dir3_leaf_log_header(tp, bp);
502 xfs_dir3_leaf_log_ents(tp, bp, lfloglow, lfloghigh);
503 xfs_dir3_leaf_check(mp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
507#ifdef DEBUG
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100508static void
509xfs_dir2_free_hdr_check(
510 struct xfs_mount *mp,
511 struct xfs_buf *bp,
512 xfs_dir2_db_t db)
513{
514 struct xfs_dir3_icfree_hdr hdr;
515
516 xfs_dir3_free_hdr_from_disk(&hdr, bp->b_addr);
517
518 ASSERT((hdr.firstdb % xfs_dir3_free_max_bests(mp)) == 0);
519 ASSERT(hdr.firstdb <= db);
520 ASSERT(db < hdr.firstdb + hdr.nvalid);
521}
522#else
523#define xfs_dir2_free_hdr_check(mp, dp, db)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif /* DEBUG */
525
526/*
527 * Return the last hash value in the leaf.
528 * Stale entries are ok.
529 */
530xfs_dahash_t /* hash value */
531xfs_dir2_leafn_lasthash(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000532 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 int *count) /* count of entries in leaf */
534{
Dave Chinner24df33b2013-04-12 07:30:21 +1000535 struct xfs_dir2_leaf *leaf = bp->b_addr;
536 struct xfs_dir2_leaf_entry *ents;
537 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Chinner24df33b2013-04-12 07:30:21 +1000539 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
540
541 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
542 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (count)
Dave Chinner24df33b2013-04-12 07:30:21 +1000545 *count = leafhdr.count;
546 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000548
549 ents = xfs_dir3_leaf_ents_p(leaf);
550 return be32_to_cpu(ents[leafhdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000554 * Look up a leaf entry for space to add a name in a node-format leaf block.
555 * The extrablk in state is a freespace block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000557STATIC int
558xfs_dir2_leafn_lookup_for_addname(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000559 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 xfs_da_args_t *args, /* operation arguments */
561 int *indexp, /* out: leaf entry index */
562 xfs_da_state_t *state) /* state to fill in */
563{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000564 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000565 xfs_dir2_db_t curdb = -1; /* current data block number */
566 xfs_dir2_db_t curfdb = -1; /* current free block number */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 xfs_inode_t *dp; /* incore directory inode */
568 int error; /* error return value */
569 int fi; /* free entry index */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000570 xfs_dir2_free_t *free = NULL; /* free block structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int index; /* leaf entry index */
572 xfs_dir2_leaf_t *leaf; /* leaf structure */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000573 int length; /* length of new data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
575 xfs_mount_t *mp; /* filesystem mount point */
576 xfs_dir2_db_t newdb; /* new data block number */
577 xfs_dir2_db_t newfdb; /* new free block number */
578 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +1000579 struct xfs_dir2_leaf_entry *ents;
580 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 dp = args->dp;
583 tp = args->trans;
584 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000585 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000586 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
587 ents = xfs_dir3_leaf_ents_p(leaf);
588
589 xfs_dir3_leaf_check(mp, bp);
590 ASSERT(leafhdr.count > 0);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 /*
593 * Look up the hash value in the leaf entries.
594 */
595 index = xfs_dir2_leaf_search_hash(args, bp);
596 /*
597 * Do we have a buffer coming in?
598 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000599 if (state->extravalid) {
600 /* If so, it's a free block buffer, get the block number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 curbp = state->extrablk.bp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000602 curfdb = state->extrablk.blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000603 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100604 ASSERT(free->hdr.magic == cpu_to_be32(XFS_DIR2_FREE_MAGIC) ||
605 free->hdr.magic == cpu_to_be32(XFS_DIR3_FREE_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Dave Chinner0cb97762013-08-12 20:50:09 +1000607 length = xfs_dir3_data_entsize(mp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Loop over leaf entries with the right hash value.
610 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000611 for (lep = &ents[index];
612 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
613 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /*
615 * Skip stale leaf entries.
616 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100617 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 continue;
619 /*
620 * Pull the data block number from the entry.
621 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000622 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
624 * For addname, we're looking for a place to put the new entry.
625 * We want to use a data block with an entry of equal
626 * hash value to ours if there is one with room.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000627 *
628 * If this block isn't the data block we already have
629 * in hand, take a look at it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000631 if (newdb != curdb) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100632 __be16 *bests;
633
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000634 curdb = newdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000636 * Convert the data block to the free block
637 * holding its freespace information.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000639 newfdb = xfs_dir2_db_to_fdb(mp, newdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000641 * If it's not the one we have in hand, read it in.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000643 if (newfdb != curfdb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000645 * If we had one before, drop it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
647 if (curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000648 xfs_trans_brelse(tp, curbp);
Dave Chinner20252072012-11-12 22:54:13 +1100649
650 error = xfs_dir2_free_read(tp, dp,
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000651 xfs_dir2_db_to_da(mp, newfdb),
Dave Chinner20252072012-11-12 22:54:13 +1100652 &curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000653 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000655 free = curbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100656
657 xfs_dir2_free_hdr_check(mp, curbp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000660 * Get the index for our entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000662 fi = xfs_dir2_db_to_fdindex(mp, curdb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000664 * If it has room, return it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100666 bests = xfs_dir3_free_bests_p(mp, free);
667 if (unlikely(bests[fi] == cpu_to_be16(NULLDATAOFF))) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000668 XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int",
669 XFS_ERRLEVEL_LOW, mp);
670 if (curfdb != newfdb)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000671 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000672 return XFS_ERROR(EFSCORRUPTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000674 curfdb = newfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100675 if (be16_to_cpu(bests[fi]) >= length)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000676 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000679 /* Didn't find any space */
680 fi = -1;
681out:
Barry Naujok6a178102008-05-21 16:42:05 +1000682 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000683 if (curbp) {
684 /* Giving back a free block. */
685 state->extravalid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 state->extrablk.bp = curbp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000687 state->extrablk.index = fi;
688 state->extrablk.blkno = curfdb;
Dave Chinnercbc8adf2013-04-03 16:11:21 +1100689
690 /*
691 * Important: this magic number is not in the buffer - it's for
692 * buffer type information and therefore only the free/data type
693 * matters here, not whether CRCs are enabled or not.
694 */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000695 state->extrablk.magic = XFS_DIR2_FREE_MAGIC;
696 } else {
697 state->extravalid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 /*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000700 * Return the index, that will be the insertion point.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
702 *indexp = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return XFS_ERROR(ENOENT);
704}
705
706/*
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000707 * Look up a leaf entry in a node-format leaf block.
708 * The extrablk in state a data block.
709 */
710STATIC int
711xfs_dir2_leafn_lookup_for_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000712 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000713 xfs_da_args_t *args, /* operation arguments */
714 int *indexp, /* out: leaf entry index */
715 xfs_da_state_t *state) /* state to fill in */
716{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000717 struct xfs_buf *curbp = NULL; /* current data/free buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000718 xfs_dir2_db_t curdb = -1; /* current data block number */
719 xfs_dir2_data_entry_t *dep; /* data block entry */
720 xfs_inode_t *dp; /* incore directory inode */
721 int error; /* error return value */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000722 int index; /* leaf entry index */
723 xfs_dir2_leaf_t *leaf; /* leaf structure */
724 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
725 xfs_mount_t *mp; /* filesystem mount point */
726 xfs_dir2_db_t newdb; /* new data block number */
727 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok5163f952008-05-21 16:41:01 +1000728 enum xfs_dacmp cmp; /* comparison result */
Dave Chinner24df33b2013-04-12 07:30:21 +1000729 struct xfs_dir2_leaf_entry *ents;
730 struct xfs_dir3_icleaf_hdr leafhdr;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000731
732 dp = args->dp;
733 tp = args->trans;
734 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000735 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000736 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
737 ents = xfs_dir3_leaf_ents_p(leaf);
738
739 xfs_dir3_leaf_check(mp, bp);
740 ASSERT(leafhdr.count > 0);
741
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000742 /*
743 * Look up the hash value in the leaf entries.
744 */
745 index = xfs_dir2_leaf_search_hash(args, bp);
746 /*
747 * Do we have a buffer coming in?
748 */
749 if (state->extravalid) {
750 curbp = state->extrablk.bp;
751 curdb = state->extrablk.blkno;
752 }
753 /*
754 * Loop over leaf entries with the right hash value.
755 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000756 for (lep = &ents[index];
757 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
758 lep++, index++) {
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000759 /*
760 * Skip stale leaf entries.
761 */
762 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
763 continue;
764 /*
765 * Pull the data block number from the entry.
766 */
767 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
768 /*
769 * Not adding a new entry, so we really want to find
770 * the name given to us.
771 *
772 * If it's a different data block, go get it.
773 */
774 if (newdb != curdb) {
775 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000776 * If we had a block before that we aren't saving
777 * for a CI name, drop it
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000778 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000779 if (curbp && (args->cmpresult == XFS_CMP_DIFFERENT ||
780 curdb != state->extrablk.blkno))
Dave Chinner1d9025e2012-06-22 18:50:14 +1000781 xfs_trans_brelse(tp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000782 /*
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000783 * If needing the block that is saved with a CI match,
784 * use it otherwise read in the new data block.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000785 */
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000786 if (args->cmpresult != XFS_CMP_DIFFERENT &&
787 newdb == state->extrablk.blkno) {
788 ASSERT(state->extravalid);
789 curbp = state->extrablk.bp;
790 } else {
Dave Chinner33363fe2013-04-03 16:11:22 +1100791 error = xfs_dir3_data_read(tp, dp,
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000792 xfs_dir2_db_to_da(mp, newdb),
Dave Chinnere4813572012-11-12 22:54:14 +1100793 -1, &curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000794 if (error)
795 return error;
796 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100797 xfs_dir3_data_check(dp, curbp);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000798 curdb = newdb;
799 }
800 /*
801 * Point to the data entry.
802 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000803 dep = (xfs_dir2_data_entry_t *)((char *)curbp->b_addr +
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000804 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
805 /*
Barry Naujok5163f952008-05-21 16:41:01 +1000806 * Compare the entry and if it's an exact match, return
807 * EEXIST immediately. If it's the first case-insensitive
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000808 * match, store the block & inode number and continue looking.
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000809 */
Barry Naujok5163f952008-05-21 16:41:01 +1000810 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
811 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000812 /* If there is a CI match block, drop it */
813 if (args->cmpresult != XFS_CMP_DIFFERENT &&
814 curdb != state->extrablk.blkno)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000815 xfs_trans_brelse(tp, state->extrablk.bp);
Barry Naujok5163f952008-05-21 16:41:01 +1000816 args->cmpresult = cmp;
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000817 args->inumber = be64_to_cpu(dep->inumber);
Dave Chinner1c55cec2013-08-12 20:50:10 +1000818 args->filetype = xfs_dir3_dirent_get_ftype(mp, dep);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000819 *indexp = index;
820 state->extravalid = 1;
821 state->extrablk.bp = curbp;
822 state->extrablk.blkno = curdb;
823 state->extrablk.index = (int)((char *)dep -
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824 (char *)curbp->b_addr);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000825 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100826 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100827 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok5163f952008-05-21 16:41:01 +1000828 if (cmp == XFS_CMP_EXACT)
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000829 return XFS_ERROR(EEXIST);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000830 }
831 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000832 ASSERT(index == leafhdr.count || (args->op_flags & XFS_DA_OP_OKNOENT));
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000833 if (curbp) {
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000834 if (args->cmpresult == XFS_CMP_DIFFERENT) {
835 /* Giving back last used data block. */
836 state->extravalid = 1;
837 state->extrablk.bp = curbp;
838 state->extrablk.index = -1;
839 state->extrablk.blkno = curdb;
840 state->extrablk.magic = XFS_DIR2_DATA_MAGIC;
Dave Chinner33363fe2013-04-03 16:11:22 +1100841 curbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100842 xfs_trans_buf_set_type(tp, curbp, XFS_BLFT_DIR_DATA_BUF);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000843 } else {
844 /* If the curbp is not the CI match block, drop it */
845 if (state->extrablk.bp != curbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +1000846 xfs_trans_brelse(tp, curbp);
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000847 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000848 } else {
849 state->extravalid = 0;
850 }
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000851 *indexp = index;
Barry Naujok90bb7ab2008-06-23 13:25:38 +1000852 return XFS_ERROR(ENOENT);
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000853}
854
855/*
856 * Look up a leaf entry in a node-format leaf block.
857 * If this is an addname then the extrablk in state is a freespace block,
858 * otherwise it's a data block.
859 */
860int
861xfs_dir2_leafn_lookup_int(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000862 struct xfs_buf *bp, /* leaf buffer */
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000863 xfs_da_args_t *args, /* operation arguments */
864 int *indexp, /* out: leaf entry index */
865 xfs_da_state_t *state) /* state to fill in */
866{
Barry Naujok6a178102008-05-21 16:42:05 +1000867 if (args->op_flags & XFS_DA_OP_ADDNAME)
Barry Naujokf9f6dce2008-04-17 16:49:43 +1000868 return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp,
869 state);
870 return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state);
871}
872
873/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * Move count leaf entries from source to destination leaf.
875 * Log entries and headers. Stale entries are preserved.
876 */
877static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000878xfs_dir3_leafn_moveents(
879 xfs_da_args_t *args, /* operation arguments */
880 struct xfs_buf *bp_s, /* source */
881 struct xfs_dir3_icleaf_hdr *shdr,
882 struct xfs_dir2_leaf_entry *sents,
883 int start_s,/* source leaf index */
884 struct xfs_buf *bp_d, /* destination */
885 struct xfs_dir3_icleaf_hdr *dhdr,
886 struct xfs_dir2_leaf_entry *dents,
887 int start_d,/* destination leaf index */
888 int count) /* count of leaves to copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Dave Chinner24df33b2013-04-12 07:30:21 +1000890 struct xfs_trans *tp = args->trans;
891 int stale; /* count stale leaves copied */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000893 trace_xfs_dir2_leafn_moveents(args, start_s, start_d, count);
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /*
896 * Silently return if nothing to do.
897 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000898 if (count == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * If the destination index is not the end of the current
903 * destination leaf entries, open up a hole in the destination
904 * to hold the new entries.
905 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000906 if (start_d < dhdr->count) {
907 memmove(&dents[start_d + count], &dents[start_d],
908 (dhdr->count - start_d) * sizeof(xfs_dir2_leaf_entry_t));
909 xfs_dir3_leaf_log_ents(tp, bp_d, start_d + count,
910 count + dhdr->count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912 /*
913 * If the source has stale leaves, count the ones in the copy range
914 * so we can update the header correctly.
915 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000916 if (shdr->stale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int i; /* temp leaf index */
918
919 for (i = start_s, stale = 0; i < start_s + count; i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 if (sents[i].address ==
921 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 stale++;
923 }
924 } else
925 stale = 0;
926 /*
927 * Copy the leaf entries from source to destination.
928 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000929 memcpy(&dents[start_d], &sents[start_s],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner24df33b2013-04-12 07:30:21 +1000931 xfs_dir3_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * If there are source entries after the ones we copied,
935 * delete the ones we copied by sliding the next ones down.
936 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000937 if (start_s + count < shdr->count) {
938 memmove(&sents[start_s], &sents[start_s + count],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 count * sizeof(xfs_dir2_leaf_entry_t));
Dave Chinner24df33b2013-04-12 07:30:21 +1000940 xfs_dir3_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /*
944 * Update the headers and log them.
945 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000946 shdr->count -= count;
947 shdr->stale -= stale;
948 dhdr->count += count;
949 dhdr->stale += stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/*
953 * Determine the sort order of two leaf blocks.
954 * Returns 1 if both are valid and leaf2 should be before leaf1, else 0.
955 */
956int /* sort order */
957xfs_dir2_leafn_order(
Dave Chinner24df33b2013-04-12 07:30:21 +1000958 struct xfs_buf *leaf1_bp, /* leaf1 buffer */
959 struct xfs_buf *leaf2_bp) /* leaf2 buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Dave Chinner24df33b2013-04-12 07:30:21 +1000961 struct xfs_dir2_leaf *leaf1 = leaf1_bp->b_addr;
962 struct xfs_dir2_leaf *leaf2 = leaf2_bp->b_addr;
963 struct xfs_dir2_leaf_entry *ents1;
964 struct xfs_dir2_leaf_entry *ents2;
965 struct xfs_dir3_icleaf_hdr hdr1;
966 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Chinner24df33b2013-04-12 07:30:21 +1000968 xfs_dir3_leaf_hdr_from_disk(&hdr1, leaf1);
969 xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf2);
970 ents1 = xfs_dir3_leaf_ents_p(leaf1);
971 ents2 = xfs_dir3_leaf_ents_p(leaf2);
972
973 if (hdr1.count > 0 && hdr2.count > 0 &&
974 (be32_to_cpu(ents2[0].hashval) < be32_to_cpu(ents1[0].hashval) ||
975 be32_to_cpu(ents2[hdr2.count - 1].hashval) <
976 be32_to_cpu(ents1[hdr1.count - 1].hashval)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return 1;
978 return 0;
979}
980
981/*
982 * Rebalance leaf entries between two leaf blocks.
983 * This is actually only called when the second block is new,
984 * though the code deals with the general case.
985 * A new entry will be inserted in one of the blocks, and that
986 * entry is taken into account when balancing.
987 */
988static void
989xfs_dir2_leafn_rebalance(
990 xfs_da_state_t *state, /* btree cursor */
991 xfs_da_state_blk_t *blk1, /* first btree block */
992 xfs_da_state_blk_t *blk2) /* second btree block */
993{
994 xfs_da_args_t *args; /* operation arguments */
995 int count; /* count (& direction) leaves */
996 int isleft; /* new goes in left leaf */
997 xfs_dir2_leaf_t *leaf1; /* first leaf structure */
998 xfs_dir2_leaf_t *leaf2; /* second leaf structure */
999 int mid; /* midpoint leaf index */
Dave Chinner742ae1e2013-04-30 21:39:34 +10001000#if defined(DEBUG) || defined(XFS_WARN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int oldstale; /* old count of stale leaves */
1002#endif
1003 int oldsum; /* old total leaf count */
1004 int swap; /* swapped leaf blocks */
Dave Chinner24df33b2013-04-12 07:30:21 +10001005 struct xfs_dir2_leaf_entry *ents1;
1006 struct xfs_dir2_leaf_entry *ents2;
1007 struct xfs_dir3_icleaf_hdr hdr1;
1008 struct xfs_dir3_icleaf_hdr hdr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 args = state->args;
1011 /*
1012 * If the block order is wrong, swap the arguments.
1013 */
1014 if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) {
1015 xfs_da_state_blk_t *tmp; /* temp for block swap */
1016
1017 tmp = blk1;
1018 blk1 = blk2;
1019 blk2 = tmp;
1020 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001021 leaf1 = blk1->bp->b_addr;
1022 leaf2 = blk2->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001023 xfs_dir3_leaf_hdr_from_disk(&hdr1, leaf1);
1024 xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf2);
1025 ents1 = xfs_dir3_leaf_ents_p(leaf1);
1026 ents2 = xfs_dir3_leaf_ents_p(leaf2);
1027
1028 oldsum = hdr1.count + hdr2.count;
Dave Chinner742ae1e2013-04-30 21:39:34 +10001029#if defined(DEBUG) || defined(XFS_WARN)
Dave Chinner24df33b2013-04-12 07:30:21 +10001030 oldstale = hdr1.stale + hdr2.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031#endif
1032 mid = oldsum >> 1;
Dave Chinner24df33b2013-04-12 07:30:21 +10001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 /*
1035 * If the old leaf count was odd then the new one will be even,
1036 * so we need to divide the new count evenly.
1037 */
1038 if (oldsum & 1) {
1039 xfs_dahash_t midhash; /* middle entry hash value */
1040
Dave Chinner24df33b2013-04-12 07:30:21 +10001041 if (mid >= hdr1.count)
1042 midhash = be32_to_cpu(ents2[mid - hdr1.count].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 midhash = be32_to_cpu(ents1[mid].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 isleft = args->hashval <= midhash;
1046 }
1047 /*
1048 * If the old count is even then the new count is odd, so there's
1049 * no preferred side for the new entry.
1050 * Pick the left one.
1051 */
1052 else
1053 isleft = 1;
1054 /*
1055 * Calculate moved entry count. Positive means left-to-right,
1056 * negative means right-to-left. Then move the entries.
1057 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001058 count = hdr1.count - mid + (isleft == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (count > 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001060 xfs_dir3_leafn_moveents(args, blk1->bp, &hdr1, ents1,
1061 hdr1.count - count, blk2->bp,
1062 &hdr2, ents2, 0, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 else if (count < 0)
Dave Chinner24df33b2013-04-12 07:30:21 +10001064 xfs_dir3_leafn_moveents(args, blk2->bp, &hdr2, ents2, 0,
1065 blk1->bp, &hdr1, ents1,
1066 hdr1.count, count);
1067
1068 ASSERT(hdr1.count + hdr2.count == oldsum);
1069 ASSERT(hdr1.stale + hdr2.stale == oldstale);
1070
1071 /* log the changes made when moving the entries */
1072 xfs_dir3_leaf_hdr_to_disk(leaf1, &hdr1);
1073 xfs_dir3_leaf_hdr_to_disk(leaf2, &hdr2);
1074 xfs_dir3_leaf_log_header(args->trans, blk1->bp);
1075 xfs_dir3_leaf_log_header(args->trans, blk2->bp);
1076
1077 xfs_dir3_leaf_check(args->dp->i_mount, blk1->bp);
1078 xfs_dir3_leaf_check(args->dp->i_mount, blk2->bp);
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /*
1081 * Mark whether we're inserting into the old or new leaf.
1082 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001083 if (hdr1.count < hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 state->inleaf = swap;
Dave Chinner24df33b2013-04-12 07:30:21 +10001085 else if (hdr1.count > hdr2.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 state->inleaf = !swap;
1087 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001088 state->inleaf = swap ^ (blk1->index <= hdr1.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /*
1090 * Adjust the expected index for insertion.
1091 */
1092 if (!state->inleaf)
Dave Chinner24df33b2013-04-12 07:30:21 +10001093 blk2->index = blk1->index - hdr1.count;
Barry Naujokf9f6dce2008-04-17 16:49:43 +10001094
1095 /*
1096 * Finally sanity check just to make sure we are not returning a
1097 * negative index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
1099 if(blk2->index < 0) {
1100 state->inleaf = 1;
1101 blk2->index = 0;
Dave Chinner0b932cc2011-03-07 10:08:35 +11001102 xfs_alert(args->dp->i_mount,
Eric Sandeen08e96e12013-10-11 20:59:05 -05001103 "%s: picked the wrong leaf? reverting original leaf: blk1->index %d",
Dave Chinner0b932cc2011-03-07 10:08:35 +11001104 __func__, blk1->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
Dave Chinner20252072012-11-12 22:54:13 +11001108static int
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001109xfs_dir3_data_block_free(
Dave Chinner20252072012-11-12 22:54:13 +11001110 xfs_da_args_t *args,
1111 struct xfs_dir2_data_hdr *hdr,
1112 struct xfs_dir2_free *free,
1113 xfs_dir2_db_t fdb,
1114 int findex,
1115 struct xfs_buf *fbp,
1116 int longest)
1117{
1118 struct xfs_trans *tp = args->trans;
1119 int logfree = 0;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001120 __be16 *bests;
1121 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner20252072012-11-12 22:54:13 +11001122
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001123 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Dave Chinner20252072012-11-12 22:54:13 +11001124
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001125 bests = xfs_dir3_free_bests_p(tp->t_mountp, free);
1126 if (hdr) {
Dave Chinner20252072012-11-12 22:54:13 +11001127 /*
1128 * Data block is not empty, just set the free entry to the new
1129 * value.
1130 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001131 bests[findex] = cpu_to_be16(longest);
1132 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1133 return 0;
1134 }
1135
1136 /* One less used entry in the free table. */
1137 freehdr.nused--;
1138
1139 /*
1140 * If this was the last entry in the table, we can trim the table size
1141 * back. There might be other entries at the end referring to
1142 * non-existent data blocks, get those too.
1143 */
1144 if (findex == freehdr.nvalid - 1) {
1145 int i; /* free entry index */
1146
1147 for (i = findex - 1; i >= 0; i--) {
1148 if (bests[i] != cpu_to_be16(NULLDATAOFF))
1149 break;
1150 }
1151 freehdr.nvalid = i + 1;
1152 logfree = 0;
1153 } else {
1154 /* Not the last entry, just punch it out. */
1155 bests[findex] = cpu_to_be16(NULLDATAOFF);
Dave Chinner20252072012-11-12 22:54:13 +11001156 logfree = 1;
1157 }
1158
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001159 xfs_dir3_free_hdr_to_disk(free, &freehdr);
1160 xfs_dir2_free_log_header(tp, fbp);
1161
1162 /*
1163 * If there are no useful entries left in the block, get rid of the
1164 * block if we can.
1165 */
1166 if (!freehdr.nused) {
1167 int error;
1168
1169 error = xfs_dir2_shrink_inode(args, fdb, fbp);
1170 if (error == 0) {
1171 fbp = NULL;
1172 logfree = 0;
1173 } else if (error != ENOSPC || args->total != 0)
1174 return error;
1175 /*
1176 * It's possible to get ENOSPC if there is no
1177 * space reservation. In this case some one
1178 * else will eventually get rid of this block.
1179 */
1180 }
1181
Dave Chinner20252072012-11-12 22:54:13 +11001182 /* Log the free entry that changed, unless we got rid of it. */
1183 if (logfree)
1184 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
1185 return 0;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
1189 * Remove an entry from a node directory.
1190 * This removes the leaf entry and the data entry,
1191 * and updates the free block if necessary.
1192 */
1193static int /* error */
1194xfs_dir2_leafn_remove(
1195 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001196 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int index, /* leaf entry index */
1198 xfs_da_state_blk_t *dblk, /* data block */
1199 int *rval) /* resulting block needs join */
1200{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001201 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001203 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 xfs_dir2_data_entry_t *dep; /* data block entry */
1205 xfs_inode_t *dp; /* incore directory inode */
1206 xfs_dir2_leaf_t *leaf; /* leaf structure */
1207 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1208 int longest; /* longest data free entry */
1209 int off; /* data block entry offset */
1210 xfs_mount_t *mp; /* filesystem mount point */
1211 int needlog; /* need to log data header */
1212 int needscan; /* need to rescan data frees */
1213 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001214 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001215 struct xfs_dir3_icleaf_hdr leafhdr;
1216 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001218 trace_xfs_dir2_leafn_remove(args, index);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 dp = args->dp;
1221 tp = args->trans;
1222 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001223 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001224 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1225 ents = xfs_dir3_leaf_ents_p(leaf);
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * Point to the entry we're removing.
1229 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001230 lep = &ents[index];
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * Extract the data block and offset from the entry.
1234 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001235 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ASSERT(dblk->blkno == db);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001237 off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ASSERT(dblk->index == off);
Dave Chinner24df33b2013-04-12 07:30:21 +10001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * Kill the leaf entry by marking it stale.
1242 * Log the leaf block changes.
1243 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001244 leafhdr.stale++;
1245 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1246 xfs_dir3_leaf_log_header(tp, bp);
1247
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001248 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001249 xfs_dir3_leaf_log_ents(tp, bp, index, index);
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 /*
1252 * Make the data entry free. Keep track of the longest freespace
1253 * in the data block in case it changes.
1254 */
1255 dbp = dblk->bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001256 hdr = dbp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001257 dep = (xfs_dir2_data_entry_t *)((char *)hdr + off);
Dave Chinner33363fe2013-04-03 16:11:22 +11001258 bf = xfs_dir3_data_bestfree_p(hdr);
1259 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 needlog = needscan = 0;
1261 xfs_dir2_data_make_free(tp, dbp, off,
Dave Chinner0cb97762013-08-12 20:50:09 +10001262 xfs_dir3_data_entsize(mp, dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /*
1264 * Rescan the data block freespaces for bestfree.
1265 * Log the data block header if needed.
1266 */
1267 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001268 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (needlog)
1270 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001271 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /*
1273 * If the longest data block freespace changes, need to update
1274 * the corresponding freeblock entry.
1275 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001276 if (longest < be16_to_cpu(bf[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int error; /* error return value */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001278 struct xfs_buf *fbp; /* freeblock buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 xfs_dir2_db_t fdb; /* freeblock block number */
1280 int findex; /* index in freeblock entries */
1281 xfs_dir2_free_t *free; /* freeblock structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
1284 * Convert the data block number to a free block,
1285 * read in the free block.
1286 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001287 fdb = xfs_dir2_db_to_fdb(mp, db);
Dave Chinner20252072012-11-12 22:54:13 +11001288 error = xfs_dir2_free_read(tp, dp, xfs_dir2_db_to_da(mp, fdb),
1289 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001292 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001293#ifdef DEBUG
1294 {
1295 struct xfs_dir3_icfree_hdr freehdr;
1296 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1297 ASSERT(freehdr.firstdb == xfs_dir3_free_max_bests(mp) *
1298 (fdb - XFS_DIR2_FREE_FIRSTDB(mp)));
1299 }
1300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Calculate which entry we need to fix.
1303 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001304 findex = xfs_dir2_db_to_fdindex(mp, db);
Dave Chinner33363fe2013-04-03 16:11:22 +11001305 longest = be16_to_cpu(bf[0].length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /*
1307 * If the data block is now empty we can get rid of it
1308 * (usually).
1309 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001310 if (longest == mp->m_dirblksize -
1311 xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /*
1313 * Try to punch out the data block.
1314 */
1315 error = xfs_dir2_shrink_inode(args, db, dbp);
1316 if (error == 0) {
1317 dblk->bp = NULL;
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001318 hdr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320 /*
1321 * We can get ENOSPC if there's no space reservation.
1322 * In this case just drop the buffer and some one else
1323 * will eventually get rid of the empty block.
1324 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001325 else if (!(error == ENOSPC && args->total == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return error;
1327 }
1328 /*
1329 * If we got rid of the data block, we can eliminate that entry
1330 * in the free block.
1331 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001332 error = xfs_dir3_data_block_free(args, hdr, free,
Dave Chinner20252072012-11-12 22:54:13 +11001333 fdb, findex, fbp, longest);
1334 if (error)
1335 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Dave Chinner20252072012-11-12 22:54:13 +11001337
Dave Chinner24df33b2013-04-12 07:30:21 +10001338 xfs_dir3_leaf_check(mp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /*
Malcolm Parsons9da096f2009-03-29 09:55:42 +02001340 * Return indication of whether this leaf block is empty enough
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 * to justify trying to join it with a neighbor.
1342 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001343 *rval = (xfs_dir3_leaf_hdr_size(leaf) +
1344 (uint)sizeof(ents[0]) * (leafhdr.count - leafhdr.stale)) <
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 mp->m_dir_magicpct;
1346 return 0;
1347}
1348
1349/*
1350 * Split the leaf entries in the old block into old and new blocks.
1351 */
1352int /* error */
1353xfs_dir2_leafn_split(
1354 xfs_da_state_t *state, /* btree cursor */
1355 xfs_da_state_blk_t *oldblk, /* original block */
1356 xfs_da_state_blk_t *newblk) /* newly created block */
1357{
1358 xfs_da_args_t *args; /* operation arguments */
1359 xfs_dablk_t blkno; /* new leaf block number */
1360 int error; /* error return value */
1361 xfs_mount_t *mp; /* filesystem mount point */
1362
1363 /*
1364 * Allocate space for a new leaf node.
1365 */
1366 args = state->args;
1367 mp = args->dp->i_mount;
1368 ASSERT(args != NULL);
1369 ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC);
1370 error = xfs_da_grow_inode(args, &blkno);
1371 if (error) {
1372 return error;
1373 }
1374 /*
1375 * Initialize the new leaf block.
1376 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001377 error = xfs_dir3_leaf_get_buf(args, xfs_dir2_da_to_db(mp, blkno),
1378 &newblk->bp, XFS_DIR2_LEAFN_MAGIC);
1379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +10001381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 newblk->blkno = blkno;
1383 newblk->magic = XFS_DIR2_LEAFN_MAGIC;
1384 /*
1385 * Rebalance the entries across the two leaves, link the new
1386 * block into the leaves.
1387 */
1388 xfs_dir2_leafn_rebalance(state, oldblk, newblk);
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001389 error = xfs_da3_blk_link(state, oldblk, newblk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (error) {
1391 return error;
1392 }
1393 /*
1394 * Insert the new entry in the correct block.
1395 */
1396 if (state->inleaf)
1397 error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index);
1398 else
1399 error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index);
1400 /*
1401 * Update last hashval in each block since we added the name.
1402 */
1403 oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL);
1404 newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL);
Dave Chinner24df33b2013-04-12 07:30:21 +10001405 xfs_dir3_leaf_check(mp, oldblk->bp);
1406 xfs_dir3_leaf_check(mp, newblk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return error;
1408}
1409
1410/*
1411 * Check a leaf block and its neighbors to see if the block should be
1412 * collapsed into one or the other neighbor. Always keep the block
1413 * with the smaller block number.
1414 * If the current block is over 50% full, don't try to join it, return 0.
1415 * If the block is empty, fill in the state structure and return 2.
1416 * If it can be collapsed, fill in the state structure and return 1.
1417 * If nothing can be done, return 0.
1418 */
1419int /* error */
1420xfs_dir2_leafn_toosmall(
1421 xfs_da_state_t *state, /* btree cursor */
1422 int *action) /* resulting action to take */
1423{
1424 xfs_da_state_blk_t *blk; /* leaf block */
1425 xfs_dablk_t blkno; /* leaf block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001426 struct xfs_buf *bp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 int bytes; /* bytes in use */
1428 int count; /* leaf live entry count */
1429 int error; /* error return value */
1430 int forward; /* sibling block direction */
1431 int i; /* sibling counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 xfs_dir2_leaf_t *leaf; /* leaf structure */
1433 int rval; /* result from path_shift */
Dave Chinner24df33b2013-04-12 07:30:21 +10001434 struct xfs_dir3_icleaf_hdr leafhdr;
1435 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 /*
1438 * Check for the degenerate case of the block being over 50% full.
1439 * If so, it's not worth even looking to see if we might be able
1440 * to coalesce with a sibling.
1441 */
1442 blk = &state->path.blk[state->path.active - 1];
Dave Chinner24df33b2013-04-12 07:30:21 +10001443 leaf = blk->bp->b_addr;
1444 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1445 ents = xfs_dir3_leaf_ents_p(leaf);
1446 xfs_dir3_leaf_check(state->args->dp->i_mount, blk->bp);
1447
1448 count = leafhdr.count - leafhdr.stale;
1449 bytes = xfs_dir3_leaf_hdr_size(leaf) + count * sizeof(ents[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (bytes > (state->blocksize >> 1)) {
1451 /*
1452 * Blk over 50%, don't try to join.
1453 */
1454 *action = 0;
1455 return 0;
1456 }
1457 /*
1458 * Check for the degenerate case of the block being empty.
1459 * If the block is empty, we'll simply delete it, no need to
1460 * coalesce it with a sibling block. We choose (arbitrarily)
1461 * to merge with the forward block unless it is NULL.
1462 */
1463 if (count == 0) {
1464 /*
1465 * Make altpath point to the block we want to keep and
1466 * path point to the block we want to drop (this one).
1467 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001468 forward = (leafhdr.forw != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 memcpy(&state->altpath, &state->path, sizeof(state->path));
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001470 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 &rval);
1472 if (error)
1473 return error;
1474 *action = rval ? 2 : 0;
1475 return 0;
1476 }
1477 /*
1478 * Examine each sibling block to see if we can coalesce with
1479 * at least 25% free space to spare. We need to figure out
1480 * whether to merge with the forward or the backward block.
1481 * We prefer coalescing with the lower numbered sibling so as
1482 * to shrink a directory over time.
1483 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001484 forward = leafhdr.forw < leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 for (i = 0, bp = NULL; i < 2; forward = !forward, i++) {
Dave Chinner24df33b2013-04-12 07:30:21 +10001486 struct xfs_dir3_icleaf_hdr hdr2;
1487
1488 blkno = forward ? leafhdr.forw : leafhdr.back;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (blkno == 0)
1490 continue;
1491 /*
1492 * Read the sibling leaf block.
1493 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001494 error = xfs_dir3_leafn_read(state->args->trans, state->args->dp,
Dave Chinnere6f76672012-11-12 22:54:15 +11001495 blkno, -1, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001496 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * Count bytes in the two blocks combined.
1501 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001502 count = leafhdr.count - leafhdr.stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 bytes = state->blocksize - (state->blocksize >> 2);
Dave Chinner24df33b2013-04-12 07:30:21 +10001504
Dave Chinner1d9025e2012-06-22 18:50:14 +10001505 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001506 xfs_dir3_leaf_hdr_from_disk(&hdr2, leaf);
1507 ents = xfs_dir3_leaf_ents_p(leaf);
1508 count += hdr2.count - hdr2.stale;
1509 bytes -= count * sizeof(ents[0]);
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /*
1512 * Fits with at least 25% to spare.
1513 */
1514 if (bytes >= 0)
1515 break;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001516 xfs_trans_brelse(state->args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 /*
1519 * Didn't like either block, give up.
1520 */
1521 if (i >= 2) {
1522 *action = 0;
1523 return 0;
1524 }
Dave Chinner1d9025e2012-06-22 18:50:14 +10001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /*
1527 * Make altpath point to the block we want to keep (the lower
1528 * numbered block) and path point to the block we want to drop.
1529 */
1530 memcpy(&state->altpath, &state->path, sizeof(state->path));
1531 if (blkno < blk->blkno)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001532 error = xfs_da3_path_shift(state, &state->altpath, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 &rval);
1534 else
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001535 error = xfs_da3_path_shift(state, &state->path, forward, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 &rval);
1537 if (error) {
1538 return error;
1539 }
1540 *action = rval ? 0 : 1;
1541 return 0;
1542}
1543
1544/*
1545 * Move all the leaf entries from drop_blk to save_blk.
1546 * This is done as part of a join operation.
1547 */
1548void
1549xfs_dir2_leafn_unbalance(
1550 xfs_da_state_t *state, /* cursor */
1551 xfs_da_state_blk_t *drop_blk, /* dead block */
1552 xfs_da_state_blk_t *save_blk) /* surviving block */
1553{
1554 xfs_da_args_t *args; /* operation arguments */
1555 xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */
1556 xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001557 struct xfs_dir3_icleaf_hdr savehdr;
1558 struct xfs_dir3_icleaf_hdr drophdr;
1559 struct xfs_dir2_leaf_entry *sents;
1560 struct xfs_dir2_leaf_entry *dents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 args = state->args;
1563 ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC);
1564 ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001565 drop_leaf = drop_blk->bp->b_addr;
1566 save_leaf = save_blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001567
1568 xfs_dir3_leaf_hdr_from_disk(&savehdr, save_leaf);
1569 xfs_dir3_leaf_hdr_from_disk(&drophdr, drop_leaf);
1570 sents = xfs_dir3_leaf_ents_p(save_leaf);
1571 dents = xfs_dir3_leaf_ents_p(drop_leaf);
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /*
1574 * If there are any stale leaf entries, take this opportunity
1575 * to purge them.
1576 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001577 if (drophdr.stale)
1578 xfs_dir3_leaf_compact(args, &drophdr, drop_blk->bp);
1579 if (savehdr.stale)
1580 xfs_dir3_leaf_compact(args, &savehdr, save_blk->bp);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
1583 * Move the entries from drop to the appropriate end of save.
1584 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001585 drop_blk->hashval = be32_to_cpu(dents[drophdr.count - 1].hashval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp))
Dave Chinner24df33b2013-04-12 07:30:21 +10001587 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1588 save_blk->bp, &savehdr, sents, 0,
1589 drophdr.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001591 xfs_dir3_leafn_moveents(args, drop_blk->bp, &drophdr, dents, 0,
1592 save_blk->bp, &savehdr, sents,
1593 savehdr.count, drophdr.count);
1594 save_blk->hashval = be32_to_cpu(sents[savehdr.count - 1].hashval);
1595
1596 /* log the changes made when moving the entries */
1597 xfs_dir3_leaf_hdr_to_disk(save_leaf, &savehdr);
1598 xfs_dir3_leaf_hdr_to_disk(drop_leaf, &drophdr);
1599 xfs_dir3_leaf_log_header(args->trans, save_blk->bp);
1600 xfs_dir3_leaf_log_header(args->trans, drop_blk->bp);
1601
1602 xfs_dir3_leaf_check(args->dp->i_mount, save_blk->bp);
1603 xfs_dir3_leaf_check(args->dp->i_mount, drop_blk->bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
1606/*
1607 * Top-level node form directory addname routine.
1608 */
1609int /* error */
1610xfs_dir2_node_addname(
1611 xfs_da_args_t *args) /* operation arguments */
1612{
1613 xfs_da_state_blk_t *blk; /* leaf block for insert */
1614 int error; /* error return value */
1615 int rval; /* sub-return value */
1616 xfs_da_state_t *state; /* btree cursor */
1617
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001618 trace_xfs_dir2_node_addname(args);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /*
1621 * Allocate and initialize the state (btree cursor).
1622 */
1623 state = xfs_da_state_alloc();
1624 state->args = args;
1625 state->mp = args->dp->i_mount;
1626 state->blocksize = state->mp->m_dirblksize;
1627 state->node_ents = state->mp->m_dir_node_ents;
1628 /*
1629 * Look up the name. We're not supposed to find it, but
1630 * this gives us the insertion point.
1631 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001632 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (error)
1634 rval = error;
1635 if (rval != ENOENT) {
1636 goto done;
1637 }
1638 /*
1639 * Add the data entry to a data block.
1640 * Extravalid is set to a freeblock found by lookup.
1641 */
1642 rval = xfs_dir2_node_addname_int(args,
1643 state->extravalid ? &state->extrablk : NULL);
1644 if (rval) {
1645 goto done;
1646 }
1647 blk = &state->path.blk[state->path.active - 1];
1648 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
1649 /*
1650 * Add the new leaf entry.
1651 */
1652 rval = xfs_dir2_leafn_add(blk->bp, args, blk->index);
1653 if (rval == 0) {
1654 /*
1655 * It worked, fix the hash values up the btree.
1656 */
Barry Naujok6a178102008-05-21 16:42:05 +10001657 if (!(args->op_flags & XFS_DA_OP_JUSTCHECK))
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001658 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 } else {
1660 /*
1661 * It didn't work, we need to split the leaf block.
1662 */
1663 if (args->total == 0) {
1664 ASSERT(rval == ENOSPC);
1665 goto done;
1666 }
1667 /*
1668 * Split the leaf block and insert the new entry.
1669 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10001670 rval = xfs_da3_split(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
1672done:
1673 xfs_da_state_free(state);
1674 return rval;
1675}
1676
1677/*
1678 * Add the data entry for a node-format directory name addition.
1679 * The leaf entry is added in xfs_dir2_leafn_add.
1680 * We may enter with a freespace block that the lookup found.
1681 */
1682static int /* error */
1683xfs_dir2_node_addname_int(
1684 xfs_da_args_t *args, /* operation arguments */
1685 xfs_da_state_blk_t *fblk) /* optional freespace block */
1686{
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001687 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 xfs_dir2_db_t dbno; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001689 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 xfs_dir2_data_entry_t *dep; /* data entry pointer */
1691 xfs_inode_t *dp; /* incore directory inode */
1692 xfs_dir2_data_unused_t *dup; /* data unused entry pointer */
1693 int error; /* error return value */
1694 xfs_dir2_db_t fbno; /* freespace block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001695 struct xfs_buf *fbp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 int findex; /* freespace entry index */
1697 xfs_dir2_free_t *free=NULL; /* freespace block structure */
1698 xfs_dir2_db_t ifbno; /* initial freespace block no */
1699 xfs_dir2_db_t lastfbno=0; /* highest freespace block no */
1700 int length; /* length of the new entry */
1701 int logfree; /* need to log free entry */
1702 xfs_mount_t *mp; /* filesystem mount point */
1703 int needlog; /* need to log data header */
1704 int needscan; /* need to rescan data frees */
Nathan Scott3d693c62006-03-17 17:28:27 +11001705 __be16 *tagp; /* data entry tag pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001707 __be16 *bests;
1708 struct xfs_dir3_icfree_hdr freehdr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001709 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 dp = args->dp;
1712 mp = dp->i_mount;
1713 tp = args->trans;
Dave Chinner0cb97762013-08-12 20:50:09 +10001714 length = xfs_dir3_data_entsize(mp, args->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * If we came in with a freespace block that means that lookup
1717 * found an entry with our hash value. This is the freespace
1718 * block for that data entry.
1719 */
1720 if (fblk) {
1721 fbp = fblk->bp;
1722 /*
1723 * Remember initial freespace block number.
1724 */
1725 ifbno = fblk->blkno;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001726 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 findex = fblk->index;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001728 bests = xfs_dir3_free_bests_p(mp, free);
1729 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * This means the free entry showed that the data block had
1733 * space for our entry, so we remembered it.
1734 * Use that data block.
1735 */
1736 if (findex >= 0) {
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001737 ASSERT(findex < freehdr.nvalid);
1738 ASSERT(be16_to_cpu(bests[findex]) != NULLDATAOFF);
1739 ASSERT(be16_to_cpu(bests[findex]) >= length);
1740 dbno = freehdr.firstdb + findex;
1741 } else {
1742 /*
1743 * The data block looked at didn't have enough room.
1744 * We'll start at the beginning of the freespace entries.
1745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 dbno = -1;
1747 findex = 0;
1748 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001749 } else {
1750 /*
1751 * Didn't come in with a freespace block, so no data block.
1752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 ifbno = dbno = -1;
1754 fbp = NULL;
1755 findex = 0;
1756 }
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * If we don't have a data block yet, we're going to scan the
1760 * freespace blocks looking for one. Figure out what the
1761 * highest freespace block number is.
1762 */
1763 if (dbno == -1) {
1764 xfs_fileoff_t fo; /* freespace block number */
1765
1766 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK)))
1767 return error;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001768 lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 fbno = ifbno;
1770 }
1771 /*
1772 * While we haven't identified a data block, search the freeblock
1773 * data for a good data block. If we find a null freeblock entry,
1774 * indicating a hole in the data blocks, remember that.
1775 */
1776 while (dbno == -1) {
1777 /*
1778 * If we don't have a freeblock in hand, get the next one.
1779 */
1780 if (fbp == NULL) {
1781 /*
1782 * Happens the first time through unless lookup gave
1783 * us a freespace block to start with.
1784 */
1785 if (++fbno == 0)
1786 fbno = XFS_DIR2_FREE_FIRSTDB(mp);
1787 /*
1788 * If it's ifbno we already looked at it.
1789 */
1790 if (fbno == ifbno)
1791 fbno++;
1792 /*
1793 * If it's off the end we're done.
1794 */
1795 if (fbno >= lastfbno)
1796 break;
1797 /*
1798 * Read the block. There can be holes in the
1799 * freespace blocks, so this might not succeed.
1800 * This should be really rare, so there's no reason
1801 * to avoid it.
1802 */
Dave Chinner20252072012-11-12 22:54:13 +11001803 error = xfs_dir2_free_try_read(tp, dp,
1804 xfs_dir2_db_to_da(mp, fbno),
1805 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001806 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return error;
Dave Chinner4bb20a82012-11-12 22:54:10 +11001808 if (!fbp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 continue;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001810 free = fbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 findex = 0;
1812 }
1813 /*
1814 * Look at the current free entry. Is it good enough?
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001815 *
1816 * The bests initialisation should be where the bufer is read in
1817 * the above branch. But gcc is too stupid to realise that bests
1818 * and the freehdr are actually initialised if they are placed
1819 * there, so we have to do it here to avoid warnings. Blech.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001821 bests = xfs_dir3_free_bests_p(mp, free);
1822 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1823 if (be16_to_cpu(bests[findex]) != NULLDATAOFF &&
1824 be16_to_cpu(bests[findex]) >= length)
1825 dbno = freehdr.firstdb + findex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 else {
1827 /*
1828 * Are we done with the freeblock?
1829 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001830 if (++findex == freehdr.nvalid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Drop the block.
1833 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001834 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 fbp = NULL;
1836 if (fblk && fblk->bp)
1837 fblk->bp = NULL;
1838 }
1839 }
1840 }
1841 /*
1842 * If we don't have a data block, we need to allocate one and make
1843 * the freespace entries refer to it.
1844 */
1845 if (unlikely(dbno == -1)) {
1846 /*
1847 * Not allowed to allocate, return failure.
1848 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001849 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return XFS_ERROR(ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /*
1853 * Allocate and initialize the new data block.
1854 */
1855 if (unlikely((error = xfs_dir2_grow_inode(args,
1856 XFS_DIR2_DATA_SPACE,
1857 &dbno)) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001858 (error = xfs_dir3_data_init(args, dbno, &dbp))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /*
1862 * If (somehow) we have a freespace block, get rid of it.
1863 */
1864 if (fbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001865 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (fblk && fblk->bp)
1867 fblk->bp = NULL;
1868
1869 /*
1870 * Get the freespace block corresponding to the data block
1871 * that was just allocated.
1872 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001873 fbno = xfs_dir2_db_to_fdb(mp, dbno);
Dave Chinner20252072012-11-12 22:54:13 +11001874 error = xfs_dir2_free_try_read(tp, dp,
1875 xfs_dir2_db_to_da(mp, fbno),
1876 &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /*
1881 * If there wasn't a freespace block, the read will
1882 * return a NULL fbp. Allocate and initialize a new one.
1883 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001884 if (!fbp) {
1885 error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE,
1886 &fbno);
1887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001890 if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001891 xfs_alert(mp,
Jean Delvaredf2e3012011-07-16 18:10:35 +02001892 "%s: dir ino %llu needed freesp block %lld for\n"
Dave Chinner0b932cc2011-03-07 10:08:35 +11001893 " data block %lld, got %lld ifbno %llu lastfbno %d",
1894 __func__, (unsigned long long)dp->i_ino,
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001895 (long long)xfs_dir2_db_to_fdb(mp, dbno),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 (long long)dbno, (long long)fbno,
1897 (unsigned long long)ifbno, lastfbno);
1898 if (fblk) {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001899 xfs_alert(mp,
1900 " fblk 0x%p blkno %llu index %d magic 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 fblk,
1902 (unsigned long long)fblk->blkno,
1903 fblk->index,
1904 fblk->magic);
1905 } else {
Dave Chinner0b932cc2011-03-07 10:08:35 +11001906 xfs_alert(mp, " ... fblk is NULL");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908 XFS_ERROR_REPORT("xfs_dir2_node_addname_int",
1909 XFS_ERRLEVEL_LOW, mp);
1910 return XFS_ERROR(EFSCORRUPTED);
1911 }
1912
1913 /*
1914 * Get a buffer for the new block.
1915 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001916 error = xfs_dir3_free_get_buf(tp, dp, fbno, &fbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001917 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return error;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001919 free = fbp->b_addr;
1920 bests = xfs_dir3_free_bests_p(mp, free);
1921 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /*
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001924 * Remember the first slot as our empty slot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001926 freehdr.firstdb = (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) *
1927 xfs_dir3_free_max_bests(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 } else {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001929 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001930 bests = xfs_dir3_free_bests_p(mp, free);
1931 xfs_dir3_free_hdr_from_disk(&freehdr, free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
1934 /*
1935 * Set the freespace block index from the data block number.
1936 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001937 findex = xfs_dir2_db_to_fdindex(mp, dbno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * If it's after the end of the current entries in the
1940 * freespace block, extend that table.
1941 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001942 if (findex >= freehdr.nvalid) {
1943 ASSERT(findex < xfs_dir3_free_max_bests(mp));
1944 freehdr.nvalid = findex + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /*
1946 * Tag new entry so nused will go up.
1947 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001948 bests[findex] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 /*
1951 * If this entry was for an empty data block
1952 * (this should always be true) then update the header.
1953 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001954 if (bests[findex] == cpu_to_be16(NULLDATAOFF)) {
1955 freehdr.nused++;
1956 xfs_dir3_free_hdr_to_disk(fbp->b_addr, &freehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 xfs_dir2_free_log_header(tp, fbp);
1958 }
1959 /*
1960 * Update the real value in the table.
1961 * We haven't allocated the data entry yet so this will
1962 * change again.
1963 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001964 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001965 bf = xfs_dir3_data_bestfree_p(hdr);
1966 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 logfree = 1;
1968 }
1969 /*
1970 * We had a data block so we don't have to make a new one.
1971 */
1972 else {
1973 /*
1974 * If just checking, we succeeded.
1975 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001976 if (args->op_flags & XFS_DA_OP_JUSTCHECK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /*
1980 * Read the data block in.
1981 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001982 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, dbno),
Dave Chinnere4813572012-11-12 22:54:14 +11001983 -1, &dbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001984 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001986 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001987 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 logfree = 0;
1989 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001990 ASSERT(be16_to_cpu(bf[0].length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Point to the existing unused space.
1993 */
1994 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +11001995 ((char *)hdr + be16_to_cpu(bf[0].offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 needscan = needlog = 0;
1997 /*
1998 * Mark the first part of the unused space, inuse for us.
1999 */
2000 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002001 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 &needlog, &needscan);
2003 /*
2004 * Fill in the new entry and log it.
2005 */
2006 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002007 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 dep->namelen = args->namelen;
2009 memcpy(dep->name, args->name, dep->namelen);
Dave Chinner1c55cec2013-08-12 20:50:10 +10002010 xfs_dir3_dirent_put_ftype(mp, dep, args->filetype);
Dave Chinner0cb97762013-08-12 20:50:09 +10002011 tagp = xfs_dir3_data_entry_tag_p(mp, dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002012 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 xfs_dir2_data_log_entry(tp, dbp, dep);
2014 /*
2015 * Rescan the block for bestfree if needed.
2016 */
2017 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002018 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
2020 * Log the data block header if needed.
2021 */
2022 if (needlog)
2023 xfs_dir2_data_log_header(tp, dbp);
2024 /*
2025 * If the freespace entry is now wrong, update it.
2026 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002027 bests = xfs_dir3_free_bests_p(mp, free); /* gcc is so stupid */
Dave Chinner33363fe2013-04-03 16:11:22 +11002028 if (be16_to_cpu(bests[findex]) != be16_to_cpu(bf[0].length)) {
2029 bests[findex] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 logfree = 1;
2031 }
2032 /*
2033 * Log the freespace entry if needed.
2034 */
2035 if (logfree)
2036 xfs_dir2_free_log_bests(tp, fbp, findex, findex);
2037 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 * Return the data block and offset in args, then drop the data block.
2039 */
2040 args->blkno = (xfs_dablk_t)dbno;
Nathan Scott3d693c62006-03-17 17:28:27 +11002041 args->index = be16_to_cpu(*tagp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043}
2044
2045/*
2046 * Lookup an entry in a node-format directory.
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002047 * All the real work happens in xfs_da3_node_lookup_int.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * The only real output is the inode number of the entry.
2049 */
2050int /* error */
2051xfs_dir2_node_lookup(
2052 xfs_da_args_t *args) /* operation arguments */
2053{
2054 int error; /* error return value */
2055 int i; /* btree level */
2056 int rval; /* operation return value */
2057 xfs_da_state_t *state; /* btree cursor */
2058
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002059 trace_xfs_dir2_node_lookup(args);
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
2062 * Allocate and initialize the btree cursor.
2063 */
2064 state = xfs_da_state_alloc();
2065 state->args = args;
2066 state->mp = args->dp->i_mount;
2067 state->blocksize = state->mp->m_dirblksize;
2068 state->node_ents = state->mp->m_dir_node_ents;
2069 /*
2070 * Fill in the path to the entry in the cursor.
2071 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002072 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (error)
2074 rval = error;
Barry Naujok384f3ce2008-05-21 16:58:22 +10002075 else if (rval == ENOENT && args->cmpresult == XFS_CMP_CASE) {
2076 /* If a CI match, dup the actual name and return EEXIST */
2077 xfs_dir2_data_entry_t *dep;
2078
Dave Chinner1d9025e2012-06-22 18:50:14 +10002079 dep = (xfs_dir2_data_entry_t *)
2080 ((char *)state->extrablk.bp->b_addr +
2081 state->extrablk.index);
Barry Naujok384f3ce2008-05-21 16:58:22 +10002082 rval = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
2083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Release the btree blocks and leaf block.
2086 */
2087 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002088 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 state->path.blk[i].bp = NULL;
2090 }
2091 /*
2092 * Release the data block if we have it.
2093 */
2094 if (state->extravalid && state->extrablk.bp) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002095 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 state->extrablk.bp = NULL;
2097 }
2098 xfs_da_state_free(state);
2099 return rval;
2100}
2101
2102/*
2103 * Remove an entry from a node-format directory.
2104 */
2105int /* error */
2106xfs_dir2_node_removename(
2107 xfs_da_args_t *args) /* operation arguments */
2108{
2109 xfs_da_state_blk_t *blk; /* leaf block */
2110 int error; /* error return value */
2111 int rval; /* operation return value */
2112 xfs_da_state_t *state; /* btree cursor */
2113
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002114 trace_xfs_dir2_node_removename(args);
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 /*
2117 * Allocate and initialize the btree cursor.
2118 */
2119 state = xfs_da_state_alloc();
2120 state->args = args;
2121 state->mp = args->dp->i_mount;
2122 state->blocksize = state->mp->m_dirblksize;
2123 state->node_ents = state->mp->m_dir_node_ents;
2124 /*
2125 * Look up the entry we're deleting, set up the cursor.
2126 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002127 error = xfs_da3_node_lookup_int(state, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002128 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 rval = error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /*
2131 * Didn't find it, upper layer screwed up.
2132 */
2133 if (rval != EEXIST) {
2134 xfs_da_state_free(state);
2135 return rval;
2136 }
2137 blk = &state->path.blk[state->path.active - 1];
2138 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
2139 ASSERT(state->extravalid);
2140 /*
2141 * Remove the leaf and data entries.
2142 * Extrablk refers to the data block.
2143 */
2144 error = xfs_dir2_leafn_remove(args, blk->bp, blk->index,
2145 &state->extrablk, &rval);
Barry Naujok5163f952008-05-21 16:41:01 +10002146 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 /*
2149 * Fix the hash values up the btree.
2150 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002151 xfs_da3_fixhashpath(state, &state->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
2153 * If we need to join leaf blocks, do it.
2154 */
2155 if (rval && state->path.active > 1)
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002156 error = xfs_da3_join(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /*
2158 * If no errors so far, try conversion to leaf format.
2159 */
2160 if (!error)
2161 error = xfs_dir2_node_to_leaf(state);
2162 xfs_da_state_free(state);
2163 return error;
2164}
2165
2166/*
2167 * Replace an entry's inode number in a node-format directory.
2168 */
2169int /* error */
2170xfs_dir2_node_replace(
2171 xfs_da_args_t *args) /* operation arguments */
2172{
2173 xfs_da_state_blk_t *blk; /* leaf block */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002174 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 xfs_dir2_data_entry_t *dep; /* data entry changed */
2176 int error; /* error return value */
2177 int i; /* btree level */
2178 xfs_ino_t inum; /* new inode number */
2179 xfs_dir2_leaf_t *leaf; /* leaf structure */
2180 xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */
2181 int rval; /* internal return value */
2182 xfs_da_state_t *state; /* btree cursor */
2183
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002184 trace_xfs_dir2_node_replace(args);
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /*
2187 * Allocate and initialize the btree cursor.
2188 */
2189 state = xfs_da_state_alloc();
2190 state->args = args;
2191 state->mp = args->dp->i_mount;
2192 state->blocksize = state->mp->m_dirblksize;
2193 state->node_ents = state->mp->m_dir_node_ents;
2194 inum = args->inumber;
2195 /*
2196 * Lookup the entry to change in the btree.
2197 */
Dave Chinnerf5ea1102013-04-24 18:58:02 +10002198 error = xfs_da3_node_lookup_int(state, &rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (error) {
2200 rval = error;
2201 }
2202 /*
2203 * It should be found, since the vnodeops layer has looked it up
2204 * and locked it. But paranoia is good.
2205 */
2206 if (rval == EEXIST) {
Dave Chinner24df33b2013-04-12 07:30:21 +10002207 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Find the leaf entry.
2210 */
2211 blk = &state->path.blk[state->path.active - 1];
2212 ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002213 leaf = blk->bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10002214 ents = xfs_dir3_leaf_ents_p(leaf);
2215 lep = &ents[blk->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 ASSERT(state->extravalid);
2217 /*
2218 * Point to the data entry.
2219 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002220 hdr = state->extrablk.bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002221 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2222 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002224 ((char *)hdr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002225 xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002226 ASSERT(inum != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * Fill in the new inode number and log the entry.
2229 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10002230 dep->inumber = cpu_to_be64(inum);
Dave Chinner1c55cec2013-08-12 20:50:10 +10002231 xfs_dir3_dirent_put_ftype(state->mp, dep, args->filetype);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep);
2233 rval = 0;
2234 }
2235 /*
2236 * Didn't find it, and we're holding a data block. Drop it.
2237 */
2238 else if (state->extravalid) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002239 xfs_trans_brelse(args->trans, state->extrablk.bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 state->extrablk.bp = NULL;
2241 }
2242 /*
2243 * Release all the buffers in the cursor.
2244 */
2245 for (i = 0; i < state->path.active; i++) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002246 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 state->path.blk[i].bp = NULL;
2248 }
2249 xfs_da_state_free(state);
2250 return rval;
2251}
2252
2253/*
2254 * Trim off a trailing empty freespace block.
2255 * Return (in rvalp) 1 if we did it, 0 if not.
2256 */
2257int /* error */
2258xfs_dir2_node_trim_free(
2259 xfs_da_args_t *args, /* operation arguments */
2260 xfs_fileoff_t fo, /* free block number */
2261 int *rvalp) /* out: did something */
2262{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002263 struct xfs_buf *bp; /* freespace buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 xfs_inode_t *dp; /* incore directory inode */
2265 int error; /* error return code */
2266 xfs_dir2_free_t *free; /* freespace structure */
2267 xfs_mount_t *mp; /* filesystem mount point */
2268 xfs_trans_t *tp; /* transaction pointer */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002269 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 dp = args->dp;
2272 mp = dp->i_mount;
2273 tp = args->trans;
2274 /*
2275 * Read the freespace block.
2276 */
Dave Chinner20252072012-11-12 22:54:13 +11002277 error = xfs_dir2_free_try_read(tp, dp, fo, &bp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002278 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /*
2281 * There can be holes in freespace. If fo is a hole, there's
2282 * nothing to do.
2283 */
Dave Chinner20252072012-11-12 22:54:13 +11002284 if (!bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return 0;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002286 free = bp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002287 xfs_dir3_free_hdr_from_disk(&freehdr, free);
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 /*
2290 * If there are used entries, there's nothing to do.
2291 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002292 if (freehdr.nused > 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002293 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *rvalp = 0;
2295 return 0;
2296 }
2297 /*
2298 * Blow the block away.
2299 */
2300 if ((error =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002301 xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 bp))) {
2303 /*
2304 * Can't fail with ENOSPC since that only happens with no
2305 * space reservation, when breaking up an extent into two
2306 * pieces. This is the last block of an extent.
2307 */
2308 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002309 xfs_trans_brelse(tp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return error;
2311 }
2312 /*
2313 * Return that we succeeded.
2314 */
2315 *rvalp = 1;
2316 return 0;
2317}