blob: 0ef04f1bf51123722b09d3ca1502715c1d44a1a4 [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 Chinnere4813572012-11-12 22:54:14 +1100188static void
189xfs_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 }
203
204 bp->b_iodone = NULL;
205 xfs_buf_ioend(bp, 0);
206}
207
208int
209xfs_dir2_data_read(
210 struct xfs_trans *tp,
211 struct xfs_inode *dp,
212 xfs_dablk_t bno,
213 xfs_daddr_t mapped_bno,
214 struct xfs_buf **bpp)
215{
216 return xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp,
217 XFS_DATA_FORK, xfs_dir2_data_verify);
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
221 * Given a data block and an unused entry from that block,
222 * return the bestfree entry if any that corresponds to it.
223 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200224STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200226 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 xfs_dir2_data_unused_t *dup) /* data unused entry */
228{
229 xfs_dir2_data_free_t *dfp; /* bestfree entry */
230 xfs_dir2_data_aoff_t off; /* offset value needed */
231#if defined(DEBUG) && defined(__KERNEL__)
232 int matched; /* matched the value */
233 int seenzero; /* saw a 0 bestfree entry */
234#endif
235
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200236 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#if defined(DEBUG) && defined(__KERNEL__)
238 /*
239 * Validate some consistency in the bestfree table.
240 * Check order, non-overlapping entries, and if we find the
241 * one we're looking for it has to be exact.
242 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200243 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
244 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200245 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
246 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dfp++) {
248 if (!dfp->offset) {
249 ASSERT(!dfp->length);
250 seenzero = 1;
251 continue;
252 }
253 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100254 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100256 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100257 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100258 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100260 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100261 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200262 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100263 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265#endif
266 /*
267 * If this is smaller than the smallest bestfree entry,
268 * it can't be there since they're sorted.
269 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100270 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200271 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return NULL;
273 /*
274 * Look at the three bestfree entries for our guy.
275 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200276 for (dfp = &hdr->bestfree[0];
277 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 dfp++) {
279 if (!dfp->offset)
280 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100281 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return dfp;
283 }
284 /*
285 * Didn't find it. This only happens if there are duplicate lengths.
286 */
287 return NULL;
288}
289
290/*
291 * Insert an unused-space entry into the bestfree table.
292 */
293xfs_dir2_data_free_t * /* entry inserted */
294xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200295 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 xfs_dir2_data_unused_t *dup, /* unused space */
297 int *loghead) /* log the data header (out) */
298{
299 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
300 xfs_dir2_data_free_t new; /* new bestfree entry */
301
302#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200303 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
304 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200306 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100307 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200308 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /*
311 * Insert at position 0, 1, or 2; or not at all.
312 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100313 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 dfp[2] = dfp[1];
315 dfp[1] = dfp[0];
316 dfp[0] = new;
317 *loghead = 1;
318 return &dfp[0];
319 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100320 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 dfp[2] = dfp[1];
322 dfp[1] = new;
323 *loghead = 1;
324 return &dfp[1];
325 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100326 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 dfp[2] = new;
328 *loghead = 1;
329 return &dfp[2];
330 }
331 return NULL;
332}
333
334/*
335 * Remove a bestfree entry from the table.
336 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000337STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200339 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
341 int *loghead) /* out: log data header */
342{
343#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200344 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
345 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#endif
347 /*
348 * It's the first entry, slide the next 2 up.
349 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200350 if (dfp == &hdr->bestfree[0]) {
351 hdr->bestfree[0] = hdr->bestfree[1];
352 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 /*
355 * It's the second entry, slide the 3rd entry up.
356 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200357 else if (dfp == &hdr->bestfree[1])
358 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /*
360 * Must be the last entry.
361 */
362 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200363 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /*
365 * Clear the 3rd entry, must be zero now.
366 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200367 hdr->bestfree[2].length = 0;
368 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 *loghead = 1;
370}
371
372/*
373 * Given a data block, reconstruct its bestfree map.
374 */
375void
376xfs_dir2_data_freescan(
377 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200378 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000379 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 xfs_dir2_block_tail_t *btp; /* block tail */
382 xfs_dir2_data_entry_t *dep; /* active data entry */
383 xfs_dir2_data_unused_t *dup; /* unused data entry */
384 char *endp; /* end of block's data */
385 char *p; /* current entry pointer */
386
387#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200388 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
389 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#endif
391 /*
392 * Start by clearing the table.
393 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200394 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *loghead = 1;
396 /*
397 * Set up pointers.
398 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200399 p = (char *)(hdr + 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200400 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200401 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000402 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200404 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Loop over the block's entries.
407 */
408 while (p < endp) {
409 dup = (xfs_dir2_data_unused_t *)p;
410 /*
411 * If it's a free entry, insert it.
412 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100413 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200414 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000415 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200416 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100417 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 /*
420 * For active entries, check their tags and skip them.
421 */
422 else {
423 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200424 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000425 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
426 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 }
429}
430
431/*
432 * Initialize a data block at the given block number in the directory.
433 * Give back the buffer for the created block.
434 */
435int /* error */
436xfs_dir2_data_init(
437 xfs_da_args_t *args, /* directory operation args */
438 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000439 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000441 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200442 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 xfs_inode_t *dp; /* incore directory inode */
444 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
445 int error; /* error return value */
446 int i; /* bestfree index */
447 xfs_mount_t *mp; /* filesystem mount point */
448 xfs_trans_t *tp; /* transaction pointer */
449 int t; /* temp */
450
451 dp = args->dp;
452 mp = dp->i_mount;
453 tp = args->trans;
454 /*
455 * Get the buffer set up for the block.
456 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000457 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 XFS_DATA_FORK);
459 if (error) {
460 return error;
461 }
462 ASSERT(bp != NULL);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 * Initialize the header.
466 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000467 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200468 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
469 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200471 hdr->bestfree[i].length = 0;
472 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Set up an unused entry for the block's body.
477 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200478 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Nathan Scottad354eb2006-03-17 17:27:37 +1100479 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200481 t = mp->m_dirblksize - (uint)sizeof(*hdr);
482 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100483 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200484 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 /*
486 * Log it and return it.
487 */
488 xfs_dir2_data_log_header(tp, bp);
489 xfs_dir2_data_log_unused(tp, bp, dup);
490 *bpp = bp;
491 return 0;
492}
493
494/*
495 * Log an active data entry from the block.
496 */
497void
498xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000499 struct xfs_trans *tp,
500 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 xfs_dir2_data_entry_t *dep) /* data entry pointer */
502{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000503 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200505 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
506 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200507
Dave Chinner1d9025e2012-06-22 18:50:14 +1000508 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000509 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200510 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * Log a data block header.
515 */
516void
517xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000518 struct xfs_trans *tp,
519 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000521 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200523 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
524 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200525
Dave Chinner1d9025e2012-06-22 18:50:14 +1000526 xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
530 * Log a data unused entry.
531 */
532void
533xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000534 struct xfs_trans *tp,
535 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 xfs_dir2_data_unused_t *dup) /* data unused pointer */
537{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000538 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200540 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
541 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Log the first part of the unused entry.
545 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000546 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200548 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /*
550 * Log the end (tag) of the unused entry.
551 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000552 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200553 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
554 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 sizeof(xfs_dir2_data_off_t) - 1));
556}
557
558/*
559 * Make a byte range in the data block unused.
560 * Its current contents are unimportant.
561 */
562void
563xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000564 struct xfs_trans *tp,
565 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 xfs_dir2_data_aoff_t offset, /* starting byte offset */
567 xfs_dir2_data_aoff_t len, /* length in bytes */
568 int *needlogp, /* out: log header */
569 int *needscanp) /* out: regen bestfree */
570{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200571 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
573 char *endptr; /* end of data area */
574 xfs_mount_t *mp; /* filesystem mount point */
575 int needscan; /* need to regen bestfree */
576 xfs_dir2_data_unused_t *newdup; /* new unused entry */
577 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
578 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
579
580 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000581 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /*
584 * Figure out where the end of the data area is.
585 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200586 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200587 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 else {
589 xfs_dir2_block_tail_t *btp; /* block tail */
590
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200591 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200592 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000593 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 /*
596 * If this isn't the start of the block, then back up to
597 * the previous entry and see if it's free.
598 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200599 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100600 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200602 tagp = (__be16 *)((char *)hdr + offset) - 1;
603 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100604 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 prevdup = NULL;
606 } else
607 prevdup = NULL;
608 /*
609 * If this isn't the end of the block, see if the entry after
610 * us is free.
611 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200612 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200614 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100615 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 postdup = NULL;
617 } else
618 postdup = NULL;
619 ASSERT(*needscanp == 0);
620 needscan = 0;
621 /*
622 * Previous and following entries are both free,
623 * merge everything into a single free entry.
624 */
625 if (prevdup && postdup) {
626 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
627
628 /*
629 * See if prevdup and/or postdup are in bestfree table.
630 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200631 dfp = xfs_dir2_data_freefind(hdr, prevdup);
632 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 /*
634 * We need a rescan unless there are exactly 2 free entries
635 * namely our two. Then we know what's happening, otherwise
636 * since the third bestfree is there, there might be more
637 * entries.
638 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200639 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 /*
641 * Fix up the new big freespace.
642 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800643 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000644 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200645 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 xfs_dir2_data_log_unused(tp, bp, prevdup);
647 if (!needscan) {
648 /*
649 * Has to be the case that entries 0 and 1 are
650 * dfp and dfp2 (don't know which is which), and
651 * entry 2 is empty.
652 * Remove entry 1 first then entry 0.
653 */
654 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200655 if (dfp == &hdr->bestfree[1]) {
656 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200658 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200660 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
661 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Now insert the new entry.
664 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200665 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
666 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100667 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ASSERT(!dfp[1].length);
669 ASSERT(!dfp[2].length);
670 }
671 }
672 /*
673 * The entry before us is free, merge with it.
674 */
675 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200676 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800677 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000678 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200679 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 xfs_dir2_data_log_unused(tp, bp, prevdup);
681 /*
682 * If the previous entry was in the table, the new entry
683 * is longer, so it will be in the table too. Remove
684 * the old one and add the new one.
685 */
686 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200687 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
688 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 /*
691 * Otherwise we need a scan if the new entry is big enough.
692 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100693 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100694 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200695 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698 /*
699 * The following entry is free, merge with it.
700 */
701 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200702 dfp = xfs_dir2_data_freefind(hdr, postdup);
703 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100704 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
705 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000706 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200707 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 xfs_dir2_data_log_unused(tp, bp, newdup);
709 /*
710 * If the following entry was in the table, the new entry
711 * is longer, so it will be in the table too. Remove
712 * the old one and add the new one.
713 */
714 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200715 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
716 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718 /*
719 * Otherwise we need a scan if the new entry is big enough.
720 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100721 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100722 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200723 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726 /*
727 * Neither neighbor is free. Make a new entry.
728 */
729 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200730 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100731 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
732 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000733 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200734 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200736 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738 *needscanp = needscan;
739}
740
741/*
742 * Take a byte range out of an existing unused space and make it un-free.
743 */
744void
745xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000746 struct xfs_trans *tp,
747 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 xfs_dir2_data_unused_t *dup, /* unused entry */
749 xfs_dir2_data_aoff_t offset, /* starting offset to use */
750 xfs_dir2_data_aoff_t len, /* length to use */
751 int *needlogp, /* out: need to log header */
752 int *needscanp) /* out: need regen bestfree */
753{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200754 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
756 int matchback; /* matches end of freespace */
757 int matchfront; /* matches start of freespace */
758 int needscan; /* need to regen bestfree */
759 xfs_dir2_data_unused_t *newdup; /* new unused entry */
760 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
761 int oldlen; /* old unused entry's length */
762
Dave Chinner1d9025e2012-06-22 18:50:14 +1000763 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200764 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
765 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100766 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200767 ASSERT(offset >= (char *)dup - (char *)hdr);
768 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
769 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
771 * Look up the entry in the bestfree table.
772 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200773 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100774 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200775 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 /*
777 * Check for alignment with front and back of the entry.
778 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200779 matchfront = (char *)dup - (char *)hdr == offset;
780 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ASSERT(*needscanp == 0);
782 needscan = 0;
783 /*
784 * If we matched it exactly we just need to get rid of it from
785 * the bestfree table.
786 */
787 if (matchfront && matchback) {
788 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200789 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200791 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793 }
794 /*
795 * We match the first part of the entry.
796 * Make a new entry with the remaining freespace.
797 */
798 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200799 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100800 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
801 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000802 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200803 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 xfs_dir2_data_log_unused(tp, bp, newdup);
805 /*
806 * If it was in the table, remove it 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 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100812 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200813 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * If we got inserted at the last slot,
816 * that means we don't know if there was a better
817 * choice for the last slot, or not. Rescan.
818 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200819 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 }
822 /*
823 * We match the last part of the entry.
824 * Trim the allocated space off the tail of the entry.
825 */
826 else if (matchback) {
827 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200828 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000829 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200830 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 xfs_dir2_data_log_unused(tp, bp, newdup);
832 /*
833 * If it was in the table, remove it and add the new one.
834 */
835 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200836 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
837 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100839 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200840 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * If we got inserted at the last slot,
843 * that means we don't know if there was a better
844 * choice for the last slot, or not. Rescan.
845 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200846 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
848 }
849 /*
850 * Poking out the middle of an entry.
851 * Make two new entries.
852 */
853 else {
854 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200855 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000856 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200857 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200859 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100860 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
861 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000862 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200863 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 xfs_dir2_data_log_unused(tp, bp, newdup2);
865 /*
866 * If the old entry was in the table, we need to scan
867 * if the 3rd entry was valid, since these entries
868 * are smaller than the old one.
869 * If we don't need to scan that means there were 1 or 2
870 * entries in the table, and removing the old and adding
871 * the 2 new will work.
872 */
873 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200874 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200876 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
877 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
878 xfs_dir2_data_freeinsert(hdr, newdup2,
879 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 }
882 }
883 *needscanp = needscan;
884}