blob: 1a43c8593c0077787781f73a8df28c8479fec1c8 [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 Chinnerda6958c2012-11-12 22:54:18 +1100188void
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 }
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
Dave Chinnerda6958c2012-11-12 22:54:18 +1100220int
221xfs_dir2_data_readahead(
222 struct xfs_trans *tp,
223 struct xfs_inode *dp,
224 xfs_dablk_t bno,
225 xfs_daddr_t mapped_bno)
226{
227 return xfs_da_reada_buf(tp, dp, bno, mapped_bno,
228 XFS_DATA_FORK, xfs_dir2_data_verify);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * Given a data block and an unused entry from that block,
233 * return the bestfree entry if any that corresponds to it.
234 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200235STATIC xfs_dir2_data_free_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236xfs_dir2_data_freefind(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200237 xfs_dir2_data_hdr_t *hdr, /* data block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 xfs_dir2_data_unused_t *dup) /* data unused entry */
239{
240 xfs_dir2_data_free_t *dfp; /* bestfree entry */
241 xfs_dir2_data_aoff_t off; /* offset value needed */
242#if defined(DEBUG) && defined(__KERNEL__)
243 int matched; /* matched the value */
244 int seenzero; /* saw a 0 bestfree entry */
245#endif
246
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200247 off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#if defined(DEBUG) && defined(__KERNEL__)
249 /*
250 * Validate some consistency in the bestfree table.
251 * Check order, non-overlapping entries, and if we find the
252 * one we're looking for it has to be exact.
253 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200254 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
255 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200256 for (dfp = &hdr->bestfree[0], seenzero = matched = 0;
257 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 dfp++) {
259 if (!dfp->offset) {
260 ASSERT(!dfp->length);
261 seenzero = 1;
262 continue;
263 }
264 ASSERT(seenzero == 0);
Nathan Scott70e73f52006-03-17 17:26:52 +1100265 if (be16_to_cpu(dfp->offset) == off) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 matched = 1;
Nathan Scottad354eb2006-03-17 17:27:37 +1100267 ASSERT(dfp->length == dup->length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100268 } else if (off < be16_to_cpu(dfp->offset))
Nathan Scottad354eb2006-03-17 17:27:37 +1100269 ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 else
Nathan Scott70e73f52006-03-17 17:26:52 +1100271 ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off);
Nathan Scottad354eb2006-03-17 17:27:37 +1100272 ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200273 if (dfp > &hdr->bestfree[0])
Nathan Scott70e73f52006-03-17 17:26:52 +1100274 ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276#endif
277 /*
278 * If this is smaller than the smallest bestfree entry,
279 * it can't be there since they're sorted.
280 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100281 if (be16_to_cpu(dup->length) <
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200282 be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return NULL;
284 /*
285 * Look at the three bestfree entries for our guy.
286 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200287 for (dfp = &hdr->bestfree[0];
288 dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 dfp++) {
290 if (!dfp->offset)
291 return NULL;
Nathan Scott70e73f52006-03-17 17:26:52 +1100292 if (be16_to_cpu(dfp->offset) == off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return dfp;
294 }
295 /*
296 * Didn't find it. This only happens if there are duplicate lengths.
297 */
298 return NULL;
299}
300
301/*
302 * Insert an unused-space entry into the bestfree table.
303 */
304xfs_dir2_data_free_t * /* entry inserted */
305xfs_dir2_data_freeinsert(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200306 xfs_dir2_data_hdr_t *hdr, /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 xfs_dir2_data_unused_t *dup, /* unused space */
308 int *loghead) /* log the data header (out) */
309{
310 xfs_dir2_data_free_t *dfp; /* bestfree table pointer */
311 xfs_dir2_data_free_t new; /* new bestfree entry */
312
313#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200314 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
315 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#endif
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200317 dfp = hdr->bestfree;
Nathan Scott70e73f52006-03-17 17:26:52 +1100318 new.length = dup->length;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200319 new.offset = cpu_to_be16((char *)dup - (char *)hdr);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * Insert at position 0, 1, or 2; or not at all.
323 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100324 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 dfp[2] = dfp[1];
326 dfp[1] = dfp[0];
327 dfp[0] = new;
328 *loghead = 1;
329 return &dfp[0];
330 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100331 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 dfp[2] = dfp[1];
333 dfp[1] = new;
334 *loghead = 1;
335 return &dfp[1];
336 }
Nathan Scott70e73f52006-03-17 17:26:52 +1100337 if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 dfp[2] = new;
339 *loghead = 1;
340 return &dfp[2];
341 }
342 return NULL;
343}
344
345/*
346 * Remove a bestfree entry from the table.
347 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000348STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349xfs_dir2_data_freeremove(
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200350 xfs_dir2_data_hdr_t *hdr, /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 xfs_dir2_data_free_t *dfp, /* bestfree entry pointer */
352 int *loghead) /* out: log data header */
353{
354#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200355 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
356 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#endif
358 /*
359 * It's the first entry, slide the next 2 up.
360 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200361 if (dfp == &hdr->bestfree[0]) {
362 hdr->bestfree[0] = hdr->bestfree[1];
363 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 /*
366 * It's the second entry, slide the 3rd entry up.
367 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200368 else if (dfp == &hdr->bestfree[1])
369 hdr->bestfree[1] = hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /*
371 * Must be the last entry.
372 */
373 else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200374 ASSERT(dfp == &hdr->bestfree[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /*
376 * Clear the 3rd entry, must be zero now.
377 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200378 hdr->bestfree[2].length = 0;
379 hdr->bestfree[2].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 *loghead = 1;
381}
382
383/*
384 * Given a data block, reconstruct its bestfree map.
385 */
386void
387xfs_dir2_data_freescan(
388 xfs_mount_t *mp, /* filesystem mount point */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200389 xfs_dir2_data_hdr_t *hdr, /* data block header */
Eric Sandeenef497f82007-05-08 13:48:49 +1000390 int *loghead) /* out: log data header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 xfs_dir2_block_tail_t *btp; /* block tail */
393 xfs_dir2_data_entry_t *dep; /* active data entry */
394 xfs_dir2_data_unused_t *dup; /* unused data entry */
395 char *endp; /* end of block's data */
396 char *p; /* current entry pointer */
397
398#ifdef __KERNEL__
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200399 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
400 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#endif
402 /*
403 * Start by clearing the table.
404 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200405 memset(hdr->bestfree, 0, sizeof(hdr->bestfree));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 *loghead = 1;
407 /*
408 * Set up pointers.
409 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200410 p = (char *)(hdr + 1);
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200411 if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200412 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000413 endp = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200415 endp = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /*
417 * Loop over the block's entries.
418 */
419 while (p < endp) {
420 dup = (xfs_dir2_data_unused_t *)p;
421 /*
422 * If it's a free entry, insert it.
423 */
Nathan Scottad354eb2006-03-17 17:27:37 +1100424 if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200425 ASSERT((char *)dup - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000426 be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200427 xfs_dir2_data_freeinsert(hdr, dup, loghead);
Nathan Scottad354eb2006-03-17 17:27:37 +1100428 p += be16_to_cpu(dup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 /*
431 * For active entries, check their tags and skip them.
432 */
433 else {
434 dep = (xfs_dir2_data_entry_t *)p;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200435 ASSERT((char *)dep - (char *)hdr ==
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000436 be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)));
437 p += xfs_dir2_data_entsize(dep->namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439 }
440}
441
442/*
443 * Initialize a data block at the given block number in the directory.
444 * Give back the buffer for the created block.
445 */
446int /* error */
447xfs_dir2_data_init(
448 xfs_da_args_t *args, /* directory operation args */
449 xfs_dir2_db_t blkno, /* logical dir block number */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000450 struct xfs_buf **bpp) /* output block buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000452 struct xfs_buf *bp; /* block buffer */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200453 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 xfs_inode_t *dp; /* incore directory inode */
455 xfs_dir2_data_unused_t *dup; /* unused entry pointer */
456 int error; /* error return value */
457 int i; /* bestfree index */
458 xfs_mount_t *mp; /* filesystem mount point */
459 xfs_trans_t *tp; /* transaction pointer */
460 int t; /* temp */
461
462 dp = args->dp;
463 mp = dp->i_mount;
464 tp = args->trans;
465 /*
466 * Get the buffer set up for the block.
467 */
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000468 error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 XFS_DATA_FORK);
470 if (error) {
471 return error;
472 }
473 ASSERT(bp != NULL);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 /*
476 * Initialize the header.
477 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000478 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200479 hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC);
480 hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200482 hdr->bestfree[i].length = 0;
483 hdr->bestfree[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Set up an unused entry for the block's body.
488 */
Christoph Hellwig0ba9cd82011-07-08 14:35:42 +0200489 dup = (xfs_dir2_data_unused_t *)(hdr + 1);
Nathan Scottad354eb2006-03-17 17:27:37 +1100490 dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200492 t = mp->m_dirblksize - (uint)sizeof(*hdr);
493 hdr->bestfree[0].length = cpu_to_be16(t);
Nathan Scottad354eb2006-03-17 17:27:37 +1100494 dup->length = cpu_to_be16(t);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200495 *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /*
497 * Log it and return it.
498 */
499 xfs_dir2_data_log_header(tp, bp);
500 xfs_dir2_data_log_unused(tp, bp, dup);
501 *bpp = bp;
502 return 0;
503}
504
505/*
506 * Log an active data entry from the block.
507 */
508void
509xfs_dir2_data_log_entry(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000510 struct xfs_trans *tp,
511 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 xfs_dir2_data_entry_t *dep) /* data entry pointer */
513{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000514 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200516 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
517 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200518
Dave Chinner1d9025e2012-06-22 18:50:14 +1000519 xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr),
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000520 (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200521 (char *)hdr - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524/*
525 * Log a data block header.
526 */
527void
528xfs_dir2_data_log_header(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000529 struct xfs_trans *tp,
530 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000532 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200534 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
535 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200536
Dave Chinner1d9025e2012-06-22 18:50:14 +1000537 xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * Log a data unused entry.
542 */
543void
544xfs_dir2_data_log_unused(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000545 struct xfs_trans *tp,
546 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 xfs_dir2_data_unused_t *dup) /* data unused pointer */
548{
Dave Chinner1d9025e2012-06-22 18:50:14 +1000549 xfs_dir2_data_hdr_t *hdr = bp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200551 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
552 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /*
555 * Log the first part of the unused entry.
556 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000557 xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 (uint)((char *)&dup->length + sizeof(dup->length) -
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200559 1 - (char *)hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * Log the end (tag) of the unused entry.
562 */
Dave Chinner1d9025e2012-06-22 18:50:14 +1000563 xfs_trans_log_buf(tp, bp,
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200564 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr),
565 (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 sizeof(xfs_dir2_data_off_t) - 1));
567}
568
569/*
570 * Make a byte range in the data block unused.
571 * Its current contents are unimportant.
572 */
573void
574xfs_dir2_data_make_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000575 struct xfs_trans *tp,
576 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 xfs_dir2_data_aoff_t offset, /* starting byte offset */
578 xfs_dir2_data_aoff_t len, /* length in bytes */
579 int *needlogp, /* out: log header */
580 int *needscanp) /* out: regen bestfree */
581{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200582 xfs_dir2_data_hdr_t *hdr; /* data block pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
584 char *endptr; /* end of data area */
585 xfs_mount_t *mp; /* filesystem mount point */
586 int needscan; /* need to regen bestfree */
587 xfs_dir2_data_unused_t *newdup; /* new unused entry */
588 xfs_dir2_data_unused_t *postdup; /* unused entry after us */
589 xfs_dir2_data_unused_t *prevdup; /* unused entry before us */
590
591 mp = tp->t_mountp;
Dave Chinner1d9025e2012-06-22 18:50:14 +1000592 hdr = bp->b_addr;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Figure out where the end of the data area is.
596 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200597 if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC))
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200598 endptr = (char *)hdr + mp->m_dirblksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else {
600 xfs_dir2_block_tail_t *btp; /* block tail */
601
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200602 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200603 btp = xfs_dir2_block_tail_p(mp, hdr);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000604 endptr = (char *)xfs_dir2_block_leaf_p(btp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 /*
607 * If this isn't the start of the block, then back up to
608 * the previous entry and see if it's free.
609 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200610 if (offset > sizeof(*hdr)) {
Nathan Scott3d693c62006-03-17 17:28:27 +1100611 __be16 *tagp; /* tag just before us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200613 tagp = (__be16 *)((char *)hdr + offset) - 1;
614 prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp));
Nathan Scottad354eb2006-03-17 17:27:37 +1100615 if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 prevdup = NULL;
617 } else
618 prevdup = NULL;
619 /*
620 * If this isn't the end of the block, see if the entry after
621 * us is free.
622 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200623 if ((char *)hdr + offset + len < endptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 postdup =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200625 (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100626 if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 postdup = NULL;
628 } else
629 postdup = NULL;
630 ASSERT(*needscanp == 0);
631 needscan = 0;
632 /*
633 * Previous and following entries are both free,
634 * merge everything into a single free entry.
635 */
636 if (prevdup && postdup) {
637 xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */
638
639 /*
640 * See if prevdup and/or postdup are in bestfree table.
641 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200642 dfp = xfs_dir2_data_freefind(hdr, prevdup);
643 dfp2 = xfs_dir2_data_freefind(hdr, postdup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
645 * We need a rescan unless there are exactly 2 free entries
646 * namely our two. Then we know what's happening, otherwise
647 * since the third bestfree is there, there might be more
648 * entries.
649 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200650 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
652 * Fix up the new big freespace.
653 */
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800654 be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000655 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200656 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 xfs_dir2_data_log_unused(tp, bp, prevdup);
658 if (!needscan) {
659 /*
660 * Has to be the case that entries 0 and 1 are
661 * dfp and dfp2 (don't know which is which), and
662 * entry 2 is empty.
663 * Remove entry 1 first then entry 0.
664 */
665 ASSERT(dfp && dfp2);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200666 if (dfp == &hdr->bestfree[1]) {
667 dfp = &hdr->bestfree[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 ASSERT(dfp2 == dfp);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200669 dfp2 = &hdr->bestfree[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200671 xfs_dir2_data_freeremove(hdr, dfp2, needlogp);
672 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * Now insert the new entry.
675 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200676 dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
677 ASSERT(dfp == &hdr->bestfree[0]);
Nathan Scottad354eb2006-03-17 17:27:37 +1100678 ASSERT(dfp->length == prevdup->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 ASSERT(!dfp[1].length);
680 ASSERT(!dfp[2].length);
681 }
682 }
683 /*
684 * The entry before us is free, merge with it.
685 */
686 else if (prevdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200687 dfp = xfs_dir2_data_freefind(hdr, prevdup);
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800688 be16_add_cpu(&prevdup->length, len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000689 *xfs_dir2_data_unused_tag_p(prevdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200690 cpu_to_be16((char *)prevdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 xfs_dir2_data_log_unused(tp, bp, prevdup);
692 /*
693 * If the previous entry was in the table, the new entry
694 * is longer, so it will be in the table too. Remove
695 * the old one and add the new one.
696 */
697 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200698 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
699 xfs_dir2_data_freeinsert(hdr, prevdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 /*
702 * Otherwise we need a scan if the new entry is big enough.
703 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100704 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100705 needscan = be16_to_cpu(prevdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200706 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 /*
710 * The following entry is free, merge with it.
711 */
712 else if (postdup) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200713 dfp = xfs_dir2_data_freefind(hdr, postdup);
714 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100715 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
716 newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000717 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200718 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 xfs_dir2_data_log_unused(tp, bp, newdup);
720 /*
721 * If the following entry was in the table, the new entry
722 * is longer, so it will be in the table too. Remove
723 * the old one and add the new one.
724 */
725 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200726 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
727 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729 /*
730 * Otherwise we need a scan if the new entry is big enough.
731 */
Nathan Scott70e73f52006-03-17 17:26:52 +1100732 else {
Nathan Scottad354eb2006-03-17 17:27:37 +1100733 needscan = be16_to_cpu(newdup->length) >
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200734 be16_to_cpu(hdr->bestfree[2].length);
Nathan Scott70e73f52006-03-17 17:26:52 +1100735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737 /*
738 * Neither neighbor is free. Make a new entry.
739 */
740 else {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200741 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset);
Nathan Scottad354eb2006-03-17 17:27:37 +1100742 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
743 newdup->length = cpu_to_be16(len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000744 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200745 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200747 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 *needscanp = needscan;
750}
751
752/*
753 * Take a byte range out of an existing unused space and make it un-free.
754 */
755void
756xfs_dir2_data_use_free(
Dave Chinner1d9025e2012-06-22 18:50:14 +1000757 struct xfs_trans *tp,
758 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 xfs_dir2_data_unused_t *dup, /* unused entry */
760 xfs_dir2_data_aoff_t offset, /* starting offset to use */
761 xfs_dir2_data_aoff_t len, /* length to use */
762 int *needlogp, /* out: need to log header */
763 int *needscanp) /* out: need regen bestfree */
764{
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200765 xfs_dir2_data_hdr_t *hdr; /* data block header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 xfs_dir2_data_free_t *dfp; /* bestfree pointer */
767 int matchback; /* matches end of freespace */
768 int matchfront; /* matches start of freespace */
769 int needscan; /* need to regen bestfree */
770 xfs_dir2_data_unused_t *newdup; /* new unused entry */
771 xfs_dir2_data_unused_t *newdup2; /* another new unused entry */
772 int oldlen; /* old unused entry's length */
773
Dave Chinner1d9025e2012-06-22 18:50:14 +1000774 hdr = bp->b_addr;
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200775 ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) ||
776 hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC));
Nathan Scottad354eb2006-03-17 17:27:37 +1100777 ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200778 ASSERT(offset >= (char *)dup - (char *)hdr);
779 ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr);
780 ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
782 * Look up the entry in the bestfree table.
783 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200784 dfp = xfs_dir2_data_freefind(hdr, dup);
Nathan Scottad354eb2006-03-17 17:27:37 +1100785 oldlen = be16_to_cpu(dup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200786 ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 /*
788 * Check for alignment with front and back of the entry.
789 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200790 matchfront = (char *)dup - (char *)hdr == offset;
791 matchback = (char *)dup + oldlen - (char *)hdr == offset + len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ASSERT(*needscanp == 0);
793 needscan = 0;
794 /*
795 * If we matched it exactly we just need to get rid of it from
796 * the bestfree table.
797 */
798 if (matchfront && matchback) {
799 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200800 needscan = (hdr->bestfree[2].offset != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!needscan)
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200802 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
805 /*
806 * We match the first part of the entry.
807 * Make a new entry with the remaining freespace.
808 */
809 else if (matchfront) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200810 newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100811 newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
812 newdup->length = cpu_to_be16(oldlen - len);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000813 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200814 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 xfs_dir2_data_log_unused(tp, bp, newdup);
816 /*
817 * If it was in the table, remove it and add the new one.
818 */
819 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200820 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
821 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100823 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200824 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * If we got inserted at the last slot,
827 * that means we don't know if there was a better
828 * choice for the last slot, or not. Rescan.
829 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200830 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 }
833 /*
834 * We match the last part of the entry.
835 * Trim the allocated space off the tail of the entry.
836 */
837 else if (matchback) {
838 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200839 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000840 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200841 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 xfs_dir2_data_log_unused(tp, bp, newdup);
843 /*
844 * If it was in the table, remove it and add the new one.
845 */
846 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200847 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
848 dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 ASSERT(dfp != NULL);
Nathan Scottad354eb2006-03-17 17:27:37 +1100850 ASSERT(dfp->length == newdup->length);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200851 ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
853 * If we got inserted at the last slot,
854 * that means we don't know if there was a better
855 * choice for the last slot, or not. Rescan.
856 */
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200857 needscan = dfp == &hdr->bestfree[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 }
860 /*
861 * Poking out the middle of an entry.
862 * Make two new entries.
863 */
864 else {
865 newdup = dup;
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200866 newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup);
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000867 *xfs_dir2_data_unused_tag_p(newdup) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200868 cpu_to_be16((char *)newdup - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 xfs_dir2_data_log_unused(tp, bp, newdup);
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200870 newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len);
Nathan Scottad354eb2006-03-17 17:27:37 +1100871 newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG);
872 newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length));
Christoph Hellwigbbaaf532007-06-28 16:43:50 +1000873 *xfs_dir2_data_unused_tag_p(newdup2) =
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200874 cpu_to_be16((char *)newdup2 - (char *)hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 xfs_dir2_data_log_unused(tp, bp, newdup2);
876 /*
877 * If the old entry was in the table, we need to scan
878 * if the 3rd entry was valid, since these entries
879 * are smaller than the old one.
880 * If we don't need to scan that means there were 1 or 2
881 * entries in the table, and removing the old and adding
882 * the 2 new will work.
883 */
884 if (dfp) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200885 needscan = (hdr->bestfree[2].length != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!needscan) {
Christoph Hellwigc2066e22011-07-08 14:35:38 +0200887 xfs_dir2_data_freeremove(hdr, dfp, needlogp);
888 xfs_dir2_data_freeinsert(hdr, newdup, needlogp);
889 xfs_dir2_data_freeinsert(hdr, newdup2,
890 needlogp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892 }
893 }
894 *needscanp = needscan;
895}