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