blob: 78320df3743f66ace56ba3d0913861cc21e3ab09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11003 * Copyright (c) 2013 Red Hat, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Nathan Scott7b718762005-11-02 14:58:39 +11006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * published by the Free Software Foundation.
9 *
Nathan Scott7b718762005-11-02 14:58:39 +110010 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Nathan Scott7b718762005-11-02 14:58:39 +110015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
23#include "xfs_trans.h"
24#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100025#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_dinode.h"
30#include "xfs_inode.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020031#include "xfs_dir2_format.h"
32#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_error.h"
Dave Chinner33363fe2013-04-03 16:11:22 +110034#include "xfs_buf_item.h"
35#include "xfs_cksum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Christoph Hellwigc2066e22011-07-08 14:35:38 +020037STATIC xfs_dir2_data_free_t *
38xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
41 * Check the consistency of the data block.
42 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110043 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Dave Chinner82025d72012-11-12 22:54:12 +110045int
Dave Chinner33363fe2013-04-03 16:11:22 +110046__xfs_dir3_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100047 struct xfs_inode *dp, /* incore inode pointer */
48 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
51 xfs_dir2_data_free_t *bf; /* bestfree table */
52 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
53 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020054 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 xfs_dir2_data_entry_t *dep; /* data entry */
56 xfs_dir2_data_free_t *dfp; /* bestfree entry */
57 xfs_dir2_data_unused_t *dup; /* unused entry */
58 char *endp; /* end of useful data */
59 int freeseen; /* mask of bestfrees seen */
60 xfs_dahash_t hash; /* hash of current name */
61 int i; /* leaf index */
62 int lastfree; /* last entry was unused */
63 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
64 xfs_mount_t *mp; /* filesystem mount point */
65 char *p; /* current data position */
66 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100067 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dave Chinner82025d72012-11-12 22:54:12 +110069 mp = bp->b_target->bt_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +100070 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110071 bf = xfs_dir3_data_bestfree_p(hdr);
72 p = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020073
Dave Chinner82025d72012-11-12 22:54:12 +110074 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110075 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110076 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020077 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100078 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110080 break;
Dave Chinner33363fe2013-04-03 16:11:22 +110081 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110082 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020083 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110084 break;
85 default:
86 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
87 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020088 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 count = lastfree = freeseen = 0;
91 /*
92 * Account for zero bestfree entries.
93 */
94 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110095 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 freeseen |= 1 << 0;
97 }
98 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110099 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 freeseen |= 1 << 1;
101 }
102 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100103 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 freeseen |= 1 << 2;
105 }
Dave Chinner82025d72012-11-12 22:54:12 +1100106
107 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
108 be16_to_cpu(bf[1].length));
109 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
110 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /*
112 * Loop over the data/unused entries.
113 */
114 while (p < endp) {
115 dup = (xfs_dir2_data_unused_t *)p;
116 /*
117 * If it's unused, look for the space in the bestfree table.
118 * If we find it, account for that, else make sure it
119 * doesn't need to be there.
120 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100121 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100122 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
123 XFS_WANT_CORRUPTED_RETURN(
124 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
125 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200126 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (dfp) {
128 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100129 XFS_WANT_CORRUPTED_RETURN(
130 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100132 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100133 XFS_WANT_CORRUPTED_RETURN(
134 be16_to_cpu(dup->length) <=
135 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100136 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100137 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 lastfree = 1;
139 continue;
140 }
141 /*
142 * It's a real entry. Validate the fields.
143 * If this is a block directory then make sure it's
144 * in the leaf section of the block.
145 * The linear search is crude but this is DEBUG code.
146 */
147 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100148 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
149 XFS_WANT_CORRUPTED_RETURN(
150 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
151 XFS_WANT_CORRUPTED_RETURN(
152 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
153 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 count++;
155 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100156 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
157 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000158 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200160 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000161 name.name = dep->name;
162 name.len = dep->namelen;
163 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100164 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100165 if (be32_to_cpu(lep[i].address) == addr &&
166 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
Dave Chinner82025d72012-11-12 22:54:12 +1100169 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000171 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 /*
174 * Need to have seen all the entries and all the bestfree slots.
175 */
Dave Chinner82025d72012-11-12 22:54:12 +1100176 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100177 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
178 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100179 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200180 if (lep[i].address ==
181 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 stale++;
183 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100184 XFS_WANT_CORRUPTED_RETURN(
185 be32_to_cpu(lep[i].hashval) >=
186 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Dave Chinner82025d72012-11-12 22:54:12 +1100188 XFS_WANT_CORRUPTED_RETURN(count ==
189 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
190 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Dave Chinner82025d72012-11-12 22:54:12 +1100192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Dave Chinner33363fe2013-04-03 16:11:22 +1100195static bool
196xfs_dir3_data_verify(
Dave Chinnere4813572012-11-12 22:54:14 +1100197 struct xfs_buf *bp)
198{
199 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner33363fe2013-04-03 16:11:22 +1100200 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
Dave Chinnere4813572012-11-12 22:54:14 +1100201
Dave Chinner33363fe2013-04-03 16:11:22 +1100202 if (xfs_sb_version_hascrc(&mp->m_sb)) {
203 if (hdr3->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC))
204 return false;
205 if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_uuid))
206 return false;
207 if (be64_to_cpu(hdr3->blkno) != bp->b_bn)
208 return false;
209 } else {
210 if (hdr3->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC))
211 return false;
Dave Chinnere4813572012-11-12 22:54:14 +1100212 }
Dave Chinner33363fe2013-04-03 16:11:22 +1100213 if (__xfs_dir3_data_check(NULL, bp))
214 return false;
215 return true;
Dave Chinner612cfbf2012-11-14 17:52:32 +1100216}
Dave Chinnere4813572012-11-12 22:54:14 +1100217
Dave Chinner1813dd62012-11-14 17:54:40 +1100218/*
219 * Readahead of the first block of the directory when it is opened is completely
220 * oblivious to the format of the directory. Hence we can either get a block
221 * format buffer or a data format buffer on readahead.
222 */
223static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100224xfs_dir3_data_reada_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100225 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100226{
Dave Chinner1813dd62012-11-14 17:54:40 +1100227 struct xfs_mount *mp = bp->b_target->bt_mount;
228 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
229
230 switch (hdr->magic) {
231 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100232 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
233 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100234 bp->b_ops->verify_read(bp);
235 return;
236 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Dave Chinner33363fe2013-04-03 16:11:22 +1100237 case cpu_to_be32(XFS_DIR3_DATA_MAGIC):
238 xfs_dir3_data_verify(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +1100239 return;
240 default:
241 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
242 xfs_buf_ioerror(bp, EFSCORRUPTED);
243 break;
244 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100245}
246
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100247static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100248xfs_dir3_data_read_verify(
Dave Chinner612cfbf2012-11-14 17:52:32 +1100249 struct xfs_buf *bp)
250{
Dave Chinner33363fe2013-04-03 16:11:22 +1100251 struct xfs_mount *mp = bp->b_target->bt_mount;
252
253 if ((xfs_sb_version_hascrc(&mp->m_sb) &&
254 !xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
255 XFS_DIR3_DATA_CRC_OFF)) ||
256 !xfs_dir3_data_verify(bp)) {
257 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
258 xfs_buf_ioerror(bp, EFSCORRUPTED);
259 }
Dave Chinnere4813572012-11-12 22:54:14 +1100260}
261
Dave Chinner1813dd62012-11-14 17:54:40 +1100262static void
Dave Chinner33363fe2013-04-03 16:11:22 +1100263xfs_dir3_data_write_verify(
Dave Chinner1813dd62012-11-14 17:54:40 +1100264 struct xfs_buf *bp)
265{
Dave Chinner33363fe2013-04-03 16:11:22 +1100266 struct xfs_mount *mp = bp->b_target->bt_mount;
267 struct xfs_buf_log_item *bip = bp->b_fspriv;
268 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
269
270 if (!xfs_dir3_data_verify(bp)) {
271 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
272 xfs_buf_ioerror(bp, EFSCORRUPTED);
273 return;
274 }
275
276 if (!xfs_sb_version_hascrc(&mp->m_sb))
277 return;
278
279 if (bip)
280 hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn);
281
282 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), XFS_DIR3_DATA_CRC_OFF);
Dave Chinner1813dd62012-11-14 17:54:40 +1100283}
284
Dave Chinner33363fe2013-04-03 16:11:22 +1100285const struct xfs_buf_ops xfs_dir3_data_buf_ops = {
286 .verify_read = xfs_dir3_data_read_verify,
287 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100288};
289
Dave Chinner33363fe2013-04-03 16:11:22 +1100290static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = {
291 .verify_read = xfs_dir3_data_reada_verify,
292 .verify_write = xfs_dir3_data_write_verify,
Dave Chinner1813dd62012-11-14 17:54:40 +1100293};
294
Dave Chinner612cfbf2012-11-14 17:52:32 +1100295
Dave Chinnere4813572012-11-12 22:54:14 +1100296int
Dave Chinner33363fe2013-04-03 16:11:22 +1100297xfs_dir3_data_read(
Dave Chinnere4813572012-11-12 22:54:14 +1100298 struct xfs_trans *tp,
299 struct xfs_inode *dp,
300 xfs_dablk_t bno,
301 xfs_daddr_t mapped_bno,
302 struct xfs_buf **bpp)
303{
304 return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner33363fe2013-04-03 16:11:22 +1100305 XFS_DATA_FORK, &xfs_dir3_data_buf_ops);
Dave Chinnere4813572012-11-12 22:54:14 +1100306}
307
Dave Chinnerda6958c2012-11-12 22:54:18 +1100308int
Dave Chinner33363fe2013-04-03 16:11:22 +1100309xfs_dir3_data_readahead(
Dave Chinnerda6958c2012-11-12 22:54:18 +1100310 struct xfs_trans *tp,
311 struct xfs_inode *dp,
312 xfs_dablk_t bno,
313 xfs_daddr_t mapped_bno)
314{
315 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner33363fe2013-04-03 16:11:22 +1100316 XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
320 * Given a data block and an unused entry from that block,
321 * return the bestfree entry if any that corresponds to it.
322 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200323STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200325 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 xfs_dir2_data_unused_t *dup) /* data unused entry */
327{
328 xfs_dir2_data_free_t *dfp; /* bestfree entry */
329 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100330 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#if defined(DEBUG) && defined(__KERNEL__)
332 int matched; /* matched the value */
333 int seenzero; /* saw a 0 bestfree entry */
334#endif
335
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200336 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100337 bf = xfs_dir3_data_bestfree_p(hdr);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#if defined(DEBUG) && defined(__KERNEL__)
340 /*
341 * Validate some consistency in the bestfree table.
342 * Check order, non-overlapping entries, and if we find the
343 * one we're looking for it has to be exact.
344 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200345 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100346 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100347 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
348 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
349 for (dfp = &bf[0], seenzero = matched = 0;
350 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dfp++) {
352 if (!dfp->offset) {
353 ASSERT(!dfp->length);
354 seenzero = 1;
355 continue;
356 }
357 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100358 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100360 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100361 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100362 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100364 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100365 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100366 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100367 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369#endif
370 /*
371 * If this is smaller than the smallest bestfree entry,
372 * it can't be there since they're sorted.
373 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100374 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100375 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return NULL;
377 /*
378 * Look at the three bestfree entries for our guy.
379 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100380 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!dfp->offset)
382 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100383 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return dfp;
385 }
386 /*
387 * Didn't find it. This only happens if there are duplicate lengths.
388 */
389 return NULL;
390}
391
392/*
393 * Insert an unused-space entry into the bestfree table.
394 */
395xfs_dir2_data_free_t * /* entry inserted */
396xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200397 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_dir2_data_unused_t *dup, /* unused space */
399 int *loghead) /* log the data header (out) */
400{
401 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
402 xfs_dir2_data_free_t new; /* new bestfree entry */
403
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200404 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100405 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
406 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
407 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
408
409 dfp = xfs_dir3_data_bestfree_p(hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100410 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200411 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /*
414 * Insert at position 0, 1, or 2; or not at all.
415 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100416 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 dfp[2] = dfp[1];
418 dfp[1] = dfp[0];
419 dfp[0] = new;
420 *loghead = 1;
421 return &dfp[0];
422 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100423 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dfp[2] = dfp[1];
425 dfp[1] = new;
426 *loghead = 1;
427 return &dfp[1];
428 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100429 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 dfp[2] = new;
431 *loghead = 1;
432 return &dfp[2];
433 }
434 return NULL;
435}
436
437/*
438 * Remove a bestfree entry from the table.
439 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000440STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200442 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
444 int *loghead) /* out: log data header */
445{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100446 struct xfs_dir2_data_free *bf;
447
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200448 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100449 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
450 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
451 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /*
454 * It's the first entry, slide the next 2 up.
455 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100456 bf = xfs_dir3_data_bestfree_p(hdr);
457 if (dfp == &bf[0]) {
458 bf[0] = bf[1];
459 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 /*
462 * It's the second entry, slide the 3rd entry up.
463 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100464 else if (dfp == &bf[1])
465 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Must be the last entry.
468 */
469 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100470 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 /*
472 * Clear the 3rd entry, must be zero now.
473 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100474 bf[2].length = 0;
475 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *loghead = 1;
477}
478
479/*
480 * Given a data block, reconstruct its bestfree map.
481 */
482void
483xfs_dir2_data_freescan(
484 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200485 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000486 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 xfs_dir2_block_tail_t *btp; /* block tail */
489 xfs_dir2_data_entry_t *dep; /* active data entry */
490 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100491 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 char *endp; /* end of block's data */
493 char *p; /* current entry pointer */
494
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200495 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100496 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100497 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
498 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Start by clearing the table.
502 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100503 bf = xfs_dir3_data_bestfree_p(hdr);
504 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *loghead = 1;
506 /*
507 * Set up pointers.
508 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100509 p = (char *)xfs_dir3_data_entry_p(hdr);
510 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
511 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200512 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000513 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200515 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /*
517 * Loop over the block's entries.
518 */
519 while (p < endp) {
520 dup = (xfs_dir2_data_unused_t *)p;
521 /*
522 * If it's a free entry, insert it.
523 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100524 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200525 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000526 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200527 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100528 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 /*
531 * For active entries, check their tags and skip them.
532 */
533 else {
534 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200535 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000536 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
537 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 }
540}
541
542/*
543 * Initialize a data block at the given block number in the directory.
544 * Give back the buffer for the created block.
545 */
546int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100547xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 xfs_da_args_t *args, /* directory operation args */
549 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000550 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000552 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200553 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 xfs_inode_t *dp; /* incore directory inode */
555 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100556 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 int error; /* error return value */
558 int i; /* bestfree index */
559 xfs_mount_t *mp; /* filesystem mount point */
560 xfs_trans_t *tp; /* transaction pointer */
561 int t; /* temp */
562
563 dp = args->dp;
564 mp = dp->i_mount;
565 tp = args->trans;
566 /*
567 * Get the buffer set up for the block.
568 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000569 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100571 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return error;
Dave Chinner33363fe2013-04-03 16:11:22 +1100573 bp->b_ops = &xfs_dir3_data_buf_ops;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /*
576 * Initialize the header.
577 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000578 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100579 if (xfs_sb_version_hascrc(&mp->m_sb)) {
580 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
581
582 memset(hdr3, 0, sizeof(*hdr3));
583 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
584 hdr3->blkno = cpu_to_be64(bp->b_bn);
585 hdr3->owner = cpu_to_be64(dp->i_ino);
586 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
587
588 } else
589 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
590
591 bf = xfs_dir3_data_bestfree_p(hdr);
592 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100594 bf[i].length = 0;
595 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
599 * Set up an unused entry for the block's body.
600 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100601 dup = xfs_dir3_data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100602 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100604 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
605 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100606 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200607 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Log it and return it.
610 */
611 xfs_dir2_data_log_header(tp, bp);
612 xfs_dir2_data_log_unused(tp, bp, dup);
613 *bpp = bp;
614 return 0;
615}
616
617/*
618 * Log an active data entry from the block.
619 */
620void
621xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000622 struct xfs_trans *tp,
623 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 xfs_dir2_data_entry_t *dep) /* data entry pointer */
625{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000626 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200628 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100629 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100630 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
631 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200632
Dave Chinner1d9025e2012-06-22 18:50:14 +1000633 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000634 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200635 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
639 * Log a data block header.
640 */
641void
642xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000643 struct xfs_trans *tp,
644 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000646 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200648 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100649 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100650 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
651 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200652
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100653 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * Log a data unused entry.
658 */
659void
660xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000661 struct xfs_trans *tp,
662 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_dir2_data_unused_t *dup) /* data unused pointer */
664{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000665 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200667 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100668 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100669 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
670 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /*
673 * Log the first part of the unused entry.
674 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000675 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200677 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /*
679 * Log the end (tag) of the unused entry.
680 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000681 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200682 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
683 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 sizeof(xfs_dir2_data_off_t) - 1));
685}
686
687/*
688 * Make a byte range in the data block unused.
689 * Its current contents are unimportant.
690 */
691void
692xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000693 struct xfs_trans *tp,
694 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 xfs_dir2_data_aoff_t offset, /* starting byte offset */
696 xfs_dir2_data_aoff_t len, /* length in bytes */
697 int *needlogp, /* out: log header */
698 int *needscanp) /* out: regen bestfree */
699{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200700 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
702 char *endptr; /* end of data area */
703 xfs_mount_t *mp; /* filesystem mount point */
704 int needscan; /* need to regen bestfree */
705 xfs_dir2_data_unused_t *newdup; /* new unused entry */
706 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
707 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100708 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000711 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * Figure out where the end of the data area is.
715 */
Dave Chinner33363fe2013-04-03 16:11:22 +1100716 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
717 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200718 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 else {
720 xfs_dir2_block_tail_t *btp; /* block tail */
721
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100722 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
723 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200724 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000725 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727 /*
728 * If this isn't the start of the block, then back up to
729 * the previous entry and see if it's free.
730 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100731 if (offset > xfs_dir3_data_entry_offset(hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100732 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200734 tagp = (__be16 *)((char *)hdr + offset) - 1;
735 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100736 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 prevdup = NULL;
738 } else
739 prevdup = NULL;
740 /*
741 * If this isn't the end of the block, see if the entry after
742 * us is free.
743 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200744 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200746 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100747 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 postdup = NULL;
749 } else
750 postdup = NULL;
751 ASSERT(*needscanp == 0);
752 needscan = 0;
753 /*
754 * Previous and following entries are both free,
755 * merge everything into a single free entry.
756 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100757 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (prevdup && postdup) {
759 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
760
761 /*
762 * See if prevdup and/or postdup are in bestfree table.
763 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200764 dfp = xfs_dir2_data_freefind(hdr, prevdup);
765 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /*
767 * We need a rescan unless there are exactly 2 free entries
768 * namely our two. Then we know what's happening, otherwise
769 * since the third bestfree is there, there might be more
770 * entries.
771 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100772 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 /*
774 * Fix up the new big freespace.
775 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800776 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000777 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200778 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 xfs_dir2_data_log_unused(tp, bp, prevdup);
780 if (!needscan) {
781 /*
782 * Has to be the case that entries 0 and 1 are
783 * dfp and dfp2 (don't know which is which), and
784 * entry 2 is empty.
785 * Remove entry 1 first then entry 0.
786 */
787 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100788 if (dfp == &bf[1]) {
789 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100791 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200793 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
794 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Now insert the new entry.
797 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200798 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100799 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100800 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ASSERT(!dfp[1].length);
802 ASSERT(!dfp[2].length);
803 }
804 }
805 /*
806 * The entry before us is free, merge with it.
807 */
808 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200809 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800810 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000811 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200812 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xfs_dir2_data_log_unused(tp, bp, prevdup);
814 /*
815 * If the previous entry was in the table, the new entry
816 * is longer, so it will be in the table too. Remove
817 * the old one and add the new one.
818 */
819 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200820 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
821 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 /*
824 * Otherwise we need a scan if the new entry is big enough.
825 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100826 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100827 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100828 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831 /*
832 * The following entry is free, merge with it.
833 */
834 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200835 dfp = xfs_dir2_data_freefind(hdr, postdup);
836 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100837 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
838 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000839 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200840 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 xfs_dir2_data_log_unused(tp, bp, newdup);
842 /*
843 * If the following entry was in the table, the new entry
844 * is longer, so it will be in the table too. Remove
845 * the old one and add the new one.
846 */
847 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200848 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
849 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851 /*
852 * Otherwise we need a scan if the new entry is big enough.
853 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100854 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100855 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100856 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 /*
860 * Neither neighbor is free. Make a new entry.
861 */
862 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200863 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100864 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
865 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000866 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200867 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200869 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 *needscanp = needscan;
872}
873
874/*
875 * Take a byte range out of an existing unused space and make it un-free.
876 */
877void
878xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000879 struct xfs_trans *tp,
880 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 xfs_dir2_data_unused_t *dup, /* unused entry */
882 xfs_dir2_data_aoff_t offset, /* starting offset to use */
883 xfs_dir2_data_aoff_t len, /* length to use */
884 int *needlogp, /* out: need to log header */
885 int *needscanp) /* out: need regen bestfree */
886{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200887 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
889 int matchback; /* matches end of freespace */
890 int matchfront; /* matches start of freespace */
891 int needscan; /* need to regen bestfree */
892 xfs_dir2_data_unused_t *newdup; /* new unused entry */
893 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
894 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100895 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Chinner1d9025e2012-06-22 18:50:14 +1000897 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200898 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinner33363fe2013-04-03 16:11:22 +1100899 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100900 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
901 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100902 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200903 ASSERT(offset >= (char *)dup - (char *)hdr);
904 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
905 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Look up the entry in the bestfree table.
908 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200909 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100910 oldlen = be16_to_cpu(dup->length);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100911 bf = xfs_dir3_data_bestfree_p(hdr);
912 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /*
914 * Check for alignment with front and back of the entry.
915 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200916 matchfront = (char *)dup - (char *)hdr == offset;
917 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 ASSERT(*needscanp == 0);
919 needscan = 0;
920 /*
921 * If we matched it exactly we just need to get rid of it from
922 * the bestfree table.
923 */
924 if (matchfront && matchback) {
925 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100926 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200928 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 }
931 /*
932 * We match the first part of the entry.
933 * Make a new entry with the remaining freespace.
934 */
935 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200936 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100937 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
938 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000939 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200940 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 xfs_dir2_data_log_unused(tp, bp, newdup);
942 /*
943 * If it was in the table, remove it and add the new one.
944 */
945 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200946 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
947 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100949 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200950 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 /*
952 * If we got inserted at the last slot,
953 * that means we don't know if there was a better
954 * choice for the last slot, or not. Rescan.
955 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100956 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958 }
959 /*
960 * We match the last part of the entry.
961 * Trim the allocated space off the tail of the entry.
962 */
963 else if (matchback) {
964 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200965 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000966 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200967 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 xfs_dir2_data_log_unused(tp, bp, newdup);
969 /*
970 * If it was in the table, remove it and add the new one.
971 */
972 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200973 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
974 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100976 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200977 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * If we got inserted at the last slot,
980 * that means we don't know if there was a better
981 * choice for the last slot, or not. Rescan.
982 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100983 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 }
986 /*
987 * Poking out the middle of an entry.
988 * Make two new entries.
989 */
990 else {
991 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200992 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000993 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200994 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200996 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100997 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
998 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000999 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001000 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 xfs_dir2_data_log_unused(tp, bp, newdup2);
1002 /*
1003 * If the old entry was in the table, we need to scan
1004 * if the 3rd entry was valid, since these entries
1005 * are smaller than the old one.
1006 * If we don't need to scan that means there were 1 or 2
1007 * entries in the table, and removing the old and adding
1008 * the 2 new will work.
1009 */
1010 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +11001011 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +02001013 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
1014 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
1015 xfs_dir2_data_freeinsert(hdr, newdup2,
1016 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 }
1019 }
1020 *needscanp = needscan;
1021}