| 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-2003,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" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "xfs_inode.h" | 
| Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_inode_item.h" | 
| Dave Chinner | 8d2a5e6 | 2012-03-07 04:50:19 +0000 | [diff] [blame] | 31 | #include "xfs_dir2.h" | 
| Christoph Hellwig | 5792664 | 2011-07-13 13:43:48 +0200 | [diff] [blame] | 32 | #include "xfs_dir2_format.h" | 
|  | 33 | #include "xfs_dir2_priv.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include "xfs_error.h" | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 35 | #include "xfs_trace.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | /* | 
|  | 38 | * Local function prototypes. | 
|  | 39 | */ | 
|  | 40 | static void xfs_dir2_block_log_leaf(xfs_trans_t *tp, xfs_dabuf_t *bp, int first, | 
|  | 41 | int last); | 
|  | 42 | static void xfs_dir2_block_log_tail(xfs_trans_t *tp, xfs_dabuf_t *bp); | 
|  | 43 | static int xfs_dir2_block_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **bpp, | 
|  | 44 | int *entno); | 
|  | 45 | static int xfs_dir2_block_sort(const void *a, const void *b); | 
|  | 46 |  | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 47 | static xfs_dahash_t xfs_dir_hash_dot, xfs_dir_hash_dotdot; | 
|  | 48 |  | 
|  | 49 | /* | 
|  | 50 | * One-time startup routine called from xfs_init(). | 
|  | 51 | */ | 
|  | 52 | void | 
|  | 53 | xfs_dir_startup(void) | 
|  | 54 | { | 
| Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 55 | xfs_dir_hash_dot = xfs_da_hashname((unsigned char *)".", 1); | 
|  | 56 | xfs_dir_hash_dotdot = xfs_da_hashname((unsigned char *)"..", 2); | 
| Nathan Scott | f6c2d1f | 2006-06-20 13:04:51 +1000 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /* | 
|  | 60 | * Add an entry to a block directory. | 
|  | 61 | */ | 
|  | 62 | int						/* error */ | 
|  | 63 | xfs_dir2_block_addname( | 
|  | 64 | xfs_da_args_t		*args)		/* directory op arguments */ | 
|  | 65 | { | 
|  | 66 | xfs_dir2_data_free_t	*bf;		/* bestfree table in block */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 67 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */ | 
|  | 69 | xfs_dabuf_t		*bp;		/* buffer for block */ | 
|  | 70 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 71 | int			compact;	/* need to compact leaf ents */ | 
|  | 72 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 73 | xfs_inode_t		*dp;		/* directory inode */ | 
|  | 74 | xfs_dir2_data_unused_t	*dup;		/* block unused entry */ | 
|  | 75 | int			error;		/* error return value */ | 
|  | 76 | xfs_dir2_data_unused_t	*enddup=NULL;	/* unused at end of data */ | 
|  | 77 | xfs_dahash_t		hash;		/* hash value of found entry */ | 
|  | 78 | int			high;		/* high index for binary srch */ | 
|  | 79 | int			highstale;	/* high stale index */ | 
|  | 80 | int			lfloghigh=0;	/* last final leaf to log */ | 
|  | 81 | int			lfloglow=0;	/* first final leaf to log */ | 
|  | 82 | int			len;		/* length of the new entry */ | 
|  | 83 | int			low;		/* low index for binary srch */ | 
|  | 84 | int			lowstale;	/* low stale index */ | 
|  | 85 | int			mid=0;		/* midpoint for binary srch */ | 
|  | 86 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 87 | int			needlog;	/* need to log header */ | 
|  | 88 | int			needscan;	/* need to rescan freespace */ | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 89 | __be16			*tagp;		/* pointer to tag value */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | xfs_trans_t		*tp;		/* transaction structure */ | 
|  | 91 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 92 | trace_xfs_dir2_block_addname(args); | 
|  | 93 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | dp = args->dp; | 
|  | 95 | tp = args->trans; | 
|  | 96 | mp = dp->i_mount; | 
|  | 97 | /* | 
|  | 98 | * Read the (one and only) directory block into dabuf bp. | 
|  | 99 | */ | 
|  | 100 | if ((error = | 
|  | 101 | xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) { | 
|  | 102 | return error; | 
|  | 103 | } | 
|  | 104 | ASSERT(bp != NULL); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 105 | hdr = bp->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | /* | 
|  | 107 | * Check the magic number, corrupted if wrong. | 
|  | 108 | */ | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 109 | if (unlikely(hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | XFS_CORRUPTION_ERROR("xfs_dir2_block_addname", | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 111 | XFS_ERRLEVEL_LOW, mp, hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | xfs_da_brelse(tp, bp); | 
|  | 113 | return XFS_ERROR(EFSCORRUPTED); | 
|  | 114 | } | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 115 | len = xfs_dir2_data_entsize(args->namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* | 
|  | 117 | * Set up pointers to parts of the block. | 
|  | 118 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 119 | bf = hdr->bestfree; | 
|  | 120 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 121 | blp = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* | 
|  | 123 | * No stale entries?  Need space for entry and new leaf. | 
|  | 124 | */ | 
|  | 125 | if (!btp->stale) { | 
|  | 126 | /* | 
|  | 127 | * Tag just before the first leaf entry. | 
|  | 128 | */ | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 129 | tagp = (__be16 *)blp - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* | 
|  | 131 | * Data object just before the first leaf entry. | 
|  | 132 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 133 | enddup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* | 
|  | 135 | * If it's not free then can't do this add without cleaning up: | 
|  | 136 | * the space before the first leaf entry needs to be free so it | 
|  | 137 | * can be expanded to hold the pointer to the new entry. | 
|  | 138 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 139 | if (be16_to_cpu(enddup->freetag) != XFS_DIR2_DATA_FREE_TAG) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | dup = enddup = NULL; | 
|  | 141 | /* | 
|  | 142 | * Check out the biggest freespace and see if it's the same one. | 
|  | 143 | */ | 
|  | 144 | else { | 
|  | 145 | dup = (xfs_dir2_data_unused_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 146 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (dup == enddup) { | 
|  | 148 | /* | 
|  | 149 | * It is the biggest freespace, is it too small | 
|  | 150 | * to hold the new leaf too? | 
|  | 151 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 152 | if (be16_to_cpu(dup->length) < len + (uint)sizeof(*blp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* | 
|  | 154 | * Yes, we use the second-largest | 
|  | 155 | * entry instead if it works. | 
|  | 156 | */ | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 157 | if (be16_to_cpu(bf[1].length) >= len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | dup = (xfs_dir2_data_unused_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 159 | ((char *)hdr + | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 160 | be16_to_cpu(bf[1].offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | else | 
|  | 162 | dup = NULL; | 
|  | 163 | } | 
|  | 164 | } else { | 
|  | 165 | /* | 
|  | 166 | * Not the same free entry, | 
|  | 167 | * just check its length. | 
|  | 168 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 169 | if (be16_to_cpu(dup->length) < len) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | dup = NULL; | 
|  | 171 | } | 
|  | 172 | } | 
|  | 173 | } | 
|  | 174 | compact = 0; | 
|  | 175 | } | 
|  | 176 | /* | 
|  | 177 | * If there are stale entries we'll use one for the leaf. | 
|  | 178 | * Is the biggest entry enough to avoid compaction? | 
|  | 179 | */ | 
| Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 180 | else if (be16_to_cpu(bf[0].length) >= len) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | dup = (xfs_dir2_data_unused_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 182 | ((char *)hdr + be16_to_cpu(bf[0].offset)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | compact = 0; | 
|  | 184 | } | 
|  | 185 | /* | 
|  | 186 | * Will need to compact to make this work. | 
|  | 187 | */ | 
|  | 188 | else { | 
|  | 189 | /* | 
|  | 190 | * Tag just before the first leaf entry. | 
|  | 191 | */ | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 192 | tagp = (__be16 *)blp - 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* | 
|  | 194 | * Data object just before the first leaf entry. | 
|  | 195 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 196 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /* | 
|  | 198 | * If it's not free then the data will go where the | 
|  | 199 | * leaf data starts now, if it works at all. | 
|  | 200 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 201 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 202 | if (be16_to_cpu(dup->length) + (be32_to_cpu(btp->stale) - 1) * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | (uint)sizeof(*blp) < len) | 
|  | 204 | dup = NULL; | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 205 | } else if ((be32_to_cpu(btp->stale) - 1) * (uint)sizeof(*blp) < len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | dup = NULL; | 
|  | 207 | else | 
|  | 208 | dup = (xfs_dir2_data_unused_t *)blp; | 
|  | 209 | compact = 1; | 
|  | 210 | } | 
|  | 211 | /* | 
|  | 212 | * If this isn't a real add, we're done with the buffer. | 
|  | 213 | */ | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 214 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | xfs_da_brelse(tp, bp); | 
|  | 216 | /* | 
|  | 217 | * If we don't have space for the new entry & leaf ... | 
|  | 218 | */ | 
|  | 219 | if (!dup) { | 
|  | 220 | /* | 
|  | 221 | * Not trying to actually do anything, or don't have | 
|  | 222 | * a space reservation: return no-space. | 
|  | 223 | */ | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 224 | if ((args->op_flags & XFS_DA_OP_JUSTCHECK) || args->total == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return XFS_ERROR(ENOSPC); | 
|  | 226 | /* | 
|  | 227 | * Convert to the next larger format. | 
|  | 228 | * Then add the new entry in that format. | 
|  | 229 | */ | 
|  | 230 | error = xfs_dir2_block_to_leaf(args, bp); | 
|  | 231 | xfs_da_buf_done(bp); | 
|  | 232 | if (error) | 
|  | 233 | return error; | 
|  | 234 | return xfs_dir2_leaf_addname(args); | 
|  | 235 | } | 
|  | 236 | /* | 
|  | 237 | * Just checking, and it would work, so say so. | 
|  | 238 | */ | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 239 | if (args->op_flags & XFS_DA_OP_JUSTCHECK) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return 0; | 
|  | 241 | needlog = needscan = 0; | 
|  | 242 | /* | 
|  | 243 | * If need to compact the leaf entries, do it now. | 
|  | 244 | * Leave the highest-numbered stale entry stale. | 
|  | 245 | * XXX should be the one closest to mid but mid is not yet computed. | 
|  | 246 | */ | 
|  | 247 | if (compact) { | 
|  | 248 | int	fromidx;		/* source leaf index */ | 
|  | 249 | int	toidx;			/* target leaf index */ | 
|  | 250 |  | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 251 | for (fromidx = toidx = be32_to_cpu(btp->count) - 1, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | highstale = lfloghigh = -1; | 
|  | 253 | fromidx >= 0; | 
|  | 254 | fromidx--) { | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 255 | if (blp[fromidx].address == | 
|  | 256 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (highstale == -1) | 
|  | 258 | highstale = toidx; | 
|  | 259 | else { | 
|  | 260 | if (lfloghigh == -1) | 
|  | 261 | lfloghigh = toidx; | 
|  | 262 | continue; | 
|  | 263 | } | 
|  | 264 | } | 
|  | 265 | if (fromidx < toidx) | 
|  | 266 | blp[toidx] = blp[fromidx]; | 
|  | 267 | toidx--; | 
|  | 268 | } | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 269 | lfloglow = toidx + 1 - (be32_to_cpu(btp->stale) - 1); | 
|  | 270 | lfloghigh -= be32_to_cpu(btp->stale) - 1; | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 271 | be32_add_cpu(&btp->count, -(be32_to_cpu(btp->stale) - 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | xfs_dir2_data_make_free(tp, bp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 273 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)hdr), | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 274 | (xfs_dir2_data_aoff_t)((be32_to_cpu(btp->stale) - 1) * sizeof(*blp)), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | &needlog, &needscan); | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 276 | blp += be32_to_cpu(btp->stale) - 1; | 
|  | 277 | btp->stale = cpu_to_be32(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* | 
|  | 279 | * If we now need to rebuild the bestfree map, do so. | 
|  | 280 | * This needs to happen before the next call to use_free. | 
|  | 281 | */ | 
|  | 282 | if (needscan) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 283 | xfs_dir2_data_freescan(mp, hdr, &needlog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | needscan = 0; | 
|  | 285 | } | 
|  | 286 | } | 
|  | 287 | /* | 
|  | 288 | * Set leaf logging boundaries to impossible state. | 
|  | 289 | * For the no-stale case they're set explicitly. | 
|  | 290 | */ | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 291 | else if (btp->stale) { | 
|  | 292 | lfloglow = be32_to_cpu(btp->count); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | lfloghigh = -1; | 
|  | 294 | } | 
|  | 295 | /* | 
|  | 296 | * Find the slot that's first lower than our hash value, -1 if none. | 
|  | 297 | */ | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 298 | for (low = 0, high = be32_to_cpu(btp->count) - 1; low <= high; ) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | mid = (low + high) >> 1; | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 300 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | break; | 
|  | 302 | if (hash < args->hashval) | 
|  | 303 | low = mid + 1; | 
|  | 304 | else | 
|  | 305 | high = mid - 1; | 
|  | 306 | } | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 307 | while (mid >= 0 && be32_to_cpu(blp[mid].hashval) >= args->hashval) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | mid--; | 
|  | 309 | } | 
|  | 310 | /* | 
|  | 311 | * No stale entries, will use enddup space to hold new leaf. | 
|  | 312 | */ | 
|  | 313 | if (!btp->stale) { | 
|  | 314 | /* | 
|  | 315 | * Mark the space needed for the new leaf entry, now in use. | 
|  | 316 | */ | 
|  | 317 | xfs_dir2_data_use_free(tp, bp, enddup, | 
|  | 318 | (xfs_dir2_data_aoff_t) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 319 | ((char *)enddup - (char *)hdr + be16_to_cpu(enddup->length) - | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | sizeof(*blp)), | 
|  | 321 | (xfs_dir2_data_aoff_t)sizeof(*blp), | 
|  | 322 | &needlog, &needscan); | 
|  | 323 | /* | 
|  | 324 | * Update the tail (entry count). | 
|  | 325 | */ | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 326 | be32_add_cpu(&btp->count, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | /* | 
|  | 328 | * If we now need to rebuild the bestfree map, do so. | 
|  | 329 | * This needs to happen before the next call to use_free. | 
|  | 330 | */ | 
|  | 331 | if (needscan) { | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 332 | xfs_dir2_data_freescan(mp, hdr, &needlog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | needscan = 0; | 
|  | 334 | } | 
|  | 335 | /* | 
|  | 336 | * Adjust pointer to the first leaf entry, we're about to move | 
|  | 337 | * the table up one to open up space for the new leaf entry. | 
|  | 338 | * Then adjust our index to match. | 
|  | 339 | */ | 
|  | 340 | blp--; | 
|  | 341 | mid++; | 
|  | 342 | if (mid) | 
|  | 343 | memmove(blp, &blp[1], mid * sizeof(*blp)); | 
|  | 344 | lfloglow = 0; | 
|  | 345 | lfloghigh = mid; | 
|  | 346 | } | 
|  | 347 | /* | 
|  | 348 | * Use a stale leaf for our new entry. | 
|  | 349 | */ | 
|  | 350 | else { | 
|  | 351 | for (lowstale = mid; | 
|  | 352 | lowstale >= 0 && | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 353 | blp[lowstale].address != | 
|  | 354 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | lowstale--) | 
|  | 356 | continue; | 
|  | 357 | for (highstale = mid + 1; | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 358 | highstale < be32_to_cpu(btp->count) && | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 359 | blp[highstale].address != | 
|  | 360 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | (lowstale < 0 || mid - lowstale > highstale - mid); | 
|  | 362 | highstale++) | 
|  | 363 | continue; | 
|  | 364 | /* | 
|  | 365 | * Move entries toward the low-numbered stale entry. | 
|  | 366 | */ | 
|  | 367 | if (lowstale >= 0 && | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 368 | (highstale == be32_to_cpu(btp->count) || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | mid - lowstale <= highstale - mid)) { | 
|  | 370 | if (mid - lowstale) | 
|  | 371 | memmove(&blp[lowstale], &blp[lowstale + 1], | 
|  | 372 | (mid - lowstale) * sizeof(*blp)); | 
|  | 373 | lfloglow = MIN(lowstale, lfloglow); | 
|  | 374 | lfloghigh = MAX(mid, lfloghigh); | 
|  | 375 | } | 
|  | 376 | /* | 
|  | 377 | * Move entries toward the high-numbered stale entry. | 
|  | 378 | */ | 
|  | 379 | else { | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 380 | ASSERT(highstale < be32_to_cpu(btp->count)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | mid++; | 
|  | 382 | if (highstale - mid) | 
|  | 383 | memmove(&blp[mid + 1], &blp[mid], | 
|  | 384 | (highstale - mid) * sizeof(*blp)); | 
|  | 385 | lfloglow = MIN(mid, lfloglow); | 
|  | 386 | lfloghigh = MAX(highstale, lfloghigh); | 
|  | 387 | } | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 388 | be32_add_cpu(&btp->stale, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } | 
|  | 390 | /* | 
|  | 391 | * Point to the new data entry. | 
|  | 392 | */ | 
|  | 393 | dep = (xfs_dir2_data_entry_t *)dup; | 
|  | 394 | /* | 
|  | 395 | * Fill in the leaf entry. | 
|  | 396 | */ | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 397 | blp[mid].hashval = cpu_to_be32(args->hashval); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 398 | blp[mid].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 399 | (char *)dep - (char *)hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | xfs_dir2_block_log_leaf(tp, bp, lfloglow, lfloghigh); | 
|  | 401 | /* | 
|  | 402 | * Mark space for the data entry used. | 
|  | 403 | */ | 
|  | 404 | xfs_dir2_data_use_free(tp, bp, dup, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 405 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | (xfs_dir2_data_aoff_t)len, &needlog, &needscan); | 
|  | 407 | /* | 
|  | 408 | * Create the new data entry. | 
|  | 409 | */ | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 410 | dep->inumber = cpu_to_be64(args->inumber); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | dep->namelen = args->namelen; | 
|  | 412 | memcpy(dep->name, args->name, args->namelen); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 413 | tagp = xfs_dir2_data_entry_tag_p(dep); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 414 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* | 
|  | 416 | * Clean up the bestfree array and log the header, tail, and entry. | 
|  | 417 | */ | 
|  | 418 | if (needscan) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 419 | xfs_dir2_data_freescan(mp, hdr, &needlog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | if (needlog) | 
|  | 421 | xfs_dir2_data_log_header(tp, bp); | 
|  | 422 | xfs_dir2_block_log_tail(tp, bp); | 
|  | 423 | xfs_dir2_data_log_entry(tp, bp, dep); | 
|  | 424 | xfs_dir2_data_check(dp, bp); | 
|  | 425 | xfs_da_buf_done(bp); | 
|  | 426 | return 0; | 
|  | 427 | } | 
|  | 428 |  | 
|  | 429 | /* | 
|  | 430 | * Readdir for block directories. | 
|  | 431 | */ | 
|  | 432 | int						/* error */ | 
|  | 433 | xfs_dir2_block_getdents( | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | xfs_inode_t		*dp,		/* incore inode */ | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 435 | void			*dirent, | 
|  | 436 | xfs_off_t		*offset, | 
|  | 437 | filldir_t		filldir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 439 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | xfs_dabuf_t		*bp;		/* buffer for block */ | 
|  | 441 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 442 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 443 | xfs_dir2_data_unused_t	*dup;		/* block unused entry */ | 
|  | 444 | char			*endptr;	/* end of the data entries */ | 
|  | 445 | int			error;		/* error return value */ | 
|  | 446 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | char			*ptr;		/* current data entry */ | 
|  | 448 | int			wantoff;	/* starting block offset */ | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 449 | xfs_off_t		cook; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 |  | 
|  | 451 | mp = dp->i_mount; | 
|  | 452 | /* | 
|  | 453 | * If the block number in the offset is out of range, we're done. | 
|  | 454 | */ | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 455 | if (xfs_dir2_dataptr_to_db(mp, *offset) > mp->m_dirdatablk) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return 0; | 
|  | 457 | } | 
|  | 458 | /* | 
|  | 459 | * Can't read the block, give up, else get dabuf in bp. | 
|  | 460 | */ | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 461 | error = xfs_da_read_buf(NULL, dp, mp->m_dirdatablk, -1, | 
|  | 462 | &bp, XFS_DATA_FORK); | 
|  | 463 | if (error) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | return error; | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 465 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | ASSERT(bp != NULL); | 
|  | 467 | /* | 
|  | 468 | * Extract the byte offset we start at from the seek pointer. | 
|  | 469 | * We'll skip entries before this. | 
|  | 470 | */ | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 471 | wantoff = xfs_dir2_dataptr_to_off(mp, *offset); | 
| Christoph Hellwig | a64b041 | 2011-07-08 14:35:32 +0200 | [diff] [blame] | 472 | hdr = bp->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | xfs_dir2_data_check(dp, bp); | 
|  | 474 | /* | 
|  | 475 | * Set up values for the loop. | 
|  | 476 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 477 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | a64b041 | 2011-07-08 14:35:32 +0200 | [diff] [blame] | 478 | ptr = (char *)(hdr + 1); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 479 | endptr = (char *)xfs_dir2_block_leaf_p(btp); | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 480 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | /* | 
|  | 482 | * Loop over the data portion of the block. | 
|  | 483 | * Each object is a real entry (dep) or an unused one (dup). | 
|  | 484 | */ | 
|  | 485 | while (ptr < endptr) { | 
|  | 486 | dup = (xfs_dir2_data_unused_t *)ptr; | 
|  | 487 | /* | 
|  | 488 | * Unused, skip it. | 
|  | 489 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 490 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { | 
|  | 491 | ptr += be16_to_cpu(dup->length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | continue; | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | dep = (xfs_dir2_data_entry_t *)ptr; | 
|  | 496 |  | 
|  | 497 | /* | 
|  | 498 | * Bump pointer for the next iteration. | 
|  | 499 | */ | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 500 | ptr += xfs_dir2_data_entsize(dep->namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | /* | 
|  | 502 | * The entry is before the desired starting point, skip it. | 
|  | 503 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 504 | if ((char *)dep - (char *)hdr < wantoff) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |  | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 507 | cook = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 508 | (char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 |  | 
|  | 510 | /* | 
|  | 511 | * If it didn't fit, set the final offset to here & return. | 
|  | 512 | */ | 
| Dave Chinner | 4a24cb7 | 2010-01-20 10:48:05 +1100 | [diff] [blame] | 513 | if (filldir(dirent, (char *)dep->name, dep->namelen, | 
|  | 514 | cook & 0x7fffffff, be64_to_cpu(dep->inumber), | 
|  | 515 | DT_UNKNOWN)) { | 
| Christoph Hellwig | 1544031 | 2009-01-08 14:00:00 -0500 | [diff] [blame] | 516 | *offset = cook & 0x7fffffff; | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 517 | xfs_da_brelse(NULL, bp); | 
|  | 518 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | /* | 
|  | 523 | * Reached the end of the block. | 
| Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 524 | * Set the offset to a non-existent block 1 and return. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | */ | 
| Christoph Hellwig | 1544031 | 2009-01-08 14:00:00 -0500 | [diff] [blame] | 526 | *offset = xfs_dir2_db_off_to_dataptr(mp, mp->m_dirdatablk + 1, 0) & | 
|  | 527 | 0x7fffffff; | 
| Christoph Hellwig | 051e7cd | 2007-08-28 13:58:24 +1000 | [diff] [blame] | 528 | xfs_da_brelse(NULL, bp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return 0; | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | /* | 
|  | 533 | * Log leaf entries from the block. | 
|  | 534 | */ | 
|  | 535 | static void | 
|  | 536 | xfs_dir2_block_log_leaf( | 
|  | 537 | xfs_trans_t		*tp,		/* transaction structure */ | 
|  | 538 | xfs_dabuf_t		*bp,		/* block buffer */ | 
|  | 539 | int			first,		/* index of first logged leaf */ | 
|  | 540 | int			last)		/* index of last logged leaf */ | 
|  | 541 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 542 | xfs_dir2_data_hdr_t	*hdr = bp->data; | 
|  | 543 | xfs_dir2_leaf_entry_t	*blp; | 
|  | 544 | xfs_dir2_block_tail_t	*btp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 |  | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 546 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 547 | blp = xfs_dir2_block_leaf_p(btp); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 548 | xfs_da_log_buf(tp, bp, (uint)((char *)&blp[first] - (char *)hdr), | 
|  | 549 | (uint)((char *)&blp[last + 1] - (char *)hdr - 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | } | 
|  | 551 |  | 
|  | 552 | /* | 
|  | 553 | * Log the block tail. | 
|  | 554 | */ | 
|  | 555 | static void | 
|  | 556 | xfs_dir2_block_log_tail( | 
|  | 557 | xfs_trans_t		*tp,		/* transaction structure */ | 
|  | 558 | xfs_dabuf_t		*bp)		/* block buffer */ | 
|  | 559 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 560 | xfs_dir2_data_hdr_t	*hdr = bp->data; | 
|  | 561 | xfs_dir2_block_tail_t	*btp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 |  | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 563 | btp = xfs_dir2_block_tail_p(tp->t_mountp, hdr); | 
|  | 564 | xfs_da_log_buf(tp, bp, (uint)((char *)btp - (char *)hdr), | 
|  | 565 | (uint)((char *)(btp + 1) - (char *)hdr - 1)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } | 
|  | 567 |  | 
|  | 568 | /* | 
|  | 569 | * Look up an entry in the block.  This is the external routine, | 
|  | 570 | * xfs_dir2_block_lookup_int does the real work. | 
|  | 571 | */ | 
|  | 572 | int						/* error */ | 
|  | 573 | xfs_dir2_block_lookup( | 
|  | 574 | xfs_da_args_t		*args)		/* dir lookup arguments */ | 
|  | 575 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 576 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */ | 
|  | 578 | xfs_dabuf_t		*bp;		/* block buffer */ | 
|  | 579 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 580 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 581 | xfs_inode_t		*dp;		/* incore inode */ | 
|  | 582 | int			ent;		/* entry index */ | 
|  | 583 | int			error;		/* error return value */ | 
|  | 584 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 585 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 586 | trace_xfs_dir2_block_lookup(args); | 
|  | 587 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /* | 
|  | 589 | * Get the buffer, look up the entry. | 
|  | 590 | * If not found (ENOENT) then return, have no buffer. | 
|  | 591 | */ | 
|  | 592 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) | 
|  | 593 | return error; | 
|  | 594 | dp = args->dp; | 
|  | 595 | mp = dp->i_mount; | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 596 | hdr = bp->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | xfs_dir2_data_check(dp, bp); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 598 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 599 | blp = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | /* | 
|  | 601 | * Get the offset from the leaf entry, to point to the data. | 
|  | 602 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 603 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 604 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | /* | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 606 | * Fill in inode number, CI name if appropriate, release the block. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | */ | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 608 | args->inumber = be64_to_cpu(dep->inumber); | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 609 | error = xfs_dir_cilookup_result(args, dep->name, dep->namelen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | xfs_da_brelse(args->trans, bp); | 
| Barry Naujok | 384f3ce | 2008-05-21 16:58:22 +1000 | [diff] [blame] | 611 | return XFS_ERROR(error); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | } | 
|  | 613 |  | 
|  | 614 | /* | 
|  | 615 | * Internal block lookup routine. | 
|  | 616 | */ | 
|  | 617 | static int					/* error */ | 
|  | 618 | xfs_dir2_block_lookup_int( | 
|  | 619 | xfs_da_args_t		*args,		/* dir lookup arguments */ | 
|  | 620 | xfs_dabuf_t		**bpp,		/* returned block buffer */ | 
|  | 621 | int			*entno)		/* returned entry number */ | 
|  | 622 | { | 
|  | 623 | xfs_dir2_dataptr_t	addr;		/* data entry address */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 624 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */ | 
|  | 626 | xfs_dabuf_t		*bp;		/* block buffer */ | 
|  | 627 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 628 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 629 | xfs_inode_t		*dp;		/* incore inode */ | 
|  | 630 | int			error;		/* error return value */ | 
|  | 631 | xfs_dahash_t		hash;		/* found hash value */ | 
|  | 632 | int			high;		/* binary search high index */ | 
|  | 633 | int			low;		/* binary search low index */ | 
|  | 634 | int			mid;		/* binary search current idx */ | 
|  | 635 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 636 | xfs_trans_t		*tp;		/* transaction pointer */ | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 637 | enum xfs_dacmp		cmp;		/* comparison result */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 |  | 
|  | 639 | dp = args->dp; | 
|  | 640 | tp = args->trans; | 
|  | 641 | mp = dp->i_mount; | 
|  | 642 | /* | 
|  | 643 | * Read the buffer, return error if we can't get it. | 
|  | 644 | */ | 
|  | 645 | if ((error = | 
|  | 646 | xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &bp, XFS_DATA_FORK))) { | 
|  | 647 | return error; | 
|  | 648 | } | 
|  | 649 | ASSERT(bp != NULL); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 650 | hdr = bp->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | xfs_dir2_data_check(dp, bp); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 652 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 653 | blp = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* | 
|  | 655 | * Loop doing a binary search for our hash value. | 
|  | 656 | * Find our entry, ENOENT if it's not there. | 
|  | 657 | */ | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 658 | for (low = 0, high = be32_to_cpu(btp->count) - 1; ; ) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | ASSERT(low <= high); | 
|  | 660 | mid = (low + high) >> 1; | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 661 | if ((hash = be32_to_cpu(blp[mid].hashval)) == args->hashval) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | break; | 
|  | 663 | if (hash < args->hashval) | 
|  | 664 | low = mid + 1; | 
|  | 665 | else | 
|  | 666 | high = mid - 1; | 
|  | 667 | if (low > high) { | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 668 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | xfs_da_brelse(tp, bp); | 
|  | 670 | return XFS_ERROR(ENOENT); | 
|  | 671 | } | 
|  | 672 | } | 
|  | 673 | /* | 
|  | 674 | * Back up to the first one with the right hash value. | 
|  | 675 | */ | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 676 | while (mid > 0 && be32_to_cpu(blp[mid - 1].hashval) == args->hashval) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | mid--; | 
|  | 678 | } | 
|  | 679 | /* | 
|  | 680 | * Now loop forward through all the entries with the | 
|  | 681 | * right hash value looking for our name. | 
|  | 682 | */ | 
|  | 683 | do { | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 684 | if ((addr = be32_to_cpu(blp[mid].address)) == XFS_DIR2_NULL_DATAPTR) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | continue; | 
|  | 686 | /* | 
|  | 687 | * Get pointer to the entry from the leaf. | 
|  | 688 | */ | 
|  | 689 | dep = (xfs_dir2_data_entry_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 690 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, addr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | /* | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 692 | * Compare name and if it's an exact match, return the index | 
|  | 693 | * and buffer. If it's the first case-insensitive match, store | 
|  | 694 | * the index and buffer and continue looking for an exact match. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | */ | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 696 | cmp = mp->m_dirnameops->compname(args, dep->name, dep->namelen); | 
|  | 697 | if (cmp != XFS_CMP_DIFFERENT && cmp != args->cmpresult) { | 
|  | 698 | args->cmpresult = cmp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | *bpp = bp; | 
|  | 700 | *entno = mid; | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 701 | if (cmp == XFS_CMP_EXACT) | 
|  | 702 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 704 | } while (++mid < be32_to_cpu(btp->count) && | 
|  | 705 | be32_to_cpu(blp[mid].hashval) == hash); | 
|  | 706 |  | 
| Barry Naujok | 6a17810 | 2008-05-21 16:42:05 +1000 | [diff] [blame] | 707 | ASSERT(args->op_flags & XFS_DA_OP_OKNOENT); | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 708 | /* | 
|  | 709 | * Here, we can only be doing a lookup (not a rename or replace). | 
|  | 710 | * If a case-insensitive match was found earlier, return success. | 
|  | 711 | */ | 
|  | 712 | if (args->cmpresult == XFS_CMP_CASE) | 
|  | 713 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | /* | 
|  | 715 | * No match, release the buffer and return ENOENT. | 
|  | 716 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | xfs_da_brelse(tp, bp); | 
|  | 718 | return XFS_ERROR(ENOENT); | 
|  | 719 | } | 
|  | 720 |  | 
|  | 721 | /* | 
|  | 722 | * Remove an entry from a block format directory. | 
|  | 723 | * If that makes the block small enough to fit in shortform, transform it. | 
|  | 724 | */ | 
|  | 725 | int						/* error */ | 
|  | 726 | xfs_dir2_block_removename( | 
|  | 727 | xfs_da_args_t		*args)		/* directory operation args */ | 
|  | 728 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 729 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf pointer */ | 
|  | 731 | xfs_dabuf_t		*bp;		/* block buffer */ | 
|  | 732 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 733 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 734 | xfs_inode_t		*dp;		/* incore inode */ | 
|  | 735 | int			ent;		/* block leaf entry index */ | 
|  | 736 | int			error;		/* error return value */ | 
|  | 737 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 738 | int			needlog;	/* need to log block header */ | 
|  | 739 | int			needscan;	/* need to fixup bestfree */ | 
|  | 740 | xfs_dir2_sf_hdr_t	sfh;		/* shortform header */ | 
|  | 741 | int			size;		/* shortform size */ | 
|  | 742 | xfs_trans_t		*tp;		/* transaction pointer */ | 
|  | 743 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 744 | trace_xfs_dir2_block_removename(args); | 
|  | 745 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /* | 
|  | 747 | * Look up the entry in the block.  Gets the buffer and entry index. | 
|  | 748 | * It will always be there, the vnodeops level does a lookup first. | 
|  | 749 | */ | 
|  | 750 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { | 
|  | 751 | return error; | 
|  | 752 | } | 
|  | 753 | dp = args->dp; | 
|  | 754 | tp = args->trans; | 
|  | 755 | mp = dp->i_mount; | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 756 | hdr = bp->data; | 
|  | 757 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 758 | blp = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | /* | 
|  | 760 | * Point to the data entry using the leaf entry. | 
|  | 761 | */ | 
|  | 762 | dep = (xfs_dir2_data_entry_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 763 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | /* | 
|  | 765 | * Mark the data entry's space free. | 
|  | 766 | */ | 
|  | 767 | needlog = needscan = 0; | 
|  | 768 | xfs_dir2_data_make_free(tp, bp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 769 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)hdr), | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 770 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | /* | 
|  | 772 | * Fix up the block tail. | 
|  | 773 | */ | 
| Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 774 | be32_add_cpu(&btp->stale, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | xfs_dir2_block_log_tail(tp, bp); | 
|  | 776 | /* | 
|  | 777 | * Remove the leaf entry by marking it stale. | 
|  | 778 | */ | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 779 | blp[ent].address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | xfs_dir2_block_log_leaf(tp, bp, ent, ent); | 
|  | 781 | /* | 
|  | 782 | * Fix up bestfree, log the header if necessary. | 
|  | 783 | */ | 
|  | 784 | if (needscan) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 785 | xfs_dir2_data_freescan(mp, hdr, &needlog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (needlog) | 
|  | 787 | xfs_dir2_data_log_header(tp, bp); | 
|  | 788 | xfs_dir2_data_check(dp, bp); | 
|  | 789 | /* | 
|  | 790 | * See if the size as a shortform is good enough. | 
|  | 791 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 792 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); | 
|  | 793 | if (size > XFS_IFORK_DSIZE(dp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | xfs_da_buf_done(bp); | 
|  | 795 | return 0; | 
|  | 796 | } | 
|  | 797 | /* | 
|  | 798 | * If it works, do the conversion. | 
|  | 799 | */ | 
|  | 800 | return xfs_dir2_block_to_sf(args, bp, size, &sfh); | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | /* | 
|  | 804 | * Replace an entry in a V2 block directory. | 
|  | 805 | * Change the inode number to the new value. | 
|  | 806 | */ | 
|  | 807 | int						/* error */ | 
|  | 808 | xfs_dir2_block_replace( | 
|  | 809 | xfs_da_args_t		*args)		/* directory operation args */ | 
|  | 810 | { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 811 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */ | 
|  | 813 | xfs_dabuf_t		*bp;		/* block buffer */ | 
|  | 814 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 815 | xfs_dir2_data_entry_t	*dep;		/* block data entry */ | 
|  | 816 | xfs_inode_t		*dp;		/* incore inode */ | 
|  | 817 | int			ent;		/* leaf entry index */ | 
|  | 818 | int			error;		/* error return value */ | 
|  | 819 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 820 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 821 | trace_xfs_dir2_block_replace(args); | 
|  | 822 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | /* | 
|  | 824 | * Lookup the entry in the directory.  Get buffer and entry index. | 
|  | 825 | * This will always succeed since the caller has already done a lookup. | 
|  | 826 | */ | 
|  | 827 | if ((error = xfs_dir2_block_lookup_int(args, &bp, &ent))) { | 
|  | 828 | return error; | 
|  | 829 | } | 
|  | 830 | dp = args->dp; | 
|  | 831 | mp = dp->i_mount; | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 832 | hdr = bp->data; | 
|  | 833 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 834 | blp = xfs_dir2_block_leaf_p(btp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* | 
|  | 836 | * Point to the data entry we need to change. | 
|  | 837 | */ | 
|  | 838 | dep = (xfs_dir2_data_entry_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 839 | ((char *)hdr + xfs_dir2_dataptr_to_off(mp, be32_to_cpu(blp[ent].address))); | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 840 | ASSERT(be64_to_cpu(dep->inumber) != args->inumber); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | /* | 
|  | 842 | * Change the inode number to the new value. | 
|  | 843 | */ | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 844 | dep->inumber = cpu_to_be64(args->inumber); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | xfs_dir2_data_log_entry(args->trans, bp, dep); | 
|  | 846 | xfs_dir2_data_check(dp, bp); | 
|  | 847 | xfs_da_buf_done(bp); | 
|  | 848 | return 0; | 
|  | 849 | } | 
|  | 850 |  | 
|  | 851 | /* | 
|  | 852 | * Qsort comparison routine for the block leaf entries. | 
|  | 853 | */ | 
|  | 854 | static int					/* sort order */ | 
|  | 855 | xfs_dir2_block_sort( | 
|  | 856 | const void			*a,	/* first leaf entry */ | 
|  | 857 | const void			*b)	/* second leaf entry */ | 
|  | 858 | { | 
|  | 859 | const xfs_dir2_leaf_entry_t	*la;	/* first leaf entry */ | 
|  | 860 | const xfs_dir2_leaf_entry_t	*lb;	/* second leaf entry */ | 
|  | 861 |  | 
|  | 862 | la = a; | 
|  | 863 | lb = b; | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 864 | return be32_to_cpu(la->hashval) < be32_to_cpu(lb->hashval) ? -1 : | 
|  | 865 | (be32_to_cpu(la->hashval) > be32_to_cpu(lb->hashval) ? 1 : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | } | 
|  | 867 |  | 
|  | 868 | /* | 
|  | 869 | * Convert a V2 leaf directory to a V2 block directory if possible. | 
|  | 870 | */ | 
|  | 871 | int						/* error */ | 
|  | 872 | xfs_dir2_leaf_to_block( | 
|  | 873 | xfs_da_args_t		*args,		/* operation arguments */ | 
|  | 874 | xfs_dabuf_t		*lbp,		/* leaf buffer */ | 
|  | 875 | xfs_dabuf_t		*dbp)		/* data buffer */ | 
|  | 876 | { | 
| Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 877 | __be16			*bestsp;	/* leaf bests table */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 878 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | xfs_dir2_block_tail_t	*btp;		/* block tail */ | 
|  | 880 | xfs_inode_t		*dp;		/* incore directory inode */ | 
|  | 881 | xfs_dir2_data_unused_t	*dup;		/* unused data entry */ | 
|  | 882 | int			error;		/* error return value */ | 
|  | 883 | int			from;		/* leaf from index */ | 
|  | 884 | xfs_dir2_leaf_t		*leaf;		/* leaf structure */ | 
|  | 885 | xfs_dir2_leaf_entry_t	*lep;		/* leaf entry */ | 
|  | 886 | xfs_dir2_leaf_tail_t	*ltp;		/* leaf tail structure */ | 
|  | 887 | xfs_mount_t		*mp;		/* file system mount point */ | 
|  | 888 | int			needlog;	/* need to log data header */ | 
|  | 889 | int			needscan;	/* need to scan for bestfree */ | 
|  | 890 | xfs_dir2_sf_hdr_t	sfh;		/* shortform header */ | 
|  | 891 | int			size;		/* bytes used */ | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 892 | __be16			*tagp;		/* end of entry (tag) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | int			to;		/* block/leaf to index */ | 
|  | 894 | xfs_trans_t		*tp;		/* transaction pointer */ | 
|  | 895 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 896 | trace_xfs_dir2_leaf_to_block(args); | 
|  | 897 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | dp = args->dp; | 
|  | 899 | tp = args->trans; | 
|  | 900 | mp = dp->i_mount; | 
|  | 901 | leaf = lbp->data; | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 902 | ASSERT(leaf->hdr.info.magic == cpu_to_be16(XFS_DIR2_LEAF1_MAGIC)); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 903 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | /* | 
|  | 905 | * If there are data blocks other than the first one, take this | 
|  | 906 | * opportunity to remove trailing empty data blocks that may have | 
|  | 907 | * been left behind during no-space-reservation operations. | 
|  | 908 | * These will show up in the leaf bests table. | 
|  | 909 | */ | 
|  | 910 | while (dp->i_d.di_size > mp->m_dirblksize) { | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 911 | bestsp = xfs_dir2_leaf_bests_p(ltp); | 
| Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 912 | if (be16_to_cpu(bestsp[be32_to_cpu(ltp->bestcount) - 1]) == | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 913 | mp->m_dirblksize - (uint)sizeof(*hdr)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | if ((error = | 
|  | 915 | xfs_dir2_leaf_trim_data(args, lbp, | 
| Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 916 | (xfs_dir2_db_t)(be32_to_cpu(ltp->bestcount) - 1)))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | goto out; | 
|  | 918 | } else { | 
|  | 919 | error = 0; | 
|  | 920 | goto out; | 
|  | 921 | } | 
|  | 922 | } | 
|  | 923 | /* | 
|  | 924 | * Read the data block if we don't already have it, give up if it fails. | 
|  | 925 | */ | 
|  | 926 | if (dbp == NULL && | 
|  | 927 | (error = xfs_da_read_buf(tp, dp, mp->m_dirdatablk, -1, &dbp, | 
|  | 928 | XFS_DATA_FORK))) { | 
|  | 929 | goto out; | 
|  | 930 | } | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 931 | hdr = dbp->data; | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 932 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* | 
|  | 934 | * Size of the "leaf" area in the block. | 
|  | 935 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 936 | size = (uint)sizeof(xfs_dir2_block_tail_t) + | 
| Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 937 | (uint)sizeof(*lep) * (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* | 
|  | 939 | * Look at the last data entry. | 
|  | 940 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 941 | tagp = (__be16 *)((char *)hdr + mp->m_dirblksize) - 1; | 
|  | 942 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | /* | 
|  | 944 | * If it's not free or is too short we can't do it. | 
|  | 945 | */ | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 946 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG || | 
|  | 947 | be16_to_cpu(dup->length) < size) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | error = 0; | 
|  | 949 | goto out; | 
|  | 950 | } | 
|  | 951 | /* | 
|  | 952 | * Start converting it to block form. | 
|  | 953 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 954 | hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | needlog = 1; | 
|  | 956 | needscan = 0; | 
|  | 957 | /* | 
|  | 958 | * Use up the space at the end of the block (blp/btp). | 
|  | 959 | */ | 
|  | 960 | xfs_dir2_data_use_free(tp, dbp, dup, mp->m_dirblksize - size, size, | 
|  | 961 | &needlog, &needscan); | 
|  | 962 | /* | 
|  | 963 | * Initialize the block tail. | 
|  | 964 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 965 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 966 | btp->count = cpu_to_be32(be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | btp->stale = 0; | 
|  | 968 | xfs_dir2_block_log_tail(tp, dbp); | 
|  | 969 | /* | 
|  | 970 | * Initialize the block leaf area.  We compact out stale entries. | 
|  | 971 | */ | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 972 | lep = xfs_dir2_block_leaf_p(btp); | 
| Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 973 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { | 
| Christoph Hellwig | 69ef921 | 2011-07-08 14:36:05 +0200 | [diff] [blame] | 974 | if (leaf->ents[from].address == | 
|  | 975 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | continue; | 
|  | 977 | lep[to++] = leaf->ents[from]; | 
|  | 978 | } | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 979 | ASSERT(to == be32_to_cpu(btp->count)); | 
|  | 980 | xfs_dir2_block_log_leaf(tp, dbp, 0, be32_to_cpu(btp->count) - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* | 
|  | 982 | * Scan the bestfree if we need it and log the data block header. | 
|  | 983 | */ | 
|  | 984 | if (needscan) | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 985 | xfs_dir2_data_freescan(mp, hdr, &needlog); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | if (needlog) | 
|  | 987 | xfs_dir2_data_log_header(tp, dbp); | 
|  | 988 | /* | 
|  | 989 | * Pitch the old leaf block. | 
|  | 990 | */ | 
|  | 991 | error = xfs_da_shrink_inode(args, mp->m_dirleafblk, lbp); | 
|  | 992 | lbp = NULL; | 
|  | 993 | if (error) { | 
|  | 994 | goto out; | 
|  | 995 | } | 
|  | 996 | /* | 
|  | 997 | * Now see if the resulting block can be shrunken to shortform. | 
|  | 998 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 999 | size = xfs_dir2_block_sfsize(dp, hdr, &sfh); | 
|  | 1000 | if (size > XFS_IFORK_DSIZE(dp)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | error = 0; | 
|  | 1002 | goto out; | 
|  | 1003 | } | 
|  | 1004 | return xfs_dir2_block_to_sf(args, dbp, size, &sfh); | 
|  | 1005 | out: | 
|  | 1006 | if (lbp) | 
|  | 1007 | xfs_da_buf_done(lbp); | 
|  | 1008 | if (dbp) | 
|  | 1009 | xfs_da_buf_done(dbp); | 
|  | 1010 | return error; | 
|  | 1011 | } | 
|  | 1012 |  | 
|  | 1013 | /* | 
|  | 1014 | * Convert the shortform directory to block form. | 
|  | 1015 | */ | 
|  | 1016 | int						/* error */ | 
|  | 1017 | xfs_dir2_sf_to_block( | 
|  | 1018 | xfs_da_args_t		*args)		/* operation arguments */ | 
|  | 1019 | { | 
|  | 1020 | xfs_dir2_db_t		blkno;		/* dir-relative block # (0) */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1021 | xfs_dir2_data_hdr_t	*hdr;		/* block header */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | xfs_dir2_leaf_entry_t	*blp;		/* block leaf entries */ | 
|  | 1023 | xfs_dabuf_t		*bp;		/* block buffer */ | 
|  | 1024 | xfs_dir2_block_tail_t	*btp;		/* block tail pointer */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | xfs_dir2_data_entry_t	*dep;		/* data entry pointer */ | 
|  | 1026 | xfs_inode_t		*dp;		/* incore directory inode */ | 
|  | 1027 | int			dummy;		/* trash */ | 
|  | 1028 | xfs_dir2_data_unused_t	*dup;		/* unused entry pointer */ | 
|  | 1029 | int			endoffset;	/* end of data objects */ | 
|  | 1030 | int			error;		/* error return value */ | 
|  | 1031 | int			i;		/* index */ | 
|  | 1032 | xfs_mount_t		*mp;		/* filesystem mount point */ | 
|  | 1033 | int			needlog;	/* need to log block header */ | 
|  | 1034 | int			needscan;	/* need to scan block freespc */ | 
|  | 1035 | int			newoffset;	/* offset from current entry */ | 
|  | 1036 | int			offset;		/* target block offset */ | 
|  | 1037 | xfs_dir2_sf_entry_t	*sfep;		/* sf entry pointer */ | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1038 | xfs_dir2_sf_hdr_t	*oldsfp;	/* old shortform header  */ | 
|  | 1039 | xfs_dir2_sf_hdr_t	*sfp;		/* shortform header  */ | 
| Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1040 | __be16			*tagp;		/* end of data entry */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | xfs_trans_t		*tp;		/* transaction pointer */ | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1042 | struct xfs_name		name; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 |  | 
| Christoph Hellwig | 0b1b213 | 2009-12-14 23:14:59 +0000 | [diff] [blame] | 1044 | trace_xfs_dir2_sf_to_block(args); | 
|  | 1045 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | dp = args->dp; | 
|  | 1047 | tp = args->trans; | 
|  | 1048 | mp = dp->i_mount; | 
|  | 1049 | ASSERT(dp->i_df.if_flags & XFS_IFINLINE); | 
|  | 1050 | /* | 
|  | 1051 | * Bomb out if the shortform directory is way too short. | 
|  | 1052 | */ | 
|  | 1053 | if (dp->i_d.di_size < offsetof(xfs_dir2_sf_hdr_t, parent)) { | 
|  | 1054 | ASSERT(XFS_FORCED_SHUTDOWN(mp)); | 
|  | 1055 | return XFS_ERROR(EIO); | 
|  | 1056 | } | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1057 |  | 
|  | 1058 | oldsfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data; | 
|  | 1059 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | ASSERT(dp->i_df.if_bytes == dp->i_d.di_size); | 
|  | 1061 | ASSERT(dp->i_df.if_u1.if_data != NULL); | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1062 | ASSERT(dp->i_d.di_size >= xfs_dir2_sf_hdr_size(oldsfp->i8count)); | 
|  | 1063 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | /* | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1065 | * Copy the directory into a temporary buffer. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | * Then pitch the incore inode data so we can make extents. | 
|  | 1067 | */ | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1068 | sfp = kmem_alloc(dp->i_df.if_bytes, KM_SLEEP); | 
|  | 1069 | memcpy(sfp, oldsfp, dp->i_df.if_bytes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 |  | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1071 | xfs_idata_realloc(dp, -dp->i_df.if_bytes, XFS_DATA_FORK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | dp->i_d.di_size = 0; | 
|  | 1073 | xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE); | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1074 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | /* | 
|  | 1076 | * Add block 0 to the inode. | 
|  | 1077 | */ | 
|  | 1078 | error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, &blkno); | 
|  | 1079 | if (error) { | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1080 | kmem_free(sfp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | return error; | 
|  | 1082 | } | 
|  | 1083 | /* | 
|  | 1084 | * Initialize the data block. | 
|  | 1085 | */ | 
|  | 1086 | error = xfs_dir2_data_init(args, blkno, &bp); | 
|  | 1087 | if (error) { | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1088 | kmem_free(sfp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | return error; | 
|  | 1090 | } | 
| Christoph Hellwig | a64b041 | 2011-07-08 14:35:32 +0200 | [diff] [blame] | 1091 | hdr = bp->data; | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1092 | hdr->magic = cpu_to_be32(XFS_DIR2_BLOCK_MAGIC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | /* | 
|  | 1094 | * Compute size of block "tail" area. | 
|  | 1095 | */ | 
|  | 1096 | i = (uint)sizeof(*btp) + | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1097 | (sfp->count + 2) * (uint)sizeof(xfs_dir2_leaf_entry_t); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | /* | 
|  | 1099 | * The whole thing is initialized to free by the init routine. | 
|  | 1100 | * Say we're using the leaf and tail area. | 
|  | 1101 | */ | 
| Christoph Hellwig | a64b041 | 2011-07-08 14:35:32 +0200 | [diff] [blame] | 1102 | dup = (xfs_dir2_data_unused_t *)(hdr + 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | needlog = needscan = 0; | 
|  | 1104 | xfs_dir2_data_use_free(tp, bp, dup, mp->m_dirblksize - i, i, &needlog, | 
|  | 1105 | &needscan); | 
|  | 1106 | ASSERT(needscan == 0); | 
|  | 1107 | /* | 
|  | 1108 | * Fill in the tail. | 
|  | 1109 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1110 | btp = xfs_dir2_block_tail_p(mp, hdr); | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1111 | btp->count = cpu_to_be32(sfp->count + 2);	/* ., .. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | btp->stale = 0; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1113 | blp = xfs_dir2_block_leaf_p(btp); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1114 | endoffset = (uint)((char *)blp - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | /* | 
|  | 1116 | * Remove the freespace, we'll manage it. | 
|  | 1117 | */ | 
|  | 1118 | xfs_dir2_data_use_free(tp, bp, dup, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1119 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr), | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1120 | be16_to_cpu(dup->length), &needlog, &needscan); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | /* | 
|  | 1122 | * Create entry for . | 
|  | 1123 | */ | 
|  | 1124 | dep = (xfs_dir2_data_entry_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1125 | ((char *)hdr + XFS_DIR2_DATA_DOT_OFFSET); | 
| Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1126 | dep->inumber = cpu_to_be64(dp->i_ino); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | dep->namelen = 1; | 
|  | 1128 | dep->name[0] = '.'; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1129 | tagp = xfs_dir2_data_entry_tag_p(dep); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1130 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | xfs_dir2_data_log_entry(tp, bp, dep); | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1132 | blp[0].hashval = cpu_to_be32(xfs_dir_hash_dot); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1133 | blp[0].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1134 | (char *)dep - (char *)hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | /* | 
|  | 1136 | * Create entry for .. | 
|  | 1137 | */ | 
|  | 1138 | dep = (xfs_dir2_data_entry_t *) | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1139 | ((char *)hdr + XFS_DIR2_DATA_DOTDOT_OFFSET); | 
| Christoph Hellwig | 8bc3878 | 2011-07-08 14:35:03 +0200 | [diff] [blame] | 1140 | dep->inumber = cpu_to_be64(xfs_dir2_sf_get_parent_ino(sfp)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | dep->namelen = 2; | 
|  | 1142 | dep->name[0] = dep->name[1] = '.'; | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1143 | tagp = xfs_dir2_data_entry_tag_p(dep); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1144 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | xfs_dir2_data_log_entry(tp, bp, dep); | 
| Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1146 | blp[1].hashval = cpu_to_be32(xfs_dir_hash_dotdot); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1147 | blp[1].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1148 | (char *)dep - (char *)hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | offset = XFS_DIR2_DATA_FIRST_OFFSET; | 
|  | 1150 | /* | 
|  | 1151 | * Loop over existing entries, stuff them in. | 
|  | 1152 | */ | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1153 | i = 0; | 
|  | 1154 | if (!sfp->count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | sfep = NULL; | 
|  | 1156 | else | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1157 | sfep = xfs_dir2_sf_firstentry(sfp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | /* | 
|  | 1159 | * Need to preserve the existing offset values in the sf directory. | 
|  | 1160 | * Insert holes (unused entries) where necessary. | 
|  | 1161 | */ | 
|  | 1162 | while (offset < endoffset) { | 
|  | 1163 | /* | 
|  | 1164 | * sfep is null when we reach the end of the list. | 
|  | 1165 | */ | 
|  | 1166 | if (sfep == NULL) | 
|  | 1167 | newoffset = endoffset; | 
|  | 1168 | else | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1169 | newoffset = xfs_dir2_sf_get_offset(sfep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | /* | 
|  | 1171 | * There should be a hole here, make one. | 
|  | 1172 | */ | 
|  | 1173 | if (offset < newoffset) { | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1174 | dup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1175 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); | 
|  | 1176 | dup->length = cpu_to_be16(newoffset - offset); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1177 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16( | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1178 | ((char *)dup - (char *)hdr)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | xfs_dir2_data_log_unused(tp, bp, dup); | 
| Christoph Hellwig | c2066e2 | 2011-07-08 14:35:38 +0200 | [diff] [blame] | 1180 | xfs_dir2_data_freeinsert(hdr, dup, &dummy); | 
| Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1181 | offset += be16_to_cpu(dup->length); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | continue; | 
|  | 1183 | } | 
|  | 1184 | /* | 
|  | 1185 | * Copy a real entry. | 
|  | 1186 | */ | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1187 | dep = (xfs_dir2_data_entry_t *)((char *)hdr + newoffset); | 
| Christoph Hellwig | 8bc3878 | 2011-07-08 14:35:03 +0200 | [diff] [blame] | 1188 | dep->inumber = cpu_to_be64(xfs_dir2_sfe_get_ino(sfp, sfep)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | dep->namelen = sfep->namelen; | 
|  | 1190 | memcpy(dep->name, sfep->name, dep->namelen); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1191 | tagp = xfs_dir2_data_entry_tag_p(dep); | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1192 | *tagp = cpu_to_be16((char *)dep - (char *)hdr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | xfs_dir2_data_log_entry(tp, bp, dep); | 
| Barry Naujok | 5163f95 | 2008-05-21 16:41:01 +1000 | [diff] [blame] | 1194 | name.name = sfep->name; | 
|  | 1195 | name.len = sfep->namelen; | 
|  | 1196 | blp[2 + i].hashval = cpu_to_be32(mp->m_dirnameops-> | 
|  | 1197 | hashname(&name)); | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1198 | blp[2 + i].address = cpu_to_be32(xfs_dir2_byte_to_dataptr(mp, | 
| Christoph Hellwig | 4f6ae1a | 2011-07-08 14:35:27 +0200 | [diff] [blame] | 1199 | (char *)dep - (char *)hdr)); | 
|  | 1200 | offset = (int)((char *)(tagp + 1) - (char *)hdr); | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1201 | if (++i == sfp->count) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | sfep = NULL; | 
|  | 1203 | else | 
| Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1204 | sfep = xfs_dir2_sf_nextentry(sfp, sfep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } | 
|  | 1206 | /* Done with the temporary buffer */ | 
| Christoph Hellwig | ac8ba50 | 2011-07-08 14:35:13 +0200 | [diff] [blame] | 1207 | kmem_free(sfp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | /* | 
|  | 1209 | * Sort the leaf entries by hash value. | 
|  | 1210 | */ | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1211 | xfs_sort(blp, be32_to_cpu(btp->count), sizeof(*blp), xfs_dir2_block_sort); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | /* | 
|  | 1213 | * Log the leaf entry area and tail. | 
|  | 1214 | * Already logged the header in data_init, ignore needlog. | 
|  | 1215 | */ | 
|  | 1216 | ASSERT(needscan == 0); | 
| Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 1217 | xfs_dir2_block_log_leaf(tp, bp, 0, be32_to_cpu(btp->count) - 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | xfs_dir2_block_log_tail(tp, bp); | 
|  | 1219 | xfs_dir2_data_check(dp, bp); | 
|  | 1220 | xfs_da_buf_done(bp); | 
|  | 1221 | return 0; | 
|  | 1222 | } |