blob: dcb8a873ab92a566aabe65b19ead744c75c26d9f [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.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dinode.h"
29#include "xfs_inode.h"
Christoph Hellwig57926642011-07-13 13:43:48 +020030#include "xfs_dir2_format.h"
31#include "xfs_dir2_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_error.h"
33
Christoph Hellwigc2066e22011-07-08 14:35:38 +020034STATIC xfs_dir2_data_free_t *
35xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup);
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Check the consistency of the data block.
39 * The input can also be a block-format directory.
Dave Chinner82025d72012-11-12 22:54:12 +110040 * Return 0 is the buffer is good, otherwise an error.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
Dave Chinner82025d72012-11-12 22:54:12 +110042int
43__xfs_dir2_data_check(
Dave Chinner1d9025e2012-06-22 18:50:14 +100044 struct xfs_inode *dp, /* incore inode pointer */
45 struct xfs_buf *bp) /* data block's buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 xfs_dir2_dataptr_t addr; /* addr for leaf lookup */
48 xfs_dir2_data_free_t *bf; /* bestfree table */
49 xfs_dir2_block_tail_t *btp=NULL; /* block tail */
50 int count; /* count of entries found */
Christoph Hellwigc2066e22011-07-08 14:35:38 +020051 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 xfs_dir2_data_entry_t *dep; /* data entry */
53 xfs_dir2_data_free_t *dfp; /* bestfree entry */
54 xfs_dir2_data_unused_t *dup; /* unused entry */
55 char *endp; /* end of useful data */
56 int freeseen; /* mask of bestfrees seen */
57 xfs_dahash_t hash; /* hash of current name */
58 int i; /* leaf index */
59 int lastfree; /* last entry was unused */
60 xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */
61 xfs_mount_t *mp; /* filesystem mount point */
62 char *p; /* current data position */
63 int stale; /* count of stale leaves */
Barry Naujok5163f952008-05-21 16:41:01 +100064 struct xfs_name name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Dave Chinner82025d72012-11-12 22:54:12 +110066 mp = bp->b_target->bt_mount;
Dave Chinner1d9025e2012-06-22 18:50:14 +100067 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020068 bf = hdr->bestfree;
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +020069 p = (char *)(hdr + 1);
Christoph Hellwigc2066e22011-07-08 14:35:38 +020070
Dave Chinner82025d72012-11-12 22:54:12 +110071 switch (hdr->magic) {
72 case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020073 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +100074 lep = xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 endp = (char *)lep;
Dave Chinner82025d72012-11-12 22:54:12 +110076 break;
77 case cpu_to_be32(XFS_DIR2_DATA_MAGIC):
Christoph Hellwigc2066e22011-07-08 14:35:38 +020078 endp = (char *)hdr + mp->m_dirblksize;
Dave Chinner82025d72012-11-12 22:54:12 +110079 break;
80 default:
81 XFS_ERROR_REPORT("Bad Magic", XFS_ERRLEVEL_LOW, mp);
82 return EFSCORRUPTED;
Christoph Hellwigc2066e22011-07-08 14:35:38 +020083 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 count = lastfree = freeseen = 0;
86 /*
87 * Account for zero bestfree entries.
88 */
89 if (!bf[0].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110090 XFS_WANT_CORRUPTED_RETURN(!bf[0].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 freeseen |= 1 << 0;
92 }
93 if (!bf[1].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110094 XFS_WANT_CORRUPTED_RETURN(!bf[1].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 freeseen |= 1 << 1;
96 }
97 if (!bf[2].length) {
Dave Chinner82025d72012-11-12 22:54:12 +110098 XFS_WANT_CORRUPTED_RETURN(!bf[2].offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 freeseen |= 1 << 2;
100 }
Dave Chinner82025d72012-11-12 22:54:12 +1100101
102 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[0].length) >=
103 be16_to_cpu(bf[1].length));
104 XFS_WANT_CORRUPTED_RETURN(be16_to_cpu(bf[1].length) >=
105 be16_to_cpu(bf[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 /*
107 * Loop over the data/unused entries.
108 */
109 while (p < endp) {
110 dup = (xfs_dir2_data_unused_t *)p;
111 /*
112 * If it's unused, look for the space in the bestfree table.
113 * If we find it, account for that, else make sure it
114 * doesn't need to be there.
115 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100116 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Dave Chinner82025d72012-11-12 22:54:12 +1100117 XFS_WANT_CORRUPTED_RETURN(lastfree == 0);
118 XFS_WANT_CORRUPTED_RETURN(
119 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) ==
120 (char *)dup - (char *)hdr);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200121 dfp = xfs_dir2_data_freefind(hdr, dup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (dfp) {
123 i = (int)(dfp - bf);
Dave Chinner82025d72012-11-12 22:54:12 +1100124 XFS_WANT_CORRUPTED_RETURN(
125 (freeseen & (1 << i)) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 freeseen |= 1 << i;
Nathan Scott70e73f52006-03-17 17:26:52 +1100127 } else {
Dave Chinner82025d72012-11-12 22:54:12 +1100128 XFS_WANT_CORRUPTED_RETURN(
129 be16_to_cpu(dup->length) <=
130 be16_to_cpu(bf[2].length));
Nathan Scott70e73f52006-03-17 17:26:52 +1100131 }
Nathan Scottad354eb2006-03-17 17:27:37 +1100132 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 lastfree = 1;
134 continue;
135 }
136 /*
137 * It's a real entry. Validate the fields.
138 * If this is a block directory then make sure it's
139 * in the leaf section of the block.
140 * The linear search is crude but this is DEBUG code.
141 */
142 dep = (xfs_dir2_data_entry_t *)p;
Dave Chinner82025d72012-11-12 22:54:12 +1100143 XFS_WANT_CORRUPTED_RETURN(dep->namelen != 0);
144 XFS_WANT_CORRUPTED_RETURN(
145 !xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)));
146 XFS_WANT_CORRUPTED_RETURN(
147 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) ==
148 (char *)dep - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 count++;
150 lastfree = 0;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200151 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000152 addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 (xfs_dir2_data_aoff_t)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200154 ((char *)dep - (char *)hdr));
Barry Naujok5163f952008-05-21 16:41:01 +1000155 name.name = dep->name;
156 name.len = dep->namelen;
157 hash = mp->m_dirnameops->hashname(&name);
Nathan Scotte922fff2006-03-17 17:27:56 +1100158 for (i = 0; i < be32_to_cpu(btp->count); i++) {
Nathan Scott3c1f9c12006-03-17 17:28:18 +1100159 if (be32_to_cpu(lep[i].address) == addr &&
160 be32_to_cpu(lep[i].hashval) == hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 break;
162 }
Dave Chinner82025d72012-11-12 22:54:12 +1100163 XFS_WANT_CORRUPTED_RETURN(i < be32_to_cpu(btp->count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000165 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 /*
168 * Need to have seen all the entries and all the bestfree slots.
169 */
Dave Chinner82025d72012-11-12 22:54:12 +1100170 XFS_WANT_CORRUPTED_RETURN(freeseen == 7);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200171 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Nathan Scotte922fff2006-03-17 17:27:56 +1100172 for (i = stale = 0; i < be32_to_cpu(btp->count); i++) {
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200173 if (lep[i].address ==
174 cpu_to_be32(XFS_DIR2_NULL_DATAPTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 stale++;
176 if (i > 0)
Dave Chinner82025d72012-11-12 22:54:12 +1100177 XFS_WANT_CORRUPTED_RETURN(
178 be32_to_cpu(lep[i].hashval) >=
179 be32_to_cpu(lep[i - 1].hashval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Dave Chinner82025d72012-11-12 22:54:12 +1100181 XFS_WANT_CORRUPTED_RETURN(count ==
182 be32_to_cpu(btp->count) - be32_to_cpu(btp->stale));
183 XFS_WANT_CORRUPTED_RETURN(stale == be32_to_cpu(btp->stale));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Dave Chinner82025d72012-11-12 22:54:12 +1100185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100188static void
Dave Chinnere4813572012-11-12 22:54:14 +1100189xfs_dir2_data_verify(
190 struct xfs_buf *bp)
191{
192 struct xfs_mount *mp = bp->b_target->bt_mount;
193 struct xfs_dir2_data_hdr *hdr = bp->b_addr;
194 int block_ok = 0;
195
196 block_ok = hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC);
197 block_ok = block_ok && __xfs_dir2_data_check(NULL, bp) == 0;
198
199 if (!block_ok) {
200 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, hdr);
201 xfs_buf_ioerror(bp, EFSCORRUPTED);
202 }
Dave Chinner612cfbf2012-11-14 17:52:32 +1100203}
Dave Chinnere4813572012-11-12 22:54:14 +1100204
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100205void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100206xfs_dir2_data_write_verify(
207 struct xfs_buf *bp)
208{
209 xfs_dir2_data_verify(bp);
210}
211
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100212static void
Dave Chinner612cfbf2012-11-14 17:52:32 +1100213xfs_dir2_data_read_verify(
214 struct xfs_buf *bp)
215{
216 xfs_dir2_data_verify(bp);
217 bp->b_pre_io = xfs_dir2_data_write_verify;
Dave Chinnere4813572012-11-12 22:54:14 +1100218 bp->b_iodone = NULL;
219 xfs_buf_ioend(bp, 0);
220}
221
Dave Chinner612cfbf2012-11-14 17:52:32 +1100222
Dave Chinnere4813572012-11-12 22:54:14 +1100223int
224xfs_dir2_data_read(
225 struct xfs_trans *tp,
226 struct xfs_inode *dp,
227 xfs_dablk_t bno,
228 xfs_daddr_t mapped_bno,
229 struct xfs_buf **bpp)
230{
231 return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100232 XFS_DATA_FORK, xfs_dir2_data_read_verify);
Dave Chinnere4813572012-11-12 22:54:14 +1100233}
234
Dave Chinnerda6958c2012-11-12 22:54:18 +1100235int
236xfs_dir2_data_readahead(
237 struct xfs_trans *tp,
238 struct xfs_inode *dp,
239 xfs_dablk_t bno,
240 xfs_daddr_t mapped_bno)
241{
242 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
Dave Chinner612cfbf2012-11-14 17:52:32 +1100243 XFS_DATA_FORK, xfs_dir2_data_read_verify);
Dave Chinnerda6958c2012-11-12 22:54:18 +1100244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * Given a data block and an unused entry from that block,
248 * return the bestfree entry if any that corresponds to it.
249 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200250STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200252 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 xfs_dir2_data_unused_t *dup) /* data unused entry */
254{
255 xfs_dir2_data_free_t *dfp; /* bestfree entry */
256 xfs_dir2_data_aoff_t off; /* offset value needed */
257#if defined(DEBUG) && defined(__KERNEL__)
258 int matched; /* matched the value */
259 int seenzero; /* saw a 0 bestfree entry */
260#endif
261
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200262 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#if defined(DEBUG) && defined(__KERNEL__)
264 /*
265 * Validate some consistency in the bestfree table.
266 * Check order, non-overlapping entries, and if we find the
267 * one we're looking for it has to be exact.
268 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200269 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
270 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200271 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
272 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 dfp++) {
274 if (!dfp->offset) {
275 ASSERT(!dfp->length);
276 seenzero = 1;
277 continue;
278 }
279 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100280 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100282 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100283 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100284 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100286 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100287 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200288 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100289 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291#endif
292 /*
293 * If this is smaller than the smallest bestfree entry,
294 * it can't be there since they're sorted.
295 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100296 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200297 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return NULL;
299 /*
300 * Look at the three bestfree entries for our guy.
301 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200302 for (dfp = &hdr->bestfree[0];
303 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 dfp++) {
305 if (!dfp->offset)
306 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100307 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return dfp;
309 }
310 /*
311 * Didn't find it. This only happens if there are duplicate lengths.
312 */
313 return NULL;
314}
315
316/*
317 * Insert an unused-space entry into the bestfree table.
318 */
319xfs_dir2_data_free_t * /* entry inserted */
320xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200321 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 xfs_dir2_data_unused_t *dup, /* unused space */
323 int *loghead) /* log the data header (out) */
324{
325 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
326 xfs_dir2_data_free_t new; /* new bestfree entry */
327
328#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200329 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
330 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200332 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100333 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200334 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * Insert at position 0, 1, or 2; or not at all.
338 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100339 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 dfp[2] = dfp[1];
341 dfp[1] = dfp[0];
342 dfp[0] = new;
343 *loghead = 1;
344 return &dfp[0];
345 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100346 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 dfp[2] = dfp[1];
348 dfp[1] = new;
349 *loghead = 1;
350 return &dfp[1];
351 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100352 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 dfp[2] = new;
354 *loghead = 1;
355 return &dfp[2];
356 }
357 return NULL;
358}
359
360/*
361 * Remove a bestfree entry from the table.
362 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000363STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200365 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
367 int *loghead) /* out: log data header */
368{
369#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200370 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
371 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#endif
373 /*
374 * It's the first entry, slide the next 2 up.
375 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200376 if (dfp == &hdr->bestfree[0]) {
377 hdr->bestfree[0] = hdr->bestfree[1];
378 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380 /*
381 * It's the second entry, slide the 3rd entry up.
382 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200383 else if (dfp == &hdr->bestfree[1])
384 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
386 * Must be the last entry.
387 */
388 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200389 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Clear the 3rd entry, must be zero now.
392 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200393 hdr->bestfree[2].length = 0;
394 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *loghead = 1;
396}
397
398/*
399 * Given a data block, reconstruct its bestfree map.
400 */
401void
402xfs_dir2_data_freescan(
403 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200404 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000405 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 xfs_dir2_block_tail_t *btp; /* block tail */
408 xfs_dir2_data_entry_t *dep; /* active data entry */
409 xfs_dir2_data_unused_t *dup; /* unused data entry */
410 char *endp; /* end of block's data */
411 char *p; /* current entry pointer */
412
413#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200414 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
415 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#endif
417 /*
418 * Start by clearing the table.
419 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200420 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *loghead = 1;
422 /*
423 * Set up pointers.
424 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200425 p = (char *)(hdr + 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200426 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200427 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000428 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200430 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /*
432 * Loop over the block's entries.
433 */
434 while (p < endp) {
435 dup = (xfs_dir2_data_unused_t *)p;
436 /*
437 * If it's a free entry, insert it.
438 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100439 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200440 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000441 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200442 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100443 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 /*
446 * For active entries, check their tags and skip them.
447 */
448 else {
449 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200450 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000451 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
452 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 }
455}
456
457/*
458 * Initialize a data block at the given block number in the directory.
459 * Give back the buffer for the created block.
460 */
461int /* error */
462xfs_dir2_data_init(
463 xfs_da_args_t *args, /* directory operation args */
464 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000465 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000467 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200468 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 xfs_inode_t *dp; /* incore directory inode */
470 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
471 int error; /* error return value */
472 int i; /* bestfree index */
473 xfs_mount_t *mp; /* filesystem mount point */
474 xfs_trans_t *tp; /* transaction pointer */
475 int t; /* temp */
476
477 dp = args->dp;
478 mp = dp->i_mount;
479 tp = args->trans;
480 /*
481 * Get the buffer set up for the block.
482 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000483 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 XFS_DATA_FORK);
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return error;
Dave Chinnerb0f539d2012-11-14 17:53:49 +1100487 bp->b_pre_io = xfs_dir2_data_write_verify;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Initialize the header.
491 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000492 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200493 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
494 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200496 hdr->bestfree[i].length = 0;
497 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Set up an unused entry for the block's body.
502 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200503 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Nathan Scottad354eb2006-03-17 17:27:37 +1100504 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200506 t = mp->m_dirblksize - (uint)sizeof(*hdr);
507 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100508 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200509 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * Log it and return it.
512 */
513 xfs_dir2_data_log_header(tp, bp);
514 xfs_dir2_data_log_unused(tp, bp, dup);
515 *bpp = bp;
516 return 0;
517}
518
519/*
520 * Log an active data entry from the block.
521 */
522void
523xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000524 struct xfs_trans *tp,
525 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 xfs_dir2_data_entry_t *dep) /* data entry pointer */
527{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000528 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200530 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
531 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200532
Dave Chinner1d9025e2012-06-22 18:50:14 +1000533 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000534 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200535 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
539 * Log a data block header.
540 */
541void
542xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000543 struct xfs_trans *tp,
544 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000546 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200548 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
549 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200550
Dave Chinner1d9025e2012-06-22 18:50:14 +1000551 xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555 * Log a data unused entry.
556 */
557void
558xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000559 struct xfs_trans *tp,
560 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_dir2_data_unused_t *dup) /* data unused pointer */
562{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000563 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200565 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
566 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Log the first part of the unused entry.
570 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000571 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200573 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
575 * Log the end (tag) of the unused entry.
576 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000577 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200578 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
579 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 sizeof(xfs_dir2_data_off_t) - 1));
581}
582
583/*
584 * Make a byte range in the data block unused.
585 * Its current contents are unimportant.
586 */
587void
588xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000589 struct xfs_trans *tp,
590 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 xfs_dir2_data_aoff_t offset, /* starting byte offset */
592 xfs_dir2_data_aoff_t len, /* length in bytes */
593 int *needlogp, /* out: log header */
594 int *needscanp) /* out: regen bestfree */
595{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200596 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
598 char *endptr; /* end of data area */
599 xfs_mount_t *mp; /* filesystem mount point */
600 int needscan; /* need to regen bestfree */
601 xfs_dir2_data_unused_t *newdup; /* new unused entry */
602 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
603 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
604
605 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000606 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Figure out where the end of the data area is.
610 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200611 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200612 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else {
614 xfs_dir2_block_tail_t *btp; /* block tail */
615
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200616 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200617 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000618 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 /*
621 * If this isn't the start of the block, then back up to
622 * the previous entry and see if it's free.
623 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200624 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100625 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200627 tagp = (__be16 *)((char *)hdr + offset) - 1;
628 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100629 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 prevdup = NULL;
631 } else
632 prevdup = NULL;
633 /*
634 * If this isn't the end of the block, see if the entry after
635 * us is free.
636 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200637 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200639 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100640 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 postdup = NULL;
642 } else
643 postdup = NULL;
644 ASSERT(*needscanp == 0);
645 needscan = 0;
646 /*
647 * Previous and following entries are both free,
648 * merge everything into a single free entry.
649 */
650 if (prevdup && postdup) {
651 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
652
653 /*
654 * See if prevdup and/or postdup are in bestfree table.
655 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200656 dfp = xfs_dir2_data_freefind(hdr, prevdup);
657 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * We need a rescan unless there are exactly 2 free entries
660 * namely our two. Then we know what's happening, otherwise
661 * since the third bestfree is there, there might be more
662 * entries.
663 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200664 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /*
666 * Fix up the new big freespace.
667 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800668 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000669 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200670 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 xfs_dir2_data_log_unused(tp, bp, prevdup);
672 if (!needscan) {
673 /*
674 * Has to be the case that entries 0 and 1 are
675 * dfp and dfp2 (don't know which is which), and
676 * entry 2 is empty.
677 * Remove entry 1 first then entry 0.
678 */
679 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200680 if (dfp == &hdr->bestfree[1]) {
681 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200683 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200685 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
686 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /*
688 * Now insert the new entry.
689 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200690 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
691 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100692 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ASSERT(!dfp[1].length);
694 ASSERT(!dfp[2].length);
695 }
696 }
697 /*
698 * The entry before us is free, merge with it.
699 */
700 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200701 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800702 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000703 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200704 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 xfs_dir2_data_log_unused(tp, bp, prevdup);
706 /*
707 * If the previous entry was in the table, the new entry
708 * is longer, so it will be in the table too. Remove
709 * the old one and add the new one.
710 */
711 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200712 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
713 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 /*
716 * Otherwise we need a scan if the new entry is big enough.
717 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100718 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100719 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200720 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 /*
724 * The following entry is free, merge with it.
725 */
726 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200727 dfp = xfs_dir2_data_freefind(hdr, postdup);
728 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100729 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
730 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000731 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200732 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 xfs_dir2_data_log_unused(tp, bp, newdup);
734 /*
735 * If the following entry was in the table, the new entry
736 * is longer, so it will be in the table too. Remove
737 * the old one and add the new one.
738 */
739 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200740 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
741 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 /*
744 * Otherwise we need a scan if the new entry is big enough.
745 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100746 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100747 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200748 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
751 /*
752 * Neither neighbor is free. Make a new entry.
753 */
754 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200755 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100756 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
757 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000758 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200759 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200761 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 *needscanp = needscan;
764}
765
766/*
767 * Take a byte range out of an existing unused space and make it un-free.
768 */
769void
770xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000771 struct xfs_trans *tp,
772 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 xfs_dir2_data_unused_t *dup, /* unused entry */
774 xfs_dir2_data_aoff_t offset, /* starting offset to use */
775 xfs_dir2_data_aoff_t len, /* length to use */
776 int *needlogp, /* out: need to log header */
777 int *needscanp) /* out: need regen bestfree */
778{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200779 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
781 int matchback; /* matches end of freespace */
782 int matchfront; /* matches start of freespace */
783 int needscan; /* need to regen bestfree */
784 xfs_dir2_data_unused_t *newdup; /* new unused entry */
785 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
786 int oldlen; /* old unused entry's length */
787
Dave Chinner1d9025e2012-06-22 18:50:14 +1000788 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200789 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
790 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100791 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200792 ASSERT(offset >= (char *)dup - (char *)hdr);
793 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
794 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Look up the entry in the bestfree table.
797 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200798 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100799 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200800 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * Check for alignment with front and back of the entry.
803 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200804 matchfront = (char *)dup - (char *)hdr == offset;
805 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 ASSERT(*needscanp == 0);
807 needscan = 0;
808 /*
809 * If we matched it exactly we just need to get rid of it from
810 * the bestfree table.
811 */
812 if (matchfront && matchback) {
813 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200814 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200816 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
819 /*
820 * We match the first part of the entry.
821 * Make a new entry with the remaining freespace.
822 */
823 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200824 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100825 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
826 newdup->length = cpu_to_be16(oldlen - 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);
830 /*
831 * If it was in the table, remove it and add the new one.
832 */
833 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200834 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
835 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100837 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200838 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
840 * If we got inserted at the last slot,
841 * that means we don't know if there was a better
842 * choice for the last slot, or not. Rescan.
843 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200844 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 }
847 /*
848 * We match the last part of the entry.
849 * Trim the allocated space off the tail of the entry.
850 */
851 else if (matchback) {
852 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200853 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000854 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200855 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 xfs_dir2_data_log_unused(tp, bp, newdup);
857 /*
858 * If it was in the table, remove it and add the new one.
859 */
860 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200861 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
862 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100864 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200865 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /*
867 * If we got inserted at the last slot,
868 * that means we don't know if there was a better
869 * choice for the last slot, or not. Rescan.
870 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200871 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873 }
874 /*
875 * Poking out the middle of an entry.
876 * Make two new entries.
877 */
878 else {
879 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200880 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000881 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200882 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200884 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100885 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
886 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000887 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200888 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 xfs_dir2_data_log_unused(tp, bp, newdup2);
890 /*
891 * If the old entry was in the table, we need to scan
892 * if the 3rd entry was valid, since these entries
893 * are smaller than the old one.
894 * If we don't need to scan that means there were 1 or 2
895 * entries in the table, and removing the old and adding
896 * the 2 new will work.
897 */
898 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200899 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200901 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
902 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
903 xfs_dir2_data_freeinsert(hdr, newdup2,
904 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 }
907 }
908 *needscanp = needscan;
909}