blob: 8484ec4848eab24c97ed3b9394dee530c50edbd3 [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"
34
Christoph Hellwigc2066e22011-07-08 14:35:38 +020035STATIC xfs_dir2_data_free_t *
36xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * Check the consistency of the data block.
40 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110041 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
Dave Chinner82025d72012-11-12 22:54:12 +110043int
44__xfs_dir2_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100045 struct xfs_inode *dp, /* incore inode pointer */
46 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
49 xfs_dir2_data_free_t *bf; /* bestfree table */
50 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
51 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020052 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 xfs_dir2_data_entry_t *dep; /* data entry */
54 xfs_dir2_data_free_t *dfp; /* bestfree entry */
55 xfs_dir2_data_unused_t *dup; /* unused entry */
56 char *endp; /* end of useful data */
57 int freeseen; /* mask of bestfrees seen */
58 xfs_dahash_t hash; /* hash of current name */
59 int i; /* leaf index */
60 int lastfree; /* last entry was unused */
61 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
62 xfs_mount_t *mp; /* filesystem mount point */
63 char *p; /* current data position */
64 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100065 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Chinner82025d72012-11-12 22:54:12 +110067 mp = bp->b_target->bt_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +100068 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110069 bf = xfs_dir3_data_bestfree_p(hdr);
70 p = (char *)xfs_dir3_data_entry_p(hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020071
Dave Chinner82025d72012-11-12 22:54:12 +110072 switch (hdr->magic) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +110073 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
Dave Chinner82025d72012-11-12 22:54:12 +110074 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020075 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100076 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110078 break;
79 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020080 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110081 break;
82 default:
83 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
84 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020085 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 count = lastfree = freeseen = 0;
88 /*
89 * Account for zero bestfree entries.
90 */
91 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110092 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 freeseen |= 1 << 0;
94 }
95 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110096 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 freeseen |= 1 << 1;
98 }
99 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +1100100 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 freeseen |= 1 << 2;
102 }
Dave Chinner82025d72012-11-12 22:54:12 +1100103
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
105 be16_to_cpu(bf[1].length));
106 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
107 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /*
109 * Loop over the data/unused entries.
110 */
111 while (p < endp) {
112 dup = (xfs_dir2_data_unused_t *)p;
113 /*
114 * If it's unused, look for the space in the bestfree table.
115 * If we find it, account for that, else make sure it
116 * doesn't need to be there.
117 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100118 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100119 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
120 XFS_WANT_CORRUPTED_RETURN(
121 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
122 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200123 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (dfp) {
125 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100126 XFS_WANT_CORRUPTED_RETURN(
127 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100129 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100130 XFS_WANT_CORRUPTED_RETURN(
131 be16_to_cpu(dup->length) <=
132 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100133 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100134 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 lastfree = 1;
136 continue;
137 }
138 /*
139 * It's a real entry. Validate the fields.
140 * If this is a block directory then make sure it's
141 * in the leaf section of the block.
142 * The linear search is crude but this is DEBUG code.
143 */
144 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100145 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
146 XFS_WANT_CORRUPTED_RETURN(
147 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
148 XFS_WANT_CORRUPTED_RETURN(
149 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
150 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 count++;
152 lastfree = 0;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100153 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
154 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000155 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200157 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000158 name.name = dep->name;
159 name.len = dep->namelen;
160 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100161 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100162 if (be32_to_cpu(lep[i].address) == addr &&
163 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 break;
165 }
Dave Chinner82025d72012-11-12 22:54:12 +1100166 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000168 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 /*
171 * Need to have seen all the entries and all the bestfree slots.
172 */
Dave Chinner82025d72012-11-12 22:54:12 +1100173 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100174 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
175 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100176 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200177 if (lep[i].address ==
178 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 stale++;
180 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100181 XFS_WANT_CORRUPTED_RETURN(
182 be32_to_cpu(lep[i].hashval) >=
183 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Dave Chinner82025d72012-11-12 22:54:12 +1100185 XFS_WANT_CORRUPTED_RETURN(count ==
186 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
187 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Dave Chinner82025d72012-11-12 22:54:12 +1100189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100192static void
Dave Chinnere4813572012-11-12 22:54:14 +1100193xfs_dir2_data_verify(
194 struct xfs_buf *bp)
195{
196 struct xfs_mount *mp = bp->b_target->bt_mount;
197 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
198 int block_ok = 0;
199
200 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC);
201 block_ok = block_ok && __xfs_dir2_data_check(NULL, bp) == 0;
202
203 if (!block_ok) {
204 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
205 xfs_buf_ioerror(bp, EFSCORRUPTED);
206 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100207}
Dave Chinnere4813572012-11-12 22:54:14 +1100208
Dave Chinner1813dd62012-11-14 17:54:40 +1100209/*
210 * Readahead of the first block of the directory when it is opened is completely
211 * oblivious to the format of the directory. Hence we can either get a block
212 * format buffer or a data format buffer on readahead.
213 */
214static void
215xfs_dir2_data_reada_verify(
216 struct xfs_buf *bp)
Dave Chinner612cfbf2012-11-14 17:52:32 +1100217{
Dave Chinner1813dd62012-11-14 17:54:40 +1100218 struct xfs_mount *mp = bp->b_target->bt_mount;
219 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
220
221 switch (hdr->magic) {
222 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100223 case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC):
224 bp->b_ops = &xfs_dir3_block_buf_ops;
Dave Chinner1813dd62012-11-14 17:54:40 +1100225 bp->b_ops->verify_read(bp);
226 return;
227 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
228 xfs_dir2_data_verify(bp);
229 return;
230 default:
231 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
232 xfs_buf_ioerror(bp, EFSCORRUPTED);
233 break;
234 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100235}
236
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100237static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100238xfs_dir2_data_read_verify(
239 struct xfs_buf *bp)
240{
241 xfs_dir2_data_verify(bp);
Dave Chinnere4813572012-11-12 22:54:14 +1100242}
243
Dave Chinner1813dd62012-11-14 17:54:40 +1100244static void
245xfs_dir2_data_write_verify(
246 struct xfs_buf *bp)
247{
248 xfs_dir2_data_verify(bp);
249}
250
251const struct xfs_buf_ops xfs_dir2_data_buf_ops = {
252 .verify_read = xfs_dir2_data_read_verify,
253 .verify_write = xfs_dir2_data_write_verify,
254};
255
256static const struct xfs_buf_ops xfs_dir2_data_reada_buf_ops = {
257 .verify_read = xfs_dir2_data_reada_verify,
258 .verify_write = xfs_dir2_data_write_verify,
259};
260
Dave Chinner612cfbf2012-11-14 17:52:32 +1100261
Dave Chinnere4813572012-11-12 22:54:14 +1100262int
263xfs_dir2_data_read(
264 struct xfs_trans *tp,
265 struct xfs_inode *dp,
266 xfs_dablk_t bno,
267 xfs_daddr_t mapped_bno,
268 struct xfs_buf **bpp)
269{
270 return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner1813dd62012-11-14 17:54:40 +1100271 XFS_DATA_FORK, &xfs_dir2_data_buf_ops);
Dave Chinnere4813572012-11-12 22:54:14 +1100272}
273
Dave Chinnerda6958c2012-11-12 22:54:18 +1100274int
275xfs_dir2_data_readahead(
276 struct xfs_trans *tp,
277 struct xfs_inode *dp,
278 xfs_dablk_t bno,
279 xfs_daddr_t mapped_bno)
280{
281 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner1813dd62012-11-14 17:54:40 +1100282 XFS_DATA_FORK, &xfs_dir2_data_reada_buf_ops);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/*
286 * Given a data block and an unused entry from that block,
287 * return the bestfree entry if any that corresponds to it.
288 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200289STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200291 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 xfs_dir2_data_unused_t *dup) /* data unused entry */
293{
294 xfs_dir2_data_free_t *dfp; /* bestfree entry */
295 xfs_dir2_data_aoff_t off; /* offset value needed */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100296 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#if defined(DEBUG) && defined(__KERNEL__)
298 int matched; /* matched the value */
299 int seenzero; /* saw a 0 bestfree entry */
300#endif
301
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200302 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100303 bf = xfs_dir3_data_bestfree_p(hdr);
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#if defined(DEBUG) && defined(__KERNEL__)
306 /*
307 * Validate some consistency in the bestfree table.
308 * Check order, non-overlapping entries, and if we find the
309 * one we're looking for it has to be exact.
310 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200311 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100312 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
313 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
314 for (dfp = &bf[0], seenzero = matched = 0;
315 dfp < &bf[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 dfp++) {
317 if (!dfp->offset) {
318 ASSERT(!dfp->length);
319 seenzero = 1;
320 continue;
321 }
322 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100323 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100325 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100326 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100327 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100329 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100330 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100331 if (dfp > &bf[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100332 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334#endif
335 /*
336 * If this is smaller than the smallest bestfree entry,
337 * it can't be there since they're sorted.
338 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100339 if (be16_to_cpu(dup->length) <
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100340 be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return NULL;
342 /*
343 * Look at the three bestfree entries for our guy.
344 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100345 for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!dfp->offset)
347 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100348 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return dfp;
350 }
351 /*
352 * Didn't find it. This only happens if there are duplicate lengths.
353 */
354 return NULL;
355}
356
357/*
358 * Insert an unused-space entry into the bestfree table.
359 */
360xfs_dir2_data_free_t * /* entry inserted */
361xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200362 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 xfs_dir2_data_unused_t *dup, /* unused space */
364 int *loghead) /* log the data header (out) */
365{
366 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
367 xfs_dir2_data_free_t new; /* new bestfree entry */
368
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200369 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100370 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
371 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
372 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
373
374 dfp = xfs_dir3_data_bestfree_p(hdr);
Nathan Scott70e73f52006-03-17 17:26:52 +1100375 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200376 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Insert at position 0, 1, or 2; or not at all.
380 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100381 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 dfp[2] = dfp[1];
383 dfp[1] = dfp[0];
384 dfp[0] = new;
385 *loghead = 1;
386 return &dfp[0];
387 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100388 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 dfp[2] = dfp[1];
390 dfp[1] = new;
391 *loghead = 1;
392 return &dfp[1];
393 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100394 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 dfp[2] = new;
396 *loghead = 1;
397 return &dfp[2];
398 }
399 return NULL;
400}
401
402/*
403 * Remove a bestfree entry from the table.
404 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000405STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200407 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
409 int *loghead) /* out: log data header */
410{
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100411 struct xfs_dir2_data_free *bf;
412
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200413 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100414 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
415 hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) ||
416 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * It's the first entry, slide the next 2 up.
420 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100421 bf = xfs_dir3_data_bestfree_p(hdr);
422 if (dfp == &bf[0]) {
423 bf[0] = bf[1];
424 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 /*
427 * It's the second entry, slide the 3rd entry up.
428 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100429 else if (dfp == &bf[1])
430 bf[1] = bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Must be the last entry.
433 */
434 else
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100435 ASSERT(dfp == &bf[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Clear the 3rd entry, must be zero now.
438 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100439 bf[2].length = 0;
440 bf[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 *loghead = 1;
442}
443
444/*
445 * Given a data block, reconstruct its bestfree map.
446 */
447void
448xfs_dir2_data_freescan(
449 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200450 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000451 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 xfs_dir2_block_tail_t *btp; /* block tail */
454 xfs_dir2_data_entry_t *dep; /* active data entry */
455 xfs_dir2_data_unused_t *dup; /* unused data entry */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100456 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 char *endp; /* end of block's data */
458 char *p; /* current entry pointer */
459
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200460 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100461 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
462 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Start by clearing the table.
466 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100467 bf = xfs_dir3_data_bestfree_p(hdr);
468 memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 *loghead = 1;
470 /*
471 * Set up pointers.
472 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100473 p = (char *)xfs_dir3_data_entry_p(hdr);
474 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
475 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200476 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000477 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200479 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 /*
481 * Loop over the block's entries.
482 */
483 while (p < endp) {
484 dup = (xfs_dir2_data_unused_t *)p;
485 /*
486 * If it's a free entry, insert it.
487 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100488 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200489 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000490 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200491 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100492 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 /*
495 * For active entries, check their tags and skip them.
496 */
497 else {
498 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200499 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000500 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
501 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504}
505
506/*
507 * Initialize a data block at the given block number in the directory.
508 * Give back the buffer for the created block.
509 */
510int /* error */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100511xfs_dir3_data_init(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 xfs_da_args_t *args, /* directory operation args */
513 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000516 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200517 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 xfs_inode_t *dp; /* incore directory inode */
519 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100520 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int error; /* error return value */
522 int i; /* bestfree index */
523 xfs_mount_t *mp; /* filesystem mount point */
524 xfs_trans_t *tp; /* transaction pointer */
525 int t; /* temp */
526
527 dp = args->dp;
528 mp = dp->i_mount;
529 tp = args->trans;
530 /*
531 * Get the buffer set up for the block.
532 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000533 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100535 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return error;
Dave Chinner1813dd62012-11-14 17:54:40 +1100537 bp->b_ops = &xfs_dir2_data_buf_ops;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 /*
540 * Initialize the header.
541 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000542 hdr = bp->b_addr;
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100543
544 if (xfs_sb_version_hascrc(&mp->m_sb)) {
545 struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr;
546
547 memset(hdr3, 0, sizeof(*hdr3));
548 hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC);
549 hdr3->blkno = cpu_to_be64(bp->b_bn);
550 hdr3->owner = cpu_to_be64(dp->i_ino);
551 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_uuid);
552
553 } else
554 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
555
556 bf = xfs_dir3_data_bestfree_p(hdr);
557 bf[0].offset = cpu_to_be16(xfs_dir3_data_entry_offset(hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100559 bf[i].length = 0;
560 bf[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 /*
564 * Set up an unused entry for the block's body.
565 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100566 dup = xfs_dir3_data_unused_p(hdr);
Nathan Scottad354eb2006-03-17 17:27:37 +1100567 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100569 t = mp->m_dirblksize - (uint)xfs_dir3_data_entry_offset(hdr);
570 bf[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100571 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200572 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /*
574 * Log it and return it.
575 */
576 xfs_dir2_data_log_header(tp, bp);
577 xfs_dir2_data_log_unused(tp, bp, dup);
578 *bpp = bp;
579 return 0;
580}
581
582/*
583 * Log an active data entry from the block.
584 */
585void
586xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000587 struct xfs_trans *tp,
588 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 xfs_dir2_data_entry_t *dep) /* data entry pointer */
590{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000591 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200593 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100594 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
595 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200596
Dave Chinner1d9025e2012-06-22 18:50:14 +1000597 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000598 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200599 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
603 * Log a data block header.
604 */
605void
606xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000607 struct xfs_trans *tp,
608 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000610 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200612 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100613 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
614 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200615
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100616 xfs_trans_log_buf(tp, bp, 0, xfs_dir3_data_entry_offset(hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619/*
620 * Log a data unused entry.
621 */
622void
623xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000624 struct xfs_trans *tp,
625 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 xfs_dir2_data_unused_t *dup) /* data unused pointer */
627{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000628 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200630 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100631 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
632 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /*
635 * Log the first part of the unused entry.
636 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000637 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200639 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * Log the end (tag) of the unused entry.
642 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000643 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200644 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
645 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 sizeof(xfs_dir2_data_off_t) - 1));
647}
648
649/*
650 * Make a byte range in the data block unused.
651 * Its current contents are unimportant.
652 */
653void
654xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000655 struct xfs_trans *tp,
656 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfs_dir2_data_aoff_t offset, /* starting byte offset */
658 xfs_dir2_data_aoff_t len, /* length in bytes */
659 int *needlogp, /* out: log header */
660 int *needscanp) /* out: regen bestfree */
661{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200662 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
664 char *endptr; /* end of data area */
665 xfs_mount_t *mp; /* filesystem mount point */
666 int needscan; /* need to regen bestfree */
667 xfs_dir2_data_unused_t *newdup; /* new unused entry */
668 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
669 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100670 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000673 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Figure out where the end of the data area is.
677 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200678 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200679 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 else {
681 xfs_dir2_block_tail_t *btp; /* block tail */
682
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100683 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
684 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200685 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000686 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 /*
689 * If this isn't the start of the block, then back up to
690 * the previous entry and see if it's free.
691 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100692 if (offset > xfs_dir3_data_entry_offset(hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100693 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200695 tagp = (__be16 *)((char *)hdr + offset) - 1;
696 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100697 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 prevdup = NULL;
699 } else
700 prevdup = NULL;
701 /*
702 * If this isn't the end of the block, see if the entry after
703 * us is free.
704 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200705 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200707 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100708 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 postdup = NULL;
710 } else
711 postdup = NULL;
712 ASSERT(*needscanp == 0);
713 needscan = 0;
714 /*
715 * Previous and following entries are both free,
716 * merge everything into a single free entry.
717 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100718 bf = xfs_dir3_data_bestfree_p(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (prevdup && postdup) {
720 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
721
722 /*
723 * See if prevdup and/or postdup are in bestfree table.
724 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200725 dfp = xfs_dir2_data_freefind(hdr, prevdup);
726 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
728 * We need a rescan unless there are exactly 2 free entries
729 * namely our two. Then we know what's happening, otherwise
730 * since the third bestfree is there, there might be more
731 * entries.
732 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100733 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /*
735 * Fix up the new big freespace.
736 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800737 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000738 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200739 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 xfs_dir2_data_log_unused(tp, bp, prevdup);
741 if (!needscan) {
742 /*
743 * Has to be the case that entries 0 and 1 are
744 * dfp and dfp2 (don't know which is which), and
745 * entry 2 is empty.
746 * Remove entry 1 first then entry 0.
747 */
748 ASSERT(dfp && dfp2);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100749 if (dfp == &bf[1]) {
750 dfp = &bf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ASSERT(dfp2 == dfp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100752 dfp2 = &bf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200754 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
755 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Now insert the new entry.
758 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200759 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100760 ASSERT(dfp == &bf[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100761 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ASSERT(!dfp[1].length);
763 ASSERT(!dfp[2].length);
764 }
765 }
766 /*
767 * The entry before us is free, merge with it.
768 */
769 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200770 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800771 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000772 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200773 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 xfs_dir2_data_log_unused(tp, bp, prevdup);
775 /*
776 * If the previous entry was in the table, the new entry
777 * is longer, so it will be in the table too. Remove
778 * the old one and add the new one.
779 */
780 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200781 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
782 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 /*
785 * Otherwise we need a scan if the new entry is big enough.
786 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100787 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100788 needscan = be16_to_cpu(prevdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100789 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792 /*
793 * The following entry is free, merge with it.
794 */
795 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200796 dfp = xfs_dir2_data_freefind(hdr, postdup);
797 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100798 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
799 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000800 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200801 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 xfs_dir2_data_log_unused(tp, bp, newdup);
803 /*
804 * If the following entry was in the table, the new entry
805 * is longer, so it will be in the table too. Remove
806 * the old one and add the new one.
807 */
808 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200809 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
810 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 /*
813 * Otherwise we need a scan if the new entry is big enough.
814 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100815 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100816 needscan = be16_to_cpu(newdup->length) >
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100817 be16_to_cpu(bf[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820 /*
821 * Neither neighbor is free. Make a new entry.
822 */
823 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200824 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100825 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
826 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000827 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200828 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200830 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 *needscanp = needscan;
833}
834
835/*
836 * Take a byte range out of an existing unused space and make it un-free.
837 */
838void
839xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000840 struct xfs_trans *tp,
841 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_dir2_data_unused_t *dup, /* unused entry */
843 xfs_dir2_data_aoff_t offset, /* starting offset to use */
844 xfs_dir2_data_aoff_t len, /* length to use */
845 int *needlogp, /* out: need to log header */
846 int *needscanp) /* out: need regen bestfree */
847{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200848 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
850 int matchback; /* matches end of freespace */
851 int matchfront; /* matches start of freespace */
852 int needscan; /* need to regen bestfree */
853 xfs_dir2_data_unused_t *newdup; /* new unused entry */
854 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
855 int oldlen; /* old unused entry's length */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100856 struct xfs_dir2_data_free *bf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Dave Chinner1d9025e2012-06-22 18:50:14 +1000858 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200859 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100860 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) ||
861 hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100862 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200863 ASSERT(offset >= (char *)dup - (char *)hdr);
864 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
865 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * Look up the entry in the bestfree table.
868 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200869 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100870 oldlen = be16_to_cpu(dup->length);
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100871 bf = xfs_dir3_data_bestfree_p(hdr);
872 ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /*
874 * Check for alignment with front and back of the entry.
875 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200876 matchfront = (char *)dup - (char *)hdr == offset;
877 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ASSERT(*needscanp == 0);
879 needscan = 0;
880 /*
881 * If we matched it exactly we just need to get rid of it from
882 * the bestfree table.
883 */
884 if (matchfront && matchback) {
885 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100886 needscan = (bf[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200888 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 }
891 /*
892 * We match the first part of the entry.
893 * Make a new entry with the remaining freespace.
894 */
895 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200896 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100897 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
898 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000899 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200900 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 xfs_dir2_data_log_unused(tp, bp, newdup);
902 /*
903 * If it was in the table, remove it and add the new one.
904 */
905 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200906 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
907 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100909 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200910 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /*
912 * If we got inserted at the last slot,
913 * that means we don't know if there was a better
914 * choice for the last slot, or not. Rescan.
915 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100916 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918 }
919 /*
920 * We match the last part of the entry.
921 * Trim the allocated space off the tail of the entry.
922 */
923 else if (matchback) {
924 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200925 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000926 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200927 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 xfs_dir2_data_log_unused(tp, bp, newdup);
929 /*
930 * If it was in the table, remove it and add the new one.
931 */
932 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200933 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
934 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100936 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200937 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 /*
939 * If we got inserted at the last slot,
940 * that means we don't know if there was a better
941 * choice for the last slot, or not. Rescan.
942 */
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100943 needscan = dfp == &bf[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945 }
946 /*
947 * Poking out the middle of an entry.
948 * Make two new entries.
949 */
950 else {
951 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200952 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000953 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200954 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200956 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100957 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
958 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000959 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200960 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 xfs_dir2_data_log_unused(tp, bp, newdup2);
962 /*
963 * If the old entry was in the table, we need to scan
964 * if the 3rd entry was valid, since these entries
965 * are smaller than the old one.
966 * If we don't need to scan that means there were 1 or 2
967 * entries in the table, and removing the old and adding
968 * the 2 new will work.
969 */
970 if (dfp) {
Dave Chinnerf5f3d9b2013-04-03 16:11:20 +1100971 needscan = (bf[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200973 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
974 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
975 xfs_dir2_data_freeinsert(hdr, newdup2,
976 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978 }
979 }
980 *needscanp = needscan;
981}