blob: 5505edb9b2ff1d8b8c04218a9f4f9dbe6a983c37 [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"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100034#include "xfs_dir2.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020035#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_error.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000037#include "xfs_trace.h"
Dave Chinner24df33b2013-04-12 07:30:21 +100038#include "xfs_buf_item.h"
39#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Local function declarations.
43 */
Dave Chinner1d9025e2012-06-22 18:50:14 +100044static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, struct xfs_buf **lbpp,
45 int *indexp, struct xfs_buf **dbpp);
Dave Chinner24df33b2013-04-12 07:30:21 +100046static void xfs_dir3_leaf_log_bests(struct xfs_trans *tp, struct xfs_buf *bp,
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100047 int first, int last);
Dave Chinner24df33b2013-04-12 07:30:21 +100048static void xfs_dir3_leaf_log_tail(struct xfs_trans *tp, struct xfs_buf *bp);
Christoph Hellwigba0f32d2005-06-21 15:36:52 +100049
Dave Chinner24df33b2013-04-12 07:30:21 +100050/*
51 * Check the internal consistency of a leaf1 block.
52 * Pop an assert if something is wrong.
53 */
54#ifdef DEBUG
55#define xfs_dir3_leaf_check(mp, bp) \
56do { \
57 if (!xfs_dir3_leaf1_check((mp), (bp))) \
58 ASSERT(0); \
59} while (0);
60
61STATIC bool
62xfs_dir3_leaf1_check(
63 struct xfs_mount *mp,
64 struct xfs_buf *bp)
65{
66 struct xfs_dir2_leaf *leaf = bp->b_addr;
67 struct xfs_dir3_icleaf_hdr leafhdr;
68
69 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
70
71 if (leafhdr.magic == XFS_DIR3_LEAF1_MAGIC) {
72 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
73 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
74 return false;
75 } else if (leafhdr.magic != XFS_DIR2_LEAF1_MAGIC)
76 return false;
77
78 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
79}
80#else
81#define xfs_dir3_leaf_check(mp, bp)
82#endif
83
84void
85xfs_dir3_leaf_hdr_from_disk(
86 struct xfs_dir3_icleaf_hdr *to,
87 struct xfs_dir2_leaf *from)
88{
89 if (from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
90 from->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC)) {
91 to->forw = be32_to_cpu(from->hdr.info.forw);
92 to->back = be32_to_cpu(from->hdr.info.back);
93 to->magic = be16_to_cpu(from->hdr.info.magic);
94 to->count = be16_to_cpu(from->hdr.count);
95 to->stale = be16_to_cpu(from->hdr.stale);
96 } else {
97 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)from;
98
99 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
100 to->back = be32_to_cpu(hdr3->info.hdr.back);
101 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
102 to->count = be16_to_cpu(hdr3->count);
103 to->stale = be16_to_cpu(hdr3->stale);
104 }
105
106 ASSERT(to->magic == XFS_DIR2_LEAF1_MAGIC ||
107 to->magic == XFS_DIR3_LEAF1_MAGIC ||
108 to->magic == XFS_DIR2_LEAFN_MAGIC ||
109 to->magic == XFS_DIR3_LEAFN_MAGIC);
110}
111
112void
113xfs_dir3_leaf_hdr_to_disk(
114 struct xfs_dir2_leaf *to,
115 struct xfs_dir3_icleaf_hdr *from)
116{
117 ASSERT(from->magic == XFS_DIR2_LEAF1_MAGIC ||
118 from->magic == XFS_DIR3_LEAF1_MAGIC ||
119 from->magic == XFS_DIR2_LEAFN_MAGIC ||
120 from->magic == XFS_DIR3_LEAFN_MAGIC);
121
122 if (from->magic == XFS_DIR2_LEAF1_MAGIC ||
123 from->magic == XFS_DIR2_LEAFN_MAGIC) {
124 to->hdr.info.forw = cpu_to_be32(from->forw);
125 to->hdr.info.back = cpu_to_be32(from->back);
126 to->hdr.info.magic = cpu_to_be16(from->magic);
127 to->hdr.count = cpu_to_be16(from->count);
128 to->hdr.stale = cpu_to_be16(from->stale);
129 } else {
130 struct xfs_dir3_leaf_hdr *hdr3 = (struct xfs_dir3_leaf_hdr *)to;
131
132 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
133 hdr3->info.hdr.back = cpu_to_be32(from->back);
134 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
135 hdr3->count = cpu_to_be16(from->count);
136 hdr3->stale = cpu_to_be16(from->stale);
137 }
138}
139
140bool
141xfs_dir3_leaf_check_int(
142 struct xfs_mount *mp,
143 struct xfs_dir3_icleaf_hdr *hdr,
144 struct xfs_dir2_leaf *leaf)
145{
146 struct xfs_dir2_leaf_entry *ents;
147 xfs_dir2_leaf_tail_t *ltp;
148 int stale;
149 int i;
150
151 ents = xfs_dir3_leaf_ents_p(leaf);
152 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
153
154 /*
155 * XXX (dgc): This value is not restrictive enough.
156 * Should factor in the size of the bests table as well.
157 * We can deduce a value for that from di_size.
158 */
159 if (hdr->count > xfs_dir3_max_leaf_ents(mp, leaf))
160 return false;
161
162 /* Leaves and bests don't overlap in leaf format. */
163 if ((hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
164 hdr->magic == XFS_DIR3_LEAF1_MAGIC) &&
165 (char *)&ents[hdr->count] > (char *)xfs_dir2_leaf_bests_p(ltp))
166 return false;
167
168 /* Check hash value order, count stale entries. */
169 for (i = stale = 0; i < hdr->count; i++) {
170 if (i + 1 < hdr->count) {
171 if (be32_to_cpu(ents[i].hashval) >
172 be32_to_cpu(ents[i + 1].hashval))
173 return false;
174 }
175 if (ents[i].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
176 stale++;
177 }
178 if (hdr->stale != stale)
179 return false;
180 return true;
181}
182
183static bool
184xfs_dir3_leaf_verify(
Dave Chinnere6f76672012-11-12 22:54:15 +1100185 struct xfs_buf *bp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000186 __uint16_t magic)
Dave Chinnere6f76672012-11-12 22:54:15 +1100187{
188 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner24df33b2013-04-12 07:30:21 +1000189 struct xfs_dir2_leaf *leaf = bp->b_addr;
190 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnere6f76672012-11-12 22:54:15 +1100191
Dave Chinner24df33b2013-04-12 07:30:21 +1000192 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
193
194 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
195 if (xfs_sb_version_hascrc(&mp->m_sb)) {
196 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
197
198 if ((magic == XFS_DIR2_LEAF1_MAGIC &&
199 leafhdr.magic != XFS_DIR3_LEAF1_MAGIC) ||
200 (magic == XFS_DIR2_LEAFN_MAGIC &&
201 leafhdr.magic != XFS_DIR3_LEAFN_MAGIC))
202 return false;
203
204 if (!uuid_equal(&leaf3->info.uuid, &mp->m_sb.sb_uuid))
205 return false;
206 if (be64_to_cpu(leaf3->info.blkno) != bp->b_bn)
207 return false;
208 } else {
209 if (leafhdr.magic != magic)
210 return false;
211 }
212 return xfs_dir3_leaf_check_int(mp, &leafhdr, leaf);
213}
214
215static void
216__read_verify(
217 struct xfs_buf *bp,
218 __uint16_t magic)
219{
220 struct xfs_mount *mp = bp->b_target->bt_mount;
221
222 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
223 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
224 XFS_DIR3_LEAF_CRC_OFF)) ||
225 !xfs_dir3_leaf_verify(bp, magic)) {
226 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
Dave Chinnere6f76672012-11-12 22:54:15 +1100227 xfs_buf_ioerror(bp, EFSCORRUPTED);
228 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100229}
Dave Chinnere6f76672012-11-12 22:54:15 +1100230
Dave Chinner612cfbf2012-11-14 17:52:32 +1100231static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000232__write_verify(
233 struct xfs_buf *bp,
234 __uint16_t magic)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100235{
Dave Chinner24df33b2013-04-12 07:30:21 +1000236 struct xfs_mount *mp = bp->b_target->bt_mount;
237 struct xfs_buf_log_item *bip = bp->b_fspriv;
238 struct xfs_dir3_leaf_hdr *hdr3 = bp->b_addr;
239
240 if (!xfs_dir3_leaf_verify(bp, magic)) {
241 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
242 xfs_buf_ioerror(bp, EFSCORRUPTED);
243 return;
244 }
245
246 if (!xfs_sb_version_hascrc(&mp->m_sb))
247 return;
248
249 if (bip)
250 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
251
252 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_LEAF_CRC_OFF);
Dave Chinner612cfbf2012-11-14 17:52:32 +1100253}
254
255static void
Dave Chinner24df33b2013-04-12 07:30:21 +1000256xfs_dir3_leaf1_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100257 struct xfs_buf *bp)
258{
Dave Chinner24df33b2013-04-12 07:30:21 +1000259 __read_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinner1813dd62012-11-14 17:54:40 +1100260}
261
Dave Chinner24df33b2013-04-12 07:30:21 +1000262static void
263xfs_dir3_leaf1_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100264 struct xfs_buf *bp)
265{
Dave Chinner24df33b2013-04-12 07:30:21 +1000266 __write_verify(bp, XFS_DIR2_LEAF1_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100267}
268
Dave Chinner24df33b2013-04-12 07:30:21 +1000269static void
270xfs_dir3_leafn_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100271 struct xfs_buf *bp)
Dave Chinnere6f76672012-11-12 22:54:15 +1100272{
Dave Chinner24df33b2013-04-12 07:30:21 +1000273 __read_verify(bp, XFS_DIR2_LEAFN_MAGIC);
Dave Chinnere6f76672012-11-12 22:54:15 +1100274}
275
Dave Chinner24df33b2013-04-12 07:30:21 +1000276static void
277xfs_dir3_leafn_write_verify(
278 struct xfs_buf *bp)
279{
280 __write_verify(bp, XFS_DIR2_LEAFN_MAGIC);
281}
282
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100283const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops = {
Dave Chinner24df33b2013-04-12 07:30:21 +1000284 .verify_read = xfs_dir3_leaf1_read_verify,
285 .verify_write = xfs_dir3_leaf1_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100286};
287
Dave Chinner24df33b2013-04-12 07:30:21 +1000288const struct xfs_buf_ops xfs_dir3_leafn_buf_ops = {
289 .verify_read = xfs_dir3_leafn_read_verify,
290 .verify_write = xfs_dir3_leafn_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100291};
Dave Chinnere6f76672012-11-12 22:54:15 +1100292
293static int
Dave Chinner24df33b2013-04-12 07:30:21 +1000294xfs_dir3_leaf_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100295 struct xfs_trans *tp,
296 struct xfs_inode *dp,
297 xfs_dablk_t fbno,
298 xfs_daddr_t mappedbno,
299 struct xfs_buf **bpp)
300{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100301 int err;
302
303 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000304 XFS_DATA_FORK, &xfs_dir3_leaf1_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100305 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100306 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100307 return err;
Dave Chinnere6f76672012-11-12 22:54:15 +1100308}
309
310int
Dave Chinner24df33b2013-04-12 07:30:21 +1000311xfs_dir3_leafn_read(
Dave Chinnere6f76672012-11-12 22:54:15 +1100312 struct xfs_trans *tp,
313 struct xfs_inode *dp,
314 xfs_dablk_t fbno,
315 xfs_daddr_t mappedbno,
316 struct xfs_buf **bpp)
317{
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100318 int err;
319
320 err = xfs_da_read_buf(tp, dp, fbno, mappedbno, bpp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000321 XFS_DATA_FORK, &xfs_dir3_leafn_buf_ops);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100322 if (!err && tp)
Dave Chinner61fe1352013-04-03 16:11:30 +1100323 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100324 return err;
Dave Chinner24df33b2013-04-12 07:30:21 +1000325}
326
327/*
328 * Initialize a new leaf block, leaf1 or leafn magic accepted.
329 */
330static void
331xfs_dir3_leaf_init(
332 struct xfs_mount *mp,
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100333 struct xfs_trans *tp,
Dave Chinner24df33b2013-04-12 07:30:21 +1000334 struct xfs_buf *bp,
335 xfs_ino_t owner,
336 __uint16_t type)
337{
338 struct xfs_dir2_leaf *leaf = bp->b_addr;
339
340 ASSERT(type == XFS_DIR2_LEAF1_MAGIC || type == XFS_DIR2_LEAFN_MAGIC);
341
342 if (xfs_sb_version_hascrc(&mp->m_sb)) {
343 struct xfs_dir3_leaf_hdr *leaf3 = bp->b_addr;
344
345 memset(leaf3, 0, sizeof(*leaf3));
346
347 leaf3->info.hdr.magic = (type == XFS_DIR2_LEAF1_MAGIC)
348 ? cpu_to_be16(XFS_DIR3_LEAF1_MAGIC)
349 : cpu_to_be16(XFS_DIR3_LEAFN_MAGIC);
350 leaf3->info.blkno = cpu_to_be64(bp->b_bn);
351 leaf3->info.owner = cpu_to_be64(owner);
352 uuid_copy(&leaf3->info.uuid, &mp->m_sb.sb_uuid);
353 } else {
354 memset(leaf, 0, sizeof(*leaf));
355 leaf->hdr.info.magic = cpu_to_be16(type);
356 }
357
358 /*
359 * If it's a leaf-format directory initialize the tail.
360 * Caller is responsible for initialising the bests table.
361 */
362 if (type == XFS_DIR2_LEAF1_MAGIC) {
363 struct xfs_dir2_leaf_tail *ltp;
364
365 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
366 ltp->bestcount = 0;
367 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100368 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100369 } else {
Dave Chinner24df33b2013-04-12 07:30:21 +1000370 bp->b_ops = &xfs_dir3_leafn_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100371 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_LEAFN_BUF);
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100372 }
Dave Chinner24df33b2013-04-12 07:30:21 +1000373}
374
375int
376xfs_dir3_leaf_get_buf(
377 xfs_da_args_t *args,
378 xfs_dir2_db_t bno,
379 struct xfs_buf **bpp,
380 __uint16_t magic)
381{
382 struct xfs_inode *dp = args->dp;
383 struct xfs_trans *tp = args->trans;
384 struct xfs_mount *mp = dp->i_mount;
385 struct xfs_buf *bp;
386 int error;
387
388 ASSERT(magic == XFS_DIR2_LEAF1_MAGIC || magic == XFS_DIR2_LEAFN_MAGIC);
389 ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) &&
390 bno < XFS_DIR2_FREE_FIRSTDB(mp));
391
392 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, bno), -1, &bp,
393 XFS_DATA_FORK);
394 if (error)
395 return error;
396
Dave Chinnerd75afeb2013-04-03 16:11:29 +1100397 xfs_dir3_leaf_init(mp, tp, bp, dp->i_ino, magic);
Dave Chinner24df33b2013-04-12 07:30:21 +1000398 xfs_dir3_leaf_log_header(tp, bp);
399 if (magic == XFS_DIR2_LEAF1_MAGIC)
400 xfs_dir3_leaf_log_tail(tp, bp);
401 *bpp = bp;
402 return 0;
Dave Chinnere6f76672012-11-12 22:54:15 +1100403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405/*
406 * Convert a block form directory to a leaf form directory.
407 */
408int /* error */
409xfs_dir2_block_to_leaf(
410 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000411 struct xfs_buf *dbp) /* input block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Nathan Scott68b3a102006-03-17 17:27:19 +1100413 __be16 *bestsp; /* leaf's bestsp entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 xfs_dablk_t blkno; /* leaf block's bno */
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200415 xfs_dir2_data_hdr_t *hdr; /* block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */
417 xfs_dir2_block_tail_t *btp; /* block's tail */
418 xfs_inode_t *dp; /* incore directory inode */
419 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000420 struct xfs_buf *lbp; /* leaf block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 xfs_dir2_db_t ldb; /* leaf block's bno */
422 xfs_dir2_leaf_t *leaf; /* leaf structure */
423 xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */
424 xfs_mount_t *mp; /* filesystem mount point */
425 int needlog; /* need to log block header */
426 int needscan; /* need to rescan bestfree */
427 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +1100428 struct xfs_dir2_data_free *bf;
Dave Chinner24df33b2013-04-12 07:30:21 +1000429 struct xfs_dir2_leaf_entry *ents;
430 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000432 trace_xfs_dir2_block_to_leaf(args);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dp = args->dp;
435 mp = dp->i_mount;
436 tp = args->trans;
437 /*
438 * Add the leaf block to the inode.
439 * This interface will only put blocks in the leaf/node range.
440 * Since that's empty now, we'll get the root (block 0 in range).
441 */
442 if ((error = xfs_da_grow_inode(args, &blkno))) {
443 return error;
444 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000445 ldb = xfs_dir2_da_to_db(mp, blkno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp));
447 /*
448 * Initialize the leaf block, get a buffer for it.
449 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000450 error = xfs_dir3_leaf_get_buf(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC);
451 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return error;
Dave Chinner24df33b2013-04-12 07:30:21 +1000453
Dave Chinner1d9025e2012-06-22 18:50:14 +1000454 leaf = lbp->b_addr;
455 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100456 xfs_dir3_data_check(dp, dbp);
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200457 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000458 blp = xfs_dir2_block_leaf_p(btp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100459 bf = xfs_dir3_data_bestfree_p(hdr);
Dave Chinner24df33b2013-04-12 07:30:21 +1000460 ents = xfs_dir3_leaf_ents_p(leaf);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
463 * Set the counts in the leaf header.
464 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000465 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
466 leafhdr.count = be32_to_cpu(btp->count);
467 leafhdr.stale = be32_to_cpu(btp->stale);
468 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
469 xfs_dir3_leaf_log_header(tp, lbp);
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Could compact these but I think we always do the conversion
473 * after squeezing out stale entries.
474 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000475 memcpy(ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t));
476 xfs_dir3_leaf_log_ents(tp, lbp, 0, leafhdr.count - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 needscan = 0;
478 needlog = 1;
479 /*
480 * Make the space formerly occupied by the leaf entries and block
481 * tail be free.
482 */
483 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwig4f6ae1a2011-07-08 14:35:27 +0200484 (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr),
485 (xfs_dir2_data_aoff_t)((char *)hdr + mp->m_dirblksize -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 (char *)blp),
487 &needlog, &needscan);
488 /*
489 * Fix up the block header, make it a data block.
490 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100491 dbp->b_ops = &xfs_dir3_data_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +1100492 xfs_trans_buf_set_type(tp, dbp, XFS_BLFT_DIR_DATA_BUF);
Dave Chinner33363fe2013-04-03 16:11:22 +1100493 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))
494 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
495 else
496 hdr->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200499 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Set up leaf tail and bests table.
502 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000503 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100504 ltp->bestcount = cpu_to_be32(1);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000505 bestsp = xfs_dir2_leaf_bests_p(ltp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100506 bestsp[0] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * Log the data header and leaf bests table.
509 */
510 if (needlog)
511 xfs_dir2_data_log_header(tp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000512 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100513 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +1000514 xfs_dir3_leaf_log_bests(tp, lbp, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516}
517
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200518STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +1000519xfs_dir3_leaf_find_stale(
520 struct xfs_dir3_icleaf_hdr *leafhdr,
521 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200522 int index,
523 int *lowstale,
524 int *highstale)
525{
526 /*
527 * Find the first stale entry before our index, if any.
528 */
529 for (*lowstale = index - 1; *lowstale >= 0; --*lowstale) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000530 if (ents[*lowstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200531 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
532 break;
533 }
534
535 /*
536 * Find the first stale entry at or after our index, if any.
537 * Stop if the result would require moving more entries than using
538 * lowstale.
539 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000540 for (*highstale = index; *highstale < leafhdr->count; ++*highstale) {
541 if (ents[*highstale].address ==
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200542 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
543 break;
544 if (*lowstale >= 0 && index - *lowstale <= *highstale - index)
545 break;
546 }
547}
548
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200549struct xfs_dir2_leaf_entry *
Dave Chinner24df33b2013-04-12 07:30:21 +1000550xfs_dir3_leaf_find_entry(
551 struct xfs_dir3_icleaf_hdr *leafhdr,
552 struct xfs_dir2_leaf_entry *ents,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200553 int index, /* leaf table position */
554 int compact, /* need to compact leaves */
555 int lowstale, /* index of prev stale leaf */
556 int highstale, /* index of next stale leaf */
557 int *lfloglow, /* low leaf logging index */
558 int *lfloghigh) /* high leaf logging index */
559{
Dave Chinner24df33b2013-04-12 07:30:21 +1000560 if (!leafhdr->stale) {
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200561 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
562
563 /*
564 * Now we need to make room to insert the leaf entry.
565 *
566 * If there are no stale entries, just insert a hole at index.
567 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000568 lep = &ents[index];
569 if (index < leafhdr->count)
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200570 memmove(lep + 1, lep,
Dave Chinner24df33b2013-04-12 07:30:21 +1000571 (leafhdr->count - index) * sizeof(*lep));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200572
573 /*
574 * Record low and high logging indices for the leaf.
575 */
576 *lfloglow = index;
Dave Chinner24df33b2013-04-12 07:30:21 +1000577 *lfloghigh = leafhdr->count++;
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200578 return lep;
579 }
580
581 /*
582 * There are stale entries.
583 *
584 * We will use one of them for the new entry. It's probably not at
585 * the right location, so we'll have to shift some up or down first.
586 *
587 * If we didn't compact before, we need to find the nearest stale
588 * entries before and after our insertion point.
589 */
Christoph Hellwiga230a1d2011-07-13 13:43:48 +0200590 if (compact == 0)
Dave Chinner24df33b2013-04-12 07:30:21 +1000591 xfs_dir3_leaf_find_stale(leafhdr, ents, index,
592 &lowstale, &highstale);
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200593
594 /*
595 * If the low one is better, use it.
596 */
597 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +1000598 (highstale == leafhdr->count ||
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200599 index - lowstale - 1 < highstale - index)) {
600 ASSERT(index - lowstale - 1 >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000601 ASSERT(ents[lowstale].address ==
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200602 cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200603
604 /*
605 * Copy entries up to cover the stale entry and make room
606 * for the new entry.
607 */
608 if (index - lowstale - 1 > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000609 memmove(&ents[lowstale], &ents[lowstale + 1],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200610 (index - lowstale - 1) *
Dave Chinner24df33b2013-04-12 07:30:21 +1000611 sizeof(xfs_dir2_leaf_entry_t));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200612 }
613 *lfloglow = MIN(lowstale, *lfloglow);
614 *lfloghigh = MAX(index - 1, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000615 leafhdr->stale--;
616 return &ents[index - 1];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200617 }
618
619 /*
620 * The high one is better, so use that one.
621 */
622 ASSERT(highstale - index >= 0);
Dave Chinner24df33b2013-04-12 07:30:21 +1000623 ASSERT(ents[highstale].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR));
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200624
625 /*
626 * Copy entries down to cover the stale entry and make room for the
627 * new entry.
628 */
629 if (highstale - index > 0) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000630 memmove(&ents[index + 1], &ents[index],
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200631 (highstale - index) * sizeof(xfs_dir2_leaf_entry_t));
632 }
633 *lfloglow = MIN(index, *lfloglow);
634 *lfloghigh = MAX(highstale, *lfloghigh);
Dave Chinner24df33b2013-04-12 07:30:21 +1000635 leafhdr->stale--;
636 return &ents[index];
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*
640 * Add an entry to a leaf form directory.
641 */
642int /* error */
643xfs_dir2_leaf_addname(
644 xfs_da_args_t *args) /* operation arguments */
645{
Nathan Scott68b3a102006-03-17 17:27:19 +1100646 __be16 *bestsp; /* freespace table in leaf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int compact; /* need to compact leaves */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200648 xfs_dir2_data_hdr_t *hdr; /* data block header */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000649 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 xfs_dir2_data_entry_t *dep; /* data block entry */
651 xfs_inode_t *dp; /* incore directory inode */
652 xfs_dir2_data_unused_t *dup; /* data unused entry */
653 int error; /* error return value */
654 int grown; /* allocated new data block */
655 int highstale; /* index of next stale leaf */
656 int i; /* temporary, index */
657 int index; /* leaf table position */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000658 struct xfs_buf *lbp; /* leaf's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 xfs_dir2_leaf_t *leaf; /* leaf structure */
660 int length; /* length of new entry */
661 xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */
662 int lfloglow; /* low leaf logging index */
663 int lfloghigh; /* high leaf logging index */
664 int lowstale; /* index of prev stale leaf */
665 xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */
666 xfs_mount_t *mp; /* filesystem mount point */
667 int needbytes; /* leaf block bytes needed */
668 int needlog; /* need to log data header */
669 int needscan; /* need to rescan data free */
Nathan Scott3d693c62006-03-17 17:28:27 +1100670 __be16 *tagp; /* end of data entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfs_trans_t *tp; /* transaction pointer */
672 xfs_dir2_db_t use_block; /* data block number */
Dave Chinner33363fe2013-04-03 16:11:22 +1100673 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +1000674 struct xfs_dir2_leaf_entry *ents;
675 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Christoph Hellwig0b1b2132009-12-14 23:14:59 +0000677 trace_xfs_dir2_leaf_addname(args);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 dp = args->dp;
680 tp = args->trans;
681 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +1100682
Dave Chinner24df33b2013-04-12 07:30:21 +1000683 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100684 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +1100686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /*
688 * Look up the entry by hash value and name.
689 * We know it's not there, our caller has already done a lookup.
690 * So the index is of the entry to insert in front of.
691 * But if there are dup hash values the index is of the first of those.
692 */
693 index = xfs_dir2_leaf_search_hash(args, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +1000694 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000695 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner24df33b2013-04-12 07:30:21 +1000696 ents = xfs_dir3_leaf_ents_p(leaf);
697 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000698 bestsp = xfs_dir2_leaf_bests_p(ltp);
699 length = xfs_dir2_data_entsize(args->namelen);
Dave Chinner24df33b2013-04-12 07:30:21 +1000700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * See if there are any entries with the same hash value
703 * and space in their block for the new entry.
704 * This is good because it puts multiple same-hash value entries
705 * in a data block, improving the lookup of those entries.
706 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000707 for (use_block = -1, lep = &ents[index];
708 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 index++, lep++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100710 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 continue;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000712 i = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100713 ASSERT(i < be32_to_cpu(ltp->bestcount));
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200714 ASSERT(bestsp[i] != cpu_to_be16(NULLDATAOFF));
Nathan Scott68b3a102006-03-17 17:27:19 +1100715 if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 use_block = i;
717 break;
718 }
719 }
720 /*
721 * Didn't find a block yet, linear search all the data blocks.
722 */
723 if (use_block == -1) {
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100724 for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Remember a block we see that's missing.
727 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200728 if (bestsp[i] == cpu_to_be16(NULLDATAOFF) &&
729 use_block == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 use_block = i;
Nathan Scott68b3a102006-03-17 17:27:19 +1100731 else if (be16_to_cpu(bestsp[i]) >= length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 use_block = i;
733 break;
734 }
735 }
736 }
737 /*
738 * How many bytes do we need in the leaf block?
739 */
Christoph Hellwig22823962011-07-08 14:35:53 +0200740 needbytes = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +1000741 if (!leafhdr.stale)
Christoph Hellwig22823962011-07-08 14:35:53 +0200742 needbytes += sizeof(xfs_dir2_leaf_entry_t);
743 if (use_block == -1)
744 needbytes += sizeof(xfs_dir2_data_off_t);
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * Now kill use_block if it refers to a missing block, so we
748 * can use it as an indication of allocation needed.
749 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200750 if (use_block != -1 && bestsp[use_block] == cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 use_block = -1;
752 /*
753 * If we don't have enough free bytes but we can make enough
754 * by compacting out stale entries, we'll do that.
755 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000756 if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes &&
757 leafhdr.stale > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 compact = 1;
Dave Chinner24df33b2013-04-12 07:30:21 +1000759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Otherwise if we don't have enough free bytes we need to
762 * convert to node form.
763 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000764 else if ((char *)bestsp - (char *)&ents[leafhdr.count] < needbytes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 /*
766 * Just checking or no space reservation, give up.
767 */
Barry Naujok6a178102008-05-21 16:42:05 +1000768 if ((args->op_flags & XFS_DA_OP_JUSTCHECK) ||
769 args->total == 0) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000770 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return XFS_ERROR(ENOSPC);
772 }
773 /*
774 * Convert to node form.
775 */
776 error = xfs_dir2_leaf_to_node(args, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (error)
778 return error;
779 /*
780 * Then add the new entry.
781 */
782 return xfs_dir2_node_addname(args);
783 }
784 /*
785 * Otherwise it will fit without compaction.
786 */
787 else
788 compact = 0;
789 /*
790 * If just checking, then it will fit unless we needed to allocate
791 * a new data block.
792 */
Barry Naujok6a178102008-05-21 16:42:05 +1000793 if (args->op_flags & XFS_DA_OP_JUSTCHECK) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000794 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return use_block == -1 ? XFS_ERROR(ENOSPC) : 0;
796 }
797 /*
798 * If no allocations are allowed, return now before we've
799 * changed anything.
800 */
801 if (args->total == 0 && use_block == -1) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000802 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return XFS_ERROR(ENOSPC);
804 }
805 /*
806 * Need to compact the leaf entries, removing stale ones.
807 * Leave one stale entry behind - the one closest to our
808 * insertion index - and we'll shift that one to our insertion
809 * point later.
810 */
811 if (compact) {
Dave Chinner24df33b2013-04-12 07:30:21 +1000812 xfs_dir3_leaf_compact_x1(&leafhdr, ents, &index, &lowstale,
813 &highstale, &lfloglow, &lfloghigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815 /*
816 * There are stale entries, so we'll need log-low and log-high
817 * impossibly bad values later.
818 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000819 else if (leafhdr.stale) {
820 lfloglow = leafhdr.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 lfloghigh = -1;
822 }
823 /*
824 * If there was no data block space found, we need to allocate
825 * a new one.
826 */
827 if (use_block == -1) {
828 /*
829 * Add the new data block.
830 */
831 if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE,
832 &use_block))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000833 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return error;
835 }
836 /*
837 * Initialize the block.
838 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100839 if ((error = xfs_dir3_data_init(args, use_block, &dbp))) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000840 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return error;
842 }
843 /*
844 * If we're adding a new data block on the end we need to
845 * extend the bests table. Copy it up one entry.
846 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100847 if (use_block >= be32_to_cpu(ltp->bestcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 bestsp--;
849 memmove(&bestsp[0], &bestsp[1],
Nathan Scottafbcb3f2006-03-17 17:27:28 +1100850 be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0]));
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800851 be32_add_cpu(&ltp->bestcount, 1);
Dave Chinner24df33b2013-04-12 07:30:21 +1000852 xfs_dir3_leaf_log_tail(tp, lbp);
853 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 /*
856 * If we're filling in a previously empty block just log it.
857 */
858 else
Dave Chinner24df33b2013-04-12 07:30:21 +1000859 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
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);
862 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 grown = 1;
Dave Chinnere4813572012-11-12 22:54:14 +1100864 } else {
865 /*
866 * Already had space in some data block.
867 * Just read that one in.
868 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100869 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +1100870 xfs_dir2_db_to_da(mp, use_block),
871 -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +1100872 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +1000873 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return error;
875 }
Dave Chinner1d9025e2012-06-22 18:50:14 +1000876 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +1100877 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 grown = 0;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /*
881 * Point to the biggest freespace in our data block.
882 */
883 dup = (xfs_dir2_data_unused_t *)
Dave Chinner33363fe2013-04-03 16:11:22 +1100884 ((char *)hdr + be16_to_cpu(bf[0].offset));
Nathan Scottad354eb2006-03-17 17:27:37 +1100885 ASSERT(be16_to_cpu(dup->length) >= length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 needscan = needlog = 0;
887 /*
888 * Mark the initial part of our freespace in use for the new entry.
889 */
890 xfs_dir2_data_use_free(tp, dbp, dup,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200891 (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), length,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 &needlog, &needscan);
893 /*
894 * Initialize our new entry (at last).
895 */
896 dep = (xfs_dir2_data_entry_t *)dup;
Christoph Hellwigff9901c2006-06-09 14:48:37 +1000897 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dep->namelen = args->namelen;
899 memcpy(dep->name, args->name, dep->namelen);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000900 tagp = xfs_dir2_data_entry_tag_p(dep);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200901 *tagp = cpu_to_be16((char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /*
903 * Need to scan fix up the bestfree table.
904 */
905 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200906 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /*
908 * Need to log the data block's header.
909 */
910 if (needlog)
911 xfs_dir2_data_log_header(tp, dbp);
912 xfs_dir2_data_log_entry(tp, dbp, dep);
913 /*
914 * If the bests table needs to be changed, do it.
915 * Log the change unless we've already done that.
916 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100917 if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(bf[0].length)) {
918 bestsp[use_block] = bf[0].length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (!grown)
Dave Chinner24df33b2013-04-12 07:30:21 +1000920 xfs_dir3_leaf_log_bests(tp, lbp, use_block, use_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200922
Dave Chinner24df33b2013-04-12 07:30:21 +1000923 lep = xfs_dir3_leaf_find_entry(&leafhdr, ents, index, compact, lowstale,
Christoph Hellwig4fb44c82011-07-08 14:34:59 +0200924 highstale, &lfloglow, &lfloghigh);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /*
927 * Fill in the new leaf entry.
928 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100929 lep->hashval = cpu_to_be32(args->hashval);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000930 lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, use_block,
Nathan Scott3d693c62006-03-17 17:28:27 +1100931 be16_to_cpu(*tagp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /*
933 * Log the leaf fields and give up the buffers.
934 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000935 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
936 xfs_dir3_leaf_log_header(tp, lbp);
937 xfs_dir3_leaf_log_ents(tp, lbp, lfloglow, lfloghigh);
938 xfs_dir3_leaf_check(mp, lbp);
Dave Chinner33363fe2013-04-03 16:11:22 +1100939 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943/*
944 * Compact out any stale entries in the leaf.
945 * Log the header and changed leaf entries, if any.
946 */
947void
Dave Chinner24df33b2013-04-12 07:30:21 +1000948xfs_dir3_leaf_compact(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 xfs_da_args_t *args, /* operation arguments */
Dave Chinner24df33b2013-04-12 07:30:21 +1000950 struct xfs_dir3_icleaf_hdr *leafhdr,
Dave Chinner1d9025e2012-06-22 18:50:14 +1000951 struct xfs_buf *bp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 int from; /* source leaf index */
954 xfs_dir2_leaf_t *leaf; /* leaf structure */
955 int loglow; /* first leaf entry to log */
956 int to; /* target leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +1000957 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Dave Chinner1d9025e2012-06-22 18:50:14 +1000959 leaf = bp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +1000960 if (!leafhdr->stale)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return;
Dave Chinner24df33b2013-04-12 07:30:21 +1000962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /*
964 * Compress out the stale entries in place.
965 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000966 ents = xfs_dir3_leaf_ents_p(leaf);
967 for (from = to = 0, loglow = -1; from < leafhdr->count; from++) {
968 if (ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 continue;
970 /*
971 * Only actually copy the entries that are different.
972 */
973 if (from > to) {
974 if (loglow == -1)
975 loglow = to;
Dave Chinner24df33b2013-04-12 07:30:21 +1000976 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 to++;
979 }
980 /*
981 * Update and log the header, log the leaf entries.
982 */
Dave Chinner24df33b2013-04-12 07:30:21 +1000983 ASSERT(leafhdr->stale == from - to);
984 leafhdr->count -= leafhdr->stale;
985 leafhdr->stale = 0;
986
987 xfs_dir3_leaf_hdr_to_disk(leaf, leafhdr);
988 xfs_dir3_leaf_log_header(args->trans, bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (loglow != -1)
Dave Chinner24df33b2013-04-12 07:30:21 +1000990 xfs_dir3_leaf_log_ents(args->trans, bp, loglow, to - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993/*
994 * Compact the leaf entries, removing stale ones.
995 * Leave one stale entry behind - the one closest to our
996 * insertion index - and the caller will shift that one to our insertion
997 * point later.
998 * Return new insertion index, where the remaining stale entry is,
999 * and leaf logging indices.
1000 */
1001void
Dave Chinner24df33b2013-04-12 07:30:21 +10001002xfs_dir3_leaf_compact_x1(
1003 struct xfs_dir3_icleaf_hdr *leafhdr,
1004 struct xfs_dir2_leaf_entry *ents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int *indexp, /* insertion index */
1006 int *lowstalep, /* out: stale entry before us */
1007 int *highstalep, /* out: stale entry after us */
1008 int *lowlogp, /* out: low log index */
1009 int *highlogp) /* out: high log index */
1010{
1011 int from; /* source copy index */
1012 int highstale; /* stale entry at/after index */
1013 int index; /* insertion index */
1014 int keepstale; /* source index of kept stale */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int lowstale; /* stale entry before index */
1016 int newindex=0; /* new insertion index */
1017 int to; /* destination copy index */
1018
Dave Chinner24df33b2013-04-12 07:30:21 +10001019 ASSERT(leafhdr->stale > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 index = *indexp;
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001021
Dave Chinner24df33b2013-04-12 07:30:21 +10001022 xfs_dir3_leaf_find_stale(leafhdr, ents, index, &lowstale, &highstale);
Christoph Hellwiga230a1d2011-07-13 13:43:48 +02001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * Pick the better of lowstale and highstale.
1026 */
1027 if (lowstale >= 0 &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001028 (highstale == leafhdr->count ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 index - lowstale <= highstale - index))
1030 keepstale = lowstale;
1031 else
1032 keepstale = highstale;
1033 /*
1034 * Copy the entries in place, removing all the stale entries
1035 * except keepstale.
1036 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001037 for (from = to = 0; from < leafhdr->count; from++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * Notice the new value of index.
1040 */
1041 if (index == from)
1042 newindex = to;
1043 if (from != keepstale &&
Dave Chinner24df33b2013-04-12 07:30:21 +10001044 ents[from].address == cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (from == to)
1046 *lowlogp = to;
1047 continue;
1048 }
1049 /*
1050 * Record the new keepstale value for the insertion.
1051 */
1052 if (from == keepstale)
1053 lowstale = highstale = to;
1054 /*
1055 * Copy only the entries that have moved.
1056 */
1057 if (from > to)
Dave Chinner24df33b2013-04-12 07:30:21 +10001058 ents[to] = ents[from];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 to++;
1060 }
1061 ASSERT(from > to);
1062 /*
1063 * If the insertion point was past the last entry,
1064 * set the new insertion point accordingly.
1065 */
1066 if (index == from)
1067 newindex = to;
1068 *indexp = newindex;
1069 /*
1070 * Adjust the leaf header values.
1071 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001072 leafhdr->count -= from - to;
1073 leafhdr->stale = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /*
1075 * Remember the low/high stale value only in the "right"
1076 * direction.
1077 */
1078 if (lowstale >= newindex)
1079 lowstale = -1;
1080 else
Dave Chinner24df33b2013-04-12 07:30:21 +10001081 highstale = leafhdr->count;
1082 *highlogp = leafhdr->count - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 *lowstalep = lowstale;
1084 *highstalep = highstale;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * Log the bests entries indicated from a leaf1 block.
1089 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001090static void
Dave Chinner24df33b2013-04-12 07:30:21 +10001091xfs_dir3_leaf_log_bests(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001093 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int first, /* first entry to log */
1095 int last) /* last entry to log */
1096{
Nathan Scott68b3a102006-03-17 17:27:19 +11001097 __be16 *firstb; /* pointer to first entry */
1098 __be16 *lastb; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001099 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1101
Dave Chinner24df33b2013-04-12 07:30:21 +10001102 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1103 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC));
1104
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001105 ltp = xfs_dir2_leaf_tail_p(tp->t_mountp, leaf);
1106 firstb = xfs_dir2_leaf_bests_p(ltp) + first;
1107 lastb = xfs_dir2_leaf_bests_p(ltp) + last;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001108 xfs_trans_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1));
1110}
1111
1112/*
1113 * Log the leaf entries indicated from a leaf1 or leafn block.
1114 */
1115void
Dave Chinner24df33b2013-04-12 07:30:21 +10001116xfs_dir3_leaf_log_ents(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 xfs_trans_t *tp, /* transaction pointer */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001118 struct xfs_buf *bp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int first, /* first entry to log */
1120 int last) /* last entry to log */
1121{
1122 xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */
1123 xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */
Dave Chinner24df33b2013-04-12 07:30:21 +10001124 struct xfs_dir2_leaf *leaf = bp->b_addr;
1125 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001127 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001128 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1129 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1130 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1131
1132 ents = xfs_dir3_leaf_ents_p(leaf);
1133 firstlep = &ents[first];
1134 lastlep = &ents[last];
Dave Chinner1d9025e2012-06-22 18:50:14 +10001135 xfs_trans_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1));
1137}
1138
1139/*
1140 * Log the header of the leaf1 or leafn block.
1141 */
1142void
Dave Chinner24df33b2013-04-12 07:30:21 +10001143xfs_dir3_leaf_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001144 struct xfs_trans *tp,
1145 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Dave Chinner24df33b2013-04-12 07:30:21 +10001147 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001149 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
Dave Chinner24df33b2013-04-12 07:30:21 +10001150 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1151 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1152 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1153
Dave Chinner1d9025e2012-06-22 18:50:14 +10001154 xfs_trans_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf),
Dave Chinner24df33b2013-04-12 07:30:21 +10001155 xfs_dir3_leaf_hdr_size(leaf) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158/*
1159 * Log the tail of the leaf1 block.
1160 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10001161STATIC void
Dave Chinner24df33b2013-04-12 07:30:21 +10001162xfs_dir3_leaf_log_tail(
Dave Chinner1d9025e2012-06-22 18:50:14 +10001163 struct xfs_trans *tp,
1164 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Dave Chinner24df33b2013-04-12 07:30:21 +10001166 struct xfs_dir2_leaf *leaf = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
Dave Chinner24df33b2013-04-12 07:30:21 +10001168 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Dave Chinner24df33b2013-04-12 07:30:21 +10001170 ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC) ||
1171 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAF1_MAGIC) ||
1172 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAFN_MAGIC) ||
1173 leaf->hdr.info.magic == cpu_to_be16(XFS_DIR3_LEAFN_MAGIC));
1174
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001175 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001176 xfs_trans_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 (uint)(mp->m_dirblksize - 1));
1178}
1179
1180/*
1181 * Look up the entry referred to by args in the leaf format directory.
1182 * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which
1183 * is also used by the node-format code.
1184 */
1185int
1186xfs_dir2_leaf_lookup(
1187 xfs_da_args_t *args) /* operation arguments */
1188{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001189 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 xfs_dir2_data_entry_t *dep; /* data block entry */
1191 xfs_inode_t *dp; /* incore directory inode */
1192 int error; /* error return code */
1193 int index; /* found entry index */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001194 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 xfs_dir2_leaf_t *leaf; /* leaf structure */
1196 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1197 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001198 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001200 trace_xfs_dir2_leaf_lookup(args);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Look up name in the leaf block, returning both buffers and index.
1204 */
1205 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1206 return error;
1207 }
1208 tp = args->trans;
1209 dp = args->dp;
Dave Chinner24df33b2013-04-12 07:30:21 +10001210 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001211 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001212 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 /*
1214 * Get to the leaf entry and contained data entry address.
1215 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001216 lep = &ents[index];
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /*
1219 * Point to the data entry.
1220 */
1221 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001222 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001223 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /*
Barry Naujok384f3ce2008-05-21 16:58:22 +10001225 * Return the found inode number & CI name if appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001227 args->inumber = be64_to_cpu(dep->inumber);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001228 error = xfs_dir_cilookup_result(args, dep->name, dep->namelen);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001229 xfs_trans_brelse(tp, dbp);
1230 xfs_trans_brelse(tp, lbp);
Barry Naujok384f3ce2008-05-21 16:58:22 +10001231 return XFS_ERROR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234/*
1235 * Look up name/hash in the leaf block.
1236 * Fill in indexp with the found index, and dbpp with the data buffer.
1237 * If not found dbpp will be NULL, and ENOENT comes back.
1238 * lbpp will always be filled in with the leaf buffer unless there's an error.
1239 */
1240static int /* error */
1241xfs_dir2_leaf_lookup_int(
1242 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001243 struct xfs_buf **lbpp, /* out: leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int *indexp, /* out: index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001245 struct xfs_buf **dbpp) /* out: data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001247 xfs_dir2_db_t curdb = -1; /* current data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001248 struct xfs_buf *dbp = NULL; /* data buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 xfs_dir2_data_entry_t *dep; /* data entry */
1250 xfs_inode_t *dp; /* incore directory inode */
1251 int error; /* error return code */
1252 int index; /* index in leaf block */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001253 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1255 xfs_dir2_leaf_t *leaf; /* leaf structure */
1256 xfs_mount_t *mp; /* filesystem mount point */
1257 xfs_dir2_db_t newdb; /* new data block number */
1258 xfs_trans_t *tp; /* transaction pointer */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001259 xfs_dir2_db_t cidb = -1; /* case match data block no. */
Barry Naujok5163f952008-05-21 16:41:01 +10001260 enum xfs_dacmp cmp; /* name compare result */
Dave Chinner24df33b2013-04-12 07:30:21 +10001261 struct xfs_dir2_leaf_entry *ents;
1262 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 dp = args->dp;
1265 tp = args->trans;
1266 mp = dp->i_mount;
Dave Chinnere6f76672012-11-12 22:54:15 +11001267
Dave Chinner24df33b2013-04-12 07:30:21 +10001268 error = xfs_dir3_leaf_read(tp, dp, mp->m_dirleafblk, -1, &lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001269 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return error;
Dave Chinnere6f76672012-11-12 22:54:15 +11001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *lbpp = lbp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001273 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001274 xfs_dir3_leaf_check(mp, lbp);
1275 ents = xfs_dir3_leaf_ents_p(leaf);
1276 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 /*
1279 * Look for the first leaf entry with our hash value.
1280 */
1281 index = xfs_dir2_leaf_search_hash(args, lbp);
1282 /*
1283 * Loop over all the entries with the right hash value
1284 * looking to match the name.
1285 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001286 for (lep = &ents[index];
1287 index < leafhdr.count && be32_to_cpu(lep->hashval) == args->hashval;
1288 lep++, index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /*
1290 * Skip over stale leaf entries.
1291 */
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001292 if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 continue;
1294 /*
1295 * Get the new data block number.
1296 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001297 newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /*
1299 * If it's not the same as the old data block number,
1300 * need to pitch the old one and read the new one.
1301 */
1302 if (newdb != curdb) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001303 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001304 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001305 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001306 xfs_dir2_db_to_da(mp, newdb),
1307 -1, &dbp);
Barry Naujok5163f952008-05-21 16:41:01 +10001308 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001309 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return error;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 curdb = newdb;
1313 }
1314 /*
1315 * Point to the data entry.
1316 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001317 dep = (xfs_dir2_data_entry_t *)((char *)dbp->b_addr +
Barry Naujok5163f952008-05-21 16:41:01 +10001318 xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /*
Barry Naujok5163f952008-05-21 16:41:01 +10001320 * Compare name and if it's an exact match, return the index
1321 * and buffer. If it's the first case-insensitive match, store
1322 * the index and buffer and continue looking for an exact match.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
Barry Naujok5163f952008-05-21 16:41:01 +10001324 cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen);
1325 if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) {
1326 args->cmpresult = cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *indexp = index;
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001328 /* case exact match: return the current buffer. */
Barry Naujok5163f952008-05-21 16:41:01 +10001329 if (cmp == XFS_CMP_EXACT) {
Barry Naujok5163f952008-05-21 16:41:01 +10001330 *dbpp = dbp;
1331 return 0;
1332 }
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001333 cidb = curdb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335 }
Barry Naujok6a178102008-05-21 16:42:05 +10001336 ASSERT(args->op_flags & XFS_DA_OP_OKNOENT);
Barry Naujok5163f952008-05-21 16:41:01 +10001337 /*
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001338 * Here, we can only be doing a lookup (not a rename or remove).
1339 * If a case-insensitive match was found earlier, re-read the
1340 * appropriate data block if required and return it.
Barry Naujok5163f952008-05-21 16:41:01 +10001341 */
1342 if (args->cmpresult == XFS_CMP_CASE) {
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001343 ASSERT(cidb != -1);
1344 if (cidb != curdb) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001345 xfs_trans_brelse(tp, dbp);
Dave Chinner33363fe2013-04-03 16:11:22 +11001346 error = xfs_dir3_data_read(tp, dp,
Dave Chinnere4813572012-11-12 22:54:14 +11001347 xfs_dir2_db_to_da(mp, cidb),
1348 -1, &dbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001349 if (error) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001350 xfs_trans_brelse(tp, lbp);
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001351 return error;
1352 }
1353 }
1354 *dbpp = dbp;
Barry Naujok5163f952008-05-21 16:41:01 +10001355 return 0;
1356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * No match found, return ENOENT.
1359 */
Barry Naujok07fe4dd2008-06-27 13:32:11 +10001360 ASSERT(cidb == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (dbp)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001362 xfs_trans_brelse(tp, dbp);
1363 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return XFS_ERROR(ENOENT);
1365}
1366
1367/*
1368 * Remove an entry from a leaf format directory.
1369 */
1370int /* error */
1371xfs_dir2_leaf_removename(
1372 xfs_da_args_t *args) /* operation arguments */
1373{
Nathan Scott68b3a102006-03-17 17:27:19 +11001374 __be16 *bestsp; /* leaf block best freespace */
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001375 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 xfs_dir2_db_t db; /* data block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001377 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 xfs_dir2_data_entry_t *dep; /* data entry structure */
1379 xfs_inode_t *dp; /* incore directory inode */
1380 int error; /* error return code */
1381 xfs_dir2_db_t i; /* temporary data block # */
1382 int index; /* index into leaf entries */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001383 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 xfs_dir2_leaf_t *leaf; /* leaf structure */
1385 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1386 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1387 xfs_mount_t *mp; /* filesystem mount point */
1388 int needlog; /* need to log data header */
1389 int needscan; /* need to rescan data frees */
1390 xfs_dir2_data_off_t oldbest; /* old value of best free */
1391 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner33363fe2013-04-03 16:11:22 +11001392 struct xfs_dir2_data_free *bf; /* bestfree table */
Dave Chinner24df33b2013-04-12 07:30:21 +10001393 struct xfs_dir2_leaf_entry *ents;
1394 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001396 trace_xfs_dir2_leaf_removename(args);
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /*
1399 * Lookup the leaf entry, get the leaf and data blocks read in.
1400 */
1401 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1402 return error;
1403 }
1404 dp = args->dp;
1405 tp = args->trans;
1406 mp = dp->i_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001407 leaf = lbp->b_addr;
1408 hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001409 xfs_dir3_data_check(dp, dbp);
Dave Chinner24df33b2013-04-12 07:30:21 +10001410 bf = xfs_dir3_data_bestfree_p(hdr);
1411 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1412 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * Point to the leaf entry, use that to point to the data entry.
1415 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001416 lep = &ents[index];
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001417 db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 dep = (xfs_dir2_data_entry_t *)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001419 ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 needscan = needlog = 0;
Dave Chinner33363fe2013-04-03 16:11:22 +11001421 oldbest = be16_to_cpu(bf[0].length);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001422 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
1423 bestsp = xfs_dir2_leaf_bests_p(ltp);
Nathan Scott68b3a102006-03-17 17:27:19 +11001424 ASSERT(be16_to_cpu(bestsp[db]) == oldbest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /*
1426 * Mark the former data entry unused.
1427 */
1428 xfs_dir2_data_make_free(tp, dbp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001429 (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001430 xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 /*
1432 * We just mark the leaf entry stale by putting a null in it.
1433 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001434 leafhdr.stale++;
1435 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1436 xfs_dir3_leaf_log_header(tp, lbp);
1437
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001438 lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR);
Dave Chinner24df33b2013-04-12 07:30:21 +10001439 xfs_dir3_leaf_log_ents(tp, lbp, index, index);
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /*
1442 * Scan the freespace in the data block again if necessary,
1443 * log the data block header if necessary.
1444 */
1445 if (needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001446 xfs_dir2_data_freescan(mp, hdr, &needlog);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (needlog)
1448 xfs_dir2_data_log_header(tp, dbp);
1449 /*
1450 * If the longest freespace in the data block has changed,
1451 * put the new value in the bests table and log that.
1452 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001453 if (be16_to_cpu(bf[0].length) != oldbest) {
1454 bestsp[db] = bf[0].length;
Dave Chinner24df33b2013-04-12 07:30:21 +10001455 xfs_dir3_leaf_log_bests(tp, lbp, db, db);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Dave Chinner33363fe2013-04-03 16:11:22 +11001457 xfs_dir3_data_check(dp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /*
1459 * If the data block is now empty then get rid of the data block.
1460 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001461 if (be16_to_cpu(bf[0].length) ==
1462 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 ASSERT(db != mp->m_dirdatablk);
1464 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1465 /*
1466 * Nope, can't get rid of it because it caused
1467 * allocation of a bmap btree block to do so.
1468 * Just go on, returning success, leaving the
1469 * empty block in place.
1470 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001471 if (error == ENOSPC && args->total == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 error = 0;
Dave Chinner24df33b2013-04-12 07:30:21 +10001473 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return error;
1475 }
1476 dbp = NULL;
1477 /*
1478 * If this is the last data block then compact the
1479 * bests table by getting rid of entries.
1480 */
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001481 if (db == be32_to_cpu(ltp->bestcount) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /*
1483 * Look for the last active entry (i).
1484 */
1485 for (i = db - 1; i > 0; i--) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +02001486 if (bestsp[i] != cpu_to_be16(NULLDATAOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 break;
1488 }
1489 /*
1490 * Copy the table down so inactive entries at the
1491 * end are removed.
1492 */
1493 memmove(&bestsp[db - i], bestsp,
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001494 (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp));
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001495 be32_add_cpu(&ltp->bestcount, -(db - i));
Dave Chinner24df33b2013-04-12 07:30:21 +10001496 xfs_dir3_leaf_log_tail(tp, lbp);
1497 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 } else
Nathan Scott68b3a102006-03-17 17:27:19 +11001499 bestsp[db] = cpu_to_be16(NULLDATAOFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 /*
1502 * If the data block was not the first one, drop it.
1503 */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001504 else if (db != mp->m_dirdatablk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 dbp = NULL;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001506
Dave Chinner24df33b2013-04-12 07:30:21 +10001507 xfs_dir3_leaf_check(mp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /*
1509 * See if we can convert to block form.
1510 */
1511 return xfs_dir2_leaf_to_block(args, lbp, dbp);
1512}
1513
1514/*
1515 * Replace the inode number in a leaf format directory entry.
1516 */
1517int /* error */
1518xfs_dir2_leaf_replace(
1519 xfs_da_args_t *args) /* operation arguments */
1520{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001521 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 xfs_dir2_data_entry_t *dep; /* data block entry */
1523 xfs_inode_t *dp; /* incore directory inode */
1524 int error; /* error return code */
1525 int index; /* index of leaf entry */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001526 struct xfs_buf *lbp; /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 xfs_dir2_leaf_t *leaf; /* leaf structure */
1528 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1529 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001530 struct xfs_dir2_leaf_entry *ents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001532 trace_xfs_dir2_leaf_replace(args);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 /*
1535 * Look up the entry.
1536 */
1537 if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) {
1538 return error;
1539 }
1540 dp = args->dp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001541 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001542 ents = xfs_dir3_leaf_ents_p(leaf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * Point to the leaf entry, get data address from it.
1545 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001546 lep = &ents[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * Point to the data entry.
1549 */
1550 dep = (xfs_dir2_data_entry_t *)
Dave Chinner1d9025e2012-06-22 18:50:14 +10001551 ((char *)dbp->b_addr +
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001552 xfs_dir2_dataptr_to_off(dp->i_mount, be32_to_cpu(lep->address)));
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001553 ASSERT(args->inumber != be64_to_cpu(dep->inumber));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * Put the new inode number in, log it.
1556 */
Christoph Hellwigff9901c2006-06-09 14:48:37 +10001557 dep->inumber = cpu_to_be64(args->inumber);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 tp = args->trans;
1559 xfs_dir2_data_log_entry(tp, dbp, dep);
Dave Chinner24df33b2013-04-12 07:30:21 +10001560 xfs_dir3_leaf_check(dp->i_mount, lbp);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001561 xfs_trans_brelse(tp, lbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return 0;
1563}
1564
1565/*
1566 * Return index in the leaf block (lbp) which is either the first
1567 * one with this hash value, or if there are none, the insert point
1568 * for that hash value.
1569 */
1570int /* index value */
1571xfs_dir2_leaf_search_hash(
1572 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001573 struct xfs_buf *lbp) /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 xfs_dahash_t hash=0; /* hash from this entry */
1576 xfs_dahash_t hashwant; /* hash value looking for */
1577 int high; /* high leaf index */
1578 int low; /* low leaf index */
1579 xfs_dir2_leaf_t *leaf; /* leaf structure */
1580 xfs_dir2_leaf_entry_t *lep; /* leaf entry */
1581 int mid=0; /* current leaf index */
Dave Chinner24df33b2013-04-12 07:30:21 +10001582 struct xfs_dir2_leaf_entry *ents;
1583 struct xfs_dir3_icleaf_hdr leafhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dave Chinner1d9025e2012-06-22 18:50:14 +10001585 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001586 ents = xfs_dir3_leaf_ents_p(leaf);
1587 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589#ifndef __KERNEL__
Dave Chinner24df33b2013-04-12 07:30:21 +10001590 if (!leafhdr.count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return 0;
1592#endif
1593 /*
1594 * Note, the table cannot be empty, so we have to go through the loop.
1595 * Binary search the leaf entries looking for our hash value.
1596 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001597 for (lep = ents, low = 0, high = leafhdr.count - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 hashwant = args->hashval;
1599 low <= high; ) {
1600 mid = (low + high) >> 1;
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001601 if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 break;
1603 if (hash < hashwant)
1604 low = mid + 1;
1605 else
1606 high = mid - 1;
1607 }
1608 /*
1609 * Found one, back up through all the equal hash values.
1610 */
1611 if (hash == hashwant) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +11001612 while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 mid--;
1614 }
1615 }
1616 /*
1617 * Need to point to an entry higher than ours.
1618 */
1619 else if (hash < hashwant)
1620 mid++;
1621 return mid;
1622}
1623
1624/*
1625 * Trim off a trailing data block. We know it's empty since the leaf
1626 * freespace table says so.
1627 */
1628int /* error */
1629xfs_dir2_leaf_trim_data(
1630 xfs_da_args_t *args, /* operation arguments */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001631 struct xfs_buf *lbp, /* leaf buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 xfs_dir2_db_t db) /* data block number */
1633{
Nathan Scott68b3a102006-03-17 17:27:19 +11001634 __be16 *bestsp; /* leaf bests table */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001635 struct xfs_buf *dbp; /* data block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 xfs_inode_t *dp; /* incore directory inode */
1637 int error; /* error return value */
1638 xfs_dir2_leaf_t *leaf; /* leaf structure */
1639 xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */
1640 xfs_mount_t *mp; /* filesystem mount point */
1641 xfs_trans_t *tp; /* transaction pointer */
1642
1643 dp = args->dp;
1644 mp = dp->i_mount;
1645 tp = args->trans;
1646 /*
1647 * Read the offending data block. We need its buffer.
1648 */
Dave Chinner33363fe2013-04-03 16:11:22 +11001649 error = xfs_dir3_data_read(tp, dp, xfs_dir2_db_to_da(mp, db), -1, &dbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001650 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Dave Chinner1d9025e2012-06-22 18:50:14 +10001653 leaf = lbp->b_addr;
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001654 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001655
1656#ifdef DEBUG
1657{
Dave Chinner1d9025e2012-06-22 18:50:14 +10001658 struct xfs_dir2_data_hdr *hdr = dbp->b_addr;
Dave Chinner33363fe2013-04-03 16:11:22 +11001659 struct xfs_dir2_data_free *bf = xfs_dir3_data_bestfree_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001660
Dave Chinner33363fe2013-04-03 16:11:22 +11001661 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
1662 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC));
1663 ASSERT(be16_to_cpu(bf[0].length) ==
1664 mp->m_dirblksize - xfs_dir3_data_entry_offset(hdr));
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001665 ASSERT(db == be32_to_cpu(ltp->bestcount) - 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001666}
1667#endif
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 /*
1670 * Get rid of the data block.
1671 */
1672 if ((error = xfs_dir2_shrink_inode(args, db, dbp))) {
1673 ASSERT(error != ENOSPC);
Dave Chinner1d9025e2012-06-22 18:50:14 +10001674 xfs_trans_brelse(tp, dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return error;
1676 }
1677 /*
1678 * Eliminate the last bests entry from the table.
1679 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001680 bestsp = xfs_dir2_leaf_bests_p(ltp);
Marcin Slusarz413d57c2008-02-13 15:03:29 -08001681 be32_add_cpu(&ltp->bestcount, -1);
Nathan Scottafbcb3f2006-03-17 17:27:28 +11001682 memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp));
Dave Chinner24df33b2013-04-12 07:30:21 +10001683 xfs_dir3_leaf_log_tail(tp, lbp);
1684 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 return 0;
1686}
1687
Christoph Hellwig22823962011-07-08 14:35:53 +02001688static inline size_t
Dave Chinner24df33b2013-04-12 07:30:21 +10001689xfs_dir3_leaf_size(
1690 struct xfs_dir3_icleaf_hdr *hdr,
Christoph Hellwig22823962011-07-08 14:35:53 +02001691 int counts)
1692{
Dave Chinner24df33b2013-04-12 07:30:21 +10001693 int entries;
1694 int hdrsize;
Christoph Hellwig22823962011-07-08 14:35:53 +02001695
Dave Chinner24df33b2013-04-12 07:30:21 +10001696 entries = hdr->count - hdr->stale;
1697 if (hdr->magic == XFS_DIR2_LEAF1_MAGIC ||
1698 hdr->magic == XFS_DIR2_LEAFN_MAGIC)
1699 hdrsize = sizeof(struct xfs_dir2_leaf_hdr);
1700 else
1701 hdrsize = sizeof(struct xfs_dir3_leaf_hdr);
1702
1703 return hdrsize + entries * sizeof(xfs_dir2_leaf_entry_t)
1704 + counts * sizeof(xfs_dir2_data_off_t)
1705 + sizeof(xfs_dir2_leaf_tail_t);
Christoph Hellwig22823962011-07-08 14:35:53 +02001706}
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
1709 * Convert node form directory to leaf form directory.
1710 * The root of the node form dir needs to already be a LEAFN block.
1711 * Just return if we can't do anything.
1712 */
1713int /* error */
1714xfs_dir2_node_to_leaf(
1715 xfs_da_state_t *state) /* directory operation state */
1716{
1717 xfs_da_args_t *args; /* operation arguments */
1718 xfs_inode_t *dp; /* incore directory inode */
1719 int error; /* error return code */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001720 struct xfs_buf *fbp; /* buffer for freespace block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 xfs_fileoff_t fo; /* freespace file offset */
1722 xfs_dir2_free_t *free; /* freespace structure */
Dave Chinner1d9025e2012-06-22 18:50:14 +10001723 struct xfs_buf *lbp; /* buffer for leaf block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */
1725 xfs_dir2_leaf_t *leaf; /* leaf structure */
1726 xfs_mount_t *mp; /* filesystem mount point */
1727 int rval; /* successful free trim? */
1728 xfs_trans_t *tp; /* transaction pointer */
Dave Chinner24df33b2013-04-12 07:30:21 +10001729 struct xfs_dir3_icleaf_hdr leafhdr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001730 struct xfs_dir3_icfree_hdr freehdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /*
1733 * There's more than a leaf level in the btree, so there must
1734 * be multiple leafn blocks. Give up.
1735 */
1736 if (state->path.active > 1)
1737 return 0;
1738 args = state->args;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001739
1740 trace_xfs_dir2_node_to_leaf(args);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 mp = state->mp;
1743 dp = args->dp;
1744 tp = args->trans;
1745 /*
1746 * Get the last offset in the file.
1747 */
1748 if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) {
1749 return error;
1750 }
1751 fo -= mp->m_dirblkfsbs;
1752 /*
1753 * If there are freespace blocks other than the first one,
1754 * take this opportunity to remove trailing empty freespace blocks
1755 * that may have been left behind during no-space-reservation
1756 * operations.
1757 */
1758 while (fo > mp->m_dirfreeblk) {
1759 if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) {
1760 return error;
1761 }
1762 if (rval)
1763 fo -= mp->m_dirblkfsbs;
1764 else
1765 return 0;
1766 }
1767 /*
1768 * Now find the block just before the freespace block.
1769 */
1770 if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) {
1771 return error;
1772 }
1773 /*
1774 * If it's not the single leaf block, give up.
1775 */
1776 if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize)
1777 return 0;
1778 lbp = state->path.blk[0].bp;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001779 leaf = lbp->b_addr;
Dave Chinner24df33b2013-04-12 07:30:21 +10001780 xfs_dir3_leaf_hdr_from_disk(&leafhdr, leaf);
1781
1782 ASSERT(leafhdr.magic == XFS_DIR2_LEAFN_MAGIC ||
1783 leafhdr.magic == XFS_DIR3_LEAFN_MAGIC);
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /*
1786 * Read the freespace block.
1787 */
Dave Chinner20252072012-11-12 22:54:13 +11001788 error = xfs_dir2_free_read(tp, dp, mp->m_dirfreeblk, &fbp);
Dave Chinner4bb20a82012-11-12 22:54:10 +11001789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return error;
Dave Chinner1d9025e2012-06-22 18:50:14 +10001791 free = fbp->b_addr;
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001792 xfs_dir3_free_hdr_from_disk(&freehdr, free);
1793
1794 ASSERT(!freehdr.firstdb);
Christoph Hellwig22823962011-07-08 14:35:53 +02001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /*
1797 * Now see if the leafn and free data will fit in a leaf1.
1798 * If not, release the buffer and give up.
1799 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001800 if (xfs_dir3_leaf_size(&leafhdr, freehdr.nvalid) > mp->m_dirblksize) {
Dave Chinner1d9025e2012-06-22 18:50:14 +10001801 xfs_trans_brelse(tp, fbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
1803 }
Christoph Hellwig22823962011-07-08 14:35:53 +02001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /*
1806 * If the leaf has any stale entries in it, compress them out.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Dave Chinner24df33b2013-04-12 07:30:21 +10001808 if (leafhdr.stale)
1809 xfs_dir3_leaf_compact(args, &leafhdr, lbp);
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001810
Dave Chinner24df33b2013-04-12 07:30:21 +10001811 lbp->b_ops = &xfs_dir3_leaf1_buf_ops;
Dave Chinner61fe1352013-04-03 16:11:30 +11001812 xfs_trans_buf_set_type(tp, lbp, XFS_BLFT_DIR_LEAF1_BUF);
Dave Chinner24df33b2013-04-12 07:30:21 +10001813 leafhdr.magic = (leafhdr.magic == XFS_DIR2_LEAFN_MAGIC)
1814 ? XFS_DIR2_LEAF1_MAGIC
1815 : XFS_DIR3_LEAF1_MAGIC;
Dave Chinnerb0f539d2012-11-14 17:53:49 +11001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /*
1818 * Set up the leaf tail from the freespace block.
1819 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +10001820 ltp = xfs_dir2_leaf_tail_p(mp, leaf);
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001821 ltp->bestcount = cpu_to_be32(freehdr.nvalid);
Dave Chinner24df33b2013-04-12 07:30:21 +10001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * Set up the leaf bests table.
1825 */
Dave Chinnercbc8adf2013-04-03 16:11:21 +11001826 memcpy(xfs_dir2_leaf_bests_p(ltp), xfs_dir3_free_bests_p(mp, free),
1827 freehdr.nvalid * sizeof(xfs_dir2_data_off_t));
Dave Chinner24df33b2013-04-12 07:30:21 +10001828
1829 xfs_dir3_leaf_hdr_to_disk(leaf, &leafhdr);
1830 xfs_dir3_leaf_log_header(tp, lbp);
1831 xfs_dir3_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1);
1832 xfs_dir3_leaf_log_tail(tp, lbp);
1833 xfs_dir3_leaf_check(mp, lbp);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /*
1836 * Get rid of the freespace block.
1837 */
1838 error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp);
1839 if (error) {
1840 /*
1841 * This can't fail here because it can only happen when
1842 * punching out the middle of an extent, and this is an
1843 * isolated block.
1844 */
1845 ASSERT(error != ENOSPC);
1846 return error;
1847 }
1848 fbp = NULL;
1849 /*
1850 * Now see if we can convert the single-leaf directory
1851 * down to a block form directory.
1852 * This routine always kills the dabuf for the leaf, so
1853 * eliminate it from the path.
1854 */
1855 error = xfs_dir2_leaf_to_block(args, lbp, NULL);
1856 state->path.blk[0].bp = NULL;
1857 return error;
1858}