| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. | 
|  | 3 | * All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. | 
|  | 8 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * | 
| Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" | 
|  | 22 | #include "xfs_trans.h" | 
|  | 23 | #include "xfs_sb.h" | 
| David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 24 | #include "xfs_ag.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_da_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "xfs_bmap_btree.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_dinode.h" | 
|  | 29 | #include "xfs_inode.h" | 
| Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 30 | #include "xfs_dir2_format.h" | 
|  | 31 | #include "xfs_dir2_priv.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_error.h" | 
|  | 33 |  | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 34 | STATIC xfs_dir2_data_free_t * | 
|  | 35 | xfs_dir2_data_freefind(xfs_dir2_data_hdr_t *hdr, xfs_dir2_data_unused_t *dup); | 
|  | 36 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #ifdef DEBUG | 
|  | 38 | /* | 
|  | 39 | * Check the consistency of the data block. | 
|  | 40 | * The input can also be a block-format directory. | 
|  | 41 | * Pop an assert if we find anything bad. | 
|  | 42 | */ | 
|  | 43 | void | 
|  | 44 | xfs_dir2_data_check( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 45 | struct xfs_inode	*dp,		/* incore inode pointer */ | 
|  | 46 | struct xfs_buf		*bp)		/* data block's buffer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { | 
|  | 48 | xfs_dir2_dataptr_t	addr;		/* addr for leaf lookup */ | 
|  | 49 | xfs_dir2_data_free_t	*bf;		/* bestfree table */ | 
|  | 50 | xfs_dir2_block_tail_t	*btp=NULL;	/* block tail */ | 
|  | 51 | int			count;		/* count of entries found */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 52 | xfs_dir2_data_hdr_t	*hdr;		/* data block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | xfs_dir2_data_entry_t	*dep;		/* data entry */ | 
|  | 54 | xfs_dir2_data_free_t	*dfp;		/* bestfree entry */ | 
|  | 55 | xfs_dir2_data_unused_t	*dup;		/* unused entry */ | 
|  | 56 | char			*endp;		/* end of useful data */ | 
|  | 57 | int			freeseen;	/* mask of bestfrees seen */ | 
|  | 58 | xfs_dahash_t		hash;		/* hash of current name */ | 
|  | 59 | int			i;		/* leaf index */ | 
|  | 60 | int			lastfree;	/* last entry was unused */ | 
|  | 61 | xfs_dir2_leaf_entry_t	*lep=NULL;	/* block leaf entries */ | 
|  | 62 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 63 | char			*p;		/* current data position */ | 
|  | 64 | int			stale;		/* count of stale leaves */ | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 65 | struct xfs_name		name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | mp = dp->i_mount; | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 68 | hdr = bp->b_addr; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 69 | bf = hdr->bestfree; | 
| Christoph Hellwig | 0ba9cd8 | 2011-07-08 14:35:42 +0200 | [diff] [blame] | 70 | p = (char *)(hdr + 1); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 71 |  | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 72 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 73 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 74 | lep = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | endp = (char *)lep; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 76 | } else { | 
|  | 77 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC)); | 
|  | 78 | endp = (char *)hdr + mp->m_dirblksize; | 
|  | 79 | } | 
|  | 80 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | count = lastfree = freeseen = 0; | 
|  | 82 | /* | 
|  | 83 | * Account for zero bestfree entries. | 
|  | 84 | */ | 
|  | 85 | if (!bf[0].length) { | 
|  | 86 | ASSERT(!bf[0].offset); | 
|  | 87 | freeseen |= 1 << 0; | 
|  | 88 | } | 
|  | 89 | if (!bf[1].length) { | 
|  | 90 | ASSERT(!bf[1].offset); | 
|  | 91 | freeseen |= 1 << 1; | 
|  | 92 | } | 
|  | 93 | if (!bf[2].length) { | 
|  | 94 | ASSERT(!bf[2].offset); | 
|  | 95 | freeseen |= 1 << 2; | 
|  | 96 | } | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 97 | ASSERT(be16_to_cpu(bf[0].length) >= be16_to_cpu(bf[1].length)); | 
|  | 98 | ASSERT(be16_to_cpu(bf[1].length) >= be16_to_cpu(bf[2].length)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | /* | 
|  | 100 | * Loop over the data/unused entries. | 
|  | 101 | */ | 
|  | 102 | while (p < endp) { | 
|  | 103 | dup = (xfs_dir2_data_unused_t *)p; | 
|  | 104 | /* | 
|  | 105 | * If it's unused, look for the space in the bestfree table. | 
|  | 106 | * If we find it, account for that, else make sure it | 
|  | 107 | * doesn't need to be there. | 
|  | 108 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 109 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | ASSERT(lastfree == 0); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 111 | ASSERT(be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) == | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 112 | (char *)dup - (char *)hdr); | 
|  | 113 | dfp = xfs_dir2_data_freefind(hdr, dup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | if (dfp) { | 
|  | 115 | i = (int)(dfp - bf); | 
|  | 116 | ASSERT((freeseen & (1 << i)) == 0); | 
|  | 117 | freeseen |= 1 << i; | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 118 | } else { | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 119 | ASSERT(be16_to_cpu(dup->length) <= | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 120 | be16_to_cpu(bf[2].length)); | 
|  | 121 | } | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 122 | p += be16_to_cpu(dup->length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | lastfree = 1; | 
|  | 124 | continue; | 
|  | 125 | } | 
|  | 126 | /* | 
|  | 127 | * It's a real entry.  Validate the fields. | 
|  | 128 | * If this is a block directory then make sure it's | 
|  | 129 | * in the leaf section of the block. | 
|  | 130 | * The linear search is crude but this is DEBUG code. | 
|  | 131 | */ | 
|  | 132 | dep = (xfs_dir2_data_entry_t *)p; | 
|  | 133 | ASSERT(dep->namelen != 0); | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 134 | ASSERT(xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber)) == 0); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 135 | ASSERT(be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep)) == | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 136 | (char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | count++; | 
|  | 138 | lastfree = 0; | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 139 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) { | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 140 | addr = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | (xfs_dir2_data_aoff_t) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 142 | ((char *)dep - (char *)hdr)); | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 143 | name.name = dep->name; | 
|  | 144 | name.len = dep->namelen; | 
|  | 145 | hash = mp->m_dirnameops->hashname(&name); | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 146 | for (i = 0; i < be32_to_cpu(btp->count); i++) { | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 147 | if (be32_to_cpu(lep[i].address) == addr && | 
|  | 148 | be32_to_cpu(lep[i].hashval) == hash) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | break; | 
|  | 150 | } | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 151 | ASSERT(i < be32_to_cpu(btp->count)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 153 | p += xfs_dir2_data_entsize(dep->namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } | 
|  | 155 | /* | 
|  | 156 | * Need to have seen all the entries and all the bestfree slots. | 
|  | 157 | */ | 
|  | 158 | ASSERT(freeseen == 7); | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 159 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) { | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 160 | for (i = stale = 0; i < be32_to_cpu(btp->count); i++) { | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 161 | if (lep[i].address == | 
|  | 162 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | stale++; | 
|  | 164 | if (i > 0) | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 165 | ASSERT(be32_to_cpu(lep[i].hashval) >= be32_to_cpu(lep[i - 1].hashval)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 167 | ASSERT(count == be32_to_cpu(btp->count) - be32_to_cpu(btp->stale)); | 
|  | 168 | ASSERT(stale == be32_to_cpu(btp->stale)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } | 
|  | 170 | } | 
|  | 171 | #endif | 
|  | 172 |  | 
|  | 173 | /* | 
|  | 174 | * Given a data block and an unused entry from that block, | 
|  | 175 | * return the bestfree entry if any that corresponds to it. | 
|  | 176 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 177 | STATIC xfs_dir2_data_free_t * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | xfs_dir2_data_freefind( | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 179 | xfs_dir2_data_hdr_t	*hdr,		/* data block */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | xfs_dir2_data_unused_t	*dup)		/* data unused entry */ | 
|  | 181 | { | 
|  | 182 | xfs_dir2_data_free_t	*dfp;		/* bestfree entry */ | 
|  | 183 | xfs_dir2_data_aoff_t	off;		/* offset value needed */ | 
|  | 184 | #if defined(DEBUG) && defined(__KERNEL__) | 
|  | 185 | int			matched;	/* matched the value */ | 
|  | 186 | int			seenzero;	/* saw a 0 bestfree entry */ | 
|  | 187 | #endif | 
|  | 188 |  | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 189 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | #if defined(DEBUG) && defined(__KERNEL__) | 
|  | 191 | /* | 
|  | 192 | * Validate some consistency in the bestfree table. | 
|  | 193 | * Check order, non-overlapping entries, and if we find the | 
|  | 194 | * one we're looking for it has to be exact. | 
|  | 195 | */ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 196 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 197 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 198 | for (dfp = &hdr->bestfree[0], seenzero = matched = 0; | 
|  | 199 | dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | dfp++) { | 
|  | 201 | if (!dfp->offset) { | 
|  | 202 | ASSERT(!dfp->length); | 
|  | 203 | seenzero = 1; | 
|  | 204 | continue; | 
|  | 205 | } | 
|  | 206 | ASSERT(seenzero == 0); | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 207 | if (be16_to_cpu(dfp->offset) == off) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | matched = 1; | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 209 | ASSERT(dfp->length == dup->length); | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 210 | } else if (off < be16_to_cpu(dfp->offset)) | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 211 | ASSERT(off + be16_to_cpu(dup->length) <= be16_to_cpu(dfp->offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | else | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 213 | ASSERT(be16_to_cpu(dfp->offset) + be16_to_cpu(dfp->length) <= off); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 214 | ASSERT(matched || be16_to_cpu(dfp->length) >= be16_to_cpu(dup->length)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 215 | if (dfp > &hdr->bestfree[0]) | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 216 | ASSERT(be16_to_cpu(dfp[-1].length) >= be16_to_cpu(dfp[0].length)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } | 
|  | 218 | #endif | 
|  | 219 | /* | 
|  | 220 | * If this is smaller than the smallest bestfree entry, | 
|  | 221 | * it can't be there since they're sorted. | 
|  | 222 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 223 | if (be16_to_cpu(dup->length) < | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 224 | be16_to_cpu(hdr->bestfree[XFS_DIR2_DATA_FD_COUNT - 1].length)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return NULL; | 
|  | 226 | /* | 
|  | 227 | * Look at the three bestfree entries for our guy. | 
|  | 228 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 229 | for (dfp = &hdr->bestfree[0]; | 
|  | 230 | dfp < &hdr->bestfree[XFS_DIR2_DATA_FD_COUNT]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | dfp++) { | 
|  | 232 | if (!dfp->offset) | 
|  | 233 | return NULL; | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 234 | if (be16_to_cpu(dfp->offset) == off) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return dfp; | 
|  | 236 | } | 
|  | 237 | /* | 
|  | 238 | * Didn't find it.  This only happens if there are duplicate lengths. | 
|  | 239 | */ | 
|  | 240 | return NULL; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | /* | 
|  | 244 | * Insert an unused-space entry into the bestfree table. | 
|  | 245 | */ | 
|  | 246 | xfs_dir2_data_free_t *				/* entry inserted */ | 
|  | 247 | xfs_dir2_data_freeinsert( | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 248 | xfs_dir2_data_hdr_t	*hdr,		/* data block pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | xfs_dir2_data_unused_t	*dup,		/* unused space */ | 
|  | 250 | int			*loghead)	/* log the data header (out) */ | 
|  | 251 | { | 
|  | 252 | xfs_dir2_data_free_t	*dfp;		/* bestfree table pointer */ | 
|  | 253 | xfs_dir2_data_free_t	new;		/* new bestfree entry */ | 
|  | 254 |  | 
|  | 255 | #ifdef __KERNEL__ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 256 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 257 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | #endif | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 259 | dfp = hdr->bestfree; | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 260 | new.length = dup->length; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 261 | new.offset = cpu_to_be16((char *)dup - (char *)hdr); | 
|  | 262 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | /* | 
|  | 264 | * Insert at position 0, 1, or 2; or not at all. | 
|  | 265 | */ | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 266 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | dfp[2] = dfp[1]; | 
|  | 268 | dfp[1] = dfp[0]; | 
|  | 269 | dfp[0] = new; | 
|  | 270 | *loghead = 1; | 
|  | 271 | return &dfp[0]; | 
|  | 272 | } | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 273 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | dfp[2] = dfp[1]; | 
|  | 275 | dfp[1] = new; | 
|  | 276 | *loghead = 1; | 
|  | 277 | return &dfp[1]; | 
|  | 278 | } | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 279 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | dfp[2] = new; | 
|  | 281 | *loghead = 1; | 
|  | 282 | return &dfp[2]; | 
|  | 283 | } | 
|  | 284 | return NULL; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | /* | 
|  | 288 | * Remove a bestfree entry from the table. | 
|  | 289 | */ | 
| Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 290 | STATIC void | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | xfs_dir2_data_freeremove( | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 292 | xfs_dir2_data_hdr_t	*hdr,		/* data block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | xfs_dir2_data_free_t	*dfp,		/* bestfree entry pointer */ | 
|  | 294 | int			*loghead)	/* out: log data header */ | 
|  | 295 | { | 
|  | 296 | #ifdef __KERNEL__ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 297 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 298 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | #endif | 
|  | 300 | /* | 
|  | 301 | * It's the first entry, slide the next 2 up. | 
|  | 302 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 303 | if (dfp == &hdr->bestfree[0]) { | 
|  | 304 | hdr->bestfree[0] = hdr->bestfree[1]; | 
|  | 305 | hdr->bestfree[1] = hdr->bestfree[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } | 
|  | 307 | /* | 
|  | 308 | * It's the second entry, slide the 3rd entry up. | 
|  | 309 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 310 | else if (dfp == &hdr->bestfree[1]) | 
|  | 311 | hdr->bestfree[1] = hdr->bestfree[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | /* | 
|  | 313 | * Must be the last entry. | 
|  | 314 | */ | 
|  | 315 | else | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 316 | ASSERT(dfp == &hdr->bestfree[2]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* | 
|  | 318 | * Clear the 3rd entry, must be zero now. | 
|  | 319 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 320 | hdr->bestfree[2].length = 0; | 
|  | 321 | hdr->bestfree[2].offset = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | *loghead = 1; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | /* | 
|  | 326 | * Given a data block, reconstruct its bestfree map. | 
|  | 327 | */ | 
|  | 328 | void | 
|  | 329 | xfs_dir2_data_freescan( | 
|  | 330 | xfs_mount_t		*mp,		/* filesystem mount point */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 331 | xfs_dir2_data_hdr_t	*hdr,		/* data block header */ | 
| Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 332 | int			*loghead)	/* out: log data header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { | 
|  | 334 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 335 | xfs_dir2_data_entry_t	*dep;		/* active data entry */ | 
|  | 336 | xfs_dir2_data_unused_t	*dup;		/* unused data entry */ | 
|  | 337 | char			*endp;		/* end of block's data */ | 
|  | 338 | char			*p;		/* current entry pointer */ | 
|  | 339 |  | 
|  | 340 | #ifdef __KERNEL__ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 341 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 342 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | #endif | 
|  | 344 | /* | 
|  | 345 | * Start by clearing the table. | 
|  | 346 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 347 | memset(hdr->bestfree, 0, sizeof(hdr->bestfree)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | *loghead = 1; | 
|  | 349 | /* | 
|  | 350 | * Set up pointers. | 
|  | 351 | */ | 
| Christoph Hellwig | 0ba9cd8 | 2011-07-08 14:35:42 +0200 | [diff] [blame] | 352 | p = (char *)(hdr + 1); | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 353 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 354 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 355 | endp = (char *)xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | } else | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 357 | endp = (char *)hdr + mp->m_dirblksize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* | 
|  | 359 | * Loop over the block's entries. | 
|  | 360 | */ | 
|  | 361 | while (p < endp) { | 
|  | 362 | dup = (xfs_dir2_data_unused_t *)p; | 
|  | 363 | /* | 
|  | 364 | * If it's a free entry, insert it. | 
|  | 365 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 366 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 367 | ASSERT((char *)dup - (char *)hdr == | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 368 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 369 | xfs_dir2_data_freeinsert(hdr, dup, loghead); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 370 | p += be16_to_cpu(dup->length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } | 
|  | 372 | /* | 
|  | 373 | * For active entries, check their tags and skip them. | 
|  | 374 | */ | 
|  | 375 | else { | 
|  | 376 | dep = (xfs_dir2_data_entry_t *)p; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 377 | ASSERT((char *)dep - (char *)hdr == | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 378 | be16_to_cpu(*xfs_dir2_data_entry_tag_p(dep))); | 
|  | 379 | p += xfs_dir2_data_entsize(dep->namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } | 
|  | 381 | } | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | /* | 
|  | 385 | * Initialize a data block at the given block number in the directory. | 
|  | 386 | * Give back the buffer for the created block. | 
|  | 387 | */ | 
|  | 388 | int						/* error */ | 
|  | 389 | xfs_dir2_data_init( | 
|  | 390 | xfs_da_args_t		*args,		/* directory operation args */ | 
|  | 391 | xfs_dir2_db_t		blkno,		/* logical dir block number */ | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 392 | struct xfs_buf		**bpp)		/* output block buffer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 394 | struct xfs_buf		*bp;		/* block buffer */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 395 | xfs_dir2_data_hdr_t	*hdr;		/* data block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | xfs_inode_t		*dp;		/* incore directory inode */ | 
|  | 397 | xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */ | 
|  | 398 | int			error;		/* error return value */ | 
|  | 399 | int			i;		/* bestfree index */ | 
|  | 400 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 401 | xfs_trans_t		*tp;		/* transaction pointer */ | 
|  | 402 | int                     t;              /* temp */ | 
|  | 403 |  | 
|  | 404 | dp = args->dp; | 
|  | 405 | mp = dp->i_mount; | 
|  | 406 | tp = args->trans; | 
|  | 407 | /* | 
|  | 408 | * Get the buffer set up for the block. | 
|  | 409 | */ | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 410 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, blkno), -1, &bp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | XFS_DATA_FORK); | 
|  | 412 | if (error) { | 
|  | 413 | return error; | 
|  | 414 | } | 
|  | 415 | ASSERT(bp != NULL); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 416 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /* | 
|  | 418 | * Initialize the header. | 
|  | 419 | */ | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 420 | hdr = bp->b_addr; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 421 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); | 
|  | 422 | hdr->bestfree[0].offset = cpu_to_be16(sizeof(*hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 424 | hdr->bestfree[i].length = 0; | 
|  | 425 | hdr->bestfree[i].offset = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | } | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 427 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* | 
|  | 429 | * Set up an unused entry for the block's body. | 
|  | 430 | */ | 
| Christoph Hellwig | 0ba9cd8 | 2011-07-08 14:35:42 +0200 | [diff] [blame] | 431 | dup = (xfs_dir2_data_unused_t *)(hdr + 1); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 432 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 |  | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 434 | t = mp->m_dirblksize - (uint)sizeof(*hdr); | 
|  | 435 | hdr->bestfree[0].length = cpu_to_be16(t); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 436 | dup->length = cpu_to_be16(t); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 437 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | /* | 
|  | 439 | * Log it and return it. | 
|  | 440 | */ | 
|  | 441 | xfs_dir2_data_log_header(tp, bp); | 
|  | 442 | xfs_dir2_data_log_unused(tp, bp, dup); | 
|  | 443 | *bpp = bp; | 
|  | 444 | return 0; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | /* | 
|  | 448 | * Log an active data entry from the block. | 
|  | 449 | */ | 
|  | 450 | void | 
|  | 451 | xfs_dir2_data_log_entry( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 452 | struct xfs_trans	*tp, | 
|  | 453 | struct xfs_buf		*bp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | xfs_dir2_data_entry_t	*dep)		/* data entry pointer */ | 
|  | 455 | { | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 456 | xfs_dir2_data_hdr_t	*hdr = bp->b_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 |  | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 458 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 459 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 460 |  | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 461 | xfs_trans_log_buf(tp, bp, (uint)((char *)dep - (char *)hdr), | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 462 | (uint)((char *)(xfs_dir2_data_entry_tag_p(dep) + 1) - | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 463 | (char *)hdr - 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
|  | 466 | /* | 
|  | 467 | * Log a data block header. | 
|  | 468 | */ | 
|  | 469 | void | 
|  | 470 | xfs_dir2_data_log_header( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 471 | struct xfs_trans	*tp, | 
|  | 472 | struct xfs_buf		*bp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 474 | xfs_dir2_data_hdr_t	*hdr = bp->b_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 |  | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 476 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 477 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 478 |  | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 479 | xfs_trans_log_buf(tp, bp, 0, sizeof(*hdr) - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
|  | 482 | /* | 
|  | 483 | * Log a data unused entry. | 
|  | 484 | */ | 
|  | 485 | void | 
|  | 486 | xfs_dir2_data_log_unused( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 487 | struct xfs_trans	*tp, | 
|  | 488 | struct xfs_buf		*bp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | xfs_dir2_data_unused_t	*dup)		/* data unused pointer */ | 
|  | 490 | { | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 491 | xfs_dir2_data_hdr_t	*hdr = bp->b_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 |  | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 493 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 494 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 495 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* | 
|  | 497 | * Log the first part of the unused entry. | 
|  | 498 | */ | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 499 | xfs_trans_log_buf(tp, bp, (uint)((char *)dup - (char *)hdr), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | (uint)((char *)&dup->length + sizeof(dup->length) - | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 501 | 1 - (char *)hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | /* | 
|  | 503 | * Log the end (tag) of the unused entry. | 
|  | 504 | */ | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 505 | xfs_trans_log_buf(tp, bp, | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 506 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr), | 
|  | 507 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr + | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | sizeof(xfs_dir2_data_off_t) - 1)); | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | /* | 
|  | 512 | * Make a byte range in the data block unused. | 
|  | 513 | * Its current contents are unimportant. | 
|  | 514 | */ | 
|  | 515 | void | 
|  | 516 | xfs_dir2_data_make_free( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 517 | struct xfs_trans	*tp, | 
|  | 518 | struct xfs_buf		*bp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | xfs_dir2_data_aoff_t	offset,		/* starting byte offset */ | 
|  | 520 | xfs_dir2_data_aoff_t	len,		/* length in bytes */ | 
|  | 521 | int			*needlogp,	/* out: log header */ | 
|  | 522 | int			*needscanp)	/* out: regen bestfree */ | 
|  | 523 | { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 524 | xfs_dir2_data_hdr_t	*hdr;		/* data block pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */ | 
|  | 526 | char			*endptr;	/* end of data area */ | 
|  | 527 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 528 | int			needscan;	/* need to regen bestfree */ | 
|  | 529 | xfs_dir2_data_unused_t	*newdup;	/* new unused entry */ | 
|  | 530 | xfs_dir2_data_unused_t	*postdup;	/* unused entry after us */ | 
|  | 531 | xfs_dir2_data_unused_t	*prevdup;	/* unused entry before us */ | 
|  | 532 |  | 
|  | 533 | mp = tp->t_mountp; | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 534 | hdr = bp->b_addr; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 535 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | /* | 
|  | 537 | * Figure out where the end of the data area is. | 
|  | 538 | */ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 539 | if (hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC)) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 540 | endptr = (char *)hdr + mp->m_dirblksize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | else { | 
|  | 542 | xfs_dir2_block_tail_t	*btp;	/* block tail */ | 
|  | 543 |  | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 544 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 545 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 546 | endptr = (char *)xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } | 
|  | 548 | /* | 
|  | 549 | * If this isn't the start of the block, then back up to | 
|  | 550 | * the previous entry and see if it's free. | 
|  | 551 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 552 | if (offset > sizeof(*hdr)) { | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 553 | __be16			*tagp;	/* tag just before us */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 |  | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 555 | tagp = (__be16 *)((char *)hdr + offset) - 1; | 
|  | 556 | prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 557 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | prevdup = NULL; | 
|  | 559 | } else | 
|  | 560 | prevdup = NULL; | 
|  | 561 | /* | 
|  | 562 | * If this isn't the end of the block, see if the entry after | 
|  | 563 | * us is free. | 
|  | 564 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 565 | if ((char *)hdr + offset + len < endptr) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | postdup = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 567 | (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 568 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | postdup = NULL; | 
|  | 570 | } else | 
|  | 571 | postdup = NULL; | 
|  | 572 | ASSERT(*needscanp == 0); | 
|  | 573 | needscan = 0; | 
|  | 574 | /* | 
|  | 575 | * Previous and following entries are both free, | 
|  | 576 | * merge everything into a single free entry. | 
|  | 577 | */ | 
|  | 578 | if (prevdup && postdup) { | 
|  | 579 | xfs_dir2_data_free_t	*dfp2;	/* another bestfree pointer */ | 
|  | 580 |  | 
|  | 581 | /* | 
|  | 582 | * See if prevdup and/or postdup are in bestfree table. | 
|  | 583 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 584 | dfp = xfs_dir2_data_freefind(hdr, prevdup); | 
|  | 585 | dfp2 = xfs_dir2_data_freefind(hdr, postdup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | /* | 
|  | 587 | * We need a rescan unless there are exactly 2 free entries | 
|  | 588 | * namely our two.  Then we know what's happening, otherwise | 
|  | 589 | * since the third bestfree is there, there might be more | 
|  | 590 | * entries. | 
|  | 591 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 592 | needscan = (hdr->bestfree[2].length != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* | 
|  | 594 | * Fix up the new big freespace. | 
|  | 595 | */ | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 596 | be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length)); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 597 | *xfs_dir2_data_unused_tag_p(prevdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 598 | cpu_to_be16((char *)prevdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | xfs_dir2_data_log_unused(tp, bp, prevdup); | 
|  | 600 | if (!needscan) { | 
|  | 601 | /* | 
|  | 602 | * Has to be the case that entries 0 and 1 are | 
|  | 603 | * dfp and dfp2 (don't know which is which), and | 
|  | 604 | * entry 2 is empty. | 
|  | 605 | * Remove entry 1 first then entry 0. | 
|  | 606 | */ | 
|  | 607 | ASSERT(dfp && dfp2); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 608 | if (dfp == &hdr->bestfree[1]) { | 
|  | 609 | dfp = &hdr->bestfree[0]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | ASSERT(dfp2 == dfp); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 611 | dfp2 = &hdr->bestfree[1]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 613 | xfs_dir2_data_freeremove(hdr, dfp2, needlogp); | 
|  | 614 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | /* | 
|  | 616 | * Now insert the new entry. | 
|  | 617 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 618 | dfp = xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); | 
|  | 619 | ASSERT(dfp == &hdr->bestfree[0]); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 620 | ASSERT(dfp->length == prevdup->length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | ASSERT(!dfp[1].length); | 
|  | 622 | ASSERT(!dfp[2].length); | 
|  | 623 | } | 
|  | 624 | } | 
|  | 625 | /* | 
|  | 626 | * The entry before us is free, merge with it. | 
|  | 627 | */ | 
|  | 628 | else if (prevdup) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 629 | dfp = xfs_dir2_data_freefind(hdr, prevdup); | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 630 | be16_add_cpu(&prevdup->length, len); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 631 | *xfs_dir2_data_unused_tag_p(prevdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 632 | cpu_to_be16((char *)prevdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | xfs_dir2_data_log_unused(tp, bp, prevdup); | 
|  | 634 | /* | 
|  | 635 | * If the previous entry was in the table, the new entry | 
|  | 636 | * is longer, so it will be in the table too.  Remove | 
|  | 637 | * the old one and add the new one. | 
|  | 638 | */ | 
|  | 639 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 640 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
|  | 641 | xfs_dir2_data_freeinsert(hdr, prevdup, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } | 
|  | 643 | /* | 
|  | 644 | * Otherwise we need a scan if the new entry is big enough. | 
|  | 645 | */ | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 646 | else { | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 647 | needscan = be16_to_cpu(prevdup->length) > | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 648 | be16_to_cpu(hdr->bestfree[2].length); | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 649 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } | 
|  | 651 | /* | 
|  | 652 | * The following entry is free, merge with it. | 
|  | 653 | */ | 
|  | 654 | else if (postdup) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 655 | dfp = xfs_dir2_data_freefind(hdr, postdup); | 
|  | 656 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 657 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
|  | 658 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 659 | *xfs_dir2_data_unused_tag_p(newdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 660 | cpu_to_be16((char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | xfs_dir2_data_log_unused(tp, bp, newdup); | 
|  | 662 | /* | 
|  | 663 | * If the following entry was in the table, the new entry | 
|  | 664 | * is longer, so it will be in the table too.  Remove | 
|  | 665 | * the old one and add the new one. | 
|  | 666 | */ | 
|  | 667 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 668 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
|  | 669 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } | 
|  | 671 | /* | 
|  | 672 | * Otherwise we need a scan if the new entry is big enough. | 
|  | 673 | */ | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 674 | else { | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 675 | needscan = be16_to_cpu(newdup->length) > | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 676 | be16_to_cpu(hdr->bestfree[2].length); | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 677 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | } | 
|  | 679 | /* | 
|  | 680 | * Neither neighbor is free.  Make a new entry. | 
|  | 681 | */ | 
|  | 682 | else { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 683 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 684 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
|  | 685 | newdup->length = cpu_to_be16(len); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 686 | *xfs_dir2_data_unused_tag_p(newdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 687 | cpu_to_be16((char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | xfs_dir2_data_log_unused(tp, bp, newdup); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 689 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } | 
|  | 691 | *needscanp = needscan; | 
|  | 692 | } | 
|  | 693 |  | 
|  | 694 | /* | 
|  | 695 | * Take a byte range out of an existing unused space and make it un-free. | 
|  | 696 | */ | 
|  | 697 | void | 
|  | 698 | xfs_dir2_data_use_free( | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 699 | struct xfs_trans	*tp, | 
|  | 700 | struct xfs_buf		*bp, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | xfs_dir2_data_unused_t	*dup,		/* unused entry */ | 
|  | 702 | xfs_dir2_data_aoff_t	offset,		/* starting offset to use */ | 
|  | 703 | xfs_dir2_data_aoff_t	len,		/* length to use */ | 
|  | 704 | int			*needlogp,	/* out: need to log header */ | 
|  | 705 | int			*needscanp)	/* out: need regen bestfree */ | 
|  | 706 | { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 707 | xfs_dir2_data_hdr_t	*hdr;		/* data block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | xfs_dir2_data_free_t	*dfp;		/* bestfree pointer */ | 
|  | 709 | int			matchback;	/* matches end of freespace */ | 
|  | 710 | int			matchfront;	/* matches start of freespace */ | 
|  | 711 | int			needscan;	/* need to regen bestfree */ | 
|  | 712 | xfs_dir2_data_unused_t	*newdup;	/* new unused entry */ | 
|  | 713 | xfs_dir2_data_unused_t	*newdup2;	/* another new unused entry */ | 
|  | 714 | int			oldlen;		/* old unused entry's length */ | 
|  | 715 |  | 
| Dave Chinner | 1d9025e | 2012-06-22 18:50:14 +1000 | [diff] [blame] | 716 | hdr = bp->b_addr; | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 717 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || | 
|  | 718 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC)); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 719 | ASSERT(be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 720 | ASSERT(offset >= (char *)dup - (char *)hdr); | 
|  | 721 | ASSERT(offset + len <= (char *)dup + be16_to_cpu(dup->length) - (char *)hdr); | 
|  | 722 | ASSERT((char *)dup - (char *)hdr == be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | /* | 
|  | 724 | * Look up the entry in the bestfree table. | 
|  | 725 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 726 | dfp = xfs_dir2_data_freefind(hdr, dup); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 727 | oldlen = be16_to_cpu(dup->length); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 728 | ASSERT(dfp || oldlen <= be16_to_cpu(hdr->bestfree[2].length)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | /* | 
|  | 730 | * Check for alignment with front and back of the entry. | 
|  | 731 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 732 | matchfront = (char *)dup - (char *)hdr == offset; | 
|  | 733 | matchback = (char *)dup + oldlen - (char *)hdr == offset + len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | ASSERT(*needscanp == 0); | 
|  | 735 | needscan = 0; | 
|  | 736 | /* | 
|  | 737 | * If we matched it exactly we just need to get rid of it from | 
|  | 738 | * the bestfree table. | 
|  | 739 | */ | 
|  | 740 | if (matchfront && matchback) { | 
|  | 741 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 742 | needscan = (hdr->bestfree[2].offset != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | if (!needscan) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 744 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | } | 
|  | 746 | } | 
|  | 747 | /* | 
|  | 748 | * We match the first part of the entry. | 
|  | 749 | * Make a new entry with the remaining freespace. | 
|  | 750 | */ | 
|  | 751 | else if (matchfront) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 752 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 753 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
|  | 754 | newdup->length = cpu_to_be16(oldlen - len); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 755 | *xfs_dir2_data_unused_tag_p(newdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 756 | cpu_to_be16((char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | xfs_dir2_data_log_unused(tp, bp, newdup); | 
|  | 758 | /* | 
|  | 759 | * If it was in the table, remove it and add the new one. | 
|  | 760 | */ | 
|  | 761 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 762 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
|  | 763 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | ASSERT(dfp != NULL); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 765 | ASSERT(dfp->length == newdup->length); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 766 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | /* | 
|  | 768 | * If we got inserted at the last slot, | 
|  | 769 | * that means we don't know if there was a better | 
|  | 770 | * choice for the last slot, or not.  Rescan. | 
|  | 771 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 772 | needscan = dfp == &hdr->bestfree[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } | 
|  | 774 | } | 
|  | 775 | /* | 
|  | 776 | * We match the last part of the entry. | 
|  | 777 | * Trim the allocated space off the tail of the entry. | 
|  | 778 | */ | 
|  | 779 | else if (matchback) { | 
|  | 780 | newdup = dup; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 781 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 782 | *xfs_dir2_data_unused_tag_p(newdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 783 | cpu_to_be16((char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | xfs_dir2_data_log_unused(tp, bp, newdup); | 
|  | 785 | /* | 
|  | 786 | * If it was in the table, remove it and add the new one. | 
|  | 787 | */ | 
|  | 788 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 789 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
|  | 790 | dfp = xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | ASSERT(dfp != NULL); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 792 | ASSERT(dfp->length == newdup->length); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 793 | ASSERT(be16_to_cpu(dfp->offset) == (char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | /* | 
|  | 795 | * If we got inserted at the last slot, | 
|  | 796 | * that means we don't know if there was a better | 
|  | 797 | * choice for the last slot, or not.  Rescan. | 
|  | 798 | */ | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 799 | needscan = dfp == &hdr->bestfree[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } | 
|  | 801 | } | 
|  | 802 | /* | 
|  | 803 | * Poking out the middle of an entry. | 
|  | 804 | * Make two new entries. | 
|  | 805 | */ | 
|  | 806 | else { | 
|  | 807 | newdup = dup; | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 808 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 809 | *xfs_dir2_data_unused_tag_p(newdup) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 810 | cpu_to_be16((char *)newdup - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | xfs_dir2_data_log_unused(tp, bp, newdup); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 812 | newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 813 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
|  | 814 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 815 | *xfs_dir2_data_unused_tag_p(newdup2) = | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 816 | cpu_to_be16((char *)newdup2 - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | xfs_dir2_data_log_unused(tp, bp, newdup2); | 
|  | 818 | /* | 
|  | 819 | * If the old entry was in the table, we need to scan | 
|  | 820 | * if the 3rd entry was valid, since these entries | 
|  | 821 | * are smaller than the old one. | 
|  | 822 | * If we don't need to scan that means there were 1 or 2 | 
|  | 823 | * entries in the table, and removing the old and adding | 
|  | 824 | * the 2 new will work. | 
|  | 825 | */ | 
|  | 826 | if (dfp) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 827 | needscan = (hdr->bestfree[2].length != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | if (!needscan) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 829 | xfs_dir2_data_freeremove(hdr, dfp, needlogp); | 
|  | 830 | xfs_dir2_data_freeinsert(hdr, newdup, needlogp); | 
|  | 831 | xfs_dir2_data_freeinsert(hdr, newdup2, | 
|  | 832 | needlogp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } | 
|  | 834 | } | 
|  | 835 | } | 
|  | 836 | *needscanp = needscan; | 
|  | 837 | } |