blob: 7352e41d2aaaf77f8b47fe92c1b1809063651ad4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
Dave Chinner24df33b2013-04-12 07:30:21 +10003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_dinode.h"
31#include "xfs_inode.h"
32#include "xfs_bmap.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020033#include "xfs_dir2_format.h"
34#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000036#include "xfs_trace.h"
Dave Chinner24df33b2013-04-12 07:30:21 +100037#include "xfs_buf_item.h"
38#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * Local function declarations.
42 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100043static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
44 int *indexp, struct xfs_buf **dbpp);
Dave Chinner24df33b2013-04-12 07:30:21 +100045static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100046 int first, int last);
Dave Chinner24df33b2013-04-12 07:30:21 +100047static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100048
Dave Chinner24df33b2013-04-12 07:30:21 +100049/*
50 * Check the internal consistency of a leaf1 block.
51 * Pop an assert if something is wrong.
52 */
53#ifdef DEBUG
54#define xfs_dir3_leaf_check(mp, bp) \
55do { \
56 if (!xfs_dir3_leaf1_check((mp), (bp))) \
57 ASSERT(0); \
58} while (0);
59
60STATIC bool
61xfs_dir3_leaf1_check(
62 struct xfs_mount *mp,
63 struct xfs_buf *bp)
64{
65 struct xfs_dir2_leaf *leaf = bp->b_addr;
66 struct xfs_dir3_icleaf_hdr leafhdr;
67
68 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
69
70 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
71 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
72 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
73 return false;
74 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
75 return false;
76
77 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
78}
79#else
80#define xfs_dir3_leaf_check(mp, bp)
81#endif
82
83void
84xfs_dir3_leaf_hdr_from_disk(
85 struct xfs_dir3_icleaf_hdr *to,
86 struct xfs_dir2_leaf *from)
87{
88 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
89 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
90 to->forw = be32_to_cpu(from->hdr.info.forw);
91 to->back = be32_to_cpu(from->hdr.info.back);
92 to->magic = be16_to_cpu(from->hdr.info.magic);
93 to->count = be16_to_cpu(from->hdr.count);
94 to->stale = be16_to_cpu(from->hdr.stale);
95 } else {
96 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
97
98 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
99 to->back = be32_to_cpu(hdr3->info.hdr.back);
100 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
101 to->count = be16_to_cpu(hdr3->count);
102 to->stale = be16_to_cpu(hdr3->stale);
103 }
104
105 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
106 to->magic == XFS_DIR3_LEAF1_MAGIC ||
107 to->magic == XFS_DIR2_LEAFN_MAGIC ||
108 to->magic == XFS_DIR3_LEAFN_MAGIC);
109}
110
111void
112xfs_dir3_leaf_hdr_to_disk(
113 struct xfs_dir2_leaf *to,
114 struct xfs_dir3_icleaf_hdr *from)
115{
116 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
117 from->magic == XFS_DIR3_LEAF1_MAGIC ||
118 from->magic == XFS_DIR2_LEAFN_MAGIC ||
119 from->magic == XFS_DIR3_LEAFN_MAGIC);
120
121 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
122 from->magic == XFS_DIR2_LEAFN_MAGIC) {
123 to->hdr.info.forw = cpu_to_be32(from->forw);
124 to->hdr.info.back = cpu_to_be32(from->back);
125 to->hdr.info.magic = cpu_to_be16(from->magic);
126 to->hdr.count = cpu_to_be16(from->count);
127 to->hdr.stale = cpu_to_be16(from->stale);
128 } else {
129 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
130
131 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
132 hdr3->info.hdr.back = cpu_to_be32(from->back);
133 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
134 hdr3->count = cpu_to_be16(from->count);
135 hdr3->stale = cpu_to_be16(from->stale);
136 }
137}
138
139bool
140xfs_dir3_leaf_check_int(
141 struct xfs_mount *mp,
142 struct xfs_dir3_icleaf_hdr *hdr,
143 struct xfs_dir2_leaf *leaf)
144{
145 struct xfs_dir2_leaf_entry *ents;
146 xfs_dir2_leaf_tail_t *ltp;
147 int stale;
148 int i;
149
150 ents = xfs_dir3_leaf_ents_p(leaf);
151 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
152
153 /*
154 * XXX (dgc): This value is not restrictive enough.
155 * Should factor in the size of the bests table as well.
156 * We can deduce a value for that from di_size.
157 */
158 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
159 return false;
160
161 /* Leaves and bests don't overlap in leaf format. */
162 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
163 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
164 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
165 return false;
166
167 /* Check hash value order, count stale entries. */
168 for (i = stale = 0; i < hdr->count; i++) {
169 if (i + 1 < hdr->count) {
170 if (be32_to_cpu(ents[i].hashval) >
171 be32_to_cpu(ents[i + 1].hashval))
172 return false;
173 }
174 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
175 stale++;
176 }
177 if (hdr->stale != stale)
178 return false;
179 return true;
180}
181
182static bool
183xfs_dir3_leaf_verify(
Dave Chinnere6f76672012-11-12 22:54:15 +1100184 struct xfs_buf *bp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000185 __uint16_t magic)
Dave Chinnere6f76672012-11-12 22:54:15 +1100186{
187 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner24df33b2013-04-12 07:30:21 +1000188 struct xfs_dir2_leaf *leaf = bp->b_addr;
189 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnere6f76672012-11-12 22:54:15 +1100190
Dave Chinner24df33b2013-04-12 07:30:21 +1000191 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
192
193 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
194 if (xfs_sb_version_hascrc(&mp->m_sb)) {
195 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
196
197 if ((magic == XFS_DIR2_LEAF1_MAGIC &&
198 leafhdr.magic != XFS_DIR3_LEAF1_MAGIC) ||
199 (magic == XFS_DIR2_LEAFN_MAGIC &&
200 leafhdr.magic != XFS_DIR3_LEAFN_MAGIC))
201 return false;
202
203 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
204 return false;
205 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
206 return false;
207 } else {
208 if (leafhdr.magic != magic)
209 return false;
210 }
211 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
212}
213
214static void
215__read_verify(
216 struct xfs_buf *bp,
217 __uint16_t magic)
218{
219 struct xfs_mount *mp = bp->b_target->bt_mount;
220
221 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
222 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
223 XFS_DIR3_LEAF_CRC_OFF)) ||
224 !xfs_dir3_leaf_verify(bp, magic)) {
225 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinnere6f76672012-11-12 22:54:15 +1100226 xfs_buf_ioerror(bp, EFSCORRUPTED);
227 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100228}
Dave Chinnere6f76672012-11-12 22:54:15 +1100229
Dave Chinner612cfbf2012-11-14 17:52:32 +1100230static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000231__write_verify(
232 struct xfs_buf *bp,
233 __uint16_t magic)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100234{
Dave Chinner24df33b2013-04-12 07:30:21 +1000235 struct xfs_mount *mp = bp->b_target->bt_mount;
236 struct xfs_buf_log_item *bip = bp->b_fspriv;
237 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
238
239 if (!xfs_dir3_leaf_verify(bp, magic)) {
240 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
241 xfs_buf_ioerror(bp, EFSCORRUPTED);
242 return;
243 }
244
245 if (!xfs_sb_version_hascrc(&mp->m_sb))
246 return;
247
248 if (bip)
249 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
250
251 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100252}
253
254static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000255xfs_dir3_leaf1_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100256 struct xfs_buf *bp)
257{
Dave Chinner24df33b2013-04-12 07:30:21 +1000258 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinner1813dd62012-11-14 17:54:40 +1100259}
260
Dave Chinner24df33b2013-04-12 07:30:21 +1000261static void
262xfs_dir3_leaf1_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100263 struct xfs_buf *bp)
264{
Dave Chinner24df33b2013-04-12 07:30:21 +1000265 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100266}
267
Dave Chinner24df33b2013-04-12 07:30:21 +1000268static void
269xfs_dir3_leafn_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100270 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +1100271{
Dave Chinner24df33b2013-04-12 07:30:21 +1000272 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100273}
274
Dave Chinner24df33b2013-04-12 07:30:21 +1000275static void
276xfs_dir3_leafn_write_verify(
277 struct xfs_buf *bp)
278{
279 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
280}
281
282static const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
283 .verify_read = xfs_dir3_leaf1_read_verify,
284 .verify_write = xfs_dir3_leaf1_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100285};
286
Dave Chinner24df33b2013-04-12 07:30:21 +1000287const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
288 .verify_read = xfs_dir3_leafn_read_verify,
289 .verify_write = xfs_dir3_leafn_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100290};
Dave Chinnere6f76672012-11-12 22:54:15 +1100291
292static int
Dave Chinner24df33b2013-04-12 07:30:21 +1000293xfs_dir3_leaf_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100294 struct xfs_trans *tp,
295 struct xfs_inode *dp,
296 xfs_dablk_t fbno,
297 xfs_daddr_t mappedbno,
298 struct xfs_buf **bpp)
299{
300 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000301 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
Dave Chinnere6f76672012-11-12 22:54:15 +1100302}
303
304int
Dave Chinner24df33b2013-04-12 07:30:21 +1000305xfs_dir3_leafn_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100306 struct xfs_trans *tp,
307 struct xfs_inode *dp,
308 xfs_dablk_t fbno,
309 xfs_daddr_t mappedbno,
310 struct xfs_buf **bpp)
311{
312 return xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000313 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
314}
315
316/*
317 * Initialize a new leaf block, leaf1 or leafn magic accepted.
318 */
319static void
320xfs_dir3_leaf_init(
321 struct xfs_mount *mp,
322 struct xfs_buf *bp,
323 xfs_ino_t owner,
324 __uint16_t type)
325{
326 struct xfs_dir2_leaf *leaf = bp->b_addr;
327
328 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
329
330 if (xfs_sb_version_hascrc(&mp->m_sb)) {
331 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
332
333 memset(leaf3, 0, sizeof(*leaf3));
334
335 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
336 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
337 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
338 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
339 leaf3->info.owner = cpu_to_be64(owner);
340 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
341 } else {
342 memset(leaf, 0, sizeof(*leaf));
343 leaf->hdr.info.magic = cpu_to_be16(type);
344 }
345
346 /*
347 * If it's a leaf-format directory initialize the tail.
348 * Caller is responsible for initialising the bests table.
349 */
350 if (type == XFS_DIR2_LEAF1_MAGIC) {
351 struct xfs_dir2_leaf_tail *ltp;
352
353 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
354 ltp->bestcount = 0;
355 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
356 } else
357 bp->b_ops = &xfs_dir3_leafn_buf_ops;
358}
359
360int
361xfs_dir3_leaf_get_buf(
362 xfs_da_args_t *args,
363 xfs_dir2_db_t bno,
364 struct xfs_buf **bpp,
365 __uint16_t magic)
366{
367 struct xfs_inode *dp = args->dp;
368 struct xfs_trans *tp = args->trans;
369 struct xfs_mount *mp = dp->i_mount;
370 struct xfs_buf *bp;
371 int error;
372
373 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
374 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
375 bno < XFS_DIR2_FREE_FIRSTDB(mp));
376
377 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
378 XFS_DATA_FORK);
379 if (error)
380 return error;
381
382 xfs_dir3_leaf_init(mp, bp, dp->i_ino, magic);
383 xfs_dir3_leaf_log_header(tp, bp);
384 if (magic == XFS_DIR2_LEAF1_MAGIC)
385 xfs_dir3_leaf_log_tail(tp, bp);
386 *bpp = bp;
387 return 0;
Dave Chinnere6f76672012-11-12 22:54:15 +1100388}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390/*
391 * Convert a block form directory to a leaf form directory.
392 */
393int /* error */
394xfs_dir2_block_to_leaf(
395 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000396 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Nathan Scott68b3a102006-03-17 17:27:19 +1100398 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200400 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
402 xfs_dir2_block_tail_t *btp; /* block's tail */
403 xfs_inode_t *dp; /* incore directory inode */
404 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000405 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 xfs_dir2_db_t ldb; /* leaf block's bno */
407 xfs_dir2_leaf_t *leaf; /* leaf structure */
408 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
409 xfs_mount_t *mp; /* filesystem mount point */
410 int needlog; /* need to log block header */
411 int needscan; /* need to rescan bestfree */
412 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +1100413 struct xfs_dir2_data_free *bf;
Dave Chinner24df33b2013-04-12 07:30:21 +1000414 struct xfs_dir2_leaf_entry *ents;
415 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000417 trace_xfs_dir2_block_to_leaf(args);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dp = args->dp;
420 mp = dp->i_mount;
421 tp = args->trans;
422 /*
423 * Add the leaf block to the inode.
424 * This interface will only put blocks in the leaf/node range.
425 * Since that's empty now, we'll get the root (block 0 in range).
426 */
427 if ((error = xfs_da_grow_inode(args, &blkno))) {
428 return error;
429 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000430 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
432 /*
433 * Initialize the leaf block, get a buffer for it.
434 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000435 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
436 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +1000438
Dave Chinner1d9025e2012-06-22 18:50:14 +1000439 leaf = lbp->b_addr;
440 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100441 xfs_dir3_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200442 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000443 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100444 bf = xfs_dir3_data_bestfree_p(hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000445 ents = xfs_dir3_leaf_ents_p(leaf);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /*
448 * Set the counts in the leaf header.
449 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
451 leafhdr.count = be32_to_cpu(btp->count);
452 leafhdr.stale = be32_to_cpu(btp->stale);
453 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
454 xfs_dir3_leaf_log_header(tp, lbp);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /*
457 * Could compact these but I think we always do the conversion
458 * after squeezing out stale entries.
459 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000460 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
461 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 needscan = 0;
463 needlog = 1;
464 /*
465 * Make the space formerly occupied by the leaf entries and block
466 * tail be free.
467 */
468 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200469 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
470 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 (char *)blp),
472 &needlog, &needscan);
473 /*
474 * Fix up the block header, make it a data block.
475 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100476 dbp->b_ops = &xfs_dir3_data_buf_ops;
477 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
478 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
479 else
480 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200483 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Set up leaf tail and bests table.
486 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000487 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100488 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000489 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100490 bestsp[0] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /*
492 * Log the data header and leaf bests table.
493 */
494 if (needlog)
495 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000496 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100497 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000498 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500}
501
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200502STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +1000503xfs_dir3_leaf_find_stale(
504 struct xfs_dir3_icleaf_hdr *leafhdr,
505 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200506 int index,
507 int *lowstale,
508 int *highstale)
509{
510 /*
511 * Find the first stale entry before our index, if any.
512 */
513 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000514 if (ents[*lowstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200515 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
516 break;
517 }
518
519 /*
520 * Find the first stale entry at or after our index, if any.
521 * Stop if the result would require moving more entries than using
522 * lowstale.
523 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000524 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
525 if (ents[*highstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200526 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
527 break;
528 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
529 break;
530 }
531}
532
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200533struct xfs_dir2_leaf_entry *
Dave Chinner24df33b2013-04-12 07:30:21 +1000534xfs_dir3_leaf_find_entry(
535 struct xfs_dir3_icleaf_hdr *leafhdr,
536 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200537 int index, /* leaf table position */
538 int compact, /* need to compact leaves */
539 int lowstale, /* index of prev stale leaf */
540 int highstale, /* index of next stale leaf */
541 int *lfloglow, /* low leaf logging index */
542 int *lfloghigh) /* high leaf logging index */
543{
Dave Chinner24df33b2013-04-12 07:30:21 +1000544 if (!leafhdr->stale) {
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200545 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
546
547 /*
548 * Now we need to make room to insert the leaf entry.
549 *
550 * If there are no stale entries, just insert a hole at index.
551 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000552 lep = &ents[index];
553 if (index < leafhdr->count)
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200554 memmove(lep + 1, lep,
Dave Chinner24df33b2013-04-12 07:30:21 +1000555 (leafhdr->count - index) * sizeof(*lep));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200556
557 /*
558 * Record low and high logging indices for the leaf.
559 */
560 *lfloglow = index;
Dave Chinner24df33b2013-04-12 07:30:21 +1000561 *lfloghigh = leafhdr->count++;
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200562 return lep;
563 }
564
565 /*
566 * There are stale entries.
567 *
568 * We will use one of them for the new entry. It's probably not at
569 * the right location, so we'll have to shift some up or down first.
570 *
571 * If we didn't compact before, we need to find the nearest stale
572 * entries before and after our insertion point.
573 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200574 if (compact == 0)
Dave Chinner24df33b2013-04-12 07:30:21 +1000575 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
576 &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200577
578 /*
579 * If the low one is better, use it.
580 */
581 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000582 (highstale == leafhdr->count ||
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200583 index - lowstale - 1 < highstale - index)) {
584 ASSERT(index - lowstale - 1 >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000585 ASSERT(ents[lowstale].address ==
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200586 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200587
588 /*
589 * Copy entries up to cover the stale entry and make room
590 * for the new entry.
591 */
592 if (index - lowstale - 1 > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000593 memmove(&ents[lowstale], &ents[lowstale + 1],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200594 (index - lowstale - 1) *
Dave Chinner24df33b2013-04-12 07:30:21 +1000595 sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200596 }
597 *lfloglow = MIN(lowstale, *lfloglow);
598 *lfloghigh = MAX(index - 1, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000599 leafhdr->stale--;
600 return &ents[index - 1];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200601 }
602
603 /*
604 * The high one is better, so use that one.
605 */
606 ASSERT(highstale - index >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000607 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200608
609 /*
610 * Copy entries down to cover the stale entry and make room for the
611 * new entry.
612 */
613 if (highstale - index > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000614 memmove(&ents[index + 1], &ents[index],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200615 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
616 }
617 *lfloglow = MIN(index, *lfloglow);
618 *lfloghigh = MAX(highstale, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000619 leafhdr->stale--;
620 return &ents[index];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Add an entry to a leaf form directory.
625 */
626int /* error */
627xfs_dir2_leaf_addname(
628 xfs_da_args_t *args) /* operation arguments */
629{
Nathan Scott68b3a102006-03-17 17:27:19 +1100630 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200632 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000633 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 xfs_dir2_data_entry_t *dep; /* data block entry */
635 xfs_inode_t *dp; /* incore directory inode */
636 xfs_dir2_data_unused_t *dup; /* data unused entry */
637 int error; /* error return value */
638 int grown; /* allocated new data block */
639 int highstale; /* index of next stale leaf */
640 int i; /* temporary, index */
641 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000642 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 xfs_dir2_leaf_t *leaf; /* leaf structure */
644 int length; /* length of new entry */
645 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
646 int lfloglow; /* low leaf logging index */
647 int lfloghigh; /* high leaf logging index */
648 int lowstale; /* index of prev stale leaf */
649 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
650 xfs_mount_t *mp; /* filesystem mount point */
651 int needbytes; /* leaf block bytes needed */
652 int needlog; /* need to log data header */
653 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100654 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 xfs_trans_t *tp; /* transaction pointer */
656 xfs_dir2_db_t use_block; /* data block number */
Dave Chinner33363fe2013-04-03 16:11:22 +1100657 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +1000658 struct xfs_dir2_leaf_entry *ents;
659 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000661 trace_xfs_dir2_leaf_addname(args);
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 dp = args->dp;
664 tp = args->trans;
665 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100666
Dave Chinner24df33b2013-04-12 07:30:21 +1000667 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100668 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * Look up the entry by hash value and name.
673 * We know it's not there, our caller has already done a lookup.
674 * So the index is of the entry to insert in front of.
675 * But if there are dup hash values the index is of the first of those.
676 */
677 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000678 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000679 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000680 ents = xfs_dir3_leaf_ents_p(leaf);
681 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000682 bestsp = xfs_dir2_leaf_bests_p(ltp);
683 length = xfs_dir2_data_entsize(args->namelen);
Dave Chinner24df33b2013-04-12 07:30:21 +1000684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * See if there are any entries with the same hash value
687 * and space in their block for the new entry.
688 * This is good because it puts multiple same-hash value entries
689 * in a data block, improving the lookup of those entries.
690 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000691 for (use_block = -1, lep = &ents[index];
692 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100694 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000696 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100697 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200698 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100699 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 use_block = i;
701 break;
702 }
703 }
704 /*
705 * Didn't find a block yet, linear search all the data blocks.
706 */
707 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100708 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /*
710 * Remember a block we see that's missing.
711 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200712 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
713 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100715 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 use_block = i;
717 break;
718 }
719 }
720 }
721 /*
722 * How many bytes do we need in the leaf block?
723 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200724 needbytes = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000725 if (!leafhdr.stale)
Christoph Hellwig22823962011-07-08 14:35:53 +0200726 needbytes += sizeof(xfs_dir2_leaf_entry_t);
727 if (use_block == -1)
728 needbytes += sizeof(xfs_dir2_data_off_t);
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Now kill use_block if it refers to a missing block, so we
732 * can use it as an indication of allocation needed.
733 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200734 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 use_block = -1;
736 /*
737 * If we don't have enough free bytes but we can make enough
738 * by compacting out stale entries, we'll do that.
739 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000740 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
741 leafhdr.stale > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 compact = 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
745 * Otherwise if we don't have enough free bytes we need to
746 * convert to node form.
747 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000748 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
750 * Just checking or no space reservation, give up.
751 */
Barry Naujok6a178102008-05-21 16:42:05 +1000752 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
753 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000754 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return XFS_ERROR(ENOSPC);
756 }
757 /*
758 * Convert to node form.
759 */
760 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (error)
762 return error;
763 /*
764 * Then add the new entry.
765 */
766 return xfs_dir2_node_addname(args);
767 }
768 /*
769 * Otherwise it will fit without compaction.
770 */
771 else
772 compact = 0;
773 /*
774 * If just checking, then it will fit unless we needed to allocate
775 * a new data block.
776 */
Barry Naujok6a178102008-05-21 16:42:05 +1000777 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000778 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
780 }
781 /*
782 * If no allocations are allowed, return now before we've
783 * changed anything.
784 */
785 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000786 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return XFS_ERROR(ENOSPC);
788 }
789 /*
790 * Need to compact the leaf entries, removing stale ones.
791 * Leave one stale entry behind - the one closest to our
792 * insertion index - and we'll shift that one to our insertion
793 * point later.
794 */
795 if (compact) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000796 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
797 &highstale, &lfloglow, &lfloghigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 /*
800 * There are stale entries, so we'll need log-low and log-high
801 * impossibly bad values later.
802 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000803 else if (leafhdr.stale) {
804 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 lfloghigh = -1;
806 }
807 /*
808 * If there was no data block space found, we need to allocate
809 * a new one.
810 */
811 if (use_block == -1) {
812 /*
813 * Add the new data block.
814 */
815 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
816 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000817 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return error;
819 }
820 /*
821 * Initialize the block.
822 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100823 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000824 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return error;
826 }
827 /*
828 * If we're adding a new data block on the end we need to
829 * extend the bests table. Copy it up one entry.
830 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100831 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 bestsp--;
833 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100834 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800835 be32_add_cpu(&ltp->bestcount, 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000836 xfs_dir3_leaf_log_tail(tp, lbp);
837 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 /*
840 * If we're filling in a previously empty block just log it.
841 */
842 else
Dave Chinner24df33b2013-04-12 07:30:21 +1000843 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000844 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100845 bf = xfs_dir3_data_bestfree_p(hdr);
846 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100848 } else {
849 /*
850 * Already had space in some data block.
851 * Just read that one in.
852 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100853 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +1100854 xfs_dir2_db_to_da(mp, use_block),
855 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100856 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000857 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return error;
859 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000860 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100861 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 grown = 0;
863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /*
865 * Point to the biggest freespace in our data block.
866 */
867 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +1100868 ((char *)hdr + be16_to_cpu(bf[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100869 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 needscan = needlog = 0;
871 /*
872 * Mark the initial part of our freespace in use for the new entry.
873 */
874 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200875 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 &needlog, &needscan);
877 /*
878 * Initialize our new entry (at last).
879 */
880 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000881 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 dep->namelen = args->namelen;
883 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000884 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200885 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * Need to scan fix up the bestfree table.
888 */
889 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200890 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Need to log the data block's header.
893 */
894 if (needlog)
895 xfs_dir2_data_log_header(tp, dbp);
896 xfs_dir2_data_log_entry(tp, dbp, dep);
897 /*
898 * If the bests table needs to be changed, do it.
899 * Log the change unless we've already done that.
900 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100901 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
902 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (!grown)
Dave Chinner24df33b2013-04-12 07:30:21 +1000904 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200906
Dave Chinner24df33b2013-04-12 07:30:21 +1000907 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200908 highstale, &lfloglow, &lfloghigh);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /*
911 * Fill in the new leaf entry.
912 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100913 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000914 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100915 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * Log the leaf fields and give up the buffers.
918 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000919 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
920 xfs_dir3_leaf_log_header(tp, lbp);
921 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
922 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100923 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return 0;
925}
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * Compact out any stale entries in the leaf.
929 * Log the header and changed leaf entries, if any.
930 */
931void
Dave Chinner24df33b2013-04-12 07:30:21 +1000932xfs_dir3_leaf_compact(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xfs_da_args_t *args, /* operation arguments */
Dave Chinner24df33b2013-04-12 07:30:21 +1000934 struct xfs_dir3_icleaf_hdr *leafhdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000935 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 int from; /* source leaf index */
938 xfs_dir2_leaf_t *leaf; /* leaf structure */
939 int loglow; /* first leaf entry to log */
940 int to; /* target leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +1000941 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Chinner1d9025e2012-06-22 18:50:14 +1000943 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000944 if (!leafhdr->stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Compress out the stale entries in place.
949 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000950 ents = xfs_dir3_leaf_ents_p(leaf);
951 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
952 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 continue;
954 /*
955 * Only actually copy the entries that are different.
956 */
957 if (from > to) {
958 if (loglow == -1)
959 loglow = to;
Dave Chinner24df33b2013-04-12 07:30:21 +1000960 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 to++;
963 }
964 /*
965 * Update and log the header, log the leaf entries.
966 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000967 ASSERT(leafhdr->stale == from - to);
968 leafhdr->count -= leafhdr->stale;
969 leafhdr->stale = 0;
970
971 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
972 xfs_dir3_leaf_log_header(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (loglow != -1)
Dave Chinner24df33b2013-04-12 07:30:21 +1000974 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
978 * Compact the leaf entries, removing stale ones.
979 * Leave one stale entry behind - the one closest to our
980 * insertion index - and the caller will shift that one to our insertion
981 * point later.
982 * Return new insertion index, where the remaining stale entry is,
983 * and leaf logging indices.
984 */
985void
Dave Chinner24df33b2013-04-12 07:30:21 +1000986xfs_dir3_leaf_compact_x1(
987 struct xfs_dir3_icleaf_hdr *leafhdr,
988 struct xfs_dir2_leaf_entry *ents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int *indexp, /* insertion index */
990 int *lowstalep, /* out: stale entry before us */
991 int *highstalep, /* out: stale entry after us */
992 int *lowlogp, /* out: low log index */
993 int *highlogp) /* out: high log index */
994{
995 int from; /* source copy index */
996 int highstale; /* stale entry at/after index */
997 int index; /* insertion index */
998 int keepstale; /* source index of kept stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int lowstale; /* stale entry before index */
1000 int newindex=0; /* new insertion index */
1001 int to; /* destination copy index */
1002
Dave Chinner24df33b2013-04-12 07:30:21 +10001003 ASSERT(leafhdr->stale > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001005
Dave Chinner24df33b2013-04-12 07:30:21 +10001006 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /*
1009 * Pick the better of lowstale and highstale.
1010 */
1011 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001012 (highstale == leafhdr->count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 index - lowstale <= highstale - index))
1014 keepstale = lowstale;
1015 else
1016 keepstale = highstale;
1017 /*
1018 * Copy the entries in place, removing all the stale entries
1019 * except keepstale.
1020 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001021 for (from = to = 0; from < leafhdr->count; from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * Notice the new value of index.
1024 */
1025 if (index == from)
1026 newindex = to;
1027 if (from != keepstale &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (from == to)
1030 *lowlogp = to;
1031 continue;
1032 }
1033 /*
1034 * Record the new keepstale value for the insertion.
1035 */
1036 if (from == keepstale)
1037 lowstale = highstale = to;
1038 /*
1039 * Copy only the entries that have moved.
1040 */
1041 if (from > to)
Dave Chinner24df33b2013-04-12 07:30:21 +10001042 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 to++;
1044 }
1045 ASSERT(from > to);
1046 /*
1047 * If the insertion point was past the last entry,
1048 * set the new insertion point accordingly.
1049 */
1050 if (index == from)
1051 newindex = to;
1052 *indexp = newindex;
1053 /*
1054 * Adjust the leaf header values.
1055 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001056 leafhdr->count -= from - to;
1057 leafhdr->stale = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * Remember the low/high stale value only in the "right"
1060 * direction.
1061 */
1062 if (lowstale >= newindex)
1063 lowstale = -1;
1064 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001065 highstale = leafhdr->count;
1066 *highlogp = leafhdr->count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *lowstalep = lowstale;
1068 *highstalep = highstale;
1069}
1070
Dave Chinner9b73bd72012-06-22 18:50:15 +10001071struct xfs_dir2_leaf_map_info {
1072 xfs_extlen_t map_blocks; /* number of fsbs in map */
1073 xfs_dablk_t map_off; /* last mapped file offset */
1074 int map_size; /* total entries in *map */
1075 int map_valid; /* valid entries in *map */
1076 int nmap; /* mappings to ask xfs_bmapi */
1077 xfs_dir2_db_t curdb; /* db for current block */
1078 int ra_current; /* number of read-ahead blks */
1079 int ra_index; /* *map index for read-ahead */
1080 int ra_offset; /* map entry offset for ra */
1081 int ra_want; /* readahead count wanted */
1082 struct xfs_bmbt_irec map[]; /* map vector for blocks */
1083};
1084
1085STATIC int
1086xfs_dir2_leaf_readbuf(
1087 struct xfs_inode *dp,
1088 size_t bufsize,
1089 struct xfs_dir2_leaf_map_info *mip,
1090 xfs_dir2_off_t *curoff,
1091 struct xfs_buf **bpp)
1092{
1093 struct xfs_mount *mp = dp->i_mount;
1094 struct xfs_buf *bp = *bpp;
1095 struct xfs_bmbt_irec *map = mip->map;
1096 int error = 0;
1097 int length;
1098 int i;
1099 int j;
1100
1101 /*
1102 * If we have a buffer, we need to release it and
1103 * take it out of the mapping.
1104 */
1105
1106 if (bp) {
1107 xfs_trans_brelse(NULL, bp);
1108 bp = NULL;
1109 mip->map_blocks -= mp->m_dirblkfsbs;
1110 /*
1111 * Loop to get rid of the extents for the
1112 * directory block.
1113 */
1114 for (i = mp->m_dirblkfsbs; i > 0; ) {
1115 j = min_t(int, map->br_blockcount, i);
1116 map->br_blockcount -= j;
1117 map->br_startblock += j;
1118 map->br_startoff += j;
1119 /*
1120 * If mapping is done, pitch it from
1121 * the table.
1122 */
1123 if (!map->br_blockcount && --mip->map_valid)
1124 memmove(&map[0], &map[1],
1125 sizeof(map[0]) * mip->map_valid);
1126 i -= j;
1127 }
1128 }
1129
1130 /*
1131 * Recalculate the readahead blocks wanted.
1132 */
1133 mip->ra_want = howmany(bufsize + mp->m_dirblksize,
1134 mp->m_sb.sb_blocksize) - 1;
1135 ASSERT(mip->ra_want >= 0);
1136
1137 /*
1138 * If we don't have as many as we want, and we haven't
1139 * run out of data blocks, get some more mappings.
1140 */
1141 if (1 + mip->ra_want > mip->map_blocks &&
1142 mip->map_off < xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET)) {
1143 /*
1144 * Get more bmaps, fill in after the ones
1145 * we already have in the table.
1146 */
1147 mip->nmap = mip->map_size - mip->map_valid;
1148 error = xfs_bmapi_read(dp, mip->map_off,
1149 xfs_dir2_byte_to_da(mp, XFS_DIR2_LEAF_OFFSET) -
1150 mip->map_off,
1151 &map[mip->map_valid], &mip->nmap, 0);
1152
1153 /*
1154 * Don't know if we should ignore this or try to return an
1155 * error. The trouble with returning errors is that readdir
1156 * will just stop without actually passing the error through.
1157 */
1158 if (error)
1159 goto out; /* XXX */
1160
1161 /*
1162 * If we got all the mappings we asked for, set the final map
1163 * offset based on the last bmap value received. Otherwise,
1164 * we've reached the end.
1165 */
1166 if (mip->nmap == mip->map_size - mip->map_valid) {
1167 i = mip->map_valid + mip->nmap - 1;
1168 mip->map_off = map[i].br_startoff + map[i].br_blockcount;
1169 } else
1170 mip->map_off = xfs_dir2_byte_to_da(mp,
1171 XFS_DIR2_LEAF_OFFSET);
1172
1173 /*
1174 * Look for holes in the mapping, and eliminate them. Count up
1175 * the valid blocks.
1176 */
1177 for (i = mip->map_valid; i < mip->map_valid + mip->nmap; ) {
1178 if (map[i].br_startblock == HOLESTARTBLOCK) {
1179 mip->nmap--;
1180 length = mip->map_valid + mip->nmap - i;
1181 if (length)
1182 memmove(&map[i], &map[i + 1],
1183 sizeof(map[i]) * length);
1184 } else {
1185 mip->map_blocks += map[i].br_blockcount;
1186 i++;
1187 }
1188 }
1189 mip->map_valid += mip->nmap;
1190 }
1191
1192 /*
1193 * No valid mappings, so no more data blocks.
1194 */
1195 if (!mip->map_valid) {
1196 *curoff = xfs_dir2_da_to_byte(mp, mip->map_off);
1197 goto out;
1198 }
1199
1200 /*
1201 * Read the directory block starting at the first mapping.
1202 */
1203 mip->curdb = xfs_dir2_da_to_db(mp, map->br_startoff);
Dave Chinner33363fe2013-04-03 16:11:22 +11001204 error = xfs_dir3_data_read(NULL, dp, map->br_startoff,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001205 map->br_blockcount >= mp->m_dirblkfsbs ?
Dave Chinnere4813572012-11-12 22:54:14 +11001206 XFS_FSB_TO_DADDR(mp, map->br_startblock) : -1, &bp);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001207
1208 /*
1209 * Should just skip over the data block instead of giving up.
1210 */
1211 if (error)
1212 goto out; /* XXX */
1213
1214 /*
1215 * Adjust the current amount of read-ahead: we just read a block that
1216 * was previously ra.
1217 */
1218 if (mip->ra_current)
1219 mip->ra_current -= mp->m_dirblkfsbs;
1220
1221 /*
1222 * Do we need more readahead?
1223 */
1224 for (mip->ra_index = mip->ra_offset = i = 0;
1225 mip->ra_want > mip->ra_current && i < mip->map_blocks;
1226 i += mp->m_dirblkfsbs) {
1227 ASSERT(mip->ra_index < mip->map_valid);
1228 /*
1229 * Read-ahead a contiguous directory block.
1230 */
1231 if (i > mip->ra_current &&
1232 map[mip->ra_index].br_blockcount >= mp->m_dirblkfsbs) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001233 xfs_dir3_data_readahead(NULL, dp,
Dave Chinnerda6958c2012-11-12 22:54:18 +11001234 map[mip->ra_index].br_startoff + mip->ra_offset,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001235 XFS_FSB_TO_DADDR(mp,
1236 map[mip->ra_index].br_startblock +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001237 mip->ra_offset));
Dave Chinner9b73bd72012-06-22 18:50:15 +10001238 mip->ra_current = i;
1239 }
1240
1241 /*
1242 * Read-ahead a non-contiguous directory block. This doesn't
1243 * use our mapping, but this is a very rare case.
1244 */
1245 else if (i > mip->ra_current) {
Dave Chinner33363fe2013-04-03 16:11:22 +11001246 xfs_dir3_data_readahead(NULL, dp,
Dave Chinner9b73bd72012-06-22 18:50:15 +10001247 map[mip->ra_index].br_startoff +
Dave Chinnerda6958c2012-11-12 22:54:18 +11001248 mip->ra_offset, -1);
Dave Chinner9b73bd72012-06-22 18:50:15 +10001249 mip->ra_current = i;
1250 }
1251
1252 /*
1253 * Advance offset through the mapping table.
1254 */
1255 for (j = 0; j < mp->m_dirblkfsbs; j++) {
1256 /*
1257 * The rest of this extent but not more than a dir
1258 * block.
1259 */
1260 length = min_t(int, mp->m_dirblkfsbs,
1261 map[mip->ra_index].br_blockcount -
1262 mip->ra_offset);
1263 j += length;
1264 mip->ra_offset += length;
1265
1266 /*
1267 * Advance to the next mapping if this one is used up.
1268 */
1269 if (mip->ra_offset == map[mip->ra_index].br_blockcount) {
1270 mip->ra_offset = 0;
1271 mip->ra_index++;
1272 }
1273 }
1274 }
1275
1276out:
1277 *bpp = bp;
1278 return error;
1279}
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281/*
1282 * Getdents (readdir) for leaf and node directories.
1283 * This reads the data blocks only, so is the same for both forms.
1284 */
1285int /* error */
1286xfs_dir2_leaf_getdents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 xfs_inode_t *dp, /* incore directory inode */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001288 void *dirent,
1289 size_t bufsize,
1290 xfs_off_t *offset,
1291 filldir_t filldir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Dave Chinner9b73bd72012-06-22 18:50:15 +10001293 struct xfs_buf *bp = NULL; /* data block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001294 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 xfs_dir2_data_entry_t *dep; /* data entry */
1296 xfs_dir2_data_unused_t *dup; /* unused entry */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001297 int error = 0; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 int length; /* temporary length value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 xfs_mount_t *mp; /* filesystem mount point */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001300 int byteoff; /* offset in current block */
1301 xfs_dir2_off_t curoff; /* current overall offset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 xfs_dir2_off_t newoff; /* new curoff after new blk */
Nathan Scottf6d75cb2006-03-14 13:32:24 +11001303 char *ptr = NULL; /* pointer to current data */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001304 struct xfs_dir2_leaf_map_info *map_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /*
1307 * If the offset is at or past the largest allowed value,
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001308 * give up right away.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001310 if (*offset >= XFS_DIR2_MAX_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return 0;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 mp = dp->i_mount;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Set up to bmap a number of blocks based on the caller's
1317 * buffer size, the directory block size, and the filesystem
1318 * block size.
1319 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001320 length = howmany(bufsize + mp->m_dirblksize,
1321 mp->m_sb.sb_blocksize);
1322 map_info = kmem_zalloc(offsetof(struct xfs_dir2_leaf_map_info, map) +
1323 (length * sizeof(struct xfs_bmbt_irec)),
1324 KM_SLEEP);
1325 map_info->map_size = length;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /*
1328 * Inside the loop we keep the main offset value as a byte offset
1329 * in the directory file.
1330 */
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001331 curoff = xfs_dir2_dataptr_to_byte(mp, *offset);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 /*
1334 * Force this conversion through db so we truncate the offset
1335 * down to get the start of the data block.
1336 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001337 map_info->map_off = xfs_dir2_db_to_da(mp,
1338 xfs_dir2_byte_to_db(mp, curoff));
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 /*
1341 * Loop over directory entries until we reach the end offset.
1342 * Get more blocks and readahead as necessary.
1343 */
1344 while (curoff < XFS_DIR2_LEAF_OFFSET) {
1345 /*
1346 * If we have no buffer, or we're off the end of the
1347 * current buffer, need to get another one.
1348 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001349 if (!bp || ptr >= (char *)bp->b_addr + mp->m_dirblksize) {
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001350
Dave Chinner9b73bd72012-06-22 18:50:15 +10001351 error = xfs_dir2_leaf_readbuf(dp, bufsize, map_info,
1352 &curoff, &bp);
1353 if (error || !map_info->map_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /*
1357 * Having done a read, we need to set a new offset.
1358 */
Dave Chinner9b73bd72012-06-22 18:50:15 +10001359 newoff = xfs_dir2_db_off_to_byte(mp, map_info->curdb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /*
1361 * Start of the current block.
1362 */
1363 if (curoff < newoff)
1364 curoff = newoff;
1365 /*
1366 * Make sure we're in the right block.
1367 */
1368 else if (curoff > newoff)
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001369 ASSERT(xfs_dir2_byte_to_db(mp, curoff) ==
Dave Chinner9b73bd72012-06-22 18:50:15 +10001370 map_info->curdb);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001371 hdr = bp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001372 xfs_dir3_data_check(dp, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /*
1374 * Find our position in the block.
1375 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001376 ptr = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001377 byteoff = xfs_dir2_byte_to_off(mp, curoff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 /*
1379 * Skip past the header.
1380 */
1381 if (byteoff == 0)
Dave Chinner33363fe2013-04-03 16:11:22 +11001382 curoff += xfs_dir3_data_entry_offset(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /*
1384 * Skip past entries until we reach our offset.
1385 */
1386 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001387 while ((char *)ptr - (char *)hdr < byteoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 dup = (xfs_dir2_data_unused_t *)ptr;
1389
Nathan Scottad354eb2006-03-17 17:27:37 +11001390 if (be16_to_cpu(dup->freetag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 == XFS_DIR2_DATA_FREE_TAG) {
1392
Nathan Scottad354eb2006-03-17 17:27:37 +11001393 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 ptr += length;
1395 continue;
1396 }
1397 dep = (xfs_dir2_data_entry_t *)ptr;
1398 length =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001399 xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 ptr += length;
1401 }
1402 /*
1403 * Now set our real offset.
1404 */
1405 curoff =
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001406 xfs_dir2_db_off_to_byte(mp,
1407 xfs_dir2_byte_to_db(mp, curoff),
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001408 (char *)ptr - (char *)hdr);
1409 if (ptr >= (char *)hdr + mp->m_dirblksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 continue;
1411 }
1412 }
1413 }
1414 /*
1415 * We have a pointer to an entry.
1416 * Is it a live one?
1417 */
1418 dup = (xfs_dir2_data_unused_t *)ptr;
1419 /*
1420 * No, it's unused, skip over it.
1421 */
Nathan Scottad354eb2006-03-17 17:27:37 +11001422 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
1423 length = be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ptr += length;
1425 curoff += length;
1426 continue;
1427 }
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 dep = (xfs_dir2_data_entry_t *)ptr;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001430 length = xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Dave Chinner4a24cb72010-01-20 10:48:05 +11001432 if (filldir(dirent, (char *)dep->name, dep->namelen,
Christoph Hellwig15440312009-01-08 14:00:00 -05001433 xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff,
Christoph Hellwiga19d9f82009-03-29 09:51:08 +02001434 be64_to_cpu(dep->inumber), DT_UNKNOWN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
Christoph Hellwig051e7cd2007-08-28 13:58:24 +10001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 /*
1438 * Advance to next entry in the block.
1439 */
1440 ptr += length;
1441 curoff += length;
Eric Sandeen8e69ce12009-09-25 19:42:26 +00001442 /* bufsize may have just been a guess; don't go negative */
1443 bufsize = bufsize > length ? bufsize - length : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
1446 /*
1447 * All done. Set output offset value to current offset.
1448 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001449 if (curoff > xfs_dir2_dataptr_to_byte(mp, XFS_DIR2_MAX_DATAPTR))
Christoph Hellwig15440312009-01-08 14:00:00 -05001450 *offset = XFS_DIR2_MAX_DATAPTR & 0x7fffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 else
Christoph Hellwig15440312009-01-08 14:00:00 -05001452 *offset = xfs_dir2_byte_to_dataptr(mp, curoff) & 0x7fffffff;
Dave Chinner9b73bd72012-06-22 18:50:15 +10001453 kmem_free(map_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (bp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001455 xfs_trans_brelse(NULL, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return error;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460/*
1461 * Log the bests entries indicated from a leaf1 block.
1462 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001463static void
Dave Chinner24df33b2013-04-12 07:30:21 +10001464xfs_dir3_leaf_log_bests(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001466 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int first, /* first entry to log */
1468 int last) /* last entry to log */
1469{
Nathan Scott68b3a102006-03-17 17:27:19 +11001470 __be16 *firstb; /* pointer to first entry */
1471 __be16 *lastb; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001472 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1474
Dave Chinner24df33b2013-04-12 07:30:21 +10001475 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1476 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1477
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001478 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1479 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1480 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001481 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1483}
1484
1485/*
1486 * Log the leaf entries indicated from a leaf1 or leafn block.
1487 */
1488void
Dave Chinner24df33b2013-04-12 07:30:21 +10001489xfs_dir3_leaf_log_ents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001491 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int first, /* first entry to log */
1493 int last) /* last entry to log */
1494{
1495 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1496 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001497 struct xfs_dir2_leaf *leaf = bp->b_addr;
1498 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001500 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001501 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1502 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1503 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1504
1505 ents = xfs_dir3_leaf_ents_p(leaf);
1506 firstlep = &ents[first];
1507 lastlep = &ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001508 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1510}
1511
1512/*
1513 * Log the header of the leaf1 or leafn block.
1514 */
1515void
Dave Chinner24df33b2013-04-12 07:30:21 +10001516xfs_dir3_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001517 struct xfs_trans *tp,
1518 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Dave Chinner24df33b2013-04-12 07:30:21 +10001520 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001522 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001523 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1524 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1525 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1526
Dave Chinner1d9025e2012-06-22 18:50:14 +10001527 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Dave Chinner24df33b2013-04-12 07:30:21 +10001528 xfs_dir3_leaf_hdr_size(leaf) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
1532 * Log the tail of the leaf1 block.
1533 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001534STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +10001535xfs_dir3_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001536 struct xfs_trans *tp,
1537 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Dave Chinner24df33b2013-04-12 07:30:21 +10001539 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001541 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Dave Chinner24df33b2013-04-12 07:30:21 +10001543 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1544 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1545 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1546 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1547
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001548 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001549 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 (uint)(mp->m_dirblksize - 1));
1551}
1552
1553/*
1554 * Look up the entry referred to by args in the leaf format directory.
1555 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1556 * is also used by the node-format code.
1557 */
1558int
1559xfs_dir2_leaf_lookup(
1560 xfs_da_args_t *args) /* operation arguments */
1561{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001562 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 xfs_dir2_data_entry_t *dep; /* data block entry */
1564 xfs_inode_t *dp; /* incore directory inode */
1565 int error; /* error return code */
1566 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001567 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 xfs_dir2_leaf_t *leaf; /* leaf structure */
1569 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1570 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001571 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001573 trace_xfs_dir2_leaf_lookup(args);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 /*
1576 * Look up name in the leaf block, returning both buffers and index.
1577 */
1578 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1579 return error;
1580 }
1581 tp = args->trans;
1582 dp = args->dp;
Dave Chinner24df33b2013-04-12 07:30:21 +10001583 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001584 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001585 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 /*
1587 * Get to the leaf entry and contained data entry address.
1588 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001589 lep = &ents[index];
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Point to the data entry.
1593 */
1594 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001595 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001596 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001598 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001600 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001601 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001602 xfs_trans_brelse(tp, dbp);
1603 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001604 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607/*
1608 * Look up name/hash in the leaf block.
1609 * Fill in indexp with the found index, and dbpp with the data buffer.
1610 * If not found dbpp will be NULL, and ENOENT comes back.
1611 * lbpp will always be filled in with the leaf buffer unless there's an error.
1612 */
1613static int /* error */
1614xfs_dir2_leaf_lookup_int(
1615 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001616 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001618 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001620 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001621 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 xfs_dir2_data_entry_t *dep; /* data entry */
1623 xfs_inode_t *dp; /* incore directory inode */
1624 int error; /* error return code */
1625 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001626 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1628 xfs_dir2_leaf_t *leaf; /* leaf structure */
1629 xfs_mount_t *mp; /* filesystem mount point */
1630 xfs_dir2_db_t newdb; /* new data block number */
1631 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001632 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001633 enum xfs_dacmp cmp; /* name compare result */
Dave Chinner24df33b2013-04-12 07:30:21 +10001634 struct xfs_dir2_leaf_entry *ents;
1635 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 dp = args->dp;
1638 tp = args->trans;
1639 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001640
Dave Chinner24df33b2013-04-12 07:30:21 +10001641 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001642 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001646 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001647 xfs_dir3_leaf_check(mp, lbp);
1648 ents = xfs_dir3_leaf_ents_p(leaf);
1649 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /*
1652 * Look for the first leaf entry with our hash value.
1653 */
1654 index = xfs_dir2_leaf_search_hash(args, lbp);
1655 /*
1656 * Loop over all the entries with the right hash value
1657 * looking to match the name.
1658 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001659 for (lep = &ents[index];
1660 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1661 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /*
1663 * Skip over stale leaf entries.
1664 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001665 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 continue;
1667 /*
1668 * Get the new data block number.
1669 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001670 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * If it's not the same as the old data block number,
1673 * need to pitch the old one and read the new one.
1674 */
1675 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001676 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001677 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001678 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001679 xfs_dir2_db_to_da(mp, newdb),
1680 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001681 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001682 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return error;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 curdb = newdb;
1686 }
1687 /*
1688 * Point to the data entry.
1689 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001690 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001691 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001693 * Compare name and if it's an exact match, return the index
1694 * and buffer. If it's the first case-insensitive match, store
1695 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Barry Naujok5163f952008-05-21 16:41:01 +10001697 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1698 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1699 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001701 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001702 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001703 *dbpp = dbp;
1704 return 0;
1705 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001706 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 }
Barry Naujok6a178102008-05-21 16:42:05 +10001709 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001710 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001711 * Here, we can only be doing a lookup (not a rename or remove).
1712 * If a case-insensitive match was found earlier, re-read the
1713 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001714 */
1715 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001716 ASSERT(cidb != -1);
1717 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001718 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001719 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001720 xfs_dir2_db_to_da(mp, cidb),
1721 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001722 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001723 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001724 return error;
1725 }
1726 }
1727 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001728 return 0;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * No match found, return ENOENT.
1732 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001733 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001735 xfs_trans_brelse(tp, dbp);
1736 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return XFS_ERROR(ENOENT);
1738}
1739
1740/*
1741 * Remove an entry from a leaf format directory.
1742 */
1743int /* error */
1744xfs_dir2_leaf_removename(
1745 xfs_da_args_t *args) /* operation arguments */
1746{
Nathan Scott68b3a102006-03-17 17:27:19 +11001747 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001748 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001750 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 xfs_dir2_data_entry_t *dep; /* data entry structure */
1752 xfs_inode_t *dp; /* incore directory inode */
1753 int error; /* error return code */
1754 xfs_dir2_db_t i; /* temporary data block # */
1755 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001756 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 xfs_dir2_leaf_t *leaf; /* leaf structure */
1758 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1759 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1760 xfs_mount_t *mp; /* filesystem mount point */
1761 int needlog; /* need to log data header */
1762 int needscan; /* need to rescan data frees */
1763 xfs_dir2_data_off_t oldbest; /* old value of best free */
1764 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001765 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001766 struct xfs_dir2_leaf_entry *ents;
1767 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001769 trace_xfs_dir2_leaf_removename(args);
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /*
1772 * Lookup the leaf entry, get the leaf and data blocks read in.
1773 */
1774 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1775 return error;
1776 }
1777 dp = args->dp;
1778 tp = args->trans;
1779 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001780 leaf = lbp->b_addr;
1781 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001782 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001783 bf = xfs_dir3_data_bestfree_p(hdr);
1784 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1785 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
1787 * Point to the leaf entry, use that to point to the data entry.
1788 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001789 lep = &ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001790 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001792 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 needscan = needlog = 0;
Dave Chinner33363fe2013-04-03 16:11:22 +11001794 oldbest = be16_to_cpu(bf[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001795 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1796 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001797 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
1799 * Mark the former data entry unused.
1800 */
1801 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001802 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001803 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * We just mark the leaf entry stale by putting a null in it.
1806 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001807 leafhdr.stale++;
1808 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1809 xfs_dir3_leaf_log_header(tp, lbp);
1810
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001811 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001812 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /*
1815 * Scan the freespace in the data block again if necessary,
1816 * log the data block header if necessary.
1817 */
1818 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001819 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (needlog)
1821 xfs_dir2_data_log_header(tp, dbp);
1822 /*
1823 * If the longest freespace in the data block has changed,
1824 * put the new value in the bests table and log that.
1825 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001826 if (be16_to_cpu(bf[0].length) != oldbest) {
1827 bestsp[db] = bf[0].length;
Dave Chinner24df33b2013-04-12 07:30:21 +10001828 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001830 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * If the data block is now empty then get rid of the data block.
1833 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001834 if (be16_to_cpu(bf[0].length) ==
1835 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 ASSERT(db != mp->m_dirdatablk);
1837 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1838 /*
1839 * Nope, can't get rid of it because it caused
1840 * allocation of a bmap btree block to do so.
1841 * Just go on, returning success, leaving the
1842 * empty block in place.
1843 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001844 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 error = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10001846 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return error;
1848 }
1849 dbp = NULL;
1850 /*
1851 * If this is the last data block then compact the
1852 * bests table by getting rid of entries.
1853 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001854 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
1856 * Look for the last active entry (i).
1857 */
1858 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001859 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 break;
1861 }
1862 /*
1863 * Copy the table down so inactive entries at the
1864 * end are removed.
1865 */
1866 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001867 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001868 be32_add_cpu(&ltp->bestcount, -(db - i));
Dave Chinner24df33b2013-04-12 07:30:21 +10001869 xfs_dir3_leaf_log_tail(tp, lbp);
1870 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001872 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874 /*
1875 * If the data block was not the first one, drop it.
1876 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001877 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001879
Dave Chinner24df33b2013-04-12 07:30:21 +10001880 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /*
1882 * See if we can convert to block form.
1883 */
1884 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1885}
1886
1887/*
1888 * Replace the inode number in a leaf format directory entry.
1889 */
1890int /* error */
1891xfs_dir2_leaf_replace(
1892 xfs_da_args_t *args) /* operation arguments */
1893{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001894 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 xfs_dir2_data_entry_t *dep; /* data block entry */
1896 xfs_inode_t *dp; /* incore directory inode */
1897 int error; /* error return code */
1898 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001899 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 xfs_dir2_leaf_t *leaf; /* leaf structure */
1901 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1902 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001903 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001905 trace_xfs_dir2_leaf_replace(args);
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
1908 * Look up the entry.
1909 */
1910 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1911 return error;
1912 }
1913 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001914 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001915 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
1917 * Point to the leaf entry, get data address from it.
1918 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001919 lep = &ents[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * Point to the data entry.
1922 */
1923 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001924 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001925 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001926 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /*
1928 * Put the new inode number in, log it.
1929 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001930 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 tp = args->trans;
1932 xfs_dir2_data_log_entry(tp, dbp, dep);
Dave Chinner24df33b2013-04-12 07:30:21 +10001933 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001934 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
1936}
1937
1938/*
1939 * Return index in the leaf block (lbp) which is either the first
1940 * one with this hash value, or if there are none, the insert point
1941 * for that hash value.
1942 */
1943int /* index value */
1944xfs_dir2_leaf_search_hash(
1945 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001946 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 xfs_dahash_t hash=0; /* hash from this entry */
1949 xfs_dahash_t hashwant; /* hash value looking for */
1950 int high; /* high leaf index */
1951 int low; /* low leaf index */
1952 xfs_dir2_leaf_t *leaf; /* leaf structure */
1953 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1954 int mid=0; /* current leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +10001955 struct xfs_dir2_leaf_entry *ents;
1956 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Dave Chinner1d9025e2012-06-22 18:50:14 +10001958 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001959 ents = xfs_dir3_leaf_ents_p(leaf);
1960 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962#ifndef __KERNEL__
Dave Chinner24df33b2013-04-12 07:30:21 +10001963 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965#endif
1966 /*
1967 * Note, the table cannot be empty, so we have to go through the loop.
1968 * Binary search the leaf entries looking for our hash value.
1969 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001970 for (lep = ents, low = 0, high = leafhdr.count - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 hashwant = args->hashval;
1972 low <= high; ) {
1973 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001974 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
1976 if (hash < hashwant)
1977 low = mid + 1;
1978 else
1979 high = mid - 1;
1980 }
1981 /*
1982 * Found one, back up through all the equal hash values.
1983 */
1984 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001985 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 mid--;
1987 }
1988 }
1989 /*
1990 * Need to point to an entry higher than ours.
1991 */
1992 else if (hash < hashwant)
1993 mid++;
1994 return mid;
1995}
1996
1997/*
1998 * Trim off a trailing data block. We know it's empty since the leaf
1999 * freespace table says so.
2000 */
2001int /* error */
2002xfs_dir2_leaf_trim_data(
2003 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002004 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 xfs_dir2_db_t db) /* data block number */
2006{
Nathan Scott68b3a102006-03-17 17:27:19 +11002007 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002008 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 xfs_inode_t *dp; /* incore directory inode */
2010 int error; /* error return value */
2011 xfs_dir2_leaf_t *leaf; /* leaf structure */
2012 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
2013 xfs_mount_t *mp; /* filesystem mount point */
2014 xfs_trans_t *tp; /* transaction pointer */
2015
2016 dp = args->dp;
2017 mp = dp->i_mount;
2018 tp = args->trans;
2019 /*
2020 * Read the offending data block. We need its buffer.
2021 */
Dave Chinner33363fe2013-04-03 16:11:22 +11002022 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002023 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Dave Chinner1d9025e2012-06-22 18:50:14 +10002026 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002027 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002028
2029#ifdef DEBUG
2030{
Dave Chinner1d9025e2012-06-22 18:50:14 +10002031 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11002032 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002033
Dave Chinner33363fe2013-04-03 16:11:22 +11002034 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
2035 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
2036 ASSERT(be16_to_cpu(bf[0].length) ==
2037 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11002038 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02002039}
2040#endif
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Get rid of the data block.
2044 */
2045 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
2046 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10002047 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return error;
2049 }
2050 /*
2051 * Eliminate the last bests entry from the table.
2052 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002053 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08002054 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11002055 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Dave Chinner24df33b2013-04-12 07:30:21 +10002056 xfs_dir3_leaf_log_tail(tp, lbp);
2057 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
2060
Christoph Hellwig22823962011-07-08 14:35:53 +02002061static inline size_t
Dave Chinner24df33b2013-04-12 07:30:21 +10002062xfs_dir3_leaf_size(
2063 struct xfs_dir3_icleaf_hdr *hdr,
Christoph Hellwig22823962011-07-08 14:35:53 +02002064 int counts)
2065{
Dave Chinner24df33b2013-04-12 07:30:21 +10002066 int entries;
2067 int hdrsize;
Christoph Hellwig22823962011-07-08 14:35:53 +02002068
Dave Chinner24df33b2013-04-12 07:30:21 +10002069 entries = hdr->count - hdr->stale;
2070 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
2071 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
2072 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
2073 else
2074 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
2075
2076 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
2077 + counts * sizeof(xfs_dir2_data_off_t)
2078 + sizeof(xfs_dir2_leaf_tail_t);
Christoph Hellwig22823962011-07-08 14:35:53 +02002079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/*
2082 * Convert node form directory to leaf form directory.
2083 * The root of the node form dir needs to already be a LEAFN block.
2084 * Just return if we can't do anything.
2085 */
2086int /* error */
2087xfs_dir2_node_to_leaf(
2088 xfs_da_state_t *state) /* directory operation state */
2089{
2090 xfs_da_args_t *args; /* operation arguments */
2091 xfs_inode_t *dp; /* incore directory inode */
2092 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002093 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 xfs_fileoff_t fo; /* freespace file offset */
2095 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10002096 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
2098 xfs_dir2_leaf_t *leaf; /* leaf structure */
2099 xfs_mount_t *mp; /* filesystem mount point */
2100 int rval; /* successful free trim? */
2101 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10002102 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002103 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /*
2106 * There's more than a leaf level in the btree, so there must
2107 * be multiple leafn blocks. Give up.
2108 */
2109 if (state->path.active > 1)
2110 return 0;
2111 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002112
2113 trace_xfs_dir2_node_to_leaf(args);
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 mp = state->mp;
2116 dp = args->dp;
2117 tp = args->trans;
2118 /*
2119 * Get the last offset in the file.
2120 */
2121 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
2122 return error;
2123 }
2124 fo -= mp->m_dirblkfsbs;
2125 /*
2126 * If there are freespace blocks other than the first one,
2127 * take this opportunity to remove trailing empty freespace blocks
2128 * that may have been left behind during no-space-reservation
2129 * operations.
2130 */
2131 while (fo > mp->m_dirfreeblk) {
2132 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
2133 return error;
2134 }
2135 if (rval)
2136 fo -= mp->m_dirblkfsbs;
2137 else
2138 return 0;
2139 }
2140 /*
2141 * Now find the block just before the freespace block.
2142 */
2143 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
2144 return error;
2145 }
2146 /*
2147 * If it's not the single leaf block, give up.
2148 */
2149 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
2150 return 0;
2151 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002152 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10002153 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
2154
2155 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
2156 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /*
2159 * Read the freespace block.
2160 */
Dave Chinner20252072012-11-12 22:54:13 +11002161 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11002162 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10002164 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002165 xfs_dir3_free_hdr_from_disk(&freehdr, free);
2166
2167 ASSERT(!freehdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * Now see if the leafn and free data will fit in a leaf1.
2171 * If not, release the buffer and give up.
2172 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002173 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10002174 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return 0;
2176 }
Christoph Hellwig22823962011-07-08 14:35:53 +02002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /*
2179 * If the leaf has any stale entries in it, compress them out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 */
Dave Chinner24df33b2013-04-12 07:30:21 +10002181 if (leafhdr.stale)
2182 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002183
Dave Chinner24df33b2013-04-12 07:30:21 +10002184 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
2185 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
2186 ? XFS_DIR2_LEAF1_MAGIC
2187 : XFS_DIR3_LEAF1_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
2190 * Set up the leaf tail from the freespace block.
2191 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10002192 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002193 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
Dave Chinner24df33b2013-04-12 07:30:21 +10002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /*
2196 * Set up the leaf bests table.
2197 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11002198 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
2199 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
Dave Chinner24df33b2013-04-12 07:30:21 +10002200
2201 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
2202 xfs_dir3_leaf_log_header(tp, lbp);
2203 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
2204 xfs_dir3_leaf_log_tail(tp, lbp);
2205 xfs_dir3_leaf_check(mp, lbp);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Get rid of the freespace block.
2209 */
2210 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
2211 if (error) {
2212 /*
2213 * This can't fail here because it can only happen when
2214 * punching out the middle of an extent, and this is an
2215 * isolated block.
2216 */
2217 ASSERT(error != ENOSPC);
2218 return error;
2219 }
2220 fbp = NULL;
2221 /*
2222 * Now see if we can convert the single-leaf directory
2223 * down to a block form directory.
2224 * This routine always kills the dabuf for the leaf, so
2225 * eliminate it from the path.
2226 */
2227 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
2228 state->path.blk[0].bp = NULL;
2229 return error;
2230}